# This patch has two functions.
#
# The log comes out. 
#   /etc/tcp.smtp ....... ,QMAILQUEUE="bin/qmail-dkim",DKIMKEY="....",DKIMLOG_FD="9"
#   /etc/init.d/qmail ... /var/qmail/bin/qmail-smtpd 9>&1 2>&1 | /var/qmail/bin/splogger smtpd &
#
# The specified recipient address is signed. 
#   create /var/qmail/control/qmailqueue_domain of the same format as /var/qmail/control/badrcptto.
#   but, if ! -e  /var/qmail/control/qmailqueue_domain then all mails are signed.
#
diff -ur qmail-1.03-20110603/qmail-dkim.c qmail-1.03/qmail-dkim.c
--- qmail-1.03-20110603/qmail-dkim.c	2011-05-13 02:33:27.000000000 +0900
+++ qmail-1.03/qmail-dkim.c	2011-06-13 13:08:34.000000000 +0900
@@ -111,9 +111,11 @@
 char            inbuf[2048];
 char            outbuf[256];
 char            errbuf[256];
+char            logbuf[256];
 struct substdio ssin;
 struct substdio ssout;
 struct substdio sserr;
+struct substdio sslog;
 
 datetime_sec    starttime;
 struct datetime dt;
@@ -122,10 +124,25 @@
 char           *pidfn;
 int             messfd;
 int             readfd;
+int             logfd;
 
 char          **MakeArgs(char *);
 void            FreeMakeArgs(char **);
 
+int  logsignstat=0;
+void logstr(s) char *s; { if(substdio_puts(&sslog,s) == -1) _exit(1); }
+void logfin(s) char *s; { if(substdio_puts(&sslog,s) == -1) _exit(1); if(substdio_flush(&sslog) == -1) _exit(1); }
+void logdkimsign(arg1,arg2,arg3) char *arg1,*arg2; int arg3; {
+	char strnum[FMT_ULONG];
+	logstr("dkim-sign: d="); logstr(arg1); logstr(" s="); logstr(arg2); logstr(" pid ");
+	strnum[fmt_ulong(strnum,getpid())] = 0;
+	logstr(strnum); logstr(" cpid ");
+	strnum[fmt_ulong(strnum,arg3)] = 0;
+	logstr(strnum);
+	logfin("\n");
+	return;
+}
+
 void
 die(e)
 	int             e;
@@ -364,6 +381,7 @@
 			return;
 		die(32);
 	case 1:
+		logsignstat=1;
 		break;
 	default:
 		custom_error("Z", "Unable to read private key. (#4.3.0)", 0);
@@ -1059,6 +1077,11 @@
 	else
 		scan_int(p, &errfd);
 	substdio_fdbuf(&sserr, write, errfd, errbuf, sizeof(errbuf));
+
+	char *pdomain;
+	if (p = env_get("DKIMLOG_FD")) scan_int(p, &logfd);
+	if ( logfd ) substdio_fdbuf(&sslog, write, logfd, logbuf, sizeof(logbuf));
+
 	if (dkimsign)
 	{
 		/* selector */
@@ -1193,6 +1216,7 @@
 				DKIMSignFree(&ctxt);
 				maybe_die_dkim(DKIM_INVALID_CONTEXT);
 			}
+			pdomain = p;
 			write_signature(p, dkimsign); /*- calls DKIMSignFree(&ctxt) */
 		} else
 		if (dkimverify)
@@ -1331,6 +1355,7 @@
 		die(120);
 	}
 	close(pim[0]);
+	if ( logfd ) { if (logsignstat) { logdkimsign(pdomain,selector,pid); } else { logdkimsign("null","null",pid); } }
 	substdio_fdbuf(&ssin, read, readfd, inbuf, sizeof(inbuf));
 	substdio_fdbuf(&ssout, write, pim[1], outbuf, sizeof(outbuf));
 	if (substdio_bput(&ssout, dkimoutput.s, dkimoutput.len) == -1) /*- write DKIM signature */
