Clean up a bunch of unused code and options. Specifically:
[mmh] / uip / post.c
index f880152..b24f571 100644 (file)
@@ -2,8 +2,6 @@
 /*
  * post.c -- enter messages into the mail transport system
  *
- * $Id$
- *
  * 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/mts.h>
 
 #include <errno.h>
-#include <setjmp.h>
 #include <signal.h>
 
-#ifdef TIME_WITH_SYS_TIME
+#ifdef HAVE_SYS_TIME_H
 # include <sys/time.h>
-# include <time.h>
-#else
-# ifdef TM_IN_SYS_TIME
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
 #endif
+#include <time.h>
 
-#ifdef SMTPMTS
-# include <mts/smtp/smtp.h>
-#endif
+#include <mts/smtp/smtp.h>
 
 #ifndef CYRUS_SASL
 # define SASLminc(a) (a)
 # define SASLminc(a)  0
 #endif /* CYRUS_SASL */
 
+#ifndef TLS_SUPPORT
+# define TLSminc(a)  (a)
+#else /* TLS_SUPPORT */
+# define TLSminc(a)   0
+#endif /* TLS_SUPPORT */
+
 #define FCCS           10      /* max number of fccs allowed */
 
 #define        uptolow(c)      ((isalpha(c) && isupper (c)) ? tolower (c) : c)
@@ -106,38 +101,36 @@ static struct swit switches[] = {
     { "dashstuffing", -12 },           /* should we dashstuff BCC messages? */
 #define NBITSTUFFSW              23
     { "nodashstuffing", -14 },
-#define        MAILSW                   24
-    { "mail", -4 },                    /* specify MAIL smtp mode */
-#define        SAMLSW                   25
-    { "saml", -4 },                    /* specify SAML smtp mode */
-#define        SENDSW                   26
-    { "send", -4 },                    /* specify SEND smtp mode */
-#define        SOMLSW                   27
-    { "soml", -4 },                    /* specify SOML smtp mode */
-#define        ANNOSW                   28
+#define        ANNOSW                   24
     { "idanno number", -6 },           /* interface from send    */
-#define        DLVRSW                   29
-    { "deliver address-list", -7 },
-#define        CLIESW                   30
+#define        CLIESW                   25
     { "client host", -6 },
-#define        SERVSW                   31
-    { "server host", -6 },             /* specify alternate SMTP server */
-#define        SNOOPSW                  32
+#define        SERVSW                   26
+    { "server host", 6 },              /* specify alternate SMTP server */
+#define        SNOOPSW                  27
     { "snoop", -5 },                   /* snoop the SMTP transaction */
-#define        FILLSW                   33
-    { "fill-in file", -7 },
-#define        FILLUSW                  34
-    { "fill-up", -7 },
-#define        PARTSW                   35
+#define        PARTSW                   28
     { "partno", -6 },
-#define        QUEUESW                  36
+#define        QUEUESW                  29
     { "queued", -6 },
-#define SASLSW                   37
+#define SASLSW                   30
     { "sasl", SASLminc(-4) },
-#define SASLMECHSW               38
+#define NOSASLSW                 31
+    { "nosasl", SASLminc(-6) },
+#define SASLMXSSFSW              32
+    { "saslmaxssf", SASLminc(-10) },
+#define SASLMECHSW               33
     { "saslmech", SASLminc(-5) },
-#define USERSW                   39
+#define USERSW                   34
     { "user", SASLminc(-4) },
+#define PORTSW                  35
+    { "port server port name/number", 4 },
+#define TLSSW                   36
+    { "tls", TLSminc(-3) },
+#define FILEPROCSW              37
+    { "fileproc", -4 },
+#define MHLPROCSW               38
+    { "mhlproc", -3 },
     { NULL, 0 }
 };
 
@@ -234,8 +227,11 @@ static int checksw = 0;            /* whom -check                           */
 static int linepos=0;          /* putadr()'s position on the line       */
 static int nameoutput=0;       /* putadr() has output header name       */
 static int sasl=0;             /* Use SASL auth for SMTP                */
+static int saslssf=-1;         /* Our maximum SSF for SASL              */
 static char *saslmech=NULL;    /* Force use of particular SASL mech     */
 static char *user=NULL;                /* Authenticate as this user             */
+static char *port="smtp";      /* Name of server port for SMTP          */
+static int tls=0;              /* Use TLS for encryption                */
 
 static unsigned msgflags = 0;  /* what we've seen */
 
