]> diplodocus.org Git - nmh/blob - uip/post.c
Fixed three LC_ALL settings:
[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 setlocale(LC_ALL, "");
287 invo_name = r1bindex (argv[0], '/');
288
289 /* foil search of user profile/context */
290 if (context_foil (NULL) == -1)
291 done (1);
292
293 mts_init (invo_name);
294 arguments = getarguments (invo_name, argc, argv, 0);
295 argp = arguments;
296
297 while ((cp = *argp++)) {
298 if (*cp == '-') {
299 switch (smatch (++cp, switches)) {
300 case AMBIGSW:
301 ambigsw (cp, switches);
302 done (1);
303 case UNKWNSW:
304 adios (NULL, "-%s unknown", cp);
305
306 case HELPSW:
307 snprintf (buf, sizeof(buf), "%s [switches] file", invo_name);
308 print_help (buf, switches, 0);
309 done (0);
310 case VERSIONSW:
311 print_version(invo_name);
312 done (0);
313
314 case LIBSW:
315 if (!(cp = *argp++) || *cp == '-')
316 adios (NULL, "missing argument to %s", argp[-2]);
317 /* create a minimal context */
318 if (context_foil (cp) == -1)
319 done (1);
320 continue;
321
322 case ALIASW:
323 if (!(cp = *argp++) || *cp == '-')
324 adios (NULL, "missing argument to %s", argp[-2]);
325 if ((state = alias (cp)) != AK_OK)
326 adios (NULL, "aliasing error in %s - %s",
327 cp, akerror (state));
328 continue;
329
330 case CHKSW:
331 checksw++;
332 continue;
333 case NCHKSW:
334 checksw = 0;
335 continue;
336
337 case DEBUGSW:
338 debug++;
339 continue;
340
341 case DISTSW:
342 msgstate = RESENT;
343 continue;
344
345 case FILTSW:
346 if (!(filter = *argp++) || *filter == '-')
347 adios (NULL, "missing argument to %s", argp[-2]);
348 mime = 0;
349 continue;
350 case NFILTSW:
351 filter = NULL;
352 continue;
353
354 case FRMTSW:
355 format++;
356 continue;
357 case NFRMTSW:
358 format = 0;
359 continue;
360
361 case BITSTUFFSW:
362 dashstuff = 1;
363 continue;
364 case NBITSTUFFSW:
365 dashstuff = -1;
366 continue;
367
368 case MIMESW:
369 mime++;
370 filter = NULL;
371 continue;
372 case NMIMESW:
373 mime = 0;
374 continue;
375
376 case MSGDSW:
377 msgid++;
378 continue;
379 case NMSGDSW:
380 msgid = 0;
381 continue;
382
383 case VERBSW:
384 verbose++;
385 continue;
386 case NVERBSW:
387 verbose = 0;
388 continue;
389
390 case WATCSW:
391 watch++;
392 continue;
393 case NWATCSW:
394 watch = 0;
395 continue;
396
397 case WHOMSW:
398 whomsw++;
399 continue;
400
401 case WIDTHSW:
402 if (!(cp = *argp++) || *cp == '-')
403 adios (NULL, "missing argument to %s", argp[-2]);
404 if ((outputlinelen = atoi (cp)) < 10)
405 adios (NULL, "impossible width %d", outputlinelen);
406 continue;
407
408 case ANNOSW:
409 if (!(cp = *argp++) || *cp == '-')
410 adios (NULL, "missing argument to %s", argp[-2]);
411 if ((pfd = atoi (cp)) <= 2)
412 adios (NULL, "bad argument %s %s", argp[-2], cp);
413 continue;
414
415 case CLIESW:
416 if (!(clientsw = *argp++) || *clientsw == '-')
417 adios (NULL, "missing argument to %s", argp[-2]);
418 continue;
419 case SERVSW:
420 if (!(serversw = *argp++) || *serversw == '-')
421 adios (NULL, "missing argument to %s", argp[-2]);
422 continue;
423 case SNOOPSW:
424 snoop++;
425 continue;
426
427 case PARTSW:
428 if (!(partno = *argp++) || *partno == '-')
429 adios (NULL, "missing argument to %s", argp[-2]);
430 continue;
431
432 case QUEUESW:
433 queued++;
434 continue;
435
436 case SASLSW:
437 sasl++;
438 continue;
439
440 case NOSASLSW:
441 sasl = 0;
442 continue;
443
444 case SASLMXSSFSW:
445 if (!(cp = *argp++) || *cp == '-')
446 adios (NULL, "missing argument to %s", argp[-2]);
447 saslssf = atoi(cp);
448 continue;
449
450 case SASLMECHSW:
451 if (!(saslmech = *argp++) || *saslmech == '-')
452 adios (NULL, "missing argument to %s", argp[-2]);
453 continue;
454
455 case USERSW:
456 if (!(user = *argp++) || *user == '-')
457 adios (NULL, "missing argument to %s", argp[-2]);
458 continue;
459
460 case PORTSW:
461 if (!(port = *argp++) || *port == '-')
462 adios (NULL, "missing argument to %s", argp[-2]);
463 continue;
464
465 case TLSSW:
466 tls = 1;
467 continue;
468
469 case INITTLSSW:
470 tls = 2;
471 continue;
472
473 case NTLSSW:
474 tls = 0;
475 continue;
476
477 case FILEPROCSW:
478 if (!(cp = *argp++) || *cp == '-')
479 adios (NULL, "missing argument to %s", argp[-2]);
480 fileproc = cp;
481 continue;
482
483 case MHLPROCSW:
484 if (!(cp = *argp++) || *cp == '-')
485 adios (NULL, "missing argument to %s", argp[-2]);
486 mhlproc = cp;
487 continue;
488
489 case MTSSW:
490 if (!(cp = *argp++) || *cp == '-')
491 adios (NULL, "missing argument to %s", argp[-2]);
492 save_mts_method (cp);
493 continue;
494
495 case CREDENTIALSSW: {
496 if (!(cp = *argp++) || *cp == '-')
497 adios (NULL, "missing argument to %s", argp[-2]);
498 add_profile_entry ("credentials", cp);
499 continue;
500 }
501
502 case MESSAGEIDSW:
503 if (!(cp = *argp++) || *cp == '-')
504 adios (NULL, "missing argument to %s", argp[-2]);
505 if (save_message_id_style (cp) != 0)
506 adios (NULL, "unsupported messageid \"%s\"", cp);
507 continue;
508 }
509 }
510 if (msg)
511 adios (NULL, "only one message at a time!");
512 else
513 msg = cp;
514 }
515
516 alias (AliasFile);
517
518 if (!msg)
519 adios (NULL, "usage: %s [switches] file", invo_name);
520
521 if (outputlinelen < 10)
522 adios (NULL, "impossible width %d", outputlinelen);
523
524 if ((in = fopen (msg, "r")) == NULL)
525 adios (msg, "unable to open");
526
527 start_headers ();
528 if (debug) {
529 verbose++;
530 out = stdout;
531 } else {
532 if (whomsw) {
533 if ((out = fopen ("/dev/null", "w")) == NULL)
534 adios ("/dev/null", "unable to open");
535 } else {
536 char *cp = m_mktemp(m_maildir(invo_name), NULL, &out);
537 if (cp == NULL) {
538 cp = m_mktemp2(NULL, invo_name, NULL, &out);
539 if (cp == NULL) {
540 adios ("post", "unable to create temporary file");
541 }
542 }
543 strncpy(tmpfil, cp, sizeof(tmpfil));
544 chmod (tmpfil, 0600);
545 }
546 }
547
548 hdrtab = msgstate == NORMAL ? NHeaders : RHeaders;
549
550 for (compnum = 1;;) {
551 int bufsz = sizeof buf;
552 switch (state = m_getfld (&gstate, name, buf, &bufsz, in)) {
553 case FLD:
554 case FLDPLUS:
555 compnum++;
556 cp = add (buf, NULL);
557 while (state == FLDPLUS) {
558 bufsz = sizeof buf;
559 state = m_getfld (&gstate, name, buf, &bufsz, in);
560 cp = add (buf, cp);
561 }
562 putfmt (name, cp, out);
563 free (cp);
564 continue;
565
566 case BODY:
567 finish_headers (out);
568 if (whomsw)
569 break;
570 fprintf (out, "\n%s", buf);
571 while (state == BODY) {
572 bufsz = sizeof buf;
573 state = m_getfld (&gstate, name, buf, &bufsz, in);
574 fputs (buf, out);
575 }
576 break;
577
578 case FILEEOF:
579 finish_headers (out);
580 break;
581
582 case LENERR:
583 case FMTERR:
584 adios (NULL, "message format error in component #%d", compnum);
585
586 default:
587 adios (NULL, "getfld() returned %d", state);
588 }
589 break;
590 }
591 m_getfld_state_destroy (&gstate);
592
593 if (pfd != NOTOK)
594 anno ();
595 fclose (in);
596
597 if (debug) {
598 pl ();
599 done (0);
600 } else {
601 fclose (out);
602 }
603
604 /*
605 * Here's how we decide which address to use as the envelope-from
606 * address for SMTP.
607 *
608 * - If we were given an Envelope-From header, use that.
609 * - If we were given a Sender: address, use that.
610 * - Otherwise, use the address on the From: line
611 */
612
613 if (msgflags & MEFM) {
614 envelope = efrom;
615 } else if (seensender) {
616 envelope = sender;
617 } else {
618 envelope = from;
619 }
620
621 /* If we are doing a "whom" check */
622 if (whomsw) {
623 /* This won't work with MTS_SENDMAIL_PIPE. */
624 verify_all_addresses (1, envelope);
625 done (0);
626 }
627
628 if (msgflags & MINV) {
629 make_bcc_file (dashstuff);
630 if (msgflags & MVIS) {
631 if (sm_mts != MTS_SENDMAIL_PIPE) {
632 /* It would be nice to have support to call
633 verify_all_addresses with MTS_SENDMAIL_PIPE, but
634 that might require running sendmail as root. Note
635 that spost didn't verify addresses. */
636 verify_all_addresses (verbose, envelope);
637 }
638 post (tmpfil, 0, verbose, envelope);
639 }
640 post (bccfil, 1, verbose, envelope);
641 unlink (bccfil);
642 } else {
643 post (tmpfil, 0, isatty (1), envelope);
644 }
645
646 p_refile (tmpfil);
647 unlink (tmpfil);
648
649 if (verbose) {
650 if (partno)
651 printf ("Partial Message #%s Processed\n", partno);
652 else
653 printf ("Message Processed\n");
654 }
655
656 done (0);
657 return 1;
658 }
659
660
661 /*
662 * DRAFT GENERATION
663 */
664
665 static void
666 putfmt (char *name, char *str, FILE *out)
667 {
668 int count, grp, i, keep;
669 char *cp, *pp, *qp;
670 char namep[BUFSIZ], error[BUFSIZ];
671 struct mailname *mp = NULL, *np = NULL;
672 struct headers *hdr;
673
674 while (*str == ' ' || *str == '\t')
675 str++;
676
677 if (msgstate == NORMAL && uprf (name, "resent")) {
678 advise (NULL, "illegal header line -- %s:", name);
679 badmsg++;
680 return;
681 }
682
683 if ((i = get_header (name, hdrtab)) == NOTOK) {
684 if (strncasecmp (name, "nmh-", 4)) {
685 fprintf (out, "%s: %s", name, str);
686 } else {
687 /* Filter out all Nmh-* headers, because Norm asked. They
688 should never have reached this point. Warn about any
689 that are non-empty. */
690 if (strcmp (str, "\n")) {
691 char *newline = strchr (str, '\n');
692 if (newline) *newline = '\0';
693 if (! whomsw) {
694 advise (NULL, "ignoring header line -- %s: %s", name, str);
695 }
696 }
697 }
698
699 return;
700 }
701
702 hdr = &hdrtab[i];
703 if (hdr->flags & HIGN) {
704 return;
705 }
706 if (hdr->flags & HBAD) {
707 advise (NULL, "illegal header line -- %s:", name);
708 badmsg++;
709 return;
710 }
711 msgflags |= (hdr->set & ~(MVIS | MINV));
712
713 if (hdr->flags & HSUB)
714 subject = subject ? add (str, add ("\t", subject)) : getcpy (str);
715 if (hdr->flags & HFCC) {
716 if ((cp = strrchr(str, '\n')))
717 *cp = 0;
718 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
719 *cp++ = 0;
720 insert_fcc (hdr, pp);
721 }
722 insert_fcc (hdr, pp);
723 return;
724 }
725
726 if (!(hdr->flags & HADR)) {
727 fprintf (out, "%s: %s", name, str);
728 return;
729 }
730
731 tmpaddrs.m_next = NULL;
732 for (count = 0; (cp = getname (str)); count++)
733 if ((mp = getm (cp, NULL, 0, error, sizeof(error)))) {
734 if (tmpaddrs.m_next)
735 np->m_next = mp;
736 else
737 tmpaddrs.m_next = mp;
738 np = mp;
739 }
740 else {
741 admonish(cp, "%s", error);
742 if (hdr->flags & HTRY)
743 badadr++;
744 else
745 badmsg++;
746 }
747
748 if (count < 1) {
749 if (hdr->flags & HNIL)
750 fprintf (out, "%s: %s", name, str);
751 else {
752 /*
753 * Sender (or Resent-Sender) can have only one address
754 */
755 if ((msgstate == RESENT) ? (hdr->set & MRSN)
756 : (hdr->set & MSND)) {
757 advise (NULL, "%s: field requires one address", name);
758 badmsg++;
759 }
760 #ifdef notdef
761 advise (NULL, "%s: field requires at least one address", name);
762 badmsg++;
763 #endif /* notdef */
764 }
765 return;
766 }
767
768 if (count > 1 && (hdr->flags & HONE)) {
769 advise (NULL, "%s: field only permits one address", name);
770 badmsg++;
771 return;
772 }
773
774 nameoutput = linepos = 0;
775 snprintf (namep, sizeof(namep), "%s%s",
776 (hdr->flags & HMNG) ? "Original-" : "", name);
777
778 for (grp = 0, mp = tmpaddrs.m_next; mp; mp = np)
779 if (mp->m_nohost) { /* also used to test (hdr->flags & HTRY) */
780 /* The address doesn't include a host, so it might be an alias. */
781 pp = akvalue (mp->m_mbox); /* do mh alias substitution */
782 qp = akvisible () ? mp->m_mbox : "";
783 np = mp;
784 if (np->m_gname)
785 putgrp (namep, np->m_gname, out, hdr->flags);
786 while ((cp = getname (pp))) {
787 if (!(mp = getm (cp, NULL, 0, error, sizeof(error)))) {
788 admonish(cp, "%s", error);
789 badadr++;
790 continue;
791 }
792
793 /*
794 * If it's a From: or Resent-From: header, save the address
795 * for later possible use (as the envelope address for SMTP)
796 */
797
798 if ((msgstate == RESENT) ? (hdr->set & MRFM)
799 : (hdr->set & MFRM)) {
800 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
801 from[sizeof(from) - 1] = '\0';
802 fromcount = count;
803 }
804
805 /*
806 * Also save the Sender: or Resent-Sender: header as well
807 */
808
809 if ((msgstate == RESENT) ? (hdr->set & MRSN)
810 : (hdr->set & MSND)) {
811 strncpy(sender, auxformat(mp, 0), sizeof(sender) - 1);
812 sender[sizeof(sender) - 1] = '\0';
813 seensender++;
814 }
815
816 /*
817 * ALSO ... save Envelope-From
818 */
819
820 if (hdr->set & MEFM) {
821 strncpy(efrom, auxformat(mp, 0), sizeof(efrom) - 1);
822 efrom[sizeof(efrom) - 1] = '\0';
823 }
824
825 if (hdr->flags & HBCC)
826 mp->m_bcc++;
827 if (np->m_ingrp)
828 mp->m_ingrp = np->m_ingrp;
829 else
830 if (mp->m_gname)
831 putgrp (namep, mp->m_gname, out, hdr->flags);
832 if (mp->m_ingrp) {
833 if (sm_mts == MTS_SENDMAIL_PIPE) {
834 /* Catch this before sendmail chokes with:
835 "553 List:; syntax illegal for recipient
836 addresses".
837 If we wanted to, we could expand out blind
838 aliases and put them in Bcc:, but then
839 they'd have the Blind-Carbon-Copy
840 indication. */
841 adios (NULL,
842 "blind lists not compatible with"
843 " sendmail/pipe");
844 }
845
846 grp++;
847 }
848 if (putadr (namep, qp, mp, out, hdr->flags))
849 msgflags |= (hdr->set & (MVIS | MINV));
850 else
851 mnfree (mp);
852 }
853 mp = np;
854 np = np->m_next;
855 mnfree (mp);
856 }
857 else {
858 /* Address includes a host, so no alias substitution is needed. */
859
860 /*
861 * If it's a From: or Resent-From header, save the address
862 * for later possible use (as the envelope address for SMTP)
863 */
864
865 if ((msgstate == RESENT) ? (hdr->set & MRFM)
866 : (hdr->set & MFRM)) {
867 strncpy(from, auxformat(mp, 0), sizeof(from) - 1);
868 fromcount = count;
869 }
870
871 /*
872 * Also save the Sender: header as well
873 */
874
875 if ((msgstate == RESENT) ? (hdr->set & MRSN)
876 : (hdr->set & MSND)) {
877 strncpy(sender, auxformat(mp, 0), sizeof(sender) - 1);
878 sender[sizeof(sender) - 1] = '\0';
879 seensender++;
880 }
881
882 /*
883 * ALSO ... save Envelope-From
884 */
885
886 if (hdr->set & MEFM) {
887 strncpy(efrom, auxformat(mp, 0), sizeof(efrom) - 1);
888 efrom[sizeof(efrom) - 1] = '\0';
889 }
890
891 if (hdr->flags & HBCC)
892 mp->m_bcc++;
893 if (mp->m_gname)
894 putgrp (namep, mp->m_gname, out, hdr->flags);
895 if (mp->m_ingrp)
896 grp++;
897 keep = putadr (namep, "", mp, out, hdr->flags);
898 np = mp->m_next;
899 if (keep) {
900 mp->m_next = NULL;
901 msgflags |= (hdr->set & (MVIS | MINV));
902 }
903 else
904 mnfree (mp);
905 }
906
907 /*
908 * If this is a From:/Resent-From: header, save the full thing for
909 * later in case we need it for use when constructing a Bcc draft message
910 */
911
912 if ((msgstate == RESENT) ? (hdr->set & MRFM) : (hdr->set & MFRM)) {
913 strncpy(fullfrom, str, sizeof(fullfrom));
914 fullfrom[sizeof(fullfrom) - 1] = 0;
915 /*
916 * Strip off any trailing newlines
917 */
918
919 while (strlen(fullfrom) > 0 && fullfrom[strlen(fullfrom) - 1] == '\n') {
920 fullfrom[strlen(fullfrom) - 1] = '\0';
921 }
922 }
923
924 if (grp > 0 && (hdr->flags & HNGR)) {
925 advise (NULL, "%s: field does not allow groups", name);
926 badmsg++;
927 }
928 if (linepos) {
929 putc ('\n', out);
930 }
931 }
932
933
934 static void
935 start_headers (void)
936 {
937 time (&tclock);
938
939 /*
940 * Probably not necessary, but just in case ...
941 */
942
943 from[0] = '\0';
944 efrom[0] = '\0';
945 sender[0] = '\0';
946 fullfrom[0] = '\0';
947 }
948
949
950 /*
951 * Now that we've outputted the header fields in the draft
952 * message, we will now output any remaining header fields
953 * that we need to add/create.
954 */
955
956 static void
957 finish_headers (FILE *out)
958 {
959 switch (msgstate) {
960 case NORMAL:
961 if (!(msgflags & MFRM)) {
962 /*
963 * A From: header is now required in the draft.
964 */
965 advise (NULL, "message has no From: header");
966 advise (NULL, "See default components files for examples");
967 badmsg++;
968 break;
969 }
970
971 if (fromcount > 1 && (seensender == 0 && !(msgflags & MEFM))) {
972 advise (NULL, "A Sender: or Envelope-From: header is required "
973 "with multiple\nFrom: addresses");
974 badmsg++;
975 break;
976 }
977
978 if (whomsw)
979 break;
980
981 fprintf (out, "Date: %s\n", dtime (&tclock, 0));
982 if (msgid)
983 fprintf (out, "Message-ID: %s\n", message_id (tclock, 0));
984 /*
985 * If we have multiple From: addresses, make sure we have an
986 * Sender: header. If we don't have one, then generate one
987 * from Envelope-From: (which in this case, cannot be blank)
988 */
989
990 if (fromcount > 1 && seensender == 0) {
991 if (efrom[0] == '\0') {
992 advise (NULL, "Envelope-From cannot be blank when there "
993 "is multiple From: addresses\nand no Sender: "
994 "header");
995 badmsg++;
996 } else {
997 fprintf (out, "Sender: %s\n", efrom);
998 }
999 }
1000
1001 if (!(msgflags & MVIS))
1002 fprintf (out, "Bcc: Blind Distribution List: ;\n");
1003 break;
1004
1005 case RESENT:
1006 if (!(msgflags & MDAT)) {
1007 advise (NULL, "message has no Date: header");
1008 badmsg++;
1009 }
1010 if (!(msgflags & MFRM)) {
1011 advise (NULL, "message has no From: header");
1012 badmsg++;
1013 }
1014 if (!(msgflags & MRFM)) {
1015 advise (NULL, "message has no Resent-From: header");
1016 advise (NULL, "See default components files for examples");
1017 badmsg++;
1018 break;
1019 }
1020 if (fromcount > 1 && (seensender == 0 && !(msgflags & MEFM))) {
1021 advise (NULL, "A Resent-Sender: or Envelope-From: header is "
1022 "required with multiple\nResent-From: addresses");
1023 badmsg++;
1024 break;
1025 }
1026
1027 if (whomsw)
1028 break;
1029
1030 fprintf (out, "Resent-Date: %s\n", dtime (&tclock, 0));
1031 if (msgid)
1032 fprintf (out, "Resent-Message-ID: %s\n",
1033 message_id (tclock, 0));
1034 /*
1035 * If we have multiple Resent-From: addresses, make sure we have an
1036 * Resent-Sender: header. If we don't have one, then generate one
1037 * from Envelope-From (which in this case, cannot be blank)
1038 */
1039
1040 if (fromcount > 1 && seensender == 0) {
1041 if (efrom[0] == '\0') {
1042 advise (NULL, "Envelope-From cannot be blank when there "
1043 "is multiple Resent-From: addresses and no "
1044 "Resent-Sender: header");
1045 badmsg++;
1046 } else {
1047 fprintf (out, "Resent-Sender: %s\n", efrom);
1048 }
1049 }
1050
1051 if (!(msgflags & MVIS))
1052 fprintf (out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1053 break;
1054 }
1055
1056 if (badmsg)
1057 adios (NULL, "re-format message and try again");
1058 if (!recipients)
1059 adios (NULL, "no addressees");
1060 }
1061
1062
1063 static int
1064 get_header (char *header, struct headers *table)
1065 {
1066 struct headers *h;
1067
1068 for (h = table; h->value; h++)
1069 if (!strcasecmp (header ? header : "", h->value ? h->value : ""))
1070 return (h - table);
1071
1072 return NOTOK;
1073 }
1074
1075
1076 static int
1077 putadr (char *name, char *aka, struct mailname *mp, FILE *out, unsigned int flags)
1078 {
1079 int len;
1080 char *cp;
1081 char buffer[BUFSIZ];
1082
1083 if (mp->m_mbox == NULL || ((flags & HTRY) && !insert (mp)))
1084 return 0;
1085 if (sm_mts != MTS_SENDMAIL_PIPE &&
1086 ((flags & (HBCC | HDCC | HEFM)) || mp->m_ingrp))
1087 return 1;
1088
1089 if (!nameoutput) {
1090 fprintf (out, "%s: ", name);
1091 linepos += (nameoutput = strlen (name) + 2);
1092 }
1093
1094 if (*aka && mp->m_type != UUCPHOST && !mp->m_pers)
1095 mp->m_pers = getcpy (aka);
1096 if (format) {
1097 if (mp->m_gname) {
1098 snprintf (buffer, sizeof(buffer), "%s;", mp->m_gname);
1099 cp = buffer;
1100 } else {
1101 cp = adrformat (mp);
1102 }
1103 } else {
1104 cp = mp->m_text;
1105 }
1106 len = strlen (cp);
1107
1108 if (linepos != nameoutput) {
1109 if (len + linepos + 2 > outputlinelen)
1110 fprintf (out, ",\n%*s", linepos = nameoutput, "");
1111 else {
1112 fputs (", ", out);
1113 linepos += 2;
1114 }
1115 }
1116
1117 fputs (cp, out);
1118 linepos += len;
1119
1120 return (flags & HTRY);
1121 }
1122
1123
1124 static void
1125 putgrp (char *name, char *group, FILE *out, unsigned int flags)
1126 {
1127 int len;
1128 char *cp;
1129
1130 if (sm_mts != MTS_SENDMAIL_PIPE && (flags & HBCC))
1131 return;
1132
1133 if (!nameoutput) {
1134 fprintf (out, "%s: ", name);
1135 linepos += (nameoutput = strlen (name) + 2);
1136 }
1137
1138 cp = concat (group, ";", NULL);
1139 len = strlen (cp);
1140
1141 if (linepos > nameoutput) {
1142 if (len + linepos + 2 > outputlinelen) {
1143 fprintf (out, ",\n%*s", nameoutput, "");
1144 linepos = nameoutput;
1145 }
1146 else {
1147 fputs (", ", out);
1148 linepos += 2;
1149 }
1150 }
1151
1152 fputs (cp, out);
1153 linepos += len;
1154 }
1155
1156
1157 static int
1158 insert (struct mailname *np)
1159 {
1160 struct mailname *mp;
1161
1162 if (np->m_mbox == NULL)
1163 return 0;
1164
1165 for (mp = np->m_type == LOCALHOST ? &localaddrs
1166 : np->m_type == UUCPHOST ? &uuaddrs
1167 : &netaddrs;
1168 mp->m_next;
1169 mp = mp->m_next)
1170 if (!strcasecmp (np->m_host ? np->m_host : "",
1171 mp->m_next->m_host ? mp->m_next->m_host : "") &&
1172 !strcasecmp (np->m_mbox ? np->m_mbox : "",
1173 mp->m_next->m_mbox ? mp->m_next->m_mbox : "") &&
1174 np->m_bcc == mp->m_next->m_bcc)
1175 return 0;
1176
1177 mp->m_next = np;
1178 recipients++;
1179 return 1;
1180 }
1181
1182
1183 static void
1184 pl (void)
1185 {
1186 int i;
1187 struct mailname *mp;
1188
1189 printf ("-------\n\t-- Addresses --\nlocal:\t");
1190 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1191 printf ("%s%s%s", mp->m_mbox,
1192 mp->m_bcc ? "[BCC]" : "",
1193 mp->m_next ? ",\n\t" : "");
1194
1195 printf ("\nnet:\t");
1196 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1197 printf ("%s%s@%s%s%s", mp->m_path ? mp->m_path : "",
1198 mp->m_mbox, mp->m_host,
1199 mp->m_bcc ? "[BCC]" : "",
1200 mp->m_next ? ",\n\t" : "");
1201
1202 printf ("\nuucp:\t");
1203 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1204 printf ("%s!%s%s%s", mp->m_host, mp->m_mbox,
1205 mp->m_bcc ? "[BCC]" : "",
1206 mp->m_next ? ",\n\t" : "");
1207
1208 printf ("\n\t-- Folder Copies --\nfcc:\t");
1209 for (i = 0; i < fccind; i++)
1210 printf ("%s%s", fccfold[i], i + 1 < fccind ? ",\n\t" : "");
1211 printf ("\n");
1212 }
1213
1214
1215 static void
1216 anno (void)
1217 {
1218 struct mailname *mp;
1219
1220 for (mp = localaddrs.m_next; mp; mp = mp->m_next)
1221 if (annoaux (mp) == NOTOK)
1222 goto oops;
1223
1224 for (mp = netaddrs.m_next; mp; mp = mp->m_next)
1225 if (annoaux (mp) == NOTOK)
1226 goto oops;
1227
1228 for (mp = uuaddrs.m_next; mp; mp = mp->m_next)
1229 if (annoaux (mp) == NOTOK)
1230 break;
1231
1232 oops: ;
1233 close (pfd);
1234 pfd = NOTOK;
1235 }
1236
1237
1238 static int
1239 annoaux (struct mailname *mp)
1240 {
1241 int i;
1242 char buffer[BUFSIZ];
1243
1244 snprintf (buffer, sizeof(buffer), "%s\n", adrformat (mp));
1245 i = strlen (buffer);
1246
1247 return (write (pfd, buffer, i) == i ? OK : NOTOK);
1248 }
1249
1250
1251 static void
1252 insert_fcc (struct headers *hdr, char *pp)
1253 {
1254 char *cp;
1255
1256 for (cp = pp; isspace ((unsigned char) *cp); cp++)
1257 continue;
1258 for (pp += strlen (pp) - 1; pp > cp && isspace ((unsigned char) *pp); pp--)
1259 continue;
1260 if (pp >= cp)
1261 *++pp = 0;
1262 if (*cp == 0)
1263 return;
1264
1265 if (fccind >= FCCS)
1266 adios (NULL, "too many %ss", hdr->value);
1267 fccfold[fccind++] = getcpy (cp);
1268 }
1269
1270 /*
1271 * BCC GENERATION
1272 */
1273
1274 static void
1275 make_bcc_file (int dashstuff)
1276 {
1277 int fd, i;
1278 pid_t child_id;
1279 char **vec;
1280 FILE *out;
1281 char *tfile = NULL, *program;
1282
1283 tfile = m_mktemp2(NULL, "bccs", NULL, &out);
1284 if (tfile == NULL) adios("bcc", "unable to create temporary file");
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, queued, sasl,
1539 saslssf, 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, queued, sasl,
1579 saslssf, 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 unlink (tmpfil);
1716 if (msgflags & MINV)
1717 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 unlink (tmpfil);
1840 if (msgflags & MINV)
1841 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 }