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