@@ -258,24 +254,19 @@ static char *fccfold[FCCS];       /* foldernames for FCC'ing       */
 
 static struct headers  *hdrtab;        /* table for the message we're doing */
 
-static struct mailname localaddrs={NULL};      /* local addrs     */
-static struct mailname netaddrs={NULL};                /* network addrs   */
-static struct mailname uuaddrs={NULL};         /* uucp addrs      */
-static struct mailname tmpaddrs={NULL};                /* temporary queue */
+static struct mailname localaddrs;             /* local addrs     */
+static struct mailname netaddrs;               /* network addrs   */
+static struct mailname uuaddrs;                        /* uucp addrs      */
+static struct mailname tmpaddrs;               /* temporary queue */
 
-#ifdef SMTPMTS
 static int snoop      = 0;
-static int smtpmode   = S_MAIL;
 static char *clientsw = NULL;
 static char *serversw = NULL;
 
 extern struct smtp sm_reply;
-#endif /* SMTPMTS */
 
 static char prefix[] = "----- =_aaaaaaaaaa";
 
-static int fill_up = 0;
-static char *fill_in = NULL;
 static char *partno = NULL;
 static int queued = 0;
 
@@ -294,7 +285,7 @@ static int insert (struct mailname *);
 static void pl (void);
 static void anno (void);
 static int annoaux (struct mailname *);
-static void insert_fcc (struct headers *, char *);
+static void insert_fcc (struct headers *, unsigned char *);
 static void make_bcc_file (int);
 static void verify_all_addresses (int);
 static void chkadr (void);
@@ -449,33 +440,6 @@ main (int argc, char **argv)
                        adios (NULL, "bad argument %s %s", argp[-2], cp);
                    continue;
 
-               case DLVRSW:
-                   if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
-                   continue;
-
-#ifndef        SMTPMTS
-               case CLIESW:
-               case SERVSW:
-                   if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
-                   continue;
-
-               case SNOOPSW:
-                   continue;
-#else /* SMTPMTS */
-               case MAILSW:
-                   smtpmode = S_MAIL;
-                   continue;
-               case SAMLSW:
-                   smtpmode = S_SAML;
-                   continue;
-               case SOMLSW:
-                   smtpmode = S_SOML;
-                   continue;
-               case SENDSW:
-                   smtpmode = S_SEND;
-                   continue;
                case CLIESW:
                    if (!(clientsw = *argp++) || *clientsw == '-')
                        adios (NULL, "missing argument to %s", argp[-2]);
@@ -487,15 +451,7 @@ main (int argc, char **argv)
                case SNOOPSW:
                    snoop++;
                    continue;
-#endif /* SMTPMTS */
 
-               case FILLSW:
-                   if (!(fill_in = *argp++) || *fill_in == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
-                   continue;
-               case FILLUSW:
-                   fill_up++;
-                   continue;
                case PARTSW:
                    if (!(partno = *argp++) || *partno == '-')
                        adios (NULL, "missing argument to %s", argp[-2]);
@@ -508,6 +464,16 @@ main (int argc, char **argv)
                case SASLSW:
                    sasl++;
                    continue;
+
+               case NOSASLSW:
+                   sasl = 0;
+                   continue;
+
+               case SASLMXSSFSW:
+                   if (!(cp = *argp++) || *cp == '-')
+                       adios (NULL, "missing argument to %s", argp[-2]);
+                   saslssf = atoi(cp);
+                   continue;
                
                case SASLMECHSW:
                    if (!(saslmech = *argp++) || *saslmech == '-')
@@ -518,6 +484,27 @@ main (int argc, char **argv)
                    if (!(user = *argp++) || *user == '-')
                        adios (NULL, "missing argument to %s", argp[-2]);
                    continue;
+
+               case PORTSW:
+                   if (!(port = *argp++) || *port == '-')
+                       adios (NULL, "missing argument to %s", argp[-2]);
+                   continue;
+
+               case TLSSW:
+                   tls++;
+                   continue;
+
+               case FILEPROCSW:
+                   if (!(cp = *argp++) || *cp == '-')
+                       adios (NULL, "missing argument to %s", argp[-2]);
+                   fileproc = cp;
+                   continue;
+
+               case MHLPROCSW:
+                   if (!(cp = *argp++) || *cp == '-')
+                       adios (NULL, "missing argument to %s", argp[-2]);
+                   mhlproc = cp;
+                   continue;
            }
        }
        if (msg)
