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 },
93 static struct headers RHeaders[] = {
94 { "Resent-Reply-To", HADR, 0 },
95 { "Resent-From", HADR, MRFM },
96 { "Resent-Sender", HADR|HBAD, 0 },
97 { "Resent-Date", HNOP, MRDT },
98 { "Resent-Subject", HSUB, 0 },
99 { "Resent-To", HADR|HTRY, MVIS },
100 { "Resent-Cc", HADR|HTRY, MVIS },
101 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
102 { "Resent-Message-Id", HBAD, 0 },
103 { "Resent-Fcc", HFCC, 0 },
104 { "Reply-To", HADR, 0 },
110 static int badmsg = 0; /* message has bad semantics */
111 static int verbose = 0; /* spell it out */
112 static int debug = 0; /* debugging post */
113 static int watch = 0; /* watch the delivery process */
114 static int pushflg = 0; /* if going to fork to sendmail */
115 static int aliasflg = 0; /* if going to process aliases */
117 static unsigned msgflags = 0; /* what we've seen */
125 static char *subject = NULL; /* the subject field for BCC'ing */
126 static char fccs[BUFSIZ] = "";
127 struct mailname *bccs = NULL; /* list of the bcc recipients */
129 static struct headers *hdrtab; /* table for the message we're doing */
130 static FILE *out; /* output (temp) file */
135 static void putfmt(char *, char *, FILE *);
136 static void finish_headers(FILE *);
137 static int get_header(char *, struct headers *);
138 static void putadr(char *, struct mailname *);
139 static int putone(char *, int, int);
140 static void process_fcc(char *);
141 static void fcc(char *, char *);
142 static void process_bccs(char *);
146 main(int argc, char **argv)
149 char *cp, *msg = NULL, **argp, **arguments;
150 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
154 setlocale(LC_ALL, "");
156 invo_name = mhbasename(argv[0]);
158 /* foil search of user profile/context */
159 if (context_foil(NULL) == -1)
162 arguments = getarguments(invo_name, argc, argv, 0);
165 while ((cp = *argp++)) {
167 switch (smatch(++cp, switches)) {
169 ambigsw(cp, switches);
172 adios(NULL, "-%s unknown", cp);
175 snprintf(buf, sizeof(buf),
176 "%s [switches] file",
178 print_help(buf, switches, 1);
181 print_version(invo_name);
214 if (!(cp = *argp++) || *cp == '-')
215 adios(NULL, "missing argument to %s",
218 if ((state = alias(cp)) != AK_OK)
219 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state));
226 if (!(cp = *argp++) || *cp == '-')
227 adios(NULL, "missing argument to %s",
229 /* create a minimal context */
230 if (context_foil(cp) == -1)
236 adios(NULL, "only one message at a time!");
242 adios(NULL, "usage: %s [switches] file", invo_name);
244 if ((in = fopen(msg, "r")) == NULL)
245 adios(msg, "unable to open");
251 tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
254 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
256 for (compnum = 1, state = FLD;;) {
257 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
260 putfmt(name, buf, out);
266 while (state == FLDPLUS) {
267 state = m_getfld(state, name, buf,
271 putfmt(name, cp, out);
277 fprintf(out, "\n%s", buf);
278 while (state == BODY) {
279 state = m_getfld(state, name, buf,
291 adios(NULL, "message format error in component #%d",
295 adios(NULL, "getfld() returned %d", state);
314 *argp++ = "send-mail";
315 *argp++ = "-m"; /* send to me too */
316 *argp++ = "-t"; /* read msg for recipients */
317 *argp++ = "-i"; /* don't stop on "." */
318 if (watch || verbose) {
325 process_bccs(tmpfil);
326 if (!(msgflags & MVIS)) {
327 /* only Bcc rcpts: we're finished here */
334 ** re-open the temp file, unlink it and exec sendmail, giving it
335 ** the msg temp file as std in.
337 if (!freopen(tmpfil, "r", stdin)) {
338 adios(tmpfil, "can't reopen for sendmail");
343 /* fork to a child to run sendmail */
346 fprintf(verbose ? stdout : stderr,
347 "%s: can't fork to %s\n",
348 invo_name, sendmail);
351 /* we're the child .. */
357 execv(sendmail, sargv);
358 adios(sendmail, "can't exec");
363 /* DRAFT GENERATION */
366 putfmt(char *name, char *str, FILE *out)
372 /* remove all leading whitespace (even newlines) */
373 while (*str==' ' || *str=='\t' || *str=='\n') {
377 if ((i = get_header(name, hdrtab)) == NOTOK) {
378 /* no header we would care for: push it through */
379 fprintf(out, "%s: %s", name, str);
382 /* it's one of the interesting headers */
385 if (hdr->flags & HIGN || !*str) {
389 if (hdr->flags & HBAD) {
390 advise(NULL, "illegal header line -- %s:", name);
395 msgflags |= hdr->set;
397 if (hdr->flags & HFCC) {
402 if (hdr->flags & HBCC) {
403 struct mailname *mp = NULL;
405 /* Create list of Bcc addrs. */
406 while ((cp = getname(str))) {
407 mp = getm(cp, NULL, 0, AD_HOST, NULL);
408 mp->m_next = bccs; /* push */
414 if (aliasflg && hdr->flags & HTRY) {
416 ** This header contains address(es) that we have to do
417 ** alias expansion on. Because of the saved state in
418 ** getname we have to put all the addresses into a list.
420 struct mailname *f = NULL;
421 struct mailname *mp = NULL;
423 while ((cp = getname(str))) {
424 mp = getm(cp, NULL, 0, AD_HOST, NULL);
429 mp->m_next = f->m_next;
436 /* Now munch on the list, possibly expanding aliases */
442 ** The author(s) of spost decided that alias substitution wasn't
443 ** necessary for the non-HTRY headers. Unfortunately, one of
444 ** those headers is "From:", and having alias substitution work on
445 ** that is extremely useful for someone with a lot of POP3 email
446 ** accounts or aliases. post supports aliasing of "From:"...
448 ** Since "From:"-processing is incompletely implemented in this
449 ** unsupported and undocumented spost backend, I'm not going
450 ** to take the time to implement my new draft-From:-based email
451 ** address masquerading. If I do ever implement it here, I'd almost
452 ** certainly want to implement "From:" line alias processing as well.
453 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
456 ** Although there is no masquerading anymore in mmh, we might want
457 ** to have aliasing of From: addresses. Think about it.
458 ** -- meillo@marmaro.de 2012-02
461 if (hdr->flags & HSUB) {
462 subject = getcpy(str);
464 fprintf(out, "%s: %s", name, str);
469 ** Add yet missing headers.
472 finish_headers(FILE *out)
475 char from[BUFSIZ]; /* my network address */
476 char signature[BUFSIZ]; /* my signature */
477 char *resentstr = (msgstate == resent) ? "Resent-" : "";
479 if (!(msgflags & MDAT)) {
480 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
483 strncpy(from, getusername(), sizeof(from));
484 if ((cp = getfullname()) && *cp) {
485 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
487 snprintf(signature, sizeof(signature), "%s", from);
489 if (!(msgflags & MFRM)) {
490 fprintf(out, "%sFrom: %s\n", resentstr, signature);
492 /* In case the From: header contains multiple addresses. */
493 fprintf(out, "%sSender: %s\n", resentstr, from);
495 if (!(msgflags & MVIS)) {
496 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
500 adios(NULL, "re-format message and try again");
506 ** Return index of the requested header in the table, or NOTOK if missing.
509 get_header(char *header, struct headers *table)
513 for (h=table; h->value; h++) {
514 if (mh_strcasecmp(header, h->value)==0) {
524 ** output the address list for header "name". The address list
525 ** is a linked list of mailname structs. "nl" points to the head
526 ** of the list. Alias substitution should be done on nl.
529 putadr(char *name, struct mailname *nl)
531 struct mailname *mp, *mp2;
536 fprintf(out, "%s: ", name);
537 namelen = strlen(name) + 2;
540 for (mp = nl; mp; ) {
541 if (linepos > MAX_SM_FIELD) {
542 fprintf(out, "\n%s: ", name);
546 /* a local name - see if it's an alias */
547 cp = akvalue(mp->m_mbox);
548 if (cp == mp->m_mbox) {
549 /* wasn't an alias - use what the user typed */
550 linepos = putone(mp->m_text, linepos, namelen);
552 /* an alias - expand it */
553 while ((cp = getname(cp))) {
554 if (linepos > MAX_SM_FIELD) {
555 fprintf(out, "\n%s: ", name);
558 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
560 mp2->m_pers = getcpy(mp->m_mbox);
561 linepos = putone(adrformat(mp2), linepos, namelen);
563 linepos = putone(mp2->m_text,
571 /* not a local name - use what the user typed */
572 linepos = putone(mp->m_text, linepos, namelen);
582 putone(char *adr, int pos, int indent)
590 else if (linepos+len > OUTPUTLINELEN) {
591 fprintf(out, ",\n%*s", indent, "");
607 process_fcc(char *str)
612 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
613 adios(NULL, "Too much Fcc data");
615 /* todo: better have three states: SEPARATOR, PLUS, WORD */
616 for (cp=pp=str; *cp; cp++) {
625 if (*pp=='+' || *pp=='@') {
645 fcc(char *file, char *folders)
651 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
655 if (strlen(fileproc)+strlen(file)+strlen(folders)+100 > sizeof cmd) {
656 adios(NULL, "Too much Fcc data");
658 /* hack: read from /dev/null and refile(1) won't question us */
659 snprintf(cmd, sizeof cmd, "</dev/null %s -link -file %s %s",
660 fileproc, file, folders);
661 status = system(cmd);
663 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
664 msgstate == resent ? "Resent-" : "", folders);
665 } else if (status != 0) {
666 fprintf(stderr, "%sFcc %s: Problems occured.\n",
667 msgstate == resent ? "Resent-" : "", folders);
675 process_bccs(char *origmsg)
677 char *bccdraft = NULL;
679 struct mailname *mp = NULL;
682 for (mp=bccs; mp; mp=mp->m_next) {
684 ** Note: This draft file will be left existing by send(1),
685 ** although renamed with backup prefix.
686 ** TODO: We should have it removed eventually.
688 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
689 fprintf(out, "To: %s\n", mp->m_text);
690 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
691 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
692 fprintf(out, "------------\n");
695 snprintf(buf, sizeof buf, "send%s %s",
696 pushflg ? " -push" : "", bccdraft);
697 if (system(buf) != 0) {
698 admonish(invo_name, "Problems to send Bcc to %s",
702 /* TODO: unlink renamed bcc draft after send(1) */