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