]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_smtp.c
* Added a new message function hook type EVT_SMTPSCAN which permits modules to
[citadel.git] / citadel / serv_smtp.c
index 1e54b7f07b9fa097daef125c3f08f877e062eb45..43896c28092e0f89180028ffc68e0623751c961e 100644 (file)
 #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"
 #include "clientsocket.h"
 
 
+#ifndef HAVE_SNPRINTF
+#include "snprintf.h"
+#endif
+
 struct citsmtp {               /* Information about the current session */
        int command_state;
        char helo_node[SIZ];
@@ -48,7 +62,9 @@ struct citsmtp {              /* Information about the current session */
        int vrfy_count;
        char vrfy_match[SIZ];
        char from[SIZ];
+       char recipients[SIZ];
        int number_of_recipients;
+       int number_of_rooms;
        int delivery_mode;
        int message_originated_locally;
 };
@@ -65,10 +81,14 @@ enum {                              /* Delivery modes */
 };
 
 #define SMTP           ((struct citsmtp *)CtdlGetUserData(SYM_SMTP))
-#define SMTP_RECP      ((char *)CtdlGetUserData(SYM_SMTP_RECP))
+#define SMTP_RECPS     ((char *)CtdlGetUserData(SYM_SMTP_RECPS))
+#define SMTP_ROOMS     ((char *)CtdlGetUserData(SYM_SMTP_ROOMS))
 
 long SYM_SMTP;
-long SYM_SMTP_RECP;
+long SYM_SMTP_RECPS;
+long SYM_SMTP_ROOMS;
+
+int run_queue_now = 0; /* Set to 1 to ignore SMTP send retry times */
 
 
 
@@ -88,11 +108,12 @@ void smtp_greeting(void) {
        CC->internal_pgm = 1;
        CC->cs_flags |= CS_STEALTH;
        CtdlAllocUserData(SYM_SMTP, sizeof(struct citsmtp));
-       CtdlAllocUserData(SYM_SMTP_RECP, SIZ);
-       sprintf(SMTP_RECP, "%s", "");
+       CtdlAllocUserData(SYM_SMTP_RECPS, SIZ);
+       CtdlAllocUserData(SYM_SMTP_ROOMS, SIZ);
+       snprintf(SMTP_RECPS, SIZ, "%s", "");
+       snprintf(SMTP_ROOMS, SIZ, "%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);
 }
 
 
@@ -110,6 +131,7 @@ void smtp_hello(char *argbuf, int is_esmtp) {
                cprintf("250-Greetings and joyous salutations.\r\n");
                cprintf("250-HELP\r\n");
                cprintf("250-SIZE %ld\r\n", config.c_maxmsglen);
+               cprintf("250-PIPELINING\r\n");
                cprintf("250 AUTH=LOGIN\r\n");
        }
 }
@@ -119,7 +141,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");
@@ -131,7 +153,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");
 }
 
 
