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.
8 * This code is Copyright (c) 2002, by the authors of nmh. See the
9 * COPYRIGHT file in the root directory of the nmh distribution for
10 * complete copyright information.
15 #include <h/addrsbr.h>
16 #include <h/aliasbr.h>
17 #include <h/dropsbr.h>
22 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
23 #define FCCS 10 /* max number of fccs allowed */
25 struct swit switches
[] = {
27 { "filter filterfile", 0 },
51 { "alias aliasfile", 0 },
55 { "width columns", 0 },
63 { "dist", -4 }, /* interface from dist */
65 { "check", -5 }, /* interface from whom */
67 { "nocheck", -7 }, /* interface from whom */
69 { "whom", -4 }, /* interface from whom */
70 #define PUSHSW 22 /* fork to sendmail then exit */
72 #define NPUSHSW 23 /* exec sendmail */
75 { "library directory", -7 },
77 { "idanno number", -6 },
82 /* flags for headers->flags */
83 #define HNOP 0x0000 /* just used to keep .set around */
84 #define HBAD 0x0001 /* bad header - don't let it through */
85 #define HADR 0x0002 /* header has an address field */
86 #define HSUB 0x0004 /* Subject: header */
87 #define HTRY 0x0008 /* try to send to addrs on header */
88 #define HBCC 0x0010 /* don't output this header */
89 #define HMNG 0x0020 /* mung this header */
90 #define HNGR 0x0040 /* no groups allowed in this header */
91 #define HFCC 0x0080 /* FCC: type header */
92 #define HNIL 0x0100 /* okay for this header not to have addrs */
93 #define HIGN 0x0200 /* ignore this header */
95 /* flags for headers->set */
96 #define MFRM 0x0001 /* we've seen a From: */
97 #define MDAT 0x0002 /* we've seen a Date: */
98 #define MRFM 0x0004 /* we've seen a Resent-From: */
99 #define MVIS 0x0008 /* we've seen sighted addrs */
100 #define MINV 0x0010 /* we've seen blind addrs */
101 #define MRDT 0x0020 /* we've seen a Resent-Date: */
110 static struct headers NHeaders
[] = {
111 { "Return-Path", HBAD
, 0 },
112 { "Received", HBAD
, 0 },
113 { "Reply-To", HADR
|HNGR
, 0 },
114 { "From", HADR
|HNGR
, MFRM
},
115 { "Sender", HADR
|HBAD
, 0 },
116 { "Date", HNOP
, MDAT
},
117 { "Subject", HSUB
, 0 },
118 { "To", HADR
|HTRY
, MVIS
},
119 { "cc", HADR
|HTRY
, MVIS
},
120 { "Bcc", HADR
|HTRY
|HBCC
|HNIL
, MINV
},
121 { "Message-Id", HBAD
, 0 },
126 static struct headers RHeaders
[] = {
127 { "Resent-Reply-To", HADR
|HNGR
, 0 },
128 { "Resent-From", HADR
|HNGR
, MRFM
},
129 { "Resent-Sender", HADR
|HBAD
, 0 },
130 { "Resent-Date", HNOP
, MRDT
},
131 { "Resent-Subject", HSUB
, 0 },
132 { "Resent-To", HADR
|HTRY
, MVIS
},
133 { "Resent-cc", HADR
|HTRY
, MVIS
},
134 { "Resent-Bcc", HADR
|HTRY
|HBCC
, MINV
},
135 { "Resent-Message-Id", HBAD
, 0 },
136 { "Resent-Fcc", HFCC
, 0 },
137 { "Reply-To", HADR
, 0 },
143 static short fccind
= 0; /* index into fccfold[] */
145 static int badmsg
= 0; /* message has bad semantics */
146 static int verbose
= 0; /* spell it out */
147 static int debug
= 0; /* debugging post */
148 static int rmflg
= 1; /* remove temporary file when done */
149 static int watch
= 0; /* watch the delivery process */
150 static int backflg
= 0; /* rename input file as *.bak when done */
151 static int whomflg
= 0; /* if just checking addresses */
152 static int pushflg
= 0; /* if going to fork to sendmail */
153 static int aliasflg
= -1; /* if going to process aliases */
154 static int outputlinelen
=72;
156 static unsigned msgflags
= 0; /* what we've seen */
162 static char tmpfil
[] = "/tmp/pstXXXXXX";
164 static char from
[BUFSIZ
]; /* my network address */
165 static char signature
[BUFSIZ
]; /* my signature */
166 static char *filter
= NULL
; /* the filter for BCC'ing */
167 static char *subject
= NULL
; /* the subject field for BCC'ing */
168 static char *fccfold
[FCCS
]; /* foldernames for FCC'ing */
170 static struct headers
*hdrtab
; /* table for the message we're doing */
171 static FILE *out
; /* output (temp) file */
173 extern char *sendmail
;
176 * external prototypes
178 extern char *getfullname (void);
179 extern char *getusername (void);
181 extern boolean draft_from_masquerading
; /* defined in mts.c */
186 static void putfmt (char *, char *, FILE *);
187 static void start_headers (void);
188 static void finish_headers (FILE *);
189 static int get_header (char *, struct headers
*);
190 static void putadr (char *, struct mailname
*);
191 static int putone (char *, int, int);
192 static void insert_fcc (struct headers
*, unsigned char *);
193 static void file (char *);
194 static void fcc (char *, char *);
197 static void die (char *, char *, ...);
198 static void make_bcc_file (void);
203 main (int argc
, char **argv
)
205 int state
, i
, pid
, compnum
;
206 char *cp
, *msg
= NULL
, **argp
, **arguments
;
207 char *sargv
[16], buf
[BUFSIZ
], name
[NAMESZ
];
211 setlocale(LC_ALL
, "");
213 invo_name
= r1bindex (argv
[0], '/');
215 /* foil search of user profile/context */
216 if (context_foil (NULL
) == -1)
219 mts_init (invo_name
);
220 arguments
= getarguments (invo_name
, argc
, argv
, 0);
223 while ((cp
= *argp
++)) {
225 switch (smatch (++cp
, switches
)) {
227 ambigsw (cp
, switches
);
230 adios (NULL
, "-%s unknown", cp
);
233 snprintf (buf
, sizeof(buf
), "%s [switches] file", invo_name
);
234 print_help (buf
, switches
, 1);
237 print_version(invo_name
);
253 if (!(filter
= *argp
++) || *filter
== '-')
254 adios (NULL
, "missing argument to %s", argp
[-2]);
296 if (!(cp
= *argp
++) || *cp
== '-')
297 adios (NULL
, "missing argument to %s", argp
[-2]);
299 alias (AliasFile
);/* load default aka's */
301 if ((state
= alias(cp
)) != AK_OK
)
302 adios (NULL
, "aliasing error in file %s - %s",
303 cp
, akerror(state
) );
310 if (!(cp
= *argp
++) || *cp
== '-')
311 adios (NULL
, "missing argument to %s", argp
[-2]);
312 outputlinelen
= atoi (cp
);
313 if (outputlinelen
<= 10)
318 if (!(cp
= *argp
++) || *cp
== '-')
319 adios (NULL
, "missing argument to %s", argp
[-2]);
320 /* create a minimal context */
321 if (context_foil (cp
) == -1)
326 /* -idanno switch ignored */
327 if (!(cp
= *argp
++) || *cp
== '-')
328 adios (NULL
, "missing argument to %s", argp
[-2]);
333 adios (NULL
, "only one message at a time!");
339 alias (AliasFile
); /* load default aka's */
342 adios (NULL
, "usage: %s [switches] file", invo_name
);
344 if ((in
= fopen (msg
, "r")) == NULL
)
345 adios (msg
, "unable to open");
354 if ((out
= fdopen( mkstemp (tmpfil
), "w" )) == NULL
)
355 adios (tmpfil
, "unable to create");
358 if ((out
= fopen (tmpfil
, "w")) == NULL
)
359 adios (tmpfil
, "unable to create");
360 chmod (tmpfil
, 0600);
364 hdrtab
= (msgstate
== normal
) ? NHeaders
: RHeaders
;
366 for (compnum
= 1, state
= FLD
;;) {
367 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
)) {
370 putfmt (name
, buf
, out
);
376 while (state
== FLDPLUS
) {
377 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
380 putfmt (name
, cp
, out
);
385 finish_headers (out
);
386 fprintf (out
, "\n%s", buf
);
388 while (state
== BODY
) {
389 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
395 finish_headers (out
);
400 adios (NULL
, "message format error in component #%d",
404 adios (NULL
, "getfld() returned %d", state
);
410 if (backflg
&& !whomflg
) {
411 strncpy (buf
, m_backup (msg
), sizeof(buf
));
412 if (rename (msg
, buf
) == NOTOK
)
413 advise (buf
, "unable to rename %s to", msg
);
425 * re-open the temp file, unlink it and exec sendmail, giving it
426 * the msg temp file as std in.
428 if ( freopen( tmpfil
, "r", stdin
) == NULL
)
429 adios (tmpfil
, "can't reopen for sendmail");
434 *argp
++ = "send-mail";
435 *argp
++ = "-m"; /* send to me too */
436 *argp
++ = "-t"; /* read msg for recipients */
437 *argp
++ = "-i"; /* don't stop on "." */
440 if (watch
|| verbose
)
444 if (pushflg
&& !(watch
|| verbose
)) {
445 /* fork to a child to run sendmail */
446 for (i
=0; (pid
= vfork()) == NOTOK
&& i
< 5; i
++)
450 fprintf (verbose
? stdout
: stderr
, "%s: can't fork to %s\n",
451 invo_name
, sendmail
);
454 /* we're the child .. */
460 execv ( sendmail
, sargv
);
461 adios ( sendmail
, "can't exec");
462 return 0; /* dead code to satisfy the compiler */
465 /* DRAFT GENERATION */
468 putfmt (char *name
, char *str
, FILE *out
)
474 while (*str
== ' ' || *str
== '\t')
477 if ((i
= get_header (name
, hdrtab
)) == NOTOK
) {
478 fprintf (out
, "%s: %s", name
, str
);
483 if (hdr
->flags
& HIGN
)
485 if (hdr
->flags
& HBAD
) {
486 advise (NULL
, "illegal header line -- %s:", name
);
490 msgflags
|= hdr
->set
;
492 if (hdr
->flags
& HSUB
)
493 subject
= subject
? add (str
, add ("\t", subject
)) : getcpy (str
);
495 if (hdr
->flags
& HFCC
) {
496 if ((cp
= strrchr(str
, '\n')))
498 for (cp
= pp
= str
; (cp
= strchr(pp
, ',')); pp
= cp
) {
500 insert_fcc (hdr
, pp
);
502 insert_fcc (hdr
, pp
);
507 if (hdr
->flags
& HBCC
) {
513 if (*str
!= '\n' && *str
!= '\0') {
514 if (aliasflg
&& hdr
->flags
& HTRY
) {
515 /* this header contains address(es) that we have to do
516 * alias expansion on. Because of the saved state in
517 * getname we have to put all the addresses into a list.
518 * We then let putadr munch on that list, possibly
521 register struct mailname
*f
= 0;
522 register struct mailname
*mp
= 0;
524 while ((cp
= getname(str
))) {
525 mp
= getm( cp
, NULL
, 0, AD_HOST
, NULL
);
530 mp
->m_next
= f
->m_next
;
535 f
= mp
->m_next
; mp
->m_next
= 0;
538 /* The author(s) of spost decided that alias substitution wasn't
539 necessary for the non-HTRY headers. Unfortunately, one of those
540 headers is "From:", and having alias substitution work on that is
541 extremely useful for someone with a lot of POP3 email accounts or
542 aliases. post supports aliasing of "From:"...
544 Since "From:"-processing is incompletely implemented in this
545 unsupported and undocumented spost backend, I'm not going to take
546 the time to implement my new draft-From:-based email address
547 masquerading. If I do ever implement it here, I'd almost
548 certainly want to implement "From:" line alias processing as
549 well. -- Dan Harkless <dan-nmh@dilvish.speed.net> */
550 fprintf (out
, "%s: %s", name
, str
);
562 strncpy(from
, getusername(), sizeof(from
));
564 if ((cp
= getfullname ()) && *cp
) {
565 strncpy (sigbuf
, cp
, sizeof(sigbuf
));
566 snprintf (signature
, sizeof(signature
), "%s <%s>", sigbuf
, from
);
569 snprintf (signature
, sizeof(signature
), "%s", from
);
574 finish_headers (FILE *out
)
578 if (!(msgflags
& MDAT
))
579 fprintf (out
, "Date: %s\n", dtimenow (0));
581 if (msgflags
& MFRM
) {
582 /* There was already a From: in the draft. Don't add one. */
583 if (!draft_from_masquerading
)
584 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
585 so we'll reveal the user's actual account@thismachine
586 address in a Sender: header (and use it as the envelope
588 fprintf (out
, "Sender: %s\n", from
);
591 fprintf (out
, "From: %s\n", signature
);
594 if (!(msgflags
& MVIS
))
595 fprintf (out
, "Bcc: Blind Distribution List: ;\n");
600 if (!(msgflags
& MRDT
))
601 fprintf (out
, "Resent-Date: %s\n", dtimenow(0));
602 if (msgflags
& MRFM
) {
603 /* There was already a Resent-From: in draft. Don't add one. */
604 if (!draft_from_masquerading
)
605 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
606 so we'll reveal the user's actual account@thismachine
607 address in a Sender: header (and use it as the envelope
609 fprintf (out
, "Resent-Sender: %s\n", from
);
612 /* Construct a Resent-From: header. */
613 fprintf (out
, "Resent-From: %s\n", signature
);
615 if (!(msgflags
& MVIS
))
616 fprintf (out
, "Resent-Bcc: Blind Re-Distribution List: ;\n");
622 adios (NULL
, "re-format message and try again");
627 get_header (char *header
, struct headers
*table
)
631 for (h
= table
; h
->value
; h
++)
632 if (!mh_strcasecmp (header
, h
->value
))
640 * output the address list for header "name". The address list
641 * is a linked list of mailname structs. "nl" points to the head
642 * of the list. Alias substitution should be done on nl.
645 putadr (char *name
, struct mailname
*nl
)
647 register struct mailname
*mp
, *mp2
;
648 register int linepos
;
652 fprintf (out
, "%s: ", name
);
653 namelen
= strlen(name
) + 2;
656 for (mp
= nl
; mp
; ) {
657 if (linepos
> MAX_SM_FIELD
) {
658 fprintf (out
, "\n%s: ", name
);
662 /* a local name - see if it's an alias */
663 cp
= akvalue(mp
->m_mbox
);
664 if (cp
== mp
->m_mbox
)
665 /* wasn't an alias - use what the user typed */
666 linepos
= putone( mp
->m_text
, linepos
, namelen
);
668 /* an alias - expand it */
669 while ((cp
= getname(cp
))) {
670 if (linepos
> MAX_SM_FIELD
) {
671 fprintf (out
, "\n%s: ", name
);
674 mp2
= getm( cp
, NULL
, 0, AD_HOST
, NULL
);
676 mp2
->m_pers
= getcpy(mp
->m_mbox
);
677 linepos
= putone( adrformat(mp2
), linepos
, namelen
);
679 linepos
= putone( mp2
->m_text
, linepos
, namelen
);
684 /* not a local name - use what the user typed */
685 linepos
= putone( mp
->m_text
, linepos
, namelen
);
695 putone (char *adr
, int pos
, int indent
)
703 else if ( linepos
+len
> outputlinelen
) {
704 fprintf ( out
, ",\n%*s", indent
, "");
721 insert_fcc (struct headers
*hdr
, unsigned char *pp
)
725 for (cp
= pp
; isspace (*cp
); cp
++)
727 for (pp
+= strlen (pp
) - 1; pp
> cp
&& isspace (*pp
); pp
--)
735 adios (NULL
, "too many %ss", hdr
->value
);
736 fccfold
[fccind
++] = getcpy (cp
);
751 fd
= mkstemp(bccfil
);
752 if (fd
== -1 || (out
= fdopen(fd
, "w")) == NULL
)
753 adios (bccfil
, "unable to create");
756 if ((out
= fopen (bccfil
, "w")) == NULL
)
757 adios (bccfil
, "unable to create");
759 chmod (bccfil
, 0600);
761 fprintf (out
, "Date: %s\n", dtimenow (0));
762 if (msgflags
& MFRM
) {
763 /* There was already a From: in the draft. Don't add one. */
764 if (!draft_from_masquerading
)
765 /* mts.conf didn't contain "masquerade:[...]draft_from[...]"
766 so we'll reveal the user's actual account@thismachine
767 address in a Sender: header (and use it as the envelope
769 fprintf (out
, "Sender: %s\n", from
);
772 /* Construct a From: header. */
773 fprintf (out
, "From: %s\n", signature
);
775 fprintf (out
, "Subject: %s", subject
);
776 fprintf (out
, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
779 if (filter
== NULL
) {
780 if ((fd
= open (tmpfil
, O_RDONLY
)) == NOTOK
)
781 adios (NULL
, "unable to re-open");
782 cpydgst (fd
, fileno (out
), tmpfil
, bccfil
);
786 vec
[0] = r1bindex (mhlproc
, '/');
788 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
792 adios ("vfork", "unable to");
795 dup2 (fileno (out
), 1);
798 vec
[i
++] = "-forward";
804 execvp (mhlproc
, vec
);
805 adios (mhlproc
, "unable to exec");
808 if (status
= pidwait(child_id
, OK
))
809 admonish (NULL
, "%s lost (status=0%o)", vec
[0], status
);
814 fseek (out
, 0L, SEEK_END
);
815 fprintf (out
, "\n------- End of Blind-Carbon-Copy\n");
820 /* FCC INTERACTION */
830 for (i
= 0; i
< fccind
; i
++)
832 printf ("Fcc: %s\n", fccfold
[i
]);
834 fcc (path
, fccfold
[i
]);
839 fcc (char *file
, char *folder
)
846 printf ("%sFcc: %s\n", msgstate
== resent
? "Resent-" : "", folder
);
849 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
854 fprintf (stderr
, " %sFcc %s: ",
855 msgstate
== resent
? "Resent-" : "", folder
);
856 fprintf (verbose
? stdout
: stderr
, "no forks, so not ok\n");
860 snprintf (fold
, sizeof(fold
), "%s%s",
861 *folder
== '+' || *folder
== '@' ? "" : "+", folder
);
862 execlp (fileproc
, r1bindex (fileproc
, '/'),
863 "-link", "-file", file
, fold
, NULL
);
867 if ((status
= pidwait(child_id
, OK
))) {
869 fprintf (stderr
, " %sFcc %s: ",
870 msgstate
== resent
? "Resent-" : "", folder
);
871 fprintf (verbose
? stdout
: stderr
,
872 " errored (0%o)\n", status
);
887 die (char *what
, char *fmt
, ...)
892 advertise (what
, NULL
, fmt
, ap
);