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