--- qmail-1.03.orig/qmail-smtpd.c	2003-09-11 19:31:52.000000000 -0600
+++ qmail-1.03.logging/qmail-smtpd.c	2007-02-23 12:08:38.000000000 -0700
@@ -38,6 +38,8 @@
 
 char ssoutbuf[512];
 substdio ssout = SUBSTDIO_FDBUF(safewrite,1,ssoutbuf,sizeof ssoutbuf);
+char sserrbuf[512];
+substdio sserr = SUBSTDIO_FDBUF(safewrite,2,sserrbuf,sizeof sserrbuf);
 
 void flush() { substdio_flush(&ssout); }
 void out(s) char *s; { substdio_puts(&ssout,s); }
@@ -222,6 +224,43 @@
 stralloc mailfrom = {0};
 stralloc rcptto = {0};
 
+void logit(message) const char* message;
+{
+  stralloc buf = {0};
+  if (!stralloc_copys(&buf, "qmail-smtpd: ")) die_nomem();
+  if (!stralloc_cats(&buf, message)) die_nomem();
+  if (!stralloc_catb(&buf, mailfrom.s, mailfrom.len-1)) die_nomem();
+  if (!stralloc_cats(&buf, " from ")) die_nomem();
+  if (!stralloc_cats(&buf, remoteip)) die_nomem();
+  if (!stralloc_cats(&buf, " to ")) die_nomem();
+  if (!stralloc_catb(&buf, addr.s, addr.len-1)) die_nomem();
+  if (!stralloc_cats(&buf, " helo ")) die_nomem();
+  if (!stralloc_catb(&buf, helohost.s, helohost.len-1)) die_nomem();
+  if (!stralloc_catb(&buf, "\n", 1)) die_nomem();
+  substdio_putflush(&sserr, buf);
+}
+
+void logit2(message, reason)
+const char* message;
+const char* reason;
+{
+  stralloc buf = {0};
+  if (!stralloc_copys(&buf,"qmail-smtpd: ")) die_nomem();
+  if (!stralloc_cats(&buf, message)) die_nomem();
+  if (!stralloc_cats(&buf, " (")) die_nomem();
+  if (!stralloc_cats(&buf, reason)) die_nomem();
+  if (!stralloc_cats(&buf, "): ")) die_nomem();
+  if (!stralloc_catb(&buf, mailfrom.s, mailfrom.len-1)) die_nomem();
+  if (!stralloc_cats(&buf," from ")) die_nomem();
+  if (!stralloc_cats(&buf, remoteip)) die_nomem();
+  if (!stralloc_cats(&buf, " to ")) die_nomem();
+  if (!stralloc_catb(&buf, addr.s, addr.len-1)) die_nomem();
+  if (!stralloc_cats(&buf, " helo ")) die_nomem();
+  if (!stralloc_catb(&buf, helohost.s, helohost.len-1)) die_nomem();
+  if (!stralloc_catb(&buf, "\n", 1)) die_nomem();
+  substdio_putflush(&sserr, buf);
+}
+
 void smtp_helo(arg) char *arg;
 {
   smtp_greet("250 "); out("\r\n");
@@ -250,7 +289,7 @@
 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 +425,24 @@
   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");
 }
