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