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