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