* Renamed "struct user" to "struct ctdluser"
[citadel.git] / citadel / serv_smtp.c
index 3d9dc248cdefb755ab862f53f0a5bb180dcbe078..4b2872dcdfc6c7223fb6177cfe12e4149fe0b7c1 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
@@ -58,7 +62,7 @@
 struct citsmtp {               /* Information about the current session */
        int command_state;
        char helo_node[SIZ];
-       struct usersupp vrfy_buffer;
+       struct ctdluser vrfy_buffer;
        int vrfy_count;
        char vrfy_match[SIZ];
        char from[SIZ];
@@ -132,7 +136,8 @@ void smtp_hello(char *argbuf, int is_esmtp) {
                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");
+               cprintf("250-AUTH=LOGIN\r\n");
+               cprintf("250 ENHANCEDSTATUSCODES\r\n");
        }
 }
 
@@ -172,7 +177,7 @@ void smtp_get_user(char *argbuf) {
                SMTP->command_state = smtp_password;
        }
        else {
-               cprintf("500 No such user.\r\n");
+               cprintf("500 5.7.0 No such user.\r\n");
                SMTP->command_state = smtp_command;
        }
 }
@@ -187,13 +192,13 @@ void smtp_get_pass(char *argbuf) {
        CtdlDecodeBase64(password, argbuf, SIZ);
        lprintf(9, "Trying <%s>\n", password);
        if (CtdlTryPassword(password) == pass_ok) {
-               cprintf("235 Hello, %s\r\n", CC->usersupp.fullname);
+               cprintf("235 2.0.0 Hello, %s\r\n", CC->user.fullname);
                lprintf(9, "SMTP authenticated login successful\n");
                CC->internal_pgm = 0;
                CC->cs_flags &= ~CS_STEALTH;
        }
        else {
-               cprintf("500 Authentication failed.\r\n");
+               cprintf("500 5.7.0 Authentication failed.\r\n");
        }
        SMTP->command_state = smtp_command;
 }