@@ -543,16 +530,17 @@ main (int argc, char **argv)
        discard (out = stdout); /* XXX: reference discard() to help loader */
     } else {
        if (whomsw) {
-           if ((out = fopen (fill_in ? fill_in : "/dev/null", "w")) == NULL)
+           if ((out = fopen ("/dev/null", "w")) == NULL)
                adios ("/dev/null", "unable to open");
        } else {
-           strncpy (tmpfil, m_scratch ("", m_maildir (invo_name)),
-               sizeof(tmpfil));
-           if ((out = fopen (tmpfil, "w")) == NULL) {
-               strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
-               if ((out = fopen (tmpfil, "w")) == NULL)
-                   adios (tmpfil, "unable to create");
-           }
+            char *cp = m_mktemp(m_maildir(invo_name), NULL, &out);
+            if (cp == NULL) {
+                cp = m_mktemp2(NULL, invo_name, NULL, &out);
+                if (cp == NULL) {
+                   adios ("post", "unable to create temporary file");
+                }
+            }
+            strncpy(tmpfil, cp, sizeof(tmpfil));
            chmod (tmpfil, 0600);
        }
     }
@@ -580,7 +568,7 @@ main (int argc, char **argv)
            case BODY: 
            case BODYEOF: 
                finish_headers (out);
-               if (whomsw && !fill_in)
+               if (whomsw)
                    break;
                fprintf (out, "\n%s", buf);
                while (state == BODY) {
@@ -616,8 +604,7 @@ main (int argc, char **argv)
 
     /* If we are doing a "whom" check */
     if (whomsw) {
-       if (!fill_up)
-           verify_all_addresses (1);
+       verify_all_addresses (1);
        done (0);
     }
 
@@ -639,7 +626,8 @@ main (int argc, char **argv)
     if (verbose)
        printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
                partno);
-    return done (0);
+    done (0);
+    return 1;
 }
 
 
@@ -653,7 +641,7 @@ putfmt (char *name, char *str, FILE *out)
     int count, grp, i, keep;
     char *cp, *pp, *qp;
     char namep[BUFSIZ];
-    struct mailname *mp, *np;
+    struct mailname *mp = NULL, *np = NULL;
     struct headers *hdr;
 
     while (*str == ' ' || *str == '\t')
@@ -672,17 +660,11 @@ putfmt (char *name, char *str, FILE *out)
 
     hdr = &hdrtab[i];
     if (hdr->flags & HIGN) {
-       if (fill_in)
-           fprintf (out, "%s: %s", name, str);
        return;
     }
     if (hdr->flags & HBAD) {
-       if (fill_in)
-           fprintf (out, "%s: %s", name, str);
-       else {
-           advise (NULL, "illegal header line -- %s:", name);
-           badmsg++;
-       }
+       advise (NULL, "illegal header line -- %s:", name);
+       badmsg++;
        return;
     }
     msgflags |= (hdr->set & ~(MVIS | MINV));
@@ -690,11 +672,6 @@ putfmt (char *name, char *str, FILE *out)
     if (hdr->flags & HSUB)
        subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
     if (hdr->flags & HFCC) {
-       if (fill_in) {
-           fprintf (out, "%s: %s", name, str);
-           return;
-       }
-
        if ((cp = strrchr(str, '\n')))
            *cp = 0;
        for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
@@ -739,7 +716,7 @@ putfmt (char *name, char *str, FILE *out)
 
     nameoutput = linepos = 0;
     snprintf (namep, sizeof(namep), "%s%s",
-               !fill_in && (hdr->flags & HMNG) ? "Original-" : "", name);
+               (hdr->flags & HMNG) ? "Original-" : "", name);
 
     for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
        if (mp->m_nohost) {     /* also used to test (hdr->flags & HTRY) */
@@ -820,8 +797,6 @@ putfmt (char *name, char *str, FILE *out)
        badmsg++;
     }
     if (linepos) {
-       if (fill_in && grp > 0)
-           putc (';', out);
        putc ('\n', out);
     }
 }
