]> diplodocus.org Git - nmh/blobdiff - mts/smtp/smtp.c
Remove trailing spaces from lines in man pages.
[nmh] / mts / smtp / smtp.c
index c94ac31308063d2de637c531b857779b5051bae3..78f8355680d0c14ab811b0a1265b342afcc26483 100644 (file)
@@ -40,9 +40,6 @@
  */
 #endif
 
-#define        TRUE    1
-#define        FALSE   0
-
 #define        NBITS ((sizeof (int)) * 8)
 
 /*
@@ -84,7 +81,7 @@ static int smtp_init (char *, char *, char *, int, int, int, int, const char *,
 static int sendmail_init (char *, int, int, int, int, const char *,
                          const char *);
 
-static int rclient (char *, char *);
+static int rclient (char *, char *, char **);
 static int sm_ierror (const char *fmt, ...);
 static int sm_nerror (char *);
 static int smtalk (int time, char *fmt, ...);
@@ -103,9 +100,9 @@ sm_init (char *client, char *server, char *port, int watch, int verbose,
     if (sm_mts == MTS_SMTP)
        return smtp_init (client, server, port, watch, verbose,
                          debug, sasl, saslmech, user, oauth_svc, tls);
-    else
-       return sendmail_init (client, watch, verbose, debug, sasl,
-                             saslmech, user);
+
+    return sendmail_init (client, watch, verbose, debug, sasl,
+                          saslmech, user);
 }
 
 static int
@@ -114,7 +111,7 @@ smtp_init (char *client, char *server, char *port, int watch, int verbose,
            const char *oauth_svc, int tls)
 {
     int result, sd1;
-    char *errstr;
+    char *errstr, *chosen_server;
 
     if (watch)
        verbose = TRUE;
@@ -131,26 +128,34 @@ smtp_init (char *client, char *server, char *port, int watch, int verbose,
        } else {
            client = LocalName(1);      /* no clientname -> LocalName */
        }
-    }
 
-    /*
-     * Last-ditch check just in case client still isn't set to anything
-     */
-
-    if (client == NULL || *client == '\0')
-       client = "localhost";
+        /*
+         * Last-ditch check just in case client still isn't set to anything
+         */
+        if (client == NULL || *client == '\0')
+            client = "localhost";
+    }
 
     nsc = netsec_init();
 
     if (user)
        netsec_set_userid(nsc, user);
 
+    if ((sd1 = rclient (server, port, &chosen_server)) == NOTOK)
+       return RP_BHST;
+
+    SIGNAL (SIGPIPE, SIG_IGN);
+
+    netsec_set_fd(nsc, sd1, sd1);
+
+    netsec_set_hostname(nsc, chosen_server);
+
     if (sm_debug)
        netsec_set_snoop(nsc, 1);
 
     if (sasl) {
-       if (netsec_set_sasl_params(nsc, server, "smtp", saslmech,
-                                  sm_sasl_callback, &errstr) != OK)
+       if (netsec_set_sasl_params(nsc, "smtp", saslmech, sm_sasl_callback,
+                                  &errstr) != OK)
            return sm_nerror(errstr);
     }
 
@@ -159,25 +164,18 @@ smtp_init (char *client, char *server, char *port, int watch, int verbose,
            return sm_ierror("OAuth2 not supported");
     }
 
