diff -urN popa3d-0.6.4.1.orig/Makefile popa3d-0.6.4.1/Makefile --- popa3d-0.6.4.1.orig/Makefile Mon Nov 17 00:43:35 2003 +++ popa3d-0.6.4.1/Makefile Sat Dec 25 10:52:22 2004 @@ -3,7 +3,7 @@ RM = rm -f MKDIR = mkdir -p INSTALL = install -c -CFLAGS = -Wall -O2 -fomit-frame-pointer +CFLAGS = -Wall -O2 -fomit-frame-pointer -DPOPB4SMTP -DVALIDTIME=600 # You may use OpenSSL's MD5 routines instead of the ones supplied here #CFLAGS += -DHAVE_OPENSSL LDFLAGS = -s @@ -22,6 +22,8 @@ #LIBS += -lnsl # OpenSSL (-DHAVE_OPENSSL) #LIBS += -lcrypto +# Berkeley DB 4 - for POP-before-SMTP +LIBS += -ldb-4.2 DESTDIR = PREFIX = /usr/local @@ -37,7 +39,7 @@ auth_passwd.o auth_shadow.o auth_pam.o \ pop_root.o pop_auth.o pop_trans.o \ protocol.o database.o mailbox.o \ - misc.o \ + misc.o pop_pbs.o \ md5/md5.o all: $(PROJ) diff -urN popa3d-0.6.4.1.orig/README-POPB4SMTP popa3d-0.6.4.1/README-POPB4SMTP --- popa3d-0.6.4.1.orig/README-POPB4SMTP Thu Jan 1 00:00:00 1970 +++ popa3d-0.6.4.1/README-POPB4SMTP Sat Dec 25 11:12:28 2004 @@ -0,0 +1,52 @@ +Code Monkeyboy has updated this patch from popa3d +0.4 and Berkeley DB 1 to popa3d 0.6.4 and DB 4. What follows is Garry's +original README file for his popa3d 0.4 patch. + + popa3d patch for POP-before-SMTP and SMTP-after-POP + + Garry Glendown / Dec. 12th 2000 + +On the 'net there are a couple of solutions to allow for POP-before-SMTP +authentication in order to allow for relaying of mails. Anyway, the +solutions I found didn't really apeal to me, so I hacked popa3d a bit, +which we already used on one of our machines to serve mail to dialup +customers. + + Prerequisites + +This patch supplies data to sendmail to allow for certain IPs to use it +as a relaying host. In order to use with your sendmail installation, get +the popauth-hack (http://www.sendmail.org/~ca/email/rules/popauth.m4) +and install it by adding "HACK(`popauth')" to you .mc-file. + + Installing + +The patch - enabled through the POPB4SMTP-define in the Makefile - +accesses the file "/etc/mail/popauth.db" (create with "makemap hash +/etc/mail/popauth . + +G.Glendown / Dec 15th 2000 diff -urN popa3d-0.6.4.1.orig/pop_auth.c popa3d-0.6.4.1/pop_auth.c --- popa3d-0.6.4.1.orig/pop_auth.c Mon Sep 9 11:07:48 2002 +++ popa3d-0.6.4.1/pop_auth.c Sat Dec 25 10:50:23 2004 @@ -14,6 +14,9 @@ #if POP_VIRTUAL #include "virtual.h" #endif +#ifdef POPB4SMTP +char *client_addr(int fd); +#endif static char *pop_user, *pop_pass; @@ -75,15 +78,17 @@ #if POP_VIRTUAL if (virtual_domain) { syslog(result == AUTH_OK ? SYSLOG_PRI_LO : SYSLOG_PRI_HI, - "Authentication %s for %s@%s", + "Authentication %s for %s@%s from %s", result == AUTH_OK ? "passed" : "failed", user ? user : "UNKNOWN USER", - virtual_domain); + virtual_domain, + client_addr(1) ); return; } #endif syslog(result == AUTH_OK ? SYSLOG_PRI_LO : SYSLOG_PRI_HI, - "Authentication %s for %s", + "Authentication %s for %s from %s", result == AUTH_OK ? "passed" : "failed", - user ? user : "UNKNOWN USER"); + user ? user : "UNKNOWN USER", + client_addr(1) ); } diff -urN popa3d-0.6.4.1.orig/pop_pbs.c popa3d-0.6.4.1/pop_pbs.c --- popa3d-0.6.4.1.orig/pop_pbs.c Thu Jan 1 00:00:00 1970 +++ popa3d-0.6.4.1/pop_pbs.c Sat Dec 25 10:50:23 2004 @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include + +char addr_buf[256]; +char *client_addr(int fd) +{ + struct sockaddr sa; + struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa); + int length = sizeof(sa); + + strcpy(addr_buf,"0.0.0.0"); + + if (fd == -1) { + return addr_buf; + } + + if (getpeername(fd, &sa, &length) < 0) { + return addr_buf; + } + + strncpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr),64); + + return addr_buf; +} + +/*===========================================================================*/ +/* sem.c, Markus Franzke, 14-Feb-2000 */ +/* compressed Dec 2000 / G.Glendown */ +/*===========================================================================*/ + +#include +#include +#include +#include + +/*---------------------------------------------------------------------------*/ +/*---------------------------------------------------------------------------*/ +int sem_attach(key_t sem_key) { + + int sem_id = -1; + struct sembuf sem_ops[1]; + + sem_ops[0].sem_num = (short) 0; + sem_ops[0].sem_op = (short) 1; + sem_ops[0].sem_flg = 0; /* NICHT: SEM_UNDO */ + + if ((sem_id = semget(sem_key, 1, IPC_CREAT|IPC_EXCL|0660)) >= 0) { + semop(sem_id, sem_ops, 1); + } else if (errno == EEXIST) { + sem_id = semget(sem_key, 0, 0); + } + + return sem_id; + +} + +/*---------------------------------------------------------------------------*/ +/*---------------------------------------------------------------------------*/ +int sem_enter(int sem_id) { + + struct sembuf sem_ops[1]; + + sem_ops[0].sem_num = (short) 0; + sem_ops[0].sem_op = (short) -1; + sem_ops[0].sem_flg = SEM_UNDO ; + + + while (semop(sem_id, sem_ops, 1) < 0) { + if (errno == EINTR) { + continue; + } else if (errno == EAGAIN) { + return 1; + } else { + return 2; + } + } + + return 0; + +} + +/*---------------------------------------------------------------------------*/ +/*---------------------------------------------------------------------------*/ +void sem_leave(int sem_id) { + + struct sembuf sem_ops[1]; + + sem_ops[0].sem_num = (short) 0; + sem_ops[0].sem_op = (short) 1; + sem_ops[0].sem_flg = SEM_UNDO; + + while (semop(sem_id, sem_ops, 1) < 0) { + if (errno == EAGAIN) { + continue; + } else if (errno == EAGAIN) { + continue; + } else { + break; + } + } +} + diff -urN popa3d-0.6.4.1.orig/pop_root.c popa3d-0.6.4.1/pop_root.c --- popa3d-0.6.4.1.orig/pop_root.c Thu Mar 21 20:15:19 2002 +++ popa3d-0.6.4.1/pop_root.c Sat Dec 25 10:50:23 2004 @@ -25,6 +25,17 @@ #include #include +#ifdef POPB4SMTP +#include +#include +#include +#include +char * client_addr(int); +int sem_attach(key_t sem_key) ; +int sem_enter(int sem_id) ; +void sem_leave(int sem_id) ; +#endif + #include "params.h" #include "protocol.h" #include "pop_auth.h" @@ -108,6 +119,13 @@ static char auth[AUTH_BUFFER_SIZE + 2]; char *pass; struct passwd *pw; +#ifdef POPB4SMTP + DB *db; + DBT key,data; + char ts[16]; + int ret,tv,sem; + char f2[256]; +#endif known = 0; #if POP_VIRTUAL @@ -154,6 +172,35 @@ #if VIRTUAL_ONLY if (!virtual_domain) return AUTH_FAILED; +#endif + +#ifdef POPB4SMTP + sem=sem_attach(0x50413453); + sem_enter(sem); + db=dbopen("/etc/mail/popauth4.db",O_RDWR,0664,DB_HASH,0 ); + key.data=client_addr(1); + key.size=strlen(key.data); + sprintf(ts,"%d",(int)time(0)); + data.data=ts; + data.size=strlen(ts); + db->put(db,&key,&data,0); + db->sync(db,0); +delete: + ret=db->seq(db,&key,&data,R_FIRST); + while (!ret) { + data.size=data.size<256?data.size:255; + strncpy(f2,data.data,data.size); + f2[data.size]=0; + tv=atoi(f2); + if ((tv+VALIDTIME)del(db,&key,0); + db->sync(db,0); + goto delete; + } + ret=db->seq(db,&key,&data,R_NEXT); + } + db->close(db); + sem_leave(sem); #endif if (set_user(pw)) return AUTH_FAILED;