@@ -830,7 +805,7 @@ putfmt (char *name, char *str, FILE *out)
 static void
 start_headers (void)
 {
-    char  *cp;
+    unsigned char  *cp;
     char myhost[BUFSIZ], sigbuf[BUFSIZ];
     struct mailname *mp;
 
@@ -838,8 +813,8 @@ start_headers (void)
     mygid = getgid ();
     time (&tclock);
 
-    strncpy (from, adrsprintf (NULL, NULL), sizeof(from));
-    strncpy (myhost, LocalName (), sizeof(myhost));
+    strncpy (from, getlocaladdr(), sizeof(from));
+    strncpy (myhost, LocalName (0), sizeof(myhost));
 
     for (cp = myhost; *cp; cp++)
        *cp = uptolow (*cp);
@@ -847,7 +822,7 @@ start_headers (void)
     if ((cp = getfullname ()) && *cp) {
        strncpy (sigbuf, cp, sizeof(sigbuf));
        snprintf (signature, sizeof(signature), "%s <%s>",
-               sigbuf, adrsprintf (NULL, NULL));
+               sigbuf, getlocaladdr());
        if ((cp = getname (signature)) == NULL)
            adios (NULL, "getname () failed -- you lose extraordinarily big");
        if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
@@ -856,7 +831,7 @@ start_headers (void)
        while (getname (""))
            continue;
     } else {
-       strncpy (signature, adrsprintf (NULL, NULL), sizeof(signature));
+       strncpy (signature, getlocaladdr(), sizeof(signature));
     }
 }
 
