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 */
52 #define HDCC 0x0080 /* DCC: type header */
54 /* flags for headers->set */
55 #define MFRM 0x0001 /* we've seen a From: */
56 #define MDAT 0x0002 /* we've seen a Date: */
57 #define MRFM 0x0004 /* we've seen a Resent-From: */
58 #define MVIS 0x0008 /* we've seen sighted addrs */
59 #define MINV 0x0010 /* we've seen blind addrs */
60 #define MRDT 0x0020 /* we've seen a Resent-Date: */
61 #define MFMM 0x0040 /* The Mail is From a Alternative-Mailbox Addresse */
69 static struct headers NHeaders[] = {
70 { "Return-Path", HBAD, 0 },
71 { "Received", HBAD, 0 },
72 { "Reply-To", HADR, 0 },
73 { "From", HADR, MFRM },
74 { "Sender", HADR|HBAD, 0 },
75 { "Date", HNOP, MDAT },
76 { "Subject", HSUB, 0 },
77 { "To", HADR|HTRY, MVIS },
78 { "Cc", HADR|HTRY, MVIS },
79 { "Dcc", HADR|HTRY|HDCC, MINV },
80 { "Bcc", HADR|HTRY|HBCC, MINV },
81 { "Message-Id", HBAD, 0 },
83 { "Envelope-From", HIGN, 0 },
87 static struct headers RHeaders[] = {
88 { "Resent-Reply-To", HADR, 0 },
89 { "Resent-From", HADR, MRFM },
90 { "Resent-Sender", HADR|HBAD, 0 },
91 { "Resent-Date", HNOP, MRDT },
92 { "Resent-Subject", HSUB, 0 },
93 { "Resent-To", HADR|HTRY, MVIS },
94 { "Resent-Cc", HADR|HTRY, MVIS },
95 { "Resent-Dcc", HADR|HTRY|HDCC, MINV },
96 { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
97 { "Resent-Message-Id", HBAD, 0 },
98 { "Resent-Fcc", HFCC, 0 },
99 { "Reply-To", HADR, 0 },
101 { "Envelope-From", HIGN, 0 },
106 static int badmsg = 0;
107 static int verbose = 0;
108 static int debug = 0;
109 static int aliasflg = 0; /* if going to process aliases */
111 static unsigned msgflags = 0; /* what we've seen */
119 static char *subject = NULL; /* the subject field for BCC'ing */
120 static char fccs[BUFSIZ] = "";
121 struct mailname *bccs = NULL; /* list of the bcc recipients */
122 struct mailname *recipients = NULL; /* list of the recipients */
123 size_t recipientsc = 0;
124 struct mailname *sender = NULL;
126 static struct headers *hdrtab; /* table for the message we're doing */
127 static FILE *out; /* output (temp) file */
132 static void putfmt(char *, char *, FILE *);
133 static void finish_headers(FILE *);
134 static int get_header(char *, struct headers *);
135 static void putadr(char *, struct mailname *);
136 static int putone(char *, int, int);
137 static void process_fcc(char *);
138 static void fcc(char *, char *);
139 static void process_bccs(char *);
140 static size_t do_aliasing(struct mailname *, struct mailname **);
144 main(int argc, char **argv)
147 char *cp, *msg = NULL, **argp, **arguments;
148 char **sargv, buf[BUFSIZ], name[NAMESZ];
151 setlocale(LC_ALL, "");
152 invo_name = mhbasename(argv[0]);
156 arguments = getarguments(invo_name, argc, argv, 0);
159 while ((cp = *argp++)) {
161 switch (smatch(++cp, switches)) {
163 ambigsw(cp, switches);
166 adios(EX_USAGE, NULL, "-%s unknown", cp);
169 snprintf(buf, sizeof(buf),
170 "%s [switches] file",
172 print_help(buf, switches, 1);
173 exit(argc == 2 ? EX_OK : EX_USAGE);
175 print_version(invo_name);
176 exit(argc == 2 ? EX_OK : EX_USAGE);
195 adios(EX_USAGE, NULL, "only one message at a time!");
202 adios(EX_USAGE, NULL, "usage: %s [switches] file", invo_name);
205 if ((in = fopen(msg, "r")) == NULL) {
206 adios(EX_IOERR, msg, "unable to open");
213 tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
216 /* check for "Aliasfile:" profile entry */
217 if ((cp = context_find("Aliasfile"))) {
221 for (ap=brkstring(dp=getcpy(cp), " ", "\n"); ap && *ap;
223 if ((state = alias(etcpath(*ap))) != AK_OK) {
224 adios(EX_IOERR, NULL, "aliasing error in file %s: %s",
225 *ap, akerror(state));
231 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
233 for (compnum = 1, state = FLD;;) {
234 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
239 while (state == FLDPLUS) {
240 state = m_getfld(state, name, buf,
244 putfmt(name, cp, out);
250 fprintf(out, "\n%s", buf);
251 while (state == BODY) {
252 state = m_getfld(state, name, buf,
264 adios(EX_DATAERR, NULL, "message format error in component #%d",
268 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
275 struct mailname *i = recipients;
279 fputs(i->m_mbox, stdout);
282 fputs(i->m_host, stdout);
300 process_bccs(tmpfil);
301 if (!(msgflags & MVIS)) {
302 /* only Bcc rcpts: we're finished here */
309 ** re-open the temp file, unlink it and exec sendmail, giving it
310 ** the msg temp file as std in.
312 if (!freopen(tmpfil, "r", stdin)) {
313 adios(EX_IOERR, tmpfil, "can't reopen for sendmail");
317 if (recipientsc == 0) {
318 adios(EX_DATAERR, NULL, "message has no recipients");
321 sargv = mh_xmalloc(sizeof(char **) * (recipientsc + 4));
324 *argp++ = "send-mail";
325 *argp++ = "-i"; /* don't stop on "." */
330 while (recipients != NULL) {
331 cp = getcpy(recipients->m_mbox);
332 if (recipients->m_host) {
334 cp = add(recipients->m_host, cp);
338 recipients = recipients->m_next;
341 execvp(sendmail, sargv);
343 if (errno == E2BIG) {
344 adios(EX_DATAERR, sendmail, "too much arguments, probably to much recipients");
347 adios(EX_OSERR, sendmail, "can't exec");
352 /* DRAFT GENERATION */
355 putfmt(char *name, char *str, FILE *out)
359 struct mailname addr_start, *addr_end;
362 addr_end = &addr_start;
363 addr_end->m_next = NULL;
365 /* remove leading whitespace */
366 while (*str==' ' || *str=='\t') {
370 if ((i = get_header(name, hdrtab)) == NOTOK) {
371 /* no header we would care for */
372 if (mh_strcasecmp(name, attach_hdr)==0) {
375 if (mh_strcasecmp(name, sign_hdr)==0) {
378 if (mh_strcasecmp(name, enc_hdr)==0) {
381 /* push it through */
382 fprintf(out, "%s: %s", name, str);
385 /* it's one of the interesting headers */
388 if (hdr->flags & HIGN || strcmp(str, "\n")==0) {
392 if (hdr->flags & HBAD) {
393 advise(NULL, "illegal header line -- %s:", name);
398 msgflags |= hdr->set;
400 if (hdr->flags & HFCC) {
405 if (hdr->flags & HSUB) {
406 subject = getcpy(str);
409 if (!(hdr->flags & HADR)) {
410 fprintf(out, "%s: %s", name, str);
414 addrc = getmboxes(str, &addr_end);
417 addrc += do_aliasing(&addr_start, &addr_end);
420 if (hdr->flags & HBCC) {
421 addr_end->m_next = bccs;
422 bccs = addr_start.m_next;
426 if (hdr->set & MFRM) {
427 struct mailname *mp = NULL;
428 struct mailname *my = NULL;
429 unsigned int fromcnt = 0;
431 /* needed because the address parser holds global state */
434 for ( mp = addr_start.m_next; mp; mp = mp->m_next) {
449 if (!(hdr->flags & HDCC)) {
450 putadr(name, addr_start.m_next);
453 if (hdr->flags & HTRY) {
454 addr_end->m_next = recipients;
455 recipients = addr_start.m_next;
462 ** Add yet missing headers.
465 finish_headers(FILE *out)
468 char from[BUFSIZ]; /* my network address */
469 char signature[BUFSIZ]; /* my signature */
470 char *resentstr = (msgstate == resent) ? "Resent-" : "";
472 if (!(msgflags & MDAT)) {
473 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
476 if (sender != NULL) {
477 snprintf(signature, sizeof(signature), "%s", sender->m_text);
478 } else if ((cp = context_find("Default-From")) != NULL) {
479 snprintf(signature, sizeof(signature), "%s", cp);
481 snprintf(from, sizeof(from), "%s@%s", getusername(), LocalName());
482 if ((cp = getfullname()) && *cp) {
483 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
485 snprintf(signature, sizeof(signature), "%s", from);
488 if (!(msgflags & MFRM)) {
489 fprintf(out, "%sFrom: %s\n", resentstr, signature);
492 ** Add a Sender: header because the From: header could
493 ** be fake or contain multiple addresses.
495 if (!(msgflags & MFMM) || sender != NULL) {
496 fprintf(out, "%sSender: %s\n", resentstr, signature);
499 if (!(msgflags & MVIS)) {
500 fprintf(out, "%sBcc: undisclosed-recipients:;\n", resentstr);
504 adios(EX_DATAERR, NULL, "re-format message and try again");
510 ** Return index of the requested header in the table, or NOTOK if missing.
513 get_header(char *header, struct headers *table)
517 for (h=table; h->value; h++) {
518 if (mh_strcasecmp(header, h->value)==0) {
527 ** output the address list for header "name". The address list
528 ** is a linked list of mailname structs. "nl" points to the head
529 ** of the list. Alias substitution should be done on nl.
532 putadr(char *name, struct mailname *nl)
538 fprintf(out, "%s: ", name);
539 namelen = strlen(name) + 2;
542 for (mp = nl; mp; ) {
543 if (linepos > MAX_SM_FIELD) {
544 fprintf(out, "\n%s: ", name);
547 linepos = putone(mp->m_text, linepos, namelen);
554 putone(char *adr, int pos, int indent)
562 } else if (linepos+len > OUTPUTLINELEN) {
563 fprintf(out, ",\n%*s", indent, "");
579 process_fcc(char *str)
584 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
585 adios(EX_DATAERR, NULL, "Too much Fcc data");
587 /* todo: better have three states: SEPARATOR, PLUS, WORD */
588 for (cp=pp=str; *cp; cp++) {
597 if (*pp=='+' || *pp=='@') {
617 fcc(char *file, char *folders)
623 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
627 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
628 adios(EX_DATAERR, NULL, "Too much Fcc data");
630 /* hack: read from /dev/null and refile(1) won't question us */
631 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
633 status = system(cmd);
635 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
636 msgstate == resent ? "Resent-" : "", folders);
637 } else if (status != 0) {
638 fprintf(stderr, "%sFcc %s: Problems occured.\n",
639 msgstate == resent ? "Resent-" : "", folders);
647 process_bccs(char *origmsg)
649 char *bccdraft = NULL;
650 struct mailname *mp = NULL;
653 for (mp=bccs; mp; mp=mp->m_next) {
654 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
655 fprintf(out, "To: %s\n", mp->m_text);
656 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
657 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
658 fprintf(out, "------------\n");
661 if (execprogl("send", "send", bccdraft, (char *)NULL) != 0) {
662 admonish(invo_name, "Problems to send Bcc to %s",
670 * Do aliasing on a mailname linked list
671 * Begin at start->m_next
672 * End if m_next == NULL
673 * **end is set to the new end.
674 * Return the number of new mainames in the list
678 do_aliasing(struct mailname *start, struct mailname **end)
680 struct mailname *prev, *cur;
687 while (cur != NULL) {
689 cp = akvalue(cur->m_mbox);
690 if (strcmp(cp, cur->m_mbox) != 0) {
691 prev->m_next = cur->m_next;
692 i += getmboxes(cp, &prev);