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