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 */
21 #define FCCS 10 /* max number of fccs allowed */
23 struct swit switches[] = {
33 { "alias aliasfile", 0 },
43 { "dist", -4 }, /* interface from dist */
44 #define PUSHSW 10 /* fork to sendmail then exit */
46 #define NPUSHSW 11 /* exec sendmail */
49 { "library directory", -7 },
54 /* flags for headers->flags */
55 #define HNOP 0x0000 /* just used to keep .set around */
56 #define HBAD 0x0001 /* bad header - don't let it through */
57 #define HADR 0x0002 /* header has an address field */
58 #define HSUB 0x0004 /* Subject: header */
59 #define HTRY 0x0008 /* try to send to addrs on header */
60 #define HBCC 0x0010 /* don't output this header */
61 #define HFCC 0x0020 /* FCC: type header */
62 #define HIGN 0x0040 /* ignore this header */
64 /* flags for headers->set */
65 #define MFRM 0x0001 /* we've seen a From: */
66 #define MDAT 0x0002 /* we've seen a Date: */
67 #define MRFM 0x0004 /* we've seen a Resent-From: */
68 #define MVIS 0x0008 /* we've seen sighted addrs */
69 #define MINV 0x0010 /* we've seen blind addrs */
70 #define MRDT 0x0020 /* we've seen a Resent-Date: */
79 static struct headers NHeaders[] = {
80 { "Return-Path", HBAD, 0 },
81 { "Received", HBAD, 0 },
82 { "Reply-To", HADR, 0 },
83 { "From", HADR, MFRM },
84 { "Sender", HADR|HBAD, 0 },
85 { "Date", HNOP, MDAT },
86 { "Subject", HSUB, 0 },
87 { "To", HADR|HTRY, MVIS },
88 { "Cc", HADR|HTRY, MVIS },
89 { "Bcc", HADR|HTRY|HBCC, MINV },
90 { "Message-Id", HBAD, 0 },
95 static struct headers RHeaders[] = {
96 { "Resent-Reply-To", HADR, 0 },
97 { "Resent-From", HADR, MRFM },
98 { "Resent-Sender", HADR|HBAD, 0 },
99 { "Resent-Date", HNOP, MRDT },
100 { "Resent-Subject", HSUB, 0 },
101 { "Resent-To", HADR|HTRY, MVIS },
102 { "Resent-Cc", HADR|HTRY, MVIS },
103 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
104 { "Resent-Message-Id", HBAD, 0 },
105 { "Resent-Fcc", HFCC, 0 },
106 { "Reply-To", HADR, 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 */
125 static char tmpfil[] = "/tmp/spost-XXXXXX";
126 static char bccfil[] = "/tmp/spost-bcc-XXXXXX";
128 static char *subject = NULL; /* the subject field for BCC'ing */
129 static char fccs[BUFSIZ] = "";
130 struct mailname *bccs = NULL;
132 static struct headers *hdrtab; /* table for the message we're doing */
133 static FILE *out; /* output (temp) file */
135 extern char *sendmail;
140 static void putfmt(char *, char *, FILE *);
141 static void finish_headers(FILE *);
142 static int get_header(char *, struct headers *);
143 static void putadr(char *, struct mailname *);
144 static int putone(char *, int, int);
145 static void process_fcc(char *);
146 static void fcc(char *, char *);
147 static void make_bcc_draft(char *);
151 main(int argc, char **argv)
153 int state, i, pid, compnum;
154 char *cp, *msg = NULL, **argp, **arguments;
155 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
159 setlocale(LC_ALL, "");
161 invo_name = mhbasename(argv[0]);
163 /* foil search of user profile/context */
164 if (context_foil(NULL) == -1)
167 arguments = getarguments(invo_name, argc, argv, 0);
170 while ((cp = *argp++)) {
172 switch (smatch(++cp, switches)) {
174 ambigsw(cp, switches);
177 adios(NULL, "-%s unknown", cp);
180 snprintf(buf, sizeof(buf),
181 "%s [switches] file",
183 print_help(buf, switches, 1);
186 print_version(invo_name);
219 if (!(cp = *argp++) || *cp == '-')
220 adios(NULL, "missing argument to %s",
223 if ((state = alias(cp)) != AK_OK)
224 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state));
231 if (!(cp = *argp++) || *cp == '-')
232 adios(NULL, "missing argument to %s",
234 /* create a minimal context */
235 if (context_foil(cp) == -1)
241 adios(NULL, "only one message at a time!");
247 adios(NULL, "usage: %s [switches] file", invo_name);
249 if ((in = fopen(msg, "r")) == NULL)
250 adios(msg, "unable to open");
257 if ((out = fdopen(mkstemp(tmpfil), "w")) == NULL)
258 adios(tmpfil, "unable to create");
261 if ((out = fopen(tmpfil, "w")) == NULL)
262 adios(tmpfil, "unable to create");
267 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
269 for (compnum = 1, state = FLD;;) {
270 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
273 putfmt(name, buf, out);
279 while (state == FLDPLUS) {
280 state = m_getfld(state, name, buf,
284 putfmt(name, cp, out);
290 fprintf(out, "\n%s", buf);
291 while (state == BODY) {
292 state = m_getfld(state, name, buf,
304 adios(NULL, "message format error in component #%d",
308 adios(NULL, "getfld() returned %d", state);
328 make_bcc_draft(tmpfil);
332 ** re-open the temp file, unlink it and exec sendmail, giving it
333 ** the msg temp file as std in.
335 if (!freopen(tmpfil, "r", stdin)) {
336 adios(tmpfil, "can't reopen for sendmail");
341 *argp++ = "send-mail";
342 *argp++ = "-m"; /* send to me too */
343 *argp++ = "-t"; /* read msg for recipients */
344 *argp++ = "-i"; /* don't stop on "." */
345 if (watch || verbose) {
352 /* fork to a child to run sendmail */
353 for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
357 fprintf(verbose ? stdout : stderr,
358 "%s: can't fork to %s\n",
359 invo_name, sendmail);
362 /* we're the child .. */
370 /* fork to a child to run sendmail */
371 for (i=0; (pid = fork()) == NOTOK && i < 5; i++)
375 fprintf(verbose ? stdout : stderr,
376 "%s: can't fork to %s\n",
377 invo_name, sendmail);
380 /* we're the child .. */
387 execv(sendmail, sargv);
388 adios(sendmail, "can't exec");
389 return 0; /* dead code to satisfy the compiler */
392 /* DRAFT GENERATION */
395 putfmt(char *name, char *str, FILE *out)
401 /* remove all leading whitespace (even newlines) */
402 while (*str==' ' || *str=='\t' || *str=='\n') {
406 if ((i = get_header(name, hdrtab)) == NOTOK) {
407 /* no header we would care for: push it through */
408 fprintf(out, "%s: %s", name, str);
411 /* it's one of the interesting headers */
414 if (hdr->flags & HIGN || !*str) {
418 if (hdr->flags & HBAD) {
419 advise(NULL, "illegal header line -- %s:", name);
424 msgflags |= hdr->set;
426 if (hdr->flags & HFCC) {
431 if (hdr->flags & HBCC) {
432 struct mailname *mp = NULL;
434 /* Create list of Bcc addrs. */
435 while ((cp = getname(str))) {
436 mp = getm(cp, NULL, 0, AD_HOST, NULL);
437 mp->m_next = bccs; /* push */
443 if (aliasflg && hdr->flags & HTRY) {
445 ** This header contains address(es) that we have to do
446 ** alias expansion on. Because of the saved state in
447 ** getname we have to put all the addresses into a list.
449 struct mailname *f = NULL;
450 struct mailname *mp = NULL;
452 while ((cp = getname(str))) {
453 mp = getm(cp, NULL, 0, AD_HOST, NULL);
458 mp->m_next = f->m_next;
465 /* Now munch on the list, possibly expanding aliases */
471 ** The author(s) of spost decided that alias substitution wasn't
472 ** necessary for the non-HTRY headers. Unfortunately, one of
473 ** those headers is "From:", and having alias substitution work on
474 ** that is extremely useful for someone with a lot of POP3 email
475 ** accounts or aliases. post supports aliasing of "From:"...
477 ** Since "From:"-processing is incompletely implemented in this
478 ** unsupported and undocumented spost backend, I'm not going
479 ** to take the time to implement my new draft-From:-based email
480 ** address masquerading. If I do ever implement it here, I'd almost
481 ** certainly want to implement "From:" line alias processing as well.
482 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
485 ** Although there is no masquerading anymore in mmh, we might want
486 ** to have aliasing of From: addresses. Think about it.
487 ** -- meillo@marmaro.de 2012-02
490 if (hdr->flags & HSUB) {
491 subject = getcpy(str);
493 fprintf(out, "%s: %s", name, str);
498 ** Add yet missing headers.
501 finish_headers(FILE *out)
504 char from[BUFSIZ]; /* my network address */
505 char signature[BUFSIZ]; /* my signature */
506 char *resentstr = (msgstate == resent) ? "Resent-" : "";
508 if (!(msgflags & MDAT)) {
509 fprintf(out, "%sDate: %s\n", resentstr, dtimenow(0));
512 strncpy(from, getusername(), sizeof(from));
513 if ((cp = getfullname()) && *cp) {
514 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
516 snprintf(signature, sizeof(signature), "%s", from);
518 if (!(msgflags & MFRM)) {
519 fprintf(out, "%sFrom: %s\n", resentstr, signature);
521 /* In case the From: header contains multiple addresses. */
522 fprintf(out, "%sSender: %s\n", resentstr, from);
524 if (!(msgflags & MVIS)) {
525 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
528 adios(NULL, "re-format message and try again");
534 ** Return index of the requested header in the table, or NOTOK if missing.
537 get_header(char *header, struct headers *table)
541 for (h=table; h->value; h++) {
542 if (mh_strcasecmp(header, h->value)==0) {
552 ** output the address list for header "name". The address list
553 ** is a linked list of mailname structs. "nl" points to the head
554 ** of the list. Alias substitution should be done on nl.
557 putadr(char *name, struct mailname *nl)
559 struct mailname *mp, *mp2;
564 fprintf(out, "%s: ", name);
565 namelen = strlen(name) + 2;
568 for (mp = nl; mp; ) {
569 if (linepos > MAX_SM_FIELD) {
570 fprintf(out, "\n%s: ", name);
574 /* a local name - see if it's an alias */
575 cp = akvalue(mp->m_mbox);
576 if (cp == mp->m_mbox) {
577 /* wasn't an alias - use what the user typed */
578 linepos = putone(mp->m_text, linepos, namelen);
580 /* an alias - expand it */
581 while ((cp = getname(cp))) {
582 if (linepos > MAX_SM_FIELD) {
583 fprintf(out, "\n%s: ", name);
586 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
588 mp2->m_pers = getcpy(mp->m_mbox);
589 linepos = putone(adrformat(mp2), linepos, namelen);
591 linepos = putone(mp2->m_text,
599 /* not a local name - use what the user typed */
600 linepos = putone(mp->m_text, linepos, namelen);
610 putone(char *adr, int pos, int indent)
618 else if (linepos+len > OUTPUTLINELEN) {
619 fprintf(out, ",\n%*s", indent, "");
635 process_fcc(char *str)
640 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
641 adios(NULL, "Too much Fcc data");
643 /* todo: better have three states: SEPARATOR, PLUS, WORD */
644 for (cp=pp=str; *cp; cp++) {
653 if (*pp=='+' || *pp=='@') {
673 fcc(char *file, char *folders)
679 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
683 if (strlen(fileproc)+strlen(file)+strlen(folders)+100 > sizeof cmd) {
684 adios(NULL, "Too much Fcc data");
686 /* hack: read from /dev/null and refile(1) won't question us */
687 snprintf(cmd, sizeof cmd, "</dev/null %s -link -file %s %s",
688 fileproc, file, folders);
689 status = system(cmd);
691 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
692 msgstate == resent ? "Resent-" : "", folders);
693 } else if (status != 0) {
694 fprintf(stderr, "%sFcc %s: Problems occured.\n",
695 msgstate == resent ? "Resent-" : "", folders);
703 make_bcc_draft(char *draft)
709 fd = mkstemp(bccfil);
710 if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
711 adios(bccfil, "unable to create");
714 if ((out = fopen(bccfil, "w")) == NULL)
715 adios(bccfil, "unable to create");
720 fprintf(out, "Subject: BCC: %s", subject);
722 fprintf(out, "Subject: BCC: message without subject");
724 fprintf(out, "%s: %s\n", attach_hdr, draft);
725 fprintf(out, "------------\n");