]>
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("sasl", SASLminc(-4), SASLSW) \
80 X("nosasl", SASLminc(-6), NOSASLSW) \
81 X("saslmaxssf", SASLminc(-10), SASLMXSSFSW) \
82 X("saslmech", SASLminc(-5), SASLMECHSW) \
83 X("user", SASLminc(-4), USERSW) \
84 X("port server submission port name/number", 4, PORTSW) \
85 X("tls", TLSminc(-3), TLSSW) \
86 X("initialtls", TLSminc(-10), INITTLSSW) \
87 X("notls", TLSminc(-5), NTLSSW) \
88 X("fileproc", -4, FILEPROCSW) \
89 X("mhlproc", -3, MHLPROCSW) \
90 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
91 X("credentials legacy|file:filename", 0, CREDENTIALSSW) \
92 X("messageid localname|random", 2, MESSAGEIDSW) \
94 #define X(sw, minchars, id) id,
95 DEFINE_SWITCH_ENUM(POST
);
98 #define X(sw, minchars, id) { sw, minchars, id },
99 DEFINE_SWITCH_ARRAY(POST
, switches
);
110 * flags for headers->flags
112 #define HNOP 0x0000 /* just used to keep .set around */
113 #define HBAD 0x0001 /* bad header - don't let it through */
114 #define HADR 0x0002 /* header has an address field */
115 #define HSUB 0x0004 /* Subject: header */
116 #define HTRY 0x0008 /* try to send to addrs on header */
117 #define HBCC 0x0010 /* don't output this header, unless MTS_SENDMAIL_PIPE */
118 #define HMNG 0x0020 /* munge this header */
119 #define HNGR 0x0040 /* no groups allowed in this header */
120 #define HFCC 0x0080 /* FCC: type header */
121 #define HNIL 0x0100 /* okay for this header not to have addrs */
122 #define HIGN 0x0200 /* ignore this header */
123 #define HDCC 0x0400 /* another undocumented feature */
124 #define HONE 0x0800 /* Only (zero or) one address allowed */
125 #define HEFM 0x1000 /* Envelope-From: header */
128 * flags for headers->set
130 #define MFRM 0x0001 /* we've seen a From: */
131 #define MDAT 0x0002 /* we've seen a Date: */
132 #define MRFM 0x0004 /* we've seen a Resent-From: */
133 #define MVIS 0x0008 /* we've seen sighted addrs */
134 #define MINV 0x0010 /* we've seen blind addrs */
135 #define MSND 0x0020 /* we've seen a Sender: */
136 #define MRSN 0x0040 /* We've seen a Resent-Sendr:*/
137 #define MEFM 0x0080 /* We've seen Envelope-From: */
140 static struct headers NHeaders
[] = {
141 { "Return-Path", HBAD
, 0 },
142 { "Received", HBAD
, 0 },
143 { "Reply-To", HADR
|HNGR
, 0 },
144 { "From", HADR
|HNGR
, MFRM
},
145 { "Sender", HADR
|HNGR
|HONE
, MSND
},
147 { "Subject", HSUB
, 0 },
148 { "To", HADR
|HTRY
, MVIS
},
149 { "cc", HADR
|HTRY
, MVIS
},
150 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
151 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
152 { "Message-ID", HBAD
, 0 },
154 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
158 static struct headers RHeaders
[] = {
159 { "Resent-Reply-To", HADR
|HNGR
, 0 },
160 { "Resent-From", HADR
|HNGR
, MRFM
},
161 { "Resent-Sender", HADR
|HNGR
, MRSN
},
162 { "Resent-Date", HBAD
, 0 },
163 { "Resent-Subject", HSUB
, 0 },
164 { "Resent-To", HADR
|HTRY
, MVIS
},
165 { "Resent-cc", HADR
|HTRY
, MVIS
},
166 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
167 { "Resent-Message-ID", HBAD
, 0 },
168 { "Resent-Fcc", HFCC
, 0 },
169 { "Reply-To", HADR
, 0 },
170 { "From", HADR
|HNGR
, MFRM
},
171 { "Sender", HADR
|HNGR
, MSND
},
172 { "Date", HNOP
, MDAT
},
173 { "To", HADR
|HNIL
, 0 },
174 { "cc", HADR
|HNIL
, 0 },
175 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
177 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
181 static short fccind
= 0; /* index into fccfold[] */
182 static short outputlinelen
= OUTPUTLINELEN
;
184 static int pfd
= NOTOK
; /* fd to write annotation list to */
185 static int recipients
= 0; /* how many people will get a copy */
186 static int unkadr
= 0; /* how many of those were unknown */
187 static int badadr
= 0; /* number of bad addrs */
188 static int badmsg
= 0; /* message has bad semantics */
189 static int verbose
= 0; /* spell it out */
190 static int format
= 1; /* format addresses */
191 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
192 static int msgid
= 0; /* add msgid */
193 static int debug
= 0; /* debugging post */
194 static int watch
= 0; /* watch the delivery process */
195 static int whomsw
= 0; /* we are whom not post */
196 static int checksw
= 0; /* whom -check */
197 static int linepos
=0; /* putadr()'s position on the line */
198 static int nameoutput
=0; /* putadr() has output header name */
199 static int sasl
=0; /* Use SASL auth for SMTP */
200 static int saslssf
=-1; /* Our maximum SSF for SASL */
201 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
202 static char *user
=NULL
; /* Authenticate as this user */
203 static char *port
="submission"; /* Name of server port for SMTP submission */
204 static int tls
=-1; /* Use TLS for encryption */
205 static int fromcount
=0; /* Count of addresses on From: header */
206 static int seensender
=0; /* Have we seen a Sender: header? */
208 static unsigned msgflags
= 0; /* what we've seen */
212 static int msgstate
= NORMAL
;
214 static time_t tclock
= 0; /* the time we started (more or less) */
216 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
218 static char tmpfil
[BUFSIZ
];
219 static char bccfil
[BUFSIZ
];
221 static char from
[BUFSIZ
]; /* my network address */
222 static char sender
[BUFSIZ
]; /* my Sender: header */
223 static char efrom
[BUFSIZ
]; /* my Envelope-From: header */
224 static char fullfrom
[BUFSIZ
]; /* full contents of From header */
225 static char *filter
= NULL
; /* the filter for BCC'ing */
226 static char *subject
= NULL
; /* the subject field for BCC'ing */
227 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
229 static struct headers
*hdrtab
; /* table for the message we're doing */
231 static struct mailname localaddrs
; /* local addrs */
232 static struct mailname netaddrs
; /* network addrs */
233 static struct mailname uuaddrs
; /* uucp addrs */
234 static struct mailname tmpaddrs
; /* temporary queue */
236 static int snoop
= 0;
237 static char *clientsw
= NULL
;
238 static char *serversw
= NULL
;
240 static char prefix
[] = "----- =_aaaaaaaaaa";
242 static char *partno
= NULL
;
247 static void putfmt (char *, char *, FILE *);
248 static void start_headers (void);
249 static void finish_headers (FILE *);
250 static int get_header (char *, struct headers
*);
251 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
252 static void putgrp (char *, char *, FILE *, unsigned int);
253 static int insert (struct mailname
*);
254 static void pl (void);
255 static void anno (void);
256 static int annoaux (struct mailname
*);
257 static void insert_fcc (struct headers
*, char *);
258 static void make_bcc_file (int);
259 static void verify_all_addresses (int, char *);
260 static void chkadr (void);
261 static void sigon (void);
262 static void sigoff (void);
263 static void p_refile (char *);
264 static void fcc (char *, char *);
265 static void die (char *, char *, ...);
266 static void post (char *, int, int, char *);
267 static void do_text (char *file
, int fd
);
268 static void do_an_address (struct mailname
*, int);
269 static void do_addresses (int, int);
270 static int find_prefix (void);
274 main (int argc
, char **argv
)
276 int state
, compnum
, dashstuff
= 0;
277 char *cp
, *msg
= NULL
, **argp
, **arguments
, *envelope
;
278 char buf
[BUFSIZ
], name
[NAMESZ
];
280 m_getfld_state_t gstate
= 0;
282 if (nmh_init(argv
[0], 0 /* use context_foil() */)) { return 1; }
284 mts_init (invo_name
);
285 arguments
= getarguments (invo_name
, argc
, argv
, 0);
288 while ((cp
= *argp
++)) {
290 switch (smatch (++cp
, switches
)) {
292 ambigsw (cp
, switches
);
295 adios (NULL
, "-%s unknown", cp
);
298 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
299 print_help (buf
, switches
, 0);
302 print_version(invo_name
);
306 if (!(cp
= *argp
++) || *cp
== '-')
307 adios (NULL
, "missing argument to %s", argp
[-2]);
308 /* create a minimal context */
309 if (context_foil (cp
) == -1)
314 if (!(cp
= *argp
++) || *cp
== '-')
315 adios (NULL
, "missing argument to %s", argp
[-2]);
316 if ((state
= alias (cp
)) != AK_OK
)
317 adios (NULL
, "aliasing error in %s - %s",
318 cp
, akerror (state
));
337 if (!(filter
= *argp
++) || *filter
== '-')
338 adios (NULL
, "missing argument to %s", argp
[-2]);
393 if (!(cp
= *argp
++) || *cp
== '-')
394 adios (NULL
, "missing argument to %s", argp
[-2]);
395 if ((outputlinelen
= atoi (cp
)) < 10)
396 adios (NULL
, "impossible width %d", outputlinelen
);
400 if (!(cp
= *argp
++) || *cp
== '-')
401 adios (NULL
, "missing argument to %s", argp
[-2]);
402 if ((pfd
= atoi (cp
)) <= 2)
403 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
407 if (!(clientsw
= *argp
++) || *clientsw
== '-')
408 adios (NULL
, "missing argument to %s", argp
[-2]);
411 if (!(serversw
= *argp
++) || *serversw
== '-')
412 adios (NULL
, "missing argument to %s", argp
[-2]);
419 if (!(partno
= *argp
++) || *partno
== '-')
420 adios (NULL
, "missing argument to %s", argp
[-2]);
432 if (!(cp
= *argp
++) || *cp
== '-')
433 adios (NULL
, "missing argument to %s", argp
[-2]);
438 if (!(saslmech
= *argp
++) || *saslmech
== '-')
439 adios (NULL
, "missing argument to %s", argp
[-2]);
443 if (!(user
= *argp
++) || *user
== '-')
444 adios (NULL
, "missing argument to %s", argp
[-2]);
448 if (!(port
= *argp
++) || *port
== '-')
449 adios (NULL
, "missing argument to %s", argp
[-2]);
465 if (!(cp
= *argp
++) || *cp
== '-')
466 adios (NULL
, "missing argument to %s", argp
[-2]);
471 if (!(cp
= *argp
++) || *cp
== '-')
472 adios (NULL
, "missing argument to %s", argp
[-2]);
477 if (!(cp
= *argp
++) || *cp
== '-')
478 adios (NULL
, "missing argument to %s", argp
[-2]);
479 save_mts_method (cp
);
482 case CREDENTIALSSW
: {
483 if (!(cp
= *argp
++) || *cp
== '-')
484 adios (NULL
, "missing argument to %s", argp
[-2]);
485 add_profile_entry ("credentials", cp
);
490 if (!(cp
= *argp
++) || *cp
== '-')
491 adios (NULL
, "missing argument to %s", argp
[-2]);
492 if (save_message_id_style (cp
) != 0)
493 adios (NULL
, "unsupported messageid \"%s\"", cp
);
498 adios (NULL
, "only one message at a time!");
506 adios (NULL
, "usage: %s [switches] file", invo_name
);
508 if (outputlinelen
< 10)
509 adios (NULL
, "impossible width %d", outputlinelen
);
511 if ((in
= fopen (msg
, "r")) == NULL
)
512 adios (msg
, "unable to open");
520 if ((out
= fopen ("/dev/null", "w")) == NULL
)
521 adios ("/dev/null", "unable to open");
523 char *cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
525 adios(NULL
, "unable to create temporary file in %s",
528 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
532 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
534 for (compnum
= 1;;) {
535 int bufsz
= sizeof buf
;
536 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
540 cp
= add (buf
, NULL
);
541 while (state
== FLDPLUS
) {
543 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
546 putfmt (name
, cp
, out
);
551 finish_headers (out
);
554 fprintf (out
, "\n%s", buf
);
555 while (state
== BODY
) {
557 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
563 finish_headers (out
);
568 adios (NULL
, "message format error in component #%d", compnum
);
571 adios (NULL
, "getfld() returned %d", state
);
575 m_getfld_state_destroy (&gstate
);
589 * Here's how we decide which address to use as the envelope-from
592 * - If we were given an Envelope-From header, use that.
593 * - If we were given a Sender: address, use that.
594 * - Otherwise, use the address on the From: line
597 if (msgflags
& MEFM
) {
599 } else if (seensender
) {
608 * The user didn't specify any of the tls switches. Try to
609 * help them by implying -initialtls if they're using port 465
610 * (smtps, until IANA revoked that registration in 1998).
612 tls
= ! strcmp (port
, "465") || ! strcasecmp (port
, "smtps")
615 #else /* ! TLS_SUPPORT */
617 #endif /* ! TLS_SUPPORT */
620 /* If we are doing a "whom" check */
622 /* This won't work with MTS_SENDMAIL_PIPE. */
623 verify_all_addresses (1, envelope
);
627 if (msgflags
& MINV
) {
628 make_bcc_file (dashstuff
);
629 if (msgflags
& MVIS
) {
630 if (sm_mts
!= MTS_SENDMAIL_PIPE
) {
631 /* It would be nice to have support to call
632 verify_all_addresses with MTS_SENDMAIL_PIPE, but
633 that might require running sendmail as root. Note
634 that spost didn't verify addresses. */
635 verify_all_addresses (verbose
, envelope
);
637 post (tmpfil
, 0, verbose
, envelope
);
639 post (bccfil
, 1, verbose
, envelope
);
640 (void) m_unlink (bccfil
);
642 post (tmpfil
, 0, isatty (1), envelope
);
646 (void) m_unlink (tmpfil
);
650 printf ("Partial Message #%s Processed\n", partno
);
652 printf ("Message Processed\n");
665 putfmt (char *name
, char *str
, FILE *out
)
667 int count
, grp
, i
, keep
;
669 char namep
[BUFSIZ
], error
[BUFSIZ
];
670 struct mailname
*mp
= NULL
, *np
= NULL
;
673 while (*str
== ' ' || *str
== '\t')
676 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
677 advise (NULL
, "illegal header line -- %s:", name
);
682 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
683 if (strncasecmp (name
, "nmh-", 4)) {
684 fprintf (out
, "%s: %s", name
, str
);
686 /* Filter out all Nmh-* headers, because Norm asked. They
687 should never have reached this point. Warn about any
688 that are non-empty. */
689 if (strcmp (str
, "\n")) {
690 char *newline
= strchr (str
, '\n');
691 if (newline
) *newline
= '\0';
693 advise (NULL
, "ignoring header line -- %s: %s", name
, str
);
702 if (hdr
->flags
& HIGN
) {
705 if (hdr
->flags
& HBAD
) {
706 advise (NULL
, "illegal header line -- %s:", name
);
710 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
712 if (hdr
->flags
& HSUB
)
713 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
714 if (hdr
->flags
& HFCC
) {
715 if ((cp
= strrchr(str
, '\n')))
717 for (pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
719 insert_fcc (hdr
, pp
);
721 insert_fcc (hdr
, pp
);
725 if (!(hdr
->flags
& HADR
)) {
726 fprintf (out
, "%s: %s", name
, str
);
730 tmpaddrs
.m_next
= NULL
;
731 for (count
= 0; (cp
= getname (str
)); count
++)
732 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
)))) {
736 tmpaddrs
.m_next
= mp
;
740 admonish(cp
, "%s", error
);
741 if (hdr
->flags
& HTRY
)
748 if (hdr
->flags
& HNIL
)
749 fprintf (out
, "%s: %s", name
, str
);
752 * Sender (or Resent-Sender) can have only one address
754 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
755 : (hdr
->set
& MSND
)) {
756 advise (NULL
, "%s: field requires one address", name
);
760 advise (NULL
, "%s: field requires at least one address", name
);
767 if (count
> 1 && (hdr
->flags
& HONE
)) {
768 advise (NULL
, "%s: field only permits one address", name
);
773 nameoutput
= linepos
= 0;
774 snprintf (namep
, sizeof(namep
), "%s%s",
775 (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
777 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
778 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
779 /* The address doesn't include a host, so it might be an alias. */
780 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
781 qp
= akvisible () ? mp
->m_mbox
: "";
784 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
785 while ((cp
= getname (pp
))) {
786 if (!(mp
= getm (cp
, NULL
, 0, error
, sizeof(error
)))) {
787 admonish(cp
, "%s", error
);
793 * If it's a From: or Resent-From: header, save the address
794 * for later possible use (as the envelope address for SMTP)
797 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
798 : (hdr
->set
& MFRM
)) {
799 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
800 from
[sizeof(from
) - 1] = '\0';
805 * Also save the Sender: or Resent-Sender: header as well
808 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
809 : (hdr
->set
& MSND
)) {
810 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
811 sender
[sizeof(sender
) - 1] = '\0';
816 * ALSO ... save Envelope-From
819 if (hdr
->set
& MEFM
) {
820 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
821 efrom
[sizeof(efrom
) - 1] = '\0';
824 if (hdr
->flags
& HBCC
)
827 mp
->m_ingrp
= np
->m_ingrp
;
830 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
832 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
833 /* Catch this before sendmail chokes with:
834 "553 List:; syntax illegal for recipient
836 If we wanted to, we could expand out blind
837 aliases and put them in Bcc:, but then
838 they'd have the Blind-Carbon-Copy
841 "blind lists not compatible with"
847 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
848 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
857 /* Address includes a host, so no alias substitution is needed. */
860 * If it's a From: or Resent-From header, save the address
861 * for later possible use (as the envelope address for SMTP)
864 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
865 : (hdr
->set
& MFRM
)) {
866 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
871 * Also save the Sender: header as well
874 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
875 : (hdr
->set
& MSND
)) {
876 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
877 sender
[sizeof(sender
) - 1] = '\0';
882 * ALSO ... save Envelope-From
885 if (hdr
->set
& MEFM
) {
886 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
887 efrom
[sizeof(efrom
) - 1] = '\0';
890 if (hdr
->flags
& HBCC
)
893 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
896 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
900 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
907 * If this is a From:/Resent-From: header, save the full thing for
908 * later in case we need it for use when constructing a Bcc draft message
911 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
) : (hdr
->set
& MFRM
)) {
912 strncpy(fullfrom
, str
, sizeof(fullfrom
));
913 fullfrom
[sizeof(fullfrom
) - 1] = 0;
915 * Strip off any trailing newlines
918 while (strlen(fullfrom
) > 0 && fullfrom
[strlen(fullfrom
) - 1] == '\n') {
919 fullfrom
[strlen(fullfrom
) - 1] = '\0';
923 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
924 advise (NULL
, "%s: field does not allow groups", name
);
939 * Probably not necessary, but just in case ...
950 * Now that we've outputted the header fields in the draft
951 * message, we will now output any remaining header fields
952 * that we need to add/create.
956 finish_headers (FILE *out
)
960 if (!(msgflags
& MFRM
)) {
962 * A From: header is now required in the draft.
964 advise (NULL
, "message has no From: header");
965 advise (NULL
, "See default components files for examples");
970 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
971 advise (NULL
, "A Sender: or Envelope-From: header is required "
972 "with multiple\nFrom: addresses");
980 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
982 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
984 * If we have multiple From: addresses, make sure we have an
985 * Sender: header. If we don't have one, then generate one
986 * from Envelope-From: (which in this case, cannot be blank)
989 if (fromcount
> 1 && seensender
== 0) {
990 if (efrom
[0] == '\0') {
991 advise (NULL
, "Envelope-From cannot be blank when there "
992 "is multiple From: addresses\nand no Sender: "
996 fprintf (out
, "Sender: %s\n", efrom
);
1000 if (!(msgflags
& MVIS
))
1001 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
1005 if (!(msgflags
& MDAT
)) {
1006 advise (NULL
, "message has no Date: header");
1009 if (!(msgflags
& MFRM
)) {
1010 advise (NULL
, "message has no From: header");
1013 if (!(msgflags
& MRFM
)) {
1014 advise (NULL
, "message has no Resent-From: header");
1015 advise (NULL
, "See default components files for examples");
1019 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
1020 advise (NULL
, "A Resent-Sender: or Envelope-From: header is "
1021 "required with multiple\nResent-From: addresses");
1029 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
1031 fprintf (out
, "Resent-Message-ID: %s\n",
1032 message_id (tclock
, 0));
1034 * If we have multiple Resent-From: addresses, make sure we have an
1035 * Resent-Sender: header. If we don't have one, then generate one
1036 * from Envelope-From (which in this case, cannot be blank)
1039 if (fromcount
> 1 && seensender
== 0) {
1040 if (efrom
[0] == '\0') {
1041 advise (NULL
, "Envelope-From cannot be blank when there "
1042 "is multiple Resent-From: addresses and no "
1043 "Resent-Sender: header");
1046 fprintf (out
, "Resent-Sender: %s\n", efrom
);
1050 if (!(msgflags
& MVIS
))
1051 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1056 adios (NULL
, "re-format message and try again");
1058 adios (NULL
, "no addressees");
1063 get_header (char *header
, struct headers
*table
)
1067 for (h
= table
; h
->value
; h
++)
1068 if (!strcasecmp (header
? header
: "", h
->value
? h
->value
: ""))
1076 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
1080 char buffer
[BUFSIZ
];
1082 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
1084 if (sm_mts
!= MTS_SENDMAIL_PIPE
&&
1085 ((flags
& (HBCC
| HDCC
| HEFM
)) || mp
->m_ingrp
))
1089 fprintf (out
, "%s: ", name
);
1090 linepos
+= (nameoutput
= strlen (name
) + 2);
1093 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1094 mp
->m_pers
= getcpy (aka
);
1097 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1100 cp
= adrformat (mp
);
1107 if (linepos
!= nameoutput
) {
1108 if (len
+ linepos
+ 2 > outputlinelen
)
1109 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1119 return (flags
& HTRY
);
1124 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1129 if (sm_mts
!= MTS_SENDMAIL_PIPE
&& (flags
& HBCC
))
1133 fprintf (out
, "%s: ", name
);
1134 linepos
+= (nameoutput
= strlen (name
) + 2);
1137 cp
= concat (group
, ";", NULL
);
1140 if (linepos
> nameoutput
) {
1141 if (len
+ linepos
+ 2 > outputlinelen
) {
1142 fprintf (out
, ",\n%*s", nameoutput
, "");
1143 linepos
= nameoutput
;
1157 insert (struct mailname
*np
)
1159 struct mailname
*mp
;
1161 if (np
->m_mbox
== NULL
)
1164 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1165 : np
->m_type
== UUCPHOST
? &uuaddrs
1169 if (!strcasecmp (np
->m_host
? np
->m_host
: "",
1170 mp
->m_next
->m_host
? mp
->m_next
->m_host
: "") &&
1171 !strcasecmp (np
->m_mbox
? np
->m_mbox
: "",
1172 mp
->m_next
->m_mbox
? mp
->m_next
->m_mbox
: "") &&
1173 np
->m_bcc
== mp
->m_next
->m_bcc
)
1186 struct mailname
*mp
;
1188 printf ("-------\n\t-- Addresses --\nlocal:\t");
1189 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1190 printf ("%s%s%s", mp
->m_mbox
,
1191 mp
->m_bcc
? "[BCC]" : "",
1192 mp
->m_next
? ",\n\t" : "");
1194 printf ("\nnet:\t");
1195 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1196 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1197 mp
->m_mbox
, mp
->m_host
,
1198 mp
->m_bcc
? "[BCC]" : "",
1199 mp
->m_next
? ",\n\t" : "");
1201 printf ("\nuucp:\t");
1202 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1203 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1204 mp
->m_bcc
? "[BCC]" : "",
1205 mp
->m_next
? ",\n\t" : "");
1207 printf ("\n\t-- Folder Copies --\nfcc:\t");
1208 for (i
= 0; i
< fccind
; i
++)
1209 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1217 struct mailname
*mp
;
1219 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1220 if (annoaux (mp
) == NOTOK
)
1223 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1224 if (annoaux (mp
) == NOTOK
)
1227 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1228 if (annoaux (mp
) == NOTOK
)
1238 annoaux (struct mailname
*mp
)
1241 char buffer
[BUFSIZ
];
1243 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1244 i
= strlen (buffer
);
1246 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1251 insert_fcc (struct headers
*hdr
, char *pp
)
1255 for (cp
= pp
; isspace ((unsigned char) *cp
); cp
++)
1257 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace ((unsigned char) *pp
); pp
--)
1265 adios (NULL
, "too many %ss", hdr
->value
);
1266 fccfold
[fccind
++] = getcpy (cp
);
1274 make_bcc_file (int dashstuff
)
1280 char *tfile
= NULL
, *program
;
1282 if ((tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
)) == NULL
) {
1283 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
1285 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1287 fprintf (out
, "From: %s\n", fullfrom
);
1288 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1290 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1292 fprintf (out
, "Subject: %s", subject
);
1293 fprintf (out
, "BCC:\n");
1296 * Use MIME encapsulation for Bcc messages
1302 * Check if any lines in the message clash with the
1303 * prefix for the MIME multipart separator. If there
1304 * is a clash, increment one of the letters in the
1305 * prefix and check again.
1307 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1308 adios (NULL
, "lost prefix start");
1309 while (find_prefix () == NOTOK
) {
1314 adios (NULL
, "can't find a unique delimiter string");
1319 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1320 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1321 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1323 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1329 * Do mhl filtering of Bcc messages instead
1330 * of MIME encapsulation.
1332 if (filter
!= NULL
) {
1333 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1337 adios ("fork", "unable to");
1340 dup2 (fileno (out
), 1);
1342 vec
= argsplit(mhlproc
, &program
, &i
);
1343 vec
[i
++] = "-forward";
1348 /* was the flag -[no]dashstuffing specified? */
1350 vec
[i
++] = "-dashstuffing";
1351 else if (dashstuff
< 0)
1352 vec
[i
++] = "-nodashstuffing";
1355 execvp (program
, vec
);
1356 fprintf (stderr
, "unable to exec ");
1361 pidXwait (child_id
, mhlproc
);
1365 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1366 adios (tmpfil
, "unable to re-open");
1369 * If using MIME encapsulation, or if the -nodashstuffing
1370 * flag was given, then just copy message. Else do
1371 * RFC934 quoting (dashstuffing).
1373 if (mime
|| dashstuff
< 0)
1374 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1376 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1380 fseek (out
, 0L, SEEK_END
);
1382 fprintf (out
, "\n--%s--\n", prefix
);
1384 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1390 * Scan message to check if any lines clash with
1391 * the prefix of the MIME multipart separator.
1398 char buffer
[BUFSIZ
];
1401 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1402 adios (tmpfil
, "unable to re-open");
1404 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1405 if (buffer
[0] == '-' && buffer
[1] == '-') {
1408 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1409 if (!isspace ((unsigned char) *cp
))
1412 if (strcmp (buffer
+ 2, prefix
) == 0) {
1423 #define plural(x) (x == 1 ? "" : "s")
1428 if (badadr
&& unkadr
)
1429 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1430 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1432 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1434 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1439 do_addresses (int bccque
, int talk
)
1443 struct mailname
*lp
;
1446 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1447 if (lp
->m_bcc
? bccque
: !bccque
) {
1449 printf (" -- Local Recipients --\n");
1450 do_an_address (lp
, talk
);
1455 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1456 if (lp
->m_bcc
? bccque
: !bccque
) {
1458 printf (" -- UUCP Recipients --\n");
1459 do_an_address (lp
, talk
);
1464 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1465 if (lp
->m_bcc
? bccque
: !bccque
) {
1467 printf (" -- Network Recipients --\n");
1468 do_an_address (lp
, talk
);
1474 if (rp_isbad (retval
= sm_waend ()))
1475 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1480 * MTS-SPECIFIC INTERACTION
1485 * SENDMAIL/SMTP routines
1489 post (char *file
, int bccque
, int talk
, char *envelope
)
1496 if (msgflags
& MINV
)
1497 printf (" -- Posting for %s Recipients --\n",
1498 bccque
? "Blind" : "Sighted");
1500 printf (" -- Posting for All Recipients --\n");
1505 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
1506 char **argp
, *program
;
1509 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1513 adios ("fork", "unable to");
1516 if (freopen( file
, "r", stdin
) == NULL
) {
1517 adios (file
, "can't reopen for sendmail");
1520 argp
= argsplit(sendmail
, &program
, &argc
);
1521 argp
[argc
++] = "-t"; /* read msg for recipients */
1522 argp
[argc
++] = "-i"; /* don't stop on "." */
1524 argp
[argc
++] = "-bv";
1526 argp
[argc
++] = "-v";
1529 execv (program
, argp
);
1530 adios (sendmail
, "can't exec");
1533 pidXwait (child_id
, NULL
);
1537 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1538 verbose
, snoop
, sasl
, saslssf
,
1539 saslmech
, user
, tls
)) ||
1540 rp_isbad (retval
= sm_winit (envelope
)))
1541 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1543 do_addresses (bccque
, talk
&& verbose
);
1544 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1545 die (file
, "unable to re-open");
1550 sm_end (!(msgflags
& MINV
) || bccque
? OK
: DONE
);
1554 if (msgflags
& MINV
)
1555 printf (" -- %s Recipient Copies Posted --\n",
1556 bccque
? "Blind" : "Sighted");
1558 printf (" -- Recipient Copies Posted --\n");
1566 /* Address Verification */
1569 verify_all_addresses (int talk
, char *envelope
)
1572 struct mailname
*lp
;
1576 if (!whomsw
|| checksw
)
1577 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1578 verbose
, snoop
, sasl
, saslssf
,
1579 saslmech
, user
, tls
))
1580 || rp_isbad (retval
= sm_winit (envelope
)))
1581 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1583 if (talk
&& !whomsw
)
1584 printf (" -- Address Verification --\n");
1585 if (talk
&& localaddrs
.m_next
)
1586 printf (" -- Local Recipients --\n");
1587 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1588 do_an_address (lp
, talk
);
1590 if (talk
&& uuaddrs
.m_next
)
1591 printf (" -- UUCP Recipients --\n");
1592 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1593 do_an_address (lp
, talk
);
1595 if (talk
&& netaddrs
.m_next
)
1596 printf (" -- Network Recipients --\n");
1597 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1598 do_an_address (lp
, talk
);
1601 if (talk
&& !whomsw
)
1602 printf (" -- Address Verification Successful --\n");
1604 if (!whomsw
|| checksw
)
1613 do_an_address (struct mailname
*lp
, int talk
)
1619 switch (lp
->m_type
) {
1623 strncpy (addr
, mbox
, sizeof(addr
));
1627 mbox
= auxformat (lp
, 0);
1629 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1632 default: /* let SendMail decide if the host is bad */
1635 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1640 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1642 if (whomsw
&& !checksw
) {
1650 switch (retval
= sm_wadr (mbox
, host
,
1651 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1654 printf ("address ok\n");
1660 fprintf (stderr
, " %s: ", addr
);
1661 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1662 rp_string (retval
));
1668 fprintf (stderr
, " %s: ", addr
);
1669 die (NULL
, "unexpected response; %s", rp_string (retval
));
1677 do_text (char *file
, int fd
)
1682 lseek (fd
, (off_t
) 0, SEEK_SET
);
1684 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1685 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1686 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1690 die (file
, "problem reading from");
1692 switch (retval
= sm_wtend ()) {
1698 die (NULL
, "posting failed; %s", rp_string (retval
));
1701 die (NULL
, "unexpected response; %s", rp_string (retval
));
1715 (void) m_unlink (tmpfil
);
1716 if (msgflags
& MINV
)
1717 (void) m_unlink (bccfil
);
1719 if (!whomsw
|| checksw
)
1732 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1733 istat
= SIGNAL2 (SIGINT
, sigser
);
1734 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1735 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1745 SIGNAL (SIGHUP
, hstat
);
1746 SIGNAL (SIGINT
, istat
);
1747 SIGNAL (SIGQUIT
, qstat
);
1748 SIGNAL (SIGTERM
, tstat
);
1756 p_refile (char *file
)
1764 printf (" -- Filing Folder Copies --\n");
1765 for (i
= 0; i
< fccind
; i
++)
1766 fcc (file
, fccfold
[i
]);
1768 printf (" -- Folder Copies Filed --\n");
1773 * Call the `fileproc' to add the file to the folder.
1777 fcc (char *file
, char *folder
)
1780 int i
, status
, argp
;
1782 char **arglist
, *program
;
1785 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1788 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1794 fprintf (stderr
, " %sFcc %s: ",
1795 msgstate
== RESENT
? "Resent-" : "", folder
);
1796 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1800 /* see if we need to add `+' */
1801 snprintf (fold
, sizeof(fold
), "%s%s",
1802 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1804 /* now exec the fileproc */
1806 arglist
= argsplit(fileproc
, &program
, &argp
);
1807 arglist
[argp
++] = "-link";
1808 arglist
[argp
++] = "-file";
1809 arglist
[argp
++] = file
;
1810 arglist
[argp
++] = fold
;
1811 arglist
[argp
] = NULL
;
1812 execvp (program
, arglist
);
1816 if ((status
= pidwait (child_id
, OK
))) {
1818 fprintf (stderr
, " %sFcc %s: ",
1819 msgstate
== RESENT
? "Resent-" : "", folder
);
1820 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1823 printf ("folder ok\n");
1835 die (char *what
, char *fmt
, ...)
1839 (void) m_unlink (tmpfil
);
1840 if (msgflags
& MINV
)
1841 (void) m_unlink (bccfil
);
1843 if (!whomsw
|| checksw
)
1847 advertise (what
, NULL
, fmt
, ap
);