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 static struct swit switches[] = {
32 { "alias aliasfile", 0 },
42 { "dist", -4 }, /* interface from dist */
44 { "library directory", -7 },
49 /* flags for headers->flags */
50 #define HNOP 0x0000 /* just used to keep .set around */
51 #define HBAD 0x0001 /* bad header - don't let it through */
52 #define HADR 0x0002 /* header has an address field */
53 #define HSUB 0x0004 /* Subject: header */
54 #define HTRY 0x0008 /* try to send to addrs on header */
55 #define HBCC 0x0010 /* don't output this header */
56 #define HFCC 0x0020 /* FCC: type header */
57 #define HIGN 0x0040 /* ignore this header */
59 /* flags for headers->set */
60 #define MFRM 0x0001 /* we've seen a From: */
61 #define MDAT 0x0002 /* we've seen a Date: */
62 #define MRFM 0x0004 /* we've seen a Resent-From: */
63 #define MVIS 0x0008 /* we've seen sighted addrs */
64 #define MINV 0x0010 /* we've seen blind addrs */
65 #define MRDT 0x0020 /* we've seen a Resent-Date: */
73 static struct headers NHeaders[] = {
74 { "Return-Path", HBAD, 0 },
75 { "Received", HBAD, 0 },
76 { "Reply-To", HADR, 0 },
77 { "From", HADR, MFRM },
78 { "Sender", HADR|HBAD, 0 },
79 { "Date", HNOP, MDAT },
80 { "Subject", HSUB, 0 },
81 { "To", HADR|HTRY, MVIS },
82 { "Cc", HADR|HTRY, MVIS },
83 { "Bcc", HADR|HTRY|HBCC, MINV },
84 { "Message-Id", HBAD, 0 },
86 { "Envelope-From", HIGN, 0 },
90 static struct headers RHeaders[] = {
91 { "Resent-Reply-To", HADR, 0 },
92 { "Resent-From", HADR, MRFM },
93 { "Resent-Sender", HADR|HBAD, 0 },
94 { "Resent-Date", HNOP, MRDT },
95 { "Resent-Subject", HSUB, 0 },
96 { "Resent-To", HADR|HTRY, MVIS },
97 { "Resent-Cc", HADR|HTRY, MVIS },
98 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
99 { "Resent-Message-Id", HBAD, 0 },
100 { "Resent-Fcc", HFCC, 0 },
101 { "Reply-To", HADR, 0 },
103 { "Envelope-From", HIGN, 0 },
108 static int badmsg = 0; /* message has bad semantics */
109 static int verbose = 0; /* spell it out */
110 static int debug = 0; /* debugging post */
111 static int watch = 0; /* watch the delivery process */
112 static int aliasflg = 0; /* if going to process aliases */
114 static unsigned msgflags = 0; /* what we've seen */
122 static char *subject = NULL; /* the subject field for BCC'ing */
123 static char fccs[BUFSIZ] = "";
124 struct mailname *bccs = NULL; /* list of the bcc recipients */
126 static struct headers *hdrtab; /* table for the message we're doing */
127 static FILE *out; /* output (temp) file */
132 static void putfmt(char *, char *, FILE *);
133 static void finish_headers(FILE *);
134 static int get_header(char *, struct headers *);
135 static void putadr(char *, struct mailname *);
136 static int putone(char *, int, int);
137 static void process_fcc(char *);
138 static void fcc(char *, char *);
139 static void process_bccs(char *);
143 main(int argc, char **argv)
146 char *cp, *msg = NULL, **argp, **arguments;
147 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
150 setlocale(LC_ALL, "");
151 invo_name = mhbasename(argv[0]);
153 /* foil search of user profile/context */
154 if (context_foil(NULL) == -1)
157 arguments = getarguments(invo_name, argc, argv, 0);
160 while ((cp = *argp++)) {
162 switch (smatch(++cp, switches)) {
164 ambigsw(cp, switches);
167 adios(NULL, "-%s unknown", cp);
170 snprintf(buf, sizeof(buf),
171 "%s [switches] file",
173 print_help(buf, switches, 1);
176 print_version(invo_name);
202 if (!(cp = *argp++) || *cp == '-')
203 adios(NULL, "missing argument to %s",
206 if ((state = alias(cp)) != AK_OK)
207 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state));
214 if (!(cp = *argp++) || *cp == '-')
215 adios(NULL, "missing argument to %s",
217 /* create a minimal context */
218 if (context_foil(cp) == -1)
224 adios(NULL, "only one message at a time!");
230 adios(NULL, "usage: %s [switches] file", invo_name);
232 if ((in = fopen(msg, "r")) == NULL)
233 adios(msg, "unable to open");
239 tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
242 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
244 for (compnum = 1, state = FLD;;) {
245 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
248 putfmt(name, buf, out);
254 while (state == FLDPLUS) {
255 state = m_getfld(state, name, buf,
259 putfmt(name, cp, out);
265 fprintf(out, "\n%s", buf);
266 while (state == BODY) {
267 state = m_getfld(state, name, buf,
279 adios(NULL, "message format error in component #%d",
283 adios(NULL, "getfld() returned %d", state);
302 *argp++ = "send-mail";
303 *argp++ = "-m"; /* send to me too */
304 *argp++ = "-t"; /* read msg for recipients */
305 *argp++ = "-i"; /* don't stop on "." */
306 if (watch || verbose) {
312 process_bccs(tmpfil);
313 if (!(msgflags & MVIS)) {
314 /* only Bcc rcpts: we're finished here */
321 ** re-open the temp file, unlink it and exec sendmail, giving it
322 ** the msg temp file as std in.
324 if (!freopen(tmpfil, "r", stdin)) {
325 adios(tmpfil, "can't reopen for sendmail");
329 execv(sendmail, sargv);
330 adios(sendmail, "can't exec");
335 /* DRAFT GENERATION */
338 putfmt(char *name, char *str, FILE *out)
344 /* remove all leading whitespace (even newlines) */
345 while (*str==' ' || *str=='\t' || *str=='\n') {
349 if ((i = get_header(name, hdrtab)) == NOTOK) {
350 /* no header we would care for: push it through */
351 fprintf(out, "%s: %s", name, str);
354 /* it's one of the interesting headers */
357 if (hdr->flags & HIGN || !*str) {
361 if (hdr->flags & HBAD) {
362 advise(NULL, "illegal header line -- %s:", name);
367 msgflags |= hdr->set;
369 if (hdr->flags & HFCC) {
374 if (hdr->flags & HBCC) {
375 struct mailname *mp = NULL;
377 /* Create list of Bcc addrs. */
378 while ((cp = getname(str))) {
379 mp = getm(cp, NULL, 0, AD_HOST, NULL);
380 mp->m_next = bccs; /* push */
386 if (aliasflg && hdr->flags & HTRY) {
388 ** This header contains address(es) that we have to do
389 ** alias expansion on. Because of the saved state in
390 ** getname we have to put all the addresses into a list.
392 struct mailname *f = NULL;
393 struct mailname *mp = NULL;
395 while ((cp = getname(str))) {
396 mp = getm(cp, NULL, 0, AD_HOST, NULL);
401 mp->m_next = f->m_next;
408 /* Now munch on the list, possibly expanding aliases */
414 ** The author(s) of spost decided that alias substitution wasn't
415 ** necessary for the non-HTRY headers. Unfortunately, one of
416 ** those headers is "From:", and having alias substitution work on
417 ** that is extremely useful for someone with a lot of POP3 email
418 ** accounts or aliases. post supports aliasing of "From:"...
420 ** Since "From:"-processing is incompletely implemented in this
421 ** unsupported and undocumented spost backend, I'm not going
422 ** to take the time to implement my new draft-From:-based email
423 ** address masquerading. If I do ever implement it here, I'd almost
424 ** certainly want to implement "From:" line alias processing as well.
425 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
428 ** Although there is no masquerading anymore in mmh, we might want
429 ** to have aliasing of From: addresses. Think about it.
430 ** -- meillo@marmaro.de 2012-02
433 if (hdr->flags & HSUB) {
434 subject = getcpy(str);
436 fprintf(out, "%s: %s", name, str);
441 ** Add yet missing headers.
444 finish_headers(FILE *out)
447 char from[BUFSIZ]; /* my network address */
448 char signature[BUFSIZ]; /* my signature */
449 char *resentstr = (msgstate == resent) ? "Resent-" : "";
451 if (!(msgflags & MDAT)) {
452 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
455 strncpy(from, getusername(), sizeof(from));
456 if ((cp = getfullname()) && *cp) {
457 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
459 snprintf(signature, sizeof(signature), "%s", from);
461 if (!(msgflags & MFRM)) {
462 fprintf(out, "%sFrom: %s\n", resentstr, signature);
464 /* In case the From: header contains multiple addresses. */
465 fprintf(out, "%sSender: %s\n", resentstr, from);
467 if (!(msgflags & MVIS)) {
468 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
472 adios(NULL, "re-format message and try again");
478 ** Return index of the requested header in the table, or NOTOK if missing.
481 get_header(char *header, struct headers *table)
485 for (h=table; h->value; h++) {
486 if (mh_strcasecmp(header, h->value)==0) {
496 ** output the address list for header "name". The address list
497 ** is a linked list of mailname structs. "nl" points to the head
498 ** of the list. Alias substitution should be done on nl.
501 putadr(char *name, struct mailname *nl)
503 struct mailname *mp, *mp2;
508 fprintf(out, "%s: ", name);
509 namelen = strlen(name) + 2;
512 for (mp = nl; mp; ) {
513 if (linepos > MAX_SM_FIELD) {
514 fprintf(out, "\n%s: ", name);
518 /* a local name - see if it's an alias */
519 cp = akvalue(mp->m_mbox);
520 if (cp == mp->m_mbox) {
521 /* wasn't an alias - use what the user typed */
522 linepos = putone(mp->m_text, linepos, namelen);
524 /* an alias - expand it */
525 while ((cp = getname(cp))) {
526 if (linepos > MAX_SM_FIELD) {
527 fprintf(out, "\n%s: ", name);
530 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
532 mp2->m_pers = getcpy(mp->m_mbox);
533 linepos = putone(adrformat(mp2), linepos, namelen);
535 linepos = putone(mp2->m_text,
543 /* not a local name - use what the user typed */
544 linepos = putone(mp->m_text, linepos, namelen);
554 putone(char *adr, int pos, int indent)
562 else if (linepos+len > OUTPUTLINELEN) {
563 fprintf(out, ",\n%*s", indent, "");
579 process_fcc(char *str)
584 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
585 adios(NULL, "Too much Fcc data");
587 /* todo: better have three states: SEPARATOR, PLUS, WORD */
588 for (cp=pp=str; *cp; cp++) {
597 if (*pp=='+' || *pp=='@') {
617 fcc(char *file, char *folders)
623 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
627 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
628 adios(NULL, "Too much Fcc data");
630 /* hack: read from /dev/null and refile(1) won't question us */
631 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
633 status = system(cmd);
635 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
636 msgstate == resent ? "Resent-" : "", folders);
637 } else if (status != 0) {
638 fprintf(stderr, "%sFcc %s: Problems occured.\n",
639 msgstate == resent ? "Resent-" : "", folders);
647 process_bccs(char *origmsg)
649 char *bccdraft = NULL;
651 struct mailname *mp = NULL;
654 for (mp=bccs; mp; mp=mp->m_next) {
656 ** Note: This draft file will be left existing by send(1),
657 ** although renamed with backup prefix.
658 ** TODO: We should have it removed eventually.
660 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
661 fprintf(out, "To: %s\n", mp->m_text);
662 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
663 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
664 fprintf(out, "------------\n");
667 snprintf(buf, sizeof buf, "send %s", bccdraft);
668 if (system(buf) != 0) {
669 admonish(invo_name, "Problems to send Bcc to %s",
673 /* TODO: unlink renamed bcc draft after send(1) */