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