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