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>
22 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
24 static struct swit switches[] = {
36 { "dist", -4 }, /* interface from dist */
41 /* flags for headers->flags */
42 #define HNOP 0x0000 /* just used to keep .set around */
43 #define HBAD 0x0001 /* bad header - don't let it through */
44 #define HADR 0x0002 /* header has an address field */
45 #define HSUB 0x0004 /* Subject: header */
46 #define HTRY 0x0008 /* try to send to addrs on header */
47 #define HBCC 0x0010 /* don't output this header */
48 #define HFCC 0x0020 /* FCC: type header */
49 #define HIGN 0x0040 /* ignore this header */
51 /* flags for headers->set */
52 #define MFRM 0x0001 /* we've seen a From: */
53 #define MDAT 0x0002 /* we've seen a Date: */
54 #define MRFM 0x0004 /* we've seen a Resent-From: */
55 #define MVIS 0x0008 /* we've seen sighted addrs */
56 #define MINV 0x0010 /* we've seen blind addrs */
57 #define MRDT 0x0020 /* we've seen a Resent-Date: */
65 static struct headers NHeaders[] = {
66 { "Return-Path", HBAD, 0 },
67 { "Received", HBAD, 0 },
68 { "Reply-To", HADR, 0 },
69 { "From", HADR, MFRM },
70 { "Sender", HADR|HBAD, 0 },
71 { "Date", HNOP, MDAT },
72 { "Subject", HSUB, 0 },
73 { "To", HADR|HTRY, MVIS },
74 { "Cc", HADR|HTRY, MVIS },
75 { "Bcc", HADR|HTRY|HBCC, MINV },
76 { "Message-Id", HBAD, 0 },
78 { "Envelope-From", HIGN, 0 },
82 static struct headers RHeaders[] = {
83 { "Resent-Reply-To", HADR, 0 },
84 { "Resent-From", HADR, MRFM },
85 { "Resent-Sender", HADR|HBAD, 0 },
86 { "Resent-Date", HNOP, MRDT },
87 { "Resent-Subject", HSUB, 0 },
88 { "Resent-To", HADR|HTRY, MVIS },
89 { "Resent-Cc", HADR|HTRY, MVIS },
90 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
91 { "Resent-Message-Id", HBAD, 0 },
92 { "Resent-Fcc", HFCC, 0 },
93 { "Reply-To", HADR, 0 },
95 { "Envelope-From", HIGN, 0 },
100 static int badmsg = 0;
101 static int verbose = 0;
102 static int debug = 0;
103 static int aliasflg = 0; /* if going to process aliases */
105 static unsigned msgflags = 0; /* what we've seen */
113 static char *subject = NULL; /* the subject field for BCC'ing */
114 static char fccs[BUFSIZ] = "";
115 struct mailname *bccs = NULL; /* list of the bcc recipients */
117 static struct headers *hdrtab; /* table for the message we're doing */
118 static FILE *out; /* output (temp) file */
123 static void putfmt(char *, char *, FILE *);
124 static void finish_headers(FILE *);
125 static int get_header(char *, struct headers *);
126 static void putadr(char *, struct mailname *);
127 static int putone(char *, int, int);
128 static void process_fcc(char *);
129 static void fcc(char *, char *);
130 static void process_bccs(char *);
134 main(int argc, char **argv)
137 char *cp, *msg = NULL, **argp, **arguments;
138 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
141 setlocale(LC_ALL, "");
142 invo_name = mhbasename(argv[0]);
146 arguments = getarguments(invo_name, argc, argv, 0);
149 while ((cp = *argp++)) {
151 switch (smatch(++cp, switches)) {
153 ambigsw(cp, switches);
156 adios(NULL, "-%s unknown", cp);
159 snprintf(buf, sizeof(buf),
160 "%s [switches] file",
162 print_help(buf, switches, 1);
165 print_version(invo_name);
185 adios(NULL, "only one message at a time!");
191 adios(NULL, "usage: %s [switches] file", invo_name);
193 if ((in = fopen(msg, "r")) == NULL)
194 adios(msg, "unable to open");
200 tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
203 /* check for "Aliasfile:" profile entry */
204 if ((cp = context_find("Aliasfile"))) {
208 for (ap=brkstring(dp=getcpy(cp), " ", "\n"); ap && *ap;
210 if ((state = alias(etcpath(*ap))) != AK_OK) {
211 adios(NULL, "aliasing error in file %s: %s",
212 *ap, akerror(state));
218 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
220 for (compnum = 1, state = FLD;;) {
221 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
224 putfmt(name, buf, out);
230 while (state == FLDPLUS) {
231 state = m_getfld(state, name, buf,
235 putfmt(name, cp, out);
241 fprintf(out, "\n%s", buf);
242 while (state == BODY) {
243 state = m_getfld(state, name, buf,
255 adios(NULL, "message format error in component #%d",
259 adios(NULL, "getfld() returned %d", state);
278 process_bccs(tmpfil);
279 if (!(msgflags & MVIS)) {
280 /* only Bcc rcpts: we're finished here */
287 ** re-open the temp file, unlink it and exec sendmail, giving it
288 ** the msg temp file as std in.
290 if (!freopen(tmpfil, "r", stdin)) {
291 adios(tmpfil, "can't reopen for sendmail");
296 *argp++ = "send-mail";
297 *argp++ = "-m"; /* send to me too */
298 *argp++ = "-t"; /* read msg for recipients */
299 *argp++ = "-i"; /* don't stop on "." */
304 execv(sendmail, sargv);
305 adios(sendmail, "can't exec");
310 /* DRAFT GENERATION */
313 putfmt(char *name, char *str, FILE *out)
319 /* remove leading whitespace */
320 while (*str==' ' || *str=='\t') {
324 if ((i = get_header(name, hdrtab)) == NOTOK) {
325 /* no header we would care for */
326 if (mh_strcasecmp(name, attach_hdr)!=0 &&
327 mh_strcasecmp(name, sign_hdr)!=0 &&
328 mh_strcasecmp(name, enc_hdr)!=0) {
329 /* push it through */
330 fprintf(out, "%s: %s", name, str);
334 /* it's one of the interesting headers */
337 if (hdr->flags & HIGN || strcmp(str, "\n")==0) {
341 if (hdr->flags & HBAD) {
342 advise(NULL, "illegal header line -- %s:", name);
347 msgflags |= hdr->set;
349 if (hdr->flags & HFCC) {
354 if (hdr->flags & HBCC) {
355 struct mailname *mp = NULL;
357 /* Create list of Bcc addrs. */
358 while ((cp = getname(str))) {
359 mp = getm(cp, NULL, 0, AD_HOST, NULL);
360 mp->m_next = bccs; /* push */
366 if (aliasflg && hdr->flags & HTRY) {
368 ** This header contains address(es) that we have to do
369 ** alias expansion on. Because of the saved state in
370 ** getname we have to put all the addresses into a list.
372 struct mailname *f = NULL;
373 struct mailname *mp = NULL;
375 while ((cp = getname(str))) {
376 mp = getm(cp, NULL, 0, AD_HOST, NULL);
381 mp->m_next = f->m_next;
388 /* Now munch on the list, possibly expanding aliases */
394 ** The author(s) of spost decided that alias substitution wasn't
395 ** necessary for the non-HTRY headers. Unfortunately, one of
396 ** those headers is "From:", and having alias substitution work on
397 ** that is extremely useful for someone with a lot of POP3 email
398 ** accounts or aliases. post supports aliasing of "From:"...
400 ** Since "From:"-processing is incompletely implemented in this
401 ** unsupported and undocumented spost backend, I'm not going
402 ** to take the time to implement my new draft-From:-based email
403 ** address masquerading. If I do ever implement it here, I'd almost
404 ** certainly want to implement "From:" line alias processing as well.
405 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
408 ** Although there is no masquerading anymore in mmh, we might want
409 ** to have aliasing of From: addresses. Think about it.
410 ** -- meillo@marmaro.de 2012-02
413 if (hdr->flags & HSUB) {
414 subject = getcpy(str);
416 fprintf(out, "%s: %s", name, str);
421 ** Add yet missing headers.
424 finish_headers(FILE *out)
427 char from[BUFSIZ]; /* my network address */
428 char signature[BUFSIZ]; /* my signature */
429 char *resentstr = (msgstate == resent) ? "Resent-" : "";
431 if (!(msgflags & MDAT)) {
432 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
435 snprintf(from, sizeof(from), "%s@%s", getusername(), LocalName());
436 if ((cp = getfullname()) && *cp) {
437 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
439 snprintf(signature, sizeof(signature), "%s", from);
441 if (!(msgflags & MFRM)) {
442 fprintf(out, "%sFrom: %s\n", resentstr, signature);
445 ** Add a Sender: header because the From: header could
446 ** be fake or contain multiple addresses.
448 fprintf(out, "%sSender: %s\n", resentstr, from);
450 if (!(msgflags & MVIS)) {
451 fprintf(out, "%sBcc: undisclosed-recipients:;\n", resentstr);
455 adios(NULL, "re-format message and try again");
461 ** Return index of the requested header in the table, or NOTOK if missing.
464 get_header(char *header, struct headers *table)
468 for (h=table; h->value; h++) {
469 if (mh_strcasecmp(header, h->value)==0) {
479 ** output the address list for header "name". The address list
480 ** is a linked list of mailname structs. "nl" points to the head
481 ** of the list. Alias substitution should be done on nl.
484 putadr(char *name, struct mailname *nl)
486 struct mailname *mp, *mp2;
491 fprintf(out, "%s: ", name);
492 namelen = strlen(name) + 2;
495 for (mp = nl; mp; ) {
496 if (linepos > MAX_SM_FIELD) {
497 fprintf(out, "\n%s: ", name);
501 /* a local name - see if it's an alias */
502 cp = akvalue(mp->m_mbox);
503 if (cp == mp->m_mbox) {
504 /* wasn't an alias - use what the user typed */
505 linepos = putone(mp->m_text, linepos, namelen);
507 /* an alias - expand it */
508 while ((cp = getname(cp))) {
509 if (linepos > MAX_SM_FIELD) {
510 fprintf(out, "\n%s: ", name);
513 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
515 mp2->m_pers = getcpy(mp->m_mbox);
516 linepos = putone(adrformat(mp2), linepos, namelen);
518 linepos = putone(mp2->m_text,
526 /* not a local name - use what the user typed */
527 linepos = putone(mp->m_text, linepos, namelen);
537 putone(char *adr, int pos, int indent)
545 else if (linepos+len > OUTPUTLINELEN) {
546 fprintf(out, ",\n%*s", indent, "");
562 process_fcc(char *str)
567 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
568 adios(NULL, "Too much Fcc data");
570 /* todo: better have three states: SEPARATOR, PLUS, WORD */
571 for (cp=pp=str; *cp; cp++) {
580 if (*pp=='+' || *pp=='@') {
600 fcc(char *file, char *folders)
606 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
610 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
611 adios(NULL, "Too much Fcc data");
613 /* hack: read from /dev/null and refile(1) won't question us */
614 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
616 status = system(cmd);
618 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
619 msgstate == resent ? "Resent-" : "", folders);
620 } else if (status != 0) {
621 fprintf(stderr, "%sFcc %s: Problems occured.\n",
622 msgstate == resent ? "Resent-" : "", folders);
630 process_bccs(char *origmsg)
632 char *bccdraft = NULL;
633 struct mailname *mp = NULL;
636 for (mp=bccs; mp; mp=mp->m_next) {
637 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
638 fprintf(out, "To: %s\n", mp->m_text);
639 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
640 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
641 fprintf(out, "------------\n");
644 if (execprogl("send", "send", bccdraft, (char *)NULL) != 0) {
645 admonish(invo_name, "Problems to send Bcc to %s",