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