]> diplodocus.org Git - nmh/blobdiff - mts/smtp/smtp.c
m_gmprot.c: Move interface to own file.
[nmh] / mts / smtp / smtp.c
index 77bcb6e544df97bb8fceb618a62703ee9fe9097a..8270b343a4796f7c8891a005a3e28269f50e0cfd 100644 (file)
@@ -1,19 +1,22 @@
-/*
- * smtp.c -- nmh SMTP interface
+/* smtp.c -- nmh SMTP interface
  *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
  * complete copyright information.
  */
 
-#include <h/mh.h>
+#include "h/mh.h"
+#include "sbr/r1bindex.h"
+#include "sbr/client.h"
+#include "sbr/error.h"
 #include "smtp.h"
-#include <h/mts.h>
-#include <h/signals.h>
-#include <h/utils.h>
-#include <h/netsec.h>
+#include "h/mts.h"
+#include "h/signals.h"
+#include "h/utils.h"
+#include "h/netsec.h"
 
 #include <sys/socket.h>
+#include "sbr/base64.h"
 
 /*
  * This module implements an interface to SendMail very similar
  * RP_-style codes.
  */
 
-/*
- * On older 4.2BSD machines without the POSIX function `sigaction',
- * the alarm handing stuff for time-outs will NOT work due to the way
- * syscalls get restarted.  This is not really crucial, since SendMail
- * is generally well-behaved in this area.
- */
-
-#ifdef SENDMAILBUG
-/*
- * It appears that some versions of Sendmail will return Code 451
- * when they don't really want to indicate a failure.
- * "Code 451 almost always means sendmail has deferred; we don't
- * really want bomb out at this point since sendmail will rectify
- * things later."  So, if you define SENDMAILBUG, Code 451 is
- * considered the same as Code 250.  Yuck!
- */
-#endif
-
 #define        NBITS ((sizeof (int)) * 8)
 
-/*
- * these codes must all be different!
- */
+/* Timeout in seconds for SMTP commands.
+ * Lore has it they must be distinct. */
 #define        SM_OPEN  300      /* Changed to 5 minutes to comply with a SHOULD in RFC 1123 */
 #define        SM_HELO  20
 #define        SM_RSET  15
 #define        SM_MAIL  301      /* changed to 5 minutes and a second (for uniqueness), see above */
 #define        SM_RCPT  302      /* see above */
 #define        SM_DATA  120      /* see above */
-#define        SM_TEXT 180     /* see above */
 #define        SM_DOT  600     /* see above */
 #define        SM_QUIT  30
-#define        SM_CLOS  10
-#define        SM_AUTH  45
 
 static int sm_addrs = 0;
 static int sm_child = NOTOK;
 static int sm_debug = 0;
-static int sm_nl = TRUE;
+static bool sm_nl = true;
 static int sm_verbose = 0;
 static netsec_context *nsc = NULL;
 
@@ -82,15 +63,15 @@ static int sendmail_init (char *, int, int, int, int, const char *,
                          const char *);
 
 static int rclient (char *, char *, char **);
-static int sm_ierror (const char *fmt, ...);
+static int sm_ierror (const char *fmt, ...) CHECK_PRINTF(1, 2);
 static int sm_nerror (char *);
-static int smtalk (int time, char *fmt, ...);
+static int smtalk (int time, char *fmt, ...) CHECK_PRINTF(2, 3);
 static int sm_wstream (char *, int);
 static int smhear (void);
