]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_smtp.c
* Added a new message function hook type EVT_SMTPSCAN which permits modules to
[citadel.git] / citadel / serv_smtp.c
index 25082d77c12e04fca656e6bedf7f3a17de98c94c..43896c28092e0f89180028ffc68e0623751c961e 100644 (file)
@@ -110,8 +110,8 @@ void smtp_greeting(void) {
        CtdlAllocUserData(SYM_SMTP, sizeof(struct citsmtp));
        CtdlAllocUserData(SYM_SMTP_RECPS, SIZ);
        CtdlAllocUserData(SYM_SMTP_ROOMS, SIZ);
-       sprintf(SMTP_RECPS, "%s", "");
-       sprintf(SMTP_ROOMS, "%s", "");
+       snprintf(SMTP_RECPS, SIZ, "%s", "");
+       snprintf(SMTP_ROOMS, SIZ, "%s", "");
 
        cprintf("220 %s ESMTP Citadel/UX server ready.\r\n", config.c_fqdn);
 }
@@ -457,6 +457,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");
@@ -508,17 +509,35 @@ void smtp_data(void) {
 
        /* 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("552 %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 */
 }
 
@@ -620,7 +639,8 @@ void smtp_command_loop(void) {
  * Called by smtp_do_procmsg() to attempt delivery to one SMTP host
  *
  */
-void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
+void smtp_try(const char *key, const char *addr, int *status,
+             char *dsn, size_t n, long msgnum)
 {
        int sock = (-1);
        char mxhosts[1024];
@@ -646,7 +666,7 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        msg_fp = tmpfile();
        if (msg_fp == NULL) {
                *status = 4;
-               sprintf(dsn, "Error creating temporary file");
+               snprintf(dsn, n, "Error creating temporary file");
                return;
        }
        else {
@@ -1197,7 +1217,7 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
                        --i;
                        --lines;
                        lprintf(9, "SMTP: Trying <%s>\n", addr);
-                       smtp_try(key, addr, &status, dsn, text_msgid);
+                       smtp_try(key, addr, &status, dsn, sizeof dsn, text_msgid);
                        if (status != 2) {
                                if (results == NULL) {
                                        results = mallok(1024);
@@ -1207,7 +1227,7 @@ void smtp_do_procmsg(long msgnum, void *userdata) {
                                        results = reallok(results,
                                                strlen(results) + 1024);
                                }
-                               sprintf(&results[strlen(results)],
+                               snprintf(&results[strlen(results)], 1024,
                                        "%s|%s|%d|%s\n",
                                        key, addr, status, dsn);
                        }
@@ -1292,7 +1312,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");
@@ -1332,7 +1352,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;
        }
 
@@ -1364,7 +1384,7 @@ char *Dynamic_Module_Init(void)
                                smtp_greeting,
                                smtp_command_loop);
 
-       create_room(SMTP_SPOOLOUT_ROOM, 3, "", 0, 1);
+       create_room(SMTP_SPOOLOUT_ROOM, 3, "", 0, 1, 0);
        CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER);
        CtdlRegisterProtoHook(cmd_smtp, "SMTP", "SMTP utility commands");
        return "$Id$";