MODULES: add priorities in execution
[citadel.git] / citadel / modules / smtp / serv_smtp.c
index cccb61bf68ae6df8d893cbac4b8fc4fd72b41ee0..cc026916d6258a32f6ca833a89e6e86a8cb08f38 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * This module is an SMTP and ESMTP implementation for the Citadel system.
+ * This module is an SMTP and ESMTP server for the Citadel system.
  * It is compliant with all of the following:
  *
  * RFC  821 - Simple Mail Transfer Protocol
  * The VRFY and EXPN commands have been removed from this implementation
  * because nobody uses these commands anymore, except for spammers.
  *
- * Copyright (c) 1998-2009 by the citadel.org team
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 3 of the License, or
- *  (at your option) any later version.
+ * Copyright (c) 1998-2012 by the citadel.org team
  *
+ *  This program is open source software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 3.
+ *  
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #include "sysdep.h"
@@ -102,14 +96,6 @@ enum {                              /* Command states for login authentication */
 };
 
 
-
-
-
-/*****************************************************************************/
-/*                      SMTP SERVER (INBOUND) STUFF                          */
-/*****************************************************************************/
-
-
 /*
  * Here's where our SMTP session begins its happy day.
  */
@@ -131,11 +117,11 @@ void smtp_greeting(int is_msa)
         */
        if ( (config.c_rbl_at_greeting) && (sSMTP->is_msa == 0) ) {
                if (rbl_check(message_to_spammer)) {
-                       if (CtdlThreadCheckStop())
+                       if (server_shutting_down)
                                cprintf("421 %s\r\n", message_to_spammer);
                        else
                                cprintf("550 %s\r\n", message_to_spammer);
-                       CC->kill_me = 1;
+                       CC->kill_me = KILLME_SPAMMER;
                        /* no need to free_recipients(valid), it's not allocated yet */
                        return;
                }
@@ -144,10 +130,8 @@ void smtp_greeting(int is_msa)
        /* Otherwise we're either clean or we check later. */
 
        if (CC->nologin==1) {
-               cprintf("500 Too many users are already online (maximum is %d)\r\n",
-                       config.c_maxsessions
-               );
-               CC->kill_me = 1;
+               cprintf("451 Too many connections are already open; please try again later.\r\n");
+               CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
                /* no need to free_recipients(valid), it's not allocated yet */
                return;
        }
@@ -165,7 +149,7 @@ void smtp_greeting(int is_msa)
 void smtps_greeting(void) {
        CtdlModuleStartCryptoMsgs(NULL, NULL, NULL);
 #ifdef HAVE_OPENSSL
-       if (!CC->redirect_ssl) CC->kill_me = 1;         /* kill session if no crypto */
+       if (!CC->redirect_ssl) CC->kill_me = KILLME_NO_CRYPTO;          /* kill session if no crypto */
 #endif
        smtp_greeting(0);
 }
@@ -183,10 +167,8 @@ void smtp_msa_greeting(void) {
  * LMTP is like SMTP but with some extra bonus footage added.
  */
 void lmtp_greeting(void) {
-       citsmtp *sSMTP;
 
        smtp_greeting(0);
-       sSMTP = SMTP;
        SMTP->is_lmtp = 1;
 }
 
@@ -217,7 +199,7 @@ void lmtp_unfiltered_greeting(void) {
  */
 void smtp_auth_greeting(void) {
                cprintf("235 Hello, %s\r\n", CC->user.fullname);
-               CtdlLogPrintf(CTDL_NOTICE, "SMTP authenticated %s\n", CC->user.fullname);
+               syslog(LOG_NOTICE, "SMTP authenticated %s\n", CC->user.fullname);
                CC->internal_pgm = 0;
                CC->cs_flags &= ~CS_STEALTH;
 }
@@ -302,7 +284,7 @@ void smtp_get_user(char *argbuf) {
        citsmtp *sSMTP = SMTP;
 
        CtdlDecodeBase64(username, argbuf, SIZ);
-       /* CtdlLogPrintf(CTDL_DEBUG, "Trying <%s>\n", username); */
+       /* syslog(LOG_DEBUG, "Trying <%s>\n", username); */
        if (CtdlLoginExistingUser(NULL, username) == login_ok) {
                CtdlEncodeBase64(buf, "Password:", 9, 0);
                cprintf("334 %s\r\n", buf);
@@ -324,7 +306,7 @@ void smtp_get_pass(char *argbuf) {
 
        memset(password, 0, sizeof(password));  
        len = CtdlDecodeBase64(password, argbuf, SIZ);
-       /* CtdlLogPrintf(CTDL_DEBUG, "Trying <%s>\n", password); */
+       /* syslog(LOG_DEBUG, "Trying <%s>\n", password); */
        if (CtdlTryPassword(password, len) == pass_ok) {
                smtp_auth_greeting();
        }
@@ -584,7 +566,7 @@ void smtp_rcpt(char *argbuf) {
           && (!sSMTP->is_lmtp) ) {     /* Don't RBL LMTP clients */
                if (config.c_rbl_at_greeting == 0) {    /* Don't RBL again if we already did it */
                        if (rbl_check(message_to_spammer)) {
-                               if (CtdlThreadCheckStop())
+                               if (server_shutting_down)
                                        cprintf("421 %s\r\n", message_to_spammer);
                                else
                                        cprintf("550 %s\r\n", message_to_spammer);
@@ -594,11 +576,11 @@ void smtp_rcpt(char *argbuf) {
                }
        }
 
-       valid = validate_recipients(recp, 
-                                   smtp_get_Recipients (),
-                                   (sSMTP->is_lmtp)? POST_LMTP:
-                                      (CC->logged_in)? POST_LOGGED_IN:
-                                                       POST_EXTERNAL);
+       valid = validate_recipients(
+               recp, 
+               smtp_get_Recipients(),
+               (sSMTP->is_lmtp)? POST_LMTP: (CC->logged_in)? POST_LOGGED_IN: POST_EXTERNAL
+       );
        if (valid->num_error != 0) {
                cprintf("550 %s\r\n", valid->errormsg);
                free_recipients(valid);
@@ -695,7 +677,7 @@ void smtp_data(void) {
                return;
        }
 
-       CtdlLogPrintf(CTDL_DEBUG, "Converting message...\n");
+       syslog(LOG_DEBUG, "Converting message...\n");
        msg = convert_internet_message_buf(&body);
 
        /* If the user is locally authenticated, FORCE the From: header to
@@ -734,11 +716,11 @@ void smtp_data(void) {
        msg->cm_fields['V'] = strdup(sSMTP->recipients);
 
        /* Submit the message into the Citadel system. */
-       valid = validate_recipients(sSMTP->recipients, 
-                                   smtp_get_Recipients (),
-                                   (sSMTP->is_lmtp)? POST_LMTP:
-                                      (CC->logged_in)? POST_LOGGED_IN:
-                                                       POST_EXTERNAL);
+       valid = validate_recipients(
+               sSMTP->recipients,
+               smtp_get_Recipients(),
+               (sSMTP->is_lmtp)? POST_LMTP: (CC->logged_in)? POST_LOGGED_IN: POST_EXTERNAL
+       );
 
        /* If there are modules that want to scan this message before final
         * submission (such as virus checkers or spam filters), call them now
@@ -770,7 +752,7 @@ void smtp_data(void) {
                }
        }
 
-       /* For SMTP and ESTMP, just print the result message.  For LMTP, we
+       /* For SMTP and ESMTP, just print the result message.  For LMTP, we
         * have to print one result message for each recipient.  Since there
         * is nothing in Citadel which would cause different recipients to
         * have different results, we can get away with just spitting out the
@@ -785,20 +767,18 @@ void smtp_data(void) {
                cprintf("%s", result);
        }
 
-       /* Write something to the syslog (which may or may not be where the
+       /* Write something to the syslog(which may or may not be where the
         * rest of the Citadel logs are going; some sysadmins want LOG_MAIL).
         */
-       if (enable_syslog) {
-               syslog((LOG_MAIL | LOG_INFO),
-                       "%ld: from=<%s>, nrcpts=%d, relay=%s [%s], stat=%s",
-                       msgnum,
-                       sSMTP->from,
-                       sSMTP->number_of_recipients,
-                       CC->cs_host,
-                       CC->cs_addr,
-                       result
-               );
-       }
+       syslog((LOG_MAIL | LOG_INFO),
+               "%ld: from=<%s>, nrcpts=%d, relay=%s [%s], stat=%s",
+               msgnum,
+               sSMTP->from,
+               sSMTP->number_of_recipients,
+               CC->cs_host,
+               CC->cs_addr,
+               result
+       );
 
        /* Clean up */
        CtdlFreeMessage(msg);
@@ -808,7 +788,7 @@ void smtp_data(void) {
 
 
 /*
- * implements the STARTTLS command (Citadel API version)
+ * implements the STARTTLS command
  */
 void smtp_starttls(void)
 {
@@ -816,37 +796,34 @@ void smtp_starttls(void)
        char nosup_response[SIZ];
        char error_response[SIZ];
 
-       sprintf(ok_response,
-               "220 Begin TLS negotiation now\r\n");
-       sprintf(nosup_response,
-               "554 TLS not supported here\r\n");
-       sprintf(error_response,
-               "554 Internal error\r\n");
+       sprintf(ok_response, "220 Begin TLS negotiation now\r\n");
+       sprintf(nosup_response, "554 TLS not supported here\r\n");
+       sprintf(error_response, "554 Internal error\r\n");
        CtdlModuleStartCryptoMsgs(ok_response, nosup_response, error_response);
        smtp_rset(0);
 }
 
 
-
 /* 
- * Main command loop for SMTP sessions.
+ * Main command loop for SMTP server sessions.
  */
 void smtp_command_loop(void) {
        char cmdbuf[SIZ];
        citsmtp *sSMTP = SMTP;
 
        if (sSMTP == NULL) {
-               CtdlLogPrintf(CTDL_EMERG, "Session SMTP data is null.  WTF?  We will crash now.\n");
+               syslog(LOG_EMERG, "Session SMTP data is null.  WTF?  We will crash now.\n");
+               return cit_panic_backtrace (0);
        }
 
        time(&CC->lastcmd);
        memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
        if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
-               CtdlLogPrintf(CTDL_CRIT, "Client disconnected: ending session.\n");
-               CC->kill_me = 1;
+               syslog(LOG_CRIT, "SMTP: client disconnected: ending session.\n");
+               CC->kill_me = KILLME_CLIENT_DISCONNECTED;
                return;
        }
-       CtdlLogPrintf(CTDL_INFO, "SMTP server: %s\n", cmdbuf);
+       syslog(LOG_INFO, "SMTP server: %s\n", cmdbuf);
        while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
 
        if (sSMTP->command_state == smtp_user) {
@@ -895,7 +872,7 @@ void smtp_command_loop(void) {
 
        else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
                cprintf("221 Goodbye...\r\n");
-               CC->kill_me = 1;
+               CC->kill_me = KILLME_CLIENT_LOGGED_OUT;
                return;
        }
 
@@ -919,12 +896,6 @@ void smtp_command_loop(void) {
 }
 
 
-
-
-
-
-
-
 /*****************************************************************************/
 /*                      MODULE INITIALIZATION STUFF                          */
 /*****************************************************************************/
@@ -937,7 +908,7 @@ void smtp_cleanup_function(void) {
        /* Don't do this stuff if this is not an SMTP session! */
        if (CC->h_command_function != smtp_command_loop) return;
 
-       CtdlLogPrintf(CTDL_DEBUG, "Performing SMTP cleanup hook\n");
+       syslog(LOG_DEBUG, "Performing SMTP cleanup hook\n");
        free(SMTP);
 }
 
@@ -990,9 +961,9 @@ CTDL_MODULE_INIT(smtp)
                                        NULL,
                                        CitadelServiceSMTP_LMTP_UNF);
 
-               CtdlRegisterSessionHook(smtp_cleanup_function, EVT_STOP);
+               CtdlRegisterSessionHook(smtp_cleanup_function, EVT_STOP, PRIO_STOP + 250);
        }
        
-       /* return our Subversion id for the Log */
+       /* return our module name for the log */
        return "smtp";
 }