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