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