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 */
43 /* flags for headers->flags */
44 #define HNOP 0x0000 /* just used to keep .set around */
45 #define HBAD 0x0001 /* bad header - don't let it through */
46 #define HADR 0x0002 /* header has an address field */
47 #define HSUB 0x0004 /* Subject: header */
48 #define HTRY 0x0008 /* try to send to addrs on header */
49 #define HBCC 0x0010 /* don't output this header */
50 #define HFCC 0x0020 /* FCC: type header */
51 #define HIGN 0x0040 /* ignore this header */
53 /* flags for headers->set */
54 #define MFRM 0x0001 /* we've seen a From: */
55 #define MDAT 0x0002 /* we've seen a Date: */
56 #define MRFM 0x0004 /* we've seen a Resent-From: */
57 #define MVIS 0x0008 /* we've seen sighted addrs */
58 #define MINV 0x0010 /* we've seen blind addrs */
59 #define MRDT 0x0020 /* we've seen a Resent-Date: */
60 #define MFMM 0x0040 /* The Mail is From a Alternative-Mailbox Addresse */
68 static struct headers NHeaders[] = {
69 { "Return-Path", HBAD, 0 },
70 { "Received", HBAD, 0 },
71 { "Reply-To", HADR, 0 },
72 { "From", HADR, MFRM },
73 { "Sender", HADR|HBAD, 0 },
74 { "Date", HNOP, MDAT },
75 { "Subject", HSUB, 0 },
76 { "To", HADR|HTRY, MVIS },
77 { "Cc", HADR|HTRY, MVIS },
78 { "Bcc", HADR|HTRY|HBCC, MINV },
79 { "Message-Id", HBAD, 0 },
81 { "Envelope-From", HIGN, 0 },
85 static struct headers RHeaders[] = {
86 { "Resent-Reply-To", HADR, 0 },
87 { "Resent-From", HADR, MRFM },
88 { "Resent-Sender", HADR|HBAD, 0 },
89 { "Resent-Date", HNOP, MRDT },
90 { "Resent-Subject", HSUB, 0 },
91 { "Resent-To", HADR|HTRY, MVIS },
92 { "Resent-Cc", HADR|HTRY, MVIS },
93 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
94 { "Resent-Message-Id", HBAD, 0 },
95 { "Resent-Fcc", HFCC, 0 },
96 { "Reply-To", HADR, 0 },
98 { "Envelope-From", HIGN, 0 },
103 static int badmsg = 0;
104 static int verbose = 0;
105 static int debug = 0;
106 static int aliasflg = 0; /* if going to process aliases */
108 static unsigned msgflags = 0; /* what we've seen */
116 static char *subject = NULL; /* the subject field for BCC'ing */
117 static char fccs[BUFSIZ] = "";
118 struct mailname *bccs = NULL; /* list of the bcc recipients */
119 struct mailname *recipients = NULL; /* list of the recipients */
120 size_t recipientsc = 0;
121 struct mailname *sender = NULL;
123 static struct headers *hdrtab; /* table for the message we're doing */
124 static FILE *out; /* output (temp) file */
129 static void putfmt(char *, char *, FILE *);
130 static void finish_headers(FILE *);
131 static int get_header(char *, struct headers *);
132 static void putadr(char *, struct mailname *);
133 static int putone(char *, int, int);
134 static void process_fcc(char *);
135 static void fcc(char *, char *);
136 static void process_bccs(char *);
137 static size_t do_aliasing(struct mailname *, struct mailname **);
141 main(int argc, char **argv)
144 char *cp, *msg = NULL, **argp, **arguments;
145 char **sargv, buf[BUFSIZ], name[NAMESZ];
148 setlocale(LC_ALL, "");
149 invo_name = mhbasename(argv[0]);
153 arguments = getarguments(invo_name, argc, argv, 0);
156 while ((cp = *argp++)) {
158 switch (smatch(++cp, switches)) {
160 ambigsw(cp, switches);
163 adios(EX_USAGE, NULL, "-%s unknown", cp);
166 snprintf(buf, sizeof(buf),
167 "%s [switches] file",
169 print_help(buf, switches, 1);
170 exit(argc == 2 ? EX_OK : EX_USAGE);
172 print_version(invo_name);
173 exit(argc == 2 ? EX_OK : EX_USAGE);
192 adios(EX_USAGE, NULL, "only one message at a time!");
199 adios(EX_USAGE, NULL, "usage: %s [switches] file", invo_name);
202 if ((in = fopen(msg, "r")) == NULL) {
203 adios(EX_IOERR, msg, "unable to open");
210 tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
213 /* check for "Aliasfile:" profile entry */
214 if ((cp = context_find("Aliasfile"))) {
218 for (ap=brkstring(dp=getcpy(cp), " ", "\n"); ap && *ap;
220 if ((state = alias(etcpath(*ap))) != AK_OK) {
221 adios(EX_IOERR, NULL, "aliasing error in file %s: %s",
222 *ap, akerror(state));
228 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
230 for (compnum = 1, state = FLD;;) {
231 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
236 while (state == FLDPLUS) {
237 state = m_getfld(state, name, buf,
241 putfmt(name, cp, out);
247 fprintf(out, "\n%s", buf);
248 while (state == BODY) {
249 state = m_getfld(state, name, buf,
261 adios(EX_DATAERR, NULL, "message format error in component #%d",
265 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
272 struct mailname *i = recipients;
276 fputs(i->m_mbox, stdout);
279 fputs(i->m_host, stdout);
297 process_bccs(tmpfil);
298 if (!(msgflags & MVIS)) {
299 /* only Bcc rcpts: we're finished here */
306 ** re-open the temp file, unlink it and exec sendmail, giving it
307 ** the msg temp file as std in.
309 if (!freopen(tmpfil, "r", stdin)) {
310 adios(EX_IOERR, tmpfil, "can't reopen for sendmail");
314 if (recipientsc == 0) {
315 adios(EX_DATAERR, NULL, "message has no recipients");
318 sargv = mh_xmalloc(sizeof(char **) * (recipientsc + 4));
321 *argp++ = "send-mail";
322 *argp++ = "-i"; /* don't stop on "." */
327 while (recipients != NULL) {
328 cp = getcpy(recipients->m_mbox);
329 if (recipients->m_host) {
331 cp = add(recipients->m_host, cp);
335 recipients = recipients->m_next;
338 execvp(sendmail, sargv);
340 if (errno == E2BIG) {
341 adios(EX_DATAERR, sendmail, "too much arguments, probably to much recipients");
344 adios(EX_OSERR, sendmail, "can't exec");
349 /* DRAFT GENERATION */
352 putfmt(char *name, char *str, FILE *out)
356 struct mailname addr_start, *addr_end;
359 addr_end = &addr_start;
360 addr_end->m_next = NULL;
362 /* remove leading whitespace */
363 while (*str==' ' || *str=='\t') {
367 if ((i = get_header(name, hdrtab)) == NOTOK) {
368 /* no header we would care for */
369 if (mh_strcasecmp(name, attach_hdr)==0) {
372 if (mh_strcasecmp(name, sign_hdr)==0) {
375 if (mh_strcasecmp(name, enc_hdr)==0) {
378 /* push it through */
379 fprintf(out, "%s: %s", name, str);
382 /* it's one of the interesting headers */
385 if (hdr->flags & HIGN || strcmp(str, "\n")==0) {
389 if (hdr->flags & HBAD) {
390 advise(NULL, "illegal header line -- %s:", name);
395 msgflags |= hdr->set;
397 if (hdr->flags & HFCC) {
402 if (hdr->flags & HSUB) {
403 subject = getcpy(str);
406 if (!(hdr->flags & HADR)) {
407 fprintf(out, "%s: %s", name, str);
411 addrc = getmboxes(str, &addr_end);
414 addrc += do_aliasing(&addr_start, &addr_end);
417 if (hdr->flags & HBCC) {
418 addr_end->m_next = bccs;
419 bccs = addr_start.m_next;
423 if (hdr->set & MFRM) {
424 struct mailname *mp = NULL;
425 struct mailname *my = NULL;
426 unsigned int fromcnt = 0;
428 /* needed because the address parser holds global state */
431 for ( mp = addr_start.m_next; mp; mp = mp->m_next) {
446 putadr(name, addr_start.m_next);
448 if (hdr->flags & HTRY) {
449 addr_end->m_next = recipients;
450 recipients = addr_start.m_next;
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) {
522 ** output the address list for header "name". The address list
523 ** is a linked list of mailname structs. "nl" points to the head
524 ** of the list. Alias substitution should be done on nl.
527 putadr(char *name, struct mailname *nl)
529 struct mailname *mp, *mp2;
533 fprintf(out, "%s: ", name);
534 namelen = strlen(name) + 2;
537 for (mp = nl; mp; ) {
538 if (linepos > MAX_SM_FIELD) {
539 fprintf(out, "\n%s: ", name);
542 linepos = putone(mp->m_text, linepos, namelen);
550 putone(char *adr, int pos, int indent)
558 } else if (linepos+len > OUTPUTLINELEN) {
559 fprintf(out, ",\n%*s", indent, "");
575 process_fcc(char *str)
580 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
581 adios(EX_DATAERR, NULL, "Too much Fcc data");
583 /* todo: better have three states: SEPARATOR, PLUS, WORD */
584 for (cp=pp=str; *cp; cp++) {
593 if (*pp=='+' || *pp=='@') {
613 fcc(char *file, char *folders)
619 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
623 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
624 adios(EX_DATAERR, NULL, "Too much Fcc data");
626 /* hack: read from /dev/null and refile(1) won't question us */
627 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
629 status = system(cmd);
631 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
632 msgstate == resent ? "Resent-" : "", folders);
633 } else if (status != 0) {
634 fprintf(stderr, "%sFcc %s: Problems occured.\n",
635 msgstate == resent ? "Resent-" : "", folders);
643 process_bccs(char *origmsg)
645 char *bccdraft = NULL;
646 struct mailname *mp = NULL;
649 for (mp=bccs; mp; mp=mp->m_next) {
650 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
651 fprintf(out, "To: %s\n", mp->m_text);
652 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
653 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
654 fprintf(out, "------------\n");
657 if (execprogl("send", "send", bccdraft, (char *)NULL) != 0) {
658 admonish(invo_name, "Problems to send Bcc to %s",
666 * Do aliasing on a mailname linked list
667 * Begin at start->m_next
668 * End if m_next == NULL
669 * **end is set to the new end.
670 * Return the number of new mainames in the list
674 do_aliasing(struct mailname *start, struct mailname **end)
676 struct mailname *prev, *cur;
683 while (cur != NULL) {
685 cp = akvalue(cur->m_mbox);
686 if (strcmp(cp, cur->m_mbox) != 0) {
687 prev->m_next = cur->m_next;
688 i += getmboxes(cp, &prev);