]>
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 signature
[BUFSIZ
]; /* my signature */
227 static char *filter
= NULL
; /* the filter for BCC'ing */
228 static char *subject
= NULL
; /* the subject field for BCC'ing */
229 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
231 static struct headers
*hdrtab
; /* table for the message we're doing */
233 static struct mailname localaddrs
; /* local addrs */
234 static struct mailname netaddrs
; /* network addrs */
235 static struct mailname uuaddrs
; /* uucp addrs */
236 static struct mailname tmpaddrs
; /* temporary queue */
238 static int snoop
= 0;
239 static char *clientsw
= NULL
;
240 static char *serversw
= NULL
;
242 extern struct smtp sm_reply
;
244 static char prefix
[] = "----- =_aaaaaaaaaa";
246 static char *partno
= NULL
;
247 static int queued
= 0;
252 static void putfmt (char *, char *, FILE *);
253 static void start_headers (void);
254 static void finish_headers (FILE *);
255 static int get_header (char *, struct headers
*);
256 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
257 static void putgrp (char *, char *, FILE *, unsigned int);
258 static int insert (struct mailname
*);
259 static void pl (void);
260 static void anno (void);
261 static int annoaux (struct mailname
*);
262 static void insert_fcc (struct headers
*, char *);
263 static void make_bcc_file (int);
264 static void verify_all_addresses (int, char *);
265 static void chkadr (void);
266 static void sigon (void);
267 static void sigoff (void);
268 static void p_refile (char *);
269 static void fcc (char *, char *);
270 static void die (char *, char *, ...);
271 static void post (char *, int, int, char *);
272 static void do_text (char *file
, int fd
);
273 static void do_an_address (struct mailname
*, int);
274 static void do_addresses (int, int);
275 static int find_prefix (void);
279 main (int argc
, char **argv
)
281 int state
, compnum
, dashstuff
= 0;
282 char *cp
, *msg
= NULL
, **argp
, **arguments
, *envelope
;
283 char buf
[BUFSIZ
], name
[NAMESZ
];
285 m_getfld_state_t gstate
= 0;
288 setlocale(LC_ALL
, "");
290 invo_name
= r1bindex (argv
[0], '/');
292 /* foil search of user profile/context */
293 if (context_foil (NULL
) == -1)
296 mts_init (invo_name
);
297 arguments
= getarguments (invo_name
, argc
, argv
, 0);
300 while ((cp
= *argp
++)) {
302 switch (smatch (++cp
, switches
)) {
304 ambigsw (cp
, switches
);
307 adios (NULL
, "-%s unknown", cp
);
310 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
311 print_help (buf
, switches
, 0);
314 print_version(invo_name
);
318 if (!(cp
= *argp
++) || *cp
== '-')
319 adios (NULL
, "missing argument to %s", argp
[-2]);
320 /* create a minimal context */
321 if (context_foil (cp
) == -1)
326 if (!(cp
= *argp
++) || *cp
== '-')
327 adios (NULL
, "missing argument to %s", argp
[-2]);
328 if ((state
= alias (cp
)) != AK_OK
)
329 adios (NULL
, "aliasing error in %s - %s",
330 cp
, akerror (state
));
349 if (!(filter
= *argp
++) || *filter
== '-')
350 adios (NULL
, "missing argument to %s", argp
[-2]);
405 if (!(cp
= *argp
++) || *cp
== '-')
406 adios (NULL
, "missing argument to %s", argp
[-2]);
407 if ((outputlinelen
= atoi (cp
)) < 10)
408 adios (NULL
, "impossible width %d", outputlinelen
);
412 if (!(cp
= *argp
++) || *cp
== '-')
413 adios (NULL
, "missing argument to %s", argp
[-2]);
414 if ((pfd
= atoi (cp
)) <= 2)
415 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
419 if (!(clientsw
= *argp
++) || *clientsw
== '-')
420 adios (NULL
, "missing argument to %s", argp
[-2]);
423 if (!(serversw
= *argp
++) || *serversw
== '-')
424 adios (NULL
, "missing argument to %s", argp
[-2]);
431 if (!(partno
= *argp
++) || *partno
== '-')
432 adios (NULL
, "missing argument to %s", argp
[-2]);
448 if (!(cp
= *argp
++) || *cp
== '-')
449 adios (NULL
, "missing argument to %s", argp
[-2]);
454 if (!(saslmech
= *argp
++) || *saslmech
== '-')
455 adios (NULL
, "missing argument to %s", argp
[-2]);
459 if (!(user
= *argp
++) || *user
== '-')
460 adios (NULL
, "missing argument to %s", argp
[-2]);
464 if (!(port
= *argp
++) || *port
== '-')
465 adios (NULL
, "missing argument to %s", argp
[-2]);
481 if (!(cp
= *argp
++) || *cp
== '-')
482 adios (NULL
, "missing argument to %s", argp
[-2]);
487 if (!(cp
= *argp
++) || *cp
== '-')
488 adios (NULL
, "missing argument to %s", argp
[-2]);
493 if (!(cp
= *argp
++) || *cp
== '-')
494 adios (NULL
, "missing argument to %s", argp
[-2]);
495 save_mts_method (cp
);
498 case CREDENTIALSSW
: {
499 /* post doesn't read the profile, so insert credentials
500 entry the hard way. */
501 struct node
*np
= (struct node
*) mh_xmalloc (sizeof *np
);
503 if (!(cp
= *argp
++) || *cp
== '-')
504 adios (NULL
, "missing argument to %s", argp
[-2]);
505 np
->n_name
= "credentials";
514 if (!(cp
= *argp
++) || *cp
== '-')
515 adios (NULL
, "missing argument to %s", argp
[-2]);
516 if (save_message_id_style (cp
) != 0)
517 adios (NULL
, "unsupported messageid \"%s\"", cp
);
522 adios (NULL
, "only one message at a time!");
530 adios (NULL
, "usage: %s [switches] file", invo_name
);
532 if (outputlinelen
< 10)
533 adios (NULL
, "impossible width %d", outputlinelen
);
535 if ((in
= fopen (msg
, "r")) == NULL
)
536 adios (msg
, "unable to open");
544 if ((out
= fopen ("/dev/null", "w")) == NULL
)
545 adios ("/dev/null", "unable to open");
547 char *cp
= m_mktemp(m_maildir(invo_name
), NULL
, &out
);
549 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
551 adios ("post", "unable to create temporary file");
554 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
555 chmod (tmpfil
, 0600);
559 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
561 for (compnum
= 1;;) {
562 int bufsz
= sizeof buf
;
563 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
567 cp
= add (buf
, NULL
);
568 while (state
== FLDPLUS
) {
570 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
573 putfmt (name
, cp
, out
);
578 finish_headers (out
);
581 fprintf (out
, "\n%s", buf
);
582 while (state
== BODY
) {
584 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
590 finish_headers (out
);
595 adios (NULL
, "message format error in component #%d", compnum
);
598 adios (NULL
, "getfld() returned %d", state
);
602 m_getfld_state_destroy (&gstate
);
616 * Here's how we decide which address to use as the envelope-from
619 * - If we were given an Envelope-From header, use that.
620 * - If we were given a Sender: address, use that.
621 * - Otherwise, use the address on the From: line
624 if (msgflags
& MEFM
) {
626 } else if (seensender
) {
632 /* If we are doing a "whom" check */
634 /* This won't work with MTS_SENDMAIL_PIPE. */
635 verify_all_addresses (1, envelope
);
639 if (msgflags
& MINV
) {
640 make_bcc_file (dashstuff
);
641 if (msgflags
& MVIS
) {
642 if (sm_mts
!= MTS_SENDMAIL_PIPE
) {
643 /* It would be nice to have support to call
644 verify_all_addresses with MTS_SENDMAIL_PIPE, but
645 that might require running sendmail as root. Note
646 that spost didn't verify addresses. */
647 verify_all_addresses (verbose
, envelope
);
649 post (tmpfil
, 0, verbose
, envelope
);
651 post (bccfil
, 1, verbose
, envelope
);
654 post (tmpfil
, 0, isatty (1), envelope
);
661 printf (partno
? "Partial Message #%s Processed\n" : "Message Processed\n",
673 putfmt (char *name
, char *str
, FILE *out
)
675 int count
, grp
, i
, keep
;
678 struct mailname
*mp
= NULL
, *np
= NULL
;
681 while (*str
== ' ' || *str
== '\t')
684 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
685 advise (NULL
, "illegal header line -- %s:", name
);
690 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
691 if (strncasecmp (name
, "nmh-", 4)) {
692 fprintf (out
, "%s: %s", name
, str
);
694 /* Filter out all Nmh-* headers, because Norm asked. They
695 should never have reached this point. Warn about any
696 that are non-empty. */
697 if (strcmp (str
, "\n")) {
698 char *newline
= strchr (str
, '\n');
699 if (newline
) *newline
= '\0';
701 advise (NULL
, "ignoring header line -- %s: %s", name
, str
);
710 if (hdr
->flags
& HIGN
) {
713 if (hdr
->flags
& HBAD
) {
714 advise (NULL
, "illegal header line -- %s:", name
);
718 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
720 if (hdr
->flags
& HSUB
)
721 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
722 if (hdr
->flags
& HFCC
) {
723 if ((cp
= strrchr(str
, '\n')))
725 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
727 insert_fcc (hdr
, pp
);
729 insert_fcc (hdr
, pp
);
733 if (!(hdr
->flags
& HADR
)) {
734 fprintf (out
, "%s: %s", name
, str
);
738 tmpaddrs
.m_next
= NULL
;
739 for (count
= 0; (cp
= getname (str
)); count
++)
740 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
744 tmpaddrs
.m_next
= mp
;
748 if (hdr
->flags
& HTRY
)
754 if (hdr
->flags
& HNIL
)
755 fprintf (out
, "%s: %s", name
, str
);
758 * Sender (or Resent-Sender) can have only one address
760 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
761 : (hdr
->set
& MSND
)) {
762 advise (NULL
, "%s: field requires one address", name
);
766 advise (NULL
, "%s: field requires at least one address", name
);
773 if (count
> 1 && (hdr
->flags
& HONE
)) {
774 advise (NULL
, "%s: field only permits one address", name
);
779 nameoutput
= linepos
= 0;
780 snprintf (namep
, sizeof(namep
), "%s%s",
781 (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
783 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
784 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
785 /* The address doesn't include a host, so it might be an alias. */
786 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
787 qp
= akvisible () ? mp
->m_mbox
: "";
790 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
791 while ((cp
= getname (pp
))) {
792 if (!(mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
798 * If it's a From: or Resent-From: header, save the address
799 * for later possible use (as the envelope address for SMTP)
802 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
803 : (hdr
->set
& MFRM
)) {
804 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
805 from
[sizeof(from
) - 1] = '\0';
810 * Also save the Sender: or Resent-Sender: header as well
813 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
814 : (hdr
->set
& MSND
)) {
815 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
816 sender
[sizeof(sender
) - 1] = '\0';
821 * ALSO ... save Envelope-From
824 if (hdr
->set
& MEFM
) {
825 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
826 efrom
[sizeof(efrom
) - 1] = '\0';
829 if (hdr
->flags
& HBCC
)
832 mp
->m_ingrp
= np
->m_ingrp
;
835 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
837 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
838 /* Catch this before sendmail chokes with:
839 "553 List:; syntax illegal for recipient
841 If we wanted to, we could expand out blind
842 aliases and put them in Bcc:, but then
843 they'd have the Blind-Carbon-Copy
846 "blind lists not compatible with"
852 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
853 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
862 /* Address includes a host, so no alias substitution is needed. */
865 * If it's a From: or Resent-From header, save the address
866 * for later possible use (as the envelope address for SMTP)
869 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
870 : (hdr
->set
& MFRM
)) {
871 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
876 * Also save the Sender: header as well
879 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
880 : (hdr
->set
& MSND
)) {
881 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
882 sender
[sizeof(sender
) - 1] = '\0';
887 * ALSO ... save Envelope-From
890 if (hdr
->set
& MEFM
) {
891 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
892 efrom
[sizeof(efrom
) - 1] = '\0';
895 if (hdr
->flags
& HBCC
)
898 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
901 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
905 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
912 * If this is a From:/Resent-From: header, save the full thing for
913 * later in case we need it for use when constructing a Bcc draft message
916 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
) : (hdr
->set
& MFRM
)) {
917 strncpy(fullfrom
, str
, sizeof(fullfrom
));
918 fullfrom
[sizeof(fullfrom
) - 1] = 0;
920 * Strip off any trailing newlines
923 while (strlen(fullfrom
) > 0 && fullfrom
[strlen(fullfrom
) - 1] == '\n') {
924 fullfrom
[strlen(fullfrom
) - 1] = '\0';
928 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
929 advise (NULL
, "%s: field does not allow groups", name
);
941 char *cp
, sigbuf
[BUFSIZ
];
947 * Probably not necessary, but just in case ...
955 if ((cp
= getfullname ()) && *cp
) {
956 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
957 snprintf (signature
, sizeof(signature
), "%s <%s>",
958 sigbuf
, getlocaladdr());
959 if ((cp
= getname (signature
)) == NULL
)
960 adios (NULL
, "getname () failed -- you lose extraordinarily big");
961 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
)) == NULL
)
962 adios (NULL
, "bad signature '%s'", sigbuf
);
967 strncpy (signature
, getlocaladdr(), sizeof(signature
));
973 * Now that we've outputted the header fields in the draft
974 * message, we will now output any remaining header fields
975 * that we need to add/create.
979 finish_headers (FILE *out
)
983 if (!(msgflags
& MFRM
)) {
985 * A From: header is now required in the draft.
987 advise (NULL
, "message has no From: header");
988 advise (NULL
, "See default components files for examples");
993 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
994 advise (NULL
, "A Sender: or Envelope-From: header is required "
995 "with multiple\nFrom: addresses");
1003 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1005 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1007 * If we have multiple From: addresses, make sure we have an
1008 * Sender: header. If we don't have one, then generate one
1009 * from Envelope-From: (which in this case, cannot be blank)
1012 if (fromcount
> 1 && seensender
== 0) {
1013 if (efrom
[0] == '\0') {
1014 advise (NULL
, "Envelope-From cannot be blank when there "
1015 "is multiple From: addresses\nand no Sender: "
1019 fprintf (out
, "Sender: %s\n", efrom
);
1023 if (!(msgflags
& MVIS
))
1024 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
1028 if (!(msgflags
& MDAT
)) {
1029 advise (NULL
, "message has no Date: header");
1032 if (!(msgflags
& MFRM
)) {
1033 advise (NULL
, "message has no From: header");
1036 if (!(msgflags
& MRFM
)) {
1037 advise (NULL
, "message has no Resent-From: header");
1038 advise (NULL
, "See default components files for examples");
1042 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
1043 advise (NULL
, "A Resent-Sender: or Envelope-From: header is "
1044 "required with multiple\nResent-From: addresses");
1052 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
1054 fprintf (out
, "Resent-Message-ID: %s\n",
1055 message_id (tclock
, 0));
1057 * If we have multiple Resent-From: addresses, make sure we have an
1058 * Resent-Sender: header. If we don't have one, then generate one
1059 * from Envelope-From (which in this case, cannot be blank)
1062 if (fromcount
> 1 && seensender
== 0) {
1063 if (efrom
[0] == '\0') {
1064 advise (NULL
, "Envelope-From cannot be blank when there "
1065 "is multiple Resent-From: addresses and no "
1066 "Resent-Sender: header");
1069 fprintf (out
, "Resent-Sender: %s\n", efrom
);
1073 if (!(msgflags
& MVIS
))
1074 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1079 adios (NULL
, "re-format message and try again");
1081 adios (NULL
, "no addressees");
1086 get_header (char *header
, struct headers
*table
)
1090 for (h
= table
; h
->value
; h
++)
1091 if (!strcasecmp (header
? header
: "", h
->value
? h
->value
: ""))
1099 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
1103 char buffer
[BUFSIZ
];
1105 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
1107 if (sm_mts
!= MTS_SENDMAIL_PIPE
&&
1108 ((flags
& (HBCC
| HDCC
| HEFM
)) || mp
->m_ingrp
))
1112 fprintf (out
, "%s: ", name
);
1113 linepos
+= (nameoutput
= strlen (name
) + 2);
1116 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1117 mp
->m_pers
= getcpy (aka
);
1120 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1123 cp
= adrformat (mp
);
1130 if (linepos
!= nameoutput
) {
1131 if (len
+ linepos
+ 2 > outputlinelen
)
1132 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1142 return (flags
& HTRY
);
1147 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1152 if (sm_mts
!= MTS_SENDMAIL_PIPE
&& (flags
& HBCC
))
1156 fprintf (out
, "%s: ", name
);
1157 linepos
+= (nameoutput
= strlen (name
) + 2);
1160 cp
= concat (group
, ";", NULL
);
1163 if (linepos
> nameoutput
) {
1164 if (len
+ linepos
+ 2 > outputlinelen
) {
1165 fprintf (out
, ",\n%*s", nameoutput
, "");
1166 linepos
= nameoutput
;
1180 insert (struct mailname
*np
)
1182 struct mailname
*mp
;
1184 if (np
->m_mbox
== NULL
)
1187 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1188 : np
->m_type
== UUCPHOST
? &uuaddrs
1192 if (!strcasecmp (np
->m_host
? np
->m_host
: "",
1193 mp
->m_next
->m_host
? mp
->m_next
->m_host
: "") &&
1194 !strcasecmp (np
->m_mbox
? np
->m_mbox
: "",
1195 mp
->m_next
->m_mbox
? mp
->m_next
->m_mbox
: "") &&
1196 np
->m_bcc
== mp
->m_next
->m_bcc
)
1209 struct mailname
*mp
;
1211 printf ("-------\n\t-- Addresses --\nlocal:\t");
1212 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1213 printf ("%s%s%s", mp
->m_mbox
,
1214 mp
->m_bcc
? "[BCC]" : "",
1215 mp
->m_next
? ",\n\t" : "");
1217 printf ("\nnet:\t");
1218 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1219 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1220 mp
->m_mbox
, mp
->m_host
,
1221 mp
->m_bcc
? "[BCC]" : "",
1222 mp
->m_next
? ",\n\t" : "");
1224 printf ("\nuucp:\t");
1225 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1226 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1227 mp
->m_bcc
? "[BCC]" : "",
1228 mp
->m_next
? ",\n\t" : "");
1230 printf ("\n\t-- Folder Copies --\nfcc:\t");
1231 for (i
= 0; i
< fccind
; i
++)
1232 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1240 struct mailname
*mp
;
1242 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1243 if (annoaux (mp
) == NOTOK
)
1246 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1247 if (annoaux (mp
) == NOTOK
)
1250 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1251 if (annoaux (mp
) == NOTOK
)
1261 annoaux (struct mailname
*mp
)
1264 char buffer
[BUFSIZ
];
1266 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1267 i
= strlen (buffer
);
1269 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1274 insert_fcc (struct headers
*hdr
, char *pp
)
1278 for (cp
= pp
; isspace ((unsigned char) *cp
); cp
++)
1280 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace ((unsigned char) *pp
); pp
--)
1288 adios (NULL
, "too many %ss", hdr
->value
);
1289 fccfold
[fccind
++] = getcpy (cp
);
1297 make_bcc_file (int dashstuff
)
1303 char *tfile
= NULL
, *program
;
1305 tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
);
1306 if (tfile
== NULL
) adios("bcc", "unable to create temporary file");
1307 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1309 fprintf (out
, "From: %s\n", fullfrom
);
1310 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1312 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1314 fprintf (out
, "Subject: %s", subject
);
1315 fprintf (out
, "BCC:\n");
1318 * Use MIME encapsulation for Bcc messages
1324 * Check if any lines in the message clash with the
1325 * prefix for the MIME multipart separator. If there
1326 * is a clash, increment one of the letters in the
1327 * prefix and check again.
1329 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1330 adios (NULL
, "lost prefix start");
1331 while (find_prefix () == NOTOK
) {
1336 adios (NULL
, "can't find a unique delimiter string");
1341 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1342 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1343 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1345 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1351 * Do mhl filtering of Bcc messages instead
1352 * of MIME encapsulation.
1354 if (filter
!= NULL
) {
1355 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1359 adios ("fork", "unable to");
1362 dup2 (fileno (out
), 1);
1364 vec
= argsplit(mhlproc
, &program
, &i
);
1365 vec
[i
++] = "-forward";
1370 /* was the flag -[no]dashstuffing specified? */
1372 vec
[i
++] = "-dashstuffing";
1373 else if (dashstuff
< 0)
1374 vec
[i
++] = "-nodashstuffing";
1377 execvp (program
, vec
);
1378 fprintf (stderr
, "unable to exec ");
1383 pidXwait (child_id
, mhlproc
);
1387 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1388 adios (tmpfil
, "unable to re-open");
1391 * If using MIME encapsulation, or if the -nodashstuffing
1392 * flag was given, then just copy message. Else do
1393 * RFC934 quoting (dashstuffing).
1395 if (mime
|| dashstuff
< 0)
1396 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1398 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1402 fseek (out
, 0L, SEEK_END
);
1404 fprintf (out
, "\n--%s--\n", prefix
);
1406 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1412 * Scan message to check if any lines clash with
1413 * the prefix of the MIME multipart separator.
1420 char buffer
[BUFSIZ
];
1423 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1424 adios (tmpfil
, "unable to re-open");
1426 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1427 if (buffer
[0] == '-' && buffer
[1] == '-') {
1430 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1431 if (!isspace ((unsigned char) *cp
))
1434 if (strcmp (buffer
+ 2, prefix
) == 0) {
1445 #define plural(x) (x == 1 ? "" : "s")
1450 if (badadr
&& unkadr
)
1451 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1452 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1454 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1456 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1461 do_addresses (int bccque
, int talk
)
1465 struct mailname
*lp
;
1468 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1469 if (lp
->m_bcc
? bccque
: !bccque
) {
1471 printf (" -- Local Recipients --\n");
1472 do_an_address (lp
, talk
);
1477 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1478 if (lp
->m_bcc
? bccque
: !bccque
) {
1480 printf (" -- UUCP Recipients --\n");
1481 do_an_address (lp
, talk
);
1486 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1487 if (lp
->m_bcc
? bccque
: !bccque
) {
1489 printf (" -- Network Recipients --\n");
1490 do_an_address (lp
, talk
);
1496 if (rp_isbad (retval
= sm_waend ()))
1497 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1502 * MTS-SPECIFIC INTERACTION
1507 * SENDMAIL/SMTP routines
1511 post (char *file
, int bccque
, int talk
, char *envelope
)
1518 if (msgflags
& MINV
)
1519 printf (" -- Posting for %s Recipients --\n",
1520 bccque
? "Blind" : "Sighted");
1522 printf (" -- Posting for All Recipients --\n");
1527 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
1528 char **argp
, *program
;
1531 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1535 adios ("fork", "unable to");
1538 if (freopen( file
, "r", stdin
) == NULL
) {
1539 adios (file
, "can't reopen for sendmail");
1542 argp
= argsplit(sendmail
, &program
, &argc
);
1543 argp
[argc
++] = "-t"; /* read msg for recipients */
1544 argp
[argc
++] = "-i"; /* don't stop on "." */
1546 argp
[argc
++] = "-bv";
1548 argp
[argc
++] = "-v";
1551 execv (program
, argp
);
1552 adios (sendmail
, "can't exec");
1555 pidXwait (child_id
, NULL
);
1559 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1560 verbose
, snoop
, queued
, sasl
,
1561 saslssf
, saslmech
, user
, tls
)) ||
1562 rp_isbad (retval
= sm_winit (envelope
)))
1563 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1565 do_addresses (bccque
, talk
&& verbose
);
1566 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1567 die (file
, "unable to re-open");
1572 sm_end (!(msgflags
& MINV
) || bccque
? OK
: DONE
);
1576 if (msgflags
& MINV
)
1577 printf (" -- %s Recipient Copies Posted --\n",
1578 bccque
? "Blind" : "Sighted");
1580 printf (" -- Recipient Copies Posted --\n");
1588 /* Address Verification */
1591 verify_all_addresses (int talk
, char *envelope
)
1594 struct mailname
*lp
;
1598 if (!whomsw
|| checksw
)
1599 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1600 verbose
, snoop
, queued
, sasl
,
1601 saslssf
, saslmech
, user
, tls
))
1602 || rp_isbad (retval
= sm_winit (envelope
)))
1603 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1605 if (talk
&& !whomsw
)
1606 printf (" -- Address Verification --\n");
1607 if (talk
&& localaddrs
.m_next
)
1608 printf (" -- Local Recipients --\n");
1609 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1610 do_an_address (lp
, talk
);
1612 if (talk
&& uuaddrs
.m_next
)
1613 printf (" -- UUCP Recipients --\n");
1614 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1615 do_an_address (lp
, talk
);
1617 if (talk
&& netaddrs
.m_next
)
1618 printf (" -- Network Recipients --\n");
1619 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1620 do_an_address (lp
, talk
);
1623 if (talk
&& !whomsw
)
1624 printf (" -- Address Verification Successful --\n");
1626 if (!whomsw
|| checksw
)
1635 do_an_address (struct mailname
*lp
, int talk
)
1641 switch (lp
->m_type
) {
1645 strncpy (addr
, mbox
, sizeof(addr
));
1649 mbox
= auxformat (lp
, 0);
1651 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1654 default: /* let SendMail decide if the host is bad */
1657 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1662 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1664 if (whomsw
&& !checksw
) {
1672 switch (retval
= sm_wadr (mbox
, host
,
1673 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1676 printf ("address ok\n");
1682 fprintf (stderr
, " %s: ", addr
);
1683 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1684 rp_string (retval
));
1690 fprintf (stderr
, " %s: ", addr
);
1691 die (NULL
, "unexpected response; %s", rp_string (retval
));
1699 do_text (char *file
, int fd
)
1704 lseek (fd
, (off_t
) 0, SEEK_SET
);
1706 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1707 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1708 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1712 die (file
, "problem reading from");
1714 switch (retval
= sm_wtend ()) {
1720 die (NULL
, "posting failed; %s", rp_string (retval
));
1723 die (NULL
, "unexpected response; %s", rp_string (retval
));
1738 if (msgflags
& MINV
)
1741 if (!whomsw
|| checksw
)
1754 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1755 istat
= SIGNAL2 (SIGINT
, sigser
);
1756 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1757 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1767 SIGNAL (SIGHUP
, hstat
);
1768 SIGNAL (SIGINT
, istat
);
1769 SIGNAL (SIGQUIT
, qstat
);
1770 SIGNAL (SIGTERM
, tstat
);
1778 p_refile (char *file
)
1786 printf (" -- Filing Folder Copies --\n");
1787 for (i
= 0; i
< fccind
; i
++)
1788 fcc (file
, fccfold
[i
]);
1790 printf (" -- Folder Copies Filed --\n");
1795 * Call the `fileproc' to add the file to the folder.
1799 fcc (char *file
, char *folder
)
1802 int i
, status
, argp
;
1804 char **arglist
, *program
;
1807 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1810 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1816 fprintf (stderr
, " %sFcc %s: ",
1817 msgstate
== RESENT
? "Resent-" : "", folder
);
1818 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1822 /* see if we need to add `+' */
1823 snprintf (fold
, sizeof(fold
), "%s%s",
1824 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1826 /* now exec the fileproc */
1828 arglist
= argsplit(fileproc
, &program
, &argp
);
1829 arglist
[argp
++] = "-link";
1830 arglist
[argp
++] = "-file";
1831 arglist
[argp
++] = file
;
1832 arglist
[argp
++] = fold
;
1833 arglist
[argp
] = NULL
;
1834 execvp (program
, arglist
);
1838 if ((status
= pidwait (child_id
, OK
))) {
1840 fprintf (stderr
, " %sFcc %s: ",
1841 msgstate
== RESENT
? "Resent-" : "", folder
);
1842 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1845 printf ("folder ok\n");
1857 die (char *what
, char *fmt
, ...)
1862 if (msgflags
& MINV
)
1865 if (!whomsw
|| checksw
)
1869 advertise (what
, NULL
, fmt
, ap
);