]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_smtp.c
* Backed out the variable-length string changes.
[citadel.git] / citadel / serv_smtp.c
index 81d90956d56d6431342750c98491cb7cbc6a7626..1e54b7f07b9fa097daef125c3f08f877e062eb45 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>
 
 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;
@@ -82,7 +88,7 @@ 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",
@@ -133,8 +139,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);
@@ -154,7 +160,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);
@@ -175,7 +181,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");
@@ -282,18 +288,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) {
@@ -335,9 +354,12 @@ void smtp_mail(char *argbuf) {
         * 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, "");
@@ -355,9 +377,9 @@ void smtp_mail(char *argbuf) {
  */
 void smtp_rcpt(char *argbuf) {
        int cvt;
-       char user[256];
-       char node[256];
-       char recp[256];
+       char user[SIZ];
+       char node[SIZ];
+       char recp[SIZ];
 
        if (strlen(SMTP->from) == 0) {
                cprintf("503 Need MAIL before RCPT\r\n");
@@ -371,7 +393,9 @@ 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);
@@ -445,7 +469,7 @@ 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[256];
+       char filename[SIZ];
        static int seq = 0;
 
        lprintf(9, "smtp_deliver_ignet(msg, %s, %s)\n", user, dest);
@@ -583,7 +607,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);
@@ -598,7 +622,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");
@@ -612,7 +636,7 @@ 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) snprintf(body, 4096,
@@ -653,6 +677,8 @@ void smtp_data(void) {
        else {
                cprintf("550 Internal delivery errors: %d\r\n", retval);
        }
+
+       smtp_data_clear();      /* clear out the buffers now */
 }
 
 
@@ -662,7 +688,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 */
@@ -760,7 +786,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;
@@ -841,7 +867,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;
-               snprintf(dsn, 256, "No MX hosts found for <%s>", node);
+               snprintf(dsn, SIZ, "No MX hosts found for <%s>", node);
                return;
        }
 
@@ -849,9 +875,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");
-               snprintf(dsn, 256, "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) snprintf(dsn, 256, "%s", strerror(errno));
+               if (sock < 0) snprintf(dsn, SIZ, "%s", strerror(errno));
                if (sock >= 0) break;
        }
 
@@ -1149,6 +1175,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);
@@ -1162,6 +1189,7 @@ void smtp_do_bounce(char *instr) {
                                bounceto,
                                "", mes_type);
                }
+               TRACE;
 
                /* Otherwise, go to the Aide> room */
                lprintf(9, "bounce to room?\n");
@@ -1228,7 +1256,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;
@@ -1243,6 +1271,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) {
@@ -1273,6 +1302,11 @@ 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;
+                       remove_token(instr, i, '\n');
+               }
                if (!strcasecmp(key, "attempted")) {
                        attempted = extract_long(buf, 1);
                        if (attempted > last_attempted)
@@ -1284,7 +1318,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;
@@ -1353,9 +1387,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, "");
        }
 
 
@@ -1364,17 +1398,19 @@ 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);
+               msg->cm_fields['M'] = malloc(strlen(instr)+SIZ);
                snprintf(msg->cm_fields['M'],
-                       strlen(instr)+256,
-                       "Content-type: %s\n\n%s\nattempted|%ld\n",
-                       SPOOLMIME, instr, time(NULL) );
+                       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);
                CtdlFreeMessage(msg);
@@ -1410,7 +1446,8 @@ void smtp_do_queue(void) {
                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(7, "SMTP: queue run completed\n");
        doing_queue = 0;
@@ -1438,7 +1475,7 @@ char *Dynamic_Module_Init(void)
                                smtp_greeting,
                                smtp_command_loop);
 
-       create_room(SMTP_SPOOLOUT_ROOM, 3, "", 0);
+       create_room(SMTP_SPOOLOUT_ROOM, 3, "", 0, 1);
        CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER);
        return "$Id$";
 }