@@ -872,13 +847,13 @@ finish_headers (FILE *out)
 {
     switch (msgstate) {
        case NORMAL: 
-           if (whomsw && !fill_up)
+           if (whomsw)
                break;
 
            fprintf (out, "Date: %s\n", dtime (&tclock, 0));
            if (msgid)
                fprintf (out, "Message-ID: <%d.%ld@%s>\n",
-                       (int) getpid (), (long) tclock, LocalName ());
+                       (int) getpid (), (long) tclock, LocalName (1));
            if (msgflags & MFRM) {
                /* There was already a From: in the draft.  Don't add one. */
                if (!draft_from_masquerading)
@@ -907,13 +882,13 @@ finish_headers (FILE *out)
                advise (NULL, "message has no From: header");
                badmsg++;
            }
-           if (whomsw && !fill_up)
+           if (whomsw)
                break;
 
            fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
            if (msgid)
                fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
-                       (int) getpid (), (long) tclock, LocalName ());
+                       (int) getpid (), (long) tclock, LocalName (1));
            if (msgflags & MRFM) {
                /* There was already a Resent-From: in draft.  Don't add one. */
                if (!draft_from_masquerading)
@@ -946,7 +921,7 @@ get_header (char *header, struct headers *table)
     struct headers *h;
 
     for (h = table; h->value; h++)
-       if (!strcasecmp (header, h->value))
+       if (!mh_strcasecmp (header, h->value))
            return (h - table);
 
     return NOTOK;
@@ -962,7 +937,7 @@ putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flag
 
     if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
        return 0;
-    if ((!fill_in && (flags & (HBCC | HDCC))) || mp->m_ingrp)
+    if ((flags & (HBCC | HDCC)) || mp->m_ingrp)
        return 1;
 
     if (!nameoutput) {
@@ -973,7 +948,7 @@ putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flag
     if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
        mp->m_pers = getcpy (aka);
     if (format) {
-       if (mp->m_gname && !fill_in) {
+       if (mp->m_gname) {
            snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
            cp = buffer;
        } else {
@@ -1006,17 +981,15 @@ putgrp (char *name, char *group, FILE *out, unsigned int flags)
     int len;
     char *cp;
 
-    if (!fill_in && (flags & HBCC))
+    if (flags & HBCC)
        return;
 
     if (!nameoutput) {
        fprintf (out, "%s: ", name);
        linepos += (nameoutput = strlen (name) + 2);
-       if (fill_in)
-           linepos -= strlen (group);
     }
 
-    cp = fill_in ? group : concat (group, ";", NULL);
+    cp = concat (group, ";", NULL);
     len = strlen (cp);
 
     if (linepos > nameoutput) {
@@ -1048,8 +1021,8 @@ insert (struct mailname *np)
            : &netaddrs;
            mp->m_next;
            mp = mp->m_next)
-       if (!strcasecmp (np->m_host, mp->m_next->m_host)
-               && !strcasecmp (np->m_mbox, mp->m_next->m_mbox)
+       if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
+               && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)
                && np->m_bcc == mp->m_next->m_bcc)
            return 0;
 
@@ -1128,9 +1101,9 @@ annoaux (struct mailname *mp)
 
 
 static void
-insert_fcc (struct headers *hdr, char *pp)
+insert_fcc (struct headers *hdr, unsigned char *pp)
 {
-    char *cp;
+    unsigned char *cp;
 
     for (cp = pp; isspace (*cp); cp++)
        continue;
@@ -1157,16 +1130,17 @@ make_bcc_file (int dashstuff)
     pid_t child_id;
     char *vec[6];
     FILE *out;
+    char *tfile = NULL;
 
-    strncpy (bccfil, m_tmpfil ("bccs"), sizeof(bccfil));
-    if ((out = fopen (bccfil, "w")) == NULL)
-       adios (bccfil, "unable to create");
+    tfile = m_mktemp2(NULL, "bccs", NULL, &out);
+    if (tfile == NULL) adios("bcc", "unable to create temporary file");
     chmod (bccfil, 0600);
+    strncpy (bccfil, tfile, sizeof(bccfil));
 
     fprintf (out, "Date: %s\n", dtime (&tclock, 0));
     if (msgid)
        fprintf (out, "Message-ID: <%d.%ld@%s>\n",
-               (int) getpid (), (long) tclock, LocalName ());
+               (int) getpid (), (long) tclock, LocalName (1));
     if (msgflags & MFRM) {
       /* There was already a From: in the draft.  Don't add one. */
       if (!draft_from_masquerading)
@@ -1287,19 +1261,16 @@ make_bcc_file (int dashstuff)
 static int
 find_prefix (void)
 {
-    int        len, result;
-    char buffer[BUFSIZ];
+    int        result = OK;
+    unsigned char buffer[BUFSIZ];
     FILE *in;
 
     if ((in = fopen (tmpfil, "r")) == NULL)
        adios (tmpfil, "unable to re-open");
 
-    len = strlen (prefix);
-
-    result = OK;
     while (fgets (buffer, sizeof(buffer) - 1, in))
        if (buffer[0] == '-' && buffer[1] == '-') {
-           char *cp;
+           unsigned char *cp;
 
            for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
                if (!isspace (*cp))
@@ -1367,10 +1338,8 @@ do_addresses (int bccque, int talk)
 
     chkadr ();
 
-#ifdef SMTPMTS
     if (rp_isbad (retval = sm_waend ()))
        die (NULL, "problem ending addresses; %s", rp_string (retval));
-#endif /* SMTPMTS */
 }
 
 
@@ -1383,8 +1352,6 @@ do_addresses (int bccque, int talk)
  * SENDMAIL/SMTP routines
  */
 
-#ifdef SMTPMTS
-
 static void
 post (char *file, int bccque, int talk)
 {
@@ -1402,10 +1369,10 @@ post (char *file, int bccque, int talk)
 
     sigon ();
 
-    if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose,
-                                   snoop, onex, queued, sasl, saslmech,
-                                   user))
-           || rp_isbad (retval = sm_winit (smtpmode, from)))
+    if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch, verbose,
+                                   snoop, onex, queued, sasl, saslssf,
+                                   saslmech, user, tls))
+           || rp_isbad (retval = sm_winit (from)))
        die (NULL, "problem initializing server; %s", rp_string (retval));
 
     do_addresses (bccque, talk && verbose);
@@ -1441,9 +1408,10 @@ verify_all_addresses (int talk)
     sigon ();
 
     if (!whomsw || checksw)
-       if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose, snoop, 0,
-                                       queued, sasl, saslmech, user))
-               || rp_isbad (retval = sm_winit (smtpmode, from)))
+       if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
+                                       verbose, snoop, 0, queued, sasl,
+                                       saslssf, saslmech, user, tls))
+               || rp_isbad (retval = sm_winit (from)))
            die (NULL, "problem initializing server; %s", rp_string (retval));
 
     if (talk && !whomsw)
@@ -1568,28 +1536,22 @@ do_text (char *file, int fd)
     }
 }
 
-#endif /* SMTPMTS */
-
 
 /*
  * SIGNAL HANDLING
  */
 
-static RETSIGTYPE
+static void
 sigser (int i)
 {
-#ifndef RELIABLE_SIGNALS
-    SIGNAL (i, SIG_IGN);
-#endif
+    NMH_UNUSED (i);
 
     unlink (tmpfil);
     if (msgflags & MINV)
        unlink (bccfil);
 
-#ifdef SMTPMTS
     if (!whomsw || checksw)
        sm_end (NOTOK);
-#endif /* SMTPMTS */
 
     done (1);
 }
@@ -1705,10 +1667,8 @@ die (char *what, char *fmt, ...)
     if (msgflags & MINV)
        unlink (bccfil);
 
-#ifdef SMTPMTS
     if (!whomsw || checksw)
        sm_end (NOTOK);
-#endif /* SMTPMTS */
 
     va_start(ap, fmt);
     advertise (what, NULL, fmt, ap);