[squid-users] Authenticating squid with pop3

From: Uilton O. Dutra <uilton@dont-contact.us>
Date: Tue, 14 Sep 2004 17:26:06 -0300

Hi folks!

I wrote a program to authenticate Squid with pop3.
It works fine but I'm geting this messages:

2004/09/14 17:09:00| Too few basicauthenticator processes are
running2004/09/14 17:09:00| Starting new helpers
2004/09/14 17:09:00| helperOpenServers: Starting 5 'pop3auth' processes
2004/09/14 17:10:07| WARNING: basicauthenticator #4 (FD 32) exited
2004/09/14 17:12:11| WARNING: basicauthenticator #5 (FD 33) exited
2004/09/14 17:12:14| WARNING: basicauthenticator #1 (FD 19) exited
2004/09/14 17:12:27| WARNING: basicauthenticator #2 (FD 24) exited

And when many users access proxy, they need to supply password many
times until login. Any idea?

Best regards!

Uílton O. Dutra
Mail: uilton@schmitt.com.br
Web: http://uilton.culture-exchange.org

Clarity of mind means clarity of passion, too;
this is why a great and clear mind loves ardently and
sees distinctly what it loves. - Pascal

/*
   POP3 Authentication Module for Squid Cache
   Author: Uílton O. Dutra <uilton@schmitt.com.br>
           http://uilton.culture-exchange.org

   Based on P3C from Pedro Varangot <peter-vodka@gmx.net>
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <netdb.h>

#define BUFF 1024
#define BUFF2 1024

int connectpop(struct sockaddr_in *, int);
int getdata(int, char *, int);
int senddata(int, char *, int);
int counthe(char *, int, char);
int loguser(char *, char *);
int logout(void);

char buff[BUFF];
char buff2[BUFF2];
int sd;

char buf[256];
char *p;

int main(int argc, char **argv)
{
        char ipserv[15];
        int nmess;
        int tam, i;
        struct sockaddr_in sockaddrs;
        struct hostent *hent = NULL;

        if( argc < 2) {
            printf("./pop3auth server port\n");
            exit(1);
        }

        if( (hent = gethostbyname(argv[1])) == NULL) {
                printf("Couldnt resolve [%s]\n", argv[1]);
                herror(NULL);
                exit(1);
        }

        sockaddrs.sin_port = htons(atoi(argv[2]));
        sockaddrs.sin_addr.s_addr = *(long *)hent->h_addr;
        sockaddrs.sin_family = AF_INET;

        sd = connectpop(&sockaddrs, sizeof(sockaddrs));

        while (1) {
              if (fgets(buf, 256, stdin) == NULL)
                 break;
              if ((p = strchr(buf, '\n')) != NULL)
                 *p = '\0';
              if ((p = strchr(buf, ' ')) == NULL) {
                 (void) printf("ERR\n");
                 continue;
              }
              *p++ = '\0';

              loguser(buf, p);

              logout();
              break;
        }
        exit(0);
}

int connectpop(struct sockaddr_in *server, int tam)
{
        int fd, err;
                
        fd = socket(PF_INET, SOCK_STREAM, 0);
        
        if (fd == -1) {
                perror("socket");
                exit(1);
        }
                
        if((connect(fd, server, tam))==-1) {
                perror("connect");
                exit(2);
        }
        
        return fd;
}

int senddata(int fd, char *buff, int tam)
{
        
        int ret;
        
        ret = send(fd, buff, tam, 0);
        
        if(ret == -1) {
                perror("send");
                exit(1);
        }
        
        return ret;
}

int getdata (int fd, char *buffer, int tam)
{

        int ret, ret2;
        char tbuf[tam];
        
        ret = recv(fd, buffer, tam, 0);

        if(ret == -1) {
                perror("recv");
                exit(1);
        }
       
        return ret;
}

int loguser(char *username, char *password)
{
        bzero(buff, BUFF);
        getdata(sd, buff, BUFF);

        bzero(buff2, BUFF2);
        strcpy(buff2, "USER ");
        strcat(buff2, username);
        strcat(buff2, "\r\n\0");
        senddata(sd, buff2, counthe(buff2, BUFF2, '\0'));

        bzero(buff, BUFF);
        getdata(sd, buff, BUFF);

        bzero(buff2, BUFF2);
        strcpy(buff2, "PASS ");
        strcat(buff2, password);
        strcat(buff2, "\r\n\0");
        senddata(sd, buff2, counthe(buff2, BUFF2, '\0'));

        bzero(buff, BUFF);
        getdata(sd, buff, BUFF);

        if(strncmp(buff,"+OK",3)==0) {
            printf("OK\n");
        } else {
            printf("ERR\n");
        }
        
        return 0;
}

int logout(void)
{
        char *msgr;
        
        senddata(sd, "QUIT\r\n", 6);
        msgr = getl(sd);
        
        if (*(msgr+1) == '+') return 0;
        printf("Error quitting, program unable to log out op pop3 server\n");
        return 1;
}

int counthe(char *str, int max, char lim)
{
        int count, i;

        if(str == NULL) return -1;
        
        for(i=0;i<max;i++) {
                if (*(str + i) == lim) {
                        count = i;
                        break;
                 }
        }
        
        return count;
}

char *getl(int sd)
{

        char *dbuff;
        int nbuff = 0;

        dbuff = malloc(sizeof(char));

        while(*((dbuff)+nbuff) != '\n') {
                nbuff += 1;
                dbuff = realloc(dbuff, sizeof(char) * (nbuff + 1));
                getdata(sd, (dbuff+nbuff), 1);
        }

        dbuff = realloc(dbuff, sizeof(char) * (nbuff + 2));
        *(((dbuff)+nbuff)+1) = '\0';

        return dbuff;
}
Received on Tue Sep 14 2004 - 14:26:11 MDT

This archive was generated by hypermail pre-2.1.9 : Fri Oct 01 2004 - 12:00:02 MDT