]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_smtp.c
* misc bugfixes and cleanups
[citadel.git] / citadel / serv_smtp.c
index 8dd3b6554ed52ff263d4466fbfb336f350a501e2..99504c5842276e3a5866bad4ad115bf4b0d33627 100644 (file)
@@ -30,7 +30,6 @@
 #include "msgbase.h"
 #include "tools.h"
 #include "internet_addressing.h"
-#include "ignet.h"
 #include "genstamp.h"
 #include "domain.h"
 #include "clientsocket.h"
@@ -45,6 +44,7 @@ struct citsmtp {              /* Information about the current session */
        char from[256];
        int number_of_recipients;
        int delivery_mode;
+       int message_originated_locally;
 };
 
 enum {                         /* Command states for login authentication */
@@ -326,6 +326,9 @@ 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
@@ -342,7 +345,7 @@ void smtp_mail(char *argbuf) {
                }
        }
 
-       cprintf("250 Sender ok.  Groovy.\r\n");
+       cprintf("250 Sender ok\r\n");
 }
 
 
@@ -355,10 +358,9 @@ void smtp_rcpt(char *argbuf) {
        char user[256];
        char node[256];
        char recp[256];
-       int is_spam = 0;        /* FIXME implement anti-spamming */
 
        if (strlen(SMTP->from) == 0) {
-               cprintf("503 MAIL first, then RCPT.  Duh.\r\n");
+               cprintf("503 Need MAIL before RCPT\r\n");
                return;
        }
 
@@ -372,7 +374,8 @@ void smtp_rcpt(char *argbuf) {
        alias(recp);
 
        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) {
                case rfc822_address_locally_validated:
@@ -399,9 +402,21 @@ void smtp_rcpt(char *argbuf) {
                        cprintf("550 %s: no such user\r\n", recp);
                        return;
 
-               case rfc822_address_invalid:
-                       if (is_spam) {
-                               cprintf("551 Away with thee, spammer!\r\n");
+               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;
+
+               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);
@@ -421,6 +436,48 @@ void smtp_rcpt(char *argbuf) {
 
 
 
+/*
+ * 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[256];
+       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);
+       }
+
+}
+
 
 
 /*
@@ -450,6 +507,7 @@ 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,
@@ -460,7 +518,8 @@ int smtp_message_delivery(struct CtdlMessage *msg) {
        ++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 );
@@ -489,11 +548,19 @@ int smtp_message_delivery(struct CtdlMessage *msg) {
                        ++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);
-                       sprintf(&instr[strlen(instr)],
+                       snprintf(&instr[strlen(instr)],
+                               strlen(instr) + 1024,
                                "remote|%s|0\n",
                                user);
                        ++remote_spools;
@@ -549,7 +616,7 @@ void smtp_data(void) {
        generate_rfc822_datestamp(nowstamp, time(NULL));
        body = mallok(4096);
 
-       if (body != NULL) sprintf(body,
+       if (body != NULL) snprintf(body, 4096,
                "Received: from %s\n"
                "       by %s;\n"
                "       %s\n",
@@ -706,18 +773,6 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        /* Parse out the host portion of the recipient address */
        process_rfc822_addr(addr, user, node, name);
 
-       if (is_ignet(node)) {
-               if (ignet_spool_to(node, msgnum) == 0) {
-                       strcpy(dsn, "Delivery via Citadel network successful");
-                       *status = 2;
-               }
-               else {
-                       strcpy(dsn, "Delivery via Citadel network failed");
-                       *status = 5;
-               }
-               return;
-       }
-
        lprintf(9, "Attempting SMTP delivery to <%s> @ <%s> (%s)\n",
                user, node, name);
 
@@ -787,7 +842,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, 256, "No MX hosts found for <%s>", node);
                return;
        }
 
@@ -795,9 +850,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, 256, "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, 256, "%s", strerror(errno));
                if (sock >= 0) break;
        }
 
@@ -816,12 +871,12 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        if (buf[0] != '2') {
                if (buf[0] == '4') {
                        *status = 4;
-                       strcpy(dsn, &buf[4]);
+                       safestrncpy(dsn, &buf[4], 1023);
                        goto bail;
                }
                else {
                        *status = 5;
-                       strcpy(dsn, &buf[4]);
+                       safestrncpy(dsn, &buf[4], 1023);
                        goto bail;
                }
        }
@@ -829,7 +884,7 @@ 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) {
@@ -841,19 +896,19 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        if (buf[0] != '2') {
                if (buf[0] == '4') {
                        *status = 4;
-                       strcpy(dsn, &buf[4]);
+                       safestrncpy(dsn, &buf[4], 1023);
                        goto bail;
                }
                else {
                        *status = 5;
-                       strcpy(dsn, &buf[4]);
+                       safestrncpy(dsn, &buf[4], 1023);
                        goto bail;
                }
        }
 
 
        /* 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) {
@@ -865,19 +920,19 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        if (buf[0] != '2') {
                if (buf[0] == '4') {
                        *status = 4;
-                       strcpy(dsn, &buf[4]);
+                       safestrncpy(dsn, &buf[4], 1023);
                        goto bail;
                }
                else {
                        *status = 5;
-                       strcpy(dsn, &buf[4]);
+                       safestrncpy(dsn, &buf[4], 1023);
                        goto bail;
                }
        }
 
 
        /* 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) {
@@ -889,12 +944,12 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        if (buf[0] != '2') {
                if (buf[0] == '4') {
                        *status = 4;
-                       strcpy(dsn, &buf[4]);
+                       safestrncpy(dsn, &buf[4], 1023);
                        goto bail;
                }
                else {
                        *status = 5;
-                       strcpy(dsn, &buf[4]);
+                       safestrncpy(dsn, &buf[4], 1023);
                        goto bail;
                }
        }
@@ -912,12 +967,12 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        if (buf[0] != '3') {
                if (buf[0] == '4') {
                        *status = 3;
-                       strcpy(dsn, &buf[4]);
+                       safestrncpy(dsn, &buf[4], 1023);
                        goto bail;
                }
                else {
                        *status = 5;
-                       strcpy(dsn, &buf[4]);
+                       safestrncpy(dsn, &buf[4], 1023);
                        goto bail;
                }
        }
@@ -947,18 +1002,18 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        if (buf[0] != '2') {
                if (buf[0] == '4') {
                        *status = 4;
-                       strcpy(dsn, &buf[4]);
+                       safestrncpy(dsn, &buf[4], 1023);
                        goto bail;
                }
                else {
                        *status = 5;
-                       strcpy(dsn, &buf[4]);
+                       safestrncpy(dsn, &buf[4], 1023);
                        goto bail;
                }
        }
 
        /* We did it! */
-       strcpy(dsn, &buf[4]);
+       safestrncpy(dsn, &buf[4], 1023);
        *status = 2;
 
        lprintf(9, ">QUIT\n");
@@ -1029,14 +1084,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"
 );
 
        /*
@@ -1317,7 +1372,8 @@ void smtp_do_procmsg(long msgnum) {
                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'],
+               snprintf(msg->cm_fields['M'],
+                       strlen(instr)+256,
                        "Content-type: %s\n\n%s\nattempted|%ld\n",
                        SPOOLMIME, instr, time(NULL) );
                phree(instr);
@@ -1372,9 +1428,17 @@ 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);
+
+       CtdlRegisterServiceHook(0,                      /* ...and locally */
+                               "smtp.socket",
+                               smtp_greeting,
+                               smtp_command_loop);
+
        create_room(SMTP_SPOOLOUT_ROOM, 3, "", 0);
        CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER);
        return "$Id$";