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