-static char *EHLOset (char *);
+static char *EHLOset (char *) PURE;
 static int sm_sasl_callback(enum sasl_message_type, unsigned const char *,
                            unsigned int, unsigned char **, unsigned int *,
-                           char **);
+                           void *, char **);
 
 int
 sm_init (char *client, char *server, char *port, int watch, int verbose,
@@ -114,7 +95,7 @@ smtp_init (char *client, char *server, char *port, int watch, int verbose,
     char *errstr, *chosen_server;
 
     if (watch)
-       verbose = TRUE;
+       verbose = true;
 
     sm_verbose = verbose;
     sm_debug = debug;
@@ -155,7 +136,7 @@ smtp_init (char *client, char *server, char *port, int watch, int verbose,
 
     if (sasl) {
        if (netsec_set_sasl_params(nsc, "smtp", saslmech, sm_sasl_callback,
-                                  &errstr) != OK)
+                                  NULL, &errstr) != OK)
            return sm_nerror(errstr);
     }
 
@@ -285,7 +266,7 @@ sendmail_init (char *client, int watch, int verbose, int debug, int sasl,
     char *vec[15], *errstr;
 
     if (watch)
-       verbose = TRUE;
+       verbose = true;
 
     sm_verbose = verbose;
     sm_debug = debug;
@@ -317,7 +298,7 @@ sendmail_init (char *client, int watch, int verbose, int debug, int sasl,
 
     if (sasl) {
        if (netsec_set_sasl_params(nsc, "smtp", saslmech, sm_sasl_callback,
-                                  &errstr) != OK)
+                                  NULL, &errstr) != OK)
            return sm_nerror(errstr);
     }
 
@@ -329,9 +310,7 @@ sendmail_init (char *client, int watch, int verbose, int debug, int sasl,
        return sm_ierror ("no pipes");
     }
 
-    for (i = 0; (sm_child = fork ()) == NOTOK && i < 5; i++)
-       sleep (5);
-
+    sm_child = fork();
     switch (sm_child) {
        case NOTOK: 
            close (pdo[0]);
@@ -363,7 +342,7 @@ sendmail_init (char *client, int watch, int verbose, int debug, int sasl,
            execvp (sendmail, vec);
            fprintf (stderr, "unable to exec ");
            perror (sendmail);
-           _exit (-1);         /* NOTREACHED */
+           _exit(1);           /* NOTREACHED */
 
        default: 
            SIGNAL (SIGPIPE, SIG_IGN);
@@ -371,7 +350,7 @@ sendmail_init (char *client, int watch, int verbose, int debug, int sasl,
            close (pdi[1]);
            close (pdo[0]);
 
-           netsec_set_fd(nsc, pdi[i], pdo[1]);
+           netsec_set_fd(nsc, pdi[0], pdo[1]);
            netsec_set_timeout(nsc, SM_OPEN);
            result = smhear ();
            switch (result) {
@@ -457,7 +436,7 @@ sm_winit (char *from, int smtputf8, int eightbit)
         if (EHLOset ("8BITMIME")  &&  EHLOset ("SMTPUTF8")) {
             mail_parameters = " BODY=8BITMIME SMTPUTF8";
         } else {
-            advise (NULL, "SMTP server does not support %s, not sending.\n"
+            inform("SMTP server does not support %s, not sending.\n"
                     "Rebuild message with 7-bit headers, WITHOUT -headerencoding utf-8.",
                     EHLOset ("SMTPUTF8") ? "8BITMIME" : "SMTPUTF8");
             sm_end (NOTOK);
@@ -469,7 +448,7 @@ sm_winit (char *from, int smtputf8, int eightbit)
         if (EHLOset ("8BITMIME")) {
             mail_parameters = " BODY=8BITMIME";
         } else {
-            advise (NULL, "SMTP server does not support 8BITMIME, not sending.\n"
+            inform("SMTP server does not support 8BITMIME, not sending.\n"
                     "Suggest encoding message for 7-bit transport by setting your\n"
                     "locale to C, and/or specifying *b64 in mhbuild directives.");
             sm_end (NOTOK);
@@ -498,17 +477,13 @@ sm_wadr (char *mbox, char *host, char *path)
 {
     switch (smtalk (SM_RCPT, host && *host ? "RCPT TO:<%s%s@%s>"
                                           : "RCPT TO:<%s%s>",
-                            path ? path : "", mbox, host)) {
+                            FENDNULL(path), mbox, host)) {
        case 250: 
        case 251: 
            sm_addrs++;
            return RP_OK;
 
        case 451: 
-#ifdef SENDMAILBUG
-           sm_addrs++;
-           return RP_OK;
-#endif /* SENDMAILBUG */
        case 421: 
        case 450: 
        case 452: 
@@ -535,14 +510,10 @@ sm_waend (void)
 {
     switch (smtalk (SM_DATA, "DATA")) {
        case 354: 
-           sm_nl = TRUE;
+           sm_nl = true;
            return RP_OK;
 
        case 451: 
-#ifdef SENDMAILBUG
-           sm_nl = TRUE;
-           return RP_OK;
-#endif /* SENDMAILBUG */
        case 421: 
            return RP_NO;
 
@@ -561,29 +532,51 @@ sm_waend (void)
 int
 sm_wtxt (char *buffer, int len)
 {
-    int result;
+    int result, snoopstate;
+
+    if ((snoopstate = netsec_get_snoop(nsc)))
+       netsec_set_snoop(nsc, 0);
 
     result = sm_wstream (buffer, len);
 
-    return (result == NOTOK ? RP_BHST : RP_OK);
+    netsec_set_snoop(nsc, snoopstate);
+    return result == NOTOK ? RP_BHST : RP_OK;
 }
 
 
 int
 sm_wtend (void)
 {
-    if (sm_wstream ((char *) NULL, 0) == NOTOK)
+    int snoopstate;
+
+    if ((snoopstate = netsec_get_snoop(nsc)))
+       netsec_set_snoop(nsc, 0);
+
+    if (sm_wstream(NULL, 0) == NOTOK)
        return RP_BHST;
 
+    /*
+     * Because snoop output now happens at flush time, if we are using snoop
+     * we force an extra flush here.  While this introduces some extra network
+     * traffic, we're only doing it when snoop is in effect so I think it's
+     * reasonable.
+     */
+
+    if (snoopstate) {
+       char *errstr;
+       if (netsec_flush(nsc, &errstr) != OK) {
+           sm_nerror(errstr);
+           return RP_BHST;
+       }
+       netsec_set_snoop(nsc, snoopstate);
+    }
+
     switch (smtalk (SM_DOT + 3 * sm_addrs, ".")) {
        case 250: 
        case 251: 
            return RP_OK;
 
        case 451: 
-#ifdef SENDMAILBUG
-           return RP_OK;
-#endif /* SENDMAILBUG */
        case 452: 
        default: 
            return RP_NO;
@@ -623,7 +616,8 @@ sm_end (int type)
        case NOTOK: 
            sm_note.code = sm_reply.code;
            sm_note.length = sm_reply.length;
-           memcpy (sm_note.text, sm_reply.text, sm_reply.length + 1);/* fall */
+           memcpy (sm_note.text, sm_reply.text, sm_reply.length + 1);
+           /* FALLTHRU */
        case DONE: 
            if (smtalk (SM_RSET, "RSET") == 250 && type == DONE)
                return RP_OK;
@@ -653,7 +647,7 @@ sm_end (int type)
     }
 
     if (nsc != NULL) {
-       netsec_shutdown(nsc, 1);
+       netsec_shutdown(nsc);
        nsc = NULL;
     }
 
@@ -666,7 +660,7 @@ sm_end (int type)
        status = OK;
     }
 
-    return (status ? RP_BHST : RP_OK);
+    return status ? RP_BHST : RP_OK;
 }
 
 
@@ -752,7 +746,7 @@ sm_wstream (char *buffer, int len)
     for (bp = buffer; bp && len > 0; bp++, len--) {
        switch (*bp) {
            case '\n': 
-               sm_nl = TRUE;
+               sm_nl = true;
                if (netsec_write(nsc, "\r", 1, &errstr) != OK) {
                    sm_nerror(errstr);
                    return NOTOK;
@@ -764,10 +758,11 @@ sm_wstream (char *buffer, int len)
                    if (netsec_write(nsc, ".", 1, &errstr) != OK) {
                    sm_nerror(errstr);
                    return NOTOK;
-               } /* FALL THROUGH */
+               }
+               /* FALLTHRU */
 
            default: 
-               sm_nl = FALSE;
+               sm_nl = false;
        }
        if (netsec_write(nsc, bp, 1, &errstr) != OK) {
            sm_nerror(errstr);
@@ -784,7 +779,8 @@ sm_wstream (char *buffer, int len)
 static int
 smhear (void)
 {
-    int i, code, cont, more;
+    int i, code;
+    bool cont, more;
     size_t buflen, rc;
     unsigned char *bp;
     char *rp;
@@ -792,7 +788,7 @@ smhear (void)
     char **ehlo = EHLOkeys, *buffer;
 
     if (doingEHLO) {
-       static int at_least_once = 0;
+       static bool at_least_once;
 
        if (at_least_once) {
            char *ep;
@@ -802,7 +798,7 @@ smhear (void)
                free (ep);
            }
        } else {
-           at_least_once = 1;
+           at_least_once = true;
        }
 
        ehlo = EHLOkeys;
@@ -816,7 +812,7 @@ again: ;
     rp = sm_reply.text;
     rc = sizeof(sm_reply.text) - 1;
 
-    for (more = FALSE; (buffer = netsec_readline(nsc, &buflen,
+    for (more = false; (buffer = netsec_readline(nsc, &buflen,
                                                 &errstr)) != NULL ; ) {
 
        if (doingEHLO
@@ -842,13 +838,13 @@ again: ;
        for (; buflen > 0 && (!isascii (*bp) || !isdigit (*bp)); bp++, buflen--)
            continue;
 
-       cont = FALSE;
+       cont = false;
        code = atoi ((char *) bp);
        bp += 3, buflen -= 3;
        for (; buflen > 0 && isspace (*bp); bp++, buflen--)
            continue;
        if (buflen > 0 && *bp == '-') {
-           cont = TRUE;
+           cont = true;
            bp++, buflen--;
            for (; buflen > 0 && isspace (*bp); bp++, buflen--)
                continue;
@@ -857,7 +853,7 @@ again: ;
        if (more) {
            if (code != sm_reply.code || cont)
                continue;
-           more = FALSE;
+           more = false;
        } else {
            sm_reply.code = code;
            more = cont;
@@ -902,7 +898,8 @@ char *
 rp_string (int code)
 {
     char *text;
-    static char buffer[BUFSIZ];
+    /* The additional space is to avoid warning from gcc -Wformat-truncation. */
+    static char buffer[BUFSIZ + 19];
 
     switch (sm_reply.code != NOTOK ? code : NOTOK) {
        case RP_AOK:
@@ -966,7 +963,7 @@ EHLOset (char *s)
        }
     }
 
-    return 0;
+    return NULL;
 }
 
 /*
@@ -978,11 +975,12 @@ EHLOset (char *s)
 static int
 sm_sasl_callback(enum sasl_message_type mtype, unsigned const char *indata,
                 unsigned int indatalen, unsigned char **outdata,
-                unsigned int *outdatalen, char **errstr)
+                unsigned int *outdatalen, void *context, char **errstr)
 {
     int rc, snoopoffset;
     char *mech, *line;
     size_t len;
+    NMH_UNUSED(context);
 
     switch (mtype) {
     case NETSEC_SASL_START:
@@ -1005,15 +1003,19 @@ sm_sasl_callback(enum sasl_message_type mtype, unsigned const char *indata,
            snoopoffset = 6 + strlen(mech);
            rc = netsec_printf(nsc, errstr, "AUTH %s %s\r\n", mech, b64data);
            free(b64data);
-           netsec_set_snoop_callback(nsc, NULL, NULL);
        } else {
            rc = netsec_printf(nsc, errstr, "AUTH %s\r\n", mech);
        }
 
-       if (rc != OK)
+       if (rc != OK) {
+           netsec_set_snoop_callback(nsc, NULL, NULL);
            return NOTOK;
+       }
 
-       if (netsec_flush(nsc, errstr) != OK)
+       rc = netsec_flush(nsc, errstr);
+       netsec_set_snoop_callback(nsc, NULL, NULL);
+
+       if (rc != OK)
            return NOTOK;
 
        break;
@@ -1067,14 +1069,16 @@ sm_sasl_callback(enum sasl_message_type mtype, unsigned const char *indata,
            writeBase64raw(indata, indatalen, b64data);
            netsec_set_snoop_callback(nsc, netsec_b64_snoop_decoder, NULL);
            rc = netsec_printf(nsc, errstr, "%s\r\n", b64data);
-           netsec_set_snoop_callback(nsc, NULL, NULL);
            free(b64data);
        }
 
        if (rc != OK)
            return NOTOK;
 
-       if (netsec_flush(nsc, errstr) != OK)
+       rc = netsec_flush(nsc, errstr);
+       netsec_set_snoop_callback(nsc, NULL, NULL);
+
+       if (rc != OK)
            return NOTOK;
        break;