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>
24 #define MAX_SM_FIELD 1476 /* < largest hdr field sendmail will accept */
26 static struct swit switches[] = {
38 { "dist", -4 }, /* interface from dist */
42 char *version=VERSION;
44 /* flags for headers->flags */
45 #define HNOP 0x0000 /* just used to keep .set around */
46 #define HBAD 0x0001 /* bad header - don't let it through */
47 #define HADR 0x0002 /* header has an address field */
48 #define HSUB 0x0004 /* Subject: header */
49 #define HTRY 0x0008 /* try to send to addrs on header */
50 #define HBCC 0x0010 /* don't output this header */
51 #define HFCC 0x0020 /* FCC: type header */
52 #define HIGN 0x0040 /* ignore this header */
53 #define HDCC 0x0080 /* DCC: type header */
55 /* flags for headers->set */
56 #define MFRM 0x0001 /* we've seen a From: */
57 #define MDAT 0x0002 /* we've seen a Date: */
58 #define MRFM 0x0004 /* we've seen a Resent-From: */
59 #define MVIS 0x0008 /* we've seen sighted addrs */
60 #define MINV 0x0010 /* we've seen blind addrs */
61 #define MRDT 0x0020 /* we've seen a Resent-Date: */
62 #define MFMM 0x0040 /* The Mail is From a Alternative-Mailbox Addresse */
70 static struct headers NHeaders[] = {
71 { "Return-Path", HBAD, 0 },
72 { "Received", HBAD, 0 },
73 { "Reply-To", HADR, 0 },
74 { "From", HADR, MFRM },
75 { "Sender", HADR|HBAD, 0 },
76 { "Date", HNOP, MDAT },
77 { "Subject", HSUB, 0 },
78 { "To", HADR|HTRY, MVIS },
79 { "Cc", HADR|HTRY, MVIS },
80 { "Dcc", HADR|HTRY|HDCC, MINV },
81 { "Bcc", HADR|HTRY|HBCC, MINV },
82 { "Message-Id", HBAD, 0 },
84 { "Envelope-From", HIGN, 0 },
88 static struct headers RHeaders[] = {
89 { "Resent-Reply-To", HADR, 0 },
90 { "Resent-From", HADR, MRFM },
91 { "Resent-Sender", HADR|HBAD, 0 },
92 { "Resent-Date", HNOP, MRDT },
93 { "Resent-Subject", HSUB, 0 },
94 { "Resent-To", HADR|HTRY, MVIS },
95 { "Resent-Cc", HADR|HTRY, MVIS },
96 { "Resent-Dcc", HADR|HTRY|HDCC, MINV },
97 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
98 { "Resent-Message-Id", HBAD, 0 },
99 { "Resent-Fcc", HFCC, 0 },
100 { "Reply-To", HADR, 0 },
102 { "Envelope-From", HIGN, 0 },
107 static int badmsg = 0;
108 static int verbose = 0;
109 static int debug = 0;
110 static int aliasflg = 0; /* if going to process aliases */
112 static unsigned msgflags = 0; /* what we've seen */
120 static char *subject = NULL; /* the subject field for BCC'ing */
121 static struct mailname *from = NULL; /* the from field for BCC'ing */
122 static char fccs[BUFSIZ] = "";
123 struct mailname *bccs = NULL; /* list of the bcc recipients */
124 struct mailname *recipients = NULL; /* list of the recipients */
125 size_t recipientsc = 0;
126 struct mailname *sender = NULL;
128 static struct headers *hdrtab; /* table for the message we're doing */
129 static FILE *out; /* output (temp) file */
134 static void putfmt(char *, char *, FILE *);
135 static void finish_headers(FILE *);
136 static int get_header(char *, struct headers *);
137 static void putadr(char *, struct mailname *);
138 static int putone(char *, int, int);
139 static void process_fcc(char *);
140 static void fcc(char *, char *);
141 static void process_bccs(char *);
142 static size_t do_aliasing(struct mailname *, struct mailname **);
146 main(int argc, char **argv)
149 struct field f = {{0}};
151 char *cp, *msg = NULL, **argp, **arguments;
152 char **sargv, buf[BUFSIZ];
155 setlocale(LC_ALL, "");
156 invo_name = mhbasename(argv[0]);
160 arguments = getarguments(invo_name, argc, argv, 0);
163 while ((cp = *argp++)) {
165 switch (smatch(++cp, switches)) {
167 ambigsw(cp, switches);
170 adios(EX_USAGE, NULL, "-%s unknown", cp);
173 snprintf(buf, sizeof(buf),
174 "%s [switches] file",
176 print_help(buf, switches, 1);
177 exit(argc == 2 ? EX_OK : EX_USAGE);
179 print_version(invo_name);
180 exit(argc == 2 ? EX_OK : EX_USAGE);
199 adios(EX_USAGE, NULL, "only one message at a time!");
206 adios(EX_USAGE, NULL, "usage: %s [switches] file", invo_name);
209 if ((in = fopen(msg, "r")) == NULL) {
210 adios(EX_IOERR, msg, "unable to open");
217 tmpfil = mh_xstrdup(m_mktemp2("/tmp/", invo_name, NULL, &out));
220 /* check for "Aliasfile:" profile entry */
221 if ((cp = context_find("Aliasfile"))) {
225 for (ap=brkstring(dp=mh_xstrdup(cp), " ", "\n"); ap && *ap;
227 if ((state = alias(etcpath(*ap))) != AK_OK) {
228 adios(EX_IOERR, NULL, "aliasing error in file %s: %s",
229 *ap, akerror(state));
235 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
237 for (compnum = 1, state = FLD2;;) {
238 switch (state = m_getfld2(state, &f, in)) {
241 putfmt(f.name, f.value, out);
246 fprintf(out, "\n%s", f.value);
247 while ((state = m_getfld2(state, &f, in)) == BODY2) {
248 if (f.valuelen >= NAMESZ) {
249 adios(EX_DATAERR, NULL, "Body contains a to long line");
262 adios(EX_DATAERR, NULL, "message format error in component #%d",
266 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
272 if (state != FILEEOF2) {
273 adios(EX_IOERR, "m_getfld2", "Error while reading body");
277 struct mailname *i = recipients;
281 fputs(i->m_mbox, stdout);
284 fputs(i->m_host, stdout);
302 process_bccs(tmpfil);
303 if (!(msgflags & MVIS)) {
304 /* only Bcc rcpts: we're finished here */
311 ** re-open the temp file, unlink it and exec sendmail, giving it
312 ** the msg temp file as std in.
314 if (!freopen(tmpfil, "r", stdin)) {
315 adios(EX_IOERR, tmpfil, "can't reopen for sendmail");
319 if (recipientsc == 0) {
320 adios(EX_DATAERR, NULL, "message has no recipients");
323 sargv = mh_xcalloc(recipientsc + 4, sizeof(char **));
326 *argp++ = "send-mail";
327 *argp++ = "-i"; /* don't stop on "." */
332 while (recipients != NULL) {
333 cp = mh_xstrdup(recipients->m_mbox);
334 if (recipients->m_host) {
336 cp = add(recipients->m_host, cp);
340 recipients = recipients->m_next;
343 execvp(sendmail, sargv);
345 if (errno == E2BIG) {
346 adios(EX_DATAERR, sendmail, "too much arguments, probably to much recipients");
349 adios(EX_OSERR, sendmail, "can't exec");
354 /* DRAFT GENERATION */
357 putfmt(char *name, char *str, FILE *out)
361 struct mailname addr_start, *addr_end;
365 addr_end = &addr_start;
366 addr_end->m_next = NULL;
368 /* remove leading whitespace */
369 while (*str==' ' || *str=='\t') {
373 if ((i = get_header(name, hdrtab)) == NOTOK) {
374 /* no header we would care for */
375 if (mh_strcasecmp(name, attach_hdr)==0) {
378 if (mh_strcasecmp(name, sign_hdr)==0) {
381 if (mh_strcasecmp(name, enc_hdr)==0) {
384 /* push it through */
385 fprintf(out, "%s: %s", name, str);
388 /* it's one of the interesting headers */
391 if (hdr->flags & HIGN || strcmp(str, "\n")==0) {
395 if (hdr->flags & HBAD) {
396 advise(NULL, "illegal header line -- %s:", name);
401 msgflags |= hdr->set;
403 if (hdr->flags & HFCC) {
408 if (hdr->flags & HSUB) {
409 subject = mh_xstrdup(str);
412 if (!(hdr->flags & HADR)) {
413 fprintf(out, "%s: %s", name, str);
417 if ((ret = getmboxes(str, &addr_end)) < 0) {
418 adios(EX_DATAERR, NULL, "can't parse address: %s", str);
424 addrc += do_aliasing(&addr_start, &addr_end);
427 if (hdr->flags & HBCC) {
428 addr_end->m_next = bccs;
429 bccs = addr_start.m_next;
433 if (hdr->set & MFRM) {
434 struct mailname *mp = NULL;
435 struct mailname *my = NULL;
437 /* needed because the address parser holds global state */
440 for (mp = addr_start.m_next; mp; mp = mp->m_next) {
454 if (!(hdr->flags & HDCC)) {
455 putadr(name, addr_start.m_next);
458 if (hdr->flags & HTRY) {
459 addr_end->m_next = recipients;
460 recipients = addr_start.m_next;
467 ** Add yet missing headers.
470 finish_headers(FILE *out)
473 char from[BUFSIZ]; /* my network address */
474 char signature[BUFSIZ]; /* my signature */
475 char *resentstr = (msgstate == resent) ? "Resent-" : "";
477 if (!(msgflags & MDAT)) {
478 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
481 if (sender != NULL) {
482 snprintf(signature, sizeof(signature), "%s", sender->m_text);
483 } else if ((cp = context_find("Default-From")) != NULL) {
484 snprintf(signature, sizeof(signature), "%s", cp);
486 snprintf(from, sizeof(from), "%s@%s", getusername(), LocalName());
487 if ((cp = getfullname()) && *cp) {
488 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
490 snprintf(signature, sizeof(signature), "%s", from);
493 if (!(msgflags & MFRM)) {
494 fprintf(out, "%sFrom: %s\n", resentstr, signature);
497 ** Add a Sender: header because the From: header could
498 ** be fake or contain multiple addresses.
500 if (!(msgflags & MFMM) || sender != NULL) {
501 fprintf(out, "%sSender: %s\n", resentstr, signature);
504 if (!(msgflags & MVIS)) {
505 fprintf(out, "%sBcc: undisclosed-recipients:;\n", resentstr);
509 adios(EX_DATAERR, NULL, "re-format message and try again");
515 ** Return index of the requested header in the table, or NOTOK if missing.
518 get_header(char *header, struct headers *table)
522 for (h=table; h->value; h++) {
523 if (mh_strcasecmp(header, h->value)==0) {
532 ** output the address list for header "name". The address list
533 ** is a linked list of mailname structs. "nl" points to the head
534 ** of the list. Alias substitution should be done on nl.
537 putadr(char *name, struct mailname *nl)
544 fprintf(out, "%s: ", name);
545 namelen = strlen(name) + 2;
548 for (mp = nl; mp; ) {
549 if (linepos > MAX_SM_FIELD) {
550 fprintf(out, "\n%s: ", name);
554 if (mp->m_gname != NULL) {
555 cp = mh_xstrdup(mp->m_gname);
557 linepos = putone(cp, linepos, namelen);
562 linepos = putone(mp->m_text, linepos, namelen);
570 putone(char *adr, int pos, int indent)
578 } else if (linepos+len > OUTPUTLINELEN) {
579 fprintf(out, ",\n%*s", indent, "");
595 process_fcc(char *str)
600 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
601 adios(EX_DATAERR, NULL, "Too much Fcc data");
603 /* todo: better have three states: SEPARATOR, PLUS, WORD */
604 for (cp=pp=str; *cp; cp++) {
613 if (*pp=='+' || *pp=='@') {
633 fcc(char *file, char *folders)
639 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
643 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
644 adios(EX_DATAERR, NULL, "Too much Fcc data");
646 /* hack: read from /dev/null and refile(1) won't question us */
647 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
649 status = system(cmd);
651 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
652 msgstate == resent ? "Resent-" : "", folders);
653 } else if (status != 0) {
654 fprintf(stderr, "%sFcc %s: Problems occurred.\n",
655 msgstate == resent ? "Resent-" : "", folders);
663 process_bccs(char *origmsg)
665 char *bccdraft = NULL;
666 struct mailname *mp = NULL;
669 for (mp=bccs; mp; mp=mp->m_next) {
670 bccdraft = mh_xstrdup(m_mktemp2("/tmp/", invo_name, NULL, &out));
671 fprintf(out, "To: %s\n", mp->m_text);
673 fprintf(out, "From: %s\n", from->m_text);
675 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
676 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
677 fprintf(out, "------------\n");
680 if (execprogl("send", "send", bccdraft, (char *)NULL) != 0) {
681 admonish(invo_name, "Problems to send Bcc to %s",
689 * Do aliasing on a mailname linked list
690 * Begin at start->m_next
691 * End if m_next == NULL
692 * **end is set to the new end.
693 * Return the number of new mainames in the list
697 do_aliasing(struct mailname *start, struct mailname **end)
699 struct mailname *prev, *cur;
707 while (cur != NULL) {
709 cp = akvalue(cur->m_mbox);
710 if (strcmp(cp, cur->m_mbox) != 0) {
711 prev->m_next = cur->m_next;
712 if ((e = getmboxes(cp, &prev)) < 0) {
729 adios(EX_CONFIG, NULL, "can't parse alias %s: %s", cur->m_mbox, cp);
730 return 0; /* not reached */