@@ -142,7 +164,7 @@ void smtp_get_user(char *argbuf) {
        char buf[SIZ];
        char username[SIZ];
 
-       decode_base64(username, argbuf);
+       decode_base64(username, argbuf, SIZ);
        lprintf(9, "Trying <%s>\n", username);
        if (CtdlLoginExistingUser(username) == login_ok) {
                encode_base64(buf, "Password:");
@@ -162,10 +184,10 @@ void smtp_get_user(char *argbuf) {
 void smtp_get_pass(char *argbuf) {
        char password[SIZ];
 
-       decode_base64(password, argbuf);
+       decode_base64(password, argbuf, SIZ);
        lprintf(9, "Trying <%s>\n", password);
        if (CtdlTryPassword(password) == pass_ok) {
-               cprintf("235 Authentication successful.\r\n");
+               cprintf("235 Hello, %s\r\n", CC->usersupp.fullname);
                lprintf(9, "SMTP authenticated login successful\n");
                CC->internal_pgm = 0;
                CC->cs_flags &= ~CS_STEALTH;
@@ -288,8 +310,9 @@ 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);
+       if (CC->logged_in) {
+               logout(CC);
+       }
        cprintf("250 Zap!\r\n");
 }
 
@@ -299,10 +322,10 @@ void smtp_rset(void) {
  */
 void smtp_data_clear(void) {
        strcpy(SMTP->from, "");
+       strcpy(SMTP->recipients, "");
        SMTP->number_of_recipients = 0;
        SMTP->delivery_mode = 0;
        SMTP->message_originated_locally = 0;
-       if (SMTP_RECP != NULL) strcpy(SMTP_RECP, "");
 }
 
 
@@ -313,7 +336,8 @@ void smtp_data_clear(void) {
 void smtp_mail(char *argbuf) {
        char user[SIZ];
        char node[SIZ];
-       int cvt;
+       char name[SIZ];
+       struct recptypes *valid;
 
        if (strlen(SMTP->from) != 0) {
                cprintf("503 Only one sender permitted\r\n");
@@ -327,39 +351,39 @@ void smtp_mail(char *argbuf) {
 
        strcpy(SMTP->from, &argbuf[5]);
        striplt(SMTP->from);
+       stripallbut(SMTP->from, '<', '>');
 
        if (strlen(SMTP->from) == 0) {
                cprintf("501 Empty sender name is not permitted\r\n");
                return;
        }
 
-
        /* If this SMTP connection is from a logged-in user, make sure that
         * the user only sends email from his/her own address.
         */
        if (CC->logged_in) {
-               cvt = convert_internet_address(user, node, SMTP->from);
-               lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
-               if ( (cvt != 0) || (strcasecmp(user, CC->usersupp.fullname))) {
-                       cprintf("550 <%s> is not your address.\r\n", SMTP->from);
-                       strcpy(SMTP->from, "");
-                       return;
+               valid = validate_recipients(SMTP->from);
+               if ( (valid->num_local == 1) &&
+                  (!strcasecmp(valid->recp_local, CC->usersupp.fullname)) ) {
+                       cprintf("250 Sender ok <%s>\r\n", valid->recp_local);
+                       SMTP->message_originated_locally = 1;
                }
                else {
-                       SMTP->message_originated_locally = 1;
+                       cprintf("550 <%s> is not your address.\r\n",
+                               SMTP->from);
+                       strcpy(SMTP->from, "");
                }
+               phree(valid);
+               return;
        }
 
+
        /* 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;
+               process_rfc822_addr(SMTP->from, user, node, name);
+               if (CtdlHostAlias(node) != hostalias_nomatch) {
                        cprintf("550 You must log in to send mail from %s\r\n",
                                node);
                        strcpy(SMTP->from, "");
@@ -376,10 +400,8 @@ void smtp_mail(char *argbuf) {
  * Implements the "RCPT To:" command
  */
 void smtp_rcpt(char *argbuf) {
-       int cvt;
-       char user[SIZ];
-       char node[SIZ];
        char recp[SIZ];
+       struct recptypes *valid;
 
        if (strlen(SMTP->from) == 0) {
                cprintf("503 Need MAIL before RCPT\r\n");
@@ -393,236 +415,49 @@ void smtp_rcpt(char *argbuf) {
 
        strcpy(recp, &argbuf[3]);
        striplt(recp);
-       TRACE;
-       alias(recp);
-       TRACE;
-
-       cvt = convert_internet_address(user, node, recp);
-       snprintf(recp, sizeof recp, "%s@%s", user, node);
-       lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
-
-       switch(cvt) {
-               case rfc822_address_locally_validated:
-                       cprintf("250 %s is a valid recipient.\r\n", user);
-                       ++SMTP->number_of_recipients;
-                       CtdlReallocUserData(SYM_SMTP_RECP,
-                               strlen(SMTP_RECP) + 1024 );
-                       strcat(SMTP_RECP, "local|");
-                       strcat(SMTP_RECP, user);
-                       strcat(SMTP_RECP, "|0\n");
-                       return;
-
-               case rfc822_room_delivery:
-                       cprintf("250 Delivering to room '%s'\r\n", user);
-                       ++SMTP->number_of_recipients;
-                       CtdlReallocUserData(SYM_SMTP_RECP,
-                               strlen(SMTP_RECP) + 1024 );
-                       strcat(SMTP_RECP, "room|");
-                       strcat(SMTP_RECP, user);
-                       strcat(SMTP_RECP, "|0|\n");
-                       return;
-
-               case rfc822_no_such_user:
-                       cprintf("550 %s: no such user\r\n", recp);
-                       return;
-
-               case rfc822_address_on_citadel_network:
-                       cprintf("250 %s is on the local network\r\n", recp);
-                       ++SMTP->number_of_recipients;
-                       CtdlReallocUserData(SYM_SMTP_RECP,
-                               strlen(SMTP_RECP) + 1024 );
-                       strcat(SMTP_RECP, "ignet|");
-                       strcat(SMTP_RECP, user);
-                       strcat(SMTP_RECP, "|");
-                       strcat(SMTP_RECP, node);
-                       strcat(SMTP_RECP, "|0|\n");
-                       return;
+       stripallbut(recp, '<', '>');
 
-               case rfc822_address_nonlocal:
-                       if (SMTP->message_originated_locally == 0) {
-                               cprintf("551 Relaying denied\r\n");
-                       }
-                       else {
-                               cprintf("250 Remote recipient %s ok\r\n", recp);
-                               ++SMTP->number_of_recipients;
-                               CtdlReallocUserData(SYM_SMTP_RECP,
-                                       strlen(SMTP_RECP) + 1024 );
-                               strcat(SMTP_RECP, "remote|");
-                               strcat(SMTP_RECP, recp);
-                               strcat(SMTP_RECP, "|0|\n");
-                               return;
-                       }
-                       return;
+       if ( (strlen(recp) + strlen(SMTP->recipients) + 1 ) >= SIZ) {
+               cprintf("452 Too many recipients\r\n");
+               return;
        }
 
-       cprintf("599 Unknown error\r\n");
-}
-
-
-
-/*
- * Send a message out through the local network
- * (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 *dest) {
-       struct ser_ret smr;
-       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'] = 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) {
-               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);
-               }
-               phree(smr.ser);
+       valid = validate_recipients(recp);
+       if (valid->num_error > 0) {
+               cprintf("599 Error: %s\r\n", valid->errormsg);
+               phree(valid);
+               return;
        }
 
-}
-
-
-
-/*
- * Back end for smtp_data()  ... this does the actual delivery of the message
- * Returns 0 on success, nonzero on failure
- */
-int smtp_message_delivery(struct CtdlMessage *msg) {
-       char user[1024];
-       char node[1024];
-       char name[1024];
-       char buf[1024];
-       char dtype[1024];
-       char room[1024];
-       int successful_saves = 0;       /* number of successful local saves */
-       int failed_saves = 0;           /* number of failed deliveries */
-       int remote_spools = 0;          /* number of copies to send out */
-       long msgid = (-1L);
-       int i;
-       struct usersupp userbuf;
-       char *instr;                    /* Remote delivery instructions */
-       struct CtdlMessage *imsg;
-
-       lprintf(9, "smtp_message_delivery() called\n");
-
-       /* Fill in 'from' fields with envelope information if missing */
-       process_rfc822_addr(SMTP->from, user, node, name);
-       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);
-       ++successful_saves;
-
-       instr = mallok(1024);
-       snprintf(instr, 1024,
-                       "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
-                       "bounceto|%s\n",
-               SPOOLMIME, msgid, time(NULL),
-               SMTP->from );
-
-       for (i=0; i<SMTP->number_of_recipients; ++i) {
-               extract_token(buf, SMTP_RECP, i, '\n');
-               extract(dtype, buf, 0);
-
-               /* Stuff local mailboxes */
-               if (!strcasecmp(dtype, "local")) {
-                       extract(user, buf, 1);
-                       if (getuser(&userbuf, user) == 0) {
-                               MailboxName(room, &userbuf, MAILROOM);
-                               CtdlSaveMsgPointerInRoom(room, msgid, 0);
-                               ++successful_saves;
-                       }
-                       else {
-                               ++failed_saves;
-                       }
-               }
-
-               /* Delivery to local non-mailbox rooms */
-               if (!strcasecmp(dtype, "room")) {
-                       extract(room, buf, 1);
-                       CtdlSaveMsgPointerInRoom(room, msgid, 0);
-                       ++successful_saves;
-               }
-
-               /* Delivery over the local Citadel network (IGnet) */
-               if (!strcasecmp(dtype, "ignet")) {
-                       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);
-                       snprintf(&instr[strlen(instr)],
-                               strlen(instr) + 1024,
-                               "remote|%s|0\n",
-                               user);
-                       ++remote_spools;
+       if (valid->num_internet > 0) {
+               if (SMTP->message_originated_locally == 0) {
+                       cprintf("551 Relaying denied <%s>\r\n", recp);
+                       phree(valid);
+                       return;
                }
-
        }
 
-       /* If there are remote spools to be done, save the instructions */
-       if (remote_spools > 0) {
-               imsg = mallok(sizeof(struct CtdlMessage));
-               memset(imsg, 0, sizeof(struct CtdlMessage));
-               imsg->cm_magic = CTDLMESSAGE_MAGIC;
-               imsg->cm_anon_type = MES_NORMAL;
-               imsg->cm_format_type = FMT_RFC822;
-               imsg->cm_fields['M'] = instr;
-               CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
-               CtdlFreeMessage(imsg);
+       cprintf("250 RCPT ok <%s>\r\n", recp);
+       if (strlen(SMTP->recipients) > 0) {
+               strcat(SMTP->recipients, ",");
        }
-
-       /* If there are no remote spools, delete the message */ 
-       else {
-               phree(instr);   /* only needed here, because CtdlSaveMsg()
-                                * would free this buffer otherwise */
-               CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, msgid, ""); 
-       }
-
-       return(failed_saves);
+       strcat(SMTP->recipients, recp);
+       SMTP->number_of_recipients += 1;
 }
 
 
 
+
 /*
  * Implements the DATA command
  */
 void smtp_data(void) {
        char *body;
        struct CtdlMessage *msg;
-       int retval;
+       long msgnum;
        char nowstamp[SIZ];
+       struct recptypes *valid;
+       int scan_errors;
 
        if (strlen(SMTP->from) == 0) {
                cprintf("503 Need MAIL command first.\r\n");
@@ -636,20 +471,23 @@ void smtp_data(void) {
 
        cprintf("354 Transmit message now; terminate with '.' by itself\r\n");
        
-       datestring(nowstamp, time(NULL), DATESTRING_RFC822);
+       datestring(nowstamp, sizeof nowstamp, time(NULL), DATESTRING_RFC822);
        body = mallok(4096);
 
+       /* FIXME
+          it should be Received: from %s (real.name.dom [w.x.y.z])
+        */
        if (body != NULL) snprintf(body, 4096,
-               "Received: from %s\n"
-               "       by %s;\n"
-               "       %s\n",
+               "Received: from %s (%s)\n"
+               "       by %s; %s\n",
                        SMTP->helo_node,
+                       CC->cs_host,
                        config.c_fqdn,
                        nowstamp);
        
        body = CtdlReadMessageBody(".", config.c_maxmsglen, body);
        if (body == NULL) {
-               cprintf("550 Unable to save message text: internal error.\r\n");
+               cprintf("550 Unable to save message: internal error.\r\n");
                return;
        }
 
@@ -657,7 +495,8 @@ void smtp_data(void) {
        msg = convert_internet_message(body);
 
        /* If the user is locally authenticated, FORCE the From: header to
-        * show up as the real sender
+        * show up as the real sender.  Yes, this violates the RFC standard,
+        * but IT MAKES SENSE.  Comment it out if you don't like this behavior.
         */
        if (CC->logged_in) {
                if (msg->cm_fields['A'] != NULL) phree(msg->cm_fields['A']);
@@ -668,16 +507,37 @@ void smtp_data(void) {
                msg->cm_fields['H'] = strdoop(config.c_humannode);
        }
 
-       retval = smtp_message_delivery(msg);
-       CtdlFreeMessage(msg);
+       /* Submit the message into the Citadel system. */
+       valid = validate_recipients(SMTP->recipients);
+
+       /* If there are modules that want to scan this message before final
+        * submission (such as virus checkers or spam filters), call them now
+        * and give them an opportunity to reject the message.
+        */
+       scan_errors = PerformMessageHooks(msg, EVT_SMTPSCAN);
+
+       if (scan_errors > 0) {  /* We don't want this message! */
+
+               if (msg->cm_fields['0'] == NULL) {
+                       msg->cm_fields['0'] = strdoop(
+                               "Message rejected by filter");
+               }
 
-       if (!retval) {
-               cprintf("250 Message accepted for delivery.\r\n");
+               cprintf("552 %s\r\n", msg->cm_fields['0']);
        }
-       else {
-               cprintf("550 Internal delivery errors: %d\r\n", retval);
+       
+       else {                  /* Ok, we'll accept this message. */
+               msgnum = CtdlSubmitMsg(msg, valid, "");
+               if (msgnum > 0L) {
+                       cprintf("250 Message accepted.\r\n");
+               }
+               else {
+                       cprintf("550 Internal delivery error\r\n");
+               }
        }
 
+       CtdlFreeMessage(msg);
+       phree(valid);
        smtp_data_clear();      /* clear out the buffers now */
 }
 
@@ -697,7 +557,7 @@ void smtp_command_loop(void) {
                CC->kill_me = 1;
                return;
        }
-       lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
+       lprintf(5, "SMTP: %s\n", cmdbuf);
        while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
 
        if (SMTP->command_state == smtp_user) {
@@ -737,14 +597,14 @@ void smtp_command_loop(void) {
        }
 
        else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
-               cprintf("250 This command successfully did nothing.\r\n");
+               cprintf("250 NOOP\r\n");
        }
 
        else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
                cprintf("221 Goodbye...\r\n");
                CC->kill_me = 1;
                return;
-               }
+       }
 
        else if (!strncasecmp(cmdbuf, "RCPT", 4)) {
                smtp_rcpt(&cmdbuf[5]);
@@ -759,7 +619,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");
        }
 
 }
@@ -779,7 +639,8 @@ void smtp_command_loop(void) {
  * Called by smtp_do_procmsg() to attempt delivery to one SMTP host
  *
  */
-void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
+void smtp_try(const char *key, const char *addr, int *status,
+             char *dsn, size_t n, long msgnum)
 {
        int sock = (-1);
        char mxhosts[1024];
@@ -805,7 +666,7 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        msg_fp = tmpfile();
        if (msg_fp == NULL) {
                *status = 4;
-               sprintf(dsn, "Error creating temporary file");
+               snprintf(dsn, n, "Error creating temporary file");
                return;
        }
        else {
@@ -851,7 +712,7 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
                                if (mailfrom[i] == '<') lp = i;
                                if (mailfrom[i] == '>') rp = i;
                        }
-                       if ((lp>=0)&&(rp>lp)) {
+                       if ( (lp>=0) && (rp>lp) ) {
                                mailfrom[rp] = 0;
                                strcpy(mailfrom, &mailfrom[lp]);
                        }
@@ -861,7 +722,6 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        } while (scan_done == 0);
        if (strlen(mailfrom)==0) strcpy(mailfrom, "someone@somewhere.org");
 
-
        /* Figure out what mail exchanger host we have to connect to */
        num_mxhosts = getmx(mxhosts, node);
        lprintf(9, "Number of MX hosts for <%s> is %d\n", node, num_mxhosts);
@@ -909,12 +769,12 @@ 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 */
-       snprintf(buf, sizeof buf, "HELO %s", config.c_fqdn);
-       lprintf(9, ">%s\n", buf);
-       sock_puts_crlf(sock, buf);
+       snprintf(buf, sizeof buf, "HELO %s\r\n", config.c_fqdn);
+       lprintf(9, ">%s", buf);
+       sock_write(sock, buf, strlen(buf));
        if (ml_sock_gets(sock, buf) < 0) {
                *status = 4;
-               strcpy(dsn, "Connection broken during SMTP conversation");
+               strcpy(dsn, "Connection broken during SMTP HELO");
                goto bail;
        }
        lprintf(9, "<%s\n", buf);
@@ -933,12 +793,12 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
 
 
        /* HELO succeeded, now try the MAIL From: command */
-       snprintf(buf, sizeof buf, "MAIL From: <%s>", mailfrom);
-       lprintf(9, ">%s\n", buf);
-       sock_puts_crlf(sock, buf);
+       snprintf(buf, sizeof buf, "MAIL From: <%s>\r\n", mailfrom);
+       lprintf(9, ">%s", buf);
+       sock_write(sock, buf, strlen(buf));
        if (ml_sock_gets(sock, buf) < 0) {
                *status = 4;
-               strcpy(dsn, "Connection broken during SMTP conversation");
+               strcpy(dsn, "Connection broken during SMTP MAIL");
                goto bail;
        }
        lprintf(9, "<%s\n", buf);
@@ -957,12 +817,12 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
 
 
        /* MAIL succeeded, now try the RCPT To: command */
-       snprintf(buf, sizeof buf, "RCPT To: <%s>", addr);
-       lprintf(9, ">%s\n", buf);
-       sock_puts_crlf(sock, buf);
+       snprintf(buf, sizeof buf, "RCPT To: <%s>\r\n", addr);
+       lprintf(9, ">%s", buf);
+       sock_write(sock, buf, strlen(buf));
        if (ml_sock_gets(sock, buf) < 0) {
                *status = 4;
-               strcpy(dsn, "Connection broken during SMTP conversation");
+               strcpy(dsn, "Connection broken during SMTP RCPT");
                goto bail;
        }
        lprintf(9, "<%s\n", buf);
@@ -982,10 +842,10 @@ 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_crlf(sock, "DATA");
+       sock_write(sock, "DATA\r\n", 6);
        if (ml_sock_gets(sock, buf) < 0) {
                *status = 4;
-               strcpy(dsn, "Connection broken during SMTP conversation");
+               strcpy(dsn, "Connection broken during SMTP DATA");
                goto bail;
        }
        lprintf(9, "<%s\n", buf);
@@ -1020,7 +880,7 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        sock_write(sock, ".\r\n", 3);
        if (ml_sock_gets(sock, buf) < 0) {
                *status = 4;
-               strcpy(dsn, "Connection broken during SMTP conversation");
+               strcpy(dsn, "Connection broken during SMTP message transmit");
                goto bail;
        }
        lprintf(9, "%s\n", buf);
@@ -1042,7 +902,7 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        *status = 2;
 
        lprintf(9, ">QUIT\n");
-       sock_puts_crlf(sock, "QUIT");
+       sock_write(sock, "QUIT\r\n", 6);
        ml_sock_gets(sock, buf);
        lprintf(9, "<%s\n", buf);
 
@@ -1073,7 +933,8 @@ void smtp_do_bounce(char *instr) {
        time_t submitted = 0L;
        struct CtdlMessage *bmsg = NULL;
        int give_up = 0;
-       int mes_type = 0;
+       struct recptypes *valid;
+       int successful_bounce = 0;
 
        lprintf(9, "smtp_do_bounce() called\n");
        strcpy(bounceto, "");
@@ -1175,27 +1036,29 @@ 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);
                }
-               else if (mes_type = alias(bounceto), mes_type == MES_ERROR) {
-                       lprintf(7, "Invalid bounce address <%s>\n", bounceto);
-                       bounce_msgid = (-1L);
+
+               /* Can we deliver the bounce to the original sender? */
+               valid = validate_recipients(bounceto);
+               if (valid != NULL) {
+                       if (valid->num_error == 0) {
+                               CtdlSubmitMsg(bmsg, valid, "");
+                               successful_bounce = 1;
+                       }
                }
-               else {
-                       bounce_msgid = CtdlSaveMsg(bmsg,
-                               bounceto,
-                               "", mes_type);
+
+               /* If not, post it in the Aide> room */
+               if (successful_bounce == 0) {
+                       CtdlSubmitMsg(bmsg, NULL, AIDEROOM);
                }
-               TRACE;
 
-               /* Otherwise, go to the Aide> room */
-               lprintf(9, "bounce to room?\n");
-               if (bounce_msgid < 0L) bounce_msgid = CtdlSaveMsg(bmsg,
-                       "", AIDEROOM,
-                       MES_LOCAL);
+               /* Free up the memory we used */
+               if (valid != NULL) {
+                       phree(valid);
+               }
        }
 
        CtdlFreeMessage(bmsg);
@@ -1273,6 +1136,8 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
        time_t last_attempted = 0L;
        time_t retry = SMTP_RETRY_INTERVAL;
 
+       lprintf(9, "smtp_do_procmsg(%ld)\n", msgnum);
+
        msg = CtdlFetchMessage(msgnum);
        if (msg == NULL) {
                lprintf(3, "SMTP: tried %ld but no such message!\n", msgnum);
@@ -1287,7 +1152,6 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
        for (i=0; i<lines; ++i) {
                extract_token(buf, instr, i, '\n');
                if (num_tokens(buf, '|') < 2) {
-                       lprintf(9, "removing <%s>\n", buf);
                        remove_token(instr, i, '\n');
                        --lines;
                        --i;
@@ -1305,6 +1169,9 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
                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")) {
@@ -1314,11 +1181,10 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
                }
        }
 
-
        /*
         * Postpone delivery if we've already tried recently.
         */
-       if ( (time(NULL) - last_attempted) < retry) {
+       if (((time(NULL) - last_attempted) < retry) && (run_queue_now == 0)) {
                lprintf(7, "Retry time not yet reached.\n");
                phree(instr);
                return;
@@ -1335,7 +1201,7 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
        }
 
        /* Plow through the instructions looking for 'remote' directives and
-        * a status of 0 (no delivery yet attempted) or 3 (transient errors
+        * a status of 0 (no delivery yet attempted) or 3/4 (transient errors
         * were experienced and it's time to try again)
         */
        lines = num_tokens(instr, '\n');
@@ -1346,12 +1212,12 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
                status = extract_int(buf, 2);
                extract(dsn, buf, 3);
                if ( (!strcasecmp(key, "remote"))
-                  && ((status==0)||(status==3)) ) {
+                  && ((status==0)||(status==3)||(status==4)) ) {
                        remove_token(instr, i, '\n');
                        --i;
                        --lines;
                        lprintf(9, "SMTP: Trying <%s>\n", addr);
-                       smtp_try(key, addr, &status, dsn, text_msgid);
+                       smtp_try(key, addr, &status, dsn, sizeof dsn, text_msgid);
                        if (status != 2) {
                                if (results == NULL) {
                                        results = mallok(1024);
@@ -1361,7 +1227,7 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
                                        results = reallok(results,
                                                strlen(results) + 1024);
                                }
-                               sprintf(&results[strlen(results)],
+                               snprintf(&results[strlen(results)], 1024,
                                        "%s|%s|%d|%s\n",
                                        key, addr, status, dsn);
                        }
@@ -1410,9 +1276,9 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
                        "Content-type: %s\n\n%s\n"
                        "attempted|%ld\n"
                        "retry|%ld\n",
-                       SPOOLMIME, instr, time(NULL), retry );
+                       SPOOLMIME, instr, (long)time(NULL), (long)retry );
                phree(instr);
-               CtdlSaveMsg(msg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
+               CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
                CtdlFreeMessage(msg);
        }
 
@@ -1446,15 +1312,59 @@ void smtp_do_queue(void) {
                lprintf(3, "Cannot find room <%s>\n", SMTP_SPOOLOUT_ROOM);
                return;
        }
-       CtdlForEachMessage(MSGS_ALL, 0L, (-127),
+       CtdlForEachMessage(MSGS_ALL, 0L,
                SPOOLMIME, NULL, smtp_do_procmsg, NULL);
 
        lprintf(7, "SMTP: queue run completed\n");
+       run_queue_now = 0;
        doing_queue = 0;
 }
 
 
 
+/*****************************************************************************/
+/*                          SMTP UTILITY COMMANDS                            */
+/*****************************************************************************/
+
+void cmd_smtp(char *argbuf) {
+       char cmd[SIZ];
+       char node[SIZ];
+       char buf[SIZ];
+       int i;
+       int num_mxhosts;
+
+       if (CtdlAccessCheck(ac_aide)) return;
+
+       extract(cmd, argbuf, 0);
+
+       if (!strcasecmp(cmd, "mx")) {
+               extract(node, argbuf, 1);
+               num_mxhosts = getmx(buf, node);
+               cprintf("%d %d MX hosts listed for %s\n",
+                       LISTING_FOLLOWS, num_mxhosts, node);
+               for (i=0; i<num_mxhosts; ++i) {
+                       extract(node, buf, i);
+                       cprintf("%s\n", node);
+               }
+               cprintf("000\n");
+               return;
+       }
+
+       else if (!strcasecmp(cmd, "runqueue")) {
+               run_queue_now = 1;
+               cprintf("%d All outbound SMTP will be retried now.\n", CIT_OK);
+               return;
+       }
+
+       else {
+               cprintf("%d Invalid command.\n", ERROR+ILLEGAL_VALUE);
+       }
+
+}
+
+
+
+
 /*****************************************************************************/
 /*                      MODULE INITIALIZATION STUFF                          */
 /*****************************************************************************/
@@ -1463,7 +1373,6 @@ void smtp_do_queue(void) {
 char *Dynamic_Module_Init(void)
 {
        SYM_SMTP = CtdlGetDynamicSymbol();
-       SYM_SMTP_RECP = CtdlGetDynamicSymbol();
 
        CtdlRegisterServiceHook(config.c_smtp_port,     /* On the net... */
                                NULL,
@@ -1475,7 +1384,8 @@ char *Dynamic_Module_Init(void)
                                smtp_greeting,
                                smtp_command_loop);
 
-       create_room(SMTP_SPOOLOUT_ROOM, 3, "", 0, 1);
+       create_room(SMTP_SPOOLOUT_ROOM, 3, "", 0, 1, 0);
        CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER);
+       CtdlRegisterProtoHook(cmd_smtp, "SMTP", "SMTP utility commands");
        return "$Id$";
 }