3 * spost.c -- feed messages to sendmail
5 * This is a simpler, faster, replacement for "post" for use
6 * when "sendmail" is the transport system.
10 * This code is Copyright (c) 2002, by the authors of nmh. See the
11 * COPYRIGHT file in the root directory of the nmh distribution for
12 * complete copyright information.
17 #include <h/addrsbr.h>
18 #include <h/aliasbr.h>
19 #include <h/dropsbr.h>
23 #define uptolow(c) ((isalpha(c) && isupper (c)) ? tolower (c) : c)
25 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
26 #define FCCS 10 /* max number of fccs allowed */
28 struct swit switches
[] = {
30 { "filter filterfile", 0 },
54 { "alias aliasfile", 0 },
58 { "width columns", 0 },
66 { "dist", -4 }, /* interface from dist */
68 { "check", -5 }, /* interface from whom */
70 { "nocheck", -7 }, /* interface from whom */
72 { "whom", -4 }, /* interface from whom */
73 #define PUSHSW 22 /* fork to sendmail then exit */
75 #define NPUSHSW 23 /* exec sendmail */
78 { "library directory", -7 },
80 { "idanno number", -6 },
85 /* flags for headers->flags */
86 #define HNOP 0x0000 /* just used to keep .set around */
87 #define HBAD 0x0001 /* bad header - don't let it through */
88 #define HADR 0x0002 /* header has an address field */
89 #define HSUB 0x0004 /* Subject: header */
90 #define HTRY 0x0008 /* try to send to addrs on header */
91 #define HBCC 0x0010 /* don't output this header */
92 #define HMNG 0x0020 /* mung this header */
93 #define HNGR 0x0040 /* no groups allowed in this header */
94 #define HFCC 0x0080 /* FCC: type header */
95 #define HNIL 0x0100 /* okay for this header not to have addrs */
96 #define HIGN 0x0200 /* ignore this header */
98 /* flags for headers->set */
99 #define MFRM 0x0001 /* we've seen a From: */
100 #define MDAT 0x0002 /* we've seen a Date: */
101 #define MRFM 0x0004 /* we've seen a Resent-From: */
102 #define MVIS 0x0008 /* we've seen sighted addrs */
103 #define MINV 0x0010 /* we've seen blind addrs */
104 #define MRDT 0x0020 /* we've seen a Resent-Date: */
113 static struct headers NHeaders
[] = {
114 { "Return-Path", HBAD
, 0 },
115 { "Received", HBAD
, 0 },
116 { "Reply-To", HADR
|HNGR
, 0 },
117 { "From", HADR
|HNGR
, MFRM
},
118 { "Sender", HADR
|HBAD
, 0 },
119 { "Date", HNOP
, MDAT
},
120 { "Subject", HSUB
, 0 },
121 { "To", HADR
|HTRY
, MVIS
},
122 { "cc", HADR
|HTRY
, MVIS
},
123 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
124 { "Message-Id", HBAD
, 0 },
129 static struct headers RHeaders
[] = {
130 { "Resent-Reply-To", HADR
|HNGR
, 0 },
131 { "Resent-From", HADR
|HNGR
, MRFM
},
132 { "Resent-Sender", HADR
|HBAD
, 0 },
133 { "Resent-Date", HNOP
, MRDT
},
134 { "Resent-Subject", HSUB
, 0 },
135 { "Resent-To", HADR
|HTRY
, MVIS
},
136 { "Resent-cc", HADR
|HTRY
, MVIS
},
137 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
138 { "Resent-Message-Id", HBAD
, 0 },
139 { "Resent-Fcc", HFCC
, 0 },
140 { "Reply-To", HADR
, 0 },
146 static short fccind
= 0; /* index into fccfold[] */
148 static int badmsg
= 0; /* message has bad semantics */
149 static int verbose
= 0; /* spell it out */
150 static int debug
= 0; /* debugging post */
151 static int rmflg
= 1; /* remove temporary file when done */
152 static int watch
= 0; /* watch the delivery process */
153 static int backflg
= 0; /* rename input file as *.bak when done */
154 static int whomflg
= 0; /* if just checking addresses */
155 static int pushflg
= 0; /* if going to fork to sendmail */
156 static int aliasflg
= -1; /* if going to process aliases */
157 static int outputlinelen
=72;
159 static unsigned msgflags
= 0; /* what we've seen */
165 static char tmpfil
[] = "/tmp/pstXXXXXX";
167 static char from
[BUFSIZ
]; /* my network address */
168 static char signature
[BUFSIZ
]; /* my signature */
169 static char *filter
= NULL
; /* the filter for BCC'ing */
170 static char *subject
= NULL
; /* the subject field for BCC'ing */
171 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
173 static struct headers
*hdrtab
; /* table for the message we're doing */
174 static FILE *out
; /* output (temp) file */
176 extern char *sendmail
;
179 * external prototypes
181 extern char *getfullname (void);
182 extern char *getusername (void);
184 extern boolean draft_from_masquerading
; /* defined in mts.c */
189 static void putfmt (char *, char *, FILE *);
190 static void start_headers (void);
191 static void finish_headers (FILE *);
192 static int get_header (char *, struct headers
*);
193 static void putadr (char *, struct mailname
*);
194 static int putone (char *, int, int);
195 static void insert_fcc (struct headers
*, char *);
196 static void file (char *);
197 static void fcc (char *, char *);
200 static void die (char *, char *, ...);
201 static void make_bcc_file (void);
206 main (int argc
, char **argv
)
208 int state
, i
, pid
, compnum
;
209 char *cp
, *msg
= NULL
, **argp
, **arguments
;
210 char *sargv
[16], buf
[BUFSIZ
], name
[NAMESZ
];
214 setlocale(LC_ALL
, "");
216 invo_name
= r1bindex (argv
[0], '/');
218 /* foil search of user profile/context */
219 if (context_foil (NULL
) == -1)
222 mts_init (invo_name
);
223 arguments
= getarguments (invo_name
, argc
, argv
, 0);
226 while ((cp
= *argp
++)) {
228 switch (smatch (++cp
, switches
)) {
230 ambigsw (cp
, switches
);
233 adios (NULL
, "-%s unknown", cp
);
236 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
237 print_help (buf
, switches
, 1);
240 print_version(invo_name
);
256 if (!(filter
= *argp
++) || *filter
== '-')
257 adios (NULL
, "missing argument to %s", argp
[-2]);
299 if (!(cp
= *argp
++) || *cp
== '-')
300 adios (NULL
, "missing argument to %s", argp
[-2]);
302 alias (AliasFile
);/* load default aka's */
304 if ((state
= alias(cp
)) != AK_OK
)
305 adios (NULL
, "aliasing error in file %s - %s",
306 cp
, akerror(state
) );
313 if (!(cp
= *argp
++) || *cp
== '-')
314 adios (NULL
, "missing argument to %s", argp
[-2]);
315 outputlinelen
= atoi (cp
);
316 if (outputlinelen
<= 10)
321 if (!(cp
= *argp
++) || *cp
== '-')
322 adios (NULL
, "missing argument to %s", argp
[-2]);
323 /* create a minimal context */
324 if (context_foil (cp
) == -1)
329 /* -idanno switch ignored */
330 if (!(cp
= *argp
++) || *cp
== '-')
331 adios (NULL
, "missing argument to %s", argp
[-2]);
336 adios (NULL
, "only one message at a time!");
342 alias (AliasFile
); /* load default aka's */
345 adios (NULL
, "usage: %s [switches] file", invo_name
);
347 if ((in
= fopen (msg
, "r")) == NULL
)
348 adios (msg
, "unable to open");
357 if ((out
= fdopen( mkstemp (tmpfil
), "w" )) == NULL
)
358 adios (tmpfil
, "unable to create");
361 if ((out
= fopen (tmpfil
, "w")) == NULL
)
362 adios (tmpfil
, "unable to create");
363 chmod (tmpfil
, 0600);
367 hdrtab
= (msgstate
== normal
) ? NHeaders
: RHeaders
;
369 for (compnum
= 1, state
= FLD
;;) {
370 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
)) {
373 putfmt (name
, buf
, out
);
379 while (state
== FLDPLUS
) {
380 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
383 putfmt (name
, cp
, out
);
388 finish_headers (out
);
389 fprintf (out
, "\n%s", buf
);
391 while (state
== BODY
) {
392 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
398 finish_headers (out
);
403 adios (NULL
, "message format error in component #%d",
407 adios (NULL
, "getfld() returned %d", state
);
413 if (backflg
&& !whomflg
) {
414 strncpy (buf
, m_backup (msg
), sizeof(buf
));
415 if (rename (msg
, buf
) == NOTOK
)
416 advise (buf
, "unable to rename %s to", msg
);
428 * re-open the temp file, unlink it and exec sendmail, giving it
429 * the msg temp file as std in.
431 if ( freopen( tmpfil
, "r", stdin
) == NULL
)
432 adios (tmpfil
, "can't reopen for sendmail");
437 *argp
++ = "send-mail";
438 *argp
++ = "-m"; /* send to me too */
439 *argp
++ = "-t"; /* read msg for recipients */
440 *argp
++ = "-i"; /* don't stop on "." */
443 if (watch
|| verbose
)
447 if (pushflg
&& !(watch
|| verbose
)) {
448 /* fork to a child to run sendmail */
449 for (i
=0; (pid
= vfork()) == NOTOK
&& i
< 5; i
++)
453 fprintf (verbose
? stdout
: stderr
, "%s: can't fork to %s\n",
454 invo_name
, sendmail
);
457 /* we're the child .. */
463 execv ( sendmail
, sargv
);
464 adios ( sendmail
, "can't exec");
465 return 0; /* dead code to satisfy the compiler */
468 /* DRAFT GENERATION */
471 putfmt (char *name
, char *str
, FILE *out
)
477 while (*str
== ' ' || *str
== '\t')
480 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
481 fprintf (out
, "%s: %s", name
, str
);
486 if (hdr
->flags
& HIGN
)
488 if (hdr
->flags
& HBAD
) {
489 advise (NULL
, "illegal header line -- %s:", name
);
493 msgflags
|= hdr
->set
;
495 if (hdr
->flags
& HSUB
)
496 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
498 if (hdr
->flags
& HFCC
) {
499 if ((cp
= strrchr(str
, '\n')))
501 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
503 insert_fcc (hdr
, pp
);
505 insert_fcc (hdr
, pp
);
510 if (hdr
->flags
& HBCC
) {
516 if (*str
!= '\n' && *str
!= '\0') {
517 if (aliasflg
&& hdr
->flags
& HTRY
) {
518 /* this header contains address(es) that we have to do
519 * alias expansion on. Because of the saved state in
520 * getname we have to put all the addresses into a list.
521 * We then let putadr munch on that list, possibly
524 register struct mailname
*f
= 0;
525 register struct mailname
*mp
= 0;
527 while ((cp
= getname(str
))) {
528 mp
= getm( cp
, NULL
, 0, AD_HOST
, NULL
);
533 mp
->m_next
= f
->m_next
;
538 f
= mp
->m_next
; mp
->m_next
= 0;
541 /* The author(s) of spost decided that alias substitution wasn't
542 necessary for the non-HTRY headers. Unfortunately, one of those
543 headers is "From:", and having alias substitution work on that is
544 extremely useful for someone with a lot of POP3 email accounts or
545 aliases. post supports aliasing of "From:"...
547 Since "From:"-processing is incompletely implemented in this
548 unsupported and undocumented spost backend, I'm not going to take
549 the time to implement my new draft-From:-based email address
550 masquerading. If I do ever implement it here, I'd almost
551 certainly want to implement "From:" line alias processing as
552 well. -- Dan Harkless <dan-nmh@dilvish.speed.net> */
553 fprintf (out
, "%s: %s", name
, str
);
565 strncpy(from
, getusername(), sizeof(from
));
567 if ((cp
= getfullname ()) && *cp
) {
568 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
569 snprintf (signature
, sizeof(signature
), "%s <%s>", sigbuf
, from
);
572 snprintf (signature
, sizeof(signature
), "%s", from
);
577 finish_headers (FILE *out
)
581 if (!(msgflags
& MDAT
))
582 fprintf (out
, "Date: %s\n", dtimenow (0));
584 if (msgflags
& MFRM
) {
585 /* There was already a From: in the draft. Don't add one. */
586 if (!draft_from_masquerading
)
587 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
588 so we'll reveal the user's actual account@thismachine
589 address in a Sender: header (and use it as the envelope
591 fprintf (out
, "Sender: %s\n", from
);
594 fprintf (out
, "From: %s\n", signature
);
597 if (!(msgflags
& MVIS
))
598 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
603 if (!(msgflags
& MRDT
))
604 fprintf (out
, "Resent-Date: %s\n", dtimenow(0));
605 if (msgflags
& MRFM
) {
606 /* There was already a Resent-From: in draft. Don't add one. */
607 if (!draft_from_masquerading
)
608 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
609 so we'll reveal the user's actual account@thismachine
610 address in a Sender: header (and use it as the envelope
612 fprintf (out
, "Resent-Sender: %s\n", from
);
615 /* Construct a Resent-From: header. */
616 fprintf (out
, "Resent-From: %s\n", signature
);
618 if (!(msgflags
& MVIS
))
619 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
625 adios (NULL
, "re-format message and try again");
630 get_header (char *header
, struct headers
*table
)
634 for (h
= table
; h
->value
; h
++)
635 if (!strcasecmp (header
, h
->value
))
643 * output the address list for header "name". The address list
644 * is a linked list of mailname structs. "nl" points to the head
645 * of the list. Alias substitution should be done on nl.
648 putadr (char *name
, struct mailname
*nl
)
650 register struct mailname
*mp
, *mp2
;
651 register int linepos
;
655 fprintf (out
, "%s: ", name
);
656 namelen
= strlen(name
) + 2;
659 for (mp
= nl
; mp
; ) {
660 if (linepos
> MAX_SM_FIELD
) {
661 fprintf (out
, "\n%s: ", name
);
665 /* a local name - see if it's an alias */
666 cp
= akvalue(mp
->m_mbox
);
667 if (cp
== mp
->m_mbox
)
668 /* wasn't an alias - use what the user typed */
669 linepos
= putone( mp
->m_text
, linepos
, namelen
);
671 /* an alias - expand it */
672 while ((cp
= getname(cp
))) {
673 if (linepos
> MAX_SM_FIELD
) {
674 fprintf (out
, "\n%s: ", name
);
677 mp2
= getm( cp
, NULL
, 0, AD_HOST
, NULL
);
679 mp2
->m_pers
= getcpy(mp
->m_mbox
);
680 linepos
= putone( adrformat(mp2
), linepos
, namelen
);
682 linepos
= putone( mp2
->m_text
, linepos
, namelen
);
687 /* not a local name - use what the user typed */
688 linepos
= putone( mp
->m_text
, linepos
, namelen
);
698 putone (char *adr
, int pos
, int indent
)
706 else if ( linepos
+len
> outputlinelen
) {
707 fprintf ( out
, ",\n%*s", indent
, "");
724 insert_fcc (struct headers
*hdr
, char *pp
)
728 for (cp
= pp
; isspace (*cp
); cp
++)
730 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace (*pp
); pp
--)
738 adios (NULL
, "too many %ss", hdr
->value
);
739 fccfold
[fccind
++] = getcpy (cp
);
754 fd
= mkstemp(bccfil
);
755 if (fd
== -1 || (out
= fdopen(fd
, "w")) == NULL
)
756 adios (bccfil
, "unable to create");
759 if ((out
= fopen (bccfil
, "w")) == NULL
)
760 adios (bccfil
, "unable to create");
762 chmod (bccfil
, 0600);
764 fprintf (out
, "Date: %s\n", dtimenow (0));
765 fprintf (out
, "From: %s\n", signature
);
767 fprintf (out
, "Subject: %s", subject
);
768 fprintf (out
, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
771 if (filter
== NULL
) {
772 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
773 adios (NULL
, "unable to re-open");
774 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
778 vec
[0] = r1bindex (mhlproc
, '/');
780 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
784 adios ("vfork", "unable to");
787 dup2 (fileno (out
), 1);
790 vec
[i
++] = "-forward";
796 execvp (mhlproc
, vec
);
797 adios (mhlproc
, "unable to exec");
800 if (status
= pidwait(child_id
, OK
))
801 admonish (NULL
, "%s lost (status=0%o)", vec
[0], status
);
806 fseek (out
, 0L, SEEK_END
);
807 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
812 /* FCC INTERACTION */
822 for (i
= 0; i
< fccind
; i
++)
824 printf ("Fcc: %s\n", fccfold
[i
]);
826 fcc (path
, fccfold
[i
]);
831 fcc (char *file
, char *folder
)
838 printf ("%sFcc: %s\n", msgstate
== resent
? "Resent-" : "", folder
);
841 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
846 fprintf (stderr
, " %sFcc %s: ",
847 msgstate
== resent
? "Resent-" : "", folder
);
848 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
852 snprintf (fold
, sizeof(fold
), "%s%s",
853 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
854 execlp (fileproc
, r1bindex (fileproc
, '/'),
855 "-link", "-file", file
, fold
, NULL
);
859 if ((status
= pidwait(child_id
, OK
))) {
861 fprintf (stderr
, " %sFcc %s: ",
862 msgstate
== resent
? "Resent-" : "", folder
);
863 fprintf (verbose
? stdout
: stderr
,
864 " errored (0%o)\n", status
);
879 die (char *what
, char *fmt
, ...)
884 advertise (what
, NULL
, fmt
, ap
);