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