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>
20 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
22 struct swit switches[] = {
32 { "alias aliasfile", 0 },
42 { "dist", -4 }, /* interface from dist */
43 #define PUSHSW 10 /* fork to sendmail then exit */
45 #define NPUSHSW 11 /* exec sendmail */
48 { "library directory", -7 },
53 /* flags for headers->flags */
54 #define HNOP 0x0000 /* just used to keep .set around */
55 #define HBAD 0x0001 /* bad header - don't let it through */
56 #define HADR 0x0002 /* header has an address field */
57 #define HSUB 0x0004 /* Subject: header */
58 #define HTRY 0x0008 /* try to send to addrs on header */
59 #define HBCC 0x0010 /* don't output this header */
60 #define HFCC 0x0020 /* FCC: type header */
61 #define HIGN 0x0040 /* ignore this header */
63 /* flags for headers->set */
64 #define MFRM 0x0001 /* we've seen a From: */
65 #define MDAT 0x0002 /* we've seen a Date: */
66 #define MRFM 0x0004 /* we've seen a Resent-From: */
67 #define MVIS 0x0008 /* we've seen sighted addrs */
68 #define MINV 0x0010 /* we've seen blind addrs */
69 #define MRDT 0x0020 /* we've seen a Resent-Date: */
77 static struct headers NHeaders[] = {
78 { "Return-Path", HBAD, 0 },
79 { "Received", HBAD, 0 },
80 { "Reply-To", HADR, 0 },
81 { "From", HADR, MFRM },
82 { "Sender", HADR|HBAD, 0 },
83 { "Date", HNOP, MDAT },
84 { "Subject", HSUB, 0 },
85 { "To", HADR|HTRY, MVIS },
86 { "Cc", HADR|HTRY, MVIS },
87 { "Bcc", HADR|HTRY|HBCC, MINV },
88 { "Message-Id", HBAD, 0 },
90 { "Envelope-From", HIGN, 0 },
94 static struct headers RHeaders[] = {
95 { "Resent-Reply-To", HADR, 0 },
96 { "Resent-From", HADR, MRFM },
97 { "Resent-Sender", HADR|HBAD, 0 },
98 { "Resent-Date", HNOP, MRDT },
99 { "Resent-Subject", HSUB, 0 },
100 { "Resent-To", HADR|HTRY, MVIS },
101 { "Resent-Cc", HADR|HTRY, MVIS },
102 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
103 { "Resent-Message-Id", HBAD, 0 },
104 { "Resent-Fcc", HFCC, 0 },
105 { "Reply-To", HADR, 0 },
107 { "Envelope-From", HIGN, 0 },
112 static int badmsg = 0; /* message has bad semantics */
113 static int verbose = 0; /* spell it out */
114 static int debug = 0; /* debugging post */
115 static int watch = 0; /* watch the delivery process */
116 static int pushflg = 0; /* if going to fork to sendmail */
117 static int aliasflg = 0; /* if going to process aliases */
119 static unsigned msgflags = 0; /* what we've seen */
127 static char *subject = NULL; /* the subject field for BCC'ing */
128 static char fccs[BUFSIZ] = "";
129 struct mailname *bccs = NULL; /* list of the bcc recipients */
131 static struct headers *hdrtab; /* table for the message we're doing */
132 static FILE *out; /* output (temp) file */
137 static void putfmt(char *, char *, FILE *);
138 static void finish_headers(FILE *);
139 static int get_header(char *, struct headers *);
140 static void putadr(char *, struct mailname *);
141 static int putone(char *, int, int);
142 static void process_fcc(char *);
143 static void fcc(char *, char *);
144 static void process_bccs(char *);
148 main(int argc, char **argv)
151 char *cp, *msg = NULL, **argp, **arguments;
152 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
156 setlocale(LC_ALL, "");
158 invo_name = mhbasename(argv[0]);
160 /* foil search of user profile/context */
161 if (context_foil(NULL) == -1)
164 arguments = getarguments(invo_name, argc, argv, 0);
167 while ((cp = *argp++)) {
169 switch (smatch(++cp, switches)) {
171 ambigsw(cp, switches);
174 adios(NULL, "-%s unknown", cp);
177 snprintf(buf, sizeof(buf),
178 "%s [switches] file",
180 print_help(buf, switches, 1);
183 print_version(invo_name);
216 if (!(cp = *argp++) || *cp == '-')
217 adios(NULL, "missing argument to %s",
220 if ((state = alias(cp)) != AK_OK)
221 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state));
228 if (!(cp = *argp++) || *cp == '-')
229 adios(NULL, "missing argument to %s",
231 /* create a minimal context */
232 if (context_foil(cp) == -1)
238 adios(NULL, "only one message at a time!");
244 adios(NULL, "usage: %s [switches] file", invo_name);
246 if ((in = fopen(msg, "r")) == NULL)
247 adios(msg, "unable to open");
253 tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
256 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
258 for (compnum = 1, state = FLD;;) {
259 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
262 putfmt(name, buf, out);
268 while (state == FLDPLUS) {
269 state = m_getfld(state, name, buf,
273 putfmt(name, cp, out);
279 fprintf(out, "\n%s", buf);
280 while (state == BODY) {
281 state = m_getfld(state, name, buf,
293 adios(NULL, "message format error in component #%d",
297 adios(NULL, "getfld() returned %d", state);
316 *argp++ = "send-mail";
317 *argp++ = "-m"; /* send to me too */
318 *argp++ = "-t"; /* read msg for recipients */
319 *argp++ = "-i"; /* don't stop on "." */
320 if (watch || verbose) {
327 process_bccs(tmpfil);
328 if (!(msgflags & MVIS)) {
329 /* only Bcc rcpts: we're finished here */
336 ** re-open the temp file, unlink it and exec sendmail, giving it
337 ** the msg temp file as std in.
339 if (!freopen(tmpfil, "r", stdin)) {
340 adios(tmpfil, "can't reopen for sendmail");
345 /* fork to a child to run sendmail */
348 fprintf(verbose ? stdout : stderr,
349 "%s: can't fork to %s\n",
350 invo_name, sendmail);
353 /* we're the child .. */
359 execv(sendmail, sargv);
360 adios(sendmail, "can't exec");
365 /* DRAFT GENERATION */
368 putfmt(char *name, char *str, FILE *out)
374 /* remove all leading whitespace (even newlines) */
375 while (*str==' ' || *str=='\t' || *str=='\n') {
379 if ((i = get_header(name, hdrtab)) == NOTOK) {
380 /* no header we would care for: push it through */
381 fprintf(out, "%s: %s", name, str);
384 /* it's one of the interesting headers */
387 if (hdr->flags & HIGN || !*str) {
391 if (hdr->flags & HBAD) {
392 advise(NULL, "illegal header line -- %s:", name);
397 msgflags |= hdr->set;
399 if (hdr->flags & HFCC) {
404 if (hdr->flags & HBCC) {
405 struct mailname *mp = NULL;
407 /* Create list of Bcc addrs. */
408 while ((cp = getname(str))) {
409 mp = getm(cp, NULL, 0, AD_HOST, NULL);
410 mp->m_next = bccs; /* push */
416 if (aliasflg && hdr->flags & HTRY) {
418 ** This header contains address(es) that we have to do
419 ** alias expansion on. Because of the saved state in
420 ** getname we have to put all the addresses into a list.
422 struct mailname *f = NULL;
423 struct mailname *mp = NULL;
425 while ((cp = getname(str))) {
426 mp = getm(cp, NULL, 0, AD_HOST, NULL);
431 mp->m_next = f->m_next;
438 /* Now munch on the list, possibly expanding aliases */
444 ** The author(s) of spost decided that alias substitution wasn't
445 ** necessary for the non-HTRY headers. Unfortunately, one of
446 ** those headers is "From:", and having alias substitution work on
447 ** that is extremely useful for someone with a lot of POP3 email
448 ** accounts or aliases. post supports aliasing of "From:"...
450 ** Since "From:"-processing is incompletely implemented in this
451 ** unsupported and undocumented spost backend, I'm not going
452 ** to take the time to implement my new draft-From:-based email
453 ** address masquerading. If I do ever implement it here, I'd almost
454 ** certainly want to implement "From:" line alias processing as well.
455 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
458 ** Although there is no masquerading anymore in mmh, we might want
459 ** to have aliasing of From: addresses. Think about it.
460 ** -- meillo@marmaro.de 2012-02
463 if (hdr->flags & HSUB) {
464 subject = getcpy(str);
466 fprintf(out, "%s: %s", name, str);
471 ** Add yet missing headers.
474 finish_headers(FILE *out)
477 char from[BUFSIZ]; /* my network address */
478 char signature[BUFSIZ]; /* my signature */
479 char *resentstr = (msgstate == resent) ? "Resent-" : "";
481 if (!(msgflags & MDAT)) {
482 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
485 strncpy(from, getusername(), sizeof(from));
486 if ((cp = getfullname()) && *cp) {
487 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
489 snprintf(signature, sizeof(signature), "%s", from);
491 if (!(msgflags & MFRM)) {
492 fprintf(out, "%sFrom: %s\n", resentstr, signature);
494 /* In case the From: header contains multiple addresses. */
495 fprintf(out, "%sSender: %s\n", resentstr, from);
497 if (!(msgflags & MVIS)) {
498 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
502 adios(NULL, "re-format message and try again");
508 ** Return index of the requested header in the table, or NOTOK if missing.
511 get_header(char *header, struct headers *table)
515 for (h=table; h->value; h++) {
516 if (mh_strcasecmp(header, h->value)==0) {
526 ** output the address list for header "name". The address list
527 ** is a linked list of mailname structs. "nl" points to the head
528 ** of the list. Alias substitution should be done on nl.
531 putadr(char *name, struct mailname *nl)
533 struct mailname *mp, *mp2;
538 fprintf(out, "%s: ", name);
539 namelen = strlen(name) + 2;
542 for (mp = nl; mp; ) {
543 if (linepos > MAX_SM_FIELD) {
544 fprintf(out, "\n%s: ", name);
548 /* a local name - see if it's an alias */
549 cp = akvalue(mp->m_mbox);
550 if (cp == mp->m_mbox) {
551 /* wasn't an alias - use what the user typed */
552 linepos = putone(mp->m_text, linepos, namelen);
554 /* an alias - expand it */
555 while ((cp = getname(cp))) {
556 if (linepos > MAX_SM_FIELD) {
557 fprintf(out, "\n%s: ", name);
560 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
562 mp2->m_pers = getcpy(mp->m_mbox);
563 linepos = putone(adrformat(mp2), linepos, namelen);
565 linepos = putone(mp2->m_text,
573 /* not a local name - use what the user typed */
574 linepos = putone(mp->m_text, linepos, namelen);
584 putone(char *adr, int pos, int indent)
592 else if (linepos+len > OUTPUTLINELEN) {
593 fprintf(out, ",\n%*s", indent, "");
609 process_fcc(char *str)
614 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
615 adios(NULL, "Too much Fcc data");
617 /* todo: better have three states: SEPARATOR, PLUS, WORD */
618 for (cp=pp=str; *cp; cp++) {
627 if (*pp=='+' || *pp=='@') {
647 fcc(char *file, char *folders)
653 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
657 if (strlen(fileproc)+strlen(file)+strlen(folders)+100 > sizeof cmd) {
658 adios(NULL, "Too much Fcc data");
660 /* hack: read from /dev/null and refile(1) won't question us */
661 snprintf(cmd, sizeof cmd, "</dev/null %s -link -file %s %s",
662 fileproc, file, folders);
663 status = system(cmd);
665 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
666 msgstate == resent ? "Resent-" : "", folders);
667 } else if (status != 0) {
668 fprintf(stderr, "%sFcc %s: Problems occured.\n",
669 msgstate == resent ? "Resent-" : "", folders);
677 process_bccs(char *origmsg)
679 char *bccdraft = NULL;
681 struct mailname *mp = NULL;
684 for (mp=bccs; mp; mp=mp->m_next) {
686 ** Note: This draft file will be left existing by send(1),
687 ** although renamed with backup prefix.
688 ** TODO: We should have it removed eventually.
690 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
691 fprintf(out, "To: %s\n", mp->m_text);
692 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
693 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
694 fprintf(out, "------------\n");
697 snprintf(buf, sizeof buf, "send%s %s",
698 pushflg ? " -push" : "", bccdraft);
699 if (system(buf) != 0) {
700 admonish(invo_name, "Problems to send Bcc to %s",
704 /* TODO: unlink renamed bcc draft after send(1) */