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 process_bccs(tmpfil);
303 if (!(msgflags & MVIS)) {
304 /* only Bcc rcpts: we're finished here */
311 ** re-open the temp file, unlink it and exec sendmail, giving it
312 ** the msg temp file as std in.
314 if (!freopen(tmpfil, "r", stdin)) {
315 adios(tmpfil, "can't reopen for sendmail");
320 *argp++ = "send-mail";
321 *argp++ = "-m"; /* send to me too */
322 *argp++ = "-t"; /* read msg for recipients */
323 *argp++ = "-i"; /* don't stop on "." */
324 if (watch || verbose) {
328 execv(sendmail, sargv);
329 adios(sendmail, "can't exec");
334 /* DRAFT GENERATION */
337 putfmt(char *name, char *str, FILE *out)
343 /* remove all leading whitespace (even newlines) */
344 while (*str==' ' || *str=='\t' || *str=='\n') {
348 if ((i = get_header(name, hdrtab)) == NOTOK) {
349 /* no header we would care for: push it through */
350 fprintf(out, "%s: %s", name, str);
353 /* it's one of the interesting headers */
356 if (hdr->flags & HIGN || !*str) {
360 if (hdr->flags & HBAD) {
361 advise(NULL, "illegal header line -- %s:", name);
366 msgflags |= hdr->set;
368 if (hdr->flags & HFCC) {
373 if (hdr->flags & HBCC) {
374 struct mailname *mp = NULL;
376 /* Create list of Bcc addrs. */
377 while ((cp = getname(str))) {
378 mp = getm(cp, NULL, 0, AD_HOST, NULL);
379 mp->m_next = bccs; /* push */
385 if (aliasflg && hdr->flags & HTRY) {
387 ** This header contains address(es) that we have to do
388 ** alias expansion on. Because of the saved state in
389 ** getname we have to put all the addresses into a list.
391 struct mailname *f = NULL;
392 struct mailname *mp = NULL;
394 while ((cp = getname(str))) {
395 mp = getm(cp, NULL, 0, AD_HOST, NULL);
400 mp->m_next = f->m_next;
407 /* Now munch on the list, possibly expanding aliases */
413 ** The author(s) of spost decided that alias substitution wasn't
414 ** necessary for the non-HTRY headers. Unfortunately, one of
415 ** those headers is "From:", and having alias substitution work on
416 ** that is extremely useful for someone with a lot of POP3 email
417 ** accounts or aliases. post supports aliasing of "From:"...
419 ** Since "From:"-processing is incompletely implemented in this
420 ** unsupported and undocumented spost backend, I'm not going
421 ** to take the time to implement my new draft-From:-based email
422 ** address masquerading. If I do ever implement it here, I'd almost
423 ** certainly want to implement "From:" line alias processing as well.
424 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
427 ** Although there is no masquerading anymore in mmh, we might want
428 ** to have aliasing of From: addresses. Think about it.
429 ** -- meillo@marmaro.de 2012-02
432 if (hdr->flags & HSUB) {
433 subject = getcpy(str);
435 fprintf(out, "%s: %s", name, str);
440 ** Add yet missing headers.
443 finish_headers(FILE *out)
446 char from[BUFSIZ]; /* my network address */
447 char signature[BUFSIZ]; /* my signature */
448 char *resentstr = (msgstate == resent) ? "Resent-" : "";
450 if (!(msgflags & MDAT)) {
451 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
454 strncpy(from, getusername(), sizeof(from));
455 if ((cp = getfullname()) && *cp) {
456 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
458 snprintf(signature, sizeof(signature), "%s", from);
460 if (!(msgflags & MFRM)) {
461 fprintf(out, "%sFrom: %s\n", resentstr, signature);
463 /* In case the From: header contains multiple addresses. */
464 fprintf(out, "%sSender: %s\n", resentstr, from);
466 if (!(msgflags & MVIS)) {
467 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
471 adios(NULL, "re-format message and try again");
477 ** Return index of the requested header in the table, or NOTOK if missing.
480 get_header(char *header, struct headers *table)
484 for (h=table; h->value; h++) {
485 if (mh_strcasecmp(header, h->value)==0) {
495 ** output the address list for header "name". The address list
496 ** is a linked list of mailname structs. "nl" points to the head
497 ** of the list. Alias substitution should be done on nl.
500 putadr(char *name, struct mailname *nl)
502 struct mailname *mp, *mp2;
507 fprintf(out, "%s: ", name);
508 namelen = strlen(name) + 2;
511 for (mp = nl; mp; ) {
512 if (linepos > MAX_SM_FIELD) {
513 fprintf(out, "\n%s: ", name);
517 /* a local name - see if it's an alias */
518 cp = akvalue(mp->m_mbox);
519 if (cp == mp->m_mbox) {
520 /* wasn't an alias - use what the user typed */
521 linepos = putone(mp->m_text, linepos, namelen);
523 /* an alias - expand it */
524 while ((cp = getname(cp))) {
525 if (linepos > MAX_SM_FIELD) {
526 fprintf(out, "\n%s: ", name);
529 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
531 mp2->m_pers = getcpy(mp->m_mbox);
532 linepos = putone(adrformat(mp2), linepos, namelen);
534 linepos = putone(mp2->m_text,
542 /* not a local name - use what the user typed */
543 linepos = putone(mp->m_text, linepos, namelen);
553 putone(char *adr, int pos, int indent)
561 else if (linepos+len > OUTPUTLINELEN) {
562 fprintf(out, ",\n%*s", indent, "");
578 process_fcc(char *str)
583 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
584 adios(NULL, "Too much Fcc data");
586 /* todo: better have three states: SEPARATOR, PLUS, WORD */
587 for (cp=pp=str; *cp; cp++) {
596 if (*pp=='+' || *pp=='@') {
616 fcc(char *file, char *folders)
622 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
626 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
627 adios(NULL, "Too much Fcc data");
629 /* hack: read from /dev/null and refile(1) won't question us */
630 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
632 status = system(cmd);
634 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
635 msgstate == resent ? "Resent-" : "", folders);
636 } else if (status != 0) {
637 fprintf(stderr, "%sFcc %s: Problems occured.\n",
638 msgstate == resent ? "Resent-" : "", folders);
646 process_bccs(char *origmsg)
648 char *bccdraft = NULL;
650 struct mailname *mp = NULL;
653 for (mp=bccs; mp; mp=mp->m_next) {
655 ** Note: This draft file will be left existing by send(1),
656 ** although renamed with backup prefix.
657 ** TODO: We should have it removed eventually.
659 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
660 fprintf(out, "To: %s\n", mp->m_text);
661 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
662 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
663 fprintf(out, "------------\n");
666 snprintf(buf, sizeof buf, "send %s", bccdraft);
667 if (system(buf) != 0) {
668 admonish(invo_name, "Problems to send Bcc to %s",
672 /* TODO: unlink renamed bcc draft after send(1) */