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!");
193 adios(EX_USAGE, NULL, "usage: %s [switches] file", invo_name);
195 if ((in = fopen(msg, "r")) == NULL)
196 adios(EX_IOERR, msg, "unable to open");
202 tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
205 /* check for "Aliasfile:" profile entry */
206 if ((cp = context_find("Aliasfile"))) {
210 for (ap=brkstring(dp=getcpy(cp), " ", "\n"); ap && *ap;
212 if ((state = alias(etcpath(*ap))) != AK_OK) {
213 adios(EX_IOERR, NULL, "aliasing error in file %s: %s",
214 *ap, akerror(state));
220 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
222 for (compnum = 1, state = FLD;;) {
223 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
226 putfmt(name, buf, out);
232 while (state == FLDPLUS) {
233 state = m_getfld(state, name, buf,
237 putfmt(name, cp, out);
243 fprintf(out, "\n%s", buf);
244 while (state == BODY) {
245 state = m_getfld(state, name, buf,
257 adios(EX_DATAERR, NULL, "message format error in component #%d",
261 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
280 process_bccs(tmpfil);
281 if (!(msgflags & MVIS)) {
282 /* only Bcc rcpts: we're finished here */
289 ** re-open the temp file, unlink it and exec sendmail, giving it
290 ** the msg temp file as std in.
292 if (!freopen(tmpfil, "r", stdin)) {
293 adios(EX_IOERR, tmpfil, "can't reopen for sendmail");
298 *argp++ = "send-mail";
299 *argp++ = "-m"; /* send to me too */
300 *argp++ = "-t"; /* read msg for recipients */
301 *argp++ = "-i"; /* don't stop on "." */
306 execv(sendmail, sargv);
307 adios(EX_OSERR, sendmail, "can't exec");
312 /* DRAFT GENERATION */
315 putfmt(char *name, char *str, FILE *out)
321 /* remove leading whitespace */
322 while (*str==' ' || *str=='\t') {
326 if ((i = get_header(name, hdrtab)) == NOTOK) {
327 /* no header we would care for */
328 if (mh_strcasecmp(name, attach_hdr)!=0 &&
329 mh_strcasecmp(name, sign_hdr)!=0 &&
330 mh_strcasecmp(name, enc_hdr)!=0) {
331 /* push it through */
332 fprintf(out, "%s: %s", name, str);
336 /* it's one of the interesting headers */
339 if (hdr->flags & HIGN || strcmp(str, "\n")==0) {
343 if (hdr->flags & HBAD) {
344 advise(NULL, "illegal header line -- %s:", name);
349 msgflags |= hdr->set;
351 if (hdr->flags & HFCC) {
356 if (hdr->flags & HBCC) {
357 struct mailname *mp = NULL;
359 /* Create list of Bcc addrs. */
360 while ((cp = getname(str))) {
361 mp = getm(cp, NULL, 0, AD_HOST, NULL);
362 mp->m_next = bccs; /* push */
368 if (aliasflg && hdr->flags & HTRY) {
370 ** This header contains address(es) that we have to do
371 ** alias expansion on. Because of the saved state in
372 ** getname we have to put all the addresses into a list.
374 struct mailname *f = NULL;
375 struct mailname *mp = NULL;
377 while ((cp = getname(str))) {
378 mp = getm(cp, NULL, 0, AD_HOST, NULL);
383 mp->m_next = f->m_next;
390 /* Now munch on the list, possibly expanding aliases */
396 ** The author(s) of spost decided that alias substitution wasn't
397 ** necessary for the non-HTRY headers. Unfortunately, one of
398 ** those headers is "From:", and having alias substitution work on
399 ** that is extremely useful for someone with a lot of POP3 email
400 ** accounts or aliases. post supports aliasing of "From:"...
402 ** Since "From:"-processing is incompletely implemented in this
403 ** unsupported and undocumented spost backend, I'm not going
404 ** to take the time to implement my new draft-From:-based email
405 ** address masquerading. If I do ever implement it here, I'd almost
406 ** certainly want to implement "From:" line alias processing as well.
407 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
410 ** Although there is no masquerading anymore in mmh, we might want
411 ** to have aliasing of From: addresses. Think about it.
412 ** -- meillo@marmaro.de 2012-02
415 if (hdr->set & MFRM) {
417 struct mailname *mp = NULL;
419 /* This is need because the addresse parser hold global state */
422 while ((cp = getname(str)) != NULL) {
423 mp = getm(cp, NULL, 0, AD_NAME, NULL);
430 if (hdr->flags & HSUB) {
431 subject = getcpy(str);
433 fprintf(out, "%s: %s", name, str);
438 ** Add yet missing headers.
441 finish_headers(FILE *out)
444 char from[BUFSIZ]; /* my network address */
445 char signature[BUFSIZ]; /* my signature */
446 char *resentstr = (msgstate == resent) ? "Resent-" : "";
448 if (!(msgflags & MDAT)) {
449 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
452 if ((cp = context_find("Default-From")) != NULL) {
453 snprintf(signature, sizeof(signature), "%s", cp);
455 snprintf(from, sizeof(from), "%s@%s", getusername(), LocalName());
456 if ((cp = getfullname()) && *cp) {
457 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
459 snprintf(signature, sizeof(signature), "%s", from);
462 if (!(msgflags & MFRM)) {
463 fprintf(out, "%sFrom: %s\n", resentstr, signature);
466 ** Add a Sender: header because the From: header could
467 ** be fake or contain multiple addresses.
469 if (!(msgflags & MFMM)) {
470 fprintf(out, "%sSender: %s\n", resentstr, signature);
473 if (!(msgflags & MVIS)) {
474 fprintf(out, "%sBcc: undisclosed-recipients:;\n", resentstr);
478 adios(EX_DATAERR, NULL, "re-format message and try again");
484 ** Return index of the requested header in the table, or NOTOK if missing.
487 get_header(char *header, struct headers *table)
491 for (h=table; h->value; h++) {
492 if (mh_strcasecmp(header, h->value)==0) {
502 ** output the address list for header "name". The address list
503 ** is a linked list of mailname structs. "nl" points to the head
504 ** of the list. Alias substitution should be done on nl.
507 putadr(char *name, struct mailname *nl)
509 struct mailname *mp, *mp2;
514 fprintf(out, "%s: ", name);
515 namelen = strlen(name) + 2;
518 for (mp = nl; mp; ) {
519 if (linepos > MAX_SM_FIELD) {
520 fprintf(out, "\n%s: ", name);
524 /* a local name - see if it's an alias */
525 cp = akvalue(mp->m_mbox);
526 if (cp == mp->m_mbox) {
527 /* wasn't an alias - use what the user typed */
528 linepos = putone(mp->m_text, linepos, namelen);
530 /* an alias - expand it */
531 while ((cp = getname(cp))) {
532 if (linepos > MAX_SM_FIELD) {
533 fprintf(out, "\n%s: ", name);
536 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
538 mp2->m_pers = getcpy(mp->m_mbox);
539 linepos = putone(adrformat(mp2), linepos, namelen);
541 linepos = putone(mp2->m_text,
549 /* not a local name - use what the user typed */
550 linepos = putone(mp->m_text, linepos, namelen);
560 putone(char *adr, int pos, int indent)
568 else if (linepos+len > OUTPUTLINELEN) {
569 fprintf(out, ",\n%*s", indent, "");
585 process_fcc(char *str)
590 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
591 adios(EX_DATAERR, NULL, "Too much Fcc data");
593 /* todo: better have three states: SEPARATOR, PLUS, WORD */
594 for (cp=pp=str; *cp; cp++) {
603 if (*pp=='+' || *pp=='@') {
623 fcc(char *file, char *folders)
629 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
633 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
634 adios(EX_DATAERR, NULL, "Too much Fcc data");
636 /* hack: read from /dev/null and refile(1) won't question us */
637 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
639 status = system(cmd);
641 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
642 msgstate == resent ? "Resent-" : "", folders);
643 } else if (status != 0) {
644 fprintf(stderr, "%sFcc %s: Problems occured.\n",
645 msgstate == resent ? "Resent-" : "", folders);
653 process_bccs(char *origmsg)
655 char *bccdraft = NULL;
656 struct mailname *mp = NULL;
659 for (mp=bccs; mp; mp=mp->m_next) {
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 if (execprogl("send", "send", bccdraft, (char *)NULL) != 0) {
668 admonish(invo_name, "Problems to send Bcc to %s",