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