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