]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_smtp.c
* serv_smtp.c: instead of doubling delivery retry times unbounded, set a
[citadel.git] / citadel / serv_smtp.c
index ce80fa44fe41ac6ea6b9bf9b75b9036b0a5680df..7cd291824dcf4fd63dd988b5d2bb7aa47cfa6e6f 100644 (file)
@@ -1,4 +1,10 @@
-/* $Id$ */
+/*
+ * $Id$
+ *
+ * An implementation of RFC821 (Simple Mail Transfer Protocol) for the
+ * Citadel system.
+ *
+ */
 
 #include "sysdep.h"
 #include <stdlib.h>
 #include <pwd.h>
 #include <errno.h>
 #include <sys/types.h>
-#include <sys/time.h>
+
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# if HAVE_SYS_TIME_H
+#  include <sys/time.h>
+# else
+#  include <time.h>
+# endif
+#endif
+
 #include <sys/wait.h>
 #include <ctype.h>
 #include <string.h>
 #include <limits.h>
-#include <time.h>
 #include "citadel.h"
 #include "server.h"
 #include "sysdep_decls.h"
 
 struct citsmtp {               /* Information about the current session */
        int command_state;
-       char helo_node[256];
+       char helo_node[SIZ];
        struct usersupp vrfy_buffer;
        int vrfy_count;
-       char vrfy_match[256];
-       char from[256];
+       char vrfy_match[SIZ];
+       char from[SIZ];
        int number_of_recipients;
        int delivery_mode;
+       int message_originated_locally;
 };
 
 enum {                         /* Command states for login authentication */
@@ -81,11 +98,10 @@ void smtp_greeting(void) {
        CC->internal_pgm = 1;
        CC->cs_flags |= CS_STEALTH;
        CtdlAllocUserData(SYM_SMTP, sizeof(struct citsmtp));
-       CtdlAllocUserData(SYM_SMTP_RECP, 256);
+       CtdlAllocUserData(SYM_SMTP_RECP, SIZ);
        sprintf(SMTP_RECP, "%s", "");
 
-       cprintf("220 Welcome to the Citadel/UX ESMTP server at %s\r\n",
-               config.c_fqdn);
+       cprintf("220 %s ESMTP Citadel/UX server ready.\r\n", config.c_fqdn);
 }
 
 
@@ -100,7 +116,7 @@ void smtp_hello(char *argbuf, int is_esmtp) {
                cprintf("250 Greetings and joyous salutations.\r\n");
        }
        else {
-               cprintf("250-Greetings and joyous salutations.\r\n");
+               cprintf("250-Extended greetings and joyous salutations.\r\n");
                cprintf("250-HELP\r\n");
                cprintf("250-SIZE %ld\r\n", config.c_maxmsglen);
                cprintf("250 AUTH=LOGIN\r\n");
@@ -112,7 +128,7 @@ void smtp_hello(char *argbuf, int is_esmtp) {
  * Implement HELP command.
  */
 void smtp_help(void) {
-       cprintf("214-Here's the frequency, Kenneth:\r\n");
+       cprintf("214-Commands accepted:\r\n");
        cprintf("214-    DATA\r\n");
        cprintf("214-    EHLO\r\n");
        cprintf("214-    EXPN\r\n");
@@ -124,7 +140,7 @@ void smtp_help(void) {
        cprintf("214-    RCPT\r\n");
        cprintf("214-    RSET\r\n");
        cprintf("214-    VRFY\r\n");
-       cprintf("214 I could tell you more, but then I'd have to kill you.\r\n");
+       cprintf("214     \r\n");
 }
 
 
@@ -132,8 +148,8 @@ void smtp_help(void) {
  *
  */
 void smtp_get_user(char *argbuf) {
-       char buf[256];
-       char username[256];
+       char buf[SIZ];
+       char username[SIZ];
 
        decode_base64(username, argbuf);
        lprintf(9, "Trying <%s>\n", username);
@@ -153,7 +169,7 @@ void smtp_get_user(char *argbuf) {
  *
  */
 void smtp_get_pass(char *argbuf) {
-       char password[256];
+       char password[SIZ];
 
        decode_base64(password, argbuf);
        lprintf(9, "Trying <%s>\n", password);
@@ -174,7 +190,7 @@ void smtp_get_pass(char *argbuf) {
  *
  */
 void smtp_auth(char *argbuf) {
-       char buf[256];
+       char buf[SIZ];
 
        if (strncasecmp(argbuf, "login", 5) ) {
                cprintf("550 We only support LOGIN authentication.\r\n");
@@ -281,18 +297,31 @@ void smtp_expn(char *argbuf) {
  */
 void smtp_rset(void) {
        memset(SMTP, 0, sizeof(struct citsmtp));
+       if (SMTP_RECP != NULL) strcpy(SMTP_RECP, "");
        if (CC->logged_in) logout(CC);
        cprintf("250 Zap!\r\n");
 }
 
+/*
+ * Clear out the portions of the state buffer that need to be cleared out
+ * after the DATA command finishes.
+ */
+void smtp_data_clear(void) {
+       strcpy(SMTP->from, "");
+       SMTP->number_of_recipients = 0;
+       SMTP->delivery_mode = 0;
+       SMTP->message_originated_locally = 0;
+       if (SMTP_RECP != NULL) strcpy(SMTP_RECP, "");
+}
+
 
 
 /*
  * Implements the "MAIL From:" command
  */
 void smtp_mail(char *argbuf) {
-       char user[256];
-       char node[256];
+       char user[SIZ];
+       char node[SIZ];
        int cvt;
 
        if (strlen(SMTP->from) != 0) {
@@ -325,15 +354,21 @@ void smtp_mail(char *argbuf) {
                        strcpy(SMTP->from, "");
                        return;
                }
+               else {
+                       SMTP->message_originated_locally = 1;
+               }
        }
 
        /* Otherwise, make sure outsiders aren't trying to forge mail from
         * this system.
         */
        else {
+               TRACE;
                cvt = convert_internet_address(user, node, SMTP->from);
+               TRACE;
                lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
                if (CtdlHostAlias(node) == hostalias_localhost) {
+                       TRACE;
                        cprintf("550 You must log in to send mail from %s\r\n",
                                node);
                        strcpy(SMTP->from, "");
@@ -341,7 +376,7 @@ void smtp_mail(char *argbuf) {
                }
        }
 
-       cprintf("250 Sender ok.  Groovy.\r\n");
+       cprintf("250 Sender ok\r\n");
 }
 
 
@@ -351,13 +386,12 @@ void smtp_mail(char *argbuf) {
  */
 void smtp_rcpt(char *argbuf) {
        int cvt;
-       char user[256];
-       char node[256];
-       char recp[256];
-       int is_spam = 0;        /* FIXME implement anti-spamming */
+       char user[SIZ];
+       char node[SIZ];
+       char recp[SIZ];
 
        if (strlen(SMTP->from) == 0) {
-               cprintf("503 MAIL first, then RCPT.  Duh.\r\n");
+               cprintf("503 Need MAIL before RCPT\r\n");
                return;
        }
 
@@ -368,10 +402,12 @@ void smtp_rcpt(char *argbuf) {
 
        strcpy(recp, &argbuf[3]);
        striplt(recp);
+       TRACE;
        alias(recp);
+       TRACE;
 
        cvt = convert_internet_address(user, node, recp);
-       sprintf(recp, "%s@%s", user, node);
+       snprintf(recp, sizeof recp, "%s@%s", user, node);
        lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
 
        switch(cvt) {
@@ -412,8 +448,8 @@ void smtp_rcpt(char *argbuf) {
                        return;
 
                case rfc822_address_nonlocal:
-                       if (is_spam) {
-                               cprintf("551 Away with thee, spammer!\r\n");
+                       if (SMTP->message_originated_locally == 0) {
+                               cprintf("551 Third-party relaying denied.\r\n");
                        }
                        else {
                                cprintf("250 Remote recipient %s ok\r\n", recp);
@@ -438,23 +474,34 @@ void smtp_rcpt(char *argbuf) {
  * (This is kind of ugly.  IGnet should be done using clean server-to-server
  * code instead of the old style spool.)
  */
-void smtp_deliver_ignet(struct CtdlMessage *msg, char *user, char *room) {
+void smtp_deliver_ignet(struct CtdlMessage *msg, char *user, char *dest) {
        struct ser_ret smr;
-       char *hold_R, *hold_D;
+       char *hold_R, *hold_D, *hold_O;
        FILE *fp;
+       char filename[SIZ];
+       static int seq = 0;
+
+       lprintf(9, "smtp_deliver_ignet(msg, %s, %s)\n", user, dest);
 
        hold_R = msg->cm_fields['R'];
        hold_D = msg->cm_fields['D'];
+       hold_O = msg->cm_fields['O'];
        msg->cm_fields['R'] = user;
-       msg->cm_fields['D'] = room;
+       msg->cm_fields['D'] = dest;
+       msg->cm_fields['O'] = MAILROOM;
 
        serialize_message(&smr, msg);
 
        msg->cm_fields['R'] = hold_R;
        msg->cm_fields['D'] = hold_D;
+       msg->cm_fields['O'] = hold_O;
 
        if (smr.len != 0) {
-               fp = fopen(tmpnam("./network/spoolin/"), "wb");
+               snprintf(filename, sizeof filename,
+                       "./network/spoolin/%s.%04x.%04x",
+                       dest, getpid(), ++seq);
+               lprintf(9, "spool file name is <%s>\n", filename);
+               fp = fopen(filename, "wb");
                if (fp != NULL) {
                        fwrite(smr.ser, smr.len, 1, fp);
                        fclose(fp);
@@ -493,17 +540,18 @@ int smtp_message_delivery(struct CtdlMessage *msg) {
        if (msg->cm_fields['A']==NULL) msg->cm_fields['A'] = strdoop(user);
        if (msg->cm_fields['N']==NULL) msg->cm_fields['N'] = strdoop(node);
        if (msg->cm_fields['H']==NULL) msg->cm_fields['H'] = strdoop(name);
+       if (msg->cm_fields['O']==NULL) msg->cm_fields['O'] = strdoop(MAILROOM);
 
        /* Save the message in the queue */
        msgid = CtdlSaveMsg(msg,
                "",
                SMTP_SPOOLOUT_ROOM,
-               MES_LOCAL,
-               1);
+               MES_LOCAL);
        ++successful_saves;
 
        instr = mallok(1024);
-       sprintf(instr, "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
+       snprintf(instr, 1024,
+                       "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
                        "bounceto|%s\n",
                SPOOLMIME, msgid, time(NULL),
                SMTP->from );
@@ -534,14 +582,17 @@ int smtp_message_delivery(struct CtdlMessage *msg) {
 
                /* Delivery over the local Citadel network (IGnet) */
                if (!strcasecmp(dtype, "ignet")) {
-                       smtp_deliver_ignet(msg, user, room);
+                       extract(user, buf, 1);
+                       extract(node, buf, 2);
+                       smtp_deliver_ignet(msg, user, node);
                }
 
                /* Remote delivery */
                if (!strcasecmp(dtype, "remote")) {
                        extract(user, buf, 1);
                        instr = reallok(instr, strlen(instr) + 1024);
-                       sprintf(&instr[strlen(instr)],
+                       snprintf(&instr[strlen(instr)],
+                               strlen(instr) + 1024,
                                "remote|%s|0\n",
                                user);
                        ++remote_spools;
@@ -557,7 +608,7 @@ int smtp_message_delivery(struct CtdlMessage *msg) {
                imsg->cm_anon_type = MES_NORMAL;
                imsg->cm_format_type = FMT_RFC822;
                imsg->cm_fields['M'] = instr;
-               CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL, 1);
+               CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
                CtdlFreeMessage(imsg);
        }
 
@@ -565,7 +616,7 @@ int smtp_message_delivery(struct CtdlMessage *msg) {
        else {
                phree(instr);   /* only needed here, because CtdlSaveMsg()
                                 * would free this buffer otherwise */
-               CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, msgid, NULL); 
+               CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, msgid, ""); 
        }
 
        return(failed_saves);
@@ -580,7 +631,7 @@ void smtp_data(void) {
        char *body;
        struct CtdlMessage *msg;
        int retval;
-       char nowstamp[256];
+       char nowstamp[SIZ];
 
        if (strlen(SMTP->from) == 0) {
                cprintf("503 Need MAIL command first.\r\n");
@@ -594,10 +645,10 @@ void smtp_data(void) {
 
        cprintf("354 Transmit message now; terminate with '.' by itself\r\n");
        
-       generate_rfc822_datestamp(nowstamp, time(NULL));
+       datestring(nowstamp, time(NULL), DATESTRING_RFC822);
        body = mallok(4096);
 
-       if (body != NULL) sprintf(body,
+       if (body != NULL) snprintf(body, 4096,
                "Received: from %s\n"
                "       by %s;\n"
                "       %s\n",
@@ -630,11 +681,13 @@ void smtp_data(void) {
        CtdlFreeMessage(msg);
 
        if (!retval) {
-               cprintf("250 Message accepted for delivery.\r\n");
+               cprintf("250 ok terrific\r\n");
        }
        else {
                cprintf("550 Internal delivery errors: %d\r\n", retval);
        }
+
+       smtp_data_clear();      /* clear out the buffers now */
 }
 
 
@@ -644,7 +697,7 @@ void smtp_data(void) {
  * Main command loop for SMTP sessions.
  */
 void smtp_command_loop(void) {
-       char cmdbuf[256];
+       char cmdbuf[SIZ];
 
        time(&CC->lastcmd);
        memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
@@ -715,7 +768,7 @@ void smtp_command_loop(void) {
        }
 
        else {
-               cprintf("502 I'm sorry Dave, I'm afraid I can't do that.\r\n");
+               cprintf("502 I'm afraid I can't do that.\r\n");
        }
 
 }
@@ -742,7 +795,7 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        int num_mxhosts;
        int mx;
        int i;
-       char user[256], node[256], name[256];
+       char user[SIZ], node[SIZ], name[SIZ];
        char buf[1024];
        char mailfrom[1024];
        int lp, rp;
@@ -823,7 +876,7 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        lprintf(9, "Number of MX hosts for <%s> is %d\n", node, num_mxhosts);
        if (num_mxhosts < 1) {
                *status = 5;
-               sprintf(dsn, "No MX hosts found for <%s>", node);
+               snprintf(dsn, SIZ, "No MX hosts found for <%s>", node);
                return;
        }
 
@@ -831,9 +884,9 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
                extract(buf, mxhosts, mx);
                lprintf(9, "Trying <%s>\n", buf);
                sock = sock_connect(buf, "25", "tcp");
-               sprintf(dsn, "Could not connect: %s", strerror(errno));
+               snprintf(dsn, SIZ, "Could not connect: %s", strerror(errno));
                if (sock >= 0) lprintf(9, "Connected!\n");
-               if (sock < 0) sprintf(dsn, "%s", strerror(errno));
+               if (sock < 0) snprintf(dsn, SIZ, "%s", strerror(errno));
                if (sock >= 0) break;
        }
 
@@ -843,7 +896,7 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        }
 
        /* Process the SMTP greeting from the server */
-       if (sock_gets(sock, buf) < 0) {
+       if (ml_sock_gets(sock, buf) < 0) {
                *status = 4;
                strcpy(dsn, "Connection broken during SMTP conversation");
                goto bail;
@@ -865,10 +918,10 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        /* At this point we know we are talking to a real SMTP server */
 
        /* Do a HELO command */
-       sprintf(buf, "HELO %s", config.c_fqdn);
+       snprintf(buf, sizeof buf, "HELO %s", config.c_fqdn);
        lprintf(9, ">%s\n", buf);
-       sock_puts(sock, buf);
-       if (sock_gets(sock, buf) < 0) {
+       sock_puts_crlf(sock, buf);
+       if (ml_sock_gets(sock, buf) < 0) {
                *status = 4;
                strcpy(dsn, "Connection broken during SMTP conversation");
                goto bail;
@@ -889,10 +942,10 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
 
 
        /* HELO succeeded, now try the MAIL From: command */
-       sprintf(buf, "MAIL From: %s", mailfrom);
+       snprintf(buf, sizeof buf, "MAIL From: <%s>", mailfrom);
        lprintf(9, ">%s\n", buf);
-       sock_puts(sock, buf);
-       if (sock_gets(sock, buf) < 0) {
+       sock_puts_crlf(sock, buf);
+       if (ml_sock_gets(sock, buf) < 0) {
                *status = 4;
                strcpy(dsn, "Connection broken during SMTP conversation");
                goto bail;
@@ -913,10 +966,10 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
 
 
        /* MAIL succeeded, now try the RCPT To: command */
-       sprintf(buf, "RCPT To: %s", addr);
+       snprintf(buf, sizeof buf, "RCPT To: <%s>", addr);
        lprintf(9, ">%s\n", buf);
-       sock_puts(sock, buf);
-       if (sock_gets(sock, buf) < 0) {
+       sock_puts_crlf(sock, buf);
+       if (ml_sock_gets(sock, buf) < 0) {
                *status = 4;
                strcpy(dsn, "Connection broken during SMTP conversation");
                goto bail;
@@ -938,8 +991,8 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
 
        /* RCPT succeeded, now try the DATA command */
        lprintf(9, ">DATA\n");
-       sock_puts(sock, "DATA");
-       if (sock_gets(sock, buf) < 0) {
+       sock_puts_crlf(sock, "DATA");
+       if (ml_sock_gets(sock, buf) < 0) {
                *status = 4;
                strcpy(dsn, "Connection broken during SMTP conversation");
                goto bail;
@@ -974,7 +1027,7 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        }
 
        sock_write(sock, ".\r\n", 3);
-       if (sock_gets(sock, buf) < 0) {
+       if (ml_sock_gets(sock, buf) < 0) {
                *status = 4;
                strcpy(dsn, "Connection broken during SMTP conversation");
                goto bail;
@@ -998,8 +1051,8 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        *status = 2;
 
        lprintf(9, ">QUIT\n");
-       sock_puts(sock, "QUIT");
-       sock_gets(sock, buf);
+       sock_puts_crlf(sock, "QUIT");
+       ml_sock_gets(sock, buf);
        lprintf(9, "<%s\n", buf);
 
 bail:  if (msg_fp != NULL) fclose(msg_fp);
@@ -1065,14 +1118,14 @@ void smtp_do_bounce(char *instr) {
         bmsg->cm_fields['N'] = strdoop(config.c_nodename);
 
        if (give_up) bmsg->cm_fields['M'] = strdoop(
-"A message you sent could not be delivered to some or all of its recipients.\n"
-"The following addresses were undeliverable:\n\n"
-);
-
-        else bmsg->cm_fields['M'] = strdoop(
 "A message you sent could not be delivered to some or all of its recipients\n"
 "due to prolonged unavailability of its destination(s).\n"
 "Giving up on the following addresses:\n\n"
+);
+
+        else bmsg->cm_fields['M'] = strdoop(
+"A message you sent could not be delivered to some or all of its recipients.\n"
+"The following addresses were undeliverable:\n\n"
 );
 
        /*
@@ -1131,6 +1184,7 @@ void smtp_do_bounce(char *instr) {
 
                /* First try the user who sent the message */
                lprintf(9, "bounce to user? <%s>\n", bounceto);
+               TRACE;
                if (strlen(bounceto) == 0) {
                        lprintf(7, "No bounce address specified\n");
                        bounce_msgid = (-1L);
@@ -1142,14 +1196,15 @@ void smtp_do_bounce(char *instr) {
                else {
                        bounce_msgid = CtdlSaveMsg(bmsg,
                                bounceto,
-                               "", mes_type, 1);
+                               "", mes_type);
                }
+               TRACE;
 
                /* Otherwise, go to the Aide> room */
                lprintf(9, "bounce to room?\n");
                if (bounce_msgid < 0L) bounce_msgid = CtdlSaveMsg(bmsg,
                        "", AIDEROOM,
-                       MES_LOCAL, 1);
+                       MES_LOCAL);
        }
 
        CtdlFreeMessage(bmsg);
@@ -1210,7 +1265,7 @@ int smtp_purge_completed_deliveries(char *instr) {
  *
  * Called by smtp_do_queue() to handle an individual message.
  */
-void smtp_do_procmsg(long msgnum) {
+void smtp_do_procmsg(long msgnum, void *userdata) {
        struct CtdlMessage *msg;
        char *instr = NULL;
        char *results = NULL;
@@ -1225,6 +1280,7 @@ void smtp_do_procmsg(long msgnum) {
        int incomplete_deliveries_remaining;
        time_t attempted = 0L;
        time_t last_attempted = 0L;
+       time_t retry = SMTP_RETRY_INTERVAL;
 
        msg = CtdlFetchMessage(msgnum);
        if (msg == NULL) {
@@ -1255,6 +1311,14 @@ void smtp_do_procmsg(long msgnum) {
                if (!strcasecmp(key, "msgid")) {
                        text_msgid = extract_long(buf, 1);
                }
+               if (!strcasecmp(key, "retry")) {
+                       /* double the retry interval after each attempt */
+                       retry = extract_long(buf, 1) * 2L;
+                       if (retry > SMTP_RETRY_MAX) {
+                               retry = SMTP_RETRY_MAX;
+                       }
+                       remove_token(instr, i, '\n');
+               }
                if (!strcasecmp(key, "attempted")) {
                        attempted = extract_long(buf, 1);
                        if (attempted > last_attempted)
@@ -1266,7 +1330,7 @@ void smtp_do_procmsg(long msgnum) {
        /*
         * Postpone delivery if we've already tried recently.
         */
-       if ( (time(NULL) - last_attempted) < SMTP_RETRY_INTERVAL) {
+       if ( (time(NULL) - last_attempted) < retry) {
                lprintf(7, "Retry time not yet reached.\n");
                phree(instr);
                return;
@@ -1335,9 +1399,9 @@ void smtp_do_procmsg(long msgnum) {
         * No delivery instructions remain, so delete both the instructions
         * message and the message message.
         */
-       if (incomplete_deliveries_remaining <= 0)  {
-               CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, msgnum, NULL);    
-               CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, text_msgid, NULL);    
+       if (incomplete_deliveries_remaining <= 0) {
+               CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, msgnum, "");
+               CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, text_msgid, "");
        }
 
 
@@ -1346,18 +1410,21 @@ void smtp_do_procmsg(long msgnum) {
         * instructions and replace with the updated ones.
         */
        if (incomplete_deliveries_remaining > 0) {
-               CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, msgnum, NULL);    
+               CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, msgnum, "");
                msg = mallok(sizeof(struct CtdlMessage));
                memset(msg, 0, sizeof(struct CtdlMessage));
                msg->cm_magic = CTDLMESSAGE_MAGIC;
                msg->cm_anon_type = MES_NORMAL;
                msg->cm_format_type = FMT_RFC822;
-               msg->cm_fields['M'] = malloc(strlen(instr)+256);
-               sprintf(msg->cm_fields['M'],
-                       "Content-type: %s\n\n%s\nattempted|%ld\n",
-                       SPOOLMIME, instr, time(NULL) );
+               msg->cm_fields['M'] = malloc(strlen(instr)+SIZ);
+               snprintf(msg->cm_fields['M'],
+                       strlen(instr)+SIZ,
+                       "Content-type: %s\n\n%s\n"
+                       "attempted|%ld\n"
+                       "retry|%ld\n",
+                       SPOOLMIME, instr, time(NULL), retry );
                phree(instr);
-               CtdlSaveMsg(msg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL, 1);
+               CtdlSaveMsg(msg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
                CtdlFreeMessage(msg);
        }
 
@@ -1385,15 +1452,16 @@ void smtp_do_queue(void) {
        /* 
         * Go ahead and run the queue
         */
-       lprintf(5, "SMTP: processing outbound queue\n");
+       lprintf(7, "SMTP: processing outbound queue\n");
 
        if (getroom(&CC->quickroom, SMTP_SPOOLOUT_ROOM) != 0) {
                lprintf(3, "Cannot find room <%s>\n", SMTP_SPOOLOUT_ROOM);
                return;
        }
-       CtdlForEachMessage(MSGS_ALL, 0L, SPOOLMIME, NULL, smtp_do_procmsg);
+       CtdlForEachMessage(MSGS_ALL, 0L, (-127),
+               SPOOLMIME, NULL, smtp_do_procmsg, NULL);
 
-       lprintf(5, "SMTP: queue run completed\n");
+       lprintf(7, "SMTP: queue run completed\n");
        doing_queue = 0;
 }
 
@@ -1408,10 +1476,18 @@ char *Dynamic_Module_Init(void)
 {
        SYM_SMTP = CtdlGetDynamicSymbol();
        SYM_SMTP_RECP = CtdlGetDynamicSymbol();
-       CtdlRegisterServiceHook(SMTP_PORT,
+
+       CtdlRegisterServiceHook(config.c_smtp_port,     /* On the net... */
+                               NULL,
                                smtp_greeting,
                                smtp_command_loop);
-       create_room(SMTP_SPOOLOUT_ROOM, 3, "", 0);
+
+       CtdlRegisterServiceHook(0,                      /* ...and locally */
+                               "smtp.socket",
+                               smtp_greeting,
+                               smtp_command_loop);
+
+       create_room(SMTP_SPOOLOUT_ROOM, 3, "", 0, 1);
        CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER);
        return "$Id$";
 }