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