Add/update copyright notice in all source code files.
[mmh] / uip / post.c
index 8eb3b77..32888e6 100644 (file)
@@ -3,6 +3,10 @@
  * 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/mh.h>
 #include <h/dropsbr.h>
 #include <h/mime.h>
 
-#include <zotnet/tws/tws.h>
-#include <zotnet/mts/mts.h>
+#include <h/tws.h>
+#include <h/mts.h>
 
 #include <errno.h>
 #include <setjmp.h>
 #include <signal.h>
 
+#ifdef TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# ifdef TM_IN_SYS_TIME
+#  include <sys/time.h>
+# else
+#  include <time.h>
+# endif
+#endif
+
 #ifdef MMDFMTS
 # include <mts/mmdf/util.h>
 # include <mts/mmdf/mmdf.h>
 #endif
 
-/*
- * Currently smtp and sendmail use
- * the same interface for posting.
- */
 #ifdef SMTPMTS
-# define SENDMTS
-#endif
-
-#ifdef SENDMTS
 # include <mts/smtp/smtp.h>
 #endif
 
 # define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : (c))
 #endif
 
+#ifndef CYRUS_SASL
+# define SASLminc(a) (a)
+#else /* CYRUS_SASL */
+# define SASLminc(a)  0
+#endif /* CYRUS_SASL */
+
 #define FCCS           10      /* max number of fccs allowed */
 
+/* In the following array of structures, the numeric second field of the
+   structures (minchars) is apparently used like this:
+
+   -# : Switch can be abbreviated to # characters; switch hidden in -help.
+   0  : Switch can't be abbreviated;               switch shown in -help.
+   #  : Switch can be abbreviated to # characters; switch shown in -help. */
+
 static struct swit switches[] = {
 #define        ALIASW                    0
     { "alias aliasfile", 0 },
@@ -87,7 +107,7 @@ static struct swit switches[] = {
 #define VERSIONSW                20
     { "version", 0 },
 #define        HELPSW                   21
-    { "help", 4 },
+    { "help", 0 },
 #define BITSTUFFSW               22
     { "dashstuffing", -12 },           /* should we dashstuff BCC messages? */
 #define NBITSTUFFSW              23
@@ -118,6 +138,12 @@ static struct swit switches[] = {
     { "partno", -6 },
 #define        QUEUESW                  36
     { "queued", -6 },
+#define SASLSW                   37
+    { "sasl", SASLminc(-4) },
+#define SASLMECHSW               38
+    { "saslmech", SASLminc(-5) },
+#define USERSW                   39
+    { "user", SASLminc(-4) },
     { NULL, 0 }
 };
 
@@ -217,6 +243,9 @@ static int whomsw = 0;              /* we are whom not post                  */
 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 char *saslmech=NULL;    /* Force use of particular SASL mech     */
+static char *user=NULL;                /* Authenticate as this user             */
 
 static unsigned msgflags = 0;  /* what we've seen */
 
@@ -249,14 +278,14 @@ static char *submitmode = "m";            /* deliver to mailbox only    */
 static char submitopts[6] = "vl";      /* initial options for submit */
 #endif /* MMDFMTS */
 
-#ifdef SENDMTS
+#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 /* SENDMTS */
+#endif /* SMTPMTS */
 
 static char prefix[] = "----- =_aaaaaaaaaa";
 
@@ -265,6 +294,8 @@ static char *fill_in = NULL;
 static char *partno = NULL;
 static int queued = 0;
 
+extern boolean  draft_from_masquerading;  /* defined in mts.c */
+
 /*
  * static prototypes
  */
@@ -455,7 +486,7 @@ main (int argc, char **argv)
                        adios (NULL, "missing argument to %s", argp[-2]);
                    continue;
 
-#ifndef        SENDMTS
+#ifndef        SMTPMTS
                case CLIESW:
                case SERVSW:
                    if (!(cp = *argp++) || *cp == '-')
@@ -464,7 +495,7 @@ main (int argc, char **argv)
 
                case SNOOPSW:
                    continue;
-#else /* SENDMTS */
+#else /* SMTPMTS */
                case MAILSW:
                    smtpmode = S_MAIL;
                    continue;
@@ -488,7 +519,7 @@ main (int argc, char **argv)
                case SNOOPSW:
                    snoop++;
                    continue;
-#endif /* SENDMTS */
+#endif /* SMTPMTS */
 
                case FILLSW:
                    if (!(fill_in = *argp++) || *fill_in == '-')
@@ -505,6 +536,20 @@ main (int argc, char **argv)
                case QUEUESW:
                    queued++;
                    continue;
+               
+               case SASLSW:
+                   sasl++;
+                   continue;
+               
+               case SASLMECHSW:
+                   if (!(saslmech = *argp++) || *saslmech == '-')
+                       adios (NULL, "missing argument to %s", argp[-2]);
+                   continue;
+               
+               case USERSW:
+                   if (!(user = *argp++) || *user == '-')
+                       adios (NULL, "missing argument to %s", argp[-2]);
+                   continue;
            }
        }
        if (msg)
