]>
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 */
40 #define FCCS 10 /* max number of fccs allowed */
42 /* In the following array of structures, the numeric second field of the
43 structures (minchars) is apparently used like this:
45 -# : Switch can be abbreviated to # characters; switch hidden in -help.
46 0 : Switch can't be abbreviated; switch shown in -help.
47 # : Switch can be abbreviated to # characters; switch shown in -help. */
49 #define POST_SWITCHES \
50 X("alias aliasfile", 0, ALIASW) \
51 X("check", -5, CHKSW) /* interface from whom */ \
52 X("nocheck", -7, NCHKSW) /* interface from whom */ \
53 X("debug", -5, DEBUGSW) \
54 X("dist", -4, DISTSW) /* interface from dist */ \
55 X("filter filterfile", 0, FILTSW) \
56 X("nofilter", 0, NFILTSW) \
57 X("format", 0, FRMTSW) \
58 X("noformat", 0, NFRMTSW) \
59 X("library directory", -7, LIBSW) /* interface from send, whom */ \
60 X("mime", 0, MIMESW) \
61 X("nomime", 0, NMIMESW) \
62 X("msgid", 0, MSGDSW) \
63 X("nomsgid", 0, NMSGDSW) \
64 X("verbose", 0, VERBSW) \
65 X("noverbose", 0, NVERBSW) \
66 X("watch", 0, WATCSW) \
67 X("nowatch", 0, NWATCSW) \
68 X("whom", -4, WHOMSW) /* interface from whom */ \
69 X("width columns", 0, WIDTHSW) \
70 X("version", 0, VERSIONSW) \
71 X("help", 0, HELPSW) \
72 X("dashstuffing", -12, BITSTUFFSW) /* should we dashstuff BCC messages? */ \
73 X("nodashstuffing", -14, NBITSTUFFSW) \
74 X("idanno number", -6, ANNOSW) /* interface from send */ \
75 X("client host", -6, CLIESW) \
76 X("server host", 6, SERVSW) /* specify alternate SMTP server */ \
77 X("snoop", -5, SNOOPSW) /* snoop the SMTP transaction */ \
78 X("partno", -6, PARTSW) \
79 X("queued", -6, QUEUESW) \
80 X("sasl", SASLminc(-4), SASLSW) \
81 X("nosasl", SASLminc(-6), NOSASLSW) \
82 X("saslmaxssf", SASLminc(-10), SASLMXSSFSW) \
83 X("saslmech", SASLminc(-5), SASLMECHSW) \
84 X("user", SASLminc(-4), USERSW) \
85 X("port server port name/number", 4, PORTSW) \
86 X("tls", TLSminc(-3), TLSSW) \
87 X("initialtls", TLSminc(-10), INITTLSSW) \
88 X("notls", TLSminc(-5), NTLSSW) \
89 X("fileproc", -4, FILEPROCSW) \
90 X("mhlproc", -3, MHLPROCSW) \
91 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
92 X("credentials legacy|file:filename", 0, CREDENTIALSSW) \
93 X("messageid localname|random", 2, MESSAGEIDSW) \
95 #define X(sw, minchars, id) id,
96 DEFINE_SWITCH_ENUM(POST
);
99 #define X(sw, minchars, id) { sw, minchars, id },
100 DEFINE_SWITCH_ARRAY(POST
, switches
);
111 * flags for headers->flags
113 #define HNOP 0x0000 /* just used to keep .set around */
114 #define HBAD 0x0001 /* bad header - don't let it through */
115 #define HADR 0x0002 /* header has an address field */
116 #define HSUB 0x0004 /* Subject: header */
117 #define HTRY 0x0008 /* try to send to addrs on header */
118 #define HBCC 0x0010 /* don't output this header, unless MTS_SENDMAIL_PIPE */
119 #define HMNG 0x0020 /* munge this header */
120 #define HNGR 0x0040 /* no groups allowed in this header */
121 #define HFCC 0x0080 /* FCC: type header */
122 #define HNIL 0x0100 /* okay for this header not to have addrs */
123 #define HIGN 0x0200 /* ignore this header */
124 #define HDCC 0x0400 /* another undocumented feature */
125 #define HONE 0x0800 /* Only (zero or) one address allowed */
126 #define HEFM 0x1000 /* Envelope-From: header */
129 * flags for headers->set
131 #define MFRM 0x0001 /* we've seen a From: */
132 #define MDAT 0x0002 /* we've seen a Date: */
133 #define MRFM 0x0004 /* we've seen a Resent-From: */
134 #define MVIS 0x0008 /* we've seen sighted addrs */
135 #define MINV 0x0010 /* we've seen blind addrs */
136 #define MSND 0x0020 /* we've seen a Sender: */
137 #define MRSN 0x0040 /* We've seen a Resent-Sendr:*/
138 #define MEFM 0x0080 /* We've seen Envelope-From: */
141 static struct headers NHeaders
[] = {
142 { "Return-Path", HBAD
, 0 },
143 { "Received", HBAD
, 0 },
144 { "Reply-To", HADR
|HNGR
, 0 },
145 { "From", HADR
|HNGR
, MFRM
},
146 { "Sender", HADR
|HNGR
|HONE
, MSND
},
148 { "Subject", HSUB
, 0 },
149 { "To", HADR
|HTRY
, MVIS
},
150 { "cc", HADR
|HTRY
, MVIS
},
151 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
152 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
153 { "Message-ID", HBAD
, 0 },
155 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
159 static struct headers RHeaders
[] = {
160 { "Resent-Reply-To", HADR
|HNGR
, 0 },
161 { "Resent-From", HADR
|HNGR
, MRFM
},
162 { "Resent-Sender", HADR
|HNGR
, MRSN
},
163 { "Resent-Date", HBAD
, 0 },
164 { "Resent-Subject", HSUB
, 0 },
165 { "Resent-To", HADR
|HTRY
, MVIS
},
166 { "Resent-cc", HADR
|HTRY
, MVIS
},
167 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
168 { "Resent-Message-ID", HBAD
, 0 },
169 { "Resent-Fcc", HFCC
, 0 },
170 { "Reply-To", HADR
, 0 },
171 { "From", HADR
|HNGR
, MFRM
},
172 { "Sender", HADR
|HNGR
, MSND
},
173 { "Date", HNOP
, MDAT
},
174 { "To", HADR
|HNIL
, 0 },
175 { "cc", HADR
|HNIL
, 0 },
176 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
178 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
182 static short fccind
= 0; /* index into fccfold[] */
183 static short outputlinelen
= OUTPUTLINELEN
;
185 static int pfd
= NOTOK
; /* fd to write annotation list to */
186 static int recipients
= 0; /* how many people will get a copy */
187 static int unkadr
= 0; /* how many of those were unknown */
188 static int badadr
= 0; /* number of bad addrs */
189 static int badmsg
= 0; /* message has bad semantics */
190 static int verbose
= 0; /* spell it out */
191 static int format
= 1; /* format addresses */
192 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
193 static int msgid
= 0; /* add msgid */
194 static int debug
= 0; /* debugging post */
195 static int watch
= 0; /* watch the delivery process */
196 static int whomsw
= 0; /* we are whom not post */
197 static int checksw
= 0; /* whom -check */
198 static int linepos
=0; /* putadr()'s position on the line */
199 static int nameoutput
=0; /* putadr() has output header name */
200 static int sasl
=0; /* Use SASL auth for SMTP */
201 static int saslssf
=-1; /* Our maximum SSF for SASL */
202 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
203 static char *user
=NULL
; /* Authenticate as this user */
204 static char *port
="smtp"; /* Name of server port for SMTP */
205 static int tls
=0; /* Use TLS for encryption */
206 static int fromcount
=0; /* Count of addresses on From: header */
207 static int seensender
=0; /* Have we seen a Sender: header? */
209 static unsigned msgflags
= 0; /* what we've seen */
213 static int msgstate
= NORMAL
;
215 static time_t tclock
= 0; /* the time we started (more or less) */
217 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
219 static char tmpfil
[BUFSIZ
];
220 static char bccfil
[BUFSIZ
];
222 static char from
[BUFSIZ
]; /* my network address */
223 static char sender
[BUFSIZ
]; /* my Sender: header */
224 static char efrom
[BUFSIZ
]; /* my Envelope-From: header */
225 static char fullfrom
[BUFSIZ
]; /* full contents of From header */
226 static char *filter
= NULL
; /* the filter for BCC'ing */
227 static char *subject
= NULL
; /* the subject field for BCC'ing */
228 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
230 static struct headers
*hdrtab
; /* table for the message we're doing */
232 static struct mailname localaddrs
; /* local addrs */
233 static struct mailname netaddrs
; /* network addrs */
234 static struct mailname uuaddrs
; /* uucp addrs */
235 static struct mailname tmpaddrs
; /* temporary queue */
237 static int snoop
= 0;
238 static char *clientsw
= NULL
;
239 static char *serversw
= NULL
;
241 extern struct smtp sm_reply
;
243 static char prefix
[] = "----- =_aaaaaaaaaa";
245 static char *partno
= NULL
;
246 static int queued
= 0;
251 static void putfmt (char *, char *, FILE *);
252 static void start_headers (void);
253 static void finish_headers (FILE *);
254 static int get_header (char *, struct headers
*);
255 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
256 static void putgrp (char *, char *, FILE *, unsigned int);
257 static int insert (struct mailname
*);
258 static void pl (void);
259 static void anno (void);
260 static int annoaux (struct mailname
*);
261 static void insert_fcc (struct headers
*, char *);
262 static void make_bcc_file (int);
263 static void verify_all_addresses (int, char *);
264 static void chkadr (void);
265 static void sigon (void);
266 static void sigoff (void);
267 static void p_refile (char *);
268 static void fcc (char *, char *);
269 static void die (char *, char *, ...);
270 static void post (char *, int, int, char *);
271 static void do_text (char *file
, int fd
);
272 static void do_an_address (struct mailname
*, int);
273 static void do_addresses (int, int);
274 static int find_prefix (void);
278 main (int argc
, char **argv
)
280 int state
, compnum
, dashstuff
= 0;
281 char *cp
, *msg
= NULL
, **argp
, **arguments
, *envelope
;
282 char buf
[BUFSIZ
], name
[NAMESZ
];
284 m_getfld_state_t gstate
= 0;
286 if (nmh_init(argv
[0], 0 /* use context_foil() */)) { return 1; }
288 mts_init (invo_name
);
289 arguments
= getarguments (invo_name
, argc
, argv
, 0);
292 while ((cp
= *argp
++)) {
294 switch (smatch (++cp
, switches
)) {
296 ambigsw (cp
, switches
);
299 adios (NULL
, "-%s unknown", cp
);
302 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
303 print_help (buf
, switches
, 0);
306 print_version(invo_name
);
310 if (!(cp
= *argp
++) || *cp
== '-')
311 adios (NULL
, "missing argument to %s", argp
[-2]);
312 /* create a minimal context */
313 if (context_foil (cp
) == -1)
318 if (!(cp
= *argp
++) || *cp
== '-')
319 adios (NULL
, "missing argument to %s", argp
[-2]);
320 if ((state
= alias (cp
)) != AK_OK
)
321 adios (NULL
, "aliasing error in %s - %s",
322 cp
, akerror (state
));
341 if (!(filter
= *argp
++) || *filter
== '-')
342 adios (NULL
, "missing argument to %s", argp
[-2]);
397 if (!(cp
= *argp
++) || *cp
== '-')
398 adios (NULL
, "missing argument to %s", argp
[-2]);
399 if ((outputlinelen
= atoi (cp
)) < 10)
400 adios (NULL
, "impossible width %d", outputlinelen
);
404 if (!(cp
= *argp
++) || *cp
== '-')
405 adios (NULL
, "missing argument to %s", argp
[-2]);
406 if ((pfd
= atoi (cp
)) <= 2)
407 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
411 if (!(clientsw
= *argp
++) || *clientsw
== '-')
412 adios (NULL
, "missing argument to %s", argp
[-2]);
415 if (!(serversw
= *argp
++) || *serversw
== '-')
416 adios (NULL
, "missing argument to %s", argp
[-2]);
423 if (!(partno
= *argp
++) || *partno
== '-')
424 adios (NULL
, "missing argument to %s", argp
[-2]);
440 if (!(cp
= *argp
++) || *cp
== '-')
441 adios (NULL
, "missing argument to %s", argp
[-2]);
446 if (!(saslmech
= *argp
++) || *saslmech
== '-')
447 adios (NULL
, "missing argument to %s", argp
[-2]);
451 if (!(user
= *argp
++) || *user
== '-')
452 adios (NULL
, "missing argument to %s", argp
[-2]);
456 if (!(port
= *argp
++) || *port
== '-')
457 adios (NULL
, "missing argument to %s", argp
[-2]);
473 if (!(cp
= *argp
++) || *cp
== '-')
474 adios (NULL
, "missing argument to %s", argp
[-2]);
479 if (!(cp
= *argp
++) || *cp
== '-')
480 adios (NULL
, "missing argument to %s", argp
[-2]);
485 if (!(cp
= *argp
++) || *cp
== '-')
486 adios (NULL
, "missing argument to %s", argp
[-2]);
487 save_mts_method (cp
);
490 case CREDENTIALSSW
: {
491 if (!(cp
= *argp
++) || *cp
== '-')
492 adios (NULL
, "missing argument to %s", argp
[-2]);
493 add_profile_entry ("credentials", cp
);
498 if (!(cp
= *argp
++) || *cp
== '-')
499 adios (NULL
, "missing argument to %s", argp
[-2]);
500 if (save_message_id_style (cp
) != 0)
501 adios (NULL
, "unsupported messageid \"%s\"", cp
);
506 adios (NULL
, "only one message at a time!");
514 adios (NULL
, "usage: %s [switches] file", invo_name
);
516 if (outputlinelen
< 10)
517 adios (NULL
, "impossible width %d", outputlinelen
);
519 if ((in
= fopen (msg
, "r")) == NULL
)
520 adios (msg
, "unable to open");
528 if ((out
= fopen ("/dev/null", "w")) == NULL
)
529 adios ("/dev/null", "unable to open");
531 char *cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
533 adios(NULL
, "unable to create temporary file in %s",
536 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
540 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
542 for (compnum
= 1;;) {
543 int bufsz
= sizeof buf
;
544 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
548 cp
= add (buf
, NULL
);
549 while (state
== FLDPLUS
) {
551 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
554 putfmt (name
, cp
, out
);
559 finish_headers (out
);
562 fprintf (out
, "\n%s", buf
);
563 while (state
== BODY
) {
565 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
571 finish_headers (out
);
576 adios (NULL
, "message format error in component #%d", compnum
);
579 adios (NULL
, "getfld() returned %d", state
);
583 m_getfld_state_destroy (&gstate
);
597 * Here's how we decide which address to use as the envelope-from
600 * - If we were given an Envelope-From header, use that.
601 * - If we were given a Sender: address, use that.
602 * - Otherwise, use the address on the From: line
605 if (msgflags
& MEFM
) {
607 } else if (seensender
) {
613 /* If we are doing a "whom" check */
615 /* This won't work with MTS_SENDMAIL_PIPE. */
616 verify_all_addresses (1, envelope
);
620 if (msgflags
& MINV
) {
621 make_bcc_file (dashstuff
);
622 if (msgflags
& MVIS
) {
623 if (sm_mts
!= MTS_SENDMAIL_PIPE
) {
624 /* It would be nice to have support to call
625 verify_all_addresses with MTS_SENDMAIL_PIPE, but
626 that might require running sendmail as root. Note
627 that spost didn't verify addresses. */
628 verify_all_addresses (verbose
, envelope
);
630 post (tmpfil
, 0, verbose
, envelope
);
632 post (bccfil
, 1, verbose
, envelope
);
633 (void) m_unlink (bccfil
);
635 post (tmpfil
, 0, isatty (1), envelope
);
639 (void) m_unlink (tmpfil
);
643 printf ("Partial Message #%s Processed\n", partno
);
645 printf ("Message Processed\n");
658 putfmt (char *name
, char *str
, FILE *out
)
660 int count
, grp
, i
, keep
;
662 char namep
[BUFSIZ
], error
[BUFSIZ
];
663 struct mailname
*mp
= NULL
, *np
= NULL
;
666 while (*str
== ' ' || *str
== '\t')
669 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
670 advise (NULL
, "illegal header line -- %s:", name
);
675 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
676 if (strncasecmp (name
, "nmh-", 4)) {
677 fprintf (out
, "%s: %s", name
, str
);
679 /* Filter out all Nmh-* headers, because Norm asked. They
680 should never have reached this point. Warn about any
681 that are non-empty. */
682 if (strcmp (str
, "\n")) {
683 char *newline
= strchr (str
, '\n');
684 if (newline
) *newline
= '\0';
686 advise (NULL
, "ignoring header line -- %s: %s", name
, str
);
695 if (hdr
->flags
& HIGN
) {
698 if (hdr
->flags
& HBAD
) {
699 advise (NULL
, "illegal header line -- %s:", name
);
703 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
705 if (hdr
->flags
& HSUB
)
706 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
707 if (hdr
->flags
& HFCC
) {
708 if ((cp
= strrchr(str
, '\n')))
710 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
712 insert_fcc (hdr
, pp
);
714 insert_fcc (hdr
, pp
);
718 if (!(hdr
->flags
& HADR
)) {
719 fprintf (out
, "%s: %s", name
, str
);
723 tmpaddrs
.m_next
= NULL
;
724 for (count
= 0; (cp
= getname (str
)); count
++)
725 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
)))) {
729 tmpaddrs
.m_next
= mp
;
733 admonish(cp
, "%s", error
);
734 if (hdr
->flags
& HTRY
)
741 if (hdr
->flags
& HNIL
)
742 fprintf (out
, "%s: %s", name
, str
);
745 * Sender (or Resent-Sender) can have only one address
747 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
748 : (hdr
->set
& MSND
)) {
749 advise (NULL
, "%s: field requires one address", name
);
753 advise (NULL
, "%s: field requires at least one address", name
);
760 if (count
> 1 && (hdr
->flags
& HONE
)) {
761 advise (NULL
, "%s: field only permits one address", name
);
766 nameoutput
= linepos
= 0;
767 snprintf (namep
, sizeof(namep
), "%s%s",
768 (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
770 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
771 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
772 /* The address doesn't include a host, so it might be an alias. */
773 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
774 qp
= akvisible () ? mp
->m_mbox
: "";
777 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
778 while ((cp
= getname (pp
))) {
779 if (!(mp
= getm (cp
, NULL
, 0, error
, sizeof(error
)))) {
780 admonish(cp
, "%s", error
);
786 * If it's a From: or Resent-From: header, save the address
787 * for later possible use (as the envelope address for SMTP)
790 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
791 : (hdr
->set
& MFRM
)) {
792 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
793 from
[sizeof(from
) - 1] = '\0';
798 * Also save the Sender: or Resent-Sender: header as well
801 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
802 : (hdr
->set
& MSND
)) {
803 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
804 sender
[sizeof(sender
) - 1] = '\0';
809 * ALSO ... save Envelope-From
812 if (hdr
->set
& MEFM
) {
813 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
814 efrom
[sizeof(efrom
) - 1] = '\0';
817 if (hdr
->flags
& HBCC
)
820 mp
->m_ingrp
= np
->m_ingrp
;
823 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
825 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
826 /* Catch this before sendmail chokes with:
827 "553 List:; syntax illegal for recipient
829 If we wanted to, we could expand out blind
830 aliases and put them in Bcc:, but then
831 they'd have the Blind-Carbon-Copy
834 "blind lists not compatible with"
840 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
841 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
850 /* Address includes a host, so no alias substitution is needed. */
853 * If it's a From: or Resent-From header, save the address
854 * for later possible use (as the envelope address for SMTP)
857 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
858 : (hdr
->set
& MFRM
)) {
859 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
864 * Also save the Sender: header as well
867 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
868 : (hdr
->set
& MSND
)) {
869 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
870 sender
[sizeof(sender
) - 1] = '\0';
875 * ALSO ... save Envelope-From
878 if (hdr
->set
& MEFM
) {
879 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
880 efrom
[sizeof(efrom
) - 1] = '\0';
883 if (hdr
->flags
& HBCC
)
886 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
889 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
893 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
900 * If this is a From:/Resent-From: header, save the full thing for
901 * later in case we need it for use when constructing a Bcc draft message
904 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
) : (hdr
->set
& MFRM
)) {
905 strncpy(fullfrom
, str
, sizeof(fullfrom
));
906 fullfrom
[sizeof(fullfrom
) - 1] = 0;
908 * Strip off any trailing newlines
911 while (strlen(fullfrom
) > 0 && fullfrom
[strlen(fullfrom
) - 1] == '\n') {
912 fullfrom
[strlen(fullfrom
) - 1] = '\0';
916 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
917 advise (NULL
, "%s: field does not allow groups", name
);
932 * Probably not necessary, but just in case ...
943 * Now that we've outputted the header fields in the draft
944 * message, we will now output any remaining header fields
945 * that we need to add/create.
949 finish_headers (FILE *out
)
953 if (!(msgflags
& MFRM
)) {
955 * A From: header is now required in the draft.
957 advise (NULL
, "message has no From: header");
958 advise (NULL
, "See default components files for examples");
963 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
964 advise (NULL
, "A Sender: or Envelope-From: header is required "
965 "with multiple\nFrom: addresses");
973 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
975 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
977 * If we have multiple From: addresses, make sure we have an
978 * Sender: header. If we don't have one, then generate one
979 * from Envelope-From: (which in this case, cannot be blank)
982 if (fromcount
> 1 && seensender
== 0) {
983 if (efrom
[0] == '\0') {
984 advise (NULL
, "Envelope-From cannot be blank when there "
985 "is multiple From: addresses\nand no Sender: "
989 fprintf (out
, "Sender: %s\n", efrom
);
993 if (!(msgflags
& MVIS
))
994 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
998 if (!(msgflags
& MDAT
)) {
999 advise (NULL
, "message has no Date: header");
1002 if (!(msgflags
& MFRM
)) {
1003 advise (NULL
, "message has no From: header");
1006 if (!(msgflags
& MRFM
)) {
1007 advise (NULL
, "message has no Resent-From: header");
1008 advise (NULL
, "See default components files for examples");
1012 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
1013 advise (NULL
, "A Resent-Sender: or Envelope-From: header is "
1014 "required with multiple\nResent-From: addresses");
1022 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
1024 fprintf (out
, "Resent-Message-ID: %s\n",
1025 message_id (tclock
, 0));
1027 * If we have multiple Resent-From: addresses, make sure we have an
1028 * Resent-Sender: header. If we don't have one, then generate one
1029 * from Envelope-From (which in this case, cannot be blank)
1032 if (fromcount
> 1 && seensender
== 0) {
1033 if (efrom
[0] == '\0') {
1034 advise (NULL
, "Envelope-From cannot be blank when there "
1035 "is multiple Resent-From: addresses and no "
1036 "Resent-Sender: header");
1039 fprintf (out
, "Resent-Sender: %s\n", efrom
);
1043 if (!(msgflags
& MVIS
))
1044 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1049 adios (NULL
, "re-format message and try again");
1051 adios (NULL
, "no addressees");
1056 get_header (char *header
, struct headers
*table
)
1060 for (h
= table
; h
->value
; h
++)
1061 if (!strcasecmp (header
? header
: "", h
->value
? h
->value
: ""))
1069 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
1073 char buffer
[BUFSIZ
];
1075 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
1077 if (sm_mts
!= MTS_SENDMAIL_PIPE
&&
1078 ((flags
& (HBCC
| HDCC
| HEFM
)) || mp
->m_ingrp
))
1082 fprintf (out
, "%s: ", name
);
1083 linepos
+= (nameoutput
= strlen (name
) + 2);
1086 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1087 mp
->m_pers
= getcpy (aka
);
1090 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1093 cp
= adrformat (mp
);
1100 if (linepos
!= nameoutput
) {
1101 if (len
+ linepos
+ 2 > outputlinelen
)
1102 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1112 return (flags
& HTRY
);
1117 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1122 if (sm_mts
!= MTS_SENDMAIL_PIPE
&& (flags
& HBCC
))
1126 fprintf (out
, "%s: ", name
);
1127 linepos
+= (nameoutput
= strlen (name
) + 2);
1130 cp
= concat (group
, ";", NULL
);
1133 if (linepos
> nameoutput
) {
1134 if (len
+ linepos
+ 2 > outputlinelen
) {
1135 fprintf (out
, ",\n%*s", nameoutput
, "");
1136 linepos
= nameoutput
;
1150 insert (struct mailname
*np
)
1152 struct mailname
*mp
;
1154 if (np
->m_mbox
== NULL
)
1157 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1158 : np
->m_type
== UUCPHOST
? &uuaddrs
1162 if (!strcasecmp (np
->m_host
? np
->m_host
: "",
1163 mp
->m_next
->m_host
? mp
->m_next
->m_host
: "") &&
1164 !strcasecmp (np
->m_mbox
? np
->m_mbox
: "",
1165 mp
->m_next
->m_mbox
? mp
->m_next
->m_mbox
: "") &&
1166 np
->m_bcc
== mp
->m_next
->m_bcc
)
1179 struct mailname
*mp
;
1181 printf ("-------\n\t-- Addresses --\nlocal:\t");
1182 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1183 printf ("%s%s%s", mp
->m_mbox
,
1184 mp
->m_bcc
? "[BCC]" : "",
1185 mp
->m_next
? ",\n\t" : "");
1187 printf ("\nnet:\t");
1188 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1189 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1190 mp
->m_mbox
, mp
->m_host
,
1191 mp
->m_bcc
? "[BCC]" : "",
1192 mp
->m_next
? ",\n\t" : "");
1194 printf ("\nuucp:\t");
1195 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1196 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1197 mp
->m_bcc
? "[BCC]" : "",
1198 mp
->m_next
? ",\n\t" : "");
1200 printf ("\n\t-- Folder Copies --\nfcc:\t");
1201 for (i
= 0; i
< fccind
; i
++)
1202 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1210 struct mailname
*mp
;
1212 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1213 if (annoaux (mp
) == NOTOK
)
1216 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1217 if (annoaux (mp
) == NOTOK
)
1220 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1221 if (annoaux (mp
) == NOTOK
)
1231 annoaux (struct mailname
*mp
)
1234 char buffer
[BUFSIZ
];
1236 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1237 i
= strlen (buffer
);
1239 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1244 insert_fcc (struct headers
*hdr
, char *pp
)
1248 for (cp
= pp
; isspace ((unsigned char) *cp
); cp
++)
1250 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace ((unsigned char) *pp
); pp
--)
1258 adios (NULL
, "too many %ss", hdr
->value
);
1259 fccfold
[fccind
++] = getcpy (cp
);
1267 make_bcc_file (int dashstuff
)
1273 char *tfile
= NULL
, *program
;
1275 if ((tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
)) == NULL
) {
1276 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
1278 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1280 fprintf (out
, "From: %s\n", fullfrom
);
1281 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1283 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1285 fprintf (out
, "Subject: %s", subject
);
1286 fprintf (out
, "BCC:\n");
1289 * Use MIME encapsulation for Bcc messages
1295 * Check if any lines in the message clash with the
1296 * prefix for the MIME multipart separator. If there
1297 * is a clash, increment one of the letters in the
1298 * prefix and check again.
1300 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1301 adios (NULL
, "lost prefix start");
1302 while (find_prefix () == NOTOK
) {
1307 adios (NULL
, "can't find a unique delimiter string");
1312 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1313 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1314 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1316 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1322 * Do mhl filtering of Bcc messages instead
1323 * of MIME encapsulation.
1325 if (filter
!= NULL
) {
1326 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1330 adios ("fork", "unable to");
1333 dup2 (fileno (out
), 1);
1335 vec
= argsplit(mhlproc
, &program
, &i
);
1336 vec
[i
++] = "-forward";
1341 /* was the flag -[no]dashstuffing specified? */
1343 vec
[i
++] = "-dashstuffing";
1344 else if (dashstuff
< 0)
1345 vec
[i
++] = "-nodashstuffing";
1348 execvp (program
, vec
);
1349 fprintf (stderr
, "unable to exec ");
1354 pidXwait (child_id
, mhlproc
);
1358 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1359 adios (tmpfil
, "unable to re-open");
1362 * If using MIME encapsulation, or if the -nodashstuffing
1363 * flag was given, then just copy message. Else do
1364 * RFC934 quoting (dashstuffing).
1366 if (mime
|| dashstuff
< 0)
1367 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1369 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1373 fseek (out
, 0L, SEEK_END
);
1375 fprintf (out
, "\n--%s--\n", prefix
);
1377 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1383 * Scan message to check if any lines clash with
1384 * the prefix of the MIME multipart separator.
1391 char buffer
[BUFSIZ
];
1394 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1395 adios (tmpfil
, "unable to re-open");
1397 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1398 if (buffer
[0] == '-' && buffer
[1] == '-') {
1401 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1402 if (!isspace ((unsigned char) *cp
))
1405 if (strcmp (buffer
+ 2, prefix
) == 0) {
1416 #define plural(x) (x == 1 ? "" : "s")
1421 if (badadr
&& unkadr
)
1422 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1423 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1425 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1427 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1432 do_addresses (int bccque
, int talk
)
1436 struct mailname
*lp
;
1439 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1440 if (lp
->m_bcc
? bccque
: !bccque
) {
1442 printf (" -- Local Recipients --\n");
1443 do_an_address (lp
, talk
);
1448 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1449 if (lp
->m_bcc
? bccque
: !bccque
) {
1451 printf (" -- UUCP Recipients --\n");
1452 do_an_address (lp
, talk
);
1457 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1458 if (lp
->m_bcc
? bccque
: !bccque
) {
1460 printf (" -- Network Recipients --\n");
1461 do_an_address (lp
, talk
);
1467 if (rp_isbad (retval
= sm_waend ()))
1468 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1473 * MTS-SPECIFIC INTERACTION
1478 * SENDMAIL/SMTP routines
1482 post (char *file
, int bccque
, int talk
, char *envelope
)
1489 if (msgflags
& MINV
)
1490 printf (" -- Posting for %s Recipients --\n",
1491 bccque
? "Blind" : "Sighted");
1493 printf (" -- Posting for All Recipients --\n");
1498 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
1499 char **argp
, *program
;
1502 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1506 adios ("fork", "unable to");
1509 if (freopen( file
, "r", stdin
) == NULL
) {
1510 adios (file
, "can't reopen for sendmail");
1513 argp
= argsplit(sendmail
, &program
, &argc
);
1514 argp
[argc
++] = "-t"; /* read msg for recipients */
1515 argp
[argc
++] = "-i"; /* don't stop on "." */
1517 argp
[argc
++] = "-bv";
1519 argp
[argc
++] = "-v";
1522 execv (program
, argp
);
1523 adios (sendmail
, "can't exec");
1526 pidXwait (child_id
, NULL
);
1530 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1531 verbose
, snoop
, queued
, sasl
,
1532 saslssf
, saslmech
, user
, tls
)) ||
1533 rp_isbad (retval
= sm_winit (envelope
)))
1534 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1536 do_addresses (bccque
, talk
&& verbose
);
1537 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1538 die (file
, "unable to re-open");
1543 sm_end (!(msgflags
& MINV
) || bccque
? OK
: DONE
);
1547 if (msgflags
& MINV
)
1548 printf (" -- %s Recipient Copies Posted --\n",
1549 bccque
? "Blind" : "Sighted");
1551 printf (" -- Recipient Copies Posted --\n");
1559 /* Address Verification */
1562 verify_all_addresses (int talk
, char *envelope
)
1565 struct mailname
*lp
;
1569 if (!whomsw
|| checksw
)
1570 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1571 verbose
, snoop
, queued
, sasl
,
1572 saslssf
, saslmech
, user
, tls
))
1573 || rp_isbad (retval
= sm_winit (envelope
)))
1574 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1576 if (talk
&& !whomsw
)
1577 printf (" -- Address Verification --\n");
1578 if (talk
&& localaddrs
.m_next
)
1579 printf (" -- Local Recipients --\n");
1580 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1581 do_an_address (lp
, talk
);
1583 if (talk
&& uuaddrs
.m_next
)
1584 printf (" -- UUCP Recipients --\n");
1585 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1586 do_an_address (lp
, talk
);
1588 if (talk
&& netaddrs
.m_next
)
1589 printf (" -- Network Recipients --\n");
1590 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1591 do_an_address (lp
, talk
);
1594 if (talk
&& !whomsw
)
1595 printf (" -- Address Verification Successful --\n");
1597 if (!whomsw
|| checksw
)
1606 do_an_address (struct mailname
*lp
, int talk
)
1612 switch (lp
->m_type
) {
1616 strncpy (addr
, mbox
, sizeof(addr
));
1620 mbox
= auxformat (lp
, 0);
1622 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1625 default: /* let SendMail decide if the host is bad */
1628 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1633 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1635 if (whomsw
&& !checksw
) {
1643 switch (retval
= sm_wadr (mbox
, host
,
1644 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1647 printf ("address ok\n");
1653 fprintf (stderr
, " %s: ", addr
);
1654 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1655 rp_string (retval
));
1661 fprintf (stderr
, " %s: ", addr
);
1662 die (NULL
, "unexpected response; %s", rp_string (retval
));
1670 do_text (char *file
, int fd
)
1675 lseek (fd
, (off_t
) 0, SEEK_SET
);
1677 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1678 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1679 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1683 die (file
, "problem reading from");
1685 switch (retval
= sm_wtend ()) {
1691 die (NULL
, "posting failed; %s", rp_string (retval
));
1694 die (NULL
, "unexpected response; %s", rp_string (retval
));
1708 (void) m_unlink (tmpfil
);
1709 if (msgflags
& MINV
)
1710 (void) m_unlink (bccfil
);
1712 if (!whomsw
|| checksw
)
1725 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1726 istat
= SIGNAL2 (SIGINT
, sigser
);
1727 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1728 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1738 SIGNAL (SIGHUP
, hstat
);
1739 SIGNAL (SIGINT
, istat
);
1740 SIGNAL (SIGQUIT
, qstat
);
1741 SIGNAL (SIGTERM
, tstat
);
1749 p_refile (char *file
)
1757 printf (" -- Filing Folder Copies --\n");
1758 for (i
= 0; i
< fccind
; i
++)
1759 fcc (file
, fccfold
[i
]);
1761 printf (" -- Folder Copies Filed --\n");
1766 * Call the `fileproc' to add the file to the folder.
1770 fcc (char *file
, char *folder
)
1773 int i
, status
, argp
;
1775 char **arglist
, *program
;
1778 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1781 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1787 fprintf (stderr
, " %sFcc %s: ",
1788 msgstate
== RESENT
? "Resent-" : "", folder
);
1789 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1793 /* see if we need to add `+' */
1794 snprintf (fold
, sizeof(fold
), "%s%s",
1795 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1797 /* now exec the fileproc */
1799 arglist
= argsplit(fileproc
, &program
, &argp
);
1800 arglist
[argp
++] = "-link";
1801 arglist
[argp
++] = "-file";
1802 arglist
[argp
++] = file
;
1803 arglist
[argp
++] = fold
;
1804 arglist
[argp
] = NULL
;
1805 execvp (program
, arglist
);
1809 if ((status
= pidwait (child_id
, OK
))) {
1811 fprintf (stderr
, " %sFcc %s: ",
1812 msgstate
== RESENT
? "Resent-" : "", folder
);
1813 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1816 printf ("folder ok\n");
1828 die (char *what
, char *fmt
, ...)
1832 (void) m_unlink (tmpfil
);
1833 if (msgflags
& MINV
)
1834 (void) m_unlink (bccfil
);
1836 if (!whomsw
|| checksw
)
1840 advertise (what
, NULL
, fmt
, ap
);