]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_smtp.c
* Added a site-configurable setting to suppress the automatic correction of
[citadel.git] / citadel / serv_smtp.c
index 7334ca0fbe406986f67d6245b812affd01c13187..afb6bc0dc234c38778bd4c6d6f68d5612a44f3ca 100644 (file)
@@ -31,6 +31,9 @@
 #include <ctype.h>
 #include <string.h>
 #include <limits.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
 #include "citadel.h"
 #include "server.h"
 #include "sysdep_decls.h"
@@ -38,7 +41,7 @@
 #include "support.h"
 #include "config.h"
 #include "control.h"
-#include "dynloader.h"
+#include "serv_extensions.h"
 #include "room_ops.h"
 #include "user_ops.h"
 #include "policy.h"
@@ -49,6 +52,7 @@
 #include "genstamp.h"
 #include "domain.h"
 #include "clientsocket.h"
+#include "locate_host.h"
 
 
 #ifndef HAVE_SNPRINTF
@@ -164,10 +168,10 @@ void smtp_get_user(char *argbuf) {
        char buf[SIZ];
        char username[SIZ];
 
-       decode_base64(username, argbuf, SIZ);
+       CtdlDecodeBase64(username, argbuf, SIZ);
        lprintf(9, "Trying <%s>\n", username);
        if (CtdlLoginExistingUser(username) == login_ok) {
-               encode_base64(buf, "Password:");
+               CtdlEncodeBase64(buf, "Password:", 9);
                cprintf("334 %s\r\n", buf);
                SMTP->command_state = smtp_password;
        }
@@ -184,7 +188,7 @@ void smtp_get_user(char *argbuf) {
 void smtp_get_pass(char *argbuf) {
        char password[SIZ];
 
-       decode_base64(password, argbuf, SIZ);
+       CtdlDecodeBase64(password, argbuf, SIZ);
        lprintf(9, "Trying <%s>\n", password);
        if (CtdlTryPassword(password) == pass_ok) {
                cprintf("235 Hello, %s\r\n", CC->usersupp.fullname);
@@ -215,7 +219,7 @@ void smtp_auth(char *argbuf) {
        }
 
        else {
-               encode_base64(buf, "Username:");
+               CtdlEncodeBase64(buf, "Username:", 9);
                cprintf("334 %s\r\n", buf);
                SMTP->command_state = smtp_user;
        }
@@ -310,9 +314,18 @@ void smtp_expn(char *argbuf) {
  */
 void smtp_rset(void) {
        memset(SMTP, 0, sizeof(struct citsmtp));
-       if (CC->logged_in) {
-               logout(CC);
-       }
+
+       /*
+        * It is somewhat ambiguous whether we want to log out when a RSET
+        * command is issued.  Here's the code to do it.  It is commented out
+        * because some clients (such as Pine) issue RSET commands before
+        * each message, but still expect to be logged in.
+        *
+        * if (CC->logged_in) {
+        *      logout(CC);
+        * }
+        */
+
        cprintf("250 Zap!\r\n");
 }
 
@@ -337,7 +350,6 @@ void smtp_mail(char *argbuf) {
        char user[SIZ];
        char node[SIZ];
        char name[SIZ];
-       struct recptypes *valid;
 
        if (strlen(SMTP->from) != 0) {
                cprintf("503 Only one sender permitted\r\n");
@@ -358,26 +370,16 @@ void smtp_mail(char *argbuf) {
                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 this SMTP connection is from a logged-in user, force the 'from'
+        * to be the user's Internet e-mail address as Citadel knows it.
         */
        if (CC->logged_in) {
-               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 {
-                       cprintf("550 <%s> is not your address.\r\n",
-                               SMTP->from);
-                       strcpy(SMTP->from, "");
-               }
-               phree(valid);
+               strcpy(SMTP->from, CC->cs_inet_email);
+               cprintf("250 Sender ok <%s>\r\n", SMTP->from);
+               SMTP->message_originated_locally = 1;
                return;
        }
 
-
        /* Otherwise, make sure outsiders aren't trying to forge mail from
         * this system.
         */
@@ -401,7 +403,8 @@ void smtp_mail(char *argbuf) {
  */
 void smtp_rcpt(char *argbuf) {
        char recp[SIZ];
-       struct recptypes *valid;
+       char message_to_spammer[SIZ];
+       struct recptypes *valid = NULL;
 
        if (strlen(SMTP->from) == 0) {
                cprintf("503 Need MAIL before RCPT\r\n");
@@ -422,6 +425,15 @@ void smtp_rcpt(char *argbuf) {
                return;
        }
 
+       /* RBL check */
+       if ( (!CC->logged_in) && (!CC->is_local_socket) ) {
+               if (rbl_check(message_to_spammer)) {
+                       cprintf("550 %s\r\n", message_to_spammer);
+                       /* no need to phree(valid), it's not allocated yet */
+                       return;
+               }
+       }
+
        valid = validate_recipients(recp);
        if (valid->num_error > 0) {
                cprintf("599 Error: %s\r\n", valid->errormsg);
@@ -457,6 +469,7 @@ void smtp_data(void) {
        long msgnum;
        char nowstamp[SIZ];
        struct recptypes *valid;
+       int scan_errors;
 
        if (strlen(SMTP->from) == 0) {
                cprintf("503 Need MAIL command first.\r\n");
@@ -484,7 +497,7 @@ void smtp_data(void) {
                        config.c_fqdn,
                        nowstamp);
        
-       body = CtdlReadMessageBody(".", config.c_maxmsglen, body);
+       body = CtdlReadMessageBody(".", config.c_maxmsglen, body, 1);
        if (body == NULL) {
                cprintf("550 Unable to save message: internal error.\r\n");
                return;
@@ -495,30 +508,58 @@ void smtp_data(void) {
 
        /* If the user is locally authenticated, FORCE the From: header to
         * 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.
+        * but IT MAKES SENSE.  If you prefer strict RFC adherence over
+        * common sense, you can disable this in the configuration.
+        *
+        * We also set the "message room name" ('O' field) to MAILROOM
+        * (which is Mail> on most systems) to prevent it from getting set
+        * to something ugly like "0000058008.Sent Items>" when the message
+        * is read with a Citadel client.
         */
-       if (CC->logged_in) {
+       if ( (CC->logged_in) && (config.c_rfc822_strict_from == 0) ) {
                if (msg->cm_fields['A'] != NULL) phree(msg->cm_fields['A']);
                if (msg->cm_fields['N'] != NULL) phree(msg->cm_fields['N']);
                if (msg->cm_fields['H'] != NULL) phree(msg->cm_fields['H']);
+               if (msg->cm_fields['F'] != NULL) phree(msg->cm_fields['F']);
+               if (msg->cm_fields['O'] != NULL) phree(msg->cm_fields['O']);
                msg->cm_fields['A'] = strdoop(CC->usersupp.fullname);
                msg->cm_fields['N'] = strdoop(config.c_nodename);
                msg->cm_fields['H'] = strdoop(config.c_humannode);
+               msg->cm_fields['F'] = strdoop(CC->cs_inet_email);
+               msg->cm_fields['O'] = strdoop(MAILROOM);
        }
 
        /* Submit the message into the Citadel system. */
        valid = validate_recipients(SMTP->recipients);
-       msgnum = CtdlSubmitMsg(msg, valid, "");
-       CtdlFreeMessage(msg);
-       phree(valid);
 
-       if (msgnum > 0L) {
-               cprintf("250 Message accepted.\r\n");
+       /* 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");
+               }
+
+               cprintf("550 %s\r\n", msg->cm_fields['0']);
        }
-       else {
-               cprintf("550 Internal delivery error\r\n");
+       
+       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 */
 }
 
@@ -541,6 +582,8 @@ void smtp_command_loop(void) {
        lprintf(5, "SMTP: %s\n", cmdbuf);
        while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
 
+       lprintf(9, "CC->logged_in = %d\n", CC->logged_in);
+
        if (SMTP->command_state == smtp_user) {
                smtp_get_user(cmdbuf);
        }
@@ -652,7 +695,7 @@ void smtp_try(const char *key, const char *addr, int *status,
        }
        else {
                CtdlRedirectOutput(msg_fp, -1);
-               CtdlOutputMsg(msgnum, MT_RFC822, 0, 0, 1);
+               CtdlOutputMsg(msgnum, MT_RFC822, HEADERS_ALL, 0, 1);
                CtdlRedirectOutput(NULL, -1);
                fseek(msg_fp, 0L, SEEK_END);
                msg_size = ftell(msg_fp);
@@ -719,7 +762,6 @@ void smtp_try(const char *key, const char *addr, int *status,
                snprintf(dsn, SIZ, "Could not connect: %s", strerror(errno));
                if (sock >= 0) lprintf(9, "Connected!\n");
                if (sock < 0) snprintf(dsn, SIZ, "%s", strerror(errno));
-               if (sock >= 0) break;
        }
 
        if (sock < 0) {
@@ -964,7 +1006,6 @@ void smtp_do_bounce(char *instr) {
        /*
         * Now go through the instructions checking for stuff.
         */
-
        for (i=0; i<lines; ++i) {
                extract_token(buf, instr, i, '\n');
                extract(key, buf, 0);
@@ -1033,7 +1074,7 @@ void smtp_do_bounce(char *instr) {
 
                /* If not, post it in the Aide> room */
                if (successful_bounce == 0) {
-                       CtdlSubmitMsg(bmsg, NULL, AIDEROOM);
+                       CtdlSubmitMsg(bmsg, NULL, config.c_aideroom);
                }
 
                /* Free up the memory we used */
@@ -1194,7 +1235,16 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
                extract(dsn, buf, 3);
                if ( (!strcasecmp(key, "remote"))
                   && ((status==0)||(status==3)||(status==4)) ) {
+
+                       /* Remove this "remote" instruction from the set,
+                        * but replace the set's final newline if
+                        * remove_token() stripped it.  It has to be there.
+                        */
                        remove_token(instr, i, '\n');
+                       if (instr[strlen(instr)-1] != '\n') {
+                               strcat(instr, "\n");
+                       }
+
                        --i;
                        --lines;
                        lprintf(9, "SMTP: Trying <%s>\n", addr);
@@ -1293,7 +1343,7 @@ 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");
@@ -1333,7 +1383,7 @@ void cmd_smtp(char *argbuf) {
 
        else if (!strcasecmp(cmd, "runqueue")) {
                run_queue_now = 1;
-               cprintf("%d All outbound SMTP will be retried now.\n", OK);
+               cprintf("%d All outbound SMTP will be retried now.\n", CIT_OK);
                return;
        }
 
@@ -1344,6 +1394,29 @@ void cmd_smtp(char *argbuf) {
 }
 
 
+/*
+ * Initialize the SMTP outbound queue
+ */
+void smtp_init_spoolout(void) {
+       struct quickroom qrbuf;
+
+       /*
+        * Create the room.  This will silently fail if the room already
+        * exists, and that's perfectly ok, because we want it to exist.
+        */
+       create_room(SMTP_SPOOLOUT_ROOM, 3, "", 0, 1, 0);
+
+       /*
+        * Make sure it's set to be a "system room" so it doesn't show up
+        * in the <K>nown rooms list for Aides.
+        */
+       if (lgetroom(&qrbuf, SMTP_SPOOLOUT_ROOM) == 0) {
+               qrbuf.QRflags2 |= QR2_SYSTEM;
+               lputroom(&qrbuf);
+       }
+}
+
+
 
 
 /*****************************************************************************/
@@ -1351,7 +1424,7 @@ void cmd_smtp(char *argbuf) {
 /*****************************************************************************/
 
 
-char *Dynamic_Module_Init(void)
+char *serv_smtp_init(void)
 {
        SYM_SMTP = CtdlGetDynamicSymbol();
 
@@ -1365,7 +1438,7 @@ char *Dynamic_Module_Init(void)
                                smtp_greeting,
                                smtp_command_loop);
 
-       create_room(SMTP_SPOOLOUT_ROOM, 3, "", 0, 1);
+       smtp_init_spoolout();
        CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER);
        CtdlRegisterProtoHook(cmd_smtp, "SMTP", "SMTP utility commands");
        return "$Id$";