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>
26 #ifdef HAVE_SYS_TIME_H
27 # include <sys/time.h>
32 # include <mts/smtp/smtp.h>
36 # define SASLminc(a) (a)
37 #else /* CYRUS_SASL */
38 # define SASLminc(a) 0
39 #endif /* CYRUS_SASL */
42 # define TLSminc(a) (a)
43 #else /* TLS_SUPPORT */
45 #endif /* TLS_SUPPORT */
47 #define FCCS 10 /* max number of fccs allowed */
49 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
51 /* In the following array of structures, the numeric second field of the
52 structures (minchars) is apparently used like this:
54 -# : Switch can be abbreviated to # characters; switch hidden in -help.
55 0 : Switch can't be abbreviated; switch shown in -help.
56 # : Switch can be abbreviated to # characters; switch shown in -help. */
58 static struct swit switches
[] = {
60 { "alias aliasfile", 0 },
62 { "check", -5 }, /* interface from whom */
64 { "nocheck", -7 }, /* interface from whom */
68 { "dist", -4 }, /* interface from dist */
70 { "filter filterfile", 0 },
78 { "library directory", -7 }, /* interface from send, whom */
96 { "whom", -4 }, /* interface from whom */
98 { "width columns", 0 },
103 #define BITSTUFFSW 22
104 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
105 #define NBITSTUFFSW 23
106 { "nodashstuffing", -14 },
108 { "mail", -4 }, /* specify MAIL smtp mode */
110 { "saml", -4 }, /* specify SAML smtp mode */
112 { "send", -4 }, /* specify SEND smtp mode */
114 { "soml", -4 }, /* specify SOML smtp mode */
116 { "idanno number", -6 }, /* interface from send */
118 { "deliver address-list", -7 },
120 { "client host", -6 },
122 { "server host", -6 }, /* specify alternate SMTP server */
124 { "snoop", -5 }, /* snoop the SMTP transaction */
126 { "fill-in file", -7 },
134 { "sasl", SASLminc(-4) },
135 #define SASLMECHSW 38
136 { "saslmech", SASLminc(-5) },
138 { "user", SASLminc(-4) },
140 { "port server port name/number", 4 },
142 { "tls", TLSminc(-3) },
143 #define FILEPROCSW 42
158 * flags for headers->flags
160 #define HNOP 0x0000 /* just used to keep .set around */
161 #define HBAD 0x0001 /* bad header - don't let it through */
162 #define HADR 0x0002 /* header has an address field */
163 #define HSUB 0x0004 /* Subject: header */
164 #define HTRY 0x0008 /* try to send to addrs on header */
165 #define HBCC 0x0010 /* don't output this header */
166 #define HMNG 0x0020 /* munge this header */
167 #define HNGR 0x0040 /* no groups allowed in this header */
168 #define HFCC 0x0080 /* FCC: type header */
169 #define HNIL 0x0100 /* okay for this header not to have addrs */
170 #define HIGN 0x0200 /* ignore this header */
171 #define HDCC 0x0400 /* another undocumented feature */
174 * flags for headers->set
176 #define MFRM 0x0001 /* we've seen a From: */
177 #define MDAT 0x0002 /* we've seen a Date: */
178 #define MRFM 0x0004 /* we've seen a Resent-From: */
179 #define MVIS 0x0008 /* we've seen sighted addrs */
180 #define MINV 0x0010 /* we've seen blind addrs */
183 static struct headers NHeaders
[] = {
184 { "Return-Path", HBAD
, 0 },
185 { "Received", HBAD
, 0 },
186 { "Reply-To", HADR
|HNGR
, 0 },
187 { "From", HADR
|HNGR
, MFRM
},
188 { "Sender", HADR
|HBAD
, 0 },
190 { "Subject", HSUB
, 0 },
191 { "To", HADR
|HTRY
, MVIS
},
192 { "cc", HADR
|HTRY
, MVIS
},
193 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
194 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
195 { "Message-ID", HBAD
, 0 },
200 static struct headers RHeaders
[] = {
201 { "Resent-Reply-To", HADR
|HNGR
, 0 },
202 { "Resent-From", HADR
|HNGR
, MRFM
},
203 { "Resent-Sender", HADR
|HBAD
, 0 },
204 { "Resent-Date", HBAD
, 0 },
205 { "Resent-Subject", HSUB
, 0 },
206 { "Resent-To", HADR
|HTRY
, MVIS
},
207 { "Resent-cc", HADR
|HTRY
, MVIS
},
208 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
209 { "Resent-Message-ID", HBAD
, 0 },
210 { "Resent-Fcc", HFCC
, 0 },
211 { "Reply-To", HADR
, 0 },
212 { "From", HADR
|HNGR
, MFRM
},
213 { "Sender", HADR
|HNGR
, 0 },
214 { "Date", HNOP
, MDAT
},
215 { "To", HADR
|HNIL
, 0 },
216 { "cc", HADR
|HNIL
, 0 },
217 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
222 static short fccind
= 0; /* index into fccfold[] */
223 static short outputlinelen
= OUTPUTLINELEN
;
225 static int pfd
= NOTOK
; /* fd to write annotation list to */
226 static uid_t myuid
= -1; /* my user id */
227 static gid_t mygid
= -1; /* my group id */
228 static int recipients
= 0; /* how many people will get a copy */
229 static int unkadr
= 0; /* how many of those were unknown */
230 static int badadr
= 0; /* number of bad addrs */
231 static int badmsg
= 0; /* message has bad semantics */
232 static int verbose
= 0; /* spell it out */
233 static int format
= 1; /* format addresses */
234 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
235 static int msgid
= 0; /* add msgid */
236 static int debug
= 0; /* debugging post */
237 static int watch
= 0; /* watch the delivery process */
238 static int whomsw
= 0; /* we are whom not post */
239 static int checksw
= 0; /* whom -check */
240 static int linepos
=0; /* putadr()'s position on the line */
241 static int nameoutput
=0; /* putadr() has output header name */
242 static int sasl
=0; /* Use SASL auth for SMTP */
243 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
244 static char *user
=NULL
; /* Authenticate as this user */
245 static char *port
="smtp"; /* Name of server port for SMTP */
246 static int tls
=0; /* Use TLS for encryption */
248 static unsigned msgflags
= 0; /* what we've seen */
252 static int msgstate
= NORMAL
;
254 static time_t tclock
= 0; /* the time we started (more or less) */
256 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
258 static char tmpfil
[BUFSIZ
];
259 static char bccfil
[BUFSIZ
];
261 static char from
[BUFSIZ
]; /* my network address */
262 static char signature
[BUFSIZ
]; /* my signature */
263 static char *filter
= NULL
; /* the filter for BCC'ing */
264 static char *subject
= NULL
; /* the subject field for BCC'ing */
265 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
267 static struct headers
*hdrtab
; /* table for the message we're doing */
269 static struct mailname localaddrs
; /* local addrs */
270 static struct mailname netaddrs
; /* network addrs */
271 static struct mailname uuaddrs
; /* uucp addrs */
272 static struct mailname tmpaddrs
; /* temporary queue */
275 static int snoop
= 0;
276 static int smtpmode
= S_MAIL
;
277 static char *clientsw
= NULL
;
278 static char *serversw
= NULL
;
280 extern struct smtp sm_reply
;
283 static char prefix
[] = "----- =_aaaaaaaaaa";
285 static int fill_up
= 0;
286 static char *fill_in
= NULL
;
287 static char *partno
= NULL
;
288 static int queued
= 0;
290 extern boolean draft_from_masquerading
; /* defined in mts.c */
295 static void putfmt (char *, char *, FILE *);
296 static void start_headers (void);
297 static void finish_headers (FILE *);
298 static int get_header (char *, struct headers
*);
299 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
300 static void putgrp (char *, char *, FILE *, unsigned int);
301 static int insert (struct mailname
*);
302 static void pl (void);
303 static void anno (void);
304 static int annoaux (struct mailname
*);
305 static void insert_fcc (struct headers
*, unsigned char *);
306 static void make_bcc_file (int);
307 static void verify_all_addresses (int);
308 static void chkadr (void);
309 static void sigon (void);
310 static void sigoff (void);
311 static void p_refile (char *);
312 static void fcc (char *, char *);
313 static void die (char *, char *, ...);
314 static void post (char *, int, int);
315 static void do_text (char *file
, int fd
);
316 static void do_an_address (struct mailname
*, int);
317 static void do_addresses (int, int);
318 static int find_prefix (void);
322 main (int argc
, char **argv
)
324 int state
, compnum
, dashstuff
= 0;
325 char *cp
, *msg
= NULL
, **argp
, **arguments
;
326 char buf
[BUFSIZ
], name
[NAMESZ
];
330 setlocale(LC_ALL
, "");
332 invo_name
= r1bindex (argv
[0], '/');
334 /* foil search of user profile/context */
335 if (context_foil (NULL
) == -1)
338 mts_init (invo_name
);
339 arguments
= getarguments (invo_name
, argc
, argv
, 0);
342 while ((cp
= *argp
++)) {
344 switch (smatch (++cp
, switches
)) {
346 ambigsw (cp
, switches
);
349 adios (NULL
, "-%s unknown", cp
);
352 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
353 print_help (buf
, switches
, 0);
356 print_version(invo_name
);
360 if (!(cp
= *argp
++) || *cp
== '-')
361 adios (NULL
, "missing argument to %s", argp
[-2]);
362 /* create a minimal context */
363 if (context_foil (cp
) == -1)
368 if (!(cp
= *argp
++) || *cp
== '-')
369 adios (NULL
, "missing argument to %s", argp
[-2]);
370 if ((state
= alias (cp
)) != AK_OK
)
371 adios (NULL
, "aliasing error in %s - %s",
372 cp
, akerror (state
));
391 if (!(filter
= *argp
++) || *filter
== '-')
392 adios (NULL
, "missing argument to %s", argp
[-2]);
447 if (!(cp
= *argp
++) || *cp
== '-')
448 adios (NULL
, "missing argument to %s", argp
[-2]);
449 if ((outputlinelen
= atoi (cp
)) < 10)
450 adios (NULL
, "impossible width %d", outputlinelen
);
454 if (!(cp
= *argp
++) || *cp
== '-')
455 adios (NULL
, "missing argument to %s", argp
[-2]);
456 if ((pfd
= atoi (cp
)) <= 2)
457 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
461 if (!(cp
= *argp
++) || *cp
== '-')
462 adios (NULL
, "missing argument to %s", argp
[-2]);
468 if (!(cp
= *argp
++) || *cp
== '-')
469 adios (NULL
, "missing argument to %s", argp
[-2]);
488 if (!(clientsw
= *argp
++) || *clientsw
== '-')
489 adios (NULL
, "missing argument to %s", argp
[-2]);
492 if (!(serversw
= *argp
++) || *serversw
== '-')
493 adios (NULL
, "missing argument to %s", argp
[-2]);
501 if (!(fill_in
= *argp
++) || *fill_in
== '-')
502 adios (NULL
, "missing argument to %s", argp
[-2]);
508 if (!(partno
= *argp
++) || *partno
== '-')
509 adios (NULL
, "missing argument to %s", argp
[-2]);
521 if (!(saslmech
= *argp
++) || *saslmech
== '-')
522 adios (NULL
, "missing argument to %s", argp
[-2]);
526 if (!(user
= *argp
++) || *user
== '-')
527 adios (NULL
, "missing argument to %s", argp
[-2]);
531 if (!(port
= *argp
++) || *port
== '-')
532 adios (NULL
, "missing argument to %s", argp
[-2]);
540 if (!(cp
= *argp
++) || *cp
== '-')
541 adios (NULL
, "missing argument to %s", argp
[-2]);
546 if (!(cp
= *argp
++) || *cp
== '-')
547 adios (NULL
, "missing argument to %s", argp
[-2]);
553 adios (NULL
, "only one message at a time!");
561 adios (NULL
, "usage: %s [switches] file", invo_name
);
563 if (outputlinelen
< 10)
564 adios (NULL
, "impossible width %d", outputlinelen
);
566 if ((in
= fopen (msg
, "r")) == NULL
)
567 adios (msg
, "unable to open");
572 discard (out
= stdout
); /* XXX: reference discard() to help loader */
575 if ((out
= fopen (fill_in
? fill_in
: "/dev/null", "w")) == NULL
)
576 adios ("/dev/null", "unable to open");
578 char *cp
= m_mktemp(m_maildir(invo_name
), NULL
, &out
);
580 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
582 adios ("post", "unable to create temporary file");
585 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
586 chmod (tmpfil
, 0600);
590 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
592 for (compnum
= 1, state
= FLD
;;) {
593 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
)) {
598 cp
= add (buf
, NULL
);
599 while (state
== FLDPLUS
) {
600 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
603 putfmt (name
, cp
, out
);
607 finish_headers (out
);
612 finish_headers (out
);
613 if (whomsw
&& !fill_in
)
615 fprintf (out
, "\n%s", buf
);
616 while (state
== BODY
) {
617 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
623 finish_headers (out
);
628 adios (NULL
, "message format error in component #%d", compnum
);
631 adios (NULL
, "getfld() returned %d", state
);
647 /* If we are doing a "whom" check */
650 verify_all_addresses (1);
654 if (msgflags
& MINV
) {
655 make_bcc_file (dashstuff
);
656 if (msgflags
& MVIS
) {
657 verify_all_addresses (verbose
);
658 post (tmpfil
, 0, verbose
);
660 post (bccfil
, 1, verbose
);
663 post (tmpfil
, 0, isatty (1));
670 printf (partno
? "Partial Message #%s Processed\n" : "Message Processed\n",
682 putfmt (char *name
, char *str
, FILE *out
)
684 int count
, grp
, i
, keep
;
687 struct mailname
*mp
= NULL
, *np
= NULL
;
690 while (*str
== ' ' || *str
== '\t')
693 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
694 advise (NULL
, "illegal header line -- %s:", name
);
699 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
700 fprintf (out
, "%s: %s", name
, str
);
705 if (hdr
->flags
& HIGN
) {
707 fprintf (out
, "%s: %s", name
, str
);
710 if (hdr
->flags
& HBAD
) {
712 fprintf (out
, "%s: %s", name
, str
);
714 advise (NULL
, "illegal header line -- %s:", name
);
719 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
721 if (hdr
->flags
& HSUB
)
722 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
723 if (hdr
->flags
& HFCC
) {
725 fprintf (out
, "%s: %s", name
, str
);
729 if ((cp
= strrchr(str
, '\n')))
731 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
733 insert_fcc (hdr
, pp
);
735 insert_fcc (hdr
, pp
);
739 if (!(hdr
->flags
& HADR
)) {
740 fprintf (out
, "%s: %s", name
, str
);
744 tmpaddrs
.m_next
= NULL
;
745 for (count
= 0; (cp
= getname (str
)); count
++)
746 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
750 tmpaddrs
.m_next
= mp
;
754 if (hdr
->flags
& HTRY
)
760 if (hdr
->flags
& HNIL
)
761 fprintf (out
, "%s: %s", name
, str
);
764 advise (NULL
, "%s: field requires at least one address", name
);
771 nameoutput
= linepos
= 0;
772 snprintf (namep
, sizeof(namep
), "%s%s",
773 !fill_in
&& (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
775 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
776 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
777 /* The address doesn't include a host, so it might be an alias. */
778 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
779 qp
= akvisible () ? mp
->m_mbox
: "";
782 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
783 while ((cp
= getname (pp
))) {
784 if (!(mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
789 if (draft_from_masquerading
&& ((msgstate
== RESENT
)
791 : (hdr
->set
& MFRM
)))
792 /* The user manually specified a [Resent-]From: address in
793 their draft and the "masquerade:" line in mts.conf
794 doesn't contain "draft_from", so we'll set things up to
795 use the actual email address embedded in the draft
796 [Resent-]From: (after alias substitution, and without the
797 GECOS full name or angle brackets) as the envelope
799 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
801 if (hdr
->flags
& HBCC
)
804 mp
->m_ingrp
= np
->m_ingrp
;
807 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
810 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
811 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
820 /* Address includes a host, so no alias substitution is needed. */
821 if (draft_from_masquerading
&& ((msgstate
== RESENT
)
823 : (hdr
->set
& MFRM
)))
824 /* The user manually specified a [Resent-]From: address in
825 their draft and the "masquerade:" line in mts.conf
826 doesn't contain "draft_from", so we'll set things up to
827 use the actual email address embedded in the draft
828 [Resent-]From: (after alias substitution, and without the
829 GECOS full name or angle brackets) as the envelope
831 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
833 if (hdr
->flags
& HBCC
)
836 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
839 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
843 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
849 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
850 advise (NULL
, "%s: field does not allow groups", name
);
854 if (fill_in
&& grp
> 0)
865 char myhost
[BUFSIZ
], sigbuf
[BUFSIZ
];
872 strncpy (from
, adrsprintf (NULL
, NULL
), sizeof(from
));
873 strncpy (myhost
, LocalName (), sizeof(myhost
));
875 for (cp
= myhost
; *cp
; cp
++)
878 if ((cp
= getfullname ()) && *cp
) {
879 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
880 snprintf (signature
, sizeof(signature
), "%s <%s>",
881 sigbuf
, adrsprintf (NULL
, NULL
));
882 if ((cp
= getname (signature
)) == NULL
)
883 adios (NULL
, "getname () failed -- you lose extraordinarily big");
884 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
)) == NULL
)
885 adios (NULL
, "bad signature '%s'", sigbuf
);
890 strncpy (signature
, adrsprintf (NULL
, NULL
), sizeof(signature
));
896 * Now that we've outputted the header fields in the draft
897 * message, we will now output any remaining header fields
898 * that we need to add/create.
902 finish_headers (FILE *out
)
906 if (whomsw
&& !fill_up
)
909 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
911 fprintf (out
, "Message-ID: <%d.%ld@%s>\n",
912 (int) getpid (), (long) tclock
, LocalName ());
913 if (msgflags
& MFRM
) {
914 /* There was already a From: in the draft. Don't add one. */
915 if (!draft_from_masquerading
)
916 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
917 so we'll reveal the user's actual account@thismachine
918 address in a Sender: header (and use it as the envelope
920 fprintf (out
, "Sender: %s\n", from
);
923 /* Construct a From: header. */
924 fprintf (out
, "From: %s\n", signature
);
928 if (!(msgflags
& MVIS
))
929 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
933 if (!(msgflags
& MDAT
)) {
934 advise (NULL
, "message has no Date: header");
937 if (!(msgflags
& MFRM
)) {
938 advise (NULL
, "message has no From: header");
941 if (whomsw
&& !fill_up
)
944 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
946 fprintf (out
, "Resent-Message-ID: <%d.%ld@%s>\n",
947 (int) getpid (), (long) tclock
, LocalName ());
948 if (msgflags
& MRFM
) {
949 /* There was already a Resent-From: in draft. Don't add one. */
950 if (!draft_from_masquerading
)
951 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
952 so we'll reveal the user's actual account@thismachine
953 address in a Sender: header (and use it as the envelope
955 fprintf (out
, "Resent-Sender: %s\n", from
);
958 /* Construct a Resent-From: header. */
959 fprintf (out
, "Resent-From: %s\n", signature
);
962 if (!(msgflags
& MVIS
))
963 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
968 adios (NULL
, "re-format message and try again");
970 adios (NULL
, "no addressees");
975 get_header (char *header
, struct headers
*table
)
979 for (h
= table
; h
->value
; h
++)
980 if (!mh_strcasecmp (header
, h
->value
))
988 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
994 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
996 if ((!fill_in
&& (flags
& (HBCC
| HDCC
))) || mp
->m_ingrp
)
1000 fprintf (out
, "%s: ", name
);
1001 linepos
+= (nameoutput
= strlen (name
) + 2);
1004 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1005 mp
->m_pers
= getcpy (aka
);
1007 if (mp
->m_gname
&& !fill_in
) {
1008 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1011 cp
= adrformat (mp
);
1018 if (linepos
!= nameoutput
) {
1019 if (len
+ linepos
+ 2 > outputlinelen
)
1020 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1030 return (flags
& HTRY
);
1035 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1040 if (!fill_in
&& (flags
& HBCC
))
1044 fprintf (out
, "%s: ", name
);
1045 linepos
+= (nameoutput
= strlen (name
) + 2);
1047 linepos
-= strlen (group
);
1050 cp
= fill_in
? group
: concat (group
, ";", NULL
);
1053 if (linepos
> nameoutput
) {
1054 if (len
+ linepos
+ 2 > outputlinelen
) {
1055 fprintf (out
, ",\n%*s", nameoutput
, "");
1056 linepos
= nameoutput
;
1070 insert (struct mailname
*np
)
1072 struct mailname
*mp
;
1074 if (np
->m_mbox
== NULL
)
1077 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1078 : np
->m_type
== UUCPHOST
? &uuaddrs
1082 if (!mh_strcasecmp (np
->m_host
, mp
->m_next
->m_host
)
1083 && !mh_strcasecmp (np
->m_mbox
, mp
->m_next
->m_mbox
)
1084 && np
->m_bcc
== mp
->m_next
->m_bcc
)
1097 struct mailname
*mp
;
1099 printf ("-------\n\t-- Addresses --\nlocal:\t");
1100 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1101 printf ("%s%s%s", mp
->m_mbox
,
1102 mp
->m_bcc
? "[BCC]" : "",
1103 mp
->m_next
? ",\n\t" : "");
1105 printf ("\nnet:\t");
1106 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1107 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1108 mp
->m_mbox
, mp
->m_host
,
1109 mp
->m_bcc
? "[BCC]" : "",
1110 mp
->m_next
? ",\n\t" : "");
1112 printf ("\nuucp:\t");
1113 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1114 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1115 mp
->m_bcc
? "[BCC]" : "",
1116 mp
->m_next
? ",\n\t" : "");
1118 printf ("\n\t-- Folder Copies --\nfcc:\t");
1119 for (i
= 0; i
< fccind
; i
++)
1120 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1128 struct mailname
*mp
;
1130 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1131 if (annoaux (mp
) == NOTOK
)
1134 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1135 if (annoaux (mp
) == NOTOK
)
1138 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1139 if (annoaux (mp
) == NOTOK
)
1149 annoaux (struct mailname
*mp
)
1152 char buffer
[BUFSIZ
];
1154 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1155 i
= strlen (buffer
);
1157 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1162 insert_fcc (struct headers
*hdr
, unsigned char *pp
)
1166 for (cp
= pp
; isspace (*cp
); cp
++)
1168 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace (*pp
); pp
--)
1176 adios (NULL
, "too many %ss", hdr
->value
);
1177 fccfold
[fccind
++] = getcpy (cp
);
1185 make_bcc_file (int dashstuff
)
1193 tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
);
1194 if (tfile
== NULL
) adios("bcc", "unable to create temporary file");
1195 chmod (bccfil
, 0600);
1196 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1198 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1200 fprintf (out
, "Message-ID: <%d.%ld@%s>\n",
1201 (int) getpid (), (long) tclock
, LocalName ());
1202 if (msgflags
& MFRM
) {
1203 /* There was already a From: in the draft. Don't add one. */
1204 if (!draft_from_masquerading
)
1205 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
1206 so we'll reveal the user's actual account@thismachine
1207 address in a Sender: header (and use it as the envelope
1209 fprintf (out
, "Sender: %s\n", from
);
1212 /* Construct a From: header. */
1213 fprintf (out
, "From: %s\n", signature
);
1215 fprintf (out
, "Subject: %s", subject
);
1216 fprintf (out
, "BCC:\n");
1219 * Use MIME encapsulation for Bcc messages
1225 * Check if any lines in the message clash with the
1226 * prefix for the MIME multipart separator. If there
1227 * is a clash, increment one of the letters in the
1228 * prefix and check again.
1230 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1231 adios (NULL
, "lost prefix start");
1232 while (find_prefix () == NOTOK
) {
1237 adios (NULL
, "can't find a unique delimiter string");
1242 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1243 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1244 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1246 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1252 * Do mhl filtering of Bcc messages instead
1253 * of MIME encapsulation.
1255 if (filter
!= NULL
) {
1256 vec
[0] = r1bindex (mhlproc
, '/');
1258 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1262 adios ("fork", "unable to");
1265 dup2 (fileno (out
), 1);
1268 vec
[i
++] = "-forward";
1273 /* was the flag -[no]dashstuffing specified? */
1275 vec
[i
++] = "-dashstuffing";
1276 else if (dashstuff
< 0)
1277 vec
[i
++] = "-nodashstuffing";
1280 execvp (mhlproc
, vec
);
1281 fprintf (stderr
, "unable to exec ");
1286 pidXwait (child_id
, mhlproc
);
1290 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1291 adios (tmpfil
, "unable to re-open");
1294 * If using MIME encapsulation, or if the -nodashstuffing
1295 * flag was given, then just copy message. Else do
1296 * RFC934 quoting (dashstuffing).
1298 if (mime
|| dashstuff
< 0)
1299 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1301 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1305 fseek (out
, 0L, SEEK_END
);
1307 fprintf (out
, "\n--%s--\n", prefix
);
1309 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1315 * Scan message to check if any lines clash with
1316 * the prefix of the MIME multipart separator.
1323 unsigned char buffer
[BUFSIZ
];
1326 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1327 adios (tmpfil
, "unable to re-open");
1329 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1330 if (buffer
[0] == '-' && buffer
[1] == '-') {
1333 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1337 if (strcmp (buffer
+ 2, prefix
) == 0) {
1348 #define plural(x) (x == 1 ? "" : "s")
1353 if (badadr
&& unkadr
)
1354 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1355 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1357 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1359 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1364 do_addresses (int bccque
, int talk
)
1368 struct mailname
*lp
;
1371 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1372 if (lp
->m_bcc
? bccque
: !bccque
) {
1374 printf (" -- Local Recipients --\n");
1375 do_an_address (lp
, talk
);
1380 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1381 if (lp
->m_bcc
? bccque
: !bccque
) {
1383 printf (" -- UUCP Recipients --\n");
1384 do_an_address (lp
, talk
);
1389 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1390 if (lp
->m_bcc
? bccque
: !bccque
) {
1392 printf (" -- Network Recipients --\n");
1393 do_an_address (lp
, talk
);
1400 if (rp_isbad (retval
= sm_waend ()))
1401 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1402 #endif /* SMTPMTS */
1407 * MTS-SPECIFIC INTERACTION
1412 * SENDMAIL/SMTP routines
1418 post (char *file
, int bccque
, int talk
)
1423 onex
= !(msgflags
& MINV
) || bccque
;
1425 if (msgflags
& MINV
)
1426 printf (" -- Posting for %s Recipients --\n",
1427 bccque
? "Blind" : "Sighted");
1429 printf (" -- Posting for All Recipients --\n");
1434 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
, verbose
,
1435 snoop
, onex
, queued
, sasl
, saslmech
,
1437 || rp_isbad (retval
= sm_winit (smtpmode
, from
)))
1438 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1440 do_addresses (bccque
, talk
&& verbose
);
1441 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1442 die (file
, "unable to re-open");
1447 sm_end (onex
? OK
: DONE
);
1451 if (msgflags
& MINV
)
1452 printf (" -- %s Recipient Copies Posted --\n",
1453 bccque
? "Blind" : "Sighted");
1455 printf (" -- Recipient Copies Posted --\n");
1462 /* Address Verification */
1465 verify_all_addresses (int talk
)
1468 struct mailname
*lp
;
1472 if (!whomsw
|| checksw
)
1473 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1474 verbose
, snoop
, 0, queued
, sasl
,
1475 saslmech
, user
, tls
))
1476 || rp_isbad (retval
= sm_winit (smtpmode
, from
)))
1477 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1479 if (talk
&& !whomsw
)
1480 printf (" -- Address Verification --\n");
1481 if (talk
&& localaddrs
.m_next
)
1482 printf (" -- Local Recipients --\n");
1483 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1484 do_an_address (lp
, talk
);
1486 if (talk
&& uuaddrs
.m_next
)
1487 printf (" -- UUCP Recipients --\n");
1488 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1489 do_an_address (lp
, talk
);
1491 if (talk
&& netaddrs
.m_next
)
1492 printf (" -- Network Recipients --\n");
1493 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1494 do_an_address (lp
, talk
);
1497 if (talk
&& !whomsw
)
1498 printf (" -- Address Verification Successful --\n");
1500 if (!whomsw
|| checksw
)
1509 do_an_address (struct mailname
*lp
, int talk
)
1515 switch (lp
->m_type
) {
1519 strncpy (addr
, mbox
, sizeof(addr
));
1523 mbox
= auxformat (lp
, 0);
1525 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1528 default: /* let SendMail decide if the host is bad */
1531 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1536 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1538 if (whomsw
&& !checksw
) {
1546 switch (retval
= sm_wadr (mbox
, host
,
1547 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1550 printf ("address ok\n");
1556 fprintf (stderr
, " %s: ", addr
);
1557 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1558 rp_string (retval
));
1564 fprintf (stderr
, " %s: ", addr
);
1565 die (NULL
, "unexpected response; %s", rp_string (retval
));
1573 do_text (char *file
, int fd
)
1578 lseek (fd
, (off_t
) 0, SEEK_SET
);
1580 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1581 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1582 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1586 die (file
, "problem reading from");
1588 switch (retval
= sm_wtend ()) {
1594 die (NULL
, "posting failed; %s", rp_string (retval
));
1597 die (NULL
, "unexpected response; %s", rp_string (retval
));
1601 #endif /* SMTPMTS */
1614 if (msgflags
& MINV
)
1618 if (!whomsw
|| checksw
)
1620 #endif /* SMTPMTS */
1632 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1633 istat
= SIGNAL2 (SIGINT
, sigser
);
1634 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1635 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1645 SIGNAL (SIGHUP
, hstat
);
1646 SIGNAL (SIGINT
, istat
);
1647 SIGNAL (SIGQUIT
, qstat
);
1648 SIGNAL (SIGTERM
, tstat
);
1656 p_refile (char *file
)
1664 printf (" -- Filing Folder Copies --\n");
1665 for (i
= 0; i
< fccind
; i
++)
1666 fcc (file
, fccfold
[i
]);
1668 printf (" -- Folder Copies Filed --\n");
1673 * Call the `fileproc' to add the file to the folder.
1677 fcc (char *file
, char *folder
)
1684 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1687 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1693 fprintf (stderr
, " %sFcc %s: ",
1694 msgstate
== RESENT
? "Resent-" : "", folder
);
1695 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1699 /* see if we need to add `+' */
1700 snprintf (fold
, sizeof(fold
), "%s%s",
1701 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1703 /* now exec the fileproc */
1704 execlp (fileproc
, r1bindex (fileproc
, '/'),
1705 "-link", "-file", file
, fold
, NULL
);
1709 if ((status
= pidwait (child_id
, OK
))) {
1711 fprintf (stderr
, " %sFcc %s: ",
1712 msgstate
== RESENT
? "Resent-" : "", folder
);
1713 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1716 printf ("folder ok\n");
1728 die (char *what
, char *fmt
, ...)
1733 if (msgflags
& MINV
)
1737 if (!whomsw
|| checksw
)
1739 #endif /* SMTPMTS */
1742 advertise (what
, NULL
, fmt
, ap
);