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