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