]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/smtp/serv_smtp.c
Additional logging to troubleshoot false positives on spoof check
[citadel.git] / citadel / modules / smtp / serv_smtp.c
index e40e4c7edca5e97492850ecf5c7a73c0648642a8..813d303a482feeecf4fa1664dfc5e8b1ecbb4e8c 100644 (file)
@@ -303,6 +303,62 @@ void smtp_hello(long offset, long which_command)
 }
 
 
+/*
+ * Backend function for smtp_webcit_preferences_hack().
+ * Look at a message and determine if it's the preferences file.
+ */
+void smtp_webcit_preferences_hack_backend(long msgnum, void *userdata) {
+       struct CtdlMessage *msg;
+       char **webcit_conf = (char **) userdata;
+
+       if (*webcit_conf) {
+               return; // already got it
+       }
+
+       msg = CtdlFetchMessage(msgnum, 1);
+       if (msg == NULL) {
+               return;
+       }
+
+       if ( (msg->cm_fields['U']) && (!strcasecmp(msg->cm_fields['U'], "__ WebCit Preferences __")) ) {
+               /* This is it!  Change ownership of the message text so it doesn't get freed. */
+               *webcit_conf = (char *)msg->cm_fields['M'];
+               msg->cm_fields['M'] = NULL;
+       }
+       CtdlFreeMessage(msg);
+}
+
+
+/*
+ * The configuration item for the user's preferred display name for outgoing email is, unfortunately,
+ * stored in the account's WebCit configuration.  We have to fetch it now.
+ */
+void smtp_webcit_preferences_hack(void) {
+       char config_roomname[ROOMNAMELEN];
+       char *webcit_conf = NULL;
+
+       snprintf(config_roomname, sizeof config_roomname, "%010ld.%s", CC->user.usernum, USERCONFIGROOM);
+       if (CtdlGetRoom(&CC->room, config_roomname) != 0) {
+               return;
+       }
+
+       /*
+        * Find the WebCit configuration message
+        */
+
+       CtdlForEachMessage(MSGS_ALL, 1, NULL, NULL, NULL, smtp_webcit_preferences_hack_backend, (void *)&webcit_conf);
+
+       if (!webcit_conf) {
+               return;
+       }
+
+       /* FIXME : now do something with this data */
+
+       free(webcit_conf);
+       abort();
+}
+
+
 
 /*
  * Implement HELP command.
@@ -388,6 +444,7 @@ void smtp_try_plain(long offset, long Flags)
 
        if (result == login_ok) {
                if (CtdlTryPassword(pass, len) == pass_ok) {
+////                   smtp_webcit_preferences_hack();
                        smtp_auth_greeting(offset, Flags);
                        return;
                }
@@ -411,10 +468,10 @@ void smtp_auth(long offset, long Flags)
                return;
        }
 
-       extract_token(method, ChrPtr(sSMTP->Cmd), 0, ' ', sizeof method);
+       extract_token(method, ChrPtr(sSMTP->Cmd) + offset, 0, ' ', sizeof method);
 
        if (!strncasecmp(method, "login", 5) ) {
-               if (StrLength(sSMTP->Cmd) >= 7) {
+               if (StrLength(sSMTP->Cmd) - offset >= 7) {
                        smtp_get_user(6);
                }
                else {
@@ -427,18 +484,18 @@ void smtp_auth(long offset, long Flags)
 
        if (!strncasecmp(method, "plain", 5) ) {
                long len;
-               if (num_tokens(ChrPtr(sSMTP->Cmd), ' ') < 2) {
+               if (num_tokens(ChrPtr(sSMTP->Cmd) + offset, ' ') < 2) {
                        cprintf("334 \r\n");
                        SMTP->command_state = smtp_plain;
                        return;
                }
 
                len = extract_token(encoded_authstring, 
-                                   ChrPtr(sSMTP->Cmd),
+                                   ChrPtr(sSMTP->Cmd) + offset,
                                    1, ' ',
                                    sizeof encoded_authstring);
                StrBufPlain(sSMTP->Cmd, encoded_authstring, len);
-               smtp_try_plain(offset, Flags);
+               smtp_try_plain(0, Flags);
                return;
        }
 
@@ -459,8 +516,6 @@ void smtp_auth(long offset, long Flags)
  * Set do_response to nonzero to output the SMTP RSET response code.
  */
 void smtp_rset(long offset, long do_response) {
-       int is_lmtp;
-       int is_unfiltered;
        citsmtp *sSMTP = SMTP;
 
        /*
@@ -468,10 +523,21 @@ void smtp_rset(long offset, long do_response) {
         * but we need to preserve this one little piece of information, so
         * we save it for later.
         */
-       is_lmtp = sSMTP->is_lmtp;
-       is_unfiltered = sSMTP->is_unfiltered;
 
-       memset(sSMTP, 0, sizeof(citsmtp));
+       FlushStrBuf(sSMTP->Cmd);
+       FlushStrBuf(sSMTP->helo_node);
+       FlushStrBuf(sSMTP->from);
+       FlushStrBuf(sSMTP->recipients);
+       FlushStrBuf(sSMTP->OneRcpt);
+
+       sSMTP->command_state = 0;
+       sSMTP->number_of_recipients = 0;
+       sSMTP->delivery_mode = 0;
+       sSMTP->message_originated_locally = 0;
+       sSMTP->is_msa = 0;
+       /*
+        * we must remember is_lmtp & is_unfiltered.
+        */
 
        /*
         * It is somewhat ambiguous whether we want to log out when a RSET
@@ -484,12 +550,6 @@ void smtp_rset(long offset, long do_response) {
         * }
         */
 
-       /*
-        * Reinstate this little piece of information we saved (see above).
-        */
-       sSMTP->is_lmtp = is_lmtp;
-       sSMTP->is_unfiltered = is_unfiltered;
-
        if (do_response) {
                cprintf("250 Zap!\r\n");
        }
@@ -565,9 +625,13 @@ void smtp_mail(long offset, long flags) {
         */
        else if (config.c_allow_spoofing == 0) {
                process_rfc822_addr(ChrPtr(sSMTP->from), user, node, name);
+               syslog(LOG_DEBUG, "Claimed envelope sender is '%s' == '%s' @ '%s' ('%s')",
+                       ChrPtr(sSMTP->from), user, node, name
+               );
                if (CtdlHostAlias(node) != hostalias_nomatch) {
                        cprintf("550 You must log in to send mail from %s\r\n", node);
                        FlushStrBuf(sSMTP->from);
+                       syslog(LOG_DEBUG, "Rejecting unauthenticated mail from %s", node);
                        return;
                }
        }
@@ -1007,6 +1071,7 @@ CTDL_MODULE_INIT(smtp)
        if (!threading)
        {
                SMTPCmds = NewHash(1, NULL);
+               
                RegisterSmtpCMD("AUTH", smtp_auth, 0);
                RegisterSmtpCMD("DATA", smtp_data, 0);
                RegisterSmtpCMD("HELO", smtp_hello, HELO);
@@ -1060,6 +1125,7 @@ CTDL_MODULE_INIT(smtp)
                                        NULL,
                                        CitadelServiceSMTP_LMTP_UNF);
 
+               CtdlRegisterCleanupHook(smtp_cleanup);
                CtdlRegisterSessionHook(smtp_cleanup_function, EVT_STOP, PRIO_STOP + 250);
        }