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