diff -ur qmail-1.03-orig/qmail-smtpd.c qmail-1.03/qmail-smtpd.c
--- qmail-1.03-orig/qmail-smtpd.c	2011-05-13 02:32:06.000000000 +0900
+++ qmail-1.03/qmail-smtpd.c	2011-06-01 10:22:38.000000000 +0900
@@ -374,6 +374,11 @@
 struct constmap mapbadhelo;
 #endif
 
+int             qmailqueue_count = 0;
+int             qmailqueue_ok = 0;
+stralloc        qmailqueue_domain = {0};
+struct constmap qmailqueue_map;
+
 void setup()
 {
   char *x;
@@ -479,6 +484,11 @@
       }
 #endif
 
+    qmailqueue_ok = control_readfile(&qmailqueue_domain,"control/qmailqueue_domain", 0);
+    if (qmailqueue_ok == -1) die_control();
+    if (qmailqueue_ok)
+      if (!constmap_init(&qmailqueue_map,qmailqueue_domain.s,qmailqueue_domain.len,0)) die_nomem();
+
   dohelo(remotehost);
 }
 
@@ -814,6 +824,31 @@
   rcptcount = 0;
   out("250 ok\r\n");
 }
+
+int qmailqueue_rcptcheck()
+{
+  int i;
+  int j;
+  int k = 0;
+  char subvalue;
+  if (qmailqueue_ok) {
+    if (constmap(&qmailqueue_map,addr.s,addr.len - 1)) return 1;
+    j = byte_rchr(addr.s,addr.len,'@');
+    if (j < addr.len)
+      if (constmap(&qmailqueue_map,addr.s + j,addr.len - j - 1)) return 1;
+    i = 0;
+    for (j = 0;j < qmailqueue_domain.len;++j)
+      if (!qmailqueue_domain.s[j]) {
+        subvalue = qmailqueue_domain.s[i] != '!';
+        if (!subvalue) i++;
+        if ((k != subvalue) && wildmat(addr.s,qmailqueue_domain.s + i)) k = subvalue;
+        i = j + 1;
+      }
+    return k;
+  }
+  return 0;
+}
+
 void smtp_rcpt(arg) char *arg; {
   if (!seenmail) { err_wantmail(); return; } 
   if (!addrparse(arg)) { err_syntax(); return; }
@@ -867,6 +902,10 @@
   if (!stralloc_cats(&rcptto,"T")) die_nomem();		/* address accepted; tag it: "T" */
   if (!stralloc_cats(&rcptto,addr.s)) die_nomem();
   if (!stralloc_0(&rcptto)) die_nomem();
+
+  if (qmailqueue_ok)
+    if (qmailqueue_rcptcheck()) qmailqueue_count++;
+
   out("250 ok\r\n");
 }
 
@@ -1111,6 +1150,7 @@
   flagmimetype = 0;
   linetype = ' ';
 #endif
+  if (qmailqueue_ok && ! qmailqueue_count ) disable_qmailqueue();
   if (qmail_open(&qqt) == -1) { err_qqt(); return; }
   qp = qmail_qp(&qqt);
   out("354 go ahead\r\n");
diff -ur qmail-1.03-orig/qmail.c qmail-1.03/qmail.c
--- qmail-1.03-orig/qmail.c	2011-05-13 02:33:27.000000000 +0900
+++ qmail-1.03/qmail.c	2011-05-31 22:36:15.000000000 +0900
@@ -10,10 +10,16 @@
 
 static char *binqqargs[2] = { 0, 0 } ;
 
+static int qmailqueue=1;
+void disable_qmailqueue(){
+   qmailqueue=0;
+}
+
 static void setup_qqargs()
 {
-  if(!binqqargs[0])
-    binqqargs[0] = env_get("QMAILQUEUE");
+  if(!binqqargs[0]) {
+    if ( qmailqueue ) binqqargs[0] = env_get("QMAILQUEUE");
+  }
   if(!binqqargs[0])
     binqqargs[0] = "bin/qmail-queue";
 }