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