]>
diplodocus.org Git - nmh/blob - uip/post.c
e0bc4773aea232ba5f1438ad4d7b9273eb8e4434
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>
24 #ifdef HAVE_SYS_TIME_H
25 # include <sys/time.h>
29 #include <mts/smtp/smtp.h>
32 # define SASLminc(a) (a)
33 #else /* CYRUS_SASL */
34 # define SASLminc(a) 0
35 #endif /* CYRUS_SASL */
38 # define TLSminc(a) (a)
39 #else /* TLS_SUPPORT */
41 #endif /* TLS_SUPPORT */
43 #define FCCS 10 /* max number of fccs allowed */
45 /* In the following array of structures, the numeric second field of the
46 structures (minchars) is apparently used like this:
48 -# : Switch can be abbreviated to # characters; switch hidden in -help.
49 0 : Switch can't be abbreviated; switch shown in -help.
50 # : Switch can be abbreviated to # characters; switch shown in -help. */
52 #define POST_SWITCHES \
53 X("alias aliasfile", 0, ALIASW) \
54 X("check", -5, CHKSW) /* interface from whom */ \
55 X("nocheck", -7, NCHKSW) /* interface from whom */ \
56 X("debug", -5, DEBUGSW) \
57 X("dist", -4, DISTSW) /* interface from dist */ \
58 X("filter filterfile", 0, FILTSW) \
59 X("nofilter", 0, NFILTSW) \
60 X("format", 0, FRMTSW) \
61 X("noformat", 0, NFRMTSW) \
62 X("library directory", -7, LIBSW) /* interface from send, whom */ \
63 X("mime", 0, MIMESW) \
64 X("nomime", 0, NMIMESW) \
65 X("msgid", 0, MSGDSW) \
66 X("nomsgid", 0, NMSGDSW) \
67 X("verbose", 0, VERBSW) \
68 X("noverbose", 0, NVERBSW) \
69 X("watch", 0, WATCSW) \
70 X("nowatch", 0, NWATCSW) \
71 X("whom", -4, WHOMSW) /* interface from whom */ \
72 X("width columns", 0, WIDTHSW) \
73 X("version", 0, VERSIONSW) \
74 X("help", 0, HELPSW) \
75 X("dashstuffing", -12, BITSTUFFSW) /* should we dashstuff BCC messages? */ \
76 X("nodashstuffing", -14, NBITSTUFFSW) \
77 X("idanno number", -6, ANNOSW) /* interface from send */ \
78 X("client host", -6, CLIESW) \
79 X("server host", 6, SERVSW) /* specify alternate SMTP server */ \
80 X("snoop", -5, SNOOPSW) /* snoop the SMTP transaction */ \
81 X("partno", -6, PARTSW) \
82 X("queued", -6, QUEUESW) \
83 X("sasl", SASLminc(-4), SASLSW) \
84 X("nosasl", SASLminc(-6), NOSASLSW) \
85 X("saslmaxssf", SASLminc(-10), SASLMXSSFSW) \
86 X("saslmech", SASLminc(-5), SASLMECHSW) \
87 X("user", SASLminc(-4), USERSW) \
88 X("port server port name/number", 4, PORTSW) \
89 X("tls", TLSminc(-3), TLSSW) \
90 X("initialtls", TLSminc(-10), INITTLSSW) \
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 int recipients
= 0; /* how many people will get a copy */
189 static int unkadr
= 0; /* how many of those were unknown */
190 static int badadr
= 0; /* number of bad addrs */
191 static int badmsg
= 0; /* message has bad semantics */
192 static int verbose
= 0; /* spell it out */
193 static int format
= 1; /* format addresses */
194 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
195 static int msgid
= 0; /* add msgid */
196 static int debug
= 0; /* debugging post */
197 static int watch
= 0; /* watch the delivery process */
198 static int whomsw
= 0; /* we are whom not post */
199 static int checksw
= 0; /* whom -check */
200 static int linepos
=0; /* putadr()'s position on the line */
201 static int nameoutput
=0; /* putadr() has output header name */
202 static int sasl
=0; /* Use SASL auth for SMTP */
203 static int saslssf
=-1; /* Our maximum SSF for SASL */
204 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
205 static char *user
=NULL
; /* Authenticate as this user */
206 static char *port
="smtp"; /* Name of server port for SMTP */
207 static int tls
=0; /* Use TLS for encryption */
208 static int fromcount
=0; /* Count of addresses on From: header */
209 static int seensender
=0; /* Have we seen a Sender: header? */
211 static unsigned msgflags
= 0; /* what we've seen */
215 static int msgstate
= NORMAL
;
217 static time_t tclock
= 0; /* the time we started (more or less) */
219 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
221 static char tmpfil
[BUFSIZ
];
222 static char bccfil
[BUFSIZ
];
224 static char from
[BUFSIZ
]; /* my network address */
225 static char sender
[BUFSIZ
]; /* my Sender: header */
226 static char efrom
[BUFSIZ
]; /* my Envelope-From: header */
227 static char fullfrom
[BUFSIZ
]; /* full contents of From header */
228 static char signature
[BUFSIZ
]; /* my signature */
229 static char *filter
= NULL
; /* the filter for BCC'ing */
230 static char *subject
= NULL
; /* the subject field for BCC'ing */
231 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
233 static struct headers
*hdrtab
; /* table for the message we're doing */
235 static struct mailname localaddrs
; /* local addrs */
236 static struct mailname netaddrs
; /* network addrs */
237 static struct mailname uuaddrs
; /* uucp addrs */
238 static struct mailname tmpaddrs
; /* temporary queue */
240 static int snoop
= 0;
241 static char *clientsw
= NULL
;
242 static char *serversw
= NULL
;
244 extern struct smtp sm_reply
;
246 static char prefix
[] = "----- =_aaaaaaaaaa";
248 static char *partno
= NULL
;
249 static int queued
= 0;
254 static void putfmt (char *, char *, FILE *);
255 static void start_headers (void);
256 static void finish_headers (FILE *);
257 static int get_header (char *, struct headers
*);
258 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
259 static void putgrp (char *, char *, FILE *, unsigned int);
260 static int insert (struct mailname
*);
261 static void pl (void);
262 static void anno (void);
263 static int annoaux (struct mailname
*);
264 static void insert_fcc (struct headers
*, char *);
265 static void make_bcc_file (int);
266 static void verify_all_addresses (int, char *);
267 static void chkadr (void);
268 static void sigon (void);
269 static void sigoff (void);
270 static void p_refile (char *);
271 static void fcc (char *, char *);
272 static void die (char *, char *, ...);
273 static void post (char *, int, int, char *);
274 static void do_text (char *file
, int fd
);
275 static void do_an_address (struct mailname
*, int);
276 static void do_addresses (int, int);
277 static int find_prefix (void);
281 main (int argc
, char **argv
)
283 int state
, compnum
, dashstuff
= 0;
284 char *cp
, *msg
= NULL
, **argp
, **arguments
, *envelope
;
285 char buf
[BUFSIZ
], name
[NAMESZ
];
287 m_getfld_state_t gstate
= 0;
290 setlocale(LC_ALL
, "");
292 invo_name
= r1bindex (argv
[0], '/');
294 /* foil search of user profile/context */
295 if (context_foil (NULL
) == -1)
298 mts_init (invo_name
);
299 arguments
= getarguments (invo_name
, argc
, argv
, 0);
302 while ((cp
= *argp
++)) {
304 switch (smatch (++cp
, switches
)) {
306 ambigsw (cp
, switches
);
309 adios (NULL
, "-%s unknown", cp
);
312 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
313 print_help (buf
, switches
, 0);
316 print_version(invo_name
);
320 if (!(cp
= *argp
++) || *cp
== '-')
321 adios (NULL
, "missing argument to %s", argp
[-2]);
322 /* create a minimal context */
323 if (context_foil (cp
) == -1)
328 if (!(cp
= *argp
++) || *cp
== '-')
329 adios (NULL
, "missing argument to %s", argp
[-2]);
330 if ((state
= alias (cp
)) != AK_OK
)
331 adios (NULL
, "aliasing error in %s - %s",
332 cp
, akerror (state
));
351 if (!(filter
= *argp
++) || *filter
== '-')
352 adios (NULL
, "missing argument to %s", argp
[-2]);
407 if (!(cp
= *argp
++) || *cp
== '-')
408 adios (NULL
, "missing argument to %s", argp
[-2]);
409 if ((outputlinelen
= atoi (cp
)) < 10)
410 adios (NULL
, "impossible width %d", outputlinelen
);
414 if (!(cp
= *argp
++) || *cp
== '-')
415 adios (NULL
, "missing argument to %s", argp
[-2]);
416 if ((pfd
= atoi (cp
)) <= 2)
417 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
421 if (!(clientsw
= *argp
++) || *clientsw
== '-')
422 adios (NULL
, "missing argument to %s", argp
[-2]);
425 if (!(serversw
= *argp
++) || *serversw
== '-')
426 adios (NULL
, "missing argument to %s", argp
[-2]);
433 if (!(partno
= *argp
++) || *partno
== '-')
434 adios (NULL
, "missing argument to %s", argp
[-2]);
450 if (!(cp
= *argp
++) || *cp
== '-')
451 adios (NULL
, "missing argument to %s", argp
[-2]);
456 if (!(saslmech
= *argp
++) || *saslmech
== '-')
457 adios (NULL
, "missing argument to %s", argp
[-2]);
461 if (!(user
= *argp
++) || *user
== '-')
462 adios (NULL
, "missing argument to %s", argp
[-2]);
466 if (!(port
= *argp
++) || *port
== '-')
467 adios (NULL
, "missing argument to %s", argp
[-2]);
483 if (!(cp
= *argp
++) || *cp
== '-')
484 adios (NULL
, "missing argument to %s", argp
[-2]);
489 if (!(cp
= *argp
++) || *cp
== '-')
490 adios (NULL
, "missing argument to %s", argp
[-2]);
495 if (!(cp
= *argp
++) || *cp
== '-')
496 adios (NULL
, "missing argument to %s", argp
[-2]);
497 save_mts_method (cp
);
501 if (!(cp
= *argp
++) || *cp
== '-')
502 adios (NULL
, "missing argument to %s", argp
[-2]);
503 if (save_message_id_style (cp
) != 0)
504 adios (NULL
, "unsupported messageid \"%s\"", cp
);
509 adios (NULL
, "only one message at a time!");
517 adios (NULL
, "usage: %s [switches] file", invo_name
);
519 if (outputlinelen
< 10)
520 adios (NULL
, "impossible width %d", outputlinelen
);
522 if ((in
= fopen (msg
, "r")) == NULL
)
523 adios (msg
, "unable to open");
531 if ((out
= fopen ("/dev/null", "w")) == NULL
)
532 adios ("/dev/null", "unable to open");
534 char *cp
= m_mktemp(m_maildir(invo_name
), NULL
, &out
);
536 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
538 adios ("post", "unable to create temporary file");
541 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
542 chmod (tmpfil
, 0600);
546 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
548 for (compnum
= 1;;) {
549 int bufsz
= sizeof buf
;
550 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
554 cp
= add (buf
, NULL
);
555 while (state
== FLDPLUS
) {
557 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
560 putfmt (name
, cp
, out
);
565 finish_headers (out
);
568 fprintf (out
, "\n%s", buf
);
569 while (state
== BODY
) {
571 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
577 finish_headers (out
);
582 adios (NULL
, "message format error in component #%d", compnum
);
585 adios (NULL
, "getfld() returned %d", state
);
589 m_getfld_state_destroy (&gstate
);
603 * Here's how we decide which address to use as the envelope-from
606 * - If we were given an Envelope-From header, use that.
607 * - If we were given a Sender: address, use that.
608 * - Otherwise, use the address on the From: line
611 if (msgflags
& MEFM
) {
613 } else if (seensender
) {
619 /* If we are doing a "whom" check */
621 /* This won't work with MTS_SENDMAIL_PIPE. */
622 verify_all_addresses (1, envelope
);
626 if (msgflags
& MINV
) {
627 make_bcc_file (dashstuff
);
628 if (msgflags
& MVIS
) {
629 if (sm_mts
!= MTS_SENDMAIL_PIPE
) {
630 /* It would be nice to have support to call
631 verify_all_addresses with MTS_SENDMAIL_PIPE, but
632 that might require running sendmail as root. Note
633 that spost didn't verify addresses. */
634 verify_all_addresses (verbose
, envelope
);
636 post (tmpfil
, 0, verbose
, envelope
);
638 post (bccfil
, 1, verbose
, envelope
);
641 post (tmpfil
, 0, isatty (1), envelope
);
648 printf (partno
? "Partial Message #%s Processed\n" : "Message Processed\n",
660 putfmt (char *name
, char *str
, FILE *out
)
662 int count
, grp
, i
, keep
;
665 struct mailname
*mp
= NULL
, *np
= NULL
;
668 while (*str
== ' ' || *str
== '\t')
671 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
672 advise (NULL
, "illegal header line -- %s:", name
);
677 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
678 if (strncasecmp (name
, "nmh-", 4)) {
679 fprintf (out
, "%s: %s", name
, str
);
681 /* Filter out all Nmh-* headers, because Norm asked. They
682 should never have reached this point. Warn about any
683 that are non-empty. */
684 if (strcmp (str
, "\n")) {
685 char *newline
= strchr (str
, '\n');
686 if (newline
) *newline
= '\0';
688 advise (NULL
, "ignoring header line -- %s: %s", name
, str
);
697 if (hdr
->flags
& HIGN
) {
700 if (hdr
->flags
& HBAD
) {
701 advise (NULL
, "illegal header line -- %s:", name
);
705 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
707 if (hdr
->flags
& HSUB
)
708 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
709 if (hdr
->flags
& HFCC
) {
710 if ((cp
= strrchr(str
, '\n')))
712 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
714 insert_fcc (hdr
, pp
);
716 insert_fcc (hdr
, pp
);
720 if (!(hdr
->flags
& HADR
)) {
721 fprintf (out
, "%s: %s", name
, str
);
725 tmpaddrs
.m_next
= NULL
;
726 for (count
= 0; (cp
= getname (str
)); count
++)
727 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
731 tmpaddrs
.m_next
= mp
;
735 if (hdr
->flags
& HTRY
)
741 if (hdr
->flags
& HNIL
)
742 fprintf (out
, "%s: %s", name
, str
);
745 * Sender (or Resent-Sender) can have only one address
747 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
748 : (hdr
->set
& MSND
)) {
749 advise (NULL
, "%s: field requires one address", name
);
753 advise (NULL
, "%s: field requires at least one address", name
);
760 if (count
> 1 && (hdr
->flags
& HONE
)) {
761 advise (NULL
, "%s: field only permits one address", name
);
766 nameoutput
= linepos
= 0;
767 snprintf (namep
, sizeof(namep
), "%s%s",
768 (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
770 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
771 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
772 /* The address doesn't include a host, so it might be an alias. */
773 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
774 qp
= akvisible () ? mp
->m_mbox
: "";
777 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
778 while ((cp
= getname (pp
))) {
779 if (!(mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
785 * If it's a From: or Resent-From: header, save the address
786 * for later possible use (as the envelope address for SMTP)
789 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
790 : (hdr
->set
& MFRM
)) {
791 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
792 from
[sizeof(from
) - 1] = '\0';
797 * Also save the Sender: or Resent-Sender: header as well
800 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
801 : (hdr
->set
& MSND
)) {
802 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
803 sender
[sizeof(sender
) - 1] = '\0';
808 * ALSO ... save Envelope-From
811 if (hdr
->set
& MEFM
) {
812 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
813 efrom
[sizeof(efrom
) - 1] = '\0';
816 if (hdr
->flags
& HBCC
)
819 mp
->m_ingrp
= np
->m_ingrp
;
822 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
824 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
825 /* Catch this before sendmail chokes with:
826 "553 List:; syntax illegal for recipient
828 If we wanted to, we could expand out blind
829 aliases and put them in Bcc:, but then
830 they'd have the Blind-Carbon-Copy
833 "blind lists not compatible with"
839 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
840 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
849 /* Address includes a host, so no alias substitution is needed. */
852 * If it's a From: or Resent-From header, save the address
853 * for later possible use (as the envelope address for SMTP)
856 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
857 : (hdr
->set
& MFRM
)) {
858 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
863 * Also save the Sender: header as well
866 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
867 : (hdr
->set
& MSND
)) {
868 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
869 sender
[sizeof(sender
) - 1] = '\0';
874 * ALSO ... save Envelope-From
877 if (hdr
->set
& MEFM
) {
878 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
879 efrom
[sizeof(efrom
) - 1] = '\0';
882 if (hdr
->flags
& HBCC
)
885 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
888 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
892 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
899 * If this is a From:/Resent-From: header, save the full thing for
900 * later in case we need it for use when constructing a Bcc draft message
903 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
) : (hdr
->set
& MFRM
)) {
904 strncpy(fullfrom
, str
, sizeof(fullfrom
));
905 fullfrom
[sizeof(fullfrom
) - 1] = 0;
907 * Strip off any trailing newlines
910 while (strlen(fullfrom
) > 0 && fullfrom
[strlen(fullfrom
) - 1] == '\n') {
911 fullfrom
[strlen(fullfrom
) - 1] = '\0';
915 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
916 advise (NULL
, "%s: field does not allow groups", name
);
928 char *cp
, sigbuf
[BUFSIZ
];
934 * Probably not necessary, but just in case ...
942 if ((cp
= getfullname ()) && *cp
) {
943 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
944 snprintf (signature
, sizeof(signature
), "%s <%s>",
945 sigbuf
, getlocaladdr());
946 if ((cp
= getname (signature
)) == NULL
)
947 adios (NULL
, "getname () failed -- you lose extraordinarily big");
948 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
)) == NULL
)
949 adios (NULL
, "bad signature '%s'", sigbuf
);
954 strncpy (signature
, getlocaladdr(), sizeof(signature
));
960 * Now that we've outputted the header fields in the draft
961 * message, we will now output any remaining header fields
962 * that we need to add/create.
966 finish_headers (FILE *out
)
970 if (!(msgflags
& MFRM
)) {
972 * A From: header is now required in the draft.
974 advise (NULL
, "message has no From: header");
975 advise (NULL
, "See default components files for examples");
980 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
981 advise (NULL
, "A Sender: or Envelope-From: header is required "
982 "with multiple\nFrom: addresses");
990 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
992 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
994 * If we have multiple From: addresses, make sure we have an
995 * Sender: header. If we don't have one, then generate one
996 * from Envelope-From: (which in this case, cannot be blank)
999 if (fromcount
> 1 && seensender
== 0) {
1000 if (efrom
[0] == '\0') {
1001 advise (NULL
, "Envelope-From cannot be blank when there "
1002 "is multiple From: addresses\nand no Sender: "
1006 fprintf (out
, "Sender: %s\n", efrom
);
1010 if (!(msgflags
& MVIS
))
1011 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
1015 if (!(msgflags
& MDAT
)) {
1016 advise (NULL
, "message has no Date: header");
1019 if (!(msgflags
& MFRM
)) {
1020 advise (NULL
, "message has no From: header");
1023 if (!(msgflags
& MRFM
)) {
1024 advise (NULL
, "message has no Resent-From: header");
1025 advise (NULL
, "See default components files for examples");
1029 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
1030 advise (NULL
, "A Resent-Sender: or Envelope-From: header is "
1031 "required with multiple\nResent-From: addresses");
1039 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
1041 fprintf (out
, "Resent-Message-ID: %s\n",
1042 message_id (tclock
, 0));
1044 * If we have multiple Resent-From: addresses, make sure we have an
1045 * Resent-Sender: header. If we don't have one, then generate one
1046 * from Envelope-From (which in this case, cannot be blank)
1049 if (fromcount
> 1 && seensender
== 0) {
1050 if (efrom
[0] == '\0') {
1051 advise (NULL
, "Envelope-From cannot be blank when there "
1052 "is multiple Resent-From: addresses and no "
1053 "Resent-Sender: header");
1056 fprintf (out
, "Resent-Sender: %s\n", efrom
);
1060 if (!(msgflags
& MVIS
))
1061 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1066 adios (NULL
, "re-format message and try again");
1068 adios (NULL
, "no addressees");
1073 get_header (char *header
, struct headers
*table
)
1077 for (h
= table
; h
->value
; h
++)
1078 if (!strcasecmp (header
? header
: "", h
->value
? h
->value
: ""))
1086 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
1090 char buffer
[BUFSIZ
];
1092 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
1094 if (sm_mts
!= MTS_SENDMAIL_PIPE
&&
1095 ((flags
& (HBCC
| HDCC
| HEFM
)) || mp
->m_ingrp
))
1099 fprintf (out
, "%s: ", name
);
1100 linepos
+= (nameoutput
= strlen (name
) + 2);
1103 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1104 mp
->m_pers
= getcpy (aka
);
1107 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1110 cp
= adrformat (mp
);
1117 if (linepos
!= nameoutput
) {
1118 if (len
+ linepos
+ 2 > outputlinelen
)
1119 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1129 return (flags
& HTRY
);
1134 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1139 if (sm_mts
!= MTS_SENDMAIL_PIPE
&& (flags
& HBCC
))
1143 fprintf (out
, "%s: ", name
);
1144 linepos
+= (nameoutput
= strlen (name
) + 2);
1147 cp
= concat (group
, ";", NULL
);
1150 if (linepos
> nameoutput
) {
1151 if (len
+ linepos
+ 2 > outputlinelen
) {
1152 fprintf (out
, ",\n%*s", nameoutput
, "");
1153 linepos
= nameoutput
;
1167 insert (struct mailname
*np
)
1169 struct mailname
*mp
;
1171 if (np
->m_mbox
== NULL
)
1174 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1175 : np
->m_type
== UUCPHOST
? &uuaddrs
1179 if (!strcasecmp (np
->m_host
? np
->m_host
: "",
1180 mp
->m_next
->m_host
? mp
->m_next
->m_host
: "") &&
1181 !strcasecmp (np
->m_mbox
? np
->m_mbox
: "",
1182 mp
->m_next
->m_mbox
? mp
->m_next
->m_mbox
: "") &&
1183 np
->m_bcc
== mp
->m_next
->m_bcc
)
1196 struct mailname
*mp
;
1198 printf ("-------\n\t-- Addresses --\nlocal:\t");
1199 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1200 printf ("%s%s%s", mp
->m_mbox
,
1201 mp
->m_bcc
? "[BCC]" : "",
1202 mp
->m_next
? ",\n\t" : "");
1204 printf ("\nnet:\t");
1205 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1206 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1207 mp
->m_mbox
, mp
->m_host
,
1208 mp
->m_bcc
? "[BCC]" : "",
1209 mp
->m_next
? ",\n\t" : "");
1211 printf ("\nuucp:\t");
1212 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1213 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1214 mp
->m_bcc
? "[BCC]" : "",
1215 mp
->m_next
? ",\n\t" : "");
1217 printf ("\n\t-- Folder Copies --\nfcc:\t");
1218 for (i
= 0; i
< fccind
; i
++)
1219 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1227 struct mailname
*mp
;
1229 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1230 if (annoaux (mp
) == NOTOK
)
1233 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1234 if (annoaux (mp
) == NOTOK
)
1237 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1238 if (annoaux (mp
) == NOTOK
)
1248 annoaux (struct mailname
*mp
)
1251 char buffer
[BUFSIZ
];
1253 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1254 i
= strlen (buffer
);
1256 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1261 insert_fcc (struct headers
*hdr
, char *pp
)
1265 for (cp
= pp
; isspace ((unsigned char) *cp
); cp
++)
1267 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace ((unsigned char) *pp
); pp
--)
1275 adios (NULL
, "too many %ss", hdr
->value
);
1276 fccfold
[fccind
++] = getcpy (cp
);
1284 make_bcc_file (int dashstuff
)
1290 char *tfile
= NULL
, *program
;
1292 tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
);
1293 if (tfile
== NULL
) adios("bcc", "unable to create temporary file");
1294 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1296 fprintf (out
, "From: %s\n", fullfrom
);
1297 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1299 fprintf (out
, "Message-ID: %s\n", message_id (tclock
, 0));
1301 fprintf (out
, "Subject: %s", subject
);
1302 fprintf (out
, "BCC:\n");
1305 * Use MIME encapsulation for Bcc messages
1311 * Check if any lines in the message clash with the
1312 * prefix for the MIME multipart separator. If there
1313 * is a clash, increment one of the letters in the
1314 * prefix and check again.
1316 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1317 adios (NULL
, "lost prefix start");
1318 while (find_prefix () == NOTOK
) {
1323 adios (NULL
, "can't find a unique delimiter string");
1328 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1329 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1330 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1332 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1338 * Do mhl filtering of Bcc messages instead
1339 * of MIME encapsulation.
1341 if (filter
!= NULL
) {
1342 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1346 adios ("fork", "unable to");
1349 dup2 (fileno (out
), 1);
1351 vec
= argsplit(mhlproc
, &program
, &i
);
1352 vec
[i
++] = "-forward";
1357 /* was the flag -[no]dashstuffing specified? */
1359 vec
[i
++] = "-dashstuffing";
1360 else if (dashstuff
< 0)
1361 vec
[i
++] = "-nodashstuffing";
1364 execvp (program
, vec
);
1365 fprintf (stderr
, "unable to exec ");
1370 pidXwait (child_id
, mhlproc
);
1374 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1375 adios (tmpfil
, "unable to re-open");
1378 * If using MIME encapsulation, or if the -nodashstuffing
1379 * flag was given, then just copy message. Else do
1380 * RFC934 quoting (dashstuffing).
1382 if (mime
|| dashstuff
< 0)
1383 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1385 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1389 fseek (out
, 0L, SEEK_END
);
1391 fprintf (out
, "\n--%s--\n", prefix
);
1393 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1399 * Scan message to check if any lines clash with
1400 * the prefix of the MIME multipart separator.
1407 char buffer
[BUFSIZ
];
1410 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1411 adios (tmpfil
, "unable to re-open");
1413 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1414 if (buffer
[0] == '-' && buffer
[1] == '-') {
1417 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1418 if (!isspace ((unsigned char) *cp
))
1421 if (strcmp (buffer
+ 2, prefix
) == 0) {
1432 #define plural(x) (x == 1 ? "" : "s")
1437 if (badadr
&& unkadr
)
1438 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1439 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1441 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1443 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1448 do_addresses (int bccque
, int talk
)
1452 struct mailname
*lp
;
1455 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1456 if (lp
->m_bcc
? bccque
: !bccque
) {
1458 printf (" -- Local Recipients --\n");
1459 do_an_address (lp
, talk
);
1464 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1465 if (lp
->m_bcc
? bccque
: !bccque
) {
1467 printf (" -- UUCP Recipients --\n");
1468 do_an_address (lp
, talk
);
1473 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1474 if (lp
->m_bcc
? bccque
: !bccque
) {
1476 printf (" -- Network Recipients --\n");
1477 do_an_address (lp
, talk
);
1483 if (rp_isbad (retval
= sm_waend ()))
1484 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1489 * MTS-SPECIFIC INTERACTION
1494 * SENDMAIL/SMTP routines
1498 post (char *file
, int bccque
, int talk
, char *envelope
)
1505 if (msgflags
& MINV
)
1506 printf (" -- Posting for %s Recipients --\n",
1507 bccque
? "Blind" : "Sighted");
1509 printf (" -- Posting for All Recipients --\n");
1514 if (sm_mts
== MTS_SENDMAIL_PIPE
) {
1515 char **argp
, *program
;
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");
1529 argp
= argsplit(sendmail
, &program
, &argc
);
1530 argp
[argc
++] = "-t"; /* read msg for recipients */
1531 argp
[argc
++] = "-i"; /* don't stop on "." */
1533 argp
[argc
++] = "-bv";
1535 argp
[argc
++] = "-v";
1538 execv (program
, argp
);
1539 adios (sendmail
, "can't exec");
1542 pidXwait (child_id
, NULL
);
1546 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1547 verbose
, snoop
, queued
, sasl
,
1548 saslssf
, saslmech
, user
, tls
)) ||
1549 rp_isbad (retval
= sm_winit (envelope
)))
1550 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1552 do_addresses (bccque
, talk
&& verbose
);
1553 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1554 die (file
, "unable to re-open");
1559 sm_end (!(msgflags
& MINV
) || bccque
? OK
: DONE
);
1563 if (msgflags
& MINV
)
1564 printf (" -- %s Recipient Copies Posted --\n",
1565 bccque
? "Blind" : "Sighted");
1567 printf (" -- Recipient Copies Posted --\n");
1575 /* Address Verification */
1578 verify_all_addresses (int talk
, char *envelope
)
1581 struct mailname
*lp
;
1585 if (!whomsw
|| checksw
)
1586 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1587 verbose
, snoop
, queued
, sasl
,
1588 saslssf
, saslmech
, user
, tls
))
1589 || rp_isbad (retval
= sm_winit (envelope
)))
1590 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1592 if (talk
&& !whomsw
)
1593 printf (" -- Address Verification --\n");
1594 if (talk
&& localaddrs
.m_next
)
1595 printf (" -- Local Recipients --\n");
1596 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1597 do_an_address (lp
, talk
);
1599 if (talk
&& uuaddrs
.m_next
)
1600 printf (" -- UUCP Recipients --\n");
1601 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1602 do_an_address (lp
, talk
);
1604 if (talk
&& netaddrs
.m_next
)
1605 printf (" -- Network Recipients --\n");
1606 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1607 do_an_address (lp
, talk
);
1610 if (talk
&& !whomsw
)
1611 printf (" -- Address Verification Successful --\n");
1613 if (!whomsw
|| checksw
)
1622 do_an_address (struct mailname
*lp
, int talk
)
1628 switch (lp
->m_type
) {
1632 strncpy (addr
, mbox
, sizeof(addr
));
1636 mbox
= auxformat (lp
, 0);
1638 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1641 default: /* let SendMail decide if the host is bad */
1644 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1649 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1651 if (whomsw
&& !checksw
) {
1659 switch (retval
= sm_wadr (mbox
, host
,
1660 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1663 printf ("address ok\n");
1669 fprintf (stderr
, " %s: ", addr
);
1670 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1671 rp_string (retval
));
1677 fprintf (stderr
, " %s: ", addr
);
1678 die (NULL
, "unexpected response; %s", rp_string (retval
));
1686 do_text (char *file
, int fd
)
1691 lseek (fd
, (off_t
) 0, SEEK_SET
);
1693 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1694 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1695 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1699 die (file
, "problem reading from");
1701 switch (retval
= sm_wtend ()) {
1707 die (NULL
, "posting failed; %s", rp_string (retval
));
1710 die (NULL
, "unexpected response; %s", rp_string (retval
));
1725 if (msgflags
& MINV
)
1728 if (!whomsw
|| checksw
)
1741 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1742 istat
= SIGNAL2 (SIGINT
, sigser
);
1743 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1744 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1754 SIGNAL (SIGHUP
, hstat
);
1755 SIGNAL (SIGINT
, istat
);
1756 SIGNAL (SIGQUIT
, qstat
);
1757 SIGNAL (SIGTERM
, tstat
);
1765 p_refile (char *file
)
1773 printf (" -- Filing Folder Copies --\n");
1774 for (i
= 0; i
< fccind
; i
++)
1775 fcc (file
, fccfold
[i
]);
1777 printf (" -- Folder Copies Filed --\n");
1782 * Call the `fileproc' to add the file to the folder.
1786 fcc (char *file
, char *folder
)
1789 int i
, status
, argp
;
1791 char **arglist
, *program
;
1794 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1797 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1803 fprintf (stderr
, " %sFcc %s: ",
1804 msgstate
== RESENT
? "Resent-" : "", folder
);
1805 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1809 /* see if we need to add `+' */
1810 snprintf (fold
, sizeof(fold
), "%s%s",
1811 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1813 /* now exec the fileproc */
1815 arglist
= argsplit(fileproc
, &program
, &argp
);
1816 arglist
[argp
++] = "-link";
1817 arglist
[argp
++] = "-file";
1818 arglist
[argp
++] = file
;
1819 arglist
[argp
++] = fold
;
1820 arglist
[argp
] = NULL
;
1821 execvp (program
, arglist
);
1825 if ((status
= pidwait (child_id
, OK
))) {
1827 fprintf (stderr
, " %sFcc %s: ",
1828 msgstate
== RESENT
? "Resent-" : "", folder
);
1829 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1832 printf ("folder ok\n");
1844 die (char *what
, char *fmt
, ...)
1849 if (msgflags
& MINV
)
1852 if (!whomsw
|| checksw
)
1856 advertise (what
, NULL
, fmt
, ap
);