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>
31 # include <mts/smtp/smtp.h>
35 # define SASLminc(a) (a)
36 #else /* CYRUS_SASL */
37 # define SASLminc(a) 0
38 #endif /* CYRUS_SASL */
41 # define TLSminc(a) (a)
42 #else /* TLS_SUPPORT */
44 #endif /* TLS_SUPPORT */
46 #define FCCS 10 /* max number of fccs allowed */
48 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
50 /* In the following array of structures, the numeric second field of the
51 structures (minchars) is apparently used like this:
53 -# : Switch can be abbreviated to # characters; switch hidden in -help.
54 0 : Switch can't be abbreviated; switch shown in -help.
55 # : Switch can be abbreviated to # characters; switch shown in -help. */
57 static struct swit switches
[] = {
59 { "alias aliasfile", 0 },
61 { "check", -5 }, /* interface from whom */
63 { "nocheck", -7 }, /* interface from whom */
67 { "dist", -4 }, /* interface from dist */
69 { "filter filterfile", 0 },
77 { "library directory", -7 }, /* interface from send, whom */
95 { "whom", -4 }, /* interface from whom */
97 { "width columns", 0 },
102 #define BITSTUFFSW 22
103 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
104 #define NBITSTUFFSW 23
105 { "nodashstuffing", -14 },
107 { "mail", -4 }, /* specify MAIL smtp mode */
109 { "saml", -4 }, /* specify SAML smtp mode */
111 { "send", -4 }, /* specify SEND smtp mode */
113 { "soml", -4 }, /* specify SOML smtp mode */
115 { "idanno number", -6 }, /* interface from send */
117 { "deliver address-list", -7 },
119 { "client host", -6 },
121 { "server host", -6 }, /* specify alternate SMTP server */
123 { "snoop", -5 }, /* snoop the SMTP transaction */
125 { "fill-in file", -7 },
133 { "sasl", SASLminc(-4) },
134 #define SASLMECHSW 38
135 { "saslmech", SASLminc(-5) },
137 { "user", SASLminc(-4) },
139 { "port server port name/number", 4 },
141 { "tls", TLSminc(-3) },
142 #define FILEPROCSW 42
157 * flags for headers->flags
159 #define HNOP 0x0000 /* just used to keep .set around */
160 #define HBAD 0x0001 /* bad header - don't let it through */
161 #define HADR 0x0002 /* header has an address field */
162 #define HSUB 0x0004 /* Subject: header */
163 #define HTRY 0x0008 /* try to send to addrs on header */
164 #define HBCC 0x0010 /* don't output this header */
165 #define HMNG 0x0020 /* munge this header */
166 #define HNGR 0x0040 /* no groups allowed in this header */
167 #define HFCC 0x0080 /* FCC: type header */
168 #define HNIL 0x0100 /* okay for this header not to have addrs */
169 #define HIGN 0x0200 /* ignore this header */
170 #define HDCC 0x0400 /* another undocumented feature */
173 * flags for headers->set
175 #define MFRM 0x0001 /* we've seen a From: */
176 #define MDAT 0x0002 /* we've seen a Date: */
177 #define MRFM 0x0004 /* we've seen a Resent-From: */
178 #define MVIS 0x0008 /* we've seen sighted addrs */
179 #define MINV 0x0010 /* we've seen blind addrs */
182 static struct headers NHeaders
[] = {
183 { "Return-Path", HBAD
, 0 },
184 { "Received", HBAD
, 0 },
185 { "Reply-To", HADR
|HNGR
, 0 },
186 { "From", HADR
|HNGR
, MFRM
},
187 { "Sender", HADR
|HBAD
, 0 },
189 { "Subject", HSUB
, 0 },
190 { "To", HADR
|HTRY
, MVIS
},
191 { "cc", HADR
|HTRY
, MVIS
},
192 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
193 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
194 { "Message-ID", HBAD
, 0 },
199 static struct headers RHeaders
[] = {
200 { "Resent-Reply-To", HADR
|HNGR
, 0 },
201 { "Resent-From", HADR
|HNGR
, MRFM
},
202 { "Resent-Sender", HADR
|HBAD
, 0 },
203 { "Resent-Date", HBAD
, 0 },
204 { "Resent-Subject", HSUB
, 0 },
205 { "Resent-To", HADR
|HTRY
, MVIS
},
206 { "Resent-cc", HADR
|HTRY
, MVIS
},
207 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
208 { "Resent-Message-ID", HBAD
, 0 },
209 { "Resent-Fcc", HFCC
, 0 },
210 { "Reply-To", HADR
, 0 },
211 { "From", HADR
|HNGR
, MFRM
},
212 { "Sender", HADR
|HNGR
, 0 },
213 { "Date", HNOP
, MDAT
},
214 { "To", HADR
|HNIL
, 0 },
215 { "cc", HADR
|HNIL
, 0 },
216 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
221 static short fccind
= 0; /* index into fccfold[] */
222 static short outputlinelen
= OUTPUTLINELEN
;
224 static int pfd
= NOTOK
; /* fd to write annotation list to */
225 static uid_t myuid
= -1; /* my user id */
226 static gid_t mygid
= -1; /* my group id */
227 static int recipients
= 0; /* how many people will get a copy */
228 static int unkadr
= 0; /* how many of those were unknown */
229 static int badadr
= 0; /* number of bad addrs */
230 static int badmsg
= 0; /* message has bad semantics */
231 static int verbose
= 0; /* spell it out */
232 static int format
= 1; /* format addresses */
233 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
234 static int msgid
= 0; /* add msgid */
235 static int debug
= 0; /* debugging post */
236 static int watch
= 0; /* watch the delivery process */
237 static int whomsw
= 0; /* we are whom not post */
238 static int checksw
= 0; /* whom -check */
239 static int linepos
=0; /* putadr()'s position on the line */
240 static int nameoutput
=0; /* putadr() has output header name */
241 static int sasl
=0; /* Use SASL auth for SMTP */
242 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
243 static char *user
=NULL
; /* Authenticate as this user */
244 static char *port
="smtp"; /* Name of server port for SMTP */
245 static int tls
=0; /* Use TLS for encryption */
247 static unsigned msgflags
= 0; /* what we've seen */
251 static int msgstate
= NORMAL
;
253 static time_t tclock
= 0; /* the time we started (more or less) */
255 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
257 static char tmpfil
[BUFSIZ
];
258 static char bccfil
[BUFSIZ
];
260 static char from
[BUFSIZ
]; /* my network address */
261 static char signature
[BUFSIZ
]; /* my signature */
262 static char *filter
= NULL
; /* the filter for BCC'ing */
263 static char *subject
= NULL
; /* the subject field for BCC'ing */
264 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
266 static struct headers
*hdrtab
; /* table for the message we're doing */
268 static struct mailname localaddrs
; /* local addrs */
269 static struct mailname netaddrs
; /* network addrs */
270 static struct mailname uuaddrs
; /* uucp addrs */
271 static struct mailname tmpaddrs
; /* temporary queue */
274 static int snoop
= 0;
275 static int smtpmode
= S_MAIL
;
276 static char *clientsw
= NULL
;
277 static char *serversw
= NULL
;
279 extern struct smtp sm_reply
;
282 static char prefix
[] = "----- =_aaaaaaaaaa";
284 static int fill_up
= 0;
285 static char *fill_in
= NULL
;
286 static char *partno
= NULL
;
287 static int queued
= 0;
289 extern boolean draft_from_masquerading
; /* defined in mts.c */
294 static void putfmt (char *, char *, FILE *);
295 static void start_headers (void);
296 static void finish_headers (FILE *);
297 static int get_header (char *, struct headers
*);
298 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
299 static void putgrp (char *, char *, FILE *, unsigned int);
300 static int insert (struct mailname
*);
301 static void pl (void);
302 static void anno (void);
303 static int annoaux (struct mailname
*);
304 static void insert_fcc (struct headers
*, unsigned char *);
305 static void make_bcc_file (int);
306 static void verify_all_addresses (int);
307 static void chkadr (void);
308 static void sigon (void);
309 static void sigoff (void);
310 static void p_refile (char *);
311 static void fcc (char *, char *);
312 static void die (char *, char *, ...);
313 static void post (char *, int, int);
314 static void do_text (char *file
, int fd
);
315 static void do_an_address (struct mailname
*, int);
316 static void do_addresses (int, int);
317 static int find_prefix (void);
321 main (int argc
, char **argv
)
323 int state
, compnum
, dashstuff
= 0;
324 char *cp
, *msg
= NULL
, **argp
, **arguments
;
325 char buf
[BUFSIZ
], name
[NAMESZ
];
329 setlocale(LC_ALL
, "");
331 invo_name
= r1bindex (argv
[0], '/');
333 /* foil search of user profile/context */
334 if (context_foil (NULL
) == -1)
337 mts_init (invo_name
);
338 arguments
= getarguments (invo_name
, argc
, argv
, 0);
341 while ((cp
= *argp
++)) {
343 switch (smatch (++cp
, switches
)) {
345 ambigsw (cp
, switches
);
348 adios (NULL
, "-%s unknown", cp
);
351 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
352 print_help (buf
, switches
, 0);
355 print_version(invo_name
);
359 if (!(cp
= *argp
++) || *cp
== '-')
360 adios (NULL
, "missing argument to %s", argp
[-2]);
361 /* create a minimal context */
362 if (context_foil (cp
) == -1)
367 if (!(cp
= *argp
++) || *cp
== '-')
368 adios (NULL
, "missing argument to %s", argp
[-2]);
369 if ((state
= alias (cp
)) != AK_OK
)
370 adios (NULL
, "aliasing error in %s - %s",
371 cp
, akerror (state
));
390 if (!(filter
= *argp
++) || *filter
== '-')
391 adios (NULL
, "missing argument to %s", argp
[-2]);
446 if (!(cp
= *argp
++) || *cp
== '-')
447 adios (NULL
, "missing argument to %s", argp
[-2]);
448 if ((outputlinelen
= atoi (cp
)) < 10)
449 adios (NULL
, "impossible width %d", outputlinelen
);
453 if (!(cp
= *argp
++) || *cp
== '-')
454 adios (NULL
, "missing argument to %s", argp
[-2]);
455 if ((pfd
= atoi (cp
)) <= 2)
456 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
460 if (!(cp
= *argp
++) || *cp
== '-')
461 adios (NULL
, "missing argument to %s", argp
[-2]);
467 if (!(cp
= *argp
++) || *cp
== '-')
468 adios (NULL
, "missing argument to %s", argp
[-2]);
487 if (!(clientsw
= *argp
++) || *clientsw
== '-')
488 adios (NULL
, "missing argument to %s", argp
[-2]);
491 if (!(serversw
= *argp
++) || *serversw
== '-')
492 adios (NULL
, "missing argument to %s", argp
[-2]);
500 if (!(fill_in
= *argp
++) || *fill_in
== '-')
501 adios (NULL
, "missing argument to %s", argp
[-2]);
507 if (!(partno
= *argp
++) || *partno
== '-')
508 adios (NULL
, "missing argument to %s", argp
[-2]);
520 if (!(saslmech
= *argp
++) || *saslmech
== '-')
521 adios (NULL
, "missing argument to %s", argp
[-2]);
525 if (!(user
= *argp
++) || *user
== '-')
526 adios (NULL
, "missing argument to %s", argp
[-2]);
530 if (!(port
= *argp
++) || *port
== '-')
531 adios (NULL
, "missing argument to %s", argp
[-2]);
539 if (!(cp
= *argp
++) || *cp
== '-')
540 adios (NULL
, "missing argument to %s", argp
[-2]);
545 if (!(cp
= *argp
++) || *cp
== '-')
546 adios (NULL
, "missing argument to %s", argp
[-2]);
552 adios (NULL
, "only one message at a time!");
560 adios (NULL
, "usage: %s [switches] file", invo_name
);
562 if (outputlinelen
< 10)
563 adios (NULL
, "impossible width %d", outputlinelen
);
565 if ((in
= fopen (msg
, "r")) == NULL
)
566 adios (msg
, "unable to open");
571 discard (out
= stdout
); /* XXX: reference discard() to help loader */
574 if ((out
= fopen (fill_in
? fill_in
: "/dev/null", "w")) == NULL
)
575 adios ("/dev/null", "unable to open");
577 char *cp
= m_mktemp(m_maildir(invo_name
), NULL
, &out
);
579 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
581 adios ("post", "unable to create temporary file");
584 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
585 chmod (tmpfil
, 0600);
589 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
591 for (compnum
= 1, state
= FLD
;;) {
592 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
)) {
597 cp
= add (buf
, NULL
);
598 while (state
== FLDPLUS
) {
599 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
602 putfmt (name
, cp
, out
);
606 finish_headers (out
);
611 finish_headers (out
);
612 if (whomsw
&& !fill_in
)
614 fprintf (out
, "\n%s", buf
);
615 while (state
== BODY
) {
616 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
622 finish_headers (out
);
627 adios (NULL
, "message format error in component #%d", compnum
);
630 adios (NULL
, "getfld() returned %d", state
);
646 /* If we are doing a "whom" check */
649 verify_all_addresses (1);
653 if (msgflags
& MINV
) {
654 make_bcc_file (dashstuff
);
655 if (msgflags
& MVIS
) {
656 verify_all_addresses (verbose
);
657 post (tmpfil
, 0, verbose
);
659 post (bccfil
, 1, verbose
);
662 post (tmpfil
, 0, isatty (1));
669 printf (partno
? "Partial Message #%s Processed\n" : "Message Processed\n",
681 putfmt (char *name
, char *str
, FILE *out
)
683 int count
, grp
, i
, keep
;
686 struct mailname
*mp
= NULL
, *np
= NULL
;
689 while (*str
== ' ' || *str
== '\t')
692 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
693 advise (NULL
, "illegal header line -- %s:", name
);
698 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
699 fprintf (out
, "%s: %s", name
, str
);
704 if (hdr
->flags
& HIGN
) {
706 fprintf (out
, "%s: %s", name
, str
);
709 if (hdr
->flags
& HBAD
) {
711 fprintf (out
, "%s: %s", name
, str
);
713 advise (NULL
, "illegal header line -- %s:", name
);
718 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
720 if (hdr
->flags
& HSUB
)
721 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
722 if (hdr
->flags
& HFCC
) {
724 fprintf (out
, "%s: %s", name
, str
);
728 if ((cp
= strrchr(str
, '\n')))
730 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
732 insert_fcc (hdr
, pp
);
734 insert_fcc (hdr
, pp
);
738 if (!(hdr
->flags
& HADR
)) {
739 fprintf (out
, "%s: %s", name
, str
);
743 tmpaddrs
.m_next
= NULL
;
744 for (count
= 0; (cp
= getname (str
)); count
++)
745 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
749 tmpaddrs
.m_next
= mp
;
753 if (hdr
->flags
& HTRY
)
759 if (hdr
->flags
& HNIL
)
760 fprintf (out
, "%s: %s", name
, str
);
763 advise (NULL
, "%s: field requires at least one address", name
);
770 nameoutput
= linepos
= 0;
771 snprintf (namep
, sizeof(namep
), "%s%s",
772 !fill_in
&& (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
774 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
775 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
776 /* The address doesn't include a host, so it might be an alias. */
777 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
778 qp
= akvisible () ? mp
->m_mbox
: "";
781 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
782 while ((cp
= getname (pp
))) {
783 if (!(mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
788 if (draft_from_masquerading
&& ((msgstate
== RESENT
)
790 : (hdr
->set
& MFRM
)))
791 /* The user manually specified a [Resent-]From: address in
792 their draft and the "masquerade:" line in mts.conf
793 doesn't contain "draft_from", so we'll set things up to
794 use the actual email address embedded in the draft
795 [Resent-]From: (after alias substitution, and without the
796 GECOS full name or angle brackets) as the envelope
798 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
800 if (hdr
->flags
& HBCC
)
803 mp
->m_ingrp
= np
->m_ingrp
;
806 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
809 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
810 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
819 /* Address includes a host, so no alias substitution is needed. */
820 if (draft_from_masquerading
&& ((msgstate
== RESENT
)
822 : (hdr
->set
& MFRM
)))
823 /* The user manually specified a [Resent-]From: address in
824 their draft and the "masquerade:" line in mts.conf
825 doesn't contain "draft_from", so we'll set things up to
826 use the actual email address embedded in the draft
827 [Resent-]From: (after alias substitution, and without the
828 GECOS full name or angle brackets) as the envelope
830 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
832 if (hdr
->flags
& HBCC
)
835 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
838 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
842 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
848 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
849 advise (NULL
, "%s: field does not allow groups", name
);
853 if (fill_in
&& grp
> 0)
864 char myhost
[BUFSIZ
], sigbuf
[BUFSIZ
];
871 strncpy (from
, getlocaladdr(), sizeof(from
));
872 strncpy (myhost
, LocalName (0), sizeof(myhost
));
874 for (cp
= myhost
; *cp
; cp
++)
877 if ((cp
= getfullname ()) && *cp
) {
878 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
879 snprintf (signature
, sizeof(signature
), "%s <%s>",
880 sigbuf
, getlocaladdr());
881 if ((cp
= getname (signature
)) == NULL
)
882 adios (NULL
, "getname () failed -- you lose extraordinarily big");
883 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
)) == NULL
)
884 adios (NULL
, "bad signature '%s'", sigbuf
);
889 strncpy (signature
, getlocaladdr(), sizeof(signature
));
895 * Now that we've outputted the header fields in the draft
896 * message, we will now output any remaining header fields
897 * that we need to add/create.
901 finish_headers (FILE *out
)
905 if (whomsw
&& !fill_up
)
908 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
910 fprintf (out
, "Message-ID: <%d.%ld@%s>\n",
911 (int) getpid (), (long) tclock
, LocalName (1));
912 if (msgflags
& MFRM
) {
913 /* There was already a From: in the draft. Don't add one. */
914 if (!draft_from_masquerading
)
915 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
916 so we'll reveal the user's actual account@thismachine
917 address in a Sender: header (and use it as the envelope
919 fprintf (out
, "Sender: %s\n", from
);
922 /* Construct a From: header. */
923 fprintf (out
, "From: %s\n", signature
);
927 if (!(msgflags
& MVIS
))
928 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
932 if (!(msgflags
& MDAT
)) {
933 advise (NULL
, "message has no Date: header");
936 if (!(msgflags
& MFRM
)) {
937 advise (NULL
, "message has no From: header");
940 if (whomsw
&& !fill_up
)
943 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
945 fprintf (out
, "Resent-Message-ID: <%d.%ld@%s>\n",
946 (int) getpid (), (long) tclock
, LocalName (1));
947 if (msgflags
& MRFM
) {
948 /* There was already a Resent-From: in draft. Don't add one. */
949 if (!draft_from_masquerading
)
950 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
951 so we'll reveal the user's actual account@thismachine
952 address in a Sender: header (and use it as the envelope
954 fprintf (out
, "Resent-Sender: %s\n", from
);
957 /* Construct a Resent-From: header. */
958 fprintf (out
, "Resent-From: %s\n", signature
);
961 if (!(msgflags
& MVIS
))
962 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
967 adios (NULL
, "re-format message and try again");
969 adios (NULL
, "no addressees");
974 get_header (char *header
, struct headers
*table
)
978 for (h
= table
; h
->value
; h
++)
979 if (!mh_strcasecmp (header
, h
->value
))
987 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
993 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
995 if ((!fill_in
&& (flags
& (HBCC
| HDCC
))) || mp
->m_ingrp
)
999 fprintf (out
, "%s: ", name
);
1000 linepos
+= (nameoutput
= strlen (name
) + 2);
1003 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1004 mp
->m_pers
= getcpy (aka
);
1006 if (mp
->m_gname
&& !fill_in
) {
1007 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1010 cp
= adrformat (mp
);
1017 if (linepos
!= nameoutput
) {
1018 if (len
+ linepos
+ 2 > outputlinelen
)
1019 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1029 return (flags
& HTRY
);
1034 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1039 if (!fill_in
&& (flags
& HBCC
))
1043 fprintf (out
, "%s: ", name
);
1044 linepos
+= (nameoutput
= strlen (name
) + 2);
1046 linepos
-= strlen (group
);
1049 cp
= fill_in
? group
: concat (group
, ";", NULL
);
1052 if (linepos
> nameoutput
) {
1053 if (len
+ linepos
+ 2 > outputlinelen
) {
1054 fprintf (out
, ",\n%*s", nameoutput
, "");
1055 linepos
= nameoutput
;
1069 insert (struct mailname
*np
)
1071 struct mailname
*mp
;
1073 if (np
->m_mbox
== NULL
)
1076 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1077 : np
->m_type
== UUCPHOST
? &uuaddrs
1081 if (!mh_strcasecmp (np
->m_host
, mp
->m_next
->m_host
)
1082 && !mh_strcasecmp (np
->m_mbox
, mp
->m_next
->m_mbox
)
1083 && np
->m_bcc
== mp
->m_next
->m_bcc
)
1096 struct mailname
*mp
;
1098 printf ("-------\n\t-- Addresses --\nlocal:\t");
1099 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1100 printf ("%s%s%s", mp
->m_mbox
,
1101 mp
->m_bcc
? "[BCC]" : "",
1102 mp
->m_next
? ",\n\t" : "");
1104 printf ("\nnet:\t");
1105 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1106 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1107 mp
->m_mbox
, mp
->m_host
,
1108 mp
->m_bcc
? "[BCC]" : "",
1109 mp
->m_next
? ",\n\t" : "");
1111 printf ("\nuucp:\t");
1112 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1113 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1114 mp
->m_bcc
? "[BCC]" : "",
1115 mp
->m_next
? ",\n\t" : "");
1117 printf ("\n\t-- Folder Copies --\nfcc:\t");
1118 for (i
= 0; i
< fccind
; i
++)
1119 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1127 struct mailname
*mp
;
1129 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1130 if (annoaux (mp
) == NOTOK
)
1133 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1134 if (annoaux (mp
) == NOTOK
)
1137 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1138 if (annoaux (mp
) == NOTOK
)
1148 annoaux (struct mailname
*mp
)
1151 char buffer
[BUFSIZ
];
1153 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1154 i
= strlen (buffer
);
1156 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1161 insert_fcc (struct headers
*hdr
, unsigned char *pp
)
1165 for (cp
= pp
; isspace (*cp
); cp
++)
1167 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace (*pp
); pp
--)
1175 adios (NULL
, "too many %ss", hdr
->value
);
1176 fccfold
[fccind
++] = getcpy (cp
);
1184 make_bcc_file (int dashstuff
)
1192 tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
);
1193 if (tfile
== NULL
) adios("bcc", "unable to create temporary file");
1194 chmod (bccfil
, 0600);
1195 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1197 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1199 fprintf (out
, "Message-ID: <%d.%ld@%s>\n",
1200 (int) getpid (), (long) tclock
, LocalName (1));
1201 if (msgflags
& MFRM
) {
1202 /* There was already a From: in the draft. Don't add one. */
1203 if (!draft_from_masquerading
)
1204 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
1205 so we'll reveal the user's actual account@thismachine
1206 address in a Sender: header (and use it as the envelope
1208 fprintf (out
, "Sender: %s\n", from
);
1211 /* Construct a From: header. */
1212 fprintf (out
, "From: %s\n", signature
);
1214 fprintf (out
, "Subject: %s", subject
);
1215 fprintf (out
, "BCC:\n");
1218 * Use MIME encapsulation for Bcc messages
1224 * Check if any lines in the message clash with the
1225 * prefix for the MIME multipart separator. If there
1226 * is a clash, increment one of the letters in the
1227 * prefix and check again.
1229 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1230 adios (NULL
, "lost prefix start");
1231 while (find_prefix () == NOTOK
) {
1236 adios (NULL
, "can't find a unique delimiter string");
1241 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1242 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1243 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1245 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1251 * Do mhl filtering of Bcc messages instead
1252 * of MIME encapsulation.
1254 if (filter
!= NULL
) {
1255 vec
[0] = r1bindex (mhlproc
, '/');
1257 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1261 adios ("fork", "unable to");
1264 dup2 (fileno (out
), 1);
1267 vec
[i
++] = "-forward";
1272 /* was the flag -[no]dashstuffing specified? */
1274 vec
[i
++] = "-dashstuffing";
1275 else if (dashstuff
< 0)
1276 vec
[i
++] = "-nodashstuffing";
1279 execvp (mhlproc
, vec
);
1280 fprintf (stderr
, "unable to exec ");
1285 pidXwait (child_id
, mhlproc
);
1289 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1290 adios (tmpfil
, "unable to re-open");
1293 * If using MIME encapsulation, or if the -nodashstuffing
1294 * flag was given, then just copy message. Else do
1295 * RFC934 quoting (dashstuffing).
1297 if (mime
|| dashstuff
< 0)
1298 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1300 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1304 fseek (out
, 0L, SEEK_END
);
1306 fprintf (out
, "\n--%s--\n", prefix
);
1308 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1314 * Scan message to check if any lines clash with
1315 * the prefix of the MIME multipart separator.
1322 unsigned char buffer
[BUFSIZ
];
1325 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1326 adios (tmpfil
, "unable to re-open");
1328 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1329 if (buffer
[0] == '-' && buffer
[1] == '-') {
1332 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1336 if (strcmp (buffer
+ 2, prefix
) == 0) {
1347 #define plural(x) (x == 1 ? "" : "s")
1352 if (badadr
&& unkadr
)
1353 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1354 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1356 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1358 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1363 do_addresses (int bccque
, int talk
)
1367 struct mailname
*lp
;
1370 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1371 if (lp
->m_bcc
? bccque
: !bccque
) {
1373 printf (" -- Local Recipients --\n");
1374 do_an_address (lp
, talk
);
1379 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1380 if (lp
->m_bcc
? bccque
: !bccque
) {
1382 printf (" -- UUCP Recipients --\n");
1383 do_an_address (lp
, talk
);
1388 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1389 if (lp
->m_bcc
? bccque
: !bccque
) {
1391 printf (" -- Network Recipients --\n");
1392 do_an_address (lp
, talk
);
1399 if (rp_isbad (retval
= sm_waend ()))
1400 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1401 #endif /* SMTPMTS */
1406 * MTS-SPECIFIC INTERACTION
1411 * SENDMAIL/SMTP routines
1417 post (char *file
, int bccque
, int talk
)
1422 onex
= !(msgflags
& MINV
) || bccque
;
1424 if (msgflags
& MINV
)
1425 printf (" -- Posting for %s Recipients --\n",
1426 bccque
? "Blind" : "Sighted");
1428 printf (" -- Posting for All Recipients --\n");
1433 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
, verbose
,
1434 snoop
, onex
, queued
, sasl
, saslmech
,
1436 || rp_isbad (retval
= sm_winit (smtpmode
, from
)))
1437 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1439 do_addresses (bccque
, talk
&& verbose
);
1440 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1441 die (file
, "unable to re-open");
1446 sm_end (onex
? OK
: DONE
);
1450 if (msgflags
& MINV
)
1451 printf (" -- %s Recipient Copies Posted --\n",
1452 bccque
? "Blind" : "Sighted");
1454 printf (" -- Recipient Copies Posted --\n");
1461 /* Address Verification */
1464 verify_all_addresses (int talk
)
1467 struct mailname
*lp
;
1471 if (!whomsw
|| checksw
)
1472 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1473 verbose
, snoop
, 0, queued
, sasl
,
1474 saslmech
, user
, tls
))
1475 || rp_isbad (retval
= sm_winit (smtpmode
, from
)))
1476 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1478 if (talk
&& !whomsw
)
1479 printf (" -- Address Verification --\n");
1480 if (talk
&& localaddrs
.m_next
)
1481 printf (" -- Local Recipients --\n");
1482 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1483 do_an_address (lp
, talk
);
1485 if (talk
&& uuaddrs
.m_next
)
1486 printf (" -- UUCP Recipients --\n");
1487 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1488 do_an_address (lp
, talk
);
1490 if (talk
&& netaddrs
.m_next
)
1491 printf (" -- Network Recipients --\n");
1492 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1493 do_an_address (lp
, talk
);
1496 if (talk
&& !whomsw
)
1497 printf (" -- Address Verification Successful --\n");
1499 if (!whomsw
|| checksw
)
1508 do_an_address (struct mailname
*lp
, int talk
)
1514 switch (lp
->m_type
) {
1518 strncpy (addr
, mbox
, sizeof(addr
));
1522 mbox
= auxformat (lp
, 0);
1524 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1527 default: /* let SendMail decide if the host is bad */
1530 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1535 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1537 if (whomsw
&& !checksw
) {
1545 switch (retval
= sm_wadr (mbox
, host
,
1546 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1549 printf ("address ok\n");
1555 fprintf (stderr
, " %s: ", addr
);
1556 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1557 rp_string (retval
));
1563 fprintf (stderr
, " %s: ", addr
);
1564 die (NULL
, "unexpected response; %s", rp_string (retval
));
1572 do_text (char *file
, int fd
)
1577 lseek (fd
, (off_t
) 0, SEEK_SET
);
1579 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1580 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1581 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1585 die (file
, "problem reading from");
1587 switch (retval
= sm_wtend ()) {
1593 die (NULL
, "posting failed; %s", rp_string (retval
));
1596 die (NULL
, "unexpected response; %s", rp_string (retval
));
1600 #endif /* SMTPMTS */
1613 if (msgflags
& MINV
)
1617 if (!whomsw
|| checksw
)
1619 #endif /* SMTPMTS */
1631 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1632 istat
= SIGNAL2 (SIGINT
, sigser
);
1633 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1634 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1644 SIGNAL (SIGHUP
, hstat
);
1645 SIGNAL (SIGINT
, istat
);
1646 SIGNAL (SIGQUIT
, qstat
);
1647 SIGNAL (SIGTERM
, tstat
);
1655 p_refile (char *file
)
1663 printf (" -- Filing Folder Copies --\n");
1664 for (i
= 0; i
< fccind
; i
++)
1665 fcc (file
, fccfold
[i
]);
1667 printf (" -- Folder Copies Filed --\n");
1672 * Call the `fileproc' to add the file to the folder.
1676 fcc (char *file
, char *folder
)
1683 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1686 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1692 fprintf (stderr
, " %sFcc %s: ",
1693 msgstate
== RESENT
? "Resent-" : "", folder
);
1694 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1698 /* see if we need to add `+' */
1699 snprintf (fold
, sizeof(fold
), "%s%s",
1700 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1702 /* now exec the fileproc */
1703 execlp (fileproc
, r1bindex (fileproc
, '/'),
1704 "-link", "-file", file
, fold
, NULL
);
1708 if ((status
= pidwait (child_id
, OK
))) {
1710 fprintf (stderr
, " %sFcc %s: ",
1711 msgstate
== RESENT
? "Resent-" : "", folder
);
1712 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1715 printf ("folder ok\n");
1727 die (char *what
, char *fmt
, ...)
1732 if (msgflags
& MINV
)
1736 if (!whomsw
|| checksw
)
1738 #endif /* SMTPMTS */
1741 advertise (what
, NULL
, fmt
, ap
);