]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_smtp.c
removed all references to sprintf from several files (not all files yet)
[citadel.git] / citadel / serv_smtp.c
index a74b8cb5a9676584a1716dbcc678fc5cc229ea2c..25082d77c12e04fca656e6bedf7f3a17de98c94c 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 */
@@ -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,11 +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];
-       int bypass_spam_checking = 0;
+       struct recptypes *valid;
 
        if (strlen(SMTP->from) == 0) {
                cprintf("503 Need MAIL before RCPT\r\n");
@@ -417,72 +417,32 @@ void smtp_rcpt(char *argbuf) {
        striplt(recp);
        stripallbut(recp, '<', '>');
 
-       /* Allow relaying if it's from the Internet to another Citadel node
-        * for whom we are providing directory service.
-        */
-       bypass_spam_checking = IsDirectory(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 RCPT ok local\r\n");
-                       if (SMTP->valid.num_local > 0) {
-                               strcat(SMTP->valid.recp_local, "|");
-                       }
-                       strcat(SMTP->valid.recp_local, user);
-                       SMTP->valid.num_local += 1;
-                       SMTP->number_of_recipients += 1;
-                       return;
-
-               case rfc822_room_delivery:
-                       cprintf("250 RCPT ok room\r\n");
-                       if (SMTP->valid.num_room > 0) {
-                               strcat(SMTP->valid.recp_room, "|");
-                       }
-                       strcat(SMTP->valid.recp_room, user);
-                       SMTP->valid.num_room += 1;
-                       SMTP->number_of_recipients += 1;
-                       return;
-
-               case rfc822_address_on_citadel_network:
-                       cprintf("250 RCPT ok ignet\r\n");
-                       if (SMTP->valid.num_ignet > 0) {
-                               strcat(SMTP->valid.recp_ignet, "|");
-                       }
-                       strcat(SMTP->valid.recp_ignet, user);
-                       SMTP->valid.num_ignet += 1;
-                       SMTP->number_of_recipients += 1;
-                       return;
-
-               case rfc822_no_such_user:
-                       cprintf("550 %s: no such user\r\n", recp);
-                       return;
+       if ( (strlen(recp) + strlen(SMTP->recipients) + 1 ) >= SIZ) {
+               cprintf("452 Too many recipients\r\n");
+               return;
+       }
 
-               case rfc822_address_nonlocal:
-                       if ( (SMTP->message_originated_locally == 0)
-                          && (bypass_spam_checking == 0) ) {
-                               cprintf("551 Relaying denied.\r\n");
-                       }
-                       else {
-                               cprintf("250 RCPT ok relay <%s>\r\n", recp);
+       valid = validate_recipients(recp);
+       if (valid->num_error > 0) {
+               cprintf("599 Error: %s\r\n", valid->errormsg);
+               phree(valid);
+               return;
+       }
 
-                               if (SMTP->valid.num_internet > 0) {
-                                       strcat(SMTP->valid.recp_internet, "|");
-                               }
-                               strcat(SMTP->valid.recp_internet, user);
-                               SMTP->valid.num_internet += 1;
-                               SMTP->number_of_recipients += 1;
-                               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;
 }
 
 
@@ -496,6 +456,7 @@ void smtp_data(void) {
        struct CtdlMessage *msg;
        long msgnum;
        char nowstamp[SIZ];
+       struct recptypes *valid;
 
        if (strlen(SMTP->from) == 0) {
                cprintf("503 Need MAIL command first.\r\n");
@@ -509,14 +470,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);
        
@@ -543,8 +507,10 @@ void smtp_data(void) {
        }
 
        /* Submit the message into the Citadel system. */
-       msgnum = CtdlSubmitMsg(msg, &SMTP->valid, "");
+       valid = validate_recipients(SMTP->recipients);
+       msgnum = CtdlSubmitMsg(msg, valid, "");
        CtdlFreeMessage(msg);
+       phree(valid);
 
        if (msgnum > 0L) {
                cprintf("250 Message accepted.\r\n");