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