diff -burp qmail-1.03.orig/qmail-smtpd.c qmail-1.03.logging/qmail-smtpd.c --- qmail-1.03.orig/qmail-smtpd.c 2003-09-11 20:31:52.000000000 -0500 +++ qmail-1.03.logging/qmail-smtpd.c 2010-01-05 18:02:43.000000000 -0600 @@ -38,16 +38,20 @@ int safewrite(fd,buf,len) int fd; char * char ssoutbuf[512]; substdio ssout = SUBSTDIO_FDBUF(safewrite,1,ssoutbuf,sizeof ssoutbuf); +char sserrbuf[512]; +substdio sserr = SUBSTDIO_FDBUF(write,2,sserrbuf,sizeof sserrbuf); +void logit(const char* message); +void logit2(const char* message, const char* reason); void flush() { substdio_flush(&ssout); } void out(s) char *s; { substdio_puts(&ssout,s); } -void die_read() { _exit(1); } -void die_alarm() { out("451 timeout (#4.4.2)\r\n"); flush(); _exit(1); } +void die_read() { logit("read failed"); _exit(1); } +void die_alarm() { logit("timeout"); out("451 timeout (#4.4.2)\r\n"); flush(); _exit(1); } void die_nomem() { out("421 out of memory (#4.3.0)\r\n"); flush(); _exit(1); } -void die_control() { out("421 unable to read controls (#4.3.0)\r\n"); flush(); _exit(1); } -void die_ipme() { out("421 unable to figure out my IP addresses (#4.3.0)\r\n"); flush(); _exit(1); } -void straynewline() { out("451 See http://pobox.com/~djb/docs/smtplf.html.\r\n"); flush(); _exit(1); } +void die_control() { logit("unable to read controls"); out("421 unable to read controls (#4.3.0)\r\n"); flush(); _exit(1); } +void die_ipme() { logit("unable to figure out my IP addresses"); out("421 unable to figure out my IP addresses (#4.3.0)\r\n"); flush(); _exit(1); } +void straynewline() { logit("bad newlines"); out("451 See http://pobox.com/~djb/docs/smtplf.html.\r\n"); flush(); _exit(1); } void err_bmf() { out("553 sorry, your envelope sender is in my badmailfrom list (#5.7.1)\r\n"); } void err_nogateway() { out("553 sorry, that domain isn't in my list of allowed rcpthosts (#5.7.1)\r\n"); } @@ -221,6 +225,61 @@ int seenmail = 0; int flagbarf; /* defined if seenmail */ stralloc mailfrom = {0}; stralloc rcptto = {0}; +stralloc log_buf = {0}; + +void logit(message) const char* message; +{ + if (!stralloc_copys(&log_buf, "qmail-smtpd: ")) die_nomem(); + if (!stralloc_cats(&log_buf, message)) die_nomem(); + if (!stralloc_catb(&log_buf, ": ", 2)) die_nomem(); + if (mailfrom.s) { + if (!stralloc_catb(&log_buf, mailfrom.s, mailfrom.len-1)) die_nomem(); + } else + if (!stralloc_catb(&log_buf, "(null)", 6)) die_nomem(); + if (!stralloc_catb(&log_buf, " from ", 6)) die_nomem(); + if (!stralloc_cats(&log_buf, remoteip)) die_nomem(); + if (!stralloc_catb(&log_buf, " to ", 4)) die_nomem(); + if (addr.s) { + if (!stralloc_catb(&log_buf, addr.s, addr.len-1)) die_nomem(); + } else + if (!stralloc_catb(&log_buf, "(null)", 6)) die_nomem(); + if (!stralloc_catb(&log_buf, " helo ", 6)) die_nomem(); + if (helohost.s) { + if (!stralloc_catb(&log_buf, helohost.s, helohost.len-1)) die_nomem(); + } else + if (!stralloc_catb(&log_buf, "(null)", 6)) die_nomem(); + if (!stralloc_catb(&log_buf, "\n", 1)) die_nomem(); + substdio_putflush(&sserr, log_buf); +} + +void logit2(message, reason) +const char* message; +const char* reason; +{ + if (!stralloc_copys(&log_buf,"qmail-smtpd: ")) die_nomem(); + if (!stralloc_cats(&log_buf, message)) die_nomem(); + if (!stralloc_cats(&log_buf, " (")) die_nomem(); + if (!stralloc_cats(&log_buf, reason)) die_nomem(); + if (!stralloc_cats(&log_buf, "): ")) die_nomem(); + if (mailfrom.s) { + if (!stralloc_catb(&log_buf, mailfrom.s, mailfrom.len-1)) die_nomem(); + } else + if (!stralloc_catb(&log_buf, "(null)", 6)) die_nomem(); + if (!stralloc_cats(&log_buf," from ")) die_nomem(); + if (!stralloc_cats(&log_buf, remoteip)) die_nomem(); + if (!stralloc_cats(&log_buf, " to ")) die_nomem(); + if (addr.s) { + if (!stralloc_catb(&log_buf, addr.s, addr.len-1)) die_nomem(); + } else + if (!stralloc_catb(&log_buf, "(null)", 6)) die_nomem(); + if (!stralloc_cats(&log_buf, " helo ")) die_nomem(); + if (helohost.s) { + if (!stralloc_catb(&log_buf, helohost.s, helohost.len-1)) die_nomem(); + } else + if (!stralloc_catb(&log_buf, "(null)", 6)) die_nomem(); + if (!stralloc_catb(&log_buf, "\n", 1)) die_nomem(); + substdio_putflush(&sserr, log_buf); +} void smtp_helo(arg) char *arg; { @@ -250,7 +309,7 @@ void smtp_mail(arg) char *arg; void smtp_rcpt(arg) char *arg; { if (!seenmail) { err_wantmail(); return; } if (!addrparse(arg)) { err_syntax(); return; } - if (flagbarf) { err_bmf(); return; } + if (flagbarf) { logit("badmailfrom"); err_bmf(); return; } if (relayclient) { --addr.len; if (!stralloc_cats(&addr,relayclient)) die_nomem(); @@ -386,10 +445,24 @@ void smtp_data() { qmail_put(&qqt,rcptto.s,rcptto.len); qqx = qmail_close(&qqt); - if (!*qqx) { acceptmessage(qp); return; } - if (hops) { out("554 too many hops, this message is looping (#5.4.6)\r\n"); return; } - if (databytes) if (!bytestooverflow) { out("552 sorry, that message size exceeds my databytes limit (#5.3.4)\r\n"); return; } - if (*qqx == 'D') out("554 "); else out("451 "); + if (!*qqx) { acceptmessage(qp); logit("message accepted"); return; } + if (hops) { + out("554 too many hops, this message is looping (#5.4.6)\r\n"); + logit("message looping"); + return; + } + if (databytes) if (!bytestooverflow) { + out("552 sorry, that message size exceeds my databytes limit (#5.3.4)\r\n"); + logit("message too big"); + return; + } + if (*qqx == 'D') { + out("554 "); + logit2("message rejected", qqx + 1); + } else { + out("451 "); + logit2("message delayed", qqx + 1); + } out(qqx + 1); out("\r\n"); }