]>
diplodocus.org Git - nmh/blob - uip/post.c
3 * post.c -- enter messages into the mail transport system
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
13 #include <h/addrsbr.h>
14 #include <h/aliasbr.h>
15 #include <h/dropsbr.h>
21 #ifdef HAVE_SYS_TIME_H
22 # include <sys/time.h>
26 #include <mts/smtp/smtp.h>
29 # define SASLminc(a) (a)
30 #else /* CYRUS_SASL */
31 # define SASLminc(a) 0
32 #endif /* CYRUS_SASL */
35 # define TLSminc(a) (a)
36 #else /* TLS_SUPPORT */
38 #endif /* TLS_SUPPORT */
41 # define OAUTHminc(a) (a)
42 #else /* OAUTH_SUPPORT */
43 # define OAUTHminc(a) 0
44 #endif /* OAUTH_SUPPORT */
46 #define FCCS 10 /* max number of fccs allowed */
48 /* In the following array of structures, the numeric second field of the
49 structures (minchars) is apparently used like this:
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. */
55 #define POST_SWITCHES \
56 X("alias aliasfile", 0, ALIASW) \
57 X("check", -5, CHKSW) /* interface from whom */ \
58 X("nocheck", -7, NCHKSW) /* interface from whom */ \
59 X("debug", -5, DEBUGSW) \
60 X("dist", -4, DISTSW) /* interface from dist */ \
61 X("filter filterfile", 0, FILTSW) \
62 X("nofilter", 0, NFILTSW) \
63 X("format", 0, FRMTSW) \
64 X("noformat", 0, NFRMTSW) \
65 X("library directory", -7, LIBSW) /* interface from send, whom */ \
66 X("mime", 0, MIMESW) \
67 X("nomime", 0, NMIMESW) \
68 X("msgid", 0, MSGDSW) \
69 X("nomsgid", 0, NMSGDSW) \
70 X("verbose", 0, VERBSW) \
71 X("noverbose", 0, NVERBSW) \
72 X("watch", 0, WATCSW) \
73 X("nowatch", 0, NWATCSW) \
74 X("whom", -4, WHOMSW) /* interface from whom */ \
75 X("width columns", 0, WIDTHSW) \
76 X("version", 0, VERSIONSW) \
77 X("help", 0, HELPSW) \
78 X("dashstuffing", -12, BITSTUFFSW) /* should we dashstuff BCC messages? */ \
79 X("nodashstuffing", -14, NBITSTUFFSW) \
80 X("idanno number", -6, ANNOSW) /* interface from send */ \
81 X("client host", -6, CLIESW) \
82 X("server host", 6, SERVSW) /* specify alternate SMTP server */ \
83 X("snoop", -5, SNOOPSW) /* snoop the SMTP transaction */ \
84 X("partno", -6, PARTSW) \
85 X("sasl", SASLminc(4), SASLSW) \
86 X("nosasl", SASLminc(6), NOSASLSW) \
87 X("saslmaxssf", SASLminc(10), SASLMXSSFSW) \
88 X("saslmech", SASLminc(5), SASLMECHSW) \
89 X("user", SASLminc(-4), USERSW) \
90 X("port server submission port name/number", 4, PORTSW) \
91 X("tls", TLSminc(-3), TLSSW) \
92 X("initialtls", TLSminc(-10), INITTLSSW) \
93 X("notls", TLSminc(-5), NTLSSW) \
94 X("fileproc", -4, FILEPROCSW) \
95 X("mhlproc", -3, MHLPROCSW) \
96 X("sendmail program", 0, MTSSM) \
97 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
98 X("credentials legacy|file:filename", 0, CREDENTIALSSW) \
99 X("messageid localname|random", 2, MESSAGEIDSW) \
100 X("oauthcredfile", OAUTHminc(-7), OAUTHCREDFILESW) \
101 X("oauthclientid", OAUTHminc(-12), OAUTHCLIDSW) \
102 X("oauthclientsecret", OAUTHminc(-12), OAUTHCLSECSW) \
103 X("oauthauthendpoint", OAUTHminc(-6), OAUTHAUTHENDSW) \
104 X("oauthredirect", OAUTHminc(-6), OAUTHREDIRSW) \
105 X("oauthtokenendpoint", OAUTHminc(-6), OAUTHTOKENDSW) \
106 X("oauthscope", OAUTHminc(-6), OAUTHSCOPESW) \
108 #define X(sw, minchars, id) id,
109 DEFINE_SWITCH_ENUM(POST
);
112 #define X(sw, minchars, id) { sw, minchars, id },
113 DEFINE_SWITCH_ARRAY(POST
, switches
);
118 * Mapping between command-line switches and profile entries, communicated
119 * from 'send'. We use a service name of 'post' internally.
122 static struct oauth_profile
{
123 const char *profname
;
125 } oauthswitches
[] = {
126 { "oauth-post-credential-file", OAUTHCREDFILESW
},
127 { "oauth-post-client_id", OAUTHCLIDSW
},
128 { "oauth-post-client_secret", OAUTHCLSECSW
},
129 { "oauth-post-auth_endpoint", OAUTHAUTHENDSW
},
130 { "oauth-post-redirect_url", OAUTHREDIRSW
},
131 { "oauth-post-token_endpoint", OAUTHTOKENDSW
},
132 { "oauth-post-scope", OAUTHSCOPESW
},
143 * flags for headers->flags
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 */
161 * flags for headers->set
163 #define MFRM 0x0001 /* we've seen a From: */
164 #define MDAT 0x0002 /* we've seen a Date: */
165 #define MRFM 0x0004 /* we've seen a Resent-From: */
166 #define MVIS 0x0008 /* we've seen sighted addrs */
167 #define MINV 0x0010 /* we've seen blind addrs */
168 #define MSND 0x0020 /* we've seen a Sender: */
169 #define MRSN 0x0040 /* We've seen a Resent-Sendr:*/
170 #define MEFM 0x0080 /* We've seen Envelope-From: */
173 static struct headers NHeaders
[] = {
174 { "Return-Path", HBAD
, 0 },
175 { "Received", HBAD
, 0 },
176 { "Reply-To", HADR
|HNGR
, 0 },
177 { "From", HADR
|HNGR
, MFRM
},
178 { "Sender", HADR
|HNGR
|HONE
, MSND
},
180 { "Subject", HSUB
, 0 },
181 { "To", HADR
|HTRY
, MVIS
},
182 { "cc", HADR
|HTRY
, MVIS
},
183 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
184 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
185 { "Message-ID", HBAD
, 0 },
187 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
191 static struct headers RHeaders
[] = {
192 { "Resent-Reply-To", HADR
|HNGR
, 0 },
193 { "Resent-From", HADR
|HNGR
, MRFM
},
194 { "Resent-Sender", HADR
|HNGR
, MRSN
},
195 { "Resent-Date", HBAD
, 0 },
196 { "Resent-Subject", HSUB
, 0 },
197 { "Resent-To", HADR
|HTRY
, MVIS
},
198 { "Resent-cc", HADR
|HTRY
, MVIS
},
199 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
200 { "Resent-Message-ID", HBAD
, 0 },
201 { "Resent-Fcc", HFCC
, 0 },
202 { "Reply-To", HADR
, 0 },
203 { "From", HADR
|HNGR
, MFRM
},
204 { "Sender", HADR
|HNGR
, MSND
},
205 { "Date", HNOP
, MDAT
},
206 { "To", HADR
|HNIL
, 0 },
207 { "cc", HADR
|HNIL
, 0 },
208 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
210 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
214 static short fccind
= 0; /* index into fccfold[] */
215 static short outputlinelen
= OUTPUTLINELEN
;
217 static int pfd
= NOTOK
; /* fd to write annotation list to */
218 static int recipients
= 0; /* how many people will get a copy */
219 static int unkadr
= 0; /* how many of those were unknown */
220 static int badadr
= 0; /* number of bad addrs */
221 static int badmsg
= 0; /* message has bad semantics */
222 static int verbose
= 0; /* spell it out */
223 static int format
= 1; /* format addresses */
224 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
225 static int msgid
= 0; /* add msgid */
226 static int debug
= 0; /* debugging post */
227 static int watch
= 0; /* watch the delivery process */
228 static int whomsw
= 0; /* we are whom not post */
229 static int checksw
= 0; /* whom -check */
230 static int linepos
=0; /* putadr()'s position on the line */
231 static int nameoutput
=0; /* putadr() has output header name */
232 static int sasl
=0; /* Use SASL auth for SMTP */
233 static int saslssf
=-1; /* Our maximum SSF for SASL */
234 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
235 static char *user
=NULL
; /* Authenticate as this user */
236 static char *port
="submission"; /* Name of server port for SMTP submission */
237 static int tls
=-1; /* Use TLS for encryption */
238 static int fromcount
=0; /* Count of addresses on From: header */
239 static int seensender
=0; /* Have we seen a Sender: header? */
241 static unsigned msgflags
= 0; /* what we've seen */
245 static int msgstate
= NORMAL
;
247 static time_t tclock
= 0; /* the time we started (more or less) */
249 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
251 static char tmpfil
[BUFSIZ
];
252 static char bccfil
[BUFSIZ
];
254 static char from
[BUFSIZ
]; /* my network address */
255 static char sender
[BUFSIZ
]; /* my Sender: header */
256 static char efrom
[BUFSIZ
]; /* my Envelope-From: header */
257 static char fullfrom
[BUFSIZ
]; /* full contents of From header */
258 static char *filter
= NULL
; /* the filter for BCC'ing */
259 static char *subject
= NULL
; /* the subject field for BCC'ing */
260 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
262 static struct headers
*hdrtab
; /* table for the message we're doing */
264 static struct mailname localaddrs
; /* local addrs */
265 static struct mailname netaddrs
; /* network addrs */
266 static struct mailname uuaddrs
; /* uucp addrs */
267 static struct mailname tmpaddrs
; /* temporary queue */
269 static int snoop
= 0;
270 static char *clientsw
= NULL
;
271 static char *serversw
= NULL
;
273 static char prefix
[] = "----- =_aaaaaaaaaa";
275 static char *partno
= NULL
;
280 static void putfmt (char *, char *, FILE *);
281 static void start_headers (void);
282 static void finish_headers (FILE *);
283 static int get_header (char *, struct headers
*);
284 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
285 static void putgrp (char *, char *, FILE *, unsigned int);
286 static int insert (struct mailname
*);
287 static void pl (void);
288 static void anno (void);
289 static int annoaux (struct mailname
*);
290 static void insert_fcc (struct headers
*, char *);
291 static void make_bcc_file (int);
292 static void verify_all_addresses (int, char *, int);
293 static void chkadr (void);
294 static void sigon (void);
295 static void sigoff (void);
296 static void p_refile (char *);
297 static void fcc (char *, char *);
298 static void die (char *, char *, ...);
299 static void post (char *, int, int, char *, int);
300 static void do_text (char *file
, int fd
);
301 static void do_an_address (struct mailname
*, int);
302 static void do_addresses (int, int);
303 static int find_prefix (void);
307 main (int argc
, char **argv
)
309 int state
, compnum
, dashstuff
= 0, swnum
, oauth_flag
= 0;
310 char *cp
, *msg
= NULL
, **argp
, **arguments
, *envelope
;
311 char buf
[BUFSIZ
], name
[NAMESZ
];
313 m_getfld_state_t gstate
= 0;
315 if (nmh_init(argv
[0], 0 /* use context_foil() */)) { return 1; }
317 mts_init (invo_name
);
318 arguments
= getarguments (invo_name
, argc
, argv
, 0);
321 while ((cp
= *argp
++)) {
323 switch ((swnum
= smatch (++cp
, switches
))) {
325 ambigsw (cp
, switches
);
328 adios (NULL
, "-%s unknown", cp
);
331 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
332 print_help (buf
, switches
, 0);
335 print_version(invo_name
);
339 if (!(cp
= *argp
++) || *cp
== '-')
340 adios (NULL
, "missing argument to %s", argp
[-2]);
341 /* create a minimal context */
342 if (context_foil (cp
) == -1)
347 if (!(cp
= *argp
++) || *cp
== '-')
348 adios (NULL
, "missing argument to %s", argp
[-2]);
349 if ((state
= alias (cp
)) != AK_OK
)
350 adios (NULL
, "aliasing error in %s - %s",
351 cp
, akerror (state
));
370 if (!(filter
= *argp
++) || *filter
== '-')
371 adios (NULL
, "missing argument to %s", argp
[-2]);
426 if (!(cp
= *argp
++) || *cp
== '-')
427 adios (NULL
, "missing argument to %s", argp
[-2]);
428 if ((outputlinelen
= atoi (cp
)) < 10)
429 adios (NULL
, "impossible width %d", outputlinelen
);
433 if (!(cp
= *argp
++) || *cp
== '-')
434 adios (NULL
, "missing argument to %s", argp
[-2]);
435 if ((pfd
= atoi (cp
)) <= 2)
436 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
440 if (!(clientsw
= *argp
++) || *clientsw
== '-')
441 adios (NULL
, "missing argument to %s", argp
[-2]);
444 if (!(serversw
= *argp
++) || *serversw
== '-')
445 adios (NULL
, "missing argument to %s", argp
[-2]);
452 if (!(partno
= *argp
++) || *partno
== '-')
453 adios (NULL
, "missing argument to %s", argp
[-2]);
465 if (!(cp
= *argp
++) || *cp
== '-')
466 adios (NULL
, "missing argument to %s", argp
[-2]);
471 if (!(saslmech
= *argp
++) || *saslmech
== '-')
472 adios (NULL
, "missing argument to %s", argp
[-2]);
475 case OAUTHCREDFILESW
:
485 if (!(cp
= *argp
++) || *cp
== '-')
486 adios (NULL
, "missing argument to %s", argp
[-2]);
488 for (i
= 0; oauthswitches
[i
].profname
!= NULL
; i
++) {
489 if (oauthswitches
[i
].switchnum
== swnum
) {
490 add_profile_entry(oauthswitches
[i
].profname
, cp
);
495 if (oauthswitches
[i
].profname
== NULL
)
496 adios (NULL
, "internal error: cannot map switch %s "
497 "to profile entry", argp
[-2]);
505 if (!(user
= *argp
++) || *user
== '-')
506 adios (NULL
, "missing argument to %s", argp
[-2]);
510 if (!(port
= *argp
++) || *port
== '-')
511 adios (NULL
, "missing argument to %s", argp
[-2]);
527 if (!(cp
= *argp
++) || *cp
== '-')
528 adios (NULL
, "missing argument to %s", argp
[-2]);
533 if (!(cp
= *argp
++) || *cp
== '-')
534 adios (NULL
, "missing argument to %s", argp
[-2]);
539 if (!(cp
= *argp
++) || *cp
== '-')
540 adios (NULL
, "missing argument to %s", argp
[-2]);
545 if (!(cp
= *argp
++) || *cp
== '-')
546 adios (NULL
, "missing argument to %s", argp
[-2]);
547 save_mts_method (cp
);
550 case CREDENTIALSSW
: {
551 if (!(cp
= *argp
++) || *cp
== '-')
552 adios (NULL
, "missing argument to %s", argp
[-2]);
553 add_profile_entry ("credentials", cp
);
558 if (!(cp
= *argp
++) || *cp
== '-')
559 adios (NULL
, "missing argument to %s", argp
[-2]);
560 if (save_message_id_style (cp
) != 0)
561 adios (NULL
, "unsupported messageid \"%s\"", cp
);
566 adios (NULL
, "only one message at a time!");
574 adios (NULL
, "usage: %s [switches] file", invo_name
);
576 if (outputlinelen
< 10)
577 adios (NULL
, "impossible width %d", outputlinelen
);
579 if ((in
= fopen (msg
, "r")) == NULL
)
580 adios (msg
, "unable to open");
588 if ((out
= fopen ("/dev/null", "w")) == NULL
)
589 adios ("/dev/null", "unable to open");
591 char *cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
593 adios(NULL
, "unable to create temporary file in %s",
596 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
600 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
602 for (compnum
= 1;;) {
603 int bufsz
= sizeof buf
;
604 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
608 cp
= add (buf
, NULL
);
609 while (state
== FLDPLUS
) {
611 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
614 putfmt (name
, cp
, out
);
619 finish_headers (out
);
622 fprintf (out
, "\n%s", buf
);
623 while (state
== BODY
) {
625 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
631 finish_headers (out
);
636 adios (NULL
, "message format error in component #%d", compnum
);
639 adios (NULL
, "getfld() returned %d", state
);
643 m_getfld_state_destroy (&gstate
);
657 * Here's how we decide which address to use as the envelope-from
660 * - If we were given an Envelope-From header, use that.
661 * - If we were given a Sender: address, use that.
662 * - Otherwise, use the address on the From: line
665 if (msgflags
& MEFM
) {
667 } else if (seensender
) {
676 * The user didn't specify any of the tls switches. Try to
677 * help them by implying -initialtls if they're using port 465
678 * (smtps, until IANA revoked that registration in 1998).
680 tls
= ! strcmp (port
, "465") || ! strcasecmp (port
, "smtps")
683 #else /* ! TLS_SUPPORT */
685 #endif /* ! TLS_SUPPORT */
688 /* If we are doing a "whom" check */
690 /* This won't work with MTS_SENDMAIL_PIPE. */
691 verify_all_addresses (1, envelope
, oauth_flag
);
695 if (msgflags
& MINV
) {
696 make_bcc_file (dashstuff
);
697 if (msgflags
& MVIS
) {
698 if (sm_mts
!= MTS_SENDMAIL_PIPE
) {
699 /* It would be nice to have support to call
700 verify_all_addresses with MTS_SENDMAIL_PIPE, but
701 that might require running sendmail as root. Note
702 that spost didn't verify addresses. */
703 verify_all_addresses (verbose
, envelope
, oauth_flag
);
705 post (tmpfil
, 0, verbose
, envelope
, oauth_flag
);
707 post (bccfil
, 1, verbose
, envelope
, oauth_flag
);
708 (void) m_unlink (bccfil
);
710 post (tmpfil
, 0, isatty (1), envelope
, oauth_flag
);
714 (void) m_unlink (tmpfil
);
718 printf ("Partial Message #%s Processed\n", partno
);
720 printf ("Message Processed\n");
733 putfmt (char *name
, char *str
, FILE *out
)
735 int count
, grp
, i
, keep
;
737 char namep
[BUFSIZ
], error
[BUFSIZ
];
738 struct mailname
*mp
= NULL
, *np
= NULL
;
741 while (*str
== ' ' || *str
== '\t')
744 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
745 advise (NULL
, "illegal header line -- %s:", name
);
750 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
751 if (strncasecmp (name
, "nmh-", 4)) {
752 fprintf (out
, "%s: %s", name
, str
);
754 /* Filter out all Nmh-* headers, because Norm asked. They
755 should never have reached this point. Warn about any
756 that are non-empty. */
757 if (strcmp (str
, "\n")) {
758 char *newline
= strchr (str
, '\n');
759 if (newline
) *newline
= '\0';
761 advise (NULL
, "ignoring header line -- %s: %s", name
, str
);
770 if (hdr
->flags
& HIGN
) {
773 if (hdr
->flags
& HBAD
) {
774 advise (NULL
, "illegal header line -- %s:", name
);
778 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
780 if (hdr
->flags
& HSUB
)
781 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
782 if (hdr
->flags
& HFCC
) {
783 if ((cp
= strrchr(str
, '\n')))
785 for (pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
787 insert_fcc (hdr
, pp
);
789 insert_fcc (hdr
, pp
);
793 if (!(hdr
->flags
& HADR
)) {
794 fprintf (out
, "%s: %s", name
, str
);
798 tmpaddrs
.m_next
= NULL
;
799 for (count
= 0; (cp
= getname (str
)); count
++)
800 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
)))) {
804 tmpaddrs
.m_next
= mp
;
808 admonish(cp
, "%s", error
);
809 if (hdr
->flags
& HTRY
)
816 if (hdr
->flags
& HNIL
)
817 fprintf (out
, "%s: %s", name
, str
);
820 * Sender (or Resent-Sender) can have only one address
822 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
823 : (hdr
->set
& MSND
)) {
824 advise (NULL
, "%s: field requires one address", name
);
828 advise (NULL
, "%s: field requires at least one address", name
);
835 if (count
> 1 && (hdr
->flags
& HONE
)) {
836 advise (NULL
, "%s: field only permits one address", name
);
841 nameoutput
= linepos
= 0;
842 snprintf (namep
, sizeof(namep
), "%s%s",
843 (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
845 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
846 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
847 /* The address doesn't include a host, so it might be an alias. */
848 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
849 qp
= akvisible () ? mp
->m_mbox
: "";
852 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
853 while ((cp
= getname (pp
))) {
854 if (!(mp
= getm (cp
, NULL
, 0, error
, sizeof(error
)))) {
855 admonish(cp
, "%s", error
);
861 * If it's a From: or Resent-From: header, save the address
862 * for later possible use (as the envelope address for SMTP)
865 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
866 : (hdr
->set
& MFRM
)) {
867 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
868 from
[sizeof(from
) - 1] = '\0';
873 * Also save the Sender: or Resent-Sender: header as well
876 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
877 : (hdr
->set
& MSND
)) {
878 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
879 sender
[sizeof(sender
) - 1] = '\0';
884 * ALSO ... save Envelope-From
887 if (hdr
->set
& MEFM
) {
888 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
889 efrom
[sizeof(efrom
) - 1] = '\0';
892 if (hdr
->flags
& HBCC
)
895 mp
->m_ingrp
= np
->m_ingrp
;
898 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
900 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
901 /* Catch this before sendmail chokes with:
902 "553 List:; syntax illegal for recipient
904 If we wanted to, we could expand out blind
905 aliases and put them in Bcc:, but then
906 they'd have the Blind-Carbon-Copy
909 "blind lists not compatible with"
915 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
916 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
925 /* Address includes a host, so no alias substitution is needed. */
928 * If it's a From: or Resent-From header, save the address
929 * for later possible use (as the envelope address for SMTP)
932 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
933 : (hdr
->set
& MFRM
)) {
934 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
939 * Also save the Sender: header as well
942 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
943 : (hdr
->set
& MSND
)) {
944 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
945 sender
[sizeof(sender
) - 1] = '\0';
950 * ALSO ... save Envelope-From
953 if (hdr
->set
& MEFM
) {
954 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
955 efrom
[sizeof(efrom
) - 1] = '\0';
958 if (hdr
->flags
& HBCC
)
961 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
964 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
968 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
975 * If this is a From:/Resent-From: header, save the full thing for
976 * later in case we need it for use when constructing a Bcc draft message
979 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
) : (hdr
->set
& MFRM
)) {
980 strncpy(fullfrom
, str
, sizeof(fullfrom
));
981 fullfrom
[sizeof(fullfrom
) - 1] = 0;
983 * Strip off any trailing newlines
986 while (strlen(fullfrom
) > 0 && fullfrom
[strlen(fullfrom
) - 1] == '\n') {
987 fullfrom
[strlen(fullfrom
) - 1] = '\0';
991 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
992 advise (NULL
, "%s: field does not allow groups", name
);
1002 start_headers (void)
1007 * Probably not necessary, but just in case ...
1018 * Now that we've outputted the header fields in the draft
1019 * message, we will now output any remaining header fields
1020 * that we need to add/create.
1024 finish_headers (FILE *out
)
1028 if (!(msgflags
& MFRM
)) {
1030 * A From: header is now required in the draft.
1032 advise (NULL
, "message has no From: header");
1033 advise (NULL
, "See default components files for examples");
1038 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
1039 advise (NULL
, "A Sender: or Envelope-From: header is required "
1040 "with multiple\nFrom: addresses");
1048 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1050 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1052 * If we have multiple From: addresses, make sure we have an
1053 * Sender: header. If we don't have one, then generate one
1054 * from Envelope-From: (which in this case, cannot be blank)
1057 if (fromcount
> 1 && seensender
== 0) {
1058 if (efrom
[0] == '\0') {
1059 advise (NULL
, "Envelope-From cannot be blank when there "
1060 "is multiple From: addresses\nand no Sender: "
1064 fprintf (out
, "Sender: %s\n", efrom
);
1068 if (!(msgflags
& MVIS
))
1069 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
1073 if (!(msgflags
& MDAT
)) {
1074 advise (NULL
, "message has no Date: header");
1077 if (!(msgflags
& MFRM
)) {
1078 advise (NULL
, "message has no From: header");
1081 if (!(msgflags
& MRFM
)) {
1082 advise (NULL
, "message has no Resent-From: header");
1083 advise (NULL
, "See default components files for examples");
1087 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
1088 advise (NULL
, "A Resent-Sender: or Envelope-From: header is "
1089 "required with multiple\nResent-From: addresses");
1097 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
1099 fprintf (out
, "Resent-Message-ID: %s\n",
1100 message_id (tclock
, 0));
1102 * If we have multiple Resent-From: addresses, make sure we have an
1103 * Resent-Sender: header. If we don't have one, then generate one
1104 * from Envelope-From (which in this case, cannot be blank)
1107 if (fromcount
> 1 && seensender
== 0) {
1108 if (efrom
[0] == '\0') {
1109 advise (NULL
, "Envelope-From cannot be blank when there "
1110 "is multiple Resent-From: addresses and no "
1111 "Resent-Sender: header");
1114 fprintf (out
, "Resent-Sender: %s\n", efrom
);
1118 if (!(msgflags
& MVIS
))
1119 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1124 adios (NULL
, "re-format message and try again");
1126 adios (NULL
, "no addressees");
1131 get_header (char *header
, struct headers
*table
)
1135 for (h
= table
; h
->value
; h
++)
1136 if (!strcasecmp (header
? header
: "", h
->value
? h
->value
: ""))
1144 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
1148 char buffer
[BUFSIZ
];
1150 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
1152 if (sm_mts
!= MTS_SENDMAIL_PIPE
&&
1153 ((flags
& (HBCC
| HDCC
| HEFM
)) || mp
->m_ingrp
))
1157 fprintf (out
, "%s: ", name
);
1158 linepos
+= (nameoutput
= strlen (name
) + 2);
1161 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1162 mp
->m_pers
= getcpy (aka
);
1165 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1168 cp
= adrformat (mp
);
1175 if (linepos
!= nameoutput
) {
1176 if (len
+ linepos
+ 2 > outputlinelen
)
1177 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1187 return (flags
& HTRY
);
1192 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1197 if (sm_mts
!= MTS_SENDMAIL_PIPE
&& (flags
& HBCC
))
1201 fprintf (out
, "%s: ", name
);
1202 linepos
+= (nameoutput
= strlen (name
) + 2);
1205 cp
= concat (group
, ";", NULL
);
1208 if (linepos
> nameoutput
) {
1209 if (len
+ linepos
+ 2 > outputlinelen
) {
1210 fprintf (out
, ",\n%*s", nameoutput
, "");
1211 linepos
= nameoutput
;
1225 insert (struct mailname
*np
)
1227 struct mailname
*mp
;
1229 if (np
->m_mbox
== NULL
)
1232 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1233 : np
->m_type
== UUCPHOST
? &uuaddrs
1237 if (!strcasecmp (np
->m_host
? np
->m_host
: "",
1238 mp
->m_next
->m_host
? mp
->m_next
->m_host
: "") &&
1239 !strcasecmp (np
->m_mbox
? np
->m_mbox
: "",
1240 mp
->m_next
->m_mbox
? mp
->m_next
->m_mbox
: "") &&
1241 np
->m_bcc
== mp
->m_next
->m_bcc
)
1254 struct mailname
*mp
;
1256 printf ("-------\n\t-- Addresses --\nlocal:\t");
1257 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1258 printf ("%s%s%s", mp
->m_mbox
,
1259 mp
->m_bcc
? "[BCC]" : "",
1260 mp
->m_next
? ",\n\t" : "");
1262 printf ("\nnet:\t");
1263 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1264 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1265 mp
->m_mbox
, mp
->m_host
,
1266 mp
->m_bcc
? "[BCC]" : "",
1267 mp
->m_next
? ",\n\t" : "");
1269 printf ("\nuucp:\t");
1270 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1271 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1272 mp
->m_bcc
? "[BCC]" : "",
1273 mp
->m_next
? ",\n\t" : "");
1275 printf ("\n\t-- Folder Copies --\nfcc:\t");
1276 for (i
= 0; i
< fccind
; i
++)
1277 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1285 struct mailname
*mp
;
1287 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1288 if (annoaux (mp
) == NOTOK
)
1291 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1292 if (annoaux (mp
) == NOTOK
)
1295 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1296 if (annoaux (mp
) == NOTOK
)
1306 annoaux (struct mailname
*mp
)
1309 char buffer
[BUFSIZ
];
1311 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1312 i
= strlen (buffer
);
1314 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1319 insert_fcc (struct headers
*hdr
, char *pp
)
1323 for (cp
= pp
; isspace ((unsigned char) *cp
); cp
++)
1325 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace ((unsigned char) *pp
); pp
--)
1333 adios (NULL
, "too many %ss", hdr
->value
);
1334 fccfold
[fccind
++] = getcpy (cp
);
1342 make_bcc_file (int dashstuff
)
1348 char *tfile
= NULL
, *program
;
1350 if ((tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
)) == NULL
) {
1351 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
1353 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1355 fprintf (out
, "From: %s\n", fullfrom
);
1356 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1358 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1360 fprintf (out
, "Subject: %s", subject
);
1361 fprintf (out
, "BCC:\n");
1364 * Use MIME encapsulation for Bcc messages
1370 * Check if any lines in the message clash with the
1371 * prefix for the MIME multipart separator. If there
1372 * is a clash, increment one of the letters in the
1373 * prefix and check again.
1375 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1376 adios (NULL
, "lost prefix start");
1377 while (find_prefix () == NOTOK
) {
1382 adios (NULL
, "can't find a unique delimiter string");
1387 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1388 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1389 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1391 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1397 * Do mhl filtering of Bcc messages instead
1398 * of MIME encapsulation.
1400 if (filter
!= NULL
) {
1401 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1405 adios ("fork", "unable to");
1408 dup2 (fileno (out
), 1);
1410 vec
= argsplit(mhlproc
, &program
, &i
);
1411 vec
[i
++] = "-forward";
1416 /* was the flag -[no]dashstuffing specified? */
1418 vec
[i
++] = "-dashstuffing";
1419 else if (dashstuff
< 0)
1420 vec
[i
++] = "-nodashstuffing";
1423 execvp (program
, vec
);
1424 fprintf (stderr
, "unable to exec ");
1429 pidXwait (child_id
, mhlproc
);
1433 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1434 adios (tmpfil
, "unable to re-open");
1437 * If using MIME encapsulation, or if the -nodashstuffing
1438 * flag was given, then just copy message. Else do
1439 * RFC934 quoting (dashstuffing).
1441 if (mime
|| dashstuff
< 0)
1442 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1444 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1448 fseek (out
, 0L, SEEK_END
);
1450 fprintf (out
, "\n--%s--\n", prefix
);
1452 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1458 * Scan message to check if any lines clash with
1459 * the prefix of the MIME multipart separator.
1466 char buffer
[BUFSIZ
];
1469 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1470 adios (tmpfil
, "unable to re-open");
1472 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1473 if (buffer
[0] == '-' && buffer
[1] == '-') {
1476 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1477 if (!isspace ((unsigned char) *cp
))
1480 if (strcmp (buffer
+ 2, prefix
) == 0) {
1491 #define plural(x) (x == 1 ? "" : "s")
1496 if (badadr
&& unkadr
)
1497 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1498 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1500 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1502 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1507 do_addresses (int bccque
, int talk
)
1511 struct mailname
*lp
;
1514 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1515 if (lp
->m_bcc
? bccque
: !bccque
) {
1517 printf (" -- Local Recipients --\n");
1518 do_an_address (lp
, talk
);
1523 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1524 if (lp
->m_bcc
? bccque
: !bccque
) {
1526 printf (" -- UUCP Recipients --\n");
1527 do_an_address (lp
, talk
);
1532 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1533 if (lp
->m_bcc
? bccque
: !bccque
) {
1535 printf (" -- Network Recipients --\n");
1536 do_an_address (lp
, talk
);
1542 if (rp_isbad (retval
= sm_waend ()))
1543 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1548 * MTS-SPECIFIC INTERACTION
1553 * SENDMAIL/SMTP routines
1557 post (char *file
, int bccque
, int talk
, char *envelope
, int oauth_flag
)
1564 if (msgflags
& MINV
)
1565 printf (" -- Posting for %s Recipients --\n",
1566 bccque
? "Blind" : "Sighted");
1568 printf (" -- Posting for All Recipients --\n");
1573 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
1574 char **argp
, *program
;
1577 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1581 adios ("fork", "unable to");
1584 if (freopen( file
, "r", stdin
) == NULL
) {
1585 adios (file
, "can't reopen for sendmail");
1588 argp
= argsplit(sendmail
, &program
, &argc
);
1589 argp
[argc
++] = "-t"; /* read msg for recipients */
1590 argp
[argc
++] = "-i"; /* don't stop on "." */
1592 argp
[argc
++] = "-bv";
1594 argp
[argc
++] = "-v";
1597 execv (program
, argp
);
1598 adios (sendmail
, "can't exec");
1601 pidXwait (child_id
, NULL
);
1605 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1606 verbose
, snoop
, sasl
, saslssf
,
1608 oauth_flag
? "post" : NULL
, tls
))
1609 || rp_isbad (retval
= sm_winit (envelope
)))
1610 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1612 do_addresses (bccque
, talk
&& verbose
);
1613 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1614 die (file
, "unable to re-open");
1619 sm_end (!(msgflags
& MINV
) || bccque
? OK
: DONE
);
1623 if (msgflags
& MINV
)
1624 printf (" -- %s Recipient Copies Posted --\n",
1625 bccque
? "Blind" : "Sighted");
1627 printf (" -- Recipient Copies Posted --\n");
1635 /* Address Verification */
1638 verify_all_addresses (int talk
, char *envelope
, int oauth_flag
)
1641 struct mailname
*lp
;
1645 if (!whomsw
|| checksw
)
1646 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1647 verbose
, snoop
, sasl
, saslssf
,
1649 oauth_flag
? "post" : NULL
, tls
))
1650 || rp_isbad (retval
= sm_winit (envelope
)))
1651 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1653 if (talk
&& !whomsw
)
1654 printf (" -- Address Verification --\n");
1655 if (talk
&& localaddrs
.m_next
)
1656 printf (" -- Local Recipients --\n");
1657 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1658 do_an_address (lp
, talk
);
1660 if (talk
&& uuaddrs
.m_next
)
1661 printf (" -- UUCP Recipients --\n");
1662 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1663 do_an_address (lp
, talk
);
1665 if (talk
&& netaddrs
.m_next
)
1666 printf (" -- Network Recipients --\n");
1667 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1668 do_an_address (lp
, talk
);
1671 if (talk
&& !whomsw
)
1672 printf (" -- Address Verification Successful --\n");
1674 if (!whomsw
|| checksw
)
1683 do_an_address (struct mailname
*lp
, int talk
)
1689 switch (lp
->m_type
) {
1693 strncpy (addr
, mbox
, sizeof(addr
));
1697 mbox
= auxformat (lp
, 0);
1699 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1702 default: /* let SendMail decide if the host is bad */
1705 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1710 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1712 if (whomsw
&& !checksw
) {
1720 switch (retval
= sm_wadr (mbox
, host
,
1721 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1724 printf ("address ok\n");
1730 fprintf (stderr
, " %s: ", addr
);
1731 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1732 rp_string (retval
));
1738 fprintf (stderr
, " %s: ", addr
);
1739 die (NULL
, "unexpected response; %s", rp_string (retval
));
1747 do_text (char *file
, int fd
)
1752 lseek (fd
, (off_t
) 0, SEEK_SET
);
1754 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1755 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1756 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1760 die (file
, "problem reading from");
1762 switch (retval
= sm_wtend ()) {
1768 die (NULL
, "posting failed; %s", rp_string (retval
));
1771 die (NULL
, "unexpected response; %s", rp_string (retval
));
1785 (void) m_unlink (tmpfil
);
1786 if (msgflags
& MINV
)
1787 (void) m_unlink (bccfil
);
1789 if (!whomsw
|| checksw
)
1802 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1803 istat
= SIGNAL2 (SIGINT
, sigser
);
1804 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1805 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1815 SIGNAL (SIGHUP
, hstat
);
1816 SIGNAL (SIGINT
, istat
);
1817 SIGNAL (SIGQUIT
, qstat
);
1818 SIGNAL (SIGTERM
, tstat
);
1826 p_refile (char *file
)
1834 printf (" -- Filing Folder Copies --\n");
1835 for (i
= 0; i
< fccind
; i
++)
1836 fcc (file
, fccfold
[i
]);
1838 printf (" -- Folder Copies Filed --\n");
1843 * Call the `fileproc' to add the file to the folder.
1847 fcc (char *file
, char *folder
)
1850 int i
, status
, argp
;
1852 char **arglist
, *program
;
1855 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1858 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1864 fprintf (stderr
, " %sFcc %s: ",
1865 msgstate
== RESENT
? "Resent-" : "", folder
);
1866 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1870 /* see if we need to add `+' */
1871 snprintf (fold
, sizeof(fold
), "%s%s",
1872 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1874 /* now exec the fileproc */
1876 arglist
= argsplit(fileproc
, &program
, &argp
);
1877 arglist
[argp
++] = "-link";
1878 arglist
[argp
++] = "-file";
1879 arglist
[argp
++] = file
;
1880 arglist
[argp
++] = fold
;
1881 arglist
[argp
] = NULL
;
1882 execvp (program
, arglist
);
1886 if ((status
= pidwait (child_id
, OK
))) {
1888 fprintf (stderr
, " %sFcc %s: ",
1889 msgstate
== RESENT
? "Resent-" : "", folder
);
1890 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1893 printf ("folder ok\n");
1905 die (char *what
, char *fmt
, ...)
1909 (void) m_unlink (tmpfil
);
1910 if (msgflags
& MINV
)
1911 (void) m_unlink (bccfil
);
1913 if (!whomsw
|| checksw
)
1917 advertise (what
, NULL
, fmt
, ap
);