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