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 */
118 struct mailname *sender = NULL;
120 static struct headers *hdrtab; /* table for the message we're doing */
121 static FILE *out; /* output (temp) file */
126 static void putfmt(char *, char *, FILE *);
127 static void finish_headers(FILE *);
128 static int get_header(char *, struct headers *);
129 static void putadr(char *, struct mailname *);
130 static int putone(char *, int, int);
131 static void process_fcc(char *);
132 static void fcc(char *, char *);
133 static void process_bccs(char *);
137 main(int argc, char **argv)
140 char *cp, *msg = NULL, **argp, **arguments;
141 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
144 setlocale(LC_ALL, "");
145 invo_name = mhbasename(argv[0]);
149 arguments = getarguments(invo_name, argc, argv, 0);
152 while ((cp = *argp++)) {
154 switch (smatch(++cp, switches)) {
156 ambigsw(cp, switches);
159 adios(EX_USAGE, NULL, "-%s unknown", cp);
162 snprintf(buf, sizeof(buf),
163 "%s [switches] file",
165 print_help(buf, switches, 1);
166 exit(argc == 2 ? EX_OK : EX_USAGE);
168 print_version(invo_name);
169 exit(argc == 2 ? EX_OK : EX_USAGE);
188 adios(EX_USAGE, NULL, "only one message at a time!");
194 adios(EX_USAGE, NULL, "usage: %s [switches] file", invo_name);
196 if ((in = fopen(msg, "r")) == NULL)
197 adios(EX_IOERR, msg, "unable to open");
203 tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
206 /* check for "Aliasfile:" profile entry */
207 if ((cp = context_find("Aliasfile"))) {
211 for (ap=brkstring(dp=getcpy(cp), " ", "\n"); ap && *ap;
213 if ((state = alias(etcpath(*ap))) != AK_OK) {
214 adios(EX_IOERR, NULL, "aliasing error in file %s: %s",
215 *ap, akerror(state));
221 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
223 for (compnum = 1, state = FLD;;) {
224 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
229 while (state == FLDPLUS) {
230 state = m_getfld(state, name, buf,
234 putfmt(name, cp, out);
240 fprintf(out, "\n%s", buf);
241 while (state == BODY) {
242 state = m_getfld(state, name, buf,
254 adios(EX_DATAERR, NULL, "message format error in component #%d",
258 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
277 process_bccs(tmpfil);
278 if (!(msgflags & MVIS)) {
279 /* only Bcc rcpts: we're finished here */
286 ** re-open the temp file, unlink it and exec sendmail, giving it
287 ** the msg temp file as std in.
289 if (!freopen(tmpfil, "r", stdin)) {
290 adios(EX_IOERR, tmpfil, "can't reopen for sendmail");
295 *argp++ = "send-mail";
296 *argp++ = "-m"; /* send to me too */
297 *argp++ = "-t"; /* read msg for recipients */
298 *argp++ = "-i"; /* don't stop on "." */
303 execv(sendmail, sargv);
304 adios(EX_OSERR, sendmail, "can't exec");
309 /* DRAFT GENERATION */
312 putfmt(char *name, char *str, FILE *out)
318 /* remove leading whitespace */
319 while (*str==' ' || *str=='\t') {
323 if ((i = get_header(name, hdrtab)) == NOTOK) {
324 /* no header we would care for */
325 if (mh_strcasecmp(name, attach_hdr)!=0 &&
326 mh_strcasecmp(name, sign_hdr)!=0 &&
327 mh_strcasecmp(name, enc_hdr)!=0) {
328 /* push it through */
329 fprintf(out, "%s: %s", name, str);
333 /* it's one of the interesting headers */
336 if (hdr->flags & HIGN || strcmp(str, "\n")==0) {
340 if (hdr->flags & HBAD) {
341 advise(NULL, "illegal header line -- %s:", name);
346 msgflags |= hdr->set;
348 if (hdr->flags & HFCC) {
353 if (hdr->flags & HBCC) {
354 struct mailname *mp = NULL;
356 /* Create list of Bcc addrs. */
357 while ((cp = getname(str))) {
358 mp = getm(cp, NULL, 0, AD_HOST, NULL);
359 mp->m_next = bccs; /* push */
365 if (aliasflg && hdr->flags & HTRY) {
367 ** This header contains address(es) that we have to do
368 ** alias expansion on. Because of the saved state in
369 ** getname we have to put all the addresses into a list.
371 struct mailname *f = NULL;
372 struct mailname *mp = NULL;
374 while ((cp = getname(str))) {
375 mp = getm(cp, NULL, 0, AD_HOST, NULL);
380 mp->m_next = f->m_next;
387 /* Now munch on the list, possibly expanding aliases */
393 ** The author(s) of spost decided that alias substitution wasn't
394 ** necessary for the non-HTRY headers. Unfortunately, one of
395 ** those headers is "From:", and having alias substitution work on
396 ** that is extremely useful for someone with a lot of POP3 email
397 ** accounts or aliases. post supports aliasing of "From:"...
399 ** Since "From:"-processing is incompletely implemented in this
400 ** unsupported and undocumented spost backend, I'm not going
401 ** to take the time to implement my new draft-From:-based email
402 ** address masquerading. If I do ever implement it here, I'd almost
403 ** certainly want to implement "From:" line alias processing as well.
404 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
407 ** Although there is no masquerading anymore in mmh, we might want
408 ** to have aliasing of From: addresses. Think about it.
409 ** -- meillo@marmaro.de 2012-02
412 if (hdr->set & MFRM) {
414 struct mailname *mp = NULL;
415 struct mailname *my = NULL;
416 unsigned int fromcnt = 0;
418 /* This is need because the addresse parser hold global state */
421 while ((cp = getname(str)) != NULL) {
423 mp = getm(cp, NULL, 0, AD_NAME, NULL);
449 if (hdr->flags & HSUB) {
450 subject = getcpy(str);
452 fprintf(out, "%s: %s", name, str);
457 ** Add yet missing headers.
460 finish_headers(FILE *out)
463 char from[BUFSIZ]; /* my network address */
464 char signature[BUFSIZ]; /* my signature */
465 char *resentstr = (msgstate == resent) ? "Resent-" : "";
467 if (!(msgflags & MDAT)) {
468 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
471 if (sender != NULL) {
472 snprintf(signature, sizeof(signature), "%s", sender->m_text);
473 } else if ((cp = context_find("Default-From")) != NULL) {
474 snprintf(signature, sizeof(signature), "%s", cp);
476 snprintf(from, sizeof(from), "%s@%s", getusername(), LocalName());
477 if ((cp = getfullname()) && *cp) {
478 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
480 snprintf(signature, sizeof(signature), "%s", from);
483 if (!(msgflags & MFRM)) {
484 fprintf(out, "%sFrom: %s\n", resentstr, signature);
487 ** Add a Sender: header because the From: header could
488 ** be fake or contain multiple addresses.
490 if (!(msgflags & MFMM) || sender != NULL) {
491 fprintf(out, "%sSender: %s\n", resentstr, signature);
494 if (!(msgflags & MVIS)) {
495 fprintf(out, "%sBcc: undisclosed-recipients:;\n", resentstr);
499 adios(EX_DATAERR, NULL, "re-format message and try again");
505 ** Return index of the requested header in the table, or NOTOK if missing.
508 get_header(char *header, struct headers *table)
512 for (h=table; h->value; h++) {
513 if (mh_strcasecmp(header, h->value)==0) {
523 ** output the address list for header "name". The address list
524 ** is a linked list of mailname structs. "nl" points to the head
525 ** of the list. Alias substitution should be done on nl.
528 putadr(char *name, struct mailname *nl)
530 struct mailname *mp, *mp2;
535 fprintf(out, "%s: ", name);
536 namelen = strlen(name) + 2;
539 for (mp = nl; mp; ) {
540 if (linepos > MAX_SM_FIELD) {
541 fprintf(out, "\n%s: ", name);
545 /* a local name - see if it's an alias */
546 cp = akvalue(mp->m_mbox);
547 if (cp == mp->m_mbox) {
548 /* wasn't an alias - use what the user typed */
549 linepos = putone(mp->m_text, linepos, namelen);
551 /* an alias - expand it */
552 while ((cp = getname(cp))) {
553 if (linepos > MAX_SM_FIELD) {
554 fprintf(out, "\n%s: ", name);
557 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
559 mp2->m_pers = getcpy(mp->m_mbox);
560 linepos = putone(adrformat(mp2), linepos, namelen);
562 linepos = putone(mp2->m_text,
570 /* not a local name - use what the user typed */
571 linepos = putone(mp->m_text, linepos, namelen);
581 putone(char *adr, int pos, int indent)
589 else if (linepos+len > OUTPUTLINELEN) {
590 fprintf(out, ",\n%*s", indent, "");
606 process_fcc(char *str)
611 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
612 adios(EX_DATAERR, NULL, "Too much Fcc data");
614 /* todo: better have three states: SEPARATOR, PLUS, WORD */
615 for (cp=pp=str; *cp; cp++) {
624 if (*pp=='+' || *pp=='@') {
644 fcc(char *file, char *folders)
650 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
654 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
655 adios(EX_DATAERR, NULL, "Too much Fcc data");
657 /* hack: read from /dev/null and refile(1) won't question us */
658 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
660 status = system(cmd);
662 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
663 msgstate == resent ? "Resent-" : "", folders);
664 } else if (status != 0) {
665 fprintf(stderr, "%sFcc %s: Problems occured.\n",
666 msgstate == resent ? "Resent-" : "", folders);
674 process_bccs(char *origmsg)
676 char *bccdraft = NULL;
677 struct mailname *mp = NULL;
680 for (mp=bccs; mp; mp=mp->m_next) {
681 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
682 fprintf(out, "To: %s\n", mp->m_text);
683 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
684 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
685 fprintf(out, "------------\n");
688 if (execprogl("send", "send", bccdraft, (char *)NULL) != 0) {
689 admonish(invo_name, "Problems to send Bcc to %s",