3 * post.c -- enter messages into the mail transport system
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
13 #include <h/addrsbr.h>
14 #include <h/aliasbr.h>
15 #include <h/dropsbr.h>
25 #ifdef HAVE_SYS_TIME_H
26 # include <sys/time.h>
30 #include <mts/smtp/smtp.h>
33 # define SASLminc(a) (a)
34 #else /* CYRUS_SASL */
35 # define SASLminc(a) 0
36 #endif /* CYRUS_SASL */
39 # define TLSminc(a) (a)
40 #else /* TLS_SUPPORT */
42 #endif /* TLS_SUPPORT */
44 #define FCCS 10 /* max number of fccs allowed */
46 /* In the following array of structures, the numeric second field of the
47 structures (minchars) is apparently used like this:
49 -# : Switch can be abbreviated to # characters; switch hidden in -help.
50 0 : Switch can't be abbreviated; switch shown in -help.
51 # : Switch can be abbreviated to # characters; switch shown in -help. */
53 static struct swit switches[] = {
55 { "alias aliasfile", 0 },
57 { "check", -5 }, /* interface from whom */
59 { "nocheck", -7 }, /* interface from whom */
63 { "dist", -4 }, /* interface from dist */
65 { "filter filterfile", 0 },
73 { "library directory", -7 }, /* interface from send, whom */
91 { "whom", -4 }, /* interface from whom */
93 { "width columns", 0 },
99 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
100 #define NBITSTUFFSW 23
101 { "nodashstuffing", -14 },
103 { "idanno number", -6 }, /* interface from send */
105 { "client host", -6 },
107 { "server host", 6 }, /* specify alternate SMTP server */
109 { "snoop", -5 }, /* snoop the SMTP transaction */
115 { "sasl", SASLminc(-4) },
117 { "nosasl", SASLminc(-6) },
118 #define SASLMXSSFSW 32
119 { "saslmaxssf", SASLminc(-10) },
120 #define SASLMECHSW 33
121 { "saslmech", SASLminc(-5) },
123 { "user", SASLminc(-4) },
125 { "port server port name/number", 4 },
127 { "tls", TLSminc(-3) },
129 { "notls", TLSminc(-5) },
130 #define FILEPROCSW 38
135 { "mts smtp|sendmail/smtp|sendmail/pipe", 2 },
136 #define MESSAGEIDSW 41
137 { "messageid localname|random", 2 },
149 * flags for headers->flags
151 #define HNOP 0x0000 /* just used to keep .set around */
152 #define HBAD 0x0001 /* bad header - don't let it through */
153 #define HADR 0x0002 /* header has an address field */
154 #define HSUB 0x0004 /* Subject: header */
155 #define HTRY 0x0008 /* try to send to addrs on header */
156 #define HBCC 0x0010 /* don't output this header, unless MTS_SENDMAIL_PIPE */
157 #define HMNG 0x0020 /* munge this header */
158 #define HNGR 0x0040 /* no groups allowed in this header */
159 #define HFCC 0x0080 /* FCC: type header */
160 #define HNIL 0x0100 /* okay for this header not to have addrs */
161 #define HIGN 0x0200 /* ignore this header */
162 #define HDCC 0x0400 /* another undocumented feature */
163 #define HONE 0x0800 /* Only (zero or) one address allowed */
164 #define HEFM 0x1000 /* Envelope-From: header */
167 * flags for headers->set
169 #define MFRM 0x0001 /* we've seen a From: */
170 #define MDAT 0x0002 /* we've seen a Date: */
171 #define MRFM 0x0004 /* we've seen a Resent-From: */
172 #define MVIS 0x0008 /* we've seen sighted addrs */
173 #define MINV 0x0010 /* we've seen blind addrs */
174 #define MSND 0x0020 /* we've seen a Sender: */
175 #define MRSN 0x0040 /* We've seen a Resent-Sendr:*/
176 #define MEFM 0x0080 /* We've seen Envelope-From: */
179 static struct headers NHeaders[] = {
180 { "Return-Path", HBAD, 0 },
181 { "Received", HBAD, 0 },
182 { "Reply-To", HADR|HNGR, 0 },
183 { "From", HADR|HNGR, MFRM },
184 { "Sender", HADR|HNGR|HONE, MSND },
186 { "Subject", HSUB, 0 },
187 { "To", HADR|HTRY, MVIS },
188 { "cc", HADR|HTRY, MVIS },
189 { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
190 { "Dcc", HADR|HTRY|HDCC|HNIL, MVIS }, /* sorta cc & bcc combined */
191 { "Message-ID", HBAD, 0 },
193 { "Envelope-From", HADR|HONE|HEFM, MEFM },
197 static struct headers RHeaders[] = {
198 { "Resent-Reply-To", HADR|HNGR, 0 },
199 { "Resent-From", HADR|HNGR, MRFM },
200 { "Resent-Sender", HADR|HNGR, MRSN },
201 { "Resent-Date", HBAD, 0 },
202 { "Resent-Subject", HSUB, 0 },
203 { "Resent-To", HADR|HTRY, MVIS },
204 { "Resent-cc", HADR|HTRY, MVIS },
205 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
206 { "Resent-Message-ID", HBAD, 0 },
207 { "Resent-Fcc", HFCC, 0 },
208 { "Reply-To", HADR, 0 },
209 { "From", HADR|HNGR, MFRM },
210 { "Sender", HADR|HNGR, MSND },
211 { "Date", HNOP, MDAT },
212 { "To", HADR|HNIL, 0 },
213 { "cc", HADR|HNIL, 0 },
214 { "Bcc", HADR|HTRY|HBCC|HNIL, 0 },
216 { "Envelope-From", HADR|HONE|HEFM, MEFM },
220 static short fccind = 0; /* index into fccfold[] */
221 static short outputlinelen = OUTPUTLINELEN;
223 static int pfd = NOTOK; /* fd to write annotation list to */
224 static uid_t myuid= -1; /* my user id */
225 static gid_t mygid= -1; /* my group id */
226 static int recipients = 0; /* how many people will get a copy */
227 static int unkadr = 0; /* how many of those were unknown */
228 static int badadr = 0; /* number of bad addrs */
229 static int badmsg = 0; /* message has bad semantics */
230 static int verbose = 0; /* spell it out */
231 static int format = 1; /* format addresses */
232 static int mime = 0; /* use MIME-style encapsulations for Bcc */
233 static int msgid = 0; /* add msgid */
234 static int debug = 0; /* debugging post */
235 static int watch = 0; /* watch the delivery process */
236 static int whomsw = 0; /* we are whom not post */
237 static int checksw = 0; /* whom -check */
238 static int linepos=0; /* putadr()'s position on the line */
239 static int nameoutput=0; /* putadr() has output header name */
240 static int sasl=0; /* Use SASL auth for SMTP */
241 static int saslssf=-1; /* Our maximum SSF for SASL */
242 static char *saslmech=NULL; /* Force use of particular SASL mech */
243 static char *user=NULL; /* Authenticate as this user */
244 static char *port="smtp"; /* Name of server port for SMTP */
245 static int tls=0; /* Use TLS for encryption */
246 static int fromcount=0; /* Count of addresses on From: header */
247 static int seensender=0; /* Have we seen a Sender: header? */
249 static unsigned msgflags = 0; /* what we've seen */
253 static int msgstate = NORMAL;
255 static time_t tclock = 0; /* the time we started (more or less) */
257 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
259 static char tmpfil[BUFSIZ];
260 static char bccfil[BUFSIZ];
262 static char from[BUFSIZ]; /* my network address */
263 static char sender[BUFSIZ]; /* my Sender: header */
264 static char efrom[BUFSIZ]; /* my Envelope-From: header */
265 static char fullfrom[BUFSIZ]; /* full contents of From header */
266 static char signature[BUFSIZ]; /* my signature */
267 static char *filter = NULL; /* the filter for BCC'ing */
268 static char *subject = NULL; /* the subject field for BCC'ing */
269 static char *fccfold[FCCS]; /* foldernames for FCC'ing */
271 static struct headers *hdrtab; /* table for the message we're doing */
273 static struct mailname localaddrs; /* local addrs */
274 static struct mailname netaddrs; /* network addrs */
275 static struct mailname uuaddrs; /* uucp addrs */
276 static struct mailname tmpaddrs; /* temporary queue */
278 static int snoop = 0;
279 static char *clientsw = NULL;
280 static char *serversw = NULL;
282 extern struct smtp sm_reply;
284 static char prefix[] = "----- =_aaaaaaaaaa";
286 static char *partno = NULL;
287 static int queued = 0;
292 static void putfmt (char *, char *, FILE *);
293 static void start_headers (void);
294 static void finish_headers (FILE *);
295 static int get_header (char *, struct headers *);
296 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
297 static void putgrp (char *, char *, FILE *, unsigned int);
298 static int insert (struct mailname *);
299 static void pl (void);
300 static void anno (void);
301 static int annoaux (struct mailname *);
302 static void insert_fcc (struct headers *, unsigned char *);
303 static void make_bcc_file (int);
304 static void verify_all_addresses (int, char *);
305 static void chkadr (void);
306 static void sigon (void);
307 static void sigoff (void);
308 static void p_refile (char *);
309 static void fcc (char *, char *);
310 static void die (char *, char *, ...);
311 static void post (char *, int, int, char *);
312 static void do_text (char *file, int fd);
313 static void do_an_address (struct mailname *, int);
314 static void do_addresses (int, int);
315 static int find_prefix (void);
319 main (int argc, char **argv)
321 int state, compnum, dashstuff = 0;
322 char *cp, *msg = NULL, **argp, **arguments, *envelope;
323 char buf[BUFSIZ], name[NAMESZ];
327 setlocale(LC_ALL, "");
329 invo_name = r1bindex (argv[0], '/');
331 /* foil search of user profile/context */
332 if (context_foil (NULL) == -1)
335 mts_init (invo_name);
336 arguments = getarguments (invo_name, argc, argv, 0);
339 while ((cp = *argp++)) {
341 switch (smatch (++cp, switches)) {
343 ambigsw (cp, switches);
346 adios (NULL, "-%s unknown", cp);
349 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
350 print_help (buf, switches, 0);
353 print_version(invo_name);
357 if (!(cp = *argp++) || *cp == '-')
358 adios (NULL, "missing argument to %s", argp[-2]);
359 /* create a minimal context */
360 if (context_foil (cp) == -1)
365 if (!(cp = *argp++) || *cp == '-')
366 adios (NULL, "missing argument to %s", argp[-2]);
367 if ((state = alias (cp)) != AK_OK)
368 adios (NULL, "aliasing error in %s - %s",
369 cp, akerror (state));
388 if (!(filter = *argp++) || *filter == '-')
389 adios (NULL, "missing argument to %s", argp[-2]);
444 if (!(cp = *argp++) || *cp == '-')
445 adios (NULL, "missing argument to %s", argp[-2]);
446 if ((outputlinelen = atoi (cp)) < 10)
447 adios (NULL, "impossible width %d", outputlinelen);
451 if (!(cp = *argp++) || *cp == '-')
452 adios (NULL, "missing argument to %s", argp[-2]);
453 if ((pfd = atoi (cp)) <= 2)
454 adios (NULL, "bad argument %s %s", argp[-2], cp);
458 if (!(clientsw = *argp++) || *clientsw == '-')
459 adios (NULL, "missing argument to %s", argp[-2]);
462 if (!(serversw = *argp++) || *serversw == '-')
463 adios (NULL, "missing argument to %s", argp[-2]);
470 if (!(partno = *argp++) || *partno == '-')
471 adios (NULL, "missing argument to %s", argp[-2]);
487 if (!(cp = *argp++) || *cp == '-')
488 adios (NULL, "missing argument to %s", argp[-2]);
493 if (!(saslmech = *argp++) || *saslmech == '-')
494 adios (NULL, "missing argument to %s", argp[-2]);
498 if (!(user = *argp++) || *user == '-')
499 adios (NULL, "missing argument to %s", argp[-2]);
503 if (!(port = *argp++) || *port == '-')
504 adios (NULL, "missing argument to %s", argp[-2]);
516 if (!(cp = *argp++) || *cp == '-')
517 adios (NULL, "missing argument to %s", argp[-2]);
522 if (!(cp = *argp++) || *cp == '-')
523 adios (NULL, "missing argument to %s", argp[-2]);
528 if (!(cp = *argp++) || *cp == '-')
529 adios (NULL, "missing argument to %s", argp[-2]);
530 save_mts_method (cp);
534 if (!(cp = *argp++) || *cp == '-')
535 adios (NULL, "missing argument to %s", argp[-2]);
536 if (save_message_id_style (cp) != 0)
537 adios (NULL, "unsupported messageid \"%s\"", cp);
542 adios (NULL, "only one message at a time!");
550 adios (NULL, "usage: %s [switches] file", invo_name);
552 if (outputlinelen < 10)
553 adios (NULL, "impossible width %d", outputlinelen);
555 if ((in = fopen (msg, "r")) == NULL)
556 adios (msg, "unable to open");
561 discard (out = stdout); /* XXX: reference discard() to help loader */
564 if ((out = fopen ("/dev/null", "w")) == NULL)
565 adios ("/dev/null", "unable to open");
567 char *cp = m_mktemp(m_maildir(invo_name), NULL, &out);
569 cp = m_mktemp2(NULL, invo_name, NULL, &out);
571 adios ("post", "unable to create temporary file");
574 strncpy(tmpfil, cp, sizeof(tmpfil));
575 chmod (tmpfil, 0600);
579 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
581 for (compnum = 1, state = FLD;;) {
582 switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
587 cp = add (buf, NULL);
588 while (state == FLDPLUS) {
589 state = m_getfld (state, name, buf, sizeof(buf), in);
592 putfmt (name, cp, out);
596 finish_headers (out);
601 finish_headers (out);
604 fprintf (out, "\n%s", buf);
605 while (state == BODY) {
606 state = m_getfld (state, name, buf, sizeof(buf), in);
612 finish_headers (out);
617 adios (NULL, "message format error in component #%d", compnum);
620 adios (NULL, "getfld() returned %d", state);
637 * Here's how we decide which address to use as the envelope-from
640 * - If we were given an Envelope-From header, use that.
641 * - If we were given a Sender: address, use that.
642 * - Otherwise, use the address on the From: line
645 if (msgflags & MEFM) {
647 } else if (seensender) {
653 /* If we are doing a "whom" check */
655 /* This won't work with MTS_SENDMAIL_PIPE. */
656 verify_all_addresses (1, envelope);
660 if (msgflags & MINV) {
661 make_bcc_file (dashstuff);
662 if (msgflags & MVIS) {
663 if (sm_mts != MTS_SENDMAIL_PIPE) {
664 /* It would be nice to have support to call
665 verify_all_addresses with MTS_SENDMAIL_PIPE, but
666 that might require running sendmail as root. Note
667 that spost didn't verify addresses. */
668 verify_all_addresses (verbose, envelope);
670 post (tmpfil, 0, verbose, envelope);
672 post (bccfil, 1, verbose, envelope);
675 post (tmpfil, 0, isatty (1), envelope);
682 printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
694 putfmt (char *name, char *str, FILE *out)
696 int count, grp, i, keep;
699 struct mailname *mp = NULL, *np = NULL;
702 while (*str == ' ' || *str == '\t')
705 if (msgstate == NORMAL && uprf (name, "resent")) {
706 advise (NULL, "illegal header line -- %s:", name);
711 if ((i = get_header (name, hdrtab)) == NOTOK) {
712 if (strncasecmp (name, "nmh-", 4)) {
713 fprintf (out, "%s: %s", name, str);
715 /* Filter out all Nmh-* headers, because Norm asked. They
716 should never have reached this point. Warn about any
717 that are non-empty. */
718 if (strcmp (str, "\n")) {
719 char *newline = strchr (str, '\n');
720 if (newline) *newline = '\0';
722 advise (NULL, "ignoring header line -- %s: %s", name, str);
731 if (hdr->flags & HIGN) {
734 if (hdr->flags & HBAD) {
735 advise (NULL, "illegal header line -- %s:", name);
739 msgflags |= (hdr->set & ~(MVIS | MINV));
741 if (hdr->flags & HSUB)
742 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
743 if (hdr->flags & HFCC) {
744 if ((cp = strrchr(str, '\n')))
746 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
748 insert_fcc (hdr, pp);
750 insert_fcc (hdr, pp);
754 if (!(hdr->flags & HADR)) {
755 fprintf (out, "%s: %s", name, str);
759 tmpaddrs.m_next = NULL;
760 for (count = 0; (cp = getname (str)); count++)
761 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
765 tmpaddrs.m_next = mp;
769 if (hdr->flags & HTRY)
775 if (hdr->flags & HNIL)
776 fprintf (out, "%s: %s", name, str);
779 * Sender (or Resent-Sender) can have only one address
781 if ((msgstate == RESENT) ? (hdr->set & MRSN)
782 : (hdr->set & MSND)) {
783 advise (NULL, "%s: field requires one address", name);
787 advise (NULL, "%s: field requires at least one address", name);
794 if (count > 1 && (hdr->flags & HONE)) {
795 advise (NULL, "%s: field only permits one address", name);
800 nameoutput = linepos = 0;
801 snprintf (namep, sizeof(namep), "%s%s",
802 (hdr->flags & HMNG) ? "Original-" : "", name);
804 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
805 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
806 /* The address doesn't include a host, so it might be an alias. */
807 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
808 qp = akvisible () ? mp->m_mbox : "";
811 putgrp (namep, np->m_gname, out, hdr->flags);
812 while ((cp = getname (pp))) {
813 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
819 * If it's a From: or Resent-From: header, save the address
820 * for later possible use (as the envelope address for SMTP)
823 if ((msgstate == RESENT) ? (hdr->set & MRFM)
824 : (hdr->set & MFRM)) {
825 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
826 from[sizeof(from) - 1] = '\0';
831 * Also save the Sender: or Resent-Sender: header as well
834 if ((msgstate == RESENT) ? (hdr->set & MRSN)
835 : (hdr->set & MSND)) {
836 strncpy(sender, auxformat(mp, 0), sizeof(sender) - 1);
837 sender[sizeof(sender) - 1] = '\0';
842 * ALSO ... save Envelope-From
845 if (hdr->set & MEFM) {
846 strncpy(efrom, auxformat(mp, 0), sizeof(efrom) - 1);
847 efrom[sizeof(efrom) - 1] = '\0';
850 if (hdr->flags & HBCC)
853 mp->m_ingrp = np->m_ingrp;
856 putgrp (namep, mp->m_gname, out, hdr->flags);
859 if (putadr (namep, qp, mp, out, hdr->flags))
860 msgflags |= (hdr->set & (MVIS | MINV));
869 /* Address includes a host, so no alias substitution is needed. */
872 * If it's a From: or Resent-From header, save the address
873 * for later possible use (as the envelope address for SMTP)
876 if ((msgstate == RESENT) ? (hdr->set & MRFM)
877 : (hdr->set & MFRM)) {
878 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
883 * Also save the Sender: header as well
886 if ((msgstate == RESENT) ? (hdr->set & MRSN)
887 : (hdr->set & MSND)) {
888 strncpy(sender, auxformat(mp, 0), sizeof(sender) - 1);
889 sender[sizeof(sender) - 1] = '\0';
894 * ALSO ... save Envelope-From
897 if (hdr->set & MEFM) {
898 strncpy(efrom, auxformat(mp, 0), sizeof(efrom) - 1);
899 efrom[sizeof(efrom) - 1] = '\0';
902 if (hdr->flags & HBCC)
905 putgrp (namep, mp->m_gname, out, hdr->flags);
908 keep = putadr (namep, "", mp, out, hdr->flags);
912 msgflags |= (hdr->set & (MVIS | MINV));
919 * If this is a From:/Resent-From: header, save the full thing for
920 * later in case we need it for use when constructing a Bcc draft message
923 if ((msgstate == RESENT) ? (hdr->set & MRFM) : (hdr->set & MFRM)) {
924 strncpy(fullfrom, str, sizeof(fullfrom));
925 fullfrom[sizeof(fullfrom) - 1] = 0;
927 * Strip off any trailing newlines
930 while (strlen(fullfrom) > 0 && fullfrom[strlen(fullfrom) - 1] == '\n') {
931 fullfrom[strlen(fullfrom) - 1] = '\0';
935 if (grp > 0 && (hdr->flags & HNGR)) {
936 advise (NULL, "%s: field does not allow groups", name);
957 * Probably not necessary, but just in case ...
965 if ((cp = getfullname ()) && *cp) {
966 strncpy (sigbuf, cp, sizeof(sigbuf));
967 snprintf (signature, sizeof(signature), "%s <%s>",
968 sigbuf, getlocaladdr());
969 if ((cp = getname (signature)) == NULL)
970 adios (NULL, "getname () failed -- you lose extraordinarily big");
971 if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
972 adios (NULL, "bad signature '%s'", sigbuf);
977 strncpy (signature, getlocaladdr(), sizeof(signature));
983 * Now that we've outputted the header fields in the draft
984 * message, we will now output any remaining header fields
985 * that we need to add/create.
989 finish_headers (FILE *out)
993 if (!(msgflags & MFRM)) {
995 * A From: header is now required in the draft.
997 advise (NULL, "message has no From: header");
998 advise (NULL, "See default components files for examples");
1003 if (fromcount > 1 && (seensender == 0 && !(msgflags & MEFM))) {
1004 advise (NULL, "A Sender: or Envelope-From: header is required "
1005 "with multiple\nFrom: addresses");
1013 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1015 fprintf (out, "Message-ID: %s\n", message_id (tclock, 0));
1017 * If we have multiple From: addresses, make sure we have an
1018 * Sender: header. If we don't have one, then generate one
1019 * from Envelope-From: (which in this case, cannot be blank)
1022 if (fromcount > 1 && seensender == 0) {
1023 if (efrom[0] == '\0') {
1024 advise (NULL, "Envelope-From cannot be blank when there "
1025 "is multiple From: addresses\nand no Sender: "
1029 fprintf (out, "Sender: %s\n", efrom);
1033 if (!(msgflags & MVIS))
1034 fprintf (out, "Bcc: Blind Distribution List: ;\n");
1038 if (!(msgflags & MDAT)) {
1039 advise (NULL, "message has no Date: header");
1042 if (!(msgflags & MFRM)) {
1043 advise (NULL, "message has no From: header");
1046 if (!(msgflags & MRFM)) {
1047 advise (NULL, "message has no Resent-From: header");
1048 advise (NULL, "See default components files for examples");
1052 if (fromcount > 1 && (seensender == 0 && !(msgflags & MEFM))) {
1053 advise (NULL, "A Resent-Sender: or Envelope-From: header is "
1054 "required with multiple\nResent-From: addresses");
1062 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
1064 fprintf (out, "Resent-Message-ID: %s\n",
1065 message_id (tclock, 0));
1067 * If we have multiple Resent-From: addresses, make sure we have an
1068 * Resent-Sender: header. If we don't have one, then generate one
1069 * from Envelope-From (which in this case, cannot be blank)
1072 if (fromcount > 1 && seensender == 0) {
1073 if (efrom[0] == '\0') {
1074 advise (NULL, "Envelope-From cannot be blank when there "
1075 "is multiple Resent-From: addresses and no "
1076 "Resent-Sender: header");
1079 fprintf (out, "Resent-Sender: %s\n", efrom);
1083 if (!(msgflags & MVIS))
1084 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1089 adios (NULL, "re-format message and try again");
1091 adios (NULL, "no addressees");
1096 get_header (char *header, struct headers *table)
1100 for (h = table; h->value; h++)
1101 if (!mh_strcasecmp (header, h->value))
1109 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
1113 char buffer[BUFSIZ];
1115 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
1117 if (sm_mts != MTS_SENDMAIL_PIPE &&
1118 ((flags & (HBCC | HDCC | HEFM)) || mp->m_ingrp))
1122 fprintf (out, "%s: ", name);
1123 linepos += (nameoutput = strlen (name) + 2);
1126 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
1127 mp->m_pers = getcpy (aka);
1130 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
1133 cp = adrformat (mp);
1140 if (linepos != nameoutput) {
1141 if (len + linepos + 2 > outputlinelen)
1142 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1152 return (flags & HTRY);
1157 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1162 if (sm_mts != MTS_SENDMAIL_PIPE && (flags & HBCC))
1166 fprintf (out, "%s: ", name);
1167 linepos += (nameoutput = strlen (name) + 2);
1170 cp = concat (group, ";", NULL);
1173 if (linepos > nameoutput) {
1174 if (len + linepos + 2 > outputlinelen) {
1175 fprintf (out, ",\n%*s", nameoutput, "");
1176 linepos = nameoutput;
1190 insert (struct mailname *np)
1192 struct mailname *mp;
1194 if (np->m_mbox == NULL)
1197 for (mp = np->m_type == LOCALHOST ? &localaddrs
1198 : np->m_type == UUCPHOST ? &uuaddrs
1202 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
1203 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1204 && np->m_bcc == mp->m_next->m_bcc)
1217 struct mailname *mp;
1219 printf ("-------\n\t-- Addresses --\nlocal:\t");
1220 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1221 printf ("%s%s%s", mp->m_mbox,
1222 mp->m_bcc ? "[BCC]" : "",
1223 mp->m_next ? ",\n\t" : "");
1225 printf ("\nnet:\t");
1226 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1227 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1228 mp->m_mbox, mp->m_host,
1229 mp->m_bcc ? "[BCC]" : "",
1230 mp->m_next ? ",\n\t" : "");
1232 printf ("\nuucp:\t");
1233 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1234 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1235 mp->m_bcc ? "[BCC]" : "",
1236 mp->m_next ? ",\n\t" : "");
1238 printf ("\n\t-- Folder Copies --\nfcc:\t");
1239 for (i = 0; i < fccind; i++)
1240 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1248 struct mailname *mp;
1250 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1251 if (annoaux (mp) == NOTOK)
1254 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1255 if (annoaux (mp) == NOTOK)
1258 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1259 if (annoaux (mp) == NOTOK)
1269 annoaux (struct mailname *mp)
1272 char buffer[BUFSIZ];
1274 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1275 i = strlen (buffer);
1277 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1282 insert_fcc (struct headers *hdr, unsigned char *pp)
1286 for (cp = pp; isspace (*cp); cp++)
1288 for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1296 adios (NULL, "too many %ss", hdr->value);
1297 fccfold[fccind++] = getcpy (cp);
1305 make_bcc_file (int dashstuff)
1313 tfile = m_mktemp2(NULL, "bccs", NULL, &out);
1314 if (tfile == NULL) adios("bcc", "unable to create temporary file");
1315 strncpy (bccfil, tfile, sizeof(bccfil));
1317 fprintf (out, "From: %s\n", fullfrom);
1318 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1320 fprintf (out, "Message-ID: %s\n", message_id (tclock, 0));
1322 fprintf (out, "Subject: %s", subject);
1323 fprintf (out, "BCC:\n");
1326 * Use MIME encapsulation for Bcc messages
1332 * Check if any lines in the message clash with the
1333 * prefix for the MIME multipart separator. If there
1334 * is a clash, increment one of the letters in the
1335 * prefix and check again.
1337 if ((cp = strchr(prefix, 'a')) == NULL)
1338 adios (NULL, "lost prefix start");
1339 while (find_prefix () == NOTOK) {
1344 adios (NULL, "can't find a unique delimiter string");
1349 fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1350 VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1351 fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1353 fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1359 * Do mhl filtering of Bcc messages instead
1360 * of MIME encapsulation.
1362 if (filter != NULL) {
1363 vec[0] = r1bindex (mhlproc, '/');
1365 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1369 adios ("fork", "unable to");
1372 dup2 (fileno (out), 1);
1375 vec[i++] = "-forward";
1380 /* was the flag -[no]dashstuffing specified? */
1382 vec[i++] = "-dashstuffing";
1383 else if (dashstuff < 0)
1384 vec[i++] = "-nodashstuffing";
1387 execvp (mhlproc, vec);
1388 fprintf (stderr, "unable to exec ");
1393 pidXwait (child_id, mhlproc);
1397 if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1398 adios (tmpfil, "unable to re-open");
1401 * If using MIME encapsulation, or if the -nodashstuffing
1402 * flag was given, then just copy message. Else do
1403 * RFC934 quoting (dashstuffing).
1405 if (mime || dashstuff < 0)
1406 cpydata (fd, fileno (out), tmpfil, bccfil);
1408 cpydgst (fd, fileno (out), tmpfil, bccfil);
1412 fseek (out, 0L, SEEK_END);
1414 fprintf (out, "\n--%s--\n", prefix);
1416 fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1422 * Scan message to check if any lines clash with
1423 * the prefix of the MIME multipart separator.
1430 unsigned char buffer[BUFSIZ];
1433 if ((in = fopen (tmpfil, "r")) == NULL)
1434 adios (tmpfil, "unable to re-open");
1436 while (fgets (buffer, sizeof(buffer) - 1, in))
1437 if (buffer[0] == '-' && buffer[1] == '-') {
1440 for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1444 if (strcmp (buffer + 2, prefix) == 0) {
1455 #define plural(x) (x == 1 ? "" : "s")
1460 if (badadr && unkadr)
1461 die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1462 badadr, plural (badadr), unkadr, plural (badadr));
1464 die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1466 die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1471 do_addresses (int bccque, int talk)
1475 struct mailname *lp;
1478 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1479 if (lp->m_bcc ? bccque : !bccque) {
1481 printf (" -- Local Recipients --\n");
1482 do_an_address (lp, talk);
1487 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1488 if (lp->m_bcc ? bccque : !bccque) {
1490 printf (" -- UUCP Recipients --\n");
1491 do_an_address (lp, talk);
1496 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1497 if (lp->m_bcc ? bccque : !bccque) {
1499 printf (" -- Network Recipients --\n");
1500 do_an_address (lp, talk);
1506 if (rp_isbad (retval = sm_waend ()))
1507 die (NULL, "problem ending addresses; %s", rp_string (retval));
1512 * MTS-SPECIFIC INTERACTION
1517 * SENDMAIL/SMTP routines
1521 post (char *file, int bccque, int talk, char *envelope)
1528 if (msgflags & MINV)
1529 printf (" -- Posting for %s Recipients --\n",
1530 bccque ? "Blind" : "Sighted");
1532 printf (" -- Posting for All Recipients --\n");
1537 if (sm_mts == MTS_SENDMAIL_PIPE) {
1538 char *sargv[16], **argp;
1540 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1544 adios ("fork", "unable to");
1547 if (freopen( file, "r", stdin) == NULL) {
1548 adios (file, "can't reopen for sendmail");
1552 *argp++ = "sendmail";
1553 *argp++ = "-t"; /* read msg for recipients */
1554 *argp++ = "-i"; /* don't stop on "." */
1561 execv (sendmail, sargv);
1562 adios (sendmail, "can't exec");
1565 pidXwait (child_id, NULL);
1569 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1570 verbose, snoop, queued, sasl,
1571 saslssf, saslmech, user, tls)) ||
1572 rp_isbad (retval = sm_winit (envelope)))
1573 die (NULL, "problem initializing server; %s", rp_string (retval));
1575 do_addresses (bccque, talk && verbose);
1576 if ((fd = open (file, O_RDONLY)) == NOTOK)
1577 die (file, "unable to re-open");
1582 sm_end (!(msgflags & MINV) || bccque ? OK : DONE);
1586 if (msgflags & MINV)
1587 printf (" -- %s Recipient Copies Posted --\n",
1588 bccque ? "Blind" : "Sighted");
1590 printf (" -- Recipient Copies Posted --\n");
1598 /* Address Verification */
1601 verify_all_addresses (int talk, char *envelope)
1604 struct mailname *lp;
1608 if (!whomsw || checksw)
1609 if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1610 verbose, snoop, queued, sasl,
1611 saslssf, saslmech, user, tls))
1612 || rp_isbad (retval = sm_winit (envelope)))
1613 die (NULL, "problem initializing server; %s", rp_string (retval));
1615 if (talk && !whomsw)
1616 printf (" -- Address Verification --\n");
1617 if (talk && localaddrs.m_next)
1618 printf (" -- Local Recipients --\n");
1619 for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1620 do_an_address (lp, talk);
1622 if (talk && uuaddrs.m_next)
1623 printf (" -- UUCP Recipients --\n");
1624 for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1625 do_an_address (lp, talk);
1627 if (talk && netaddrs.m_next)
1628 printf (" -- Network Recipients --\n");
1629 for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1630 do_an_address (lp, talk);
1633 if (talk && !whomsw)
1634 printf (" -- Address Verification Successful --\n");
1636 if (!whomsw || checksw)
1645 do_an_address (struct mailname *lp, int talk)
1651 switch (lp->m_type) {
1655 strncpy (addr, mbox, sizeof(addr));
1659 mbox = auxformat (lp, 0);
1661 snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1664 default: /* let SendMail decide if the host is bad */
1667 snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1672 printf (" %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1674 if (whomsw && !checksw) {
1682 switch (retval = sm_wadr (mbox, host,
1683 lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1686 printf ("address ok\n");
1692 fprintf (stderr, " %s: ", addr);
1693 fprintf (talk ? stdout : stderr, "loses; %s\n",
1694 rp_string (retval));
1700 fprintf (stderr, " %s: ", addr);
1701 die (NULL, "unexpected response; %s", rp_string (retval));
1709 do_text (char *file, int fd)
1714 lseek (fd, (off_t) 0, SEEK_SET);
1716 while ((state = read (fd, buf, sizeof(buf))) > 0) {
1717 if (rp_isbad (retval = sm_wtxt (buf, state)))
1718 die (NULL, "problem writing text; %s\n", rp_string (retval));
1722 die (file, "problem reading from");
1724 switch (retval = sm_wtend ()) {
1730 die (NULL, "posting failed; %s", rp_string (retval));
1733 die (NULL, "unexpected response; %s", rp_string (retval));
1748 if (msgflags & MINV)
1751 if (!whomsw || checksw)
1764 hstat = SIGNAL2 (SIGHUP, sigser);
1765 istat = SIGNAL2 (SIGINT, sigser);
1766 qstat = SIGNAL2 (SIGQUIT, sigser);
1767 tstat = SIGNAL2 (SIGTERM, sigser);
1777 SIGNAL (SIGHUP, hstat);
1778 SIGNAL (SIGINT, istat);
1779 SIGNAL (SIGQUIT, qstat);
1780 SIGNAL (SIGTERM, tstat);
1788 p_refile (char *file)
1796 printf (" -- Filing Folder Copies --\n");
1797 for (i = 0; i < fccind; i++)
1798 fcc (file, fccfold[i]);
1800 printf (" -- Folder Copies Filed --\n");
1805 * Call the `fileproc' to add the file to the folder.
1809 fcc (char *file, char *folder)
1816 printf (" %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1819 for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1825 fprintf (stderr, " %sFcc %s: ",
1826 msgstate == RESENT ? "Resent-" : "", folder);
1827 fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1831 /* see if we need to add `+' */
1832 snprintf (fold, sizeof(fold), "%s%s",
1833 *folder == '+' || *folder == '@' ? "" : "+", folder);
1835 /* now exec the fileproc */
1836 execlp (fileproc, r1bindex (fileproc, '/'),
1837 "-link", "-file", file, fold, NULL);
1841 if ((status = pidwait (child_id, OK))) {
1843 fprintf (stderr, " %sFcc %s: ",
1844 msgstate == RESENT ? "Resent-" : "", folder);
1845 pidstatus (status, verbose ? stdout : stderr, NULL);
1848 printf ("folder ok\n");
1860 die (char *what, char *fmt, ...)
1865 if (msgflags & MINV)
1868 if (!whomsw || checksw)
1872 advertise (what, NULL, fmt, ap);