3 * post.c -- enter messages into the mail transport system
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/signals.h>
15 #include <h/addrsbr.h>
16 #include <h/aliasbr.h>
17 #include <h/dropsbr.h>
28 #ifdef TIME_WITH_SYS_TIME
29 # include <sys/time.h>
32 # ifdef TM_IN_SYS_TIME
33 # include <sys/time.h>
40 # include <mts/smtp/smtp.h>
44 # define SASLminc(a) (a)
45 #else /* CYRUS_SASL */
46 # define SASLminc(a) 0
47 #endif /* CYRUS_SASL */
49 #define FCCS 10 /* max number of fccs allowed */
51 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
53 /* In the following array of structures, the numeric second field of the
54 structures (minchars) is apparently used like this:
56 -# : Switch can be abbreviated to # characters; switch hidden in -help.
57 0 : Switch can't be abbreviated; switch shown in -help.
58 # : Switch can be abbreviated to # characters; switch shown in -help. */
60 static struct swit switches
[] = {
62 { "alias aliasfile", 0 },
64 { "check", -5 }, /* interface from whom */
66 { "nocheck", -7 }, /* interface from whom */
70 { "dist", -4 }, /* interface from dist */
72 { "filter filterfile", 0 },
80 { "library directory", -7 }, /* interface from send, whom */
98 { "whom", -4 }, /* interface from whom */
100 { "width columns", 0 },
105 #define BITSTUFFSW 22
106 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
107 #define NBITSTUFFSW 23
108 { "nodashstuffing", -14 },
110 { "mail", -4 }, /* specify MAIL smtp mode */
112 { "saml", -4 }, /* specify SAML smtp mode */
114 { "send", -4 }, /* specify SEND smtp mode */
116 { "soml", -4 }, /* specify SOML smtp mode */
118 { "idanno number", -6 }, /* interface from send */
120 { "deliver address-list", -7 },
122 { "client host", -6 },
124 { "server host", -6 }, /* specify alternate SMTP server */
126 { "snoop", -5 }, /* snoop the SMTP transaction */
128 { "fill-in file", -7 },
136 { "sasl", SASLminc(-4) },
137 #define SASLMECHSW 38
138 { "saslmech", SASLminc(-5) },
140 { "user", SASLminc(-4) },
142 { "port server port name/number", 4 },
154 * flags for headers->flags
156 #define HNOP 0x0000 /* just used to keep .set around */
157 #define HBAD 0x0001 /* bad header - don't let it through */
158 #define HADR 0x0002 /* header has an address field */
159 #define HSUB 0x0004 /* Subject: header */
160 #define HTRY 0x0008 /* try to send to addrs on header */
161 #define HBCC 0x0010 /* don't output this header */
162 #define HMNG 0x0020 /* munge this header */
163 #define HNGR 0x0040 /* no groups allowed in this header */
164 #define HFCC 0x0080 /* FCC: type header */
165 #define HNIL 0x0100 /* okay for this header not to have addrs */
166 #define HIGN 0x0200 /* ignore this header */
167 #define HDCC 0x0400 /* another undocumented feature */
170 * flags for headers->set
172 #define MFRM 0x0001 /* we've seen a From: */
173 #define MDAT 0x0002 /* we've seen a Date: */
174 #define MRFM 0x0004 /* we've seen a Resent-From: */
175 #define MVIS 0x0008 /* we've seen sighted addrs */
176 #define MINV 0x0010 /* we've seen blind addrs */
179 static struct headers NHeaders
[] = {
180 { "Return-Path", HBAD
, 0 },
181 { "Received", HBAD
, 0 },
182 { "Reply-To", HADR
|HNGR
, 0 },
183 { "From", HADR
|HNGR
, MFRM
},
184 { "Sender", HADR
|HBAD
, 0 },
186 { "Subject", HSUB
, 0 },
187 { "To", HADR
|HTRY
, MVIS
},
188 { "cc", HADR
|HTRY
, MVIS
},
189 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
190 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
191 { "Message-ID", HBAD
, 0 },
196 static struct headers RHeaders
[] = {
197 { "Resent-Reply-To", HADR
|HNGR
, 0 },
198 { "Resent-From", HADR
|HNGR
, MRFM
},
199 { "Resent-Sender", HADR
|HBAD
, 0 },
200 { "Resent-Date", HBAD
, 0 },
201 { "Resent-Subject", HSUB
, 0 },
202 { "Resent-To", HADR
|HTRY
, MVIS
},
203 { "Resent-cc", HADR
|HTRY
, MVIS
},
204 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
205 { "Resent-Message-ID", HBAD
, 0 },
206 { "Resent-Fcc", HFCC
, 0 },
207 { "Reply-To", HADR
, 0 },
208 { "From", HADR
|HNGR
, MFRM
},
209 { "Sender", HADR
|HNGR
, 0 },
210 { "Date", HNOP
, MDAT
},
211 { "To", HADR
|HNIL
, 0 },
212 { "cc", HADR
|HNIL
, 0 },
213 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
218 static short fccind
= 0; /* index into fccfold[] */
219 static short outputlinelen
= OUTPUTLINELEN
;
221 static int pfd
= NOTOK
; /* fd to write annotation list to */
222 static uid_t myuid
= -1; /* my user id */
223 static gid_t mygid
= -1; /* my group id */
224 static int recipients
= 0; /* how many people will get a copy */
225 static int unkadr
= 0; /* how many of those were unknown */
226 static int badadr
= 0; /* number of bad addrs */
227 static int badmsg
= 0; /* message has bad semantics */
228 static int verbose
= 0; /* spell it out */
229 static int format
= 1; /* format addresses */
230 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
231 static int msgid
= 0; /* add msgid */
232 static int debug
= 0; /* debugging post */
233 static int watch
= 0; /* watch the delivery process */
234 static int whomsw
= 0; /* we are whom not post */
235 static int checksw
= 0; /* whom -check */
236 static int linepos
=0; /* putadr()'s position on the line */
237 static int nameoutput
=0; /* putadr() has output header name */
238 static int sasl
=0; /* Use SASL auth for SMTP */
239 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
240 static char *user
=NULL
; /* Authenticate as this user */
241 static char *port
="smtp"; /* Name of server port for SMTP */
243 static unsigned msgflags
= 0; /* what we've seen */
247 static int msgstate
= NORMAL
;
249 static time_t tclock
= 0; /* the time we started (more or less) */
251 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
253 static char tmpfil
[BUFSIZ
];
254 static char bccfil
[BUFSIZ
];
256 static char from
[BUFSIZ
]; /* my network address */
257 static char signature
[BUFSIZ
]; /* my signature */
258 static char *filter
= NULL
; /* the filter for BCC'ing */
259 static char *subject
= NULL
; /* the subject field for BCC'ing */
260 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
262 static struct headers
*hdrtab
; /* table for the message we're doing */
264 static struct mailname localaddrs
={NULL
}; /* local addrs */
265 static struct mailname netaddrs
={NULL
}; /* network addrs */
266 static struct mailname uuaddrs
={NULL
}; /* uucp addrs */
267 static struct mailname tmpaddrs
={NULL
}; /* temporary queue */
270 static int snoop
= 0;
271 static int smtpmode
= S_MAIL
;
272 static char *clientsw
= NULL
;
273 static char *serversw
= NULL
;
275 extern struct smtp sm_reply
;
278 static char prefix
[] = "----- =_aaaaaaaaaa";
280 static int fill_up
= 0;
281 static char *fill_in
= NULL
;
282 static char *partno
= NULL
;
283 static int queued
= 0;
285 extern boolean draft_from_masquerading
; /* defined in mts.c */
290 static void putfmt (char *, char *, FILE *);
291 static void start_headers (void);
292 static void finish_headers (FILE *);
293 static int get_header (char *, struct headers
*);
294 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
295 static void putgrp (char *, char *, FILE *, unsigned int);
296 static int insert (struct mailname
*);
297 static void pl (void);
298 static void anno (void);
299 static int annoaux (struct mailname
*);
300 static void insert_fcc (struct headers
*, unsigned char *);
301 static void make_bcc_file (int);
302 static void verify_all_addresses (int);
303 static void chkadr (void);
304 static void sigon (void);
305 static void sigoff (void);
306 static void p_refile (char *);
307 static void fcc (char *, char *);
308 static void die (char *, char *, ...);
309 static void post (char *, int, int);
310 static void do_text (char *file
, int fd
);
311 static void do_an_address (struct mailname
*, int);
312 static void do_addresses (int, int);
313 static int find_prefix (void);
317 main (int argc
, char **argv
)
319 int state
, compnum
, dashstuff
= 0;
320 char *cp
, *msg
= NULL
, **argp
, **arguments
;
321 char buf
[BUFSIZ
], name
[NAMESZ
];
325 setlocale(LC_ALL
, "");
327 invo_name
= r1bindex (argv
[0], '/');
329 /* foil search of user profile/context */
330 if (context_foil (NULL
) == -1)
333 mts_init (invo_name
);
334 arguments
= getarguments (invo_name
, argc
, argv
, 0);
337 while ((cp
= *argp
++)) {
339 switch (smatch (++cp
, switches
)) {
341 ambigsw (cp
, switches
);
344 adios (NULL
, "-%s unknown", cp
);
347 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
348 print_help (buf
, switches
, 0);
351 print_version(invo_name
);
355 if (!(cp
= *argp
++) || *cp
== '-')
356 adios (NULL
, "missing argument to %s", argp
[-2]);
357 /* create a minimal context */
358 if (context_foil (cp
) == -1)
363 if (!(cp
= *argp
++) || *cp
== '-')
364 adios (NULL
, "missing argument to %s", argp
[-2]);
365 if ((state
= alias (cp
)) != AK_OK
)
366 adios (NULL
, "aliasing error in %s - %s",
367 cp
, akerror (state
));
386 if (!(filter
= *argp
++) || *filter
== '-')
387 adios (NULL
, "missing argument to %s", argp
[-2]);
442 if (!(cp
= *argp
++) || *cp
== '-')
443 adios (NULL
, "missing argument to %s", argp
[-2]);
444 if ((outputlinelen
= atoi (cp
)) < 10)
445 adios (NULL
, "impossible width %d", outputlinelen
);
449 if (!(cp
= *argp
++) || *cp
== '-')
450 adios (NULL
, "missing argument to %s", argp
[-2]);
451 if ((pfd
= atoi (cp
)) <= 2)
452 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
456 if (!(cp
= *argp
++) || *cp
== '-')
457 adios (NULL
, "missing argument to %s", argp
[-2]);
463 if (!(cp
= *argp
++) || *cp
== '-')
464 adios (NULL
, "missing argument to %s", argp
[-2]);
483 if (!(clientsw
= *argp
++) || *clientsw
== '-')
484 adios (NULL
, "missing argument to %s", argp
[-2]);
487 if (!(serversw
= *argp
++) || *serversw
== '-')
488 adios (NULL
, "missing argument to %s", argp
[-2]);
496 if (!(fill_in
= *argp
++) || *fill_in
== '-')
497 adios (NULL
, "missing argument to %s", argp
[-2]);
503 if (!(partno
= *argp
++) || *partno
== '-')
504 adios (NULL
, "missing argument to %s", argp
[-2]);
516 if (!(saslmech
= *argp
++) || *saslmech
== '-')
517 adios (NULL
, "missing argument to %s", argp
[-2]);
521 if (!(user
= *argp
++) || *user
== '-')
522 adios (NULL
, "missing argument to %s", argp
[-2]);
526 if (!(port
= *argp
++) || *port
== '-')
527 adios (NULL
, "missing argument to %s", argp
[-2]);
532 adios (NULL
, "only one message at a time!");
540 adios (NULL
, "usage: %s [switches] file", invo_name
);
542 if (outputlinelen
< 10)
543 adios (NULL
, "impossible width %d", outputlinelen
);
545 if ((in
= fopen (msg
, "r")) == NULL
)
546 adios (msg
, "unable to open");
551 discard (out
= stdout
); /* XXX: reference discard() to help loader */
554 if ((out
= fopen (fill_in
? fill_in
: "/dev/null", "w")) == NULL
)
555 adios ("/dev/null", "unable to open");
557 strncpy (tmpfil
, m_scratch ("", m_maildir (invo_name
)),
559 if ((out
= fopen (tmpfil
, "w")) == NULL
) {
560 strncpy (tmpfil
, m_tmpfil (invo_name
), sizeof(tmpfil
));
561 if ((out
= fopen (tmpfil
, "w")) == NULL
)
562 adios (tmpfil
, "unable to create");
564 chmod (tmpfil
, 0600);
568 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
570 for (compnum
= 1, state
= FLD
;;) {
571 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
)) {
576 cp
= add (buf
, NULL
);
577 while (state
== FLDPLUS
) {
578 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
581 putfmt (name
, cp
, out
);
585 finish_headers (out
);
590 finish_headers (out
);
591 if (whomsw
&& !fill_in
)
593 fprintf (out
, "\n%s", buf
);
594 while (state
== BODY
) {
595 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
601 finish_headers (out
);
606 adios (NULL
, "message format error in component #%d", compnum
);
609 adios (NULL
, "getfld() returned %d", state
);
625 /* If we are doing a "whom" check */
628 verify_all_addresses (1);
632 if (msgflags
& MINV
) {
633 make_bcc_file (dashstuff
);
634 if (msgflags
& MVIS
) {
635 verify_all_addresses (verbose
);
636 post (tmpfil
, 0, verbose
);
638 post (bccfil
, 1, verbose
);
641 post (tmpfil
, 0, isatty (1));
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
, *np
;
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 fprintf (out
, "%s: %s", name
, str
);
683 if (hdr
->flags
& HIGN
) {
685 fprintf (out
, "%s: %s", name
, str
);
688 if (hdr
->flags
& HBAD
) {
690 fprintf (out
, "%s: %s", name
, str
);
692 advise (NULL
, "illegal header line -- %s:", name
);
697 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
699 if (hdr
->flags
& HSUB
)
700 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
701 if (hdr
->flags
& HFCC
) {
703 fprintf (out
, "%s: %s", name
, str
);
707 if ((cp
= strrchr(str
, '\n')))
709 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
711 insert_fcc (hdr
, pp
);
713 insert_fcc (hdr
, pp
);
717 if (!(hdr
->flags
& HADR
)) {
718 fprintf (out
, "%s: %s", name
, str
);
722 tmpaddrs
.m_next
= NULL
;
723 for (count
= 0; (cp
= getname (str
)); count
++)
724 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
728 tmpaddrs
.m_next
= mp
;
732 if (hdr
->flags
& HTRY
)
738 if (hdr
->flags
& HNIL
)
739 fprintf (out
, "%s: %s", name
, str
);
742 advise (NULL
, "%s: field requires at least one address", name
);
749 nameoutput
= linepos
= 0;
750 snprintf (namep
, sizeof(namep
), "%s%s",
751 !fill_in
&& (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
753 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
754 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
755 /* The address doesn't include a host, so it might be an alias. */
756 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
757 qp
= akvisible () ? mp
->m_mbox
: "";
760 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
761 while ((cp
= getname (pp
))) {
762 if (!(mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
767 if (draft_from_masquerading
&& ((msgstate
== RESENT
)
769 : (hdr
->set
& MFRM
)))
770 /* The user manually specified a [Resent-]From: address in
771 their draft and the "masquerade:" line in mts.conf
772 doesn't contain "draft_from", so we'll set things up to
773 use the actual email address embedded in the draft
774 [Resent-]From: (after alias substitution, and without the
775 GECOS full name or angle brackets) as the envelope
777 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
779 if (hdr
->flags
& HBCC
)
782 mp
->m_ingrp
= np
->m_ingrp
;
785 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
788 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
789 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
798 /* Address includes a host, so no alias substitution is needed. */
799 if (draft_from_masquerading
&& ((msgstate
== RESENT
)
801 : (hdr
->set
& MFRM
)))
802 /* The user manually specified a [Resent-]From: address in
803 their draft and the "masquerade:" line in mts.conf
804 doesn't contain "draft_from", so we'll set things up to
805 use the actual email address embedded in the draft
806 [Resent-]From: (after alias substitution, and without the
807 GECOS full name or angle brackets) as the envelope
809 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
811 if (hdr
->flags
& HBCC
)
814 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
817 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
821 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
827 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
828 advise (NULL
, "%s: field does not allow groups", name
);
832 if (fill_in
&& grp
> 0)
843 char myhost
[BUFSIZ
], sigbuf
[BUFSIZ
];
850 strncpy (from
, adrsprintf (NULL
, NULL
), sizeof(from
));
851 strncpy (myhost
, LocalName (), sizeof(myhost
));
853 for (cp
= myhost
; *cp
; cp
++)
856 if ((cp
= getfullname ()) && *cp
) {
857 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
858 snprintf (signature
, sizeof(signature
), "%s <%s>",
859 sigbuf
, adrsprintf (NULL
, NULL
));
860 if ((cp
= getname (signature
)) == NULL
)
861 adios (NULL
, "getname () failed -- you lose extraordinarily big");
862 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
)) == NULL
)
863 adios (NULL
, "bad signature '%s'", sigbuf
);
868 strncpy (signature
, adrsprintf (NULL
, NULL
), sizeof(signature
));
874 * Now that we've outputted the header fields in the draft
875 * message, we will now output any remaining header fields
876 * that we need to add/create.
880 finish_headers (FILE *out
)
884 if (whomsw
&& !fill_up
)
887 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
889 fprintf (out
, "Message-ID: <%d.%ld@%s>\n",
890 (int) getpid (), (long) tclock
, LocalName ());
891 if (msgflags
& MFRM
) {
892 /* There was already a From: in the draft. Don't add one. */
893 if (!draft_from_masquerading
)
894 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
895 so we'll reveal the user's actual account@thismachine
896 address in a Sender: header (and use it as the envelope
898 fprintf (out
, "Sender: %s\n", from
);
901 /* Construct a From: header. */
902 fprintf (out
, "From: %s\n", signature
);
906 if (!(msgflags
& MVIS
))
907 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
911 if (!(msgflags
& MDAT
)) {
912 advise (NULL
, "message has no Date: header");
915 if (!(msgflags
& MFRM
)) {
916 advise (NULL
, "message has no From: header");
919 if (whomsw
&& !fill_up
)
922 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
924 fprintf (out
, "Resent-Message-ID: <%d.%ld@%s>\n",
925 (int) getpid (), (long) tclock
, LocalName ());
926 if (msgflags
& MRFM
) {
927 /* There was already a Resent-From: in draft. Don't add one. */
928 if (!draft_from_masquerading
)
929 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
930 so we'll reveal the user's actual account@thismachine
931 address in a Sender: header (and use it as the envelope
933 fprintf (out
, "Resent-Sender: %s\n", from
);
936 /* Construct a Resent-From: header. */
937 fprintf (out
, "Resent-From: %s\n", signature
);
940 if (!(msgflags
& MVIS
))
941 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
946 adios (NULL
, "re-format message and try again");
948 adios (NULL
, "no addressees");
953 get_header (char *header
, struct headers
*table
)
957 for (h
= table
; h
->value
; h
++)
958 if (!mh_strcasecmp (header
, h
->value
))
966 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
972 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
974 if ((!fill_in
&& (flags
& (HBCC
| HDCC
))) || mp
->m_ingrp
)
978 fprintf (out
, "%s: ", name
);
979 linepos
+= (nameoutput
= strlen (name
) + 2);
982 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
983 mp
->m_pers
= getcpy (aka
);
985 if (mp
->m_gname
&& !fill_in
) {
986 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
996 if (linepos
!= nameoutput
) {
997 if (len
+ linepos
+ 2 > outputlinelen
)
998 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1008 return (flags
& HTRY
);
1013 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1018 if (!fill_in
&& (flags
& HBCC
))
1022 fprintf (out
, "%s: ", name
);
1023 linepos
+= (nameoutput
= strlen (name
) + 2);
1025 linepos
-= strlen (group
);
1028 cp
= fill_in
? group
: concat (group
, ";", NULL
);
1031 if (linepos
> nameoutput
) {
1032 if (len
+ linepos
+ 2 > outputlinelen
) {
1033 fprintf (out
, ",\n%*s", nameoutput
, "");
1034 linepos
= nameoutput
;
1048 insert (struct mailname
*np
)
1050 struct mailname
*mp
;
1052 if (np
->m_mbox
== NULL
)
1055 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1056 : np
->m_type
== UUCPHOST
? &uuaddrs
1060 if (!mh_strcasecmp (np
->m_host
, mp
->m_next
->m_host
)
1061 && !mh_strcasecmp (np
->m_mbox
, mp
->m_next
->m_mbox
)
1062 && np
->m_bcc
== mp
->m_next
->m_bcc
)
1075 struct mailname
*mp
;
1077 printf ("-------\n\t-- Addresses --\nlocal:\t");
1078 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1079 printf ("%s%s%s", mp
->m_mbox
,
1080 mp
->m_bcc
? "[BCC]" : "",
1081 mp
->m_next
? ",\n\t" : "");
1083 printf ("\nnet:\t");
1084 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1085 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1086 mp
->m_mbox
, mp
->m_host
,
1087 mp
->m_bcc
? "[BCC]" : "",
1088 mp
->m_next
? ",\n\t" : "");
1090 printf ("\nuucp:\t");
1091 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1092 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1093 mp
->m_bcc
? "[BCC]" : "",
1094 mp
->m_next
? ",\n\t" : "");
1096 printf ("\n\t-- Folder Copies --\nfcc:\t");
1097 for (i
= 0; i
< fccind
; i
++)
1098 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1106 struct mailname
*mp
;
1108 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1109 if (annoaux (mp
) == NOTOK
)
1112 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1113 if (annoaux (mp
) == NOTOK
)
1116 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1117 if (annoaux (mp
) == NOTOK
)
1127 annoaux (struct mailname
*mp
)
1130 char buffer
[BUFSIZ
];
1132 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1133 i
= strlen (buffer
);
1135 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1140 insert_fcc (struct headers
*hdr
, unsigned char *pp
)
1144 for (cp
= pp
; isspace (*cp
); cp
++)
1146 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace (*pp
); pp
--)
1154 adios (NULL
, "too many %ss", hdr
->value
);
1155 fccfold
[fccind
++] = getcpy (cp
);
1163 make_bcc_file (int dashstuff
)
1170 strncpy (bccfil
, m_tmpfil ("bccs"), sizeof(bccfil
));
1171 if ((out
= fopen (bccfil
, "w")) == NULL
)
1172 adios (bccfil
, "unable to create");
1173 chmod (bccfil
, 0600);
1175 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1177 fprintf (out
, "Message-ID: <%d.%ld@%s>\n",
1178 (int) getpid (), (long) tclock
, LocalName ());
1179 if (msgflags
& MFRM
) {
1180 /* There was already a From: in the draft. Don't add one. */
1181 if (!draft_from_masquerading
)
1182 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
1183 so we'll reveal the user's actual account@thismachine
1184 address in a Sender: header (and use it as the envelope
1186 fprintf (out
, "Sender: %s\n", from
);
1189 /* Construct a From: header. */
1190 fprintf (out
, "From: %s\n", signature
);
1192 fprintf (out
, "Subject: %s", subject
);
1193 fprintf (out
, "BCC:\n");
1196 * Use MIME encapsulation for Bcc messages
1202 * Check if any lines in the message clash with the
1203 * prefix for the MIME multipart separator. If there
1204 * is a clash, increment one of the letters in the
1205 * prefix and check again.
1207 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1208 adios (NULL
, "lost prefix start");
1209 while (find_prefix () == NOTOK
) {
1214 adios (NULL
, "can't find a unique delimiter string");
1219 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1220 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1221 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1223 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1229 * Do mhl filtering of Bcc messages instead
1230 * of MIME encapsulation.
1232 if (filter
!= NULL
) {
1233 vec
[0] = r1bindex (mhlproc
, '/');
1235 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1239 adios ("fork", "unable to");
1242 dup2 (fileno (out
), 1);
1245 vec
[i
++] = "-forward";
1250 /* was the flag -[no]dashstuffing specified? */
1252 vec
[i
++] = "-dashstuffing";
1253 else if (dashstuff
< 0)
1254 vec
[i
++] = "-nodashstuffing";
1257 execvp (mhlproc
, vec
);
1258 fprintf (stderr
, "unable to exec ");
1263 pidXwait (child_id
, mhlproc
);
1267 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1268 adios (tmpfil
, "unable to re-open");
1271 * If using MIME encapsulation, or if the -nodashstuffing
1272 * flag was given, then just copy message. Else do
1273 * RFC934 quoting (dashstuffing).
1275 if (mime
|| dashstuff
< 0)
1276 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1278 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1282 fseek (out
, 0L, SEEK_END
);
1284 fprintf (out
, "\n--%s--\n", prefix
);
1286 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1292 * Scan message to check if any lines clash with
1293 * the prefix of the MIME multipart separator.
1300 unsigned char buffer
[BUFSIZ
];
1303 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1304 adios (tmpfil
, "unable to re-open");
1306 len
= strlen (prefix
);
1309 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1310 if (buffer
[0] == '-' && buffer
[1] == '-') {
1313 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1317 if (strcmp (buffer
+ 2, prefix
) == 0) {
1328 #define plural(x) (x == 1 ? "" : "s")
1333 if (badadr
&& unkadr
)
1334 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1335 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1337 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1339 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1344 do_addresses (int bccque
, int talk
)
1348 struct mailname
*lp
;
1351 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1352 if (lp
->m_bcc
? bccque
: !bccque
) {
1354 printf (" -- Local Recipients --\n");
1355 do_an_address (lp
, talk
);
1360 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1361 if (lp
->m_bcc
? bccque
: !bccque
) {
1363 printf (" -- UUCP Recipients --\n");
1364 do_an_address (lp
, talk
);
1369 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1370 if (lp
->m_bcc
? bccque
: !bccque
) {
1372 printf (" -- Network Recipients --\n");
1373 do_an_address (lp
, talk
);
1380 if (rp_isbad (retval
= sm_waend ()))
1381 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1382 #endif /* SMTPMTS */
1387 * MTS-SPECIFIC INTERACTION
1392 * SENDMAIL/SMTP routines
1398 post (char *file
, int bccque
, int talk
)
1403 onex
= !(msgflags
& MINV
) || bccque
;
1405 if (msgflags
& MINV
)
1406 printf (" -- Posting for %s Recipients --\n",
1407 bccque
? "Blind" : "Sighted");
1409 printf (" -- Posting for All Recipients --\n");
1414 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
, verbose
,
1415 snoop
, onex
, queued
, sasl
, saslmech
,
1417 || rp_isbad (retval
= sm_winit (smtpmode
, from
)))
1418 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1420 do_addresses (bccque
, talk
&& verbose
);
1421 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1422 die (file
, "unable to re-open");
1427 sm_end (onex
? OK
: DONE
);
1431 if (msgflags
& MINV
)
1432 printf (" -- %s Recipient Copies Posted --\n",
1433 bccque
? "Blind" : "Sighted");
1435 printf (" -- Recipient Copies Posted --\n");
1442 /* Address Verification */
1445 verify_all_addresses (int talk
)
1448 struct mailname
*lp
;
1452 if (!whomsw
|| checksw
)
1453 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1454 verbose
, snoop
, 0, queued
, sasl
,
1456 || rp_isbad (retval
= sm_winit (smtpmode
, from
)))
1457 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1459 if (talk
&& !whomsw
)
1460 printf (" -- Address Verification --\n");
1461 if (talk
&& localaddrs
.m_next
)
1462 printf (" -- Local Recipients --\n");
1463 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1464 do_an_address (lp
, talk
);
1466 if (talk
&& uuaddrs
.m_next
)
1467 printf (" -- UUCP Recipients --\n");
1468 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1469 do_an_address (lp
, talk
);
1471 if (talk
&& netaddrs
.m_next
)
1472 printf (" -- Network Recipients --\n");
1473 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1474 do_an_address (lp
, talk
);
1477 if (talk
&& !whomsw
)
1478 printf (" -- Address Verification Successful --\n");
1480 if (!whomsw
|| checksw
)
1489 do_an_address (struct mailname
*lp
, int talk
)
1495 switch (lp
->m_type
) {
1499 strncpy (addr
, mbox
, sizeof(addr
));
1503 mbox
= auxformat (lp
, 0);
1505 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1508 default: /* let SendMail decide if the host is bad */
1511 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1516 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1518 if (whomsw
&& !checksw
) {
1526 switch (retval
= sm_wadr (mbox
, host
,
1527 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1530 printf ("address ok\n");
1536 fprintf (stderr
, " %s: ", addr
);
1537 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1538 rp_string (retval
));
1544 fprintf (stderr
, " %s: ", addr
);
1545 die (NULL
, "unexpected response; %s", rp_string (retval
));
1553 do_text (char *file
, int fd
)
1558 lseek (fd
, (off_t
) 0, SEEK_SET
);
1560 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1561 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1562 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1566 die (file
, "problem reading from");
1568 switch (retval
= sm_wtend ()) {
1574 die (NULL
, "posting failed; %s", rp_string (retval
));
1577 die (NULL
, "unexpected response; %s", rp_string (retval
));
1581 #endif /* SMTPMTS */
1591 #ifndef RELIABLE_SIGNALS
1592 SIGNAL (i
, SIG_IGN
);
1596 if (msgflags
& MINV
)
1600 if (!whomsw
|| checksw
)
1602 #endif /* SMTPMTS */
1614 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1615 istat
= SIGNAL2 (SIGINT
, sigser
);
1616 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1617 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1627 SIGNAL (SIGHUP
, hstat
);
1628 SIGNAL (SIGINT
, istat
);
1629 SIGNAL (SIGQUIT
, qstat
);
1630 SIGNAL (SIGTERM
, tstat
);
1638 p_refile (char *file
)
1646 printf (" -- Filing Folder Copies --\n");
1647 for (i
= 0; i
< fccind
; i
++)
1648 fcc (file
, fccfold
[i
]);
1650 printf (" -- Folder Copies Filed --\n");
1655 * Call the `fileproc' to add the file to the folder.
1659 fcc (char *file
, char *folder
)
1666 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1669 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1675 fprintf (stderr
, " %sFcc %s: ",
1676 msgstate
== RESENT
? "Resent-" : "", folder
);
1677 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1681 /* see if we need to add `+' */
1682 snprintf (fold
, sizeof(fold
), "%s%s",
1683 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1685 /* now exec the fileproc */
1686 execlp (fileproc
, r1bindex (fileproc
, '/'),
1687 "-link", "-file", file
, fold
, NULL
);
1691 if ((status
= pidwait (child_id
, OK
))) {
1693 fprintf (stderr
, " %sFcc %s: ",
1694 msgstate
== RESENT
? "Resent-" : "", folder
);
1695 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1698 printf ("folder ok\n");
1710 die (char *what
, char *fmt
, ...)
1715 if (msgflags
& MINV
)
1719 if (!whomsw
|| checksw
)
1721 #endif /* SMTPMTS */
1724 advertise (what
, NULL
, fmt
, ap
);