Require From: header in all outgoing messages.
[mmh] / uip / post.c
1
2 /*
3  * post.c -- enter messages into the mail transport system
4  *
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.
8  */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/signals.h>
13 #include <h/addrsbr.h>
14 #include <h/aliasbr.h>
15 #include <h/dropsbr.h>
16 #include <h/mime.h>
17 #include <h/utils.h>
18
19 #include <h/tws.h>
20 #include <h/mts.h>
21
22 #include <errno.h>
23 #include <signal.h>
24
25 #ifdef HAVE_SYS_TIME_H
26 # include <sys/time.h>
27 #endif
28 #include <time.h>
29
30 #include <mts/smtp/smtp.h>
31
32 #ifndef CYRUS_SASL
33 # define SASLminc(a) (a)
34 #else /* CYRUS_SASL */
35 # define SASLminc(a)  0
36 #endif /* CYRUS_SASL */
37
38 #ifndef TLS_SUPPORT
39 # define TLSminc(a)  (a)
40 #else /* TLS_SUPPORT */
41 # define TLSminc(a)   0
42 #endif /* TLS_SUPPORT */
43
44 #define FCCS            10      /* max number of fccs allowed */
45
46 #define uptolow(c)      ((isalpha(c) && isupper (c)) ? tolower (c) : c)
47
48 /* In the following array of structures, the numeric second field of the
49    structures (minchars) is apparently used like this:
50
51    -# : Switch can be abbreviated to # characters; switch hidden in -help.
52    0  : Switch can't be abbreviated;               switch shown in -help.
53    #  : Switch can be abbreviated to # characters; switch shown in -help. */
54
55 static struct swit switches[] = {
56 #define ALIASW                    0
57     { "alias aliasfile", 0 },
58 #define CHKSW                     1
59     { "check", -5 },                    /* interface from whom */
60 #define NCHKSW                    2
61     { "nocheck", -7 },                  /* interface from whom */
62 #define DEBUGSW                   3
63     { "debug", -5 },
64 #define DISTSW                    4
65     { "dist", -4 },                     /* interface from dist */
66 #define FILTSW                    5
67     { "filter filterfile", 0 },
68 #define NFILTSW                   6
69     { "nofilter", 0 },
70 #define FRMTSW                    7
71     { "format", 0 },
72 #define NFRMTSW                   8
73     { "noformat", 0 },
74 #define LIBSW                     9
75     { "library directory", -7 },        /* interface from send, whom */
76 #define MIMESW                   10
77     { "mime", 0 },
78 #define NMIMESW                  11
79     { "nomime", 0 },
80 #define MSGDSW                   12
81     { "msgid", 0 },
82 #define NMSGDSW                  13
83     { "nomsgid", 0 },
84 #define VERBSW                   14
85     { "verbose", 0 },
86 #define NVERBSW                  15
87     { "noverbose", 0 },
88 #define WATCSW                   16
89     { "watch", 0 },
90 #define NWATCSW                  17
91     { "nowatch", 0 },
92 #define WHOMSW                   18
93     { "whom", -4 },                     /* interface from whom */
94 #define WIDTHSW                  19
95     { "width columns", 0 },
96 #define VERSIONSW                20
97     { "version", 0 },
98 #define HELPSW                   21
99     { "help", 0 },
100 #define BITSTUFFSW               22
101     { "dashstuffing", -12 },            /* should we dashstuff BCC messages? */
102 #define NBITSTUFFSW              23
103     { "nodashstuffing", -14 },
104 #define ANNOSW                   24
105     { "idanno number", -6 },            /* interface from send    */
106 #define CLIESW                   25
107     { "client host", -6 },
108 #define SERVSW                   26
109     { "server host", 6 },               /* specify alternate SMTP server */
110 #define SNOOPSW                  27
111     { "snoop", -5 },                    /* snoop the SMTP transaction */
112 #define PARTSW                   28
113     { "partno", -6 },
114 #define QUEUESW                  29
115     { "queued", -6 },
116 #define SASLSW                   30
117     { "sasl", SASLminc(-4) },
118 #define NOSASLSW                 31
119     { "nosasl", SASLminc(-6) },
120 #define SASLMXSSFSW              32
121     { "saslmaxssf", SASLminc(-10) },
122 #define SASLMECHSW               33
123     { "saslmech", SASLminc(-5) },
124 #define USERSW                   34
125     { "user", SASLminc(-4) },
126 #define PORTSW                   35
127     { "port server port name/number", 4 },
128 #define TLSSW                    36
129     { "tls", TLSminc(-3) },
130 #define NTLSSW                   37
131     { "notls", TLSminc(-5) },
132 #define FILEPROCSW               38
133     { "fileproc", -4 },
134 #define MHLPROCSW                39
135     { "mhlproc", -3 },
136     { NULL, 0 }
137 };
138
139
140 struct headers {
141     char *value;
142     unsigned int flags;
143     unsigned int set;
144 };
145
146 /*
147  * flags for headers->flags
148  */
149 #define HNOP  0x0000            /* just used to keep .set around          */
150 #define HBAD  0x0001            /* bad header - don't let it through      */
151 #define HADR  0x0002            /* header has an address field            */
152 #define HSUB  0x0004            /* Subject: header                        */
153 #define HTRY  0x0008            /* try to send to addrs on header         */
154 #define HBCC  0x0010            /* don't output this header               */
155 #define HMNG  0x0020            /* munge this header                      */
156 #define HNGR  0x0040            /* no groups allowed in this header       */
157 #define HFCC  0x0080            /* FCC: type header                       */
158 #define HNIL  0x0100            /* okay for this header not to have addrs */
159 #define HIGN  0x0200            /* ignore this header                     */
160 #define HDCC  0x0400            /* another undocumented feature           */
161 #define HONE  0x0800            /* Only one address allowed               */
162 #define HEFM  0x1000            /* Envelope-From: header                  */
163
164 /*
165  * flags for headers->set
166  */
167 #define MFRM  0x0001            /* we've seen a From:        */
168 #define MDAT  0x0002            /* we've seen a Date:        */
169 #define MRFM  0x0004            /* we've seen a Resent-From: */
170 #define MVIS  0x0008            /* we've seen sighted addrs  */
171 #define MINV  0x0010            /* we've seen blind addrs    */
172 #define MSND  0x0020            /* we've seen a Sender:      */
173 #define MRSN  0x0040            /* We've seen a Resent-Sendr:*/
174 #define MEFM  0x0080            /* We've seen Envelope-From: */
175
176
177 static struct headers NHeaders[] = {
178     { "Return-Path",   HBAD,                0 },
179     { "Received",      HBAD,                0 },
180     { "Reply-To",      HADR|HNGR,           0 },
181     { "From",          HADR|HNGR,           MFRM },
182     { "Sender",        HADR|HNGR|HONE,      MSND },
183     { "Date",          HBAD,                0 },
184     { "Subject",       HSUB,                0 },
185     { "To",            HADR|HTRY,           MVIS },
186     { "cc",            HADR|HTRY,           MVIS },
187     { "Bcc",           HADR|HTRY|HBCC|HNIL, MINV },
188     { "Dcc",           HADR|HTRY|HDCC|HNIL, MVIS },     /* sorta cc & bcc combined */
189     { "Message-ID",    HBAD,                0 },
190     { "Fcc",           HFCC,                0 },
191     { "Envelope-From", HADR|HONE|HEFM,      MEFM },
192     { NULL,            0,                   0 }
193 };
194
195 static struct headers RHeaders[] = {
196     { "Resent-Reply-To",   HADR|HNGR,           0 },
197     { "Resent-From",       HADR|HNGR,           MRFM },
198     { "Resent-Sender",     HADR|HNGR,           MRSN },
199     { "Resent-Date",       HBAD,                0 },
200     { "Resent-Subject",    HSUB,                0 },
201     { "Resent-To",         HADR|HTRY,           MVIS },
202     { "Resent-cc",         HADR|HTRY,           MVIS },
203     { "Resent-Bcc",        HADR|HTRY|HBCC,      MINV },
204     { "Resent-Message-ID", HBAD,                0 },
205     { "Resent-Fcc",        HFCC,                0 },
206     { "Reply-To",          HADR,                0 },
207     { "From",              HADR|HNGR,           MFRM },
208     { "Sender",            HADR|HNGR,           MSND },
209     { "Date",              HNOP,                MDAT },
210     { "To",                HADR|HNIL,           0 },
211     { "cc",                HADR|HNIL,           0 },
212     { "Bcc",               HADR|HTRY|HBCC|HNIL, 0 },
213     { "Fcc",               HIGN,                0 },
214     { "Envelope-From",     HADR|HONE|HEFM,      MEFM },
215     { NULL,                0,                   0 }
216 };
217
218 static short fccind = 0;        /* index into fccfold[] */
219 static short outputlinelen = OUTPUTLINELEN;
220
221 static int pfd = NOTOK;         /* fd to write annotation list to        */
222 static uid_t myuid= -1;         /* my user id                            */
223 static gid_t mygid= -1;         /* my group id                           */
224 static int recipients = 0;      /* how many people will get a copy       */
225 static int unkadr = 0;          /* how many of those were unknown        */
226 static int badadr = 0;          /* number of bad addrs                   */
227 static int badmsg = 0;          /* message has bad semantics             */
228 static int verbose = 0;         /* spell it out                          */
229 static int format = 1;          /* format addresses                      */
230 static int mime = 0;            /* use MIME-style encapsulations for Bcc */
231 static int msgid = 0;           /* add msgid                             */
232 static int debug = 0;           /* debugging post                        */
233 static int watch = 0;           /* watch the delivery process            */
234 static int whomsw = 0;          /* we are whom not post                  */
235 static int checksw = 0;         /* whom -check                           */
236 static int linepos=0;           /* putadr()'s position on the line       */
237 static int nameoutput=0;        /* putadr() has output header name       */
238 static int sasl=0;              /* Use SASL auth for SMTP                */
239 static int saslssf=-1;          /* Our maximum SSF for SASL              */
240 static char *saslmech=NULL;     /* Force use of particular SASL mech     */
241 static char *user=NULL;         /* Authenticate as this user             */
242 static char *port="smtp";       /* Name of server port for SMTP          */
243 static int tls=0;               /* Use TLS for encryption                */
244 static int fromcount=0;         /* Count of addresses on From: header    */
245 static int seensender=0;        /* Have we seen a Sender: header?        */
246
247 static unsigned msgflags = 0;   /* what we've seen */
248
249 #define NORMAL 0
250 #define RESENT 1
251 static int msgstate = NORMAL;
252
253 static time_t tclock = 0;       /* the time we started (more or less) */
254
255 static SIGNAL_HANDLER hstat, istat, qstat, tstat;
256
257 static char tmpfil[BUFSIZ];
258 static char bccfil[BUFSIZ];
259
260 static char from[BUFSIZ];       /* my network address            */
261 static char sender[BUFSIZ];     /* my Sender: header             */
262 static char efrom[BUFSIZ];      /* my Envelope-From: header      */
263 static char signature[BUFSIZ];  /* my signature                  */
264 static char *filter = NULL;     /* the filter for BCC'ing        */
265 static char *subject = NULL;    /* the subject field for BCC'ing */
266 static char *fccfold[FCCS];     /* foldernames for FCC'ing       */
267
268 static struct headers  *hdrtab; /* table for the message we're doing */
269
270 static struct mailname localaddrs;              /* local addrs     */
271 static struct mailname netaddrs;                /* network addrs   */
272 static struct mailname uuaddrs;                 /* uucp addrs      */
273 static struct mailname tmpaddrs;                /* temporary queue */
274
275 static int snoop      = 0;
276 static char *clientsw = NULL;
277 static char *serversw = NULL;
278
279 extern struct smtp sm_reply;
280
281 static char prefix[] = "----- =_aaaaaaaaaa";
282
283 static char *partno = NULL;
284 static int queued = 0;
285
286 /*
287  * static prototypes
288  */
289 static void putfmt (char *, char *, FILE *);
290 static void start_headers (void);
291 static void finish_headers (FILE *);
292 static int get_header (char *, struct headers *);
293 static int putadr (char *, char *, struct mailname *, FILE *, unsigned int);
294 static void putgrp (char *, char *, FILE *, unsigned int);
295 static int insert (struct mailname *);
296 static void pl (void);
297 static void anno (void);
298 static int annoaux (struct mailname *);
299 static void insert_fcc (struct headers *, unsigned char *);
300 static void make_bcc_file (int);
301 static void verify_all_addresses (int, char *);
302 static void chkadr (void);
303 static void sigon (void);
304 static void sigoff (void);
305 static void p_refile (char *);
306 static void fcc (char *, char *);
307 static void die (char *, char *, ...);
308 static void post (char *, int, int, char *);
309 static void do_text (char *file, int fd);
310 static void do_an_address (struct mailname *, int);
311 static void do_addresses (int, int);
312 static int find_prefix (void);
313
314
315 int
316 main (int argc, char **argv)
317 {
318     int state, compnum, dashstuff = 0;
319     char *cp, *msg = NULL, **argp, **arguments, *envelope;
320     char buf[BUFSIZ], name[NAMESZ];
321     FILE *in, *out;
322
323 #ifdef LOCALE
324     setlocale(LC_ALL, "");
325 #endif
326     invo_name = r1bindex (argv[0], '/');
327
328     /* foil search of user profile/context */
329     if (context_foil (NULL) == -1)
330         done (1);
331
332     mts_init (invo_name);
333     arguments = getarguments (invo_name, argc, argv, 0);
334     argp = arguments;
335
336     while ((cp = *argp++)) {
337         if (*cp == '-') {
338             switch (smatch (++cp, switches)) {
339                 case AMBIGSW: 
340                     ambigsw (cp, switches);
341                     done (1);
342                 case UNKWNSW: 
343                     adios (NULL, "-%s unknown", cp);
344
345                 case HELPSW: 
346                     snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
347                     print_help (buf, switches, 0);
348                     done (1);
349                 case VERSIONSW:
350                     print_version(invo_name);
351                     done (1);
352
353                 case LIBSW:
354                     if (!(cp = *argp++) || *cp == '-')
355                         adios (NULL, "missing argument to %s", argp[-2]);
356                     /* create a minimal context */
357                     if (context_foil (cp) == -1)
358                         done (1);
359                     continue;
360
361                 case ALIASW: 
362                     if (!(cp = *argp++) || *cp == '-')
363                         adios (NULL, "missing argument to %s", argp[-2]);
364                     if ((state = alias (cp)) != AK_OK)
365                         adios (NULL, "aliasing error in %s - %s",
366                                 cp, akerror (state));
367                     continue;
368
369                 case CHKSW: 
370                     checksw++;
371                     continue;
372                 case NCHKSW: 
373                     checksw = 0;
374                     continue;
375
376                 case DEBUGSW: 
377                     debug++;
378                     continue;
379
380                 case DISTSW:
381                     msgstate = RESENT;
382                     continue;
383
384                 case FILTSW:
385                     if (!(filter = *argp++) || *filter == '-')
386                         adios (NULL, "missing argument to %s", argp[-2]);
387                     mime = 0;
388                     continue;
389                 case NFILTSW:
390                     filter = NULL;
391                     continue;
392                 
393                 case FRMTSW: 
394                     format++;
395                     continue;
396                 case NFRMTSW: 
397                     format = 0;
398                     continue;
399
400                 case BITSTUFFSW:
401                     dashstuff = 1;
402                     continue;
403                 case NBITSTUFFSW:
404                     dashstuff = -1;
405                     continue;
406
407                 case MIMESW:
408                     mime++;
409                     filter = NULL;
410                     continue;
411                 case NMIMESW: 
412                     mime = 0;
413                     continue;
414
415                 case MSGDSW: 
416                     msgid++;
417                     continue;
418                 case NMSGDSW: 
419                     msgid = 0;
420                     continue;
421
422                 case VERBSW: 
423                     verbose++;
424                     continue;
425                 case NVERBSW: 
426                     verbose = 0;
427                     continue;
428
429                 case WATCSW: 
430                     watch++;
431                     continue;
432                 case NWATCSW: 
433                     watch = 0;
434                     continue;
435
436                 case WHOMSW: 
437                     whomsw++;
438                     continue;
439
440                 case WIDTHSW: 
441                     if (!(cp = *argp++) || *cp == '-')
442                         adios (NULL, "missing argument to %s", argp[-2]);
443                     if ((outputlinelen = atoi (cp)) < 10)
444                         adios (NULL, "impossible width %d", outputlinelen);
445                     continue;
446
447                 case ANNOSW: 
448                     if (!(cp = *argp++) || *cp == '-')
449                         adios (NULL, "missing argument to %s", argp[-2]);
450                     if ((pfd = atoi (cp)) <= 2)
451                         adios (NULL, "bad argument %s %s", argp[-2], cp);
452                     continue;
453
454                 case CLIESW:
455                     if (!(clientsw = *argp++) || *clientsw == '-')
456                         adios (NULL, "missing argument to %s", argp[-2]);
457                     continue;
458                 case SERVSW:
459                     if (!(serversw = *argp++) || *serversw == '-')
460                         adios (NULL, "missing argument to %s", argp[-2]);
461                     continue;
462                 case SNOOPSW:
463                     snoop++;
464                     continue;
465
466                 case PARTSW:
467                     if (!(partno = *argp++) || *partno == '-')
468                         adios (NULL, "missing argument to %s", argp[-2]);
469                     continue;
470
471                 case QUEUESW:
472                     queued++;
473                     continue;
474                 
475                 case SASLSW:
476                     sasl++;
477                     continue;
478
479                 case NOSASLSW:
480                     sasl = 0;
481                     continue;
482
483                 case SASLMXSSFSW:
484                     if (!(cp = *argp++) || *cp == '-')
485                         adios (NULL, "missing argument to %s", argp[-2]);
486                     saslssf = atoi(cp);
487                     continue;
488                 
489                 case SASLMECHSW:
490                     if (!(saslmech = *argp++) || *saslmech == '-')
491                         adios (NULL, "missing argument to %s", argp[-2]);
492                     continue;
493                 
494                 case USERSW:
495                     if (!(user = *argp++) || *user == '-')
496                         adios (NULL, "missing argument to %s", argp[-2]);
497                     continue;
498
499                 case PORTSW:
500                     if (!(port = *argp++) || *port == '-')
501                         adios (NULL, "missing argument to %s", argp[-2]);
502                     continue;
503
504                 case TLSSW:
505                     tls++;
506                     continue;
507
508                 case NTLSSW:
509                     tls = 0;
510                     continue;
511
512                 case FILEPROCSW:
513                     if (!(cp = *argp++) || *cp == '-')
514                         adios (NULL, "missing argument to %s", argp[-2]);
515                     fileproc = cp;
516                     continue;
517
518                 case MHLPROCSW:
519                     if (!(cp = *argp++) || *cp == '-')
520                         adios (NULL, "missing argument to %s", argp[-2]);
521                     mhlproc = cp;
522                     continue;
523             }
524         }
525         if (msg)
526             adios (NULL, "only one message at a time!");
527         else
528             msg = cp;
529     }
530
531     alias (AliasFile);
532
533     if (!msg)
534         adios (NULL, "usage: %s [switches] file", invo_name);
535
536     if (outputlinelen < 10)
537         adios (NULL, "impossible width %d", outputlinelen);
538
539     if ((in = fopen (msg, "r")) == NULL)
540         adios (msg, "unable to open");
541
542     start_headers ();
543     if (debug) {
544         verbose++;
545         discard (out = stdout); /* XXX: reference discard() to help loader */
546     } else {
547         if (whomsw) {
548             if ((out = fopen ("/dev/null", "w")) == NULL)
549                 adios ("/dev/null", "unable to open");
550         } else {
551             char *cp = m_mktemp(m_maildir(invo_name), NULL, &out);
552             if (cp == NULL) {
553                 cp = m_mktemp2(NULL, invo_name, NULL, &out);
554                 if (cp == NULL) {
555                     adios ("post", "unable to create temporary file");
556                 }
557             }
558             strncpy(tmpfil, cp, sizeof(tmpfil));
559             chmod (tmpfil, 0600);
560         }
561     }
562
563     hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
564
565     for (compnum = 1, state = FLD;;) {
566         switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
567             case FLD: 
568             case FLDEOF: 
569             case FLDPLUS: 
570                 compnum++;
571                 cp = add (buf, NULL);
572                 while (state == FLDPLUS) {
573                     state = m_getfld (state, name, buf, sizeof(buf), in);
574                     cp = add (buf, cp);
575                 }
576                 putfmt (name, cp, out);
577                 free (cp);
578                 if (state != FLDEOF)
579                     continue;
580                 finish_headers (out);
581                 break;
582
583             case BODY: 
584             case BODYEOF: 
585                 finish_headers (out);
586                 if (whomsw)
587                     break;
588                 fprintf (out, "\n%s", buf);
589                 while (state == BODY) {
590                     state = m_getfld (state, name, buf, sizeof(buf), in);
591                     fputs (buf, out);
592                 }
593                 break;
594
595             case FILEEOF: 
596                 finish_headers (out);
597                 break;
598
599             case LENERR: 
600             case FMTERR: 
601                 adios (NULL, "message format error in component #%d", compnum);
602
603             default: 
604                 adios (NULL, "getfld() returned %d", state);
605         }
606         break;
607     }
608
609     if (pfd != NOTOK)
610         anno ();
611     fclose (in);
612
613     if (debug) {
614         pl ();
615         done (0);
616     } else {
617         fclose (out);
618     }
619
620     /*
621      * Here's how we decide which address to use as the envelope-from
622      * address for SMTP.
623      *
624      * - If we were given an Envelope-From header, use that.
625      * - If we were given multiple addresses in the From: header, use
626      *   the Sender: address
627      * - Otherwise, use the address on the From: line
628      */
629
630     if (msgflags & MEFM) {
631         envelope = efrom;
632     } else if (fromcount > 1) {
633         envelope = sender;
634     } else {
635         envelope = from;
636     }
637
638     /* If we are doing a "whom" check */
639     if (whomsw) {
640         verify_all_addresses (1, envelope);
641         done (0);
642     }
643
644     if (msgflags & MINV) {
645         make_bcc_file (dashstuff);
646         if (msgflags & MVIS) {
647             verify_all_addresses (verbose, envelope);
648             post (tmpfil, 0, verbose, envelope);
649         }
650         post (bccfil, 1, verbose, envelope);
651         unlink (bccfil);
652     } else {
653         post (tmpfil, 0, isatty (1), envelope);
654     }
655
656     p_refile (tmpfil);
657     unlink (tmpfil);
658
659     if (verbose)
660         printf (partno ? "Partial Message #%s Processed\n" : "Message Processed\n",
661                 partno);
662     done (0);
663     return 1;
664 }
665
666
667 /*
668  * DRAFT GENERATION
669  */
670
671 static void
672 putfmt (char *name, char *str, FILE *out)
673 {
674     int count, grp, i, keep;
675     char *cp, *pp, *qp;
676     char namep[BUFSIZ];
677     struct mailname *mp = NULL, *np = NULL;
678     struct headers *hdr;
679
680     while (*str == ' ' || *str == '\t')
681         str++;
682
683     if (msgstate == NORMAL && uprf (name, "resent")) {
684         advise (NULL, "illegal header line -- %s:", name);
685         badmsg++;
686         return;
687     }
688
689     if ((i = get_header (name, hdrtab)) == NOTOK) {
690         fprintf (out, "%s: %s", name, str);
691         return;
692     }
693
694     hdr = &hdrtab[i];
695     if (hdr->flags & HIGN) {
696         return;
697     }
698     if (hdr->flags & HBAD) {
699         advise (NULL, "illegal header line -- %s:", name);
700         badmsg++;
701         return;
702     }
703     msgflags |= (hdr->set & ~(MVIS | MINV));
704
705     if (hdr->flags & HSUB)
706         subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
707     if (hdr->flags & HFCC) {
708         if ((cp = strrchr(str, '\n')))
709             *cp = 0;
710         for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
711             *cp++ = 0;
712             insert_fcc (hdr, pp);
713         }
714         insert_fcc (hdr, pp);
715         return;
716     }
717
718     if (!(hdr->flags & HADR)) {
719         fprintf (out, "%s: %s", name, str);
720         return;
721     }
722
723     tmpaddrs.m_next = NULL;
724     for (count = 0; (cp = getname (str)); count++)
725         if ((mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
726             if (tmpaddrs.m_next)
727                 np->m_next = mp;
728             else
729                 tmpaddrs.m_next = mp;
730             np = mp;
731         }
732         else
733             if (hdr->flags & HTRY)
734                 badadr++;
735             else
736                 badmsg++;
737
738     if (count < 1) {
739         if (hdr->flags & HNIL)
740             fprintf (out, "%s: %s", name, str);
741         else {
742 #ifdef notdef
743             advise (NULL, "%s: field requires at least one address", name);
744             badmsg++;
745 #endif /* notdef */
746         }
747         return;
748     }
749
750     if (count > 1 && (hdr->flags & HONE)) {
751         advise (NULL, "%s: field only permits one address", name);
752         badmsg++;
753         return;
754     }
755
756     nameoutput = linepos = 0;
757     snprintf (namep, sizeof(namep), "%s%s",
758                 (hdr->flags & HMNG) ? "Original-" : "", name);
759
760     for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
761         if (mp->m_nohost) {     /* also used to test (hdr->flags & HTRY) */
762             /* The address doesn't include a host, so it might be an alias. */
763             pp = akvalue (mp->m_mbox);  /* do mh alias substitution */
764             qp = akvisible () ? mp->m_mbox : "";
765             np = mp;
766             if (np->m_gname)
767                 putgrp (namep, np->m_gname, out, hdr->flags);
768             while ((cp = getname (pp))) {
769                 if (!(mp = getm (cp, NULL, 0, AD_HOST, NULL))) {
770                     badadr++;
771                     continue;
772                 }
773
774                 /*
775                  * If it's a From: or Resent-From: header, save the address
776                  * for later possible use (as the envelope address for SMTP)
777                  */
778
779                 if ((msgstate == RESENT) ? (hdr->set & MRFM)
780                                                 : (hdr->set & MFRM)) {
781                     strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
782                     from[sizeof(from) - 1] = '\0';
783                     fromcount = count;
784                 }
785
786                 /*
787                  * Also save the Sender: or Resent-Sender: header as well
788                  */
789
790                 if ((msgstate == RESENT) ? (hdr->set & MRSN)
791                                                 : (hdr->set & MSND)) {
792                     strncpy(sender, auxformat(mp, 0), sizeof(sender) - 1);
793                     sender[sizeof(sender) - 1] = '\0';
794                     seensender++;
795                 }
796
797                 /*
798                  * ALSO ... save Envelope-From
799                  */
800
801                 if (hdr->set & MEFM) {
802                     strncpy(efrom, auxformat(mp, 0), sizeof(efrom) - 1);
803                     efrom[sizeof(efrom) - 1] = '\0';
804                 }
805
806                 if (hdr->flags & HBCC)
807                     mp->m_bcc++;
808                 if (np->m_ingrp)
809                     mp->m_ingrp = np->m_ingrp;
810                 else
811                     if (mp->m_gname)
812                         putgrp (namep, mp->m_gname, out, hdr->flags);
813                 if (mp->m_ingrp)
814                     grp++;
815                 if (putadr (namep, qp, mp, out, hdr->flags))
816                     msgflags |= (hdr->set & (MVIS | MINV));
817                 else
818                     mnfree (mp);
819             }
820             mp = np;
821             np = np->m_next;
822             mnfree (mp);
823         }
824         else {
825             /* Address includes a host, so no alias substitution is needed. */
826
827             /*
828              * If it's a From: or Resent-From header, save the address
829              * for later possible use (as the envelope address for SMTP)
830              */
831
832             if ((msgstate == RESENT) ? (hdr->set & MRFM)
833                                             : (hdr->set & MFRM)) {
834                 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
835                 fromcount = count;
836             }
837
838             /*
839              * Also save the Sender: header as well
840              */
841
842             if ((msgstate == RESENT) ? (hdr->set & MRSN)
843                                             : (hdr->set & MSND)) {
844                 strncpy(sender, auxformat(mp, 0), sizeof(sender) - 1);
845                 sender[sizeof(sender) - 1] = '\0';
846                 seensender++;
847             }
848
849             /*
850              * ALSO ... save Envelope-From
851              */
852
853             if (hdr->set & MEFM) {
854                 strncpy(efrom, auxformat(mp, 0), sizeof(efrom) - 1);
855                 efrom[sizeof(efrom) - 1] = '\0';
856             }
857
858             if (hdr->flags & HBCC)
859                 mp->m_bcc++;
860             if (mp->m_gname)
861                 putgrp (namep, mp->m_gname, out, hdr->flags);
862             if (mp->m_ingrp)
863                 grp++;
864             keep = putadr (namep, "", mp, out, hdr->flags);
865             np = mp->m_next;
866             if (keep) {
867                 mp->m_next = NULL;
868                 msgflags |= (hdr->set & (MVIS | MINV));
869             }
870             else
871                 mnfree (mp);
872         }
873
874     if (grp > 0 && (hdr->flags & HNGR)) {
875         advise (NULL, "%s: field does not allow groups", name);
876         badmsg++;
877     }
878     if (linepos) {
879         putc ('\n', out);
880     }
881 }
882
883
884 static void
885 start_headers (void)
886 {
887     unsigned char  *cp;
888     char myhost[BUFSIZ], sigbuf[BUFSIZ];
889     struct mailname *mp;
890
891     myuid = getuid ();
892     mygid = getgid ();
893     time (&tclock);
894
895     strncpy (from, getlocaladdr(), sizeof(from));
896     strncpy (myhost, LocalName (0), sizeof(myhost));
897
898     for (cp = myhost; *cp; cp++)
899         *cp = uptolow (*cp);
900
901     if ((cp = getfullname ()) && *cp) {
902         strncpy (sigbuf, cp, sizeof(sigbuf));
903         snprintf (signature, sizeof(signature), "%s <%s>",
904                 sigbuf, getlocaladdr());
905         if ((cp = getname (signature)) == NULL)
906             adios (NULL, "getname () failed -- you lose extraordinarily big");
907         if ((mp = getm (cp, NULL, 0, AD_HOST, NULL)) == NULL)
908             adios (NULL, "bad signature '%s'", sigbuf);
909         mnfree (mp);
910         while (getname (""))
911             continue;
912     } else {
913         strncpy (signature, getlocaladdr(), sizeof(signature));
914     }
915 }
916
917
918 /*
919  * Now that we've outputted the header fields in the draft
920  * message, we will now output any remaining header fields
921  * that we need to add/create.
922  */
923
924 static void
925 finish_headers (FILE *out)
926 {
927     switch (msgstate) {
928         case NORMAL: 
929             if (!(msgflags & MFRM)) {
930                 /*
931                  * A From: header is now required in the draft.
932                  */
933                 advise (NULL, "message has no From: header");
934                 advise (NULL, "See default components files for examples");
935                 badmsg++;
936             }
937
938             if (fromcount > 1 && seensender == 0) {
939                 advise (NULL, "A Sender: header is required with multiple "
940                         "From: addresses");
941                 badmsg++;
942             }
943
944             if (whomsw)
945                 break;
946
947             fprintf (out, "Date: %s\n", dtime (&tclock, 0));
948             if (msgid)
949                 fprintf (out, "Message-ID: <%d.%ld@%s>\n",
950                         (int) getpid (), (long) tclock, LocalName (1));
951             if (!(msgflags & MVIS))
952                 fprintf (out, "Bcc: Blind Distribution List: ;\n");
953             break;
954
955         case RESENT: 
956             if (!(msgflags & MDAT)) {
957                 advise (NULL, "message has no Date: header");
958                 badmsg++;
959             }
960             if (!(msgflags & MFRM)) {
961                 advise (NULL, "message has no From: header");
962                 badmsg++;
963             }
964             if (!(msgflags & MRFM)) {
965                 advise (NULL, "message has no Resent-From: header");
966                 advise (NULL, "See default components files for examples");
967                 badmsg++;
968             }
969             if (fromcount > 1 && seensender == 0) {
970                 advise (NULL, "A Resent-Sender: header is required with "
971                         "multiple Resent-From: addresses");
972                 badmsg++;
973             }
974
975             if (whomsw)
976                 break;
977
978             fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
979             if (msgid)
980                 fprintf (out, "Resent-Message-ID: <%d.%ld@%s>\n",
981                         (int) getpid (), (long) tclock, LocalName (1));
982             if (!(msgflags & MVIS))
983                 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
984             break;
985     }
986
987     if (badmsg)
988         adios (NULL, "re-format message and try again");
989     if (!recipients)
990         adios (NULL, "no addressees");
991 }
992
993
994 static int
995 get_header (char *header, struct headers *table)
996 {
997     struct headers *h;
998
999     for (h = table; h->value; h++)
1000         if (!mh_strcasecmp (header, h->value))
1001             return (h - table);
1002
1003     return NOTOK;
1004 }
1005
1006
1007 static int
1008 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
1009 {
1010     int len;
1011     char *cp;
1012     char buffer[BUFSIZ];
1013
1014     if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
1015         return 0;
1016     if ((flags & (HBCC | HDCC | HEFM)) || mp->m_ingrp)
1017         return 1;
1018
1019     if (!nameoutput) {
1020         fprintf (out, "%s: ", name);
1021         linepos += (nameoutput = strlen (name) + 2);
1022     }
1023
1024     if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
1025         mp->m_pers = getcpy (aka);
1026     if (format) {
1027         if (mp->m_gname) {
1028             snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
1029             cp = buffer;
1030         } else {
1031             cp = adrformat (mp);
1032         }
1033     } else {
1034         cp = mp->m_text;
1035     }
1036     len = strlen (cp);
1037
1038     if (linepos != nameoutput) {
1039         if (len + linepos + 2 > outputlinelen)
1040             fprintf (out, ",\n%*s", linepos = nameoutput, "");
1041         else {
1042             fputs (", ", out);
1043             linepos += 2;
1044         }
1045     }
1046
1047     fputs (cp, out);
1048     linepos += len;
1049
1050     return (flags & HTRY);
1051 }
1052
1053
1054 static void
1055 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1056 {
1057     int len;
1058     char *cp;
1059
1060     if (flags & HBCC)
1061         return;
1062
1063     if (!nameoutput) {
1064         fprintf (out, "%s: ", name);
1065         linepos += (nameoutput = strlen (name) + 2);
1066     }
1067
1068     cp = concat (group, ";", NULL);
1069     len = strlen (cp);
1070
1071     if (linepos > nameoutput) {
1072         if (len + linepos + 2 > outputlinelen) {
1073             fprintf (out, ",\n%*s", nameoutput, "");
1074             linepos = nameoutput;
1075         }
1076         else {
1077             fputs (", ", out);
1078             linepos += 2;
1079         }
1080     }
1081
1082     fputs (cp, out);
1083     linepos += len;
1084 }
1085
1086
1087 static int
1088 insert (struct mailname *np)
1089 {
1090     struct mailname *mp;
1091
1092     if (np->m_mbox == NULL)
1093         return 0;
1094
1095     for (mp = np->m_type == LOCALHOST ? &localaddrs
1096             : np->m_type == UUCPHOST ? &uuaddrs
1097             : &netaddrs;
1098             mp->m_next;
1099             mp = mp->m_next)
1100         if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
1101                 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox)
1102                 && np->m_bcc == mp->m_next->m_bcc)
1103             return 0;
1104
1105     mp->m_next = np;
1106     recipients++;
1107     return 1;
1108 }
1109
1110
1111 static void
1112 pl (void)
1113 {
1114     int i;
1115     struct mailname *mp;
1116
1117     printf ("-------\n\t-- Addresses --\nlocal:\t");
1118     for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1119         printf ("%s%s%s", mp->m_mbox,
1120                 mp->m_bcc ? "[BCC]" : "",
1121                 mp->m_next ? ",\n\t" : "");
1122
1123     printf ("\nnet:\t");
1124     for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1125         printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1126                 mp->m_mbox, mp->m_host,
1127                 mp->m_bcc ? "[BCC]" : "",
1128                 mp->m_next ? ",\n\t" : "");
1129
1130     printf ("\nuucp:\t");
1131     for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1132         printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1133                 mp->m_bcc ? "[BCC]" : "",
1134                 mp->m_next ? ",\n\t" : "");
1135
1136     printf ("\n\t-- Folder Copies --\nfcc:\t");
1137     for (i = 0; i < fccind; i++)
1138         printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1139     printf ("\n");
1140 }
1141
1142
1143 static void
1144 anno (void)
1145 {
1146     struct mailname *mp;
1147
1148     for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1149         if (annoaux (mp) == NOTOK)
1150             goto oops;
1151
1152     for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1153         if (annoaux (mp) == NOTOK)
1154             goto oops;
1155
1156     for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1157         if (annoaux (mp) == NOTOK)
1158             break;
1159
1160 oops: ;
1161     close (pfd);
1162     pfd = NOTOK;
1163 }
1164
1165
1166 static int
1167 annoaux (struct mailname *mp)
1168 {
1169     int i;
1170     char buffer[BUFSIZ];
1171
1172     snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1173     i = strlen (buffer);
1174
1175     return (write (pfd, buffer, i) == i ? OK : NOTOK);
1176 }
1177
1178
1179 static void
1180 insert_fcc (struct headers *hdr, unsigned char *pp)
1181 {
1182     unsigned char *cp;
1183
1184     for (cp = pp; isspace (*cp); cp++)
1185         continue;
1186     for (pp += strlen (pp) - 1; pp > cp && isspace (*pp); pp--)
1187         continue;
1188     if (pp >= cp)
1189         *++pp = 0;
1190     if (*cp == 0)
1191         return;
1192
1193     if (fccind >= FCCS)
1194         adios (NULL, "too many %ss", hdr->value);
1195     fccfold[fccind++] = getcpy (cp);
1196 }
1197
1198 /*
1199  * BCC GENERATION
1200  */
1201
1202 static void
1203 make_bcc_file (int dashstuff)
1204 {
1205     int fd, i;
1206     pid_t child_id;
1207     char *vec[6];
1208     FILE *out;
1209     char *tfile = NULL;
1210
1211     tfile = m_mktemp2(NULL, "bccs", NULL, &out);
1212     if (tfile == NULL) adios("bcc", "unable to create temporary file");
1213     chmod (bccfil, 0600);
1214     strncpy (bccfil, tfile, sizeof(bccfil));
1215
1216     fprintf (out, "Date: %s\n", dtime (&tclock, 0));
1217     if (msgid)
1218         fprintf (out, "Message-ID: <%d.%ld@%s>\n",
1219                 (int) getpid (), (long) tclock, LocalName (1));
1220     if (subject)
1221         fprintf (out, "Subject: %s", subject);
1222     fprintf (out, "BCC:\n");
1223
1224     /*
1225      * Use MIME encapsulation for Bcc messages
1226      */
1227     if (mime) {
1228         char *cp;
1229
1230         /*
1231          * Check if any lines in the message clash with the
1232          * prefix for the MIME multipart separator.  If there
1233          * is a clash, increment one of the letters in the
1234          * prefix and check again.
1235          */
1236         if ((cp = strchr(prefix, 'a')) == NULL)
1237             adios (NULL, "lost prefix start");
1238         while (find_prefix () == NOTOK) {
1239             if (*cp < 'z')
1240                 (*cp)++;
1241             else
1242                 if (*++cp == 0)
1243                     adios (NULL, "can't find a unique delimiter string");
1244                 else
1245                     (*cp)++;
1246         }
1247
1248         fprintf (out, "%s: %s\n%s: multipart/digest; boundary=\"",
1249                  VRSN_FIELD, VRSN_VALUE, TYPE_FIELD);
1250         fprintf (out, "%s\"\n\n--%s\n\n", prefix, prefix);
1251     } else {
1252         fprintf (out, "\n------- Blind-Carbon-Copy\n\n");
1253     }
1254
1255     fflush (out);
1256
1257     /*
1258      * Do mhl filtering of Bcc messages instead
1259      * of MIME encapsulation.
1260      */
1261     if (filter != NULL) {
1262         vec[0] = r1bindex (mhlproc, '/');
1263
1264         for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
1265             sleep (5);
1266         switch (child_id) {
1267             case NOTOK: 
1268                 adios ("fork", "unable to");
1269
1270             case OK: 
1271                 dup2 (fileno (out), 1);
1272
1273                 i = 1;
1274                 vec[i++] = "-forward";
1275                 vec[i++] = "-form";
1276                 vec[i++] = filter;
1277                 vec[i++] = tmpfil;
1278
1279                 /* was the flag -[no]dashstuffing specified? */
1280                 if (dashstuff > 0)
1281                     vec[i++] = "-dashstuffing";
1282                 else if (dashstuff < 0)
1283                     vec[i++] = "-nodashstuffing";
1284                 vec[i] = NULL;
1285
1286                 execvp (mhlproc, vec);
1287                 fprintf (stderr, "unable to exec ");
1288                 perror (mhlproc);
1289                 _exit (-1);
1290
1291             default: 
1292                 pidXwait (child_id, mhlproc);
1293                 break;
1294         }
1295     } else {
1296         if ((fd = open (tmpfil, O_RDONLY)) == NOTOK)
1297             adios (tmpfil, "unable to re-open");
1298
1299         /*
1300          * If using MIME encapsulation, or if the -nodashstuffing
1301          * flag was given, then just copy message.  Else do
1302          * RFC934 quoting (dashstuffing).
1303          */
1304         if (mime || dashstuff < 0)
1305             cpydata (fd, fileno (out), tmpfil, bccfil);
1306         else
1307             cpydgst (fd, fileno (out), tmpfil, bccfil);
1308         close (fd);
1309     }
1310
1311     fseek (out, 0L, SEEK_END);
1312     if (mime)
1313         fprintf (out, "\n--%s--\n", prefix);
1314     else
1315         fprintf (out, "\n------- End of Blind-Carbon-Copy\n");
1316     fclose (out);
1317 }
1318
1319
1320 /*
1321  * Scan message to check if any lines clash with
1322  * the prefix of the MIME multipart separator.
1323  */
1324
1325 static int
1326 find_prefix (void)
1327 {
1328     int result = OK;
1329     unsigned char buffer[BUFSIZ];
1330     FILE *in;
1331
1332     if ((in = fopen (tmpfil, "r")) == NULL)
1333         adios (tmpfil, "unable to re-open");
1334
1335     while (fgets (buffer, sizeof(buffer) - 1, in))
1336         if (buffer[0] == '-' && buffer[1] == '-') {
1337             unsigned char *cp;
1338
1339             for (cp = buffer + strlen (buffer) - 1; cp >= buffer; cp--)
1340                 if (!isspace (*cp))
1341                     break;
1342             *++cp = '\0';
1343             if (strcmp (buffer + 2, prefix) == 0) {
1344                 result = NOTOK;
1345                 break;
1346             }
1347         }
1348
1349     fclose (in);
1350     return result;
1351 }
1352
1353
1354 #define plural(x) (x == 1 ? "" : "s")
1355
1356 static void
1357 chkadr (void)
1358 {
1359     if (badadr && unkadr)
1360         die (NULL, "%d address%s unparsable, %d addressee%s undeliverable",
1361                 badadr, plural (badadr), unkadr, plural (badadr));
1362     if (badadr)
1363         die (NULL, "%d address%s unparsable", badadr, plural (badadr));
1364     if (unkadr)
1365         die (NULL, "%d addressee%s undeliverable", unkadr, plural (unkadr));
1366 }
1367
1368
1369 static void
1370 do_addresses (int bccque, int talk)
1371 {
1372     int retval;
1373     int state;
1374     struct mailname *lp;
1375
1376     state = 0;
1377     for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1378         if (lp->m_bcc ? bccque : !bccque) {
1379             if (talk && !state)
1380                 printf ("  -- Local Recipients --\n");
1381             do_an_address (lp, talk);
1382             state++;
1383         }
1384
1385     state = 0;
1386     for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1387         if (lp->m_bcc ? bccque : !bccque) {
1388             if (talk && !state)
1389                 printf ("  -- UUCP Recipients --\n");
1390             do_an_address (lp, talk);
1391             state++;
1392         }
1393
1394     state = 0;
1395     for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1396         if (lp->m_bcc ? bccque : !bccque) {
1397             if (talk && !state)
1398                 printf ("  -- Network Recipients --\n");
1399             do_an_address (lp, talk);
1400             state++;
1401         }
1402
1403     chkadr ();
1404
1405     if (rp_isbad (retval = sm_waend ()))
1406         die (NULL, "problem ending addresses; %s", rp_string (retval));
1407 }
1408
1409
1410 /*
1411  * MTS-SPECIFIC INTERACTION
1412  */
1413
1414
1415 /*
1416  * SENDMAIL/SMTP routines
1417  */
1418
1419 static void
1420 post (char *file, int bccque, int talk, char *envelope)
1421 {
1422     int fd, onex;
1423     int retval;
1424
1425     onex = !(msgflags & MINV) || bccque;
1426     if (verbose) {
1427         if (msgflags & MINV)
1428             printf (" -- Posting for %s Recipients --\n",
1429                     bccque ? "Blind" : "Sighted");
1430         else
1431             printf (" -- Posting for All Recipients --\n");
1432     }
1433
1434     sigon ();
1435
1436     if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch, verbose,
1437                                     snoop, onex, queued, sasl, saslssf,
1438                                     saslmech, user, tls))
1439             || rp_isbad (retval = sm_winit (envelope)))
1440         die (NULL, "problem initializing server; %s", rp_string (retval));
1441
1442     do_addresses (bccque, talk && verbose);
1443     if ((fd = open (file, O_RDONLY)) == NOTOK)
1444         die (file, "unable to re-open");
1445     do_text (file, fd);
1446     close (fd);
1447     fflush (stdout);
1448
1449     sm_end (onex ? OK : DONE);
1450     sigoff ();
1451
1452     if (verbose) {
1453         if (msgflags & MINV)
1454             printf (" -- %s Recipient Copies Posted --\n",
1455                     bccque ? "Blind" : "Sighted");
1456         else
1457             printf (" -- Recipient Copies Posted --\n");
1458     }
1459
1460     fflush (stdout);
1461 }
1462
1463
1464 /* Address Verification */
1465
1466 static void
1467 verify_all_addresses (int talk, char *envelope)
1468 {
1469     int retval;
1470     struct mailname *lp;
1471
1472     sigon ();
1473
1474     if (!whomsw || checksw)
1475         if (rp_isbad (retval = sm_init (clientsw, serversw, port, watch,
1476                                         verbose, snoop, 0, queued, sasl,
1477                                         saslssf, saslmech, user, tls))
1478                 || rp_isbad (retval = sm_winit (envelope)))
1479             die (NULL, "problem initializing server; %s", rp_string (retval));
1480
1481     if (talk && !whomsw)
1482         printf (" -- Address Verification --\n");
1483     if (talk && localaddrs.m_next)
1484         printf ("  -- Local Recipients --\n");
1485     for (lp = localaddrs.m_next; lp; lp = lp->m_next)
1486         do_an_address (lp, talk);
1487
1488     if (talk && uuaddrs.m_next)
1489         printf ("  -- UUCP Recipients --\n");
1490     for (lp = uuaddrs.m_next; lp; lp = lp->m_next)
1491         do_an_address (lp, talk);
1492
1493     if (talk && netaddrs.m_next)
1494         printf ("  -- Network Recipients --\n");
1495     for (lp = netaddrs.m_next; lp; lp = lp->m_next)
1496         do_an_address (lp, talk);
1497
1498     chkadr ();
1499     if (talk && !whomsw)
1500         printf (" -- Address Verification Successful --\n");
1501
1502     if (!whomsw || checksw)
1503         sm_end (DONE);
1504
1505     fflush (stdout);
1506     sigoff ();
1507 }
1508
1509
1510 static void
1511 do_an_address (struct mailname *lp, int talk)
1512 {
1513     int retval;
1514     char *mbox, *host;
1515     char addr[BUFSIZ];
1516
1517     switch (lp->m_type) {
1518         case LOCALHOST: 
1519             mbox = lp->m_mbox;
1520             host = lp->m_host;
1521             strncpy (addr, mbox, sizeof(addr));
1522             break;
1523
1524         case UUCPHOST: 
1525             mbox = auxformat (lp, 0);
1526             host = NULL;
1527             snprintf (addr, sizeof(addr), "%s!%s", lp->m_host, lp->m_mbox);
1528             break;
1529
1530         default:                /* let SendMail decide if the host is bad  */
1531             mbox = lp->m_mbox;
1532             host = lp->m_host;
1533             snprintf (addr, sizeof(addr), "%s at %s", mbox, host);
1534             break;
1535     }
1536
1537     if (talk)
1538         printf ("  %s%s", addr, whomsw && lp->m_bcc ? "[BCC]" : "");
1539
1540     if (whomsw && !checksw) {
1541         putchar ('\n');
1542         return;
1543     }
1544     if (talk)
1545         printf (": ");
1546     fflush (stdout);
1547
1548     switch (retval = sm_wadr (mbox, host,
1549                          lp->m_type != UUCPHOST ? lp->m_path : NULL)) {
1550         case RP_OK: 
1551             if (talk)
1552                 printf ("address ok\n");
1553             break;
1554
1555         case RP_NO: 
1556         case RP_USER: 
1557             if (!talk)
1558                 fprintf (stderr, "  %s: ", addr);
1559             fprintf (talk ? stdout : stderr, "loses; %s\n",
1560                         rp_string (retval));
1561             unkadr++;
1562             break;
1563
1564         default: 
1565             if (!talk)
1566                 fprintf (stderr, "  %s: ", addr);
1567             die (NULL, "unexpected response; %s", rp_string (retval));
1568     }
1569
1570     fflush (stdout);
1571 }
1572
1573
1574 static void
1575 do_text (char *file, int fd)
1576 {
1577     int retval, state;
1578     char buf[BUFSIZ];
1579
1580     lseek (fd, (off_t) 0, SEEK_SET);
1581
1582     while ((state = read (fd, buf, sizeof(buf))) > 0) {
1583         if (rp_isbad (retval = sm_wtxt (buf, state)))
1584             die (NULL, "problem writing text; %s\n", rp_string (retval));
1585     }
1586
1587     if (state == NOTOK)
1588         die (file, "problem reading from");
1589
1590     switch (retval = sm_wtend ()) {
1591         case RP_OK: 
1592             break;
1593
1594         case RP_NO: 
1595         case RP_NDEL: 
1596             die (NULL, "posting failed; %s", rp_string (retval));
1597
1598         default: 
1599             die (NULL, "unexpected response; %s", rp_string (retval));
1600     }
1601 }
1602
1603
1604 /*
1605  * SIGNAL HANDLING
1606  */
1607
1608 static void
1609 sigser (int i)
1610 {
1611     NMH_UNUSED (i);
1612
1613     unlink (tmpfil);
1614     if (msgflags & MINV)
1615         unlink (bccfil);
1616
1617     if (!whomsw || checksw)
1618         sm_end (NOTOK);
1619
1620     done (1);
1621 }
1622
1623
1624 static void
1625 sigon (void)
1626 {
1627     if (debug)
1628         return;
1629
1630     hstat = SIGNAL2 (SIGHUP, sigser);
1631     istat = SIGNAL2 (SIGINT, sigser);
1632     qstat = SIGNAL2 (SIGQUIT, sigser);
1633     tstat = SIGNAL2 (SIGTERM, sigser);
1634 }
1635
1636
1637 static void
1638 sigoff (void)
1639 {
1640     if (debug)
1641         return;
1642
1643     SIGNAL (SIGHUP, hstat);
1644     SIGNAL (SIGINT, istat);
1645     SIGNAL (SIGQUIT, qstat);
1646     SIGNAL (SIGTERM, tstat);
1647 }
1648
1649 /*
1650  * FCC INTERACTION
1651  */
1652
1653 static void
1654 p_refile (char *file)
1655 {
1656     int i;
1657
1658     if (fccind == 0)
1659         return;
1660
1661     if (verbose)
1662         printf (" -- Filing Folder Copies --\n");
1663     for (i = 0; i < fccind; i++)
1664         fcc (file, fccfold[i]);
1665     if (verbose)
1666         printf (" -- Folder Copies Filed --\n");
1667 }
1668
1669
1670 /*
1671  * Call the `fileproc' to add the file to the folder.
1672  */
1673
1674 static void
1675 fcc (char *file, char *folder)
1676 {
1677     pid_t child_id;
1678     int i, status;
1679     char fold[BUFSIZ];
1680
1681     if (verbose)
1682         printf ("  %sFcc %s: ", msgstate == RESENT ? "Resent-" : "", folder);
1683     fflush (stdout);
1684
1685     for (i = 0; (child_id = fork ()) == NOTOK && i < 5; i++)
1686         sleep (5);
1687
1688     switch (child_id) {
1689         case NOTOK: 
1690             if (!verbose)
1691                 fprintf (stderr, "  %sFcc %s: ",
1692                         msgstate == RESENT ? "Resent-" : "", folder);
1693             fprintf (verbose ? stdout : stderr, "no forks, so not ok\n");
1694             break;
1695
1696         case OK: 
1697             /* see if we need to add `+' */
1698             snprintf (fold, sizeof(fold), "%s%s",
1699                     *folder == '+' || *folder == '@' ? "" : "+", folder);
1700
1701             /* now exec the fileproc */
1702             execlp (fileproc, r1bindex (fileproc, '/'),
1703                     "-link", "-file", file, fold, NULL);
1704             _exit (-1);
1705
1706         default: 
1707             if ((status = pidwait (child_id, OK))) {
1708                 if (!verbose)
1709                     fprintf (stderr, "  %sFcc %s: ",
1710                             msgstate == RESENT ? "Resent-" : "", folder);
1711                 pidstatus (status, verbose ? stdout : stderr, NULL);
1712             } else {
1713                 if (verbose)
1714                     printf ("folder ok\n");
1715             }
1716     }
1717
1718     fflush (stdout);
1719 }
1720
1721 /*
1722  * TERMINATION
1723  */
1724
1725 static void
1726 die (char *what, char *fmt, ...)
1727 {
1728     va_list ap;
1729
1730     unlink (tmpfil);
1731     if (msgflags & MINV)
1732         unlink (bccfil);
1733
1734     if (!whomsw || checksw)
1735         sm_end (NOTOK);
1736
1737     va_start(ap, fmt);
1738     advertise (what, NULL, fmt, ap);
1739     va_end(ap);
1740     done (1);
1741 }