-    if ((sd1 = rclient (server, port)) == NOTOK)
-       return RP_BHST;
-
-    SIGNAL (SIGPIPE, SIG_IGN);
-
-    netsec_set_fd(nsc, sd1, sd1);
-
-    if (tls) {
-       if (netsec_set_tls(nsc, 1, &errstr) != OK)
+    if (tls & S_TLSENABLEMASK) {
+       if (netsec_set_tls(nsc, 1, tls & S_NOVERIFY, &errstr) != OK)
            return sm_nerror(errstr);
     }
 
     /*
-     * If tls == 2, that means that the user requested "initial" TLS,
-     * which happens right after the connection has opened.  Do that
-     * negotiation now
+     * If tls == S_INITTLS, that means that the user requested
+     * "initial" TLS, which happens right after the connection has
+     * opened.  Do that negotiation now
      */
 
-    if (tls == 2) {
+    if (tls & S_INITTLS) {
        if (netsec_negotiate_tls(nsc, &errstr) != OK) {
            sm_end(NOTOK);
            return sm_nerror(errstr);
@@ -218,7 +216,7 @@ smtp_init (char *client, char *server, char *port, int watch, int verbose,
      * restart the EHLO dialog after TLS negotiation is complete.
      */
 
-    if (tls == 1) {
+    if (tls & S_STARTTLS) {
        if (! EHLOset("STARTTLS")) {
            sm_end(NOTOK);
            return sm_ierror("SMTP server does not support TLS");
@@ -299,26 +297,27 @@ sendmail_init (char *client, int watch, int verbose, int debug, int sasl,
            client = clientname;
        else
            client = LocalName(1);      /* no clientname -> LocalName */
-    }
 
-    /*
-     * Last-ditch check just in case client still isn't set to anything
-     */
-
-    if (client == NULL || *client == '\0')
-       client = "localhost";
+        /*
+         * Last-ditch check just in case client still isn't set to anything
+         */
+        if (client == NULL || *client == '\0')
+            client = "localhost";
+    }
 
     nsc = netsec_init();
 
     if (user)
        netsec_set_userid(nsc, user);
 
+    netsec_set_hostname(nsc, client);
+
     if (sm_debug)
        netsec_set_snoop(nsc, 1);
 
     if (sasl) {
-       if (netsec_set_sasl_params(nsc, client, "smtp", saslmech,
-                                  sm_sasl_callback, &errstr) != OK)
+       if (netsec_set_sasl_params(nsc, "smtp", saslmech, sm_sasl_callback,
+                                  &errstr) != OK)
            return sm_nerror(errstr);
     }
 
@@ -427,11 +426,16 @@ sendmail_init (char *client, int watch, int verbose, int debug, int sasl,
 }
 
 static int
-rclient (char *server, char *service)
+rclient (char *server, char *service, char **chosen_server)
 {
     int sd;
     char response[BUFSIZ];
 
+    if (server == NULL)
+       server = servers;
+
+    *chosen_server = server;
+
     if ((sd = client (server, service, response, sizeof(response),
                      sm_debug)) != NOTOK)
        return sd;
@@ -453,6 +457,9 @@ 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"
+                    "Rebuild message with 7-bit headers, WITHOUT -headerencoding utf-8.",
+                    EHLOset ("SMTPUTF8") ? "8BITMIME" : "SMTPUTF8");
             sm_end (NOTOK);
             return RP_UCMD;
         }
@@ -464,7 +471,7 @@ sm_winit (char *from, int smtputf8, int eightbit)
         } else {
             advise (NULL, "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.\n");
+                    "locale to C, and/or specifying *b64 in mhbuild directives.");
             sm_end (NOTOK);
             return RP_UCMD;
         }
@@ -616,7 +623,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;
@@ -757,7 +765,8 @@ 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;
@@ -813,7 +822,7 @@ again: ;
                                                 &errstr)) != NULL ; ) {
 
        if (doingEHLO
-               && strncmp (buffer, "250", sizeof("250") - 1) == 0
+               && has_prefix(buffer, "250")
                && (buffer[3] == '-' || doingEHLO == 2)
                && buffer[4]) {
            if (doingEHLO == 2) {
@@ -875,7 +884,7 @@ again: ;
            continue;
        if (sm_reply.code < 100) {
            if (sm_verbose) {
-               printf ("%s\n", sm_reply.text);
+               puts(sm_reply.text);
                fflush (stdout);
            }
            goto again;
@@ -952,7 +961,7 @@ EHLOset (char *s)
 
     for (ehlo = EHLOkeys; *ehlo; ehlo++) {
        ep = *ehlo;
-       if (strncmp (ep, s, len) == 0) {
+       if (has_prefix(ep, s)) {
            for (ep += len; *ep == ' '; ep++)
                continue;
            return ep;
@@ -1030,7 +1039,7 @@ sm_sasl_callback(enum sasl_message_type mtype, unsigned const char *indata,
            return NOTOK;
        }
 
-       if (strncmp(line, "334 ", 4) != 0) {
+       if (!has_prefix(line, "334 ")) {
            netsec_err(errstr, "Improper SASL protocol response: %s", line);
            return NOTOK;
        }
@@ -1079,7 +1088,7 @@ sm_sasl_callback(enum sasl_message_type mtype, unsigned const char *indata,
        if (line == NULL)
            return NOTOK;
 
-       if (strncmp(line, "235 ", 4) != 0) {
+       if (!has_prefix(line, "235 ")) {
            if (len > 4)
                netsec_err(errstr, "Authentication failed: %s", line + 4);
            else