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