]>
diplodocus.org Git - nmh/blob - uip/post.c
3 * post.c -- enter messages into the mail transport system
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
13 #include <h/addrsbr.h>
14 #include <h/aliasbr.h>
15 #include <h/dropsbr.h>
25 #ifdef HAVE_SYS_TIME_H
26 # include <sys/time.h>
30 #include <mts/smtp/smtp.h>
33 # define SASLminc(a) (a)
34 #else /* CYRUS_SASL */
35 # define SASLminc(a) 0
36 #endif /* CYRUS_SASL */
39 # define TLSminc(a) (a)
40 #else /* TLS_SUPPORT */
42 #endif /* TLS_SUPPORT */
44 #define FCCS 10 /* max number of fccs allowed */
46 /* In the following array of structures, the numeric second field of the
47 structures (minchars) is apparently used like this:
49 -# : Switch can be abbreviated to # characters; switch hidden in -help.
50 0 : Switch can't be abbreviated; switch shown in -help.
51 # : Switch can be abbreviated to # characters; switch shown in -help. */
53 static struct swit switches
[] = {
55 { "alias aliasfile", 0 },
57 { "check", -5 }, /* interface from whom */
59 { "nocheck", -7 }, /* interface from whom */
63 { "dist", -4 }, /* interface from dist */
65 { "filter filterfile", 0 },
73 { "library directory", -7 }, /* interface from send, whom */
91 { "whom", -4 }, /* interface from whom */
93 { "width columns", 0 },
99 { "dashstuffing", -12 }, /* should we dashstuff BCC messages? */
100 #define NBITSTUFFSW 23
101 { "nodashstuffing", -14 },
103 { "idanno number", -6 }, /* interface from send */
105 { "client host", -6 },
107 { "server host", 6 }, /* specify alternate SMTP server */
109 { "snoop", -5 }, /* snoop the SMTP transaction */
115 { "sasl", SASLminc(-4) },
117 { "nosasl", SASLminc(-6) },
118 #define SASLMXSSFSW 32
119 { "saslmaxssf", SASLminc(-10) },
120 #define SASLMECHSW 33
121 { "saslmech", SASLminc(-5) },
123 { "user", SASLminc(-4) },
125 { "port server port name/number", 4 },
127 { "tls", TLSminc(-3) },
129 { "notls", TLSminc(-5) },
130 #define FILEPROCSW 38
145 * flags for headers->flags
147 #define HNOP 0x0000 /* just used to keep .set around */
148 #define HBAD 0x0001 /* bad header - don't let it through */
149 #define HADR 0x0002 /* header has an address field */
150 #define HSUB 0x0004 /* Subject: header */
151 #define HTRY 0x0008 /* try to send to addrs on header */
152 #define HBCC 0x0010 /* don't output this header */
153 #define HMNG 0x0020 /* munge this header */
154 #define HNGR 0x0040 /* no groups allowed in this header */
155 #define HFCC 0x0080 /* FCC: type header */
156 #define HNIL 0x0100 /* okay for this header not to have addrs */
157 #define HIGN 0x0200 /* ignore this header */
158 #define HDCC 0x0400 /* another undocumented feature */
159 #define HONE 0x0800 /* Only (zero or) one address allowed */
160 #define HEFM 0x1000 /* Envelope-From: header */
163 * flags for headers->set
165 #define MFRM 0x0001 /* we've seen a From: */
166 #define MDAT 0x0002 /* we've seen a Date: */
167 #define MRFM 0x0004 /* we've seen a Resent-From: */
168 #define MVIS 0x0008 /* we've seen sighted addrs */
169 #define MINV 0x0010 /* we've seen blind addrs */
170 #define MSND 0x0020 /* we've seen a Sender: */
171 #define MRSN 0x0040 /* We've seen a Resent-Sendr:*/
172 #define MEFM 0x0080 /* We've seen Envelope-From: */
175 static struct headers NHeaders
[] = {
176 { "Return-Path", HBAD
, 0 },
177 { "Received", HBAD
, 0 },
178 { "Reply-To", HADR
|HNGR
, 0 },
179 { "From", HADR
|HNGR
, MFRM
},
180 { "Sender", HADR
|HNGR
|HONE
, MSND
},
182 { "Subject", HSUB
, 0 },
183 { "To", HADR
|HTRY
, MVIS
},
184 { "cc", HADR
|HTRY
, MVIS
},
185 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
186 { "Dcc", HADR
|HTRY
|HDCC
|HNIL
, MVIS
}, /* sorta cc & bcc combined */
187 { "Message-ID", HBAD
, 0 },
189 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
193 static struct headers RHeaders
[] = {
194 { "Resent-Reply-To", HADR
|HNGR
, 0 },
195 { "Resent-From", HADR
|HNGR
, MRFM
},
196 { "Resent-Sender", HADR
|HNGR
, MRSN
},
197 { "Resent-Date", HBAD
, 0 },
198 { "Resent-Subject", HSUB
, 0 },
199 { "Resent-To", HADR
|HTRY
, MVIS
},
200 { "Resent-cc", HADR
|HTRY
, MVIS
},
201 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
202 { "Resent-Message-ID", HBAD
, 0 },
203 { "Resent-Fcc", HFCC
, 0 },
204 { "Reply-To", HADR
, 0 },
205 { "From", HADR
|HNGR
, MFRM
},
206 { "Sender", HADR
|HNGR
, MSND
},
207 { "Date", HNOP
, MDAT
},
208 { "To", HADR
|HNIL
, 0 },
209 { "cc", HADR
|HNIL
, 0 },
210 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, 0 },
212 { "Envelope-From", HADR
|HONE
|HEFM
, MEFM
},
216 static short fccind
= 0; /* index into fccfold[] */
217 static short outputlinelen
= OUTPUTLINELEN
;
219 static int pfd
= NOTOK
; /* fd to write annotation list to */
220 static uid_t myuid
= -1; /* my user id */
221 static gid_t mygid
= -1; /* my group id */
222 static int recipients
= 0; /* how many people will get a copy */
223 static int unkadr
= 0; /* how many of those were unknown */
224 static int badadr
= 0; /* number of bad addrs */
225 static int badmsg
= 0; /* message has bad semantics */
226 static int verbose
= 0; /* spell it out */
227 static int format
= 1; /* format addresses */
228 static int mime
= 0; /* use MIME-style encapsulations for Bcc */
229 static int msgid
= 0; /* add msgid */
230 static int debug
= 0; /* debugging post */
231 static int watch
= 0; /* watch the delivery process */
232 static int whomsw
= 0; /* we are whom not post */
233 static int checksw
= 0; /* whom -check */
234 static int linepos
=0; /* putadr()'s position on the line */
235 static int nameoutput
=0; /* putadr() has output header name */
236 static int sasl
=0; /* Use SASL auth for SMTP */
237 static int saslssf
=-1; /* Our maximum SSF for SASL */
238 static char *saslmech
=NULL
; /* Force use of particular SASL mech */
239 static char *user
=NULL
; /* Authenticate as this user */
240 static char *port
="smtp"; /* Name of server port for SMTP */
241 static int tls
=0; /* Use TLS for encryption */
242 static int fromcount
=0; /* Count of addresses on From: header */
243 static int seensender
=0; /* Have we seen a Sender: header? */
245 static unsigned msgflags
= 0; /* what we've seen */
249 static int msgstate
= NORMAL
;
251 static time_t tclock
= 0; /* the time we started (more or less) */
253 static SIGNAL_HANDLER hstat
, istat
, qstat
, tstat
;
255 static char tmpfil
[BUFSIZ
];
256 static char bccfil
[BUFSIZ
];
258 static char from
[BUFSIZ
]; /* my network address */
259 static char sender
[BUFSIZ
]; /* my Sender: header */
260 static char efrom
[BUFSIZ
]; /* my Envelope-From: header */
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 */
273 static int snoop
= 0;
274 static char *clientsw
= NULL
;
275 static char *serversw
= NULL
;
277 extern struct smtp sm_reply
;
279 static char prefix
[] = "----- =_aaaaaaaaaa";
281 static char *partno
= NULL
;
282 static int queued
= 0;
287 static void putfmt (char *, char *, FILE *);
288 static void start_headers (void);
289 static void finish_headers (FILE *);
290 static int get_header (char *, struct headers
*);
291 static int putadr (char *, char *, struct mailname
*, FILE *, unsigned int);
292 static void putgrp (char *, char *, FILE *, unsigned int);
293 static int insert (struct mailname
*);
294 static void pl (void);
295 static void anno (void);
296 static int annoaux (struct mailname
*);
297 static void insert_fcc (struct headers
*, unsigned char *);
298 static void make_bcc_file (int);
299 static void verify_all_addresses (int, char *);
300 static void chkadr (void);
301 static void sigon (void);
302 static void sigoff (void);
303 static void p_refile (char *);
304 static void fcc (char *, char *);
305 static void die (char *, char *, ...);
306 static void post (char *, int, int, char *);
307 static void do_text (char *file
, int fd
);
308 static void do_an_address (struct mailname
*, int);
309 static void do_addresses (int, int);
310 static int find_prefix (void);
314 main (int argc
, char **argv
)
316 int state
, compnum
, dashstuff
= 0;
317 char *cp
, *msg
= NULL
, **argp
, **arguments
, *envelope
;
318 char buf
[BUFSIZ
], name
[NAMESZ
];
322 setlocale(LC_ALL
, "");
324 invo_name
= r1bindex (argv
[0], '/');
326 /* foil search of user profile/context */
327 if (context_foil (NULL
) == -1)
330 mts_init (invo_name
);
331 arguments
= getarguments (invo_name
, argc
, argv
, 0);
334 while ((cp
= *argp
++)) {
336 switch (smatch (++cp
, switches
)) {
338 ambigsw (cp
, switches
);
341 adios (NULL
, "-%s unknown", cp
);
344 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
345 print_help (buf
, switches
, 0);
348 print_version(invo_name
);
352 if (!(cp
= *argp
++) || *cp
== '-')
353 adios (NULL
, "missing argument to %s", argp
[-2]);
354 /* create a minimal context */
355 if (context_foil (cp
) == -1)
360 if (!(cp
= *argp
++) || *cp
== '-')
361 adios (NULL
, "missing argument to %s", argp
[-2]);
362 if ((state
= alias (cp
)) != AK_OK
)
363 adios (NULL
, "aliasing error in %s - %s",
364 cp
, akerror (state
));
383 if (!(filter
= *argp
++) || *filter
== '-')
384 adios (NULL
, "missing argument to %s", argp
[-2]);
439 if (!(cp
= *argp
++) || *cp
== '-')
440 adios (NULL
, "missing argument to %s", argp
[-2]);
441 if ((outputlinelen
= atoi (cp
)) < 10)
442 adios (NULL
, "impossible width %d", outputlinelen
);
446 if (!(cp
= *argp
++) || *cp
== '-')
447 adios (NULL
, "missing argument to %s", argp
[-2]);
448 if ((pfd
= atoi (cp
)) <= 2)
449 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
453 if (!(clientsw
= *argp
++) || *clientsw
== '-')
454 adios (NULL
, "missing argument to %s", argp
[-2]);
457 if (!(serversw
= *argp
++) || *serversw
== '-')
458 adios (NULL
, "missing argument to %s", argp
[-2]);
465 if (!(partno
= *argp
++) || *partno
== '-')
466 adios (NULL
, "missing argument to %s", argp
[-2]);
482 if (!(cp
= *argp
++) || *cp
== '-')
483 adios (NULL
, "missing argument to %s", argp
[-2]);
488 if (!(saslmech
= *argp
++) || *saslmech
== '-')
489 adios (NULL
, "missing argument to %s", argp
[-2]);
493 if (!(user
= *argp
++) || *user
== '-')
494 adios (NULL
, "missing argument to %s", argp
[-2]);
498 if (!(port
= *argp
++) || *port
== '-')
499 adios (NULL
, "missing argument to %s", argp
[-2]);
511 if (!(cp
= *argp
++) || *cp
== '-')
512 adios (NULL
, "missing argument to %s", argp
[-2]);
517 if (!(cp
= *argp
++) || *cp
== '-')
518 adios (NULL
, "missing argument to %s", argp
[-2]);
524 adios (NULL
, "only one message at a time!");
532 adios (NULL
, "usage: %s [switches] file", invo_name
);
534 if (outputlinelen
< 10)
535 adios (NULL
, "impossible width %d", outputlinelen
);
537 if ((in
= fopen (msg
, "r")) == NULL
)
538 adios (msg
, "unable to open");
543 discard (out
= stdout
); /* XXX: reference discard() to help loader */
546 if ((out
= fopen ("/dev/null", "w")) == NULL
)
547 adios ("/dev/null", "unable to open");
549 char *cp
= m_mktemp(m_maildir(invo_name
), NULL
, &out
);
551 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
553 adios ("post", "unable to create temporary file");
556 strncpy(tmpfil
, cp
, sizeof(tmpfil
));
557 chmod (tmpfil
, 0600);
561 hdrtab
= msgstate
== NORMAL
? NHeaders
: RHeaders
;
563 for (compnum
= 1, state
= FLD
;;) {
564 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
)) {
569 cp
= add (buf
, NULL
);
570 while (state
== FLDPLUS
) {
571 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
574 putfmt (name
, cp
, out
);
578 finish_headers (out
);
583 finish_headers (out
);
586 fprintf (out
, "\n%s", buf
);
587 while (state
== BODY
) {
588 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
594 finish_headers (out
);
599 adios (NULL
, "message format error in component #%d", compnum
);
602 adios (NULL
, "getfld() returned %d", state
);
619 * Here's how we decide which address to use as the envelope-from
622 * - If we were given an Envelope-From header, use that.
623 * - If we were given a Sender: address, use that.
624 * - Otherwise, use the address on the From: line
627 if (msgflags
& MEFM
) {
629 } else if (seensender
) {
635 /* If we are doing a "whom" check */
637 verify_all_addresses (1, envelope
);
641 if (msgflags
& MINV
) {
642 make_bcc_file (dashstuff
);
643 if (msgflags
& MVIS
) {
644 verify_all_addresses (verbose
, envelope
);
645 post (tmpfil
, 0, verbose
, envelope
);
647 post (bccfil
, 1, verbose
, envelope
);
650 post (tmpfil
, 0, isatty (1), envelope
);
657 printf (partno
? "Partial Message #%s Processed\n" : "Message Processed\n",
669 putfmt (char *name
, char *str
, FILE *out
)
671 int count
, grp
, i
, keep
;
674 struct mailname
*mp
= NULL
, *np
= NULL
;
677 while (*str
== ' ' || *str
== '\t')
680 if (msgstate
== NORMAL
&& uprf (name
, "resent")) {
681 advise (NULL
, "illegal header line -- %s:", name
);
686 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
687 fprintf (out
, "%s: %s", name
, str
);
692 if (hdr
->flags
& HIGN
) {
695 if (hdr
->flags
& HBAD
) {
696 advise (NULL
, "illegal header line -- %s:", name
);
700 msgflags
|= (hdr
->set
& ~(MVIS
| MINV
));
702 if (hdr
->flags
& HSUB
)
703 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
704 if (hdr
->flags
& HFCC
) {
705 if ((cp
= strrchr(str
, '\n')))
707 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
709 insert_fcc (hdr
, pp
);
711 insert_fcc (hdr
, pp
);
715 if (!(hdr
->flags
& HADR
)) {
716 fprintf (out
, "%s: %s", name
, str
);
720 tmpaddrs
.m_next
= NULL
;
721 for (count
= 0; (cp
= getname (str
)); count
++)
722 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
726 tmpaddrs
.m_next
= mp
;
730 if (hdr
->flags
& HTRY
)
736 if (hdr
->flags
& HNIL
)
737 fprintf (out
, "%s: %s", name
, str
);
740 * Sender (or Resent-Sender) can have only one address
742 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
743 : (hdr
->set
& MSND
)) {
744 advise (NULL
, "%s: field requires one address", name
);
748 advise (NULL
, "%s: field requires at least one address", name
);
755 if (count
> 1 && (hdr
->flags
& HONE
)) {
756 advise (NULL
, "%s: field only permits one address", name
);
761 nameoutput
= linepos
= 0;
762 snprintf (namep
, sizeof(namep
), "%s%s",
763 (hdr
->flags
& HMNG
) ? "Original-" : "", name
);
765 for (grp
= 0, mp
= tmpaddrs
.m_next
; mp
; mp
= np
)
766 if (mp
->m_nohost
) { /* also used to test (hdr->flags & HTRY) */
767 /* The address doesn't include a host, so it might be an alias. */
768 pp
= akvalue (mp
->m_mbox
); /* do mh alias substitution */
769 qp
= akvisible () ? mp
->m_mbox
: "";
772 putgrp (namep
, np
->m_gname
, out
, hdr
->flags
);
773 while ((cp
= getname (pp
))) {
774 if (!(mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
))) {
780 * If it's a From: or Resent-From: header, save the address
781 * for later possible use (as the envelope address for SMTP)
784 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
785 : (hdr
->set
& MFRM
)) {
786 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
787 from
[sizeof(from
) - 1] = '\0';
792 * Also save the Sender: or Resent-Sender: header as well
795 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
796 : (hdr
->set
& MSND
)) {
797 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
798 sender
[sizeof(sender
) - 1] = '\0';
803 * ALSO ... save Envelope-From
806 if (hdr
->set
& MEFM
) {
807 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
808 efrom
[sizeof(efrom
) - 1] = '\0';
811 if (hdr
->flags
& HBCC
)
814 mp
->m_ingrp
= np
->m_ingrp
;
817 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
820 if (putadr (namep
, qp
, mp
, out
, hdr
->flags
))
821 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
830 /* Address includes a host, so no alias substitution is needed. */
833 * If it's a From: or Resent-From header, save the address
834 * for later possible use (as the envelope address for SMTP)
837 if ((msgstate
== RESENT
) ? (hdr
->set
& MRFM
)
838 : (hdr
->set
& MFRM
)) {
839 strncpy(from
, auxformat(mp
, 0), sizeof(from
) - 1);
844 * Also save the Sender: header as well
847 if ((msgstate
== RESENT
) ? (hdr
->set
& MRSN
)
848 : (hdr
->set
& MSND
)) {
849 strncpy(sender
, auxformat(mp
, 0), sizeof(sender
) - 1);
850 sender
[sizeof(sender
) - 1] = '\0';
855 * ALSO ... save Envelope-From
858 if (hdr
->set
& MEFM
) {
859 strncpy(efrom
, auxformat(mp
, 0), sizeof(efrom
) - 1);
860 efrom
[sizeof(efrom
) - 1] = '\0';
863 if (hdr
->flags
& HBCC
)
866 putgrp (namep
, mp
->m_gname
, out
, hdr
->flags
);
869 keep
= putadr (namep
, "", mp
, out
, hdr
->flags
);
873 msgflags
|= (hdr
->set
& (MVIS
| MINV
));
879 if (grp
> 0 && (hdr
->flags
& HNGR
)) {
880 advise (NULL
, "%s: field does not allow groups", name
);
901 * Probably not necessary, but just in case ...
908 if ((cp
= getfullname ()) && *cp
) {
909 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
910 snprintf (signature
, sizeof(signature
), "%s <%s>",
911 sigbuf
, getlocaladdr());
912 if ((cp
= getname (signature
)) == NULL
)
913 adios (NULL
, "getname () failed -- you lose extraordinarily big");
914 if ((mp
= getm (cp
, NULL
, 0, AD_HOST
, NULL
)) == NULL
)
915 adios (NULL
, "bad signature '%s'", sigbuf
);
920 strncpy (signature
, getlocaladdr(), sizeof(signature
));
926 * Now that we've outputted the header fields in the draft
927 * message, we will now output any remaining header fields
928 * that we need to add/create.
932 finish_headers (FILE *out
)
936 if (!(msgflags
& MFRM
)) {
938 * A From: header is now required in the draft.
940 advise (NULL
, "message has no From: header");
941 advise (NULL
, "See default components files for examples");
946 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
947 advise (NULL
, "A Sender: or Envelope-From: header is required "
948 "with multiple\nFrom: addresses");
956 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
958 fprintf (out
, "Message-ID: <%d.%ld@%s>\n",
959 (int) getpid (), (long) tclock
, LocalName (1));
961 * If we have multiple From: addresses, make sure we have an
962 * Sender: header. If we don't have one, then generate one
963 * from Envelope-From: (which in this case, cannot be blank)
966 if (fromcount
> 1 && seensender
== 0) {
967 if (efrom
[0] == '\0') {
968 advise (NULL
, "Envelope-From cannot be blank when there "
969 "is multiple From: addresses\nand no Sender: "
973 fprintf (out
, "Sender: %s\n", efrom
);
977 if (!(msgflags
& MVIS
))
978 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
982 if (!(msgflags
& MDAT
)) {
983 advise (NULL
, "message has no Date: header");
986 if (!(msgflags
& MFRM
)) {
987 advise (NULL
, "message has no From: header");
990 if (!(msgflags
& MRFM
)) {
991 advise (NULL
, "message has no Resent-From: header");
992 advise (NULL
, "See default components files for examples");
996 if (fromcount
> 1 && (seensender
== 0 && !(msgflags
& MEFM
))) {
997 advise (NULL
, "A Resent-Sender: or Envelope-From: header is "
998 "required with multiple\nResent-From: addresses");
1006 fprintf (out
, "Resent-Date: %s\n", dtime (&tclock
, 0));
1008 fprintf (out
, "Resent-Message-ID: <%d.%ld@%s>\n",
1009 (int) getpid (), (long) tclock
, LocalName (1));
1011 * If we have multiple Resent-From: addresses, make sure we have an
1012 * Resent-Sender: header. If we don't have one, then generate one
1013 * from Envelope-From (which in this case, cannot be blank)
1016 if (fromcount
> 1 && seensender
== 0) {
1017 if (efrom
[0] == '\0') {
1018 advise (NULL
, "Envelope-From cannot be blank when there "
1019 "is multiple Resent-From: addresses and no "
1020 "Resent-Sender: header");
1023 fprintf (out
, "Resent-Sender: %s\n", efrom
);
1027 if (!(msgflags
& MVIS
))
1028 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
1033 adios (NULL
, "re-format message and try again");
1035 adios (NULL
, "no addressees");
1040 get_header (char *header
, struct headers
*table
)
1044 for (h
= table
; h
->value
; h
++)
1045 if (!mh_strcasecmp (header
, h
->value
))
1053 putadr (char *name
, char *aka
, struct mailname
*mp
, FILE *out
, unsigned int flags
)
1057 char buffer
[BUFSIZ
];
1059 if (mp
->m_mbox
== NULL
|| ((flags
& HTRY
) && !insert (mp
)))
1061 if ((flags
& (HBCC
| HDCC
| HEFM
)) || mp
->m_ingrp
)
1065 fprintf (out
, "%s: ", name
);
1066 linepos
+= (nameoutput
= strlen (name
) + 2);
1069 if (*aka
&& mp
->m_type
!= UUCPHOST
&& !mp
->m_pers
)
1070 mp
->m_pers
= getcpy (aka
);
1073 snprintf (buffer
, sizeof(buffer
), "%s;", mp
->m_gname
);
1076 cp
= adrformat (mp
);
1083 if (linepos
!= nameoutput
) {
1084 if (len
+ linepos
+ 2 > outputlinelen
)
1085 fprintf (out
, ",\n%*s", linepos
= nameoutput
, "");
1095 return (flags
& HTRY
);
1100 putgrp (char *name
, char *group
, FILE *out
, unsigned int flags
)
1109 fprintf (out
, "%s: ", name
);
1110 linepos
+= (nameoutput
= strlen (name
) + 2);
1113 cp
= concat (group
, ";", NULL
);
1116 if (linepos
> nameoutput
) {
1117 if (len
+ linepos
+ 2 > outputlinelen
) {
1118 fprintf (out
, ",\n%*s", nameoutput
, "");
1119 linepos
= nameoutput
;
1133 insert (struct mailname
*np
)
1135 struct mailname
*mp
;
1137 if (np
->m_mbox
== NULL
)
1140 for (mp
= np
->m_type
== LOCALHOST
? &localaddrs
1141 : np
->m_type
== UUCPHOST
? &uuaddrs
1145 if (!mh_strcasecmp (np
->m_host
, mp
->m_next
->m_host
)
1146 && !mh_strcasecmp (np
->m_mbox
, mp
->m_next
->m_mbox
)
1147 && np
->m_bcc
== mp
->m_next
->m_bcc
)
1160 struct mailname
*mp
;
1162 printf ("-------\n\t-- Addresses --\nlocal:\t");
1163 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1164 printf ("%s%s%s", mp
->m_mbox
,
1165 mp
->m_bcc
? "[BCC]" : "",
1166 mp
->m_next
? ",\n\t" : "");
1168 printf ("\nnet:\t");
1169 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1170 printf ("%s%s@%s%s%s", mp
->m_path
? mp
->m_path
: "",
1171 mp
->m_mbox
, mp
->m_host
,
1172 mp
->m_bcc
? "[BCC]" : "",
1173 mp
->m_next
? ",\n\t" : "");
1175 printf ("\nuucp:\t");
1176 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1177 printf ("%s!%s%s%s", mp
->m_host
, mp
->m_mbox
,
1178 mp
->m_bcc
? "[BCC]" : "",
1179 mp
->m_next
? ",\n\t" : "");
1181 printf ("\n\t-- Folder Copies --\nfcc:\t");
1182 for (i
= 0; i
< fccind
; i
++)
1183 printf ("%s%s", fccfold
[i
], i
+ 1 < fccind
? ",\n\t" : "");
1191 struct mailname
*mp
;
1193 for (mp
= localaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1194 if (annoaux (mp
) == NOTOK
)
1197 for (mp
= netaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1198 if (annoaux (mp
) == NOTOK
)
1201 for (mp
= uuaddrs
.m_next
; mp
; mp
= mp
->m_next
)
1202 if (annoaux (mp
) == NOTOK
)
1212 annoaux (struct mailname
*mp
)
1215 char buffer
[BUFSIZ
];
1217 snprintf (buffer
, sizeof(buffer
), "%s\n", adrformat (mp
));
1218 i
= strlen (buffer
);
1220 return (write (pfd
, buffer
, i
) == i
? OK
: NOTOK
);
1225 insert_fcc (struct headers
*hdr
, unsigned char *pp
)
1229 for (cp
= pp
; isspace (*cp
); cp
++)
1231 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace (*pp
); pp
--)
1239 adios (NULL
, "too many %ss", hdr
->value
);
1240 fccfold
[fccind
++] = getcpy (cp
);
1248 make_bcc_file (int dashstuff
)
1256 tfile
= m_mktemp2(NULL
, "bccs", NULL
, &out
);
1257 if (tfile
== NULL
) adios("bcc", "unable to create temporary file");
1258 chmod (bccfil
, 0600);
1259 strncpy (bccfil
, tfile
, sizeof(bccfil
));
1261 fprintf (out
, "Date: %s\n", dtime (&tclock
, 0));
1263 fprintf (out
, "Message-ID: <%d.%ld@%s>\n",
1264 (int) getpid (), (long) tclock
, LocalName (1));
1266 fprintf (out
, "Subject: %s", subject
);
1267 fprintf (out
, "BCC:\n");
1270 * Use MIME encapsulation for Bcc messages
1276 * Check if any lines in the message clash with the
1277 * prefix for the MIME multipart separator. If there
1278 * is a clash, increment one of the letters in the
1279 * prefix and check again.
1281 if ((cp
= strchr(prefix
, 'a')) == NULL
)
1282 adios (NULL
, "lost prefix start");
1283 while (find_prefix () == NOTOK
) {
1288 adios (NULL
, "can't find a unique delimiter string");
1293 fprintf (out
, "%s: %s\n%s: multipart/digest; boundary=\"",
1294 VRSN_FIELD
, VRSN_VALUE
, TYPE_FIELD
);
1295 fprintf (out
, "%s\"\n\n--%s\n\n", prefix
, prefix
);
1297 fprintf (out
, "\n------- Blind-Carbon-Copy\n\n");
1303 * Do mhl filtering of Bcc messages instead
1304 * of MIME encapsulation.
1306 if (filter
!= NULL
) {
1307 vec
[0] = r1bindex (mhlproc
, '/');
1309 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
1313 adios ("fork", "unable to");
1316 dup2 (fileno (out
), 1);
1319 vec
[i
++] = "-forward";
1324 /* was the flag -[no]dashstuffing specified? */
1326 vec
[i
++] = "-dashstuffing";
1327 else if (dashstuff
< 0)
1328 vec
[i
++] = "-nodashstuffing";
1331 execvp (mhlproc
, vec
);
1332 fprintf (stderr
, "unable to exec ");
1337 pidXwait (child_id
, mhlproc
);
1341 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
1342 adios (tmpfil
, "unable to re-open");
1345 * If using MIME encapsulation, or if the -nodashstuffing
1346 * flag was given, then just copy message. Else do
1347 * RFC934 quoting (dashstuffing).
1349 if (mime
|| dashstuff
< 0)
1350 cpydata (fd
, fileno (out
), tmpfil
, bccfil
);
1352 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
1356 fseek (out
, 0L, SEEK_END
);
1358 fprintf (out
, "\n--%s--\n", prefix
);
1360 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
1366 * Scan message to check if any lines clash with
1367 * the prefix of the MIME multipart separator.
1374 unsigned char buffer
[BUFSIZ
];
1377 if ((in
= fopen (tmpfil
, "r")) == NULL
)
1378 adios (tmpfil
, "unable to re-open");
1380 while (fgets (buffer
, sizeof(buffer
) - 1, in
))
1381 if (buffer
[0] == '-' && buffer
[1] == '-') {
1384 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1388 if (strcmp (buffer
+ 2, prefix
) == 0) {
1399 #define plural(x) (x == 1 ? "" : "s")
1404 if (badadr
&& unkadr
)
1405 die (NULL
, "%d address%s unparsable, %d addressee%s undeliverable",
1406 badadr
, plural (badadr
), unkadr
, plural (badadr
));
1408 die (NULL
, "%d address%s unparsable", badadr
, plural (badadr
));
1410 die (NULL
, "%d addressee%s undeliverable", unkadr
, plural (unkadr
));
1415 do_addresses (int bccque
, int talk
)
1419 struct mailname
*lp
;
1422 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1423 if (lp
->m_bcc
? bccque
: !bccque
) {
1425 printf (" -- Local Recipients --\n");
1426 do_an_address (lp
, talk
);
1431 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1432 if (lp
->m_bcc
? bccque
: !bccque
) {
1434 printf (" -- UUCP Recipients --\n");
1435 do_an_address (lp
, talk
);
1440 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1441 if (lp
->m_bcc
? bccque
: !bccque
) {
1443 printf (" -- Network Recipients --\n");
1444 do_an_address (lp
, talk
);
1450 if (rp_isbad (retval
= sm_waend ()))
1451 die (NULL
, "problem ending addresses; %s", rp_string (retval
));
1456 * MTS-SPECIFIC INTERACTION
1461 * SENDMAIL/SMTP routines
1465 post (char *file
, int bccque
, int talk
, char *envelope
)
1470 onex
= !(msgflags
& MINV
) || bccque
;
1472 if (msgflags
& MINV
)
1473 printf (" -- Posting for %s Recipients --\n",
1474 bccque
? "Blind" : "Sighted");
1476 printf (" -- Posting for All Recipients --\n");
1481 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
, verbose
,
1482 snoop
, onex
, queued
, sasl
, saslssf
,
1483 saslmech
, user
, tls
))
1484 || rp_isbad (retval
= sm_winit (envelope
)))
1485 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1487 do_addresses (bccque
, talk
&& verbose
);
1488 if ((fd
= open (file
, O_RDONLY
)) == NOTOK
)
1489 die (file
, "unable to re-open");
1494 sm_end (onex
? OK
: DONE
);
1498 if (msgflags
& MINV
)
1499 printf (" -- %s Recipient Copies Posted --\n",
1500 bccque
? "Blind" : "Sighted");
1502 printf (" -- Recipient Copies Posted --\n");
1509 /* Address Verification */
1512 verify_all_addresses (int talk
, char *envelope
)
1515 struct mailname
*lp
;
1519 if (!whomsw
|| checksw
)
1520 if (rp_isbad (retval
= sm_init (clientsw
, serversw
, port
, watch
,
1521 verbose
, snoop
, 0, queued
, sasl
,
1522 saslssf
, saslmech
, user
, tls
))
1523 || rp_isbad (retval
= sm_winit (envelope
)))
1524 die (NULL
, "problem initializing server; %s", rp_string (retval
));
1526 if (talk
&& !whomsw
)
1527 printf (" -- Address Verification --\n");
1528 if (talk
&& localaddrs
.m_next
)
1529 printf (" -- Local Recipients --\n");
1530 for (lp
= localaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1531 do_an_address (lp
, talk
);
1533 if (talk
&& uuaddrs
.m_next
)
1534 printf (" -- UUCP Recipients --\n");
1535 for (lp
= uuaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1536 do_an_address (lp
, talk
);
1538 if (talk
&& netaddrs
.m_next
)
1539 printf (" -- Network Recipients --\n");
1540 for (lp
= netaddrs
.m_next
; lp
; lp
= lp
->m_next
)
1541 do_an_address (lp
, talk
);
1544 if (talk
&& !whomsw
)
1545 printf (" -- Address Verification Successful --\n");
1547 if (!whomsw
|| checksw
)
1556 do_an_address (struct mailname
*lp
, int talk
)
1562 switch (lp
->m_type
) {
1566 strncpy (addr
, mbox
, sizeof(addr
));
1570 mbox
= auxformat (lp
, 0);
1572 snprintf (addr
, sizeof(addr
), "%s!%s", lp
->m_host
, lp
->m_mbox
);
1575 default: /* let SendMail decide if the host is bad */
1578 snprintf (addr
, sizeof(addr
), "%s at %s", mbox
, host
);
1583 printf (" %s%s", addr
, whomsw
&& lp
->m_bcc
? "[BCC]" : "");
1585 if (whomsw
&& !checksw
) {
1593 switch (retval
= sm_wadr (mbox
, host
,
1594 lp
->m_type
!= UUCPHOST
? lp
->m_path
: NULL
)) {
1597 printf ("address ok\n");
1603 fprintf (stderr
, " %s: ", addr
);
1604 fprintf (talk
? stdout
: stderr
, "loses; %s\n",
1605 rp_string (retval
));
1611 fprintf (stderr
, " %s: ", addr
);
1612 die (NULL
, "unexpected response; %s", rp_string (retval
));
1620 do_text (char *file
, int fd
)
1625 lseek (fd
, (off_t
) 0, SEEK_SET
);
1627 while ((state
= read (fd
, buf
, sizeof(buf
))) > 0) {
1628 if (rp_isbad (retval
= sm_wtxt (buf
, state
)))
1629 die (NULL
, "problem writing text; %s\n", rp_string (retval
));
1633 die (file
, "problem reading from");
1635 switch (retval
= sm_wtend ()) {
1641 die (NULL
, "posting failed; %s", rp_string (retval
));
1644 die (NULL
, "unexpected response; %s", rp_string (retval
));
1659 if (msgflags
& MINV
)
1662 if (!whomsw
|| checksw
)
1675 hstat
= SIGNAL2 (SIGHUP
, sigser
);
1676 istat
= SIGNAL2 (SIGINT
, sigser
);
1677 qstat
= SIGNAL2 (SIGQUIT
, sigser
);
1678 tstat
= SIGNAL2 (SIGTERM
, sigser
);
1688 SIGNAL (SIGHUP
, hstat
);
1689 SIGNAL (SIGINT
, istat
);
1690 SIGNAL (SIGQUIT
, qstat
);
1691 SIGNAL (SIGTERM
, tstat
);
1699 p_refile (char *file
)
1707 printf (" -- Filing Folder Copies --\n");
1708 for (i
= 0; i
< fccind
; i
++)
1709 fcc (file
, fccfold
[i
]);
1711 printf (" -- Folder Copies Filed --\n");
1716 * Call the `fileproc' to add the file to the folder.
1720 fcc (char *file
, char *folder
)
1727 printf (" %sFcc %s: ", msgstate
== RESENT
? "Resent-" : "", folder
);
1730 for (i
= 0; (child_id
= fork ()) == NOTOK
&& i
< 5; i
++)
1736 fprintf (stderr
, " %sFcc %s: ",
1737 msgstate
== RESENT
? "Resent-" : "", folder
);
1738 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
1742 /* see if we need to add `+' */
1743 snprintf (fold
, sizeof(fold
), "%s%s",
1744 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
1746 /* now exec the fileproc */
1747 execlp (fileproc
, r1bindex (fileproc
, '/'),
1748 "-link", "-file", file
, fold
, NULL
);
1752 if ((status
= pidwait (child_id
, OK
))) {
1754 fprintf (stderr
, " %sFcc %s: ",
1755 msgstate
== RESENT
? "Resent-" : "", folder
);
1756 pidstatus (status
, verbose
? stdout
: stderr
, NULL
);
1759 printf ("folder ok\n");
1771 die (char *what
, char *fmt
, ...)
1776 if (msgflags
& MINV
)
1779 if (!whomsw
|| checksw
)
1783 advertise (what
, NULL
, fmt
, ap
);