@@ -206,7 +211,7 @@ void smtp_auth(char *argbuf) {
        char buf[SIZ];
 
        if (strncasecmp(argbuf, "login", 5) ) {
-               cprintf("550 We only support LOGIN authentication.\r\n");
+               cprintf("550 5.7.4 We only support LOGIN authentication.\r\n");
                return;
        }
 
@@ -225,11 +230,11 @@ void smtp_auth(char *argbuf) {
 /*
  * Back end for smtp_vrfy() command
  */
-void smtp_vrfy_backend(struct usersupp *us, void *data) {
+void smtp_vrfy_backend(struct ctdluser *us, void *data) {
 
        if (!fuzzy_match(us, SMTP->vrfy_match)) {
                ++SMTP->vrfy_count;
-               memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
+               memcpy(&SMTP->vrfy_buffer, us, sizeof(struct ctdluser));
        }
 }
 
@@ -244,7 +249,7 @@ void smtp_vrfy(char *argbuf) {
        ForEachUser(smtp_vrfy_backend, NULL);
 
        if (SMTP->vrfy_count < 1) {
-               cprintf("550 String does not match anything.\r\n");
+               cprintf("550 5.1.1 String does not match anything.\r\n");
        }
        else if (SMTP->vrfy_count == 1) {
                cprintf("250 %s <cit%ld@%s>\r\n",
@@ -253,7 +258,7 @@ void smtp_vrfy(char *argbuf) {
                        config.c_fqdn);
        }
        else if (SMTP->vrfy_count > 1) {
-               cprintf("553 Request ambiguous: %d users matched.\r\n",
+               cprintf("553 5.1.4 Request ambiguous: %d users matched.\r\n",
                        SMTP->vrfy_count);
        }
 
@@ -264,7 +269,7 @@ void smtp_vrfy(char *argbuf) {
 /*
  * Back end for smtp_expn() command
  */
-void smtp_expn_backend(struct usersupp *us, void *data) {
+void smtp_expn_backend(struct ctdluser *us, void *data) {
 
        if (!fuzzy_match(us, SMTP->vrfy_match)) {
 
@@ -276,7 +281,7 @@ void smtp_expn_backend(struct usersupp *us, void *data) {
                }
 
                ++SMTP->vrfy_count;
-               memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
+               memcpy(&SMTP->vrfy_buffer, us, sizeof(struct ctdluser));
        }
 }
 
@@ -291,7 +296,7 @@ void smtp_expn(char *argbuf) {
        ForEachUser(smtp_expn_backend, NULL);
 
        if (SMTP->vrfy_count < 1) {
-               cprintf("550 String does not match anything.\r\n");
+               cprintf("550 5.1.1 String does not match anything.\r\n");
        }
        else if (SMTP->vrfy_count >= 1) {
                cprintf("250 %s <cit%ld@%s>\r\n",
@@ -310,10 +315,19 @@ void smtp_expn(char *argbuf) {
  */
 void smtp_rset(void) {
        memset(SMTP, 0, sizeof(struct citsmtp));
-       if (CC->logged_in) {
-               logout(CC);
-       }
-       cprintf("250 Zap!\r\n");
+
+       /*
+        * 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 2.0.0 Zap!\r\n");
 }
 
 /*
@@ -337,15 +351,14 @@ 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");
+               cprintf("503 5.1.0 Only one sender permitted\r\n");
                return;
        }
 
        if (strncasecmp(argbuf, "From:", 5)) {
-               cprintf("501 Syntax error\r\n");
+               cprintf("501 5.1.7 Syntax error\r\n");
                return;
        }
 
@@ -353,45 +366,41 @@ void smtp_mail(char *argbuf) {
        striplt(SMTP->from);
        stripallbut(SMTP->from, '<', '>');
 
+       /* We used to reject empty sender names, until it was brought to our
+        * attention that RFC1123 5.2.9 requires that this be allowed.  So now
+        * we allow it, but replace the empty string with a fake
+        * address so we don't have to contend with the empty string causing
+        * other code to fail when it's expecting something there.
+        */
        if (strlen(SMTP->from) == 0) {
-               cprintf("501 Empty sender name is not permitted\r\n");
-               return;
+               strcpy(SMTP->from, "someone@somewhere.org");
        }
 
-       /* 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 2.1.0 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.
         */
        else {
                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",
+                       cprintf("550 5.1.8 "
+                               "You must log in to send mail from %s\r\n",
                                node);
                        strcpy(SMTP->from, "");
                        return;
                }
        }
 
-       cprintf("250 Sender ok\r\n");
+       cprintf("250 2.0.0 Sender ok\r\n");
 }
 
 
@@ -401,15 +410,16 @@ 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");
+               cprintf("503 5.5.1 Need MAIL before RCPT\r\n");
                return;
        }
 
        if (strncasecmp(argbuf, "To:", 3)) {
-               cprintf("501 Syntax error\r\n");
+               cprintf("501 5.1.7 Syntax error\r\n");
                return;
        }
 
@@ -418,26 +428,35 @@ void smtp_rcpt(char *argbuf) {
        stripallbut(recp, '<', '>');
 
        if ( (strlen(recp) + strlen(SMTP->recipients) + 1 ) >= SIZ) {
-               cprintf("452 Too many recipients\r\n");
+               cprintf("452 4.5.3 Too many recipients\r\n");
                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);
+               cprintf("599 5.1.1 Error: %s\r\n", valid->errormsg);
                phree(valid);
                return;
        }
 
        if (valid->num_internet > 0) {
                if (SMTP->message_originated_locally == 0) {
-                       cprintf("551 Relaying denied <%s>\r\n", recp);
+                       cprintf("551 5.7.1 <%s> - relaying denied\r\n", recp);
                        phree(valid);
                        return;
                }
        }
 
-       cprintf("250 RCPT ok <%s>\r\n", recp);
+       cprintf("250 2.1.5 RCPT ok <%s>\r\n", recp);
        if (strlen(SMTP->recipients) > 0) {
                strcat(SMTP->recipients, ",");
        }
@@ -460,16 +479,16 @@ void smtp_data(void) {
        int scan_errors;
 
        if (strlen(SMTP->from) == 0) {
-               cprintf("503 Need MAIL command first.\r\n");
+               cprintf("503 5.5.1 Need MAIL command first.\r\n");
                return;
        }
 
        if (SMTP->number_of_recipients < 1) {
-               cprintf("503 Need RCPT command first.\r\n");
+               cprintf("503 5.5.1 Need RCPT command first.\r\n");
                return;
        }
 
-       cprintf("354 Transmit message now; terminate with '.' by itself\r\n");
+       cprintf("354 Transmit message now - terminate with '.' by itself\r\n");
        
        datestring(nowstamp, sizeof nowstamp, time(NULL), DATESTRING_RFC822);
        body = mallok(4096);
@@ -485,9 +504,10 @@ 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");
+               cprintf("550 5.6.5 "
+                       "Unable to save message: internal error.\r\n");
                return;
        }
 
@@ -496,15 +516,25 @@ 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']);
-               msg->cm_fields['A'] = strdoop(CC->usersupp.fullname);
+               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->user.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. */
@@ -520,19 +550,19 @@ void smtp_data(void) {
 
                if (msg->cm_fields['0'] == NULL) {
                        msg->cm_fields['0'] = strdoop(
-                               "Message rejected by filter");
+                               "5.7.1 Message rejected by filter");
                }
 
-               cprintf("552 %s\r\n", msg->cm_fields['0']);
+               cprintf("550 %s\r\n", msg->cm_fields['0']);
        }
        
        else {                  /* Ok, we'll accept this message. */
                msgnum = CtdlSubmitMsg(msg, valid, "");
                if (msgnum > 0L) {
-                       cprintf("250 Message accepted.\r\n");
+                       cprintf("250 2.0.0 Message accepted.\r\n");
                }
                else {
-                       cprintf("550 Internal delivery error\r\n");
+                       cprintf("550 5.5.0 Internal delivery error\r\n");
                }
        }
 
@@ -560,6 +590,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);
        }
@@ -619,7 +651,7 @@ void smtp_command_loop(void) {
        }
 
        else {
-               cprintf("502 I'm afraid I can't do that.\r\n");
+               cprintf("502 5.0.0 I'm afraid I can't do that.\r\n");
        }
 
 }
@@ -731,14 +763,14 @@ void smtp_try(const char *key, const char *addr, int *status,
                return;
        }
 
-       for (mx=0; mx<num_mxhosts; ++mx) {
+       sock = (-1);
+       for (mx=0; (mx<num_mxhosts && sock < 0); ++mx) {
                extract(buf, mxhosts, mx);
                lprintf(9, "Trying <%s>\n", buf);
                sock = sock_connect(buf, "25", "tcp");
                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) {
@@ -983,7 +1015,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);
@@ -1213,7 +1244,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);
@@ -1308,7 +1348,7 @@ void smtp_do_queue(void) {
         */
        lprintf(7, "SMTP: processing outbound queue\n");
 
-       if (getroom(&CC->quickroom, SMTP_SPOOLOUT_ROOM) != 0) {
+       if (getroom(&CC->room, SMTP_SPOOLOUT_ROOM) != 0) {
                lprintf(3, "Cannot find room <%s>\n", SMTP_SPOOLOUT_ROOM);
                return;
        }
@@ -1367,7 +1407,7 @@ void cmd_smtp(char *argbuf) {
  * Initialize the SMTP outbound queue
  */
 void smtp_init_spoolout(void) {
-       struct quickroom qrbuf;
+       struct ctdlroom qrbuf;
 
        /*
         * Create the room.  This will silently fail if the room already
@@ -1393,7 +1433,7 @@ void smtp_init_spoolout(void) {
 /*****************************************************************************/
 
 
-char *Dynamic_Module_Init(void)
+char *serv_smtp_init(void)
 {
        SYM_SMTP = CtdlGetDynamicSymbol();