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>
23 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
25 static struct swit switches[] = {
37 { "dist", -4 }, /* interface from dist */
42 /* flags for headers->flags */
43 #define HNOP 0x0000 /* just used to keep .set around */
44 #define HBAD 0x0001 /* bad header - don't let it through */
45 #define HADR 0x0002 /* header has an address field */
46 #define HSUB 0x0004 /* Subject: header */
47 #define HTRY 0x0008 /* try to send to addrs on header */
48 #define HBCC 0x0010 /* don't output this header */
49 #define HFCC 0x0020 /* FCC: type header */
50 #define HIGN 0x0040 /* ignore this header */
52 /* flags for headers->set */
53 #define MFRM 0x0001 /* we've seen a From: */
54 #define MDAT 0x0002 /* we've seen a Date: */
55 #define MRFM 0x0004 /* we've seen a Resent-From: */
56 #define MVIS 0x0008 /* we've seen sighted addrs */
57 #define MINV 0x0010 /* we've seen blind addrs */
58 #define MRDT 0x0020 /* we've seen a Resent-Date: */
59 #define MFMM 0x0040 /* The Mail is From a Alternative-Mailbox Addresse */
67 static struct headers NHeaders[] = {
68 { "Return-Path", HBAD, 0 },
69 { "Received", HBAD, 0 },
70 { "Reply-To", HADR, 0 },
71 { "From", HADR, MFRM },
72 { "Sender", HADR|HBAD, 0 },
73 { "Date", HNOP, MDAT },
74 { "Subject", HSUB, 0 },
75 { "To", HADR|HTRY, MVIS },
76 { "Cc", HADR|HTRY, MVIS },
77 { "Bcc", HADR|HTRY|HBCC, MINV },
78 { "Message-Id", HBAD, 0 },
80 { "Envelope-From", HIGN, 0 },
84 static struct headers RHeaders[] = {
85 { "Resent-Reply-To", HADR, 0 },
86 { "Resent-From", HADR, MRFM },
87 { "Resent-Sender", HADR|HBAD, 0 },
88 { "Resent-Date", HNOP, MRDT },
89 { "Resent-Subject", HSUB, 0 },
90 { "Resent-To", HADR|HTRY, MVIS },
91 { "Resent-Cc", HADR|HTRY, MVIS },
92 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
93 { "Resent-Message-Id", HBAD, 0 },
94 { "Resent-Fcc", HFCC, 0 },
95 { "Reply-To", HADR, 0 },
97 { "Envelope-From", HIGN, 0 },
102 static int badmsg = 0;
103 static int verbose = 0;
104 static int debug = 0;
105 static int aliasflg = 0; /* if going to process aliases */
107 static unsigned msgflags = 0; /* what we've seen */
115 static char *subject = NULL; /* the subject field for BCC'ing */
116 static char fccs[BUFSIZ] = "";
117 struct mailname *bccs = NULL; /* list of the bcc recipients */
119 static struct headers *hdrtab; /* table for the message we're doing */
120 static FILE *out; /* output (temp) file */
125 static void putfmt(char *, char *, FILE *);
126 static void finish_headers(FILE *);
127 static int get_header(char *, struct headers *);
128 static void putadr(char *, struct mailname *);
129 static int putone(char *, int, int);
130 static void process_fcc(char *);
131 static void fcc(char *, char *);
132 static void process_bccs(char *);
136 main(int argc, char **argv)
139 char *cp, *msg = NULL, **argp, **arguments;
140 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
143 setlocale(LC_ALL, "");
144 invo_name = mhbasename(argv[0]);
148 arguments = getarguments(invo_name, argc, argv, 0);
151 while ((cp = *argp++)) {
153 switch (smatch(++cp, switches)) {
155 ambigsw(cp, switches);
158 adios(EX_USAGE, NULL, "-%s unknown", cp);
161 snprintf(buf, sizeof(buf),
162 "%s [switches] file",
164 print_help(buf, switches, 1);
165 exit(argc == 2 ? EX_OK : EX_USAGE);
167 print_version(invo_name);
168 exit(argc == 2 ? EX_OK : EX_USAGE);
187 adios(EX_USAGE, NULL, "only one message at a time!");
194 adios(EX_USAGE, NULL, "usage: %s [switches] file", invo_name);
197 if ((in = fopen(msg, "r")) == NULL) {
198 adios(EX_IOERR, msg, "unable to open");
205 tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
208 /* check for "Aliasfile:" profile entry */
209 if ((cp = context_find("Aliasfile"))) {
213 for (ap=brkstring(dp=getcpy(cp), " ", "\n"); ap && *ap;
215 if ((state = alias(etcpath(*ap))) != AK_OK) {
216 adios(EX_IOERR, NULL, "aliasing error in file %s: %s",
217 *ap, akerror(state));
223 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
225 for (compnum = 1, state = FLD;;) {
226 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
229 putfmt(name, buf, out);
235 while (state == FLDPLUS) {
236 state = m_getfld(state, name, buf,
240 putfmt(name, cp, out);
246 fprintf(out, "\n%s", buf);
247 while (state == BODY) {
248 state = m_getfld(state, name, buf,
260 adios(EX_DATAERR, NULL, "message format error in component #%d",
264 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
283 process_bccs(tmpfil);
284 if (!(msgflags & MVIS)) {
285 /* only Bcc rcpts: we're finished here */
292 ** re-open the temp file, unlink it and exec sendmail, giving it
293 ** the msg temp file as std in.
295 if (!freopen(tmpfil, "r", stdin)) {
296 adios(EX_IOERR, tmpfil, "can't reopen for sendmail");
301 *argp++ = "send-mail";
302 *argp++ = "-m"; /* send to me too */
303 *argp++ = "-t"; /* read msg for recipients */
304 *argp++ = "-i"; /* don't stop on "." */
309 execv(sendmail, sargv);
310 adios(EX_OSERR, sendmail, "can't exec");
315 /* DRAFT GENERATION */
318 putfmt(char *name, char *str, FILE *out)
324 /* remove leading whitespace */
325 while (*str==' ' || *str=='\t') {
329 if ((i = get_header(name, hdrtab)) == NOTOK) {
330 /* no header we would care for */
331 if (mh_strcasecmp(name, attach_hdr)==0) {
334 if (mh_strcasecmp(name, sign_hdr)==0) {
337 if (mh_strcasecmp(name, enc_hdr)==0) {
340 /* push it through */
341 fprintf(out, "%s: %s", name, str);
344 /* it's one of the interesting headers */
347 if (hdr->flags & HIGN || strcmp(str, "\n")==0) {
351 if (hdr->flags & HBAD) {
352 advise(NULL, "illegal header line -- %s:", name);
357 msgflags |= hdr->set;
359 if (hdr->flags & HFCC) {
364 if (hdr->flags & HBCC) {
365 struct mailname *mp = NULL;
367 /* Create list of Bcc addrs. */
368 while ((cp = getname(str))) {
369 mp = getm(cp, NULL, 0, AD_HOST, NULL);
370 mp->m_next = bccs; /* push */
376 if (aliasflg && hdr->flags & HTRY) {
378 ** This header contains address(es) that we have to do
379 ** alias expansion on. Because of the saved state in
380 ** getname we have to put all the addresses into a list.
382 struct mailname *f = NULL;
383 struct mailname *mp = NULL;
385 while ((cp = getname(str))) {
386 mp = getm(cp, NULL, 0, AD_HOST, NULL);
391 mp->m_next = f->m_next;
398 /* Now munch on the list, possibly expanding aliases */
404 ** The author(s) of spost decided that alias substitution wasn't
405 ** necessary for the non-HTRY headers. Unfortunately, one of
406 ** those headers is "From:", and having alias substitution work on
407 ** that is extremely useful for someone with a lot of POP3 email
408 ** accounts or aliases. post supports aliasing of "From:"...
410 ** Since "From:"-processing is incompletely implemented in this
411 ** unsupported and undocumented spost backend, I'm not going
412 ** to take the time to implement my new draft-From:-based email
413 ** address masquerading. If I do ever implement it here, I'd almost
414 ** certainly want to implement "From:" line alias processing as well.
415 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
418 ** Although there is no masquerading anymore in mmh, we might want
419 ** to have aliasing of From: addresses. Think about it.
420 ** -- meillo@marmaro.de 2012-02
423 if (hdr->set & MFRM) {
424 struct mailname *mp = NULL;
426 /* needed because the address parser holds global state */
429 while ((cp = getname(str)) != NULL) {
430 mp = getm(cp, NULL, 0, AD_NAME, NULL);
437 if (hdr->flags & HSUB) {
438 subject = getcpy(str);
440 fprintf(out, "%s: %s", name, str);
445 ** Add yet missing headers.
448 finish_headers(FILE *out)
451 char from[BUFSIZ]; /* my network address */
452 char signature[BUFSIZ]; /* my signature */
453 char *resentstr = (msgstate == resent) ? "Resent-" : "";
455 if (!(msgflags & MDAT)) {
456 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
459 if ((cp = context_find("Default-From")) != NULL) {
460 snprintf(signature, sizeof(signature), "%s", cp);
462 snprintf(from, sizeof(from), "%s@%s", getusername(), LocalName());
463 if ((cp = getfullname()) && *cp) {
464 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
466 snprintf(signature, sizeof(signature), "%s", from);
469 if (!(msgflags & MFRM)) {
470 fprintf(out, "%sFrom: %s\n", resentstr, signature);
473 ** Add a Sender: header because the From: header could
474 ** be fake or contain multiple addresses.
476 if (!(msgflags & MFMM)) {
477 fprintf(out, "%sSender: %s\n", resentstr, signature);
480 if (!(msgflags & MVIS)) {
481 fprintf(out, "%sBcc: undisclosed-recipients:;\n", resentstr);
485 adios(EX_DATAERR, NULL, "re-format message and try again");
491 ** Return index of the requested header in the table, or NOTOK if missing.
494 get_header(char *header, struct headers *table)
498 for (h=table; h->value; h++) {
499 if (mh_strcasecmp(header, h->value)==0) {
508 ** output the address list for header "name". The address list
509 ** is a linked list of mailname structs. "nl" points to the head
510 ** of the list. Alias substitution should be done on nl.
513 putadr(char *name, struct mailname *nl)
515 struct mailname *mp, *mp2;
520 fprintf(out, "%s: ", name);
521 namelen = strlen(name) + 2;
524 for (mp = nl; mp; ) {
525 if (linepos > MAX_SM_FIELD) {
526 fprintf(out, "\n%s: ", name);
530 /* a local name - see if it's an alias */
531 cp = akvalue(mp->m_mbox);
532 if (cp == mp->m_mbox) {
533 /* wasn't an alias - use it unchanged */
534 linepos = putone(mp->m_text, linepos, namelen);
536 /* an alias - expand it */
537 while ((cp = getname(cp))) {
538 if (linepos > MAX_SM_FIELD) {
539 fprintf(out, "\n%s: ", name);
542 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
544 mp2->m_pers = getcpy(mp->m_mbox);
545 linepos = putone(adrformat(mp2), linepos, namelen);
547 linepos = putone(mp2->m_text,
555 /* not a local name - use it unchanged */
556 linepos = putone(mp->m_text, linepos, namelen);
566 putone(char *adr, int pos, int indent)
574 } else if (linepos+len > OUTPUTLINELEN) {
575 fprintf(out, ",\n%*s", indent, "");
591 process_fcc(char *str)
596 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
597 adios(EX_DATAERR, NULL, "Too much Fcc data");
599 /* todo: better have three states: SEPARATOR, PLUS, WORD */
600 for (cp=pp=str; *cp; cp++) {
609 if (*pp=='+' || *pp=='@') {
629 fcc(char *file, char *folders)
635 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
639 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
640 adios(EX_DATAERR, NULL, "Too much Fcc data");
642 /* hack: read from /dev/null and refile(1) won't question us */
643 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
645 status = system(cmd);
647 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
648 msgstate == resent ? "Resent-" : "", folders);
649 } else if (status != 0) {
650 fprintf(stderr, "%sFcc %s: Problems occured.\n",
651 msgstate == resent ? "Resent-" : "", folders);
659 process_bccs(char *origmsg)
661 char *bccdraft = NULL;
662 struct mailname *mp = NULL;
665 for (mp=bccs; mp; mp=mp->m_next) {
666 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
667 fprintf(out, "To: %s\n", mp->m_text);
668 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
669 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
670 fprintf(out, "------------\n");
673 if (execprogl("send", "send", bccdraft, (char *)NULL) != 0) {
674 admonish(invo_name, "Problems to send Bcc to %s",