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