@@ -736,7 +781,8 @@ putfmt (char *name, char *str, FILE *out)
 
     for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
        if (mp->m_nohost) {     /* also used to test (hdr->flags & HTRY) */
-           pp = akvalue (mp->m_mbox);
+           /* The address doesn't include a host, so it might be an alias. */
+           pp = akvalue (mp->m_mbox);  /* do mh alias substitution */
            qp = akvisible () ? mp->m_mbox : "";
            np = mp;
            if (np->m_gname)
@@ -746,6 +792,19 @@ putfmt (char *name, char *str, FILE *out)
                    badadr++;
                    continue;
                }
+
+               if (draft_from_masquerading && ((msgstate == RESENT)
+                                               ? (hdr->set & MRFM)
+                                               : (hdr->set & MFRM)))
+                   /* The user manually specified a [Resent-]From: address in
+                      their draft and the "masquerade:" line in mts.conf
+                      doesn't contain "draft_from", so we'll set things up to
+                      use the actual email address embedded in the draft
+                      [Resent-]From: (after alias substitution, and without the
+                      GECOS full name or angle brackets) as the envelope
+                      From:. */
+                   strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
+
                if (hdr->flags & HBCC)
                    mp->m_bcc++;
                if (np->m_ingrp)
@@ -765,6 +824,19 @@ putfmt (char *name, char *str, FILE *out)
            mnfree (mp);
        }
        else {
+           /* Address includes a host, so no alias substitution is needed. */
+           if (draft_from_masquerading && ((msgstate == RESENT)
+                                           ? (hdr->set & MRFM)
+                                           : (hdr->set & MFRM)))
+               /* The user manually specified a [Resent-]From: address in
+                  their draft and the "masquerade:" line in mts.conf
+                  doesn't contain "draft_from", so we'll set things up to
+                  use the actual email address embedded in the draft
+                  [Resent-]From: (after alias substitution, and without the
+                  GECOS full name or angle brackets) as the envelope
+                  From:. */
+               strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
+
            if (hdr->flags & HBCC)
                mp->m_bcc++;
            if (mp->m_gname)
@@ -844,10 +916,18 @@ finish_headers (FILE *out)
            fprintf (out, "Date: %s\n", dtime (&tclock, 0));
            if (msgid)
                fprintf (out, "Message-ID: <%d.%ld@%s>\n",
-                       (int) getpid (), tclock, LocalName ());
-           if (msgflags & MFRM)
-               fprintf (out, "Sender: %s\n", from);
+                       (int) getpid (), (long) tclock, LocalName ());
+           if (msgflags & MFRM) {
+               /* There was already a From: in the draft.  Don't add one. */
+               if (!draft_from_masquerading)
+                   /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
+                      so we'll reveal the user's actual account@thismachine
+                      address in a Sender: header (and use it as the envelope
+                      From: later). */
+                   fprintf (out, "Sender: %s\n", from);
+           }
            else
+               /* Construct a From: header. */
                fprintf (out, "From: %s\n", signature);
            if (whomsw)
                break;
@@ -875,10 +955,18 @@ finish_headers (FILE *out)
            fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
            if (msgid)
                fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
-                       (int) getpid (), tclock, LocalName ());
-           if (msgflags & MRFM)
-               fprintf (out, "Resent-Sender: %s\n", from);
+                       (int) getpid (), (long) tclock, LocalName ());
+           if (msgflags & MRFM) {
+               /* There was already a Resent-From: in draft.  Don't add one. */
+               if (!draft_from_masquerading)
+                   /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
+                      so we'll reveal the user's actual account@thismachine
+                      address in a Sender: header (and use it as the envelope
+                      From: later). */
+                   fprintf (out, "Resent-Sender: %s\n", from);
+           }
            else
+               /* Construct a Resent-From: header. */
                fprintf (out, "Resent-From: %s\n", signature);
            if (whomsw)
                break;
@@ -1120,7 +1208,7 @@ make_bcc_file (int dashstuff)
     fprintf (out, "Date: %s\n", dtime (&tclock, 0));
     if (msgid)
        fprintf (out, "Message-ID: <%d.%ld@%s>\n",
-               (int) getpid (), tclock, LocalName ());
+               (int) getpid (), (long) tclock, LocalName ());
     fprintf (out, "From: %s\n", signature);
     if (subject)
        fprintf (out, "Subject: %s", subject);
@@ -1315,10 +1403,10 @@ do_addresses (int bccque, int talk)
        die (NULL, "problem ending addresses [%s]\n", rp_valstr (retval));
 #endif /* MMDFMTS */
 
-#ifdef SENDMTS
+#ifdef SMTPMTS
     if (rp_isbad (retval = sm_waend ()))
        die (NULL, "problem ending addresses; %s", rp_string (retval));
-#endif /* SENDMTS */
+#endif /* SMTPMTS */
 }
 
 
@@ -1331,7 +1419,7 @@ do_addresses (int bccque, int talk)
  * SENDMAIL/SMTP routines
  */
 
-#ifdef SENDMTS
+#ifdef SMTPMTS
 
 static void
 post (char *file, int bccque, int talk)
@@ -1351,7 +1439,8 @@ post (char *file, int bccque, int talk)
     sigon ();
 
     if (rp_isbad (retval = sm_init (clientsw, serversw, watch, verbose,
-                                   snoop, onex, queued))
+                                   snoop, onex, queued, sasl, saslmech,
+                                   user))
            || rp_isbad (retval = sm_winit (smtpmode, from)))
        die (NULL, "problem initializing server; %s", rp_string (retval));
 
@@ -1388,7 +1477,8 @@ verify_all_addresses (int talk)
     sigon ();
 
     if (!whomsw || checksw)
-       if (rp_isbad (retval = sm_init (clientsw, serversw, 0, 0, snoop, 0, 0))
+       if (rp_isbad (retval = sm_init (clientsw, serversw, 0, 0, snoop, 0,
+                                       0, 0, 0, 0))
                || rp_isbad (retval = sm_winit (smtpmode, from)))
            die (NULL, "problem initializing server; %s", rp_string (retval));
 
@@ -1514,7 +1604,7 @@ do_text (char *file, int fd)
     }
 }
 
-#endif /* SENDMTS */
+#endif /* SMTPMTS */
 
 /*
  * MMDF routines
@@ -1807,10 +1897,10 @@ sigser (int i)
        mm_end (NOTOK);
 #endif /* MMDFMTS */
 
-#ifdef SENDMTS
+#ifdef SMTPMTS
     if (!whomsw || checksw)
        sm_end (NOTOK);
-#endif /* SENDMTS */
+#endif /* SMTPMTS */
 
     done (1);
 }
@@ -1931,10 +2021,10 @@ die (char *what, char *fmt, ...)
        mm_end (NOTOK);
 #endif /* MMDFMTS */
 
-#ifdef SENDMTS
+#ifdef SMTPMTS
     if (!whomsw || checksw)
        sm_end (NOTOK);
-#endif /* SENDMTS */
+#endif /* SMTPMTS */
 
     va_start(ap, fmt);
     advertise (what, NULL, fmt, ap);