2 ** spost.c -- feed messages to sendmail
4 ** This is a simpler, faster, replacement for "post" for use
5 ** when "sendmail" is the 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/addrsbr.h>
15 #include <h/aliasbr.h>
16 #include <h/dropsbr.h>
23 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
25 static struct swit switches[] = {
37 { "dist", -4 }, /* interface from dist */
42 /* flags for headers->flags */
43 #define HNOP 0x0000 /* just used to keep .set around */
44 #define HBAD 0x0001 /* bad header - don't let it through */
45 #define HADR 0x0002 /* header has an address field */
46 #define HSUB 0x0004 /* Subject: header */
47 #define HTRY 0x0008 /* try to send to addrs on header */
48 #define HBCC 0x0010 /* don't output this header */
49 #define HFCC 0x0020 /* FCC: type header */
50 #define HIGN 0x0040 /* ignore this header */
52 /* flags for headers->set */
53 #define MFRM 0x0001 /* we've seen a From: */
54 #define MDAT 0x0002 /* we've seen a Date: */
55 #define MRFM 0x0004 /* we've seen a Resent-From: */
56 #define MVIS 0x0008 /* we've seen sighted addrs */
57 #define MINV 0x0010 /* we've seen blind addrs */
58 #define MRDT 0x0020 /* we've seen a Resent-Date: */
59 #define MFMM 0x0040 /* The Mail is From a Alternative-Mailbox Addresse */
67 static struct headers NHeaders[] = {
68 { "Return-Path", HBAD, 0 },
69 { "Received", HBAD, 0 },
70 { "Reply-To", HADR, 0 },
71 { "From", HADR, MFRM },
72 { "Sender", HADR|HBAD, 0 },
73 { "Date", HNOP, MDAT },
74 { "Subject", HSUB, 0 },
75 { "To", HADR|HTRY, MVIS },
76 { "Cc", HADR|HTRY, MVIS },
77 { "Bcc", HADR|HTRY|HBCC, MINV },
78 { "Message-Id", HBAD, 0 },
80 { "Envelope-From", HIGN, 0 },
84 static struct headers RHeaders[] = {
85 { "Resent-Reply-To", HADR, 0 },
86 { "Resent-From", HADR, MRFM },
87 { "Resent-Sender", HADR|HBAD, 0 },
88 { "Resent-Date", HNOP, MRDT },
89 { "Resent-Subject", HSUB, 0 },
90 { "Resent-To", HADR|HTRY, MVIS },
91 { "Resent-Cc", HADR|HTRY, MVIS },
92 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
93 { "Resent-Message-Id", HBAD, 0 },
94 { "Resent-Fcc", HFCC, 0 },
95 { "Reply-To", HADR, 0 },
97 { "Envelope-From", HIGN, 0 },
102 static int badmsg = 0;
103 static int verbose = 0;
104 static int debug = 0;
105 static int aliasflg = 0; /* if going to process aliases */
107 static unsigned msgflags = 0; /* what we've seen */
115 static char *subject = NULL; /* the subject field for BCC'ing */
116 static char fccs[BUFSIZ] = "";
117 struct mailname *bccs = NULL; /* list of the bcc recipients */
118 struct mailname *sender = NULL;
120 static struct headers *hdrtab; /* table for the message we're doing */
121 static FILE *out; /* output (temp) file */
126 static void putfmt(char *, char *, FILE *);
127 static void finish_headers(FILE *);
128 static int get_header(char *, struct headers *);
129 static void putadr(char *, struct mailname *);
130 static int putone(char *, int, int);
131 static void process_fcc(char *);
132 static void fcc(char *, char *);
133 static void process_bccs(char *);
137 main(int argc, char **argv)
140 char *cp, *msg = NULL, **argp, **arguments;
141 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
144 setlocale(LC_ALL, "");
145 invo_name = mhbasename(argv[0]);
149 arguments = getarguments(invo_name, argc, argv, 0);
152 while ((cp = *argp++)) {
154 switch (smatch(++cp, switches)) {
156 ambigsw(cp, switches);
159 adios(EX_USAGE, NULL, "-%s unknown", cp);
162 snprintf(buf, sizeof(buf),
163 "%s [switches] file",
165 print_help(buf, switches, 1);
166 exit(argc == 2 ? EX_OK : EX_USAGE);
168 print_version(invo_name);
169 exit(argc == 2 ? EX_OK : EX_USAGE);
188 adios(EX_USAGE, NULL, "only one message at a time!");
195 adios(EX_USAGE, NULL, "usage: %s [switches] file", invo_name);
198 if ((in = fopen(msg, "r")) == NULL) {
199 adios(EX_IOERR, msg, "unable to open");
206 tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
209 /* check for "Aliasfile:" profile entry */
210 if ((cp = context_find("Aliasfile"))) {
214 for (ap=brkstring(dp=getcpy(cp), " ", "\n"); ap && *ap;
216 if ((state = alias(etcpath(*ap))) != AK_OK) {
217 adios(EX_IOERR, NULL, "aliasing error in file %s: %s",
218 *ap, akerror(state));
224 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
226 for (compnum = 1, state = FLD;;) {
227 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
232 while (state == FLDPLUS) {
233 state = m_getfld(state, name, buf,
237 putfmt(name, cp, out);
243 fprintf(out, "\n%s", buf);
244 while (state == BODY) {
245 state = m_getfld(state, name, buf,
257 adios(EX_DATAERR, NULL, "message format error in component #%d",
261 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
280 process_bccs(tmpfil);
281 if (!(msgflags & MVIS)) {
282 /* only Bcc rcpts: we're finished here */
289 ** re-open the temp file, unlink it and exec sendmail, giving it
290 ** the msg temp file as std in.
292 if (!freopen(tmpfil, "r", stdin)) {
293 adios(EX_IOERR, tmpfil, "can't reopen for sendmail");
298 *argp++ = "send-mail";
299 *argp++ = "-m"; /* send to me too */
300 *argp++ = "-t"; /* read msg for recipients */
301 *argp++ = "-i"; /* don't stop on "." */
306 execv(sendmail, sargv);
307 adios(EX_OSERR, sendmail, "can't exec");
312 /* DRAFT GENERATION */
315 putfmt(char *name, char *str, FILE *out)
321 /* remove leading whitespace */
322 while (*str==' ' || *str=='\t') {
326 if ((i = get_header(name, hdrtab)) == NOTOK) {
327 /* no header we would care for */
328 if (mh_strcasecmp(name, attach_hdr)==0) {
331 if (mh_strcasecmp(name, sign_hdr)==0) {
334 if (mh_strcasecmp(name, enc_hdr)==0) {
337 /* push it through */
338 fprintf(out, "%s: %s", name, str);
341 /* it's one of the interesting headers */
344 if (hdr->flags & HIGN || strcmp(str, "\n")==0) {
348 if (hdr->flags & HBAD) {
349 advise(NULL, "illegal header line -- %s:", name);
354 msgflags |= hdr->set;
356 if (hdr->flags & HFCC) {
361 if (hdr->flags & HBCC) {
362 struct mailname *mp = NULL;
364 /* Create list of Bcc addrs. */
365 while ((cp = getname(str))) {
366 mp = getm(cp, NULL, 0, AD_HOST, NULL);
367 mp->m_next = bccs; /* push */
373 if (aliasflg && hdr->flags & HTRY) {
375 ** This header contains address(es) that we have to do
376 ** alias expansion on. Because of the saved state in
377 ** getname we have to put all the addresses into a list.
379 struct mailname *f = NULL;
380 struct mailname *mp = NULL;
382 while ((cp = getname(str))) {
383 mp = getm(cp, NULL, 0, AD_HOST, NULL);
388 mp->m_next = f->m_next;
395 /* Now munch on the list, possibly expanding aliases */
401 ** The author(s) of spost decided that alias substitution wasn't
402 ** necessary for the non-HTRY headers. Unfortunately, one of
403 ** those headers is "From:", and having alias substitution work on
404 ** that is extremely useful for someone with a lot of POP3 email
405 ** accounts or aliases. post supports aliasing of "From:"...
407 ** Since "From:"-processing is incompletely implemented in this
408 ** unsupported and undocumented spost backend, I'm not going
409 ** to take the time to implement my new draft-From:-based email
410 ** address masquerading. If I do ever implement it here, I'd almost
411 ** certainly want to implement "From:" line alias processing as well.
412 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
415 ** Although there is no masquerading anymore in mmh, we might want
416 ** to have aliasing of From: addresses. Think about it.
417 ** -- meillo@marmaro.de 2012-02
420 if (hdr->set & MFRM) {
421 struct mailname *mp = NULL;
422 struct mailname *my = NULL;
423 unsigned int fromcnt = 0;
425 /* needed because the address parser holds global state */
428 while ((cp = getname(str)) != NULL) {
430 mp = getm(cp, NULL, 0, AD_NAME, NULL);
456 if (hdr->flags & HSUB) {
457 subject = getcpy(str);
459 fprintf(out, "%s: %s", name, str);
464 ** Add yet missing headers.
467 finish_headers(FILE *out)
470 char from[BUFSIZ]; /* my network address */
471 char signature[BUFSIZ]; /* my signature */
472 char *resentstr = (msgstate == resent) ? "Resent-" : "";
474 if (!(msgflags & MDAT)) {
475 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
478 if (sender != NULL) {
479 snprintf(signature, sizeof(signature), "%s", sender->m_text);
480 } else if ((cp = context_find("Default-From")) != NULL) {
481 snprintf(signature, sizeof(signature), "%s", cp);
483 snprintf(from, sizeof(from), "%s@%s", getusername(), LocalName());
484 if ((cp = getfullname()) && *cp) {
485 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
487 snprintf(signature, sizeof(signature), "%s", from);
490 if (!(msgflags & MFRM)) {
491 fprintf(out, "%sFrom: %s\n", resentstr, signature);
494 ** Add a Sender: header because the From: header could
495 ** be fake or contain multiple addresses.
497 if (!(msgflags & MFMM) || sender != NULL) {
498 fprintf(out, "%sSender: %s\n", resentstr, signature);
501 if (!(msgflags & MVIS)) {
502 fprintf(out, "%sBcc: undisclosed-recipients:;\n", resentstr);
506 adios(EX_DATAERR, NULL, "re-format message and try again");
512 ** Return index of the requested header in the table, or NOTOK if missing.
515 get_header(char *header, struct headers *table)
519 for (h=table; h->value; h++) {
520 if (mh_strcasecmp(header, h->value)==0) {
529 ** output the address list for header "name". The address list
530 ** is a linked list of mailname structs. "nl" points to the head
531 ** of the list. Alias substitution should be done on nl.
534 putadr(char *name, struct mailname *nl)
536 struct mailname *mp, *mp2;
541 fprintf(out, "%s: ", name);
542 namelen = strlen(name) + 2;
545 for (mp = nl; mp; ) {
546 if (linepos > MAX_SM_FIELD) {
547 fprintf(out, "\n%s: ", name);
551 /* a local name - see if it's an alias */
552 cp = akvalue(mp->m_mbox);
553 if (cp == mp->m_mbox) {
554 /* wasn't an alias - use it unchanged */
555 linepos = putone(mp->m_text, linepos, namelen);
557 /* an alias - expand it */
558 while ((cp = getname(cp))) {
559 if (linepos > MAX_SM_FIELD) {
560 fprintf(out, "\n%s: ", name);
563 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
565 mp2->m_pers = getcpy(mp->m_mbox);
566 linepos = putone(adrformat(mp2), linepos, namelen);
568 linepos = putone(mp2->m_text,
576 /* not a local name - use it unchanged */
577 linepos = putone(mp->m_text, linepos, namelen);
587 putone(char *adr, int pos, int indent)
595 } else if (linepos+len > OUTPUTLINELEN) {
596 fprintf(out, ",\n%*s", indent, "");
612 process_fcc(char *str)
617 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
618 adios(EX_DATAERR, NULL, "Too much Fcc data");
620 /* todo: better have three states: SEPARATOR, PLUS, WORD */
621 for (cp=pp=str; *cp; cp++) {
630 if (*pp=='+' || *pp=='@') {
650 fcc(char *file, char *folders)
656 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
660 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
661 adios(EX_DATAERR, NULL, "Too much Fcc data");
663 /* hack: read from /dev/null and refile(1) won't question us */
664 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
666 status = system(cmd);
668 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
669 msgstate == resent ? "Resent-" : "", folders);
670 } else if (status != 0) {
671 fprintf(stderr, "%sFcc %s: Problems occured.\n",
672 msgstate == resent ? "Resent-" : "", folders);
680 process_bccs(char *origmsg)
682 char *bccdraft = NULL;
683 struct mailname *mp = NULL;
686 for (mp=bccs; mp; mp=mp->m_next) {
687 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
688 fprintf(out, "To: %s\n", mp->m_text);
689 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
690 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
691 fprintf(out, "------------\n");
694 if (execprogl("send", "send", bccdraft, (char *)NULL) != 0) {
695 admonish(invo_name, "Problems to send Bcc to %s",