]> 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 5bd23bea3027f141f6d9abc46826f27915b7fde7..43896c28092e0f89180028ffc68e0623751c961e 100644 (file)
@@ -62,11 +62,11 @@ struct citsmtp {            /* Information about the current session */
        int vrfy_count;
        char vrfy_match[SIZ];
        char from[SIZ];
+       char recipients[SIZ];
        int number_of_recipients;
        int number_of_rooms;
        int delivery_mode;
        int message_originated_locally;
-       struct recptypes valid;
 };
 
 enum {                         /* Command states for login authentication */
@@ -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);
 }
@@ -187,7 +187,7 @@ void smtp_get_pass(char *argbuf) {
        decode_base64(password, argbuf, SIZ);
        lprintf(9, "Trying <%s>\n", password);
        if (CtdlTryPassword(password) == pass_ok) {
-               cprintf("235 Authentication successful.\r\n");
+               cprintf("235 Hello, %s\r\n", CC->usersupp.fullname);
                lprintf(9, "SMTP authenticated login successful\n");
                CC->internal_pgm = 0;
                CC->cs_flags &= ~CS_STEALTH;
@@ -322,10 +322,10 @@ void smtp_rset(void) {
  */
 void smtp_data_clear(void) {
        strcpy(SMTP->from, "");
+       strcpy(SMTP->recipients, "");
        SMTP->number_of_recipients = 0;
        SMTP->delivery_mode = 0;
        SMTP->message_originated_locally = 0;
-       memset(&SMTP->valid, 0, sizeof(struct recptypes));
 }
 
 
@@ -336,7 +336,8 @@ void smtp_data_clear(void) {
 void smtp_mail(char *argbuf) {
        char user[SIZ];
        char node[SIZ];
-       int cvt;
+       char name[SIZ];
+       struct recptypes *valid;
 
        if (strlen(SMTP->from) != 0) {
                cprintf("503 Only one sender permitted\r\n");
@@ -350,37 +351,39 @@ void smtp_mail(char *argbuf) {
 
        strcpy(SMTP->from, &argbuf[5]);
        striplt(SMTP->from);
+       stripallbut(SMTP->from, '<', '>');
 
        if (strlen(SMTP->from) == 0) {
                cprintf("501 Empty sender name is not permitted\r\n");
                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 (CC->logged_in) {
-               cvt = convert_internet_address(user, node, SMTP->from);
-               lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
-               if ( (cvt != 0) || (strcasecmp(user, CC->usersupp.fullname))) {
+               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, "");
-                       return;
-               }
-               else {
-                       SMTP->message_originated_locally = 1;
                }
+               phree(valid);
+               return;
        }
 
+
        /* Otherwise, make sure outsiders aren't trying to forge mail from
         * this system.
         */
        else {
-               cvt = convert_internet_address(user, node, SMTP->from);
-               lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
-               if (CtdlHostAlias(node) == hostalias_localhost) {
+               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",
                                node);
                        strcpy(SMTP->from, "");
@@ -397,10 +400,8 @@ void smtp_mail(char *argbuf) {
  * Implements the "RCPT To:" command
  */
 void smtp_rcpt(char *argbuf) {
-       int cvt;
-       char user[SIZ];
-       char node[SIZ];
        char recp[SIZ];
+       struct recptypes *valid;
 
        if (strlen(SMTP->from) == 0) {
                cprintf("503 Need MAIL before RCPT\r\n");
@@ -414,56 +415,34 @@ void smtp_rcpt(char *argbuf) {
 
        strcpy(recp, &argbuf[3]);
        striplt(recp);
-       alias(recp);
-
-       cvt = convert_internet_address(user, node, recp);
-       snprintf(recp, sizeof recp, "%s@%s", user, node);
-       lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
-
-       /* FIXME possible buffer overflow type of issues here. */
-       switch(cvt) {
-               case rfc822_address_locally_validated:
-                       cprintf("250 %s is a valid local user.\r\n", user);
-                       if (SMTP->valid.num_local > 0) {
-                               strcat(SMTP->valid.recp_local, "|");
-                       }
-                       strcat(SMTP->valid.recp_local, user);
-                       SMTP->valid.num_local += 1;
-                       return;
-
-               case rfc822_room_delivery:
-                       cprintf("250 Delivering to room '%s'\r\n", user);
-                       cprintf("250 %s is a valid recipient.\r\n", user);
-                       if (SMTP->valid.num_room > 0) {
-                               strcat(SMTP->valid.recp_room, "|");
-                       }
-                       strcat(SMTP->valid.recp_room, user);
-                       SMTP->valid.num_room += 1;
-                       return;
-
-               case rfc822_address_on_citadel_network:
-                       cprintf("250 Delivering to room '%s'\r\n", user);
-                       /* FIXME */
-                       return;
+       stripallbut(recp, '<', '>');
 
-               case rfc822_no_such_user:
-                       cprintf("550 %s: no such user\r\n", recp);
-                       return;
-
-               case rfc822_address_nonlocal:
-                       if (SMTP->message_originated_locally == 0) {
-                               cprintf("551 Relaying denied.\r\n");
-                       }
-                       else {
-                               cprintf("250 Remote recipient %s ok\r\n", recp);
+       if ( (strlen(recp) + strlen(SMTP->recipients) + 1 ) >= SIZ) {
+               cprintf("452 Too many recipients\r\n");
+               return;
+       }
 
+       valid = validate_recipients(recp);
+       if (valid->num_error > 0) {
+               cprintf("599 Error: %s\r\n", valid->errormsg);
+               phree(valid);
+               return;
+       }
 
-                               return;
-                       }
+       if (valid->num_internet > 0) {
+               if (SMTP->message_originated_locally == 0) {
+                       cprintf("551 Relaying denied <%s>\r\n", recp);
+                       phree(valid);
                        return;
+               }
        }
 
-       cprintf("599 Unknown error\r\n");
+       cprintf("250 RCPT ok <%s>\r\n", recp);
+       if (strlen(SMTP->recipients) > 0) {
+               strcat(SMTP->recipients, ",");
+       }
+       strcat(SMTP->recipients, recp);
+       SMTP->number_of_recipients += 1;
 }
 
 
@@ -477,6 +456,8 @@ void smtp_data(void) {
        struct CtdlMessage *msg;
        long msgnum;
        char nowstamp[SIZ];
+       struct recptypes *valid;
+       int scan_errors;
 
        if (strlen(SMTP->from) == 0) {
                cprintf("503 Need MAIL command first.\r\n");
@@ -490,14 +471,17 @@ void smtp_data(void) {
 
        cprintf("354 Transmit message now; terminate with '.' by itself\r\n");
        
-       datestring(nowstamp, time(NULL), DATESTRING_RFC822);
+       datestring(nowstamp, sizeof nowstamp, time(NULL), DATESTRING_RFC822);
        body = mallok(4096);
 
+       /* FIXME
+          it should be Received: from %s (real.name.dom [w.x.y.z])
+        */
        if (body != NULL) snprintf(body, 4096,
-               "Received: from %s\n"
-               "       by %s;\n"
-               "       %s\n",
+               "Received: from %s (%s)\n"
+               "       by %s; %s\n",
                        SMTP->helo_node,
+                       CC->cs_host,
                        config.c_fqdn,
                        nowstamp);
        
@@ -524,16 +508,36 @@ void smtp_data(void) {
        }
 
        /* Submit the message into the Citadel system. */
-       msgnum = CtdlSubmitMsg(msg, &SMTP->valid, "");
-       CtdlFreeMessage(msg);
+       valid = validate_recipients(SMTP->recipients);
 
-       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 */
 }
 
@@ -553,7 +557,7 @@ void smtp_command_loop(void) {
                CC->kill_me = 1;
                return;
        }
-       lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
+       lprintf(5, "SMTP: %s\n", cmdbuf);
        while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
 
        if (SMTP->command_state == smtp_user) {
@@ -635,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];
@@ -661,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 {
@@ -707,7 +712,7 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
                                if (mailfrom[i] == '<') lp = i;
                                if (mailfrom[i] == '>') rp = i;
                        }
-                       if ((lp>=0)&&(rp>lp)) {
+                       if ( (lp>=0) && (rp>lp) ) {
                                mailfrom[rp] = 0;
                                strcpy(mailfrom, &mailfrom[lp]);
                        }
@@ -717,7 +722,6 @@ void smtp_try(char *key, char *addr, int *status, char *dsn, long msgnum)
        } while (scan_done == 0);
        if (strlen(mailfrom)==0) strcpy(mailfrom, "someone@somewhere.org");
 
-
        /* Figure out what mail exchanger host we have to connect to */
        num_mxhosts = getmx(mxhosts, node);
        lprintf(9, "Number of MX hosts for <%s> is %d\n", node, num_mxhosts);
@@ -929,6 +933,8 @@ void smtp_do_bounce(char *instr) {
        time_t submitted = 0L;
        struct CtdlMessage *bmsg = NULL;
        int give_up = 0;
+       struct recptypes *valid;
+       int successful_bounce = 0;
 
        lprintf(9, "smtp_do_bounce() called\n");
        strcpy(bounceto, "");
@@ -1030,28 +1036,29 @@ void smtp_do_bounce(char *instr) {
 
                /* First try the user who sent the message */
                lprintf(9, "bounce to user? <%s>\n", bounceto);
-               TRACE;
                if (strlen(bounceto) == 0) {
                        lprintf(7, "No bounce address specified\n");
                        bounce_msgid = (-1L);
                }
-/* FIXME this won't work
-               else if (mes_type = alias(bounceto), mes_type == MES_ERROR) {
-                       lprintf(7, "Invalid bounce address <%s>\n", bounceto);
-                       bounce_msgid = (-1L);
+
+               /* Can we deliver the bounce to the original sender? */
+               valid = validate_recipients(bounceto);
+               if (valid != NULL) {
+                       if (valid->num_error == 0) {
+                               CtdlSubmitMsg(bmsg, valid, "");
+                               successful_bounce = 1;
+                       }
                }
-               else {
-                       bounce_msgid = CtdlSubmitMsg(bmsg,
-                               bounceto,
-                               "", mes_type);
+
+               /* If not, post it in the Aide> room */
+               if (successful_bounce == 0) {
+                       CtdlSubmitMsg(bmsg, NULL, AIDEROOM);
                }
- */
-               TRACE;
 
-               /* Otherwise, go to the Aide> room */
-               lprintf(9, "bounce to room?\n");
-               if (bounce_msgid < 0L) bounce_msgid = CtdlSubmitMsg(bmsg,
-                       NULL, AIDEROOM);
+               /* Free up the memory we used */
+               if (valid != NULL) {
+                       phree(valid);
+               }
        }
 
        CtdlFreeMessage(bmsg);
@@ -1210,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);
@@ -1220,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);
                        }
@@ -1305,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");
@@ -1345,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;
        }
 
@@ -1377,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$";