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: */
66 static struct headers NHeaders[] = {
67 { "Return-Path", HBAD, 0 },
68 { "Received", HBAD, 0 },
69 { "Reply-To", HADR, 0 },
70 { "From", HADR, MFRM },
71 { "Sender", HADR|HBAD, 0 },
72 { "Date", HNOP, MDAT },
73 { "Subject", HSUB, 0 },
74 { "To", HADR|HTRY, MVIS },
75 { "Cc", HADR|HTRY, MVIS },
76 { "Bcc", HADR|HTRY|HBCC, MINV },
77 { "Message-Id", HBAD, 0 },
79 { "Envelope-From", HIGN, 0 },
83 static struct headers RHeaders[] = {
84 { "Resent-Reply-To", HADR, 0 },
85 { "Resent-From", HADR, MRFM },
86 { "Resent-Sender", HADR|HBAD, 0 },
87 { "Resent-Date", HNOP, MRDT },
88 { "Resent-Subject", HSUB, 0 },
89 { "Resent-To", HADR|HTRY, MVIS },
90 { "Resent-Cc", HADR|HTRY, MVIS },
91 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
92 { "Resent-Message-Id", HBAD, 0 },
93 { "Resent-Fcc", HFCC, 0 },
94 { "Reply-To", HADR, 0 },
96 { "Envelope-From", HIGN, 0 },
101 static int badmsg = 0;
102 static int verbose = 0;
103 static int debug = 0;
104 static int aliasflg = 0; /* if going to process aliases */
106 static unsigned msgflags = 0; /* what we've seen */
114 static char *subject = NULL; /* the subject field for BCC'ing */
115 static char fccs[BUFSIZ] = "";
116 struct mailname *bccs = NULL; /* list of the bcc recipients */
118 static struct headers *hdrtab; /* table for the message we're doing */
119 static FILE *out; /* output (temp) file */
124 static void putfmt(char *, char *, FILE *);
125 static void finish_headers(FILE *);
126 static int get_header(char *, struct headers *);
127 static void putadr(char *, struct mailname *);
128 static int putone(char *, int, int);
129 static void process_fcc(char *);
130 static void fcc(char *, char *);
131 static void process_bccs(char *);
135 main(int argc, char **argv)
138 char *cp, *msg = NULL, **argp, **arguments;
139 char *sargv[16], buf[BUFSIZ], name[NAMESZ];
142 setlocale(LC_ALL, "");
143 invo_name = mhbasename(argv[0]);
147 arguments = getarguments(invo_name, argc, argv, 0);
150 while ((cp = *argp++)) {
152 switch (smatch(++cp, switches)) {
154 ambigsw(cp, switches);
157 adios(EX_USAGE, NULL, "-%s unknown", cp);
160 snprintf(buf, sizeof(buf),
161 "%s [switches] file",
163 print_help(buf, switches, 1);
164 exit(argc == 2 ? EX_OK : EX_USAGE);
166 print_version(invo_name);
167 exit(argc == 2 ? EX_OK : EX_USAGE);
186 adios(EX_USAGE, NULL, "only one message at a time!");
192 adios(EX_USAGE, NULL, "usage: %s [switches] file", invo_name);
194 if ((in = fopen(msg, "r")) == NULL)
195 adios(EX_IOERR, msg, "unable to open");
201 tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
204 /* check for "Aliasfile:" profile entry */
205 if ((cp = context_find("Aliasfile"))) {
209 for (ap=brkstring(dp=getcpy(cp), " ", "\n"); ap && *ap;
211 if ((state = alias(etcpath(*ap))) != AK_OK) {
212 adios(EX_IOERR, NULL, "aliasing error in file %s: %s",
213 *ap, akerror(state));
219 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
221 for (compnum = 1, state = FLD;;) {
222 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
225 putfmt(name, buf, out);
231 while (state == FLDPLUS) {
232 state = m_getfld(state, name, buf,
236 putfmt(name, cp, out);
242 fprintf(out, "\n%s", buf);
243 while (state == BODY) {
244 state = m_getfld(state, name, buf,
256 adios(EX_DATAERR, NULL, "message format error in component #%d",
260 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
279 process_bccs(tmpfil);
280 if (!(msgflags & MVIS)) {
281 /* only Bcc rcpts: we're finished here */
288 ** re-open the temp file, unlink it and exec sendmail, giving it
289 ** the msg temp file as std in.
291 if (!freopen(tmpfil, "r", stdin)) {
292 adios(EX_IOERR, tmpfil, "can't reopen for sendmail");
297 *argp++ = "send-mail";
298 *argp++ = "-m"; /* send to me too */
299 *argp++ = "-t"; /* read msg for recipients */
300 *argp++ = "-i"; /* don't stop on "." */
305 execv(sendmail, sargv);
306 adios(EX_OSERR, sendmail, "can't exec");
311 /* DRAFT GENERATION */
314 putfmt(char *name, char *str, FILE *out)
320 /* remove leading whitespace */
321 while (*str==' ' || *str=='\t') {
325 if ((i = get_header(name, hdrtab)) == NOTOK) {
326 /* no header we would care for */
327 if (mh_strcasecmp(name, attach_hdr)!=0 &&
328 mh_strcasecmp(name, sign_hdr)!=0 &&
329 mh_strcasecmp(name, enc_hdr)!=0) {
330 /* push it through */
331 fprintf(out, "%s: %s", name, str);
335 /* it's one of the interesting headers */
338 if (hdr->flags & HIGN || strcmp(str, "\n")==0) {
342 if (hdr->flags & HBAD) {
343 advise(NULL, "illegal header line -- %s:", name);
348 msgflags |= hdr->set;
350 if (hdr->flags & HFCC) {
355 if (hdr->flags & HBCC) {
356 struct mailname *mp = NULL;
358 /* Create list of Bcc addrs. */
359 while ((cp = getname(str))) {
360 mp = getm(cp, NULL, 0, AD_HOST, NULL);
361 mp->m_next = bccs; /* push */
367 if (aliasflg && hdr->flags & HTRY) {
369 ** This header contains address(es) that we have to do
370 ** alias expansion on. Because of the saved state in
371 ** getname we have to put all the addresses into a list.
373 struct mailname *f = NULL;
374 struct mailname *mp = NULL;
376 while ((cp = getname(str))) {
377 mp = getm(cp, NULL, 0, AD_HOST, NULL);
382 mp->m_next = f->m_next;
389 /* Now munch on the list, possibly expanding aliases */
395 ** The author(s) of spost decided that alias substitution wasn't
396 ** necessary for the non-HTRY headers. Unfortunately, one of
397 ** those headers is "From:", and having alias substitution work on
398 ** that is extremely useful for someone with a lot of POP3 email
399 ** accounts or aliases. post supports aliasing of "From:"...
401 ** Since "From:"-processing is incompletely implemented in this
402 ** unsupported and undocumented spost backend, I'm not going
403 ** to take the time to implement my new draft-From:-based email
404 ** address masquerading. If I do ever implement it here, I'd almost
405 ** certainly want to implement "From:" line alias processing as well.
406 ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
409 ** Although there is no masquerading anymore in mmh, we might want
410 ** to have aliasing of From: addresses. Think about it.
411 ** -- meillo@marmaro.de 2012-02
414 if (hdr->flags & HSUB) {
415 subject = getcpy(str);
417 fprintf(out, "%s: %s", name, str);
422 ** Add yet missing headers.
425 finish_headers(FILE *out)
428 char from[BUFSIZ]; /* my network address */
429 char signature[BUFSIZ]; /* my signature */
430 char *resentstr = (msgstate == resent) ? "Resent-" : "";
432 if (!(msgflags & MDAT)) {
433 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
436 snprintf(from, sizeof(from), "%s@%s", getusername(), LocalName());
437 if ((cp = getfullname()) && *cp) {
438 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
440 snprintf(signature, sizeof(signature), "%s", from);
442 if (!(msgflags & MFRM)) {
443 fprintf(out, "%sFrom: %s\n", resentstr, signature);
446 ** Add a Sender: header because the From: header could
447 ** be fake or contain multiple addresses.
449 fprintf(out, "%sSender: %s\n", resentstr, from);
451 if (!(msgflags & MVIS)) {
452 fprintf(out, "%sBcc: undisclosed-recipients:;\n", resentstr);
456 adios(EX_DATAERR, NULL, "re-format message and try again");
462 ** Return index of the requested header in the table, or NOTOK if missing.
465 get_header(char *header, struct headers *table)
469 for (h=table; h->value; h++) {
470 if (mh_strcasecmp(header, h->value)==0) {
480 ** output the address list for header "name". The address list
481 ** is a linked list of mailname structs. "nl" points to the head
482 ** of the list. Alias substitution should be done on nl.
485 putadr(char *name, struct mailname *nl)
487 struct mailname *mp, *mp2;
492 fprintf(out, "%s: ", name);
493 namelen = strlen(name) + 2;
496 for (mp = nl; mp; ) {
497 if (linepos > MAX_SM_FIELD) {
498 fprintf(out, "\n%s: ", name);
502 /* a local name - see if it's an alias */
503 cp = akvalue(mp->m_mbox);
504 if (cp == mp->m_mbox) {
505 /* wasn't an alias - use what the user typed */
506 linepos = putone(mp->m_text, linepos, namelen);
508 /* an alias - expand it */
509 while ((cp = getname(cp))) {
510 if (linepos > MAX_SM_FIELD) {
511 fprintf(out, "\n%s: ", name);
514 mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
516 mp2->m_pers = getcpy(mp->m_mbox);
517 linepos = putone(adrformat(mp2), linepos, namelen);
519 linepos = putone(mp2->m_text,
527 /* not a local name - use what the user typed */
528 linepos = putone(mp->m_text, linepos, namelen);
538 putone(char *adr, int pos, int indent)
546 else if (linepos+len > OUTPUTLINELEN) {
547 fprintf(out, ",\n%*s", indent, "");
563 process_fcc(char *str)
568 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
569 adios(EX_DATAERR, NULL, "Too much Fcc data");
571 /* todo: better have three states: SEPARATOR, PLUS, WORD */
572 for (cp=pp=str; *cp; cp++) {
581 if (*pp=='+' || *pp=='@') {
601 fcc(char *file, char *folders)
607 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
611 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
612 adios(EX_DATAERR, NULL, "Too much Fcc data");
614 /* hack: read from /dev/null and refile(1) won't question us */
615 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
617 status = system(cmd);
619 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
620 msgstate == resent ? "Resent-" : "", folders);
621 } else if (status != 0) {
622 fprintf(stderr, "%sFcc %s: Problems occured.\n",
623 msgstate == resent ? "Resent-" : "", folders);
631 process_bccs(char *origmsg)
633 char *bccdraft = NULL;
634 struct mailname *mp = NULL;
637 for (mp=bccs; mp; mp=mp->m_next) {
638 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
639 fprintf(out, "To: %s\n", mp->m_text);
640 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
641 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
642 fprintf(out, "------------\n");
645 if (execprogl("send", "send", bccdraft, (char *)NULL) != 0) {
646 admonish(invo_name, "Problems to send Bcc to %s",