]>
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>
25 #ifdef HAVE_SYS_TIME_H
26 # include <sys/time.h>
30 #include <mts/smtp/smtp.h>
33 # define SASLminc(a) (a)
34 #else /* CYRUS_SASL */
35 # define SASLminc(a) 0
36 #endif /* CYRUS_SASL */
39 # define TLSminc(a) (a)
40 #else /* TLS_SUPPORT */
42 #endif /* TLS_SUPPORT */
44 #define FCCS 10 /* max number of fccs allowed */
46 /* In the following array of structures, the numeric second field of the
47 structures (minchars) is apparently used like this:
49 -# : Switch can be abbreviated to # characters; switch hidden in -help.
50 0 : Switch can't be abbreviated; switch shown in -help.
51 # : Switch can be abbreviated to # characters; switch shown in -help. */
53 #define POST_SWITCHES \
54 X("alias aliasfile", 0, ALIASW) \
55 X("check", -5, CHKSW) /* interface from whom */ \
56 X("nocheck", -7, NCHKSW) /* interface from whom */ \
57 X("debug", -5, DEBUGSW) \
58 X("dist", -4, DISTSW) /* interface from dist */ \
59 X("filter filterfile", 0, FILTSW) \
60 X("nofilter", 0, NFILTSW) \
61 X("format", 0, FRMTSW) \
62 X("noformat", 0, NFRMTSW) \
63 X("library directory", -7, LIBSW) /* interface from send, whom */ \
64 X("mime", 0, MIMESW) \
65 X("nomime", 0, NMIMESW) \
66 X("msgid", 0, MSGDSW) \
67 X("nomsgid", 0, NMSGDSW) \
68 X("verbose", 0, VERBSW) \
69 X("noverbose", 0, NVERBSW) \
70 X("watch", 0, WATCSW) \
71 X("nowatch", 0, NWATCSW) \
72 X("whom", -4, WHOMSW) /* interface from whom */ \
73 X("width columns", 0, WIDTHSW) \
74 X("version", 0, VERSIONSW) \
75 X("help", 0, HELPSW) \
76 X("dashstuffing", -12, BITSTUFFSW) /* should we dashstuff BCC messages? */ \
77 X("nodashstuffing", -14, NBITSTUFFSW) \
78 X("idanno number", -6, ANNOSW) /* interface from send */ \
79 X("client host", -6, CLIESW) \
80 X("server host", 6, SERVSW) /* specify alternate SMTP server */ \
81 X("snoop", -5, SNOOPSW) /* snoop the SMTP transaction */ \
82 X("partno", -6, PARTSW) \
83 X("queued", -6, QUEUESW) \
84 X("sasl", SASLminc(-4), SASLSW) \
85 X("nosasl", SASLminc(-6), NOSASLSW) \
86 X("saslmaxssf", SASLminc(-10), SASLMXSSFSW) \
87 X("saslmech", SASLminc(-5), SASLMECHSW) \
88 X("user", SASLminc(-4), USERSW) \
89 X("port server port name/number", 4, PORTSW) \
90 X("tls", TLSminc(-3), TLSSW) \
91 X("notls", TLSminc(-5), NTLSSW) \
92 X("fileproc", -4, FILEPROCSW) \
93 X("mhlproc", -3, MHLPROCSW) \
94 X("mts smtp|sendmail/smtp|sendmail/pipe", 2, MTSSW) \
95 X("messageid localname|random", 2, MESSAGEIDSW) \
97 #define X(sw, minchars, id) id,
98 DEFINE_SWITCH_ENUM(POST
);
101 #define X(sw, minchars, id) { sw, minchars, id },
102 DEFINE_SWITCH_ARRAY(POST
, switches
);
113 * flags for headers->flags
115 #define HNOP 0x0000 /* just used to keep .set around */
116 #define HBAD 0x0001 /* bad header - don't let it through */
117 #define HADR 0x0002 /* header has an address field */
118 #define HSUB 0x0004 /* Subject: header */
119 #define HTRY 0x0008 /* try to send to addrs on header */
120 #define HBCC 0x0010 /* don't output this header, unless MTS_SENDMAIL_PIPE */
121 #define HMNG 0x0020 /* munge this header */
122 #define HNGR 0x0040 /* no groups allowed in this header */
123 #define HFCC 0x0080 /* FCC: type header */
124 #define HNIL 0x0100 /* okay for this header not to have addrs */
125 #define HIGN 0x0200 /* ignore this header */
126 #define HDCC 0x0400 /* another undocumented feature */
127 #define HONE 0x0800 /* Only (zero or) one address allowed */
128 #define HEFM 0x1000 /* Envelope-From: header */
131 * flags for headers->set
133 #define MFRM 0x0001 /* we've seen a From: */
134 #define MDAT 0x0002 /* we've seen a Date: */
135 #define MRFM 0x0004 /* we've seen a Resent-From: */
136 #define MVIS 0x0008 /* we've seen sighted addrs */
137 #define MINV 0x0010 /* we've seen blind addrs */
138 #define MSND 0x0020 /* we've seen a Sender: */
139 #define MRSN 0x0040 /* We've seen a Resent-Sendr:*/
140 #define MEFM 0x0080 /* We've seen Envelope-From: */
143 static struct headers NHeaders
[] = {
144 { "Return-Path", HBAD
, 0 },
145 { "Received", HBAD
, 0 },
146 { "Reply-To", HADR
|HNGR
, 0 },
147 { "From", HADR
|HNGR
, MFRM
},
148 { "Sender", HADR
|HNGR
|HONE
, MSND
},
150 { "Subject", HSUB
, 0 },
151 { "To", HADR
|HTRY
, MVIS
},
152 { "cc", HADR
|HTRY
, MVIS
},
153 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
154 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
155 { "Message-ID", HBAD
, 0 },
157 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
161 static struct headers RHeaders
[] = {
162 { "Resent-Reply-To", HADR
|HNGR
, 0 },
163 { "Resent-From", HADR
|HNGR
, MRFM
},
164 { "Resent-Sender", HADR
|HNGR
, MRSN
},
165 { "Resent-Date", HBAD
, 0 },
166 { "Resent-Subject", HSUB
, 0 },
167 { "Resent-To", HADR
|HTRY
, MVIS
},
168 { "Resent-cc", HADR
|HTRY
, MVIS
},
169 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
170 { "Resent-Message-ID", HBAD
, 0 },
171 { "Resent-Fcc", HFCC
, 0 },
172 { "Reply-To", HADR
, 0 },
173 { "From", HADR
|HNGR
, MFRM
},
174 { "Sender", HADR
|HNGR
, MSND
},
175 { "Date", HNOP
, MDAT
},
176 { "To", HADR
|HNIL
, 0 },
177 { "cc", HADR
|HNIL
, 0 },
178 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
180 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
184 static short fccind
= 0; /* index into fccfold[] */
185 static short outputlinelen
= OUTPUTLINELEN
;
187 static int pfd
= NOTOK
; /* fd to write annotation list to */
188 static uid_t myuid
= -1; /* my user id */
189 static gid_t mygid
= -1; /* my group id */
190 static int recipients
= 0; /* how many people will get a copy */
191 static int unkadr
= 0; /* how many of those were unknown */
192 static int badadr
= 0; /* number of bad addrs */
193 static int badmsg
= 0; /* message has bad semantics */
194 static int verbose
= 0; /* spell it out */
195 static int format
= 1; /* format addresses */
196 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
197 static int msgid
= 0; /* add msgid */
198 static int debug
= 0; /* debugging post */
199 static int watch
= 0; /* watch the delivery process */
200 static int whomsw
= 0; /* we are whom not post */
201 static int checksw
= 0; /* whom -check */
202 static int linepos
=0; /* putadr()'s position on the line */
203 static int nameoutput
=0; /* putadr() has output header name */
204 static int sasl
=0; /* Use SASL auth for SMTP */
205 static int saslssf
=-1; /* Our maximum SSF for SASL */
206 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
207 static char *user
=NULL
; /* Authenticate as this user */
208 static char *port
="smtp"; /* Name of server port for SMTP */
209 static int tls
=0; /* Use TLS for encryption */
210 static int fromcount
=0; /* Count of addresses on From: header */
211 static int seensender
=0; /* Have we seen a Sender: header? */
213 static unsigned msgflags
= 0; /* what we've seen */
217 static int msgstate
= NORMAL
;
219 static time_t tclock
= 0; /* the time we started (more or less) */
221 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
223 static char tmpfil
[BUFSIZ
];
224 static char bccfil
[BUFSIZ
];
226 static char from
[BUFSIZ
]; /* my network address */
227 static char sender
[BUFSIZ
]; /* my Sender: header */
228 static char efrom
[BUFSIZ
]; /* my Envelope-From: header */
229 static char fullfrom
[BUFSIZ
]; /* full contents of From header */
230 static char signature
[BUFSIZ
]; /* my signature */
231 static char *filter
= NULL
; /* the filter for BCC'ing */
232 static char *subject
= NULL
; /* the subject field for BCC'ing */
233 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
235 static struct headers
*hdrtab
; /* table for the message we're doing */
237 static struct mailname localaddrs
; /* local addrs */
238 static struct mailname netaddrs
; /* network addrs */
239 static struct mailname uuaddrs
; /* uucp addrs */
240 static struct mailname tmpaddrs
; /* temporary queue */
242 static int snoop
= 0;
243 static char *clientsw
= NULL
;
244 static char *serversw
= NULL
;
246 extern struct smtp sm_reply
;
248 static char prefix
[] = "----- =_aaaaaaaaaa";
250 static char *partno
= NULL
;
251 static int queued
= 0;
256 static void putfmt (char *, char *, FILE *);
257 static void start_headers (void);
258 static void finish_headers (FILE *);
259 static int get_header (char *, struct headers
*);
260 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
261 static void putgrp (char *, char *, FILE *, unsigned int);
262 static int insert (struct mailname
*);
263 static void pl (void);
264 static void anno (void);
265 static int annoaux (struct mailname
*);
266 static void insert_fcc (struct headers
*, unsigned char *);
267 static void make_bcc_file (int);
268 static void verify_all_addresses (int, char *);
269 static void chkadr (void);
270 static void sigon (void);
271 static void sigoff (void);
272 static void p_refile (char *);
273 static void fcc (char *, char *);
274 static void die (char *, char *, ...);
275 static void post (char *, int, int, char *);
276 static void do_text (char *file
, int fd
);
277 static void do_an_address (struct mailname
*, int);
278 static void do_addresses (int, int);
279 static int find_prefix (void);
283 main (int argc
, char **argv
)
285 int state
, compnum
, dashstuff
= 0;
286 char *cp
, *msg
= NULL
, **argp
, **arguments
, *envelope
;
287 char buf
[BUFSIZ
], name
[NAMESZ
];
289 m_getfld_state_t gstate
= 0;
292 setlocale(LC_ALL
, "");
294 invo_name
= r1bindex (argv
[0], '/');
296 /* foil search of user profile/context */
297 if (context_foil (NULL
) == -1)
300 mts_init (invo_name
);
301 arguments
= getarguments (invo_name
, argc
, argv
, 0);
304 while ((cp
= *argp
++)) {
306 switch (smatch (++cp
, switches
)) {
308 ambigsw (cp
, switches
);
311 adios (NULL
, "-%s unknown", cp
);
314 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
315 print_help (buf
, switches
, 0);
318 print_version(invo_name
);
322 if (!(cp
= *argp
++) || *cp
== '-')
323 adios (NULL
, "missing argument to %s", argp
[-2]);
324 /* create a minimal context */
325 if (context_foil (cp
) == -1)
330 if (!(cp
= *argp
++) || *cp
== '-')
331 adios (NULL
, "missing argument to %s", argp
[-2]);
332 if ((state
= alias (cp
)) != AK_OK
)
333 adios (NULL
, "aliasing error in %s - %s",
334 cp
, akerror (state
));
353 if (!(filter
= *argp
++) || *filter
== '-')
354 adios (NULL
, "missing argument to %s", argp
[-2]);
409 if (!(cp
= *argp
++) || *cp
== '-')
410 adios (NULL
, "missing argument to %s", argp
[-2]);
411 if ((outputlinelen
= atoi (cp
)) < 10)
412 adios (NULL
, "impossible width %d", outputlinelen
);
416 if (!(cp
= *argp
++) || *cp
== '-')
417 adios (NULL
, "missing argument to %s", argp
[-2]);
418 if ((pfd
= atoi (cp
)) <= 2)
419 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
423 if (!(clientsw
= *argp
++) || *clientsw
== '-')
424 adios (NULL
, "missing argument to %s", argp
[-2]);
427 if (!(serversw
= *argp
++) || *serversw
== '-')
428 adios (NULL
, "missing argument to %s", argp
[-2]);
435 if (!(partno
= *argp
++) || *partno
== '-')
436 adios (NULL
, "missing argument to %s", argp
[-2]);
452 if (!(cp
= *argp
++) || *cp
== '-')
453 adios (NULL
, "missing argument to %s", argp
[-2]);
458 if (!(saslmech
= *argp
++) || *saslmech
== '-')
459 adios (NULL
, "missing argument to %s", argp
[-2]);
463 if (!(user
= *argp
++) || *user
== '-')
464 adios (NULL
, "missing argument to %s", argp
[-2]);
468 if (!(port
= *argp
++) || *port
== '-')
469 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
);
499 if (!(cp
= *argp
++) || *cp
== '-')
500 adios (NULL
, "missing argument to %s", argp
[-2]);
501 if (save_message_id_style (cp
) != 0)
502 adios (NULL
, "unsupported messageid \"%s\"", cp
);
507 adios (NULL
, "only one message at a time!");
515 adios (NULL
, "usage: %s [switches] file", invo_name
);
517 if (outputlinelen
< 10)
518 adios (NULL
, "impossible width %d", outputlinelen
);
520 if ((in
= fopen (msg
, "r")) == NULL
)
521 adios (msg
, "unable to open");
529 if ((out
= fopen ("/dev/null", "w")) == NULL
)
530 adios ("/dev/null", "unable to open");
532 char *cp
= m_mktemp(m_maildir(invo_name
), NULL
, &out
);
534 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
536 adios ("post", "unable to create temporary file");
539 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
540 chmod (tmpfil
, 0600);
544 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
546 for (compnum
= 1;;) {
547 int bufsz
= sizeof buf
;
548 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
552 cp
= add (buf
, NULL
);
553 while (state
== FLDPLUS
) {
555 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
558 putfmt (name
, cp
, out
);
563 finish_headers (out
);
566 fprintf (out
, "\n%s", buf
);
567 while (state
== BODY
) {
569 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
575 finish_headers (out
);
580 adios (NULL
, "message format error in component #%d", compnum
);
583 adios (NULL
, "getfld() returned %d", state
);
587 m_getfld_state_destroy (&gstate
);
601 * Here's how we decide which address to use as the envelope-from
604 * - If we were given an Envelope-From header, use that.
605 * - If we were given a Sender: address, use that.
606 * - Otherwise, use the address on the From: line
609 if (msgflags
& MEFM
) {
611 } else if (seensender
) {
617 /* If we are doing a "whom" check */
619 /* This won't work with MTS_SENDMAIL_PIPE. */
620 verify_all_addresses (1, envelope
);
624 if (msgflags
& MINV
) {
625 make_bcc_file (dashstuff
);
626 if (msgflags
& MVIS
) {
627 if (sm_mts
!= MTS_SENDMAIL_PIPE
) {
628 /* It would be nice to have support to call
629 verify_all_addresses with MTS_SENDMAIL_PIPE, but
630 that might require running sendmail as root. Note
631 that spost didn't verify addresses. */
632 verify_all_addresses (verbose
, envelope
);
634 post (tmpfil
, 0, verbose
, envelope
);
636 post (bccfil
, 1, verbose
, envelope
);
639 post (tmpfil
, 0, isatty (1), envelope
);
646 printf (partno
? "Partial Message #%s Processed\n" : "Message Processed\n",
658 putfmt (char *name
, char *str
, FILE *out
)
660 int count
, grp
, i
, keep
;
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, AD_HOST
, NULL
))) {
729 tmpaddrs
.m_next
= mp
;
733 if (hdr
->flags
& HTRY
)
739 if (hdr
->flags
& HNIL
)
740 fprintf (out
, "%s: %s", name
, str
);
743 * Sender (or Resent-Sender) can have only one address
745 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
746 : (hdr
->set
& MSND
)) {
747 advise (NULL
, "%s: field requires one address", name
);
751 advise (NULL
, "%s: field requires at least one address", name
);
758 if (count
> 1 && (hdr
->flags
& HONE
)) {
759 advise (NULL
, "%s: field only permits one address", name
);
764 nameoutput
= linepos
= 0;
765 snprintf (namep
, sizeof(namep
), "%s%s",
766 (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
768 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
769 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
770 /* The address doesn't include a host, so it might be an alias. */
771 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
772 qp
= akvisible () ? mp
->m_mbox
: "";
775 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
776 while ((cp
= getname (pp
))) {
777 if (!(mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
783 * If it's a From: or Resent-From: header, save the address
784 * for later possible use (as the envelope address for SMTP)
787 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
788 : (hdr
->set
& MFRM
)) {
789 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
790 from
[sizeof(from
) - 1] = '\0';
795 * Also save the Sender: or Resent-Sender: header as well
798 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
799 : (hdr
->set
& MSND
)) {
800 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
801 sender
[sizeof(sender
) - 1] = '\0';
806 * ALSO ... save Envelope-From
809 if (hdr
->set
& MEFM
) {
810 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
811 efrom
[sizeof(efrom
) - 1] = '\0';
814 if (hdr
->flags
& HBCC
)
817 mp
->m_ingrp
= np
->m_ingrp
;
820 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
822 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
823 /* Catch this before sendmail chokes with:
824 "553 List:; syntax illegal for recipient
826 If we wanted to, we could expand out blind
827 aliases and put them in Bcc:, but then
828 they'd have the Blind-Carbon-Copy
831 "blind lists not compatible with"
837 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
838 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
847 /* Address includes a host, so no alias substitution is needed. */
850 * If it's a From: or Resent-From header, save the address
851 * for later possible use (as the envelope address for SMTP)
854 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
855 : (hdr
->set
& MFRM
)) {
856 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
861 * Also save the Sender: header as well
864 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
865 : (hdr
->set
& MSND
)) {
866 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
867 sender
[sizeof(sender
) - 1] = '\0';
872 * ALSO ... save Envelope-From
875 if (hdr
->set
& MEFM
) {
876 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
877 efrom
[sizeof(efrom
) - 1] = '\0';
880 if (hdr
->flags
& HBCC
)
883 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
886 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
890 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
897 * If this is a From:/Resent-From: header, save the full thing for
898 * later in case we need it for use when constructing a Bcc draft message
901 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
) : (hdr
->set
& MFRM
)) {
902 strncpy(fullfrom
, str
, sizeof(fullfrom
));
903 fullfrom
[sizeof(fullfrom
) - 1] = 0;
905 * Strip off any trailing newlines
908 while (strlen(fullfrom
) > 0 && fullfrom
[strlen(fullfrom
) - 1] == '\n') {
909 fullfrom
[strlen(fullfrom
) - 1] = '\0';
913 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
914 advise (NULL
, "%s: field does not allow groups", name
);
935 * Probably not necessary, but just in case ...
943 if ((cp
= getfullname ()) && *cp
) {
944 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
945 snprintf (signature
, sizeof(signature
), "%s <%s>",
946 sigbuf
, getlocaladdr());
947 if ((cp
= getname (signature
)) == NULL
)
948 adios (NULL
, "getname () failed -- you lose extraordinarily big");
949 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
)) == NULL
)
950 adios (NULL
, "bad signature '%s'", sigbuf
);
955 strncpy (signature
, getlocaladdr(), sizeof(signature
));
961 * Now that we've outputted the header fields in the draft
962 * message, we will now output any remaining header fields
963 * that we need to add/create.
967 finish_headers (FILE *out
)
971 if (!(msgflags
& MFRM
)) {
973 * A From: header is now required in the draft.
975 advise (NULL
, "message has no From: header");
976 advise (NULL
, "See default components files for examples");
981 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
982 advise (NULL
, "A Sender: or Envelope-From: header is required "
983 "with multiple\nFrom: addresses");
991 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
993 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
995 * If we have multiple From: addresses, make sure we have an
996 * Sender: header. If we don't have one, then generate one
997 * from Envelope-From: (which in this case, cannot be blank)
1000 if (fromcount
> 1 && seensender
== 0) {
1001 if (efrom
[0] == '\0') {
1002 advise (NULL
, "Envelope-From cannot be blank when there "
1003 "is multiple From: addresses\nand no Sender: "
1007 fprintf (out
, "Sender: %s\n", efrom
);
1011 if (!(msgflags
& MVIS
))
1012 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
1016 if (!(msgflags
& MDAT
)) {
1017 advise (NULL
, "message has no Date: header");
1020 if (!(msgflags
& MFRM
)) {
1021 advise (NULL
, "message has no From: header");
1024 if (!(msgflags
& MRFM
)) {
1025 advise (NULL
, "message has no Resent-From: header");
1026 advise (NULL
, "See default components files for examples");
1030 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
1031 advise (NULL
, "A Resent-Sender: or Envelope-From: header is "
1032 "required with multiple\nResent-From: addresses");
1040 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
1042 fprintf (out
, "Resent-Message-ID: %s\n",
1043 message_id (tclock
, 0));
1045 * If we have multiple Resent-From: addresses, make sure we have an
1046 * Resent-Sender: header. If we don't have one, then generate one
1047 * from Envelope-From (which in this case, cannot be blank)
1050 if (fromcount
> 1 && seensender
== 0) {
1051 if (efrom
[0] == '\0') {
1052 advise (NULL
, "Envelope-From cannot be blank when there "
1053 "is multiple Resent-From: addresses and no "
1054 "Resent-Sender: header");
1057 fprintf (out
, "Resent-Sender: %s\n", efrom
);
1061 if (!(msgflags
& MVIS
))
1062 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1067 adios (NULL
, "re-format message and try again");
1069 adios (NULL
, "no addressees");
1074 get_header (char *header
, struct headers
*table
)
1078 for (h
= table
; h
->value
; h
++)
1079 if (!mh_strcasecmp (header
, h
->value
))
1087 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
1091 char buffer
[BUFSIZ
];
1093 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
1095 if (sm_mts
!= MTS_SENDMAIL_PIPE
&&
1096 ((flags
& (HBCC
| HDCC
| HEFM
)) || mp
->m_ingrp
))
1100 fprintf (out
, "%s: ", name
);
1101 linepos
+= (nameoutput
= strlen (name
) + 2);
1104 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1105 mp
->m_pers
= getcpy (aka
);
1108 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1111 cp
= adrformat (mp
);
1118 if (linepos
!= nameoutput
) {
1119 if (len
+ linepos
+ 2 > outputlinelen
)
1120 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1130 return (flags
& HTRY
);
1135 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1140 if (sm_mts
!= MTS_SENDMAIL_PIPE
&& (flags
& HBCC
))
1144 fprintf (out
, "%s: ", name
);
1145 linepos
+= (nameoutput
= strlen (name
) + 2);
1148 cp
= concat (group
, ";", NULL
);
1151 if (linepos
> nameoutput
) {
1152 if (len
+ linepos
+ 2 > outputlinelen
) {
1153 fprintf (out
, ",\n%*s", nameoutput
, "");
1154 linepos
= nameoutput
;
1168 insert (struct mailname
*np
)
1170 struct mailname
*mp
;
1172 if (np
->m_mbox
== NULL
)
1175 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1176 : np
->m_type
== UUCPHOST
? &uuaddrs
1180 if (!mh_strcasecmp (np
->m_host
, mp
->m_next
->m_host
)
1181 && !mh_strcasecmp (np
->m_mbox
, mp
->m_next
->m_mbox
)
1182 && np
->m_bcc
== mp
->m_next
->m_bcc
)
1195 struct mailname
*mp
;
1197 printf ("-------\n\t-- Addresses --\nlocal:\t");
1198 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1199 printf ("%s%s%s", mp
->m_mbox
,
1200 mp
->m_bcc
? "[BCC]" : "",
1201 mp
->m_next
? ",\n\t" : "");
1203 printf ("\nnet:\t");
1204 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1205 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1206 mp
->m_mbox
, mp
->m_host
,
1207 mp
->m_bcc
? "[BCC]" : "",
1208 mp
->m_next
? ",\n\t" : "");
1210 printf ("\nuucp:\t");
1211 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1212 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1213 mp
->m_bcc
? "[BCC]" : "",
1214 mp
->m_next
? ",\n\t" : "");
1216 printf ("\n\t-- Folder Copies --\nfcc:\t");
1217 for (i
= 0; i
< fccind
; i
++)
1218 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1226 struct mailname
*mp
;
1228 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1229 if (annoaux (mp
) == NOTOK
)
1232 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1233 if (annoaux (mp
) == NOTOK
)
1236 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1237 if (annoaux (mp
) == NOTOK
)
1247 annoaux (struct mailname
*mp
)
1250 char buffer
[BUFSIZ
];
1252 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1253 i
= strlen (buffer
);
1255 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1260 insert_fcc (struct headers
*hdr
, unsigned char *pp
)
1264 for (cp
= pp
; isspace (*cp
); cp
++)
1266 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace (*pp
); pp
--)
1274 adios (NULL
, "too many %ss", hdr
->value
);
1275 fccfold
[fccind
++] = getcpy (cp
);
1283 make_bcc_file (int dashstuff
)
1291 tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
);
1292 if (tfile
== NULL
) adios("bcc", "unable to create temporary file");
1293 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1295 fprintf (out
, "From: %s\n", fullfrom
);
1296 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1298 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1300 fprintf (out
, "Subject: %s", subject
);
1301 fprintf (out
, "BCC:\n");
1304 * Use MIME encapsulation for Bcc messages
1310 * Check if any lines in the message clash with the
1311 * prefix for the MIME multipart separator. If there
1312 * is a clash, increment one of the letters in the
1313 * prefix and check again.
1315 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1316 adios (NULL
, "lost prefix start");
1317 while (find_prefix () == NOTOK
) {
1322 adios (NULL
, "can't find a unique delimiter string");
1327 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1328 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1329 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1331 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1337 * Do mhl filtering of Bcc messages instead
1338 * of MIME encapsulation.
1340 if (filter
!= NULL
) {
1341 vec
[0] = r1bindex (mhlproc
, '/');
1343 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1347 adios ("fork", "unable to");
1350 dup2 (fileno (out
), 1);
1353 vec
[i
++] = "-forward";
1358 /* was the flag -[no]dashstuffing specified? */
1360 vec
[i
++] = "-dashstuffing";
1361 else if (dashstuff
< 0)
1362 vec
[i
++] = "-nodashstuffing";
1365 execvp (mhlproc
, vec
);
1366 fprintf (stderr
, "unable to exec ");
1371 pidXwait (child_id
, mhlproc
);
1375 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1376 adios (tmpfil
, "unable to re-open");
1379 * If using MIME encapsulation, or if the -nodashstuffing
1380 * flag was given, then just copy message. Else do
1381 * RFC934 quoting (dashstuffing).
1383 if (mime
|| dashstuff
< 0)
1384 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1386 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1390 fseek (out
, 0L, SEEK_END
);
1392 fprintf (out
, "\n--%s--\n", prefix
);
1394 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1400 * Scan message to check if any lines clash with
1401 * the prefix of the MIME multipart separator.
1408 unsigned char buffer
[BUFSIZ
];
1411 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1412 adios (tmpfil
, "unable to re-open");
1414 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1415 if (buffer
[0] == '-' && buffer
[1] == '-') {
1418 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1422 if (strcmp (buffer
+ 2, prefix
) == 0) {
1433 #define plural(x) (x == 1 ? "" : "s")
1438 if (badadr
&& unkadr
)
1439 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1440 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1442 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1444 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1449 do_addresses (int bccque
, int talk
)
1453 struct mailname
*lp
;
1456 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1457 if (lp
->m_bcc
? bccque
: !bccque
) {
1459 printf (" -- Local Recipients --\n");
1460 do_an_address (lp
, talk
);
1465 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1466 if (lp
->m_bcc
? bccque
: !bccque
) {
1468 printf (" -- UUCP Recipients --\n");
1469 do_an_address (lp
, talk
);
1474 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1475 if (lp
->m_bcc
? bccque
: !bccque
) {
1477 printf (" -- Network Recipients --\n");
1478 do_an_address (lp
, talk
);
1484 if (rp_isbad (retval
= sm_waend ()))
1485 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1490 * MTS-SPECIFIC INTERACTION
1495 * SENDMAIL/SMTP routines
1499 post (char *file
, int bccque
, int talk
, char *envelope
)
1506 if (msgflags
& MINV
)
1507 printf (" -- Posting for %s Recipients --\n",
1508 bccque
? "Blind" : "Sighted");
1510 printf (" -- Posting for All Recipients --\n");
1515 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
1516 char *sargv
[16], **argp
;
1518 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1522 adios ("fork", "unable to");
1525 if (freopen( file
, "r", stdin
) == NULL
) {
1526 adios (file
, "can't reopen for sendmail");
1530 *argp
++ = "sendmail";
1531 *argp
++ = "-t"; /* read msg for recipients */
1532 *argp
++ = "-i"; /* don't stop on "." */
1539 execv (sendmail
, sargv
);
1540 adios (sendmail
, "can't exec");
1543 pidXwait (child_id
, NULL
);
1547 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1548 verbose
, snoop
, queued
, sasl
,
1549 saslssf
, saslmech
, user
, tls
)) ||
1550 rp_isbad (retval
= sm_winit (envelope
)))
1551 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1553 do_addresses (bccque
, talk
&& verbose
);
1554 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1555 die (file
, "unable to re-open");
1560 sm_end (!(msgflags
& MINV
) || bccque
? OK
: DONE
);
1564 if (msgflags
& MINV
)
1565 printf (" -- %s Recipient Copies Posted --\n",
1566 bccque
? "Blind" : "Sighted");
1568 printf (" -- Recipient Copies Posted --\n");
1576 /* Address Verification */
1579 verify_all_addresses (int talk
, char *envelope
)
1582 struct mailname
*lp
;
1586 if (!whomsw
|| checksw
)
1587 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1588 verbose
, snoop
, queued
, sasl
,
1589 saslssf
, saslmech
, user
, tls
))
1590 || rp_isbad (retval
= sm_winit (envelope
)))
1591 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1593 if (talk
&& !whomsw
)
1594 printf (" -- Address Verification --\n");
1595 if (talk
&& localaddrs
.m_next
)
1596 printf (" -- Local Recipients --\n");
1597 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1598 do_an_address (lp
, talk
);
1600 if (talk
&& uuaddrs
.m_next
)
1601 printf (" -- UUCP Recipients --\n");
1602 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1603 do_an_address (lp
, talk
);
1605 if (talk
&& netaddrs
.m_next
)
1606 printf (" -- Network Recipients --\n");
1607 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1608 do_an_address (lp
, talk
);
1611 if (talk
&& !whomsw
)
1612 printf (" -- Address Verification Successful --\n");
1614 if (!whomsw
|| checksw
)
1623 do_an_address (struct mailname
*lp
, int talk
)
1629 switch (lp
->m_type
) {
1633 strncpy (addr
, mbox
, sizeof(addr
));
1637 mbox
= auxformat (lp
, 0);
1639 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1642 default: /* let SendMail decide if the host is bad */
1645 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1650 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1652 if (whomsw
&& !checksw
) {
1660 switch (retval
= sm_wadr (mbox
, host
,
1661 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1664 printf ("address ok\n");
1670 fprintf (stderr
, " %s: ", addr
);
1671 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1672 rp_string (retval
));
1678 fprintf (stderr
, " %s: ", addr
);
1679 die (NULL
, "unexpected response; %s", rp_string (retval
));
1687 do_text (char *file
, int fd
)
1692 lseek (fd
, (off_t
) 0, SEEK_SET
);
1694 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1695 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1696 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1700 die (file
, "problem reading from");
1702 switch (retval
= sm_wtend ()) {
1708 die (NULL
, "posting failed; %s", rp_string (retval
));
1711 die (NULL
, "unexpected response; %s", rp_string (retval
));
1726 if (msgflags
& MINV
)
1729 if (!whomsw
|| checksw
)
1742 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1743 istat
= SIGNAL2 (SIGINT
, sigser
);
1744 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1745 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1755 SIGNAL (SIGHUP
, hstat
);
1756 SIGNAL (SIGINT
, istat
);
1757 SIGNAL (SIGQUIT
, qstat
);
1758 SIGNAL (SIGTERM
, tstat
);
1766 p_refile (char *file
)
1774 printf (" -- Filing Folder Copies --\n");
1775 for (i
= 0; i
< fccind
; i
++)
1776 fcc (file
, fccfold
[i
]);
1778 printf (" -- Folder Copies Filed --\n");
1783 * Call the `fileproc' to add the file to the folder.
1787 fcc (char *file
, char *folder
)
1790 int i
, status
, argp
;
1792 char **arglist
, *program
;
1795 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1798 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1804 fprintf (stderr
, " %sFcc %s: ",
1805 msgstate
== RESENT
? "Resent-" : "", folder
);
1806 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1810 /* see if we need to add `+' */
1811 snprintf (fold
, sizeof(fold
), "%s%s",
1812 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1814 /* now exec the fileproc */
1816 arglist
= argsplit(fileproc
, &program
, &argp
);
1817 arglist
[argp
++] = "-link";
1818 arglist
[argp
++] = "-file";
1819 arglist
[argp
++] = file
;
1820 arglist
[argp
++] = fold
;
1821 arglist
[argp
] = NULL
;
1822 execvp (program
, arglist
);
1826 if ((status
= pidwait (child_id
, OK
))) {
1828 fprintf (stderr
, " %sFcc %s: ",
1829 msgstate
== RESENT
? "Resent-" : "", folder
);
1830 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1833 printf ("folder ok\n");
1845 die (char *what
, char *fmt
, ...)
1850 if (msgflags
& MINV
)
1853 if (!whomsw
|| checksw
)
1857 advertise (what
, NULL
, fmt
, ap
);