diff -ruBbd courier-imap-3.0.8-orig/authlib/authdaemon.c courier-imap-3.0.8/authlib/authdaemon.c --- courier-imap-3.0.8-orig/authlib/authdaemon.c 2004-11-26 15:39:16.826827212 -0500 +++ courier-imap-3.0.8/authlib/authdaemon.c 2004-11-26 15:41:25.139137231 -0500 @@ -110,9 +110,13 @@ char tbuf[NUMBUFSIZE]; size_t l=strlen(service)+strlen(authtype)+strlen(authdata)+1; char *n=libmail_str_size_t(l, tbuf); - char *buf=malloc(strlen(n)+l+20); int rc; + /* we need to increase the size of the buffer for the ip */ + int nint = atoi(n) + strlen(getenv("TCPREMOTEIP")); + sprintf(n, "%i", nint); + char *buf=malloc(strlen(n)+l+40); + struct authdaemon_info ai; if (!buf) @@ -124,6 +128,7 @@ strcat(strcat(strcpy(buf, "AUTH "), n), "\n"); strcat(strcat(buf, service), "\n"); strcat(strcat(buf, authtype), "\n"); + strcat(strcat(buf, getenv("TCPREMOTEIP")), "\n"); strcat(buf, authdata); ai.username=NULL; diff -ruBbd courier-imap-3.0.8-orig/authlib/authdaemond.c courier-imap-3.0.8/authlib/authdaemond.c --- courier-imap-3.0.8-orig/authlib/authdaemond.c 2004-11-26 15:39:16.819827140 -0500 +++ courier-imap-3.0.8/authlib/authdaemond.c 2004-11-26 15:41:25.140137241 -0500 @@ -557,6 +557,8 @@ char *authtype; int i; char *pp; +char *rip; +char *env_buf; service=p; if ((p=strchr(p, '\n')) == 0) return; @@ -564,6 +566,14 @@ authtype=p; if ((p=strchr(p, '\n')) == 0) return; *p++=0; + rip=p; + if ((p=strchr(p, '\n')) == 0) return; + *p++=0; + + /* now that we have the remote ip, set the environment variable */ + env_buf = malloc(sizeof("TCPREMOTEIP=")+strlen(rip)+1); + sprintf(env_buf,"TCPREMOTEIP=%s", rip); + putenv(env_buf); pp=malloc(strlen(p)+1); if (!pp) @@ -571,6 +581,7 @@ perror("CRIT: malloc() failed"); return; } + pp = strcpy(pp, p); dprintf("received auth request, service=%s, authtype=%s", service, authtype); for (authbegin(); (i=authnext()) >= 0; ) @@ -586,18 +597,21 @@ dprintf("%s: ACCEPT, username %s", modname, q); free(q); free(pp); + free(env_buf); return; } if (errno != EPERM) { dprintf("%s: TEMPFAIL - no more modules will be tried", modname); free(pp); + free(env_buf); return; /* Temporary error */ } dprintf("%s: REJECT - try next module", modname); } dprintf("FAIL, all modules rejected"); writeauth(fd, "FAIL\n", 5); + free(env_buf); free(pp); } diff -ruBbd courier-imap-3.0.8-orig/authlib/authdaemond.in courier-imap-3.0.8/authlib/authdaemond.in --- courier-imap-3.0.8-orig/authlib/authdaemond.in 2004-11-26 15:39:16.826827212 -0500 +++ courier-imap-3.0.8/authlib/authdaemond.in 2004-11-26 15:41:25.140137241 -0500 @@ -4,23 +4,13 @@ sbindir=@sbindir@ DIR=`dirname $0` -AUTHDAEMOND=authdaemond.plain - -if test -x $DIR/authdaemond.ldap -then - AUTHDAEMOND=authdaemond.ldap -fi +AUTHDAEMOND=authdaemond.mysql if test -x $DIR/authdaemond.mysql then AUTHDAEMOND=authdaemond.mysql fi -if test -x $DIR/authdaemond.pgsql -then - AUTHDAEMOND=authdaemond.pgsql -fi - . @authdaemonrc@ if test "$version" != "" then diff -ruBbd courier-imap-3.0.8-orig/authlib/authmysqllib.c courier-imap-3.0.8/authlib/authmysqllib.c --- courier-imap-3.0.8-orig/authlib/authmysqllib.c 2004-11-26 15:39:16.818827130 -0500 +++ courier-imap-3.0.8/authlib/authmysqllib.c 2004-11-26 15:41:25.141137251 -0500 @@ -38,6 +38,20 @@ /* siefca@pld.org.pl */ typedef int (*parsefunc)(const char *, size_t, void *); +char *ip6to4(char * ip6) { + char *ip4; + + ip4=strrchr(ip6, ':'); + + if(ip4 != NULL) { + return ip4+1; + } + + /* Probably an ipv4 */ + return ip6; +} + + static const char *read_env(const char *env) { static char *mysqlauth=0; @@ -638,6 +652,8 @@ *quota_field =NULL, *options_field =NULL, *where_clause =NULL, + *post_query_last_read =NULL, + *post_query_pop_before_smtp =NULL, *select_clause =NULL; /* siefca@pld.org.pl */ static const char query[]= @@ -698,6 +714,9 @@ options_field=read_env("MYSQL_AUXOPTIONS_FIELD"); if (!options_field) options_field="\"\""; + post_query_last_read=read_env("MYSQL_QUERY_LASTREAD"); + post_query_pop_before_smtp=read_env("MYSQL_QUERY_POPB4SMTP"); + where_clause=read_env("MYSQL_WHERE_CLAUSE"); if (!where_clause) where_clause = ""; } @@ -826,7 +845,26 @@ ui.fullname=strdup(row[8]); if (num_fields > 9 && row[9] && row[9][0]) ui.options=strdup(row[9]); + + /* POST AUTH SQL: update the 'mail_last_read' table */ + if (post_query_last_read) { + querybuf = malloc(strlen(post_query_last_read) + strlen(username)); + sprintf(querybuf, post_query_last_read, username); + dprintf("SQL: mail last read: %s", querybuf); + mysql_query(mysql, querybuf); + free(querybuf); } + + /* POST AUTH SQL: update the 'mail_pop_before_smtp' table */ + if (post_query_pop_before_smtp) { + querybuf = malloc(strlen(post_query_pop_before_smtp) + strlen(get_localpart(username)) + strlen(get_domain(username, NULL)) + 20); + sprintf(querybuf, post_query_pop_before_smtp, ip6to4(getenv("TCPREMOTEIP")), get_localpart(username), get_domain(username, NULL)); + dprintf("SQL: IMAP before SMTP: %s", querybuf); + mysql_query(mysql, querybuf); + free(querybuf); + } + } + else { dprintf("zero rows returned"); diff -ruBbd courier-imap-3.0.8-orig/authlib/authmysqlrc courier-imap-3.0.8/authlib/authmysqlrc --- courier-imap-3.0.8-orig/authlib/authmysqlrc 2004-11-26 15:39:16.827827222 -0500 +++ courier-imap-3.0.8/authlib/authmysqlrc 2004-11-26 15:41:25.142137261 -0500 @@ -168,6 +168,10 @@ # # MYSQL_WHERE_CLAUSE server='mailhost.example.com' +# Additional post-login configuration parameters +# MYSQL_QUERY_LASTREAD REPLACE INTO mail_last_read (address, tstamp) VALUES ('%s', NULL) +# MYSQL_QUERY_POPB4SMTP REPLACE INTO mail_pop_before_smtp (ip, expires, email_local, email_domain) VALUES (INET_ATON('%s'), NOW() + 1800, '%s', '%s') + ##NAME: MYSQL_SELECT_CLAUSE:0 # # (EXPERIMENTAL)