diff -NurpP --minimal courier-authlib-0.56/authdaemon.c courier-authlib-0.56-hack.v02/authdaemon.c --- courier-authlib-0.56/authdaemon.c 2004-11-21 04:14:58.000000000 +0100 +++ courier-authlib-0.56-hack.v02/authdaemon.c 2005-07-23 08:21:33.000000000 +0200 @@ -42,9 +42,12 @@ int auth_generic(const char *service, 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 nint = atoi(n) + strlen(getenv("TCPREMOTEIP")) + 1; + char *buf; int rc; + sprintf(n, "%i", nint); + buf = malloc(strlen(n)+l+40); courier_authdebug_login_init(); if (!buf) @@ -53,6 +56,7 @@ int auth_generic(const char *service, 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); rc=authdaemondo(buf, callback_func, callback_arg); diff -NurpP --minimal courier-authlib-0.56/authdaemond.c courier-authlib-0.56-hack.v02/authdaemond.c --- courier-authlib-0.56/authdaemond.c 2005-02-20 05:41:20.000000000 +0100 +++ courier-authlib-0.56-hack.v02/authdaemond.c 2005-07-23 08:24:13.000000000 +0200 @@ -705,6 +705,8 @@ static void auth(int fd, char *p) { char *service; char *authtype; + char *rip; + char *env_buf; char *pp; struct authstaticinfolist *l; @@ -714,6 +716,14 @@ static void auth(int fd, char *p) 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) @@ -722,6 +732,8 @@ static void auth(int fd, char *p) return; } + pp = strcpy(pp, p); + DPRINTF("received auth request, service=%s, authtype=%s", service, authtype); for (l=modulelist; l; l=l->next) { @@ -738,6 +750,7 @@ static void auth(int fd, char *p) if (rc == 0) { free(pp); + free(env_buf); return; } @@ -745,6 +758,7 @@ static void auth(int fd, char *p) { 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); @@ -752,6 +766,7 @@ static void auth(int fd, char *p) DPRINTF("FAIL, all modules rejected"); writeauth(fd, "FAIL\n", 5); free(pp); + free(env_buf); } static void idlefunc() diff -NurpP --minimal courier-authlib-0.56/authmysqllib.c courier-authlib-0.56-hack.v02/authmysqllib.c --- courier-authlib-0.56/authmysqllib.c 2005-02-20 05:41:20.000000000 +0100 +++ courier-authlib-0.56-hack.v02/authmysqllib.c 2005-07-23 08:37:51.000000000 +0200 @@ -40,6 +40,19 @@ struct var_data { /* 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; @@ -630,6 +643,8 @@ int num_fields; char *endp; const char *select_clause; /* siefca@pld.org.pl */ +const char *post_query_last_read = NULL, + *post_query_pop_before_smtp = NULL; static const char query[]= "SELECT %s, %s, %s, %s, %s, %s, %s, %s, %s, %s FROM %s WHERE %s = \""; @@ -639,6 +654,9 @@ static const char query[]= initui(); select_clause=read_env("MYSQL_SELECT_CLAUSE"); + post_query_last_read=read_env("MYSQL_QUERY_LASTREAD"); + post_query_pop_before_smtp=read_env("MYSQL_QUERY_POPB4SMTP"); + defdomain=read_env("DEFAULT_DOMAIN"); if (!defdomain) defdomain=""; @@ -837,6 +855,24 @@ static const char query[]= 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 { diff -NurpP --minimal courier-authlib-0.56/authmysqlrc courier-authlib-0.56-hack.v02/authmysqlrc --- courier-authlib-0.56/authmysqlrc 2004-11-14 03:58:16.000000000 +0100 +++ courier-authlib-0.56-hack.v02/authmysqlrc 2005-07-23 06:50:53.000000000 +0200 @@ -264,3 +264,13 @@ MYSQL_NAME_FIELD name # WHERE local_part='$(local_part)' \ # AND domain_name='$(domain)' # + +##NAME: MYSQL_QUERY_LASTREAD:0 +# +# MYSQL_QUERY_LASTREAD REPLACE INTO mail_last_read (address, tstamp) VALUES ('%s', NULL) + +##NAME: MYSQL_QUERY_POPB4SMTP:0 +# +# MYSQL_QUERY_POPB4SMTP REPLACE INTO mail_pop_before_smtp (ip, expires, email_local, email_domain) VALUES (INET_ATON('%s'), NOW() + 1800, '%s', '%s') + +