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 struct mailname *from = NULL; /* the from field for BCC'ing */
121 static char fccs[BUFSIZ] = "";
122 struct mailname *bccs = NULL; /* list of the bcc recipients */
123 struct mailname *recipients = NULL; /* list of the recipients */
124 size_t recipientsc = 0;
125 struct mailname *sender = NULL;
127 static struct headers *hdrtab; /* table for the message we're doing */
128 static FILE *out; /* output (temp) file */
133 static void putfmt(char *, char *, FILE *);
134 static void finish_headers(FILE *);
135 static int get_header(char *, struct headers *);
136 static void putadr(char *, struct mailname *);
137 static int putone(char *, int, int);
138 static void process_fcc(char *);
139 static void fcc(char *, char *);
140 static void process_bccs(char *);
141 static size_t do_aliasing(struct mailname *, struct mailname **);
145 main(int argc, char **argv)
148 struct field f = {{0}};
150 char *cp, *msg = NULL, **argp, **arguments;
151 char **sargv, buf[BUFSIZ];
154 setlocale(LC_ALL, "");
155 invo_name = mhbasename(argv[0]);
159 arguments = getarguments(invo_name, argc, argv, 0);
162 while ((cp = *argp++)) {
164 switch (smatch(++cp, switches)) {
166 ambigsw(cp, switches);
169 adios(EX_USAGE, NULL, "-%s unknown", cp);
172 snprintf(buf, sizeof(buf),
173 "%s [switches] file",
175 print_help(buf, switches, 1);
176 exit(argc == 2 ? EX_OK : EX_USAGE);
178 print_version(invo_name);
179 exit(argc == 2 ? EX_OK : EX_USAGE);
198 adios(EX_USAGE, NULL, "only one message at a time!");
205 adios(EX_USAGE, NULL, "usage: %s [switches] file", invo_name);
208 if ((in = fopen(msg, "r")) == NULL) {
209 adios(EX_IOERR, msg, "unable to open");
216 tmpfil = mh_xstrdup(m_mktemp2("/tmp/", invo_name, NULL, &out));
219 /* check for "Aliasfile:" profile entry */
220 if ((cp = context_find("Aliasfile"))) {
224 for (ap=brkstring(dp=mh_xstrdup(cp), " ", "\n"); ap && *ap;
226 if ((state = alias(etcpath(*ap))) != AK_OK) {
227 adios(EX_IOERR, NULL, "aliasing error in file %s: %s",
228 *ap, akerror(state));
234 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
236 for (compnum = 1, state = FLD2;;) {
237 switch (state = m_getfld2(state, &f, in)) {
240 putfmt(f.name, f.value, out);
245 fprintf(out, "\n%s", f.value);
246 while ((state = m_getfld2(state, &f, in)) == BODY2) {
247 if (f.valuelen >= NAMESZ) {
248 adios(EX_DATAERR, NULL, "Body contains a to long line");
261 adios(EX_DATAERR, NULL, "message format error in component #%d",
265 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
271 if (state != FILEEOF2) {
272 adios(EX_IOERR, "m_getfld2", "Error while reading body");
276 struct mailname *i = recipients;
280 fputs(i->m_mbox, stdout);
283 fputs(i->m_host, stdout);
301 process_bccs(tmpfil);
302 if (!(msgflags & MVIS)) {
303 /* only Bcc rcpts: we're finished here */
310 ** re-open the temp file, unlink it and exec sendmail, giving it
311 ** the msg temp file as std in.
313 if (!freopen(tmpfil, "r", stdin)) {
314 adios(EX_IOERR, tmpfil, "can't reopen for sendmail");
318 if (recipientsc == 0) {
319 adios(EX_DATAERR, NULL, "message has no recipients");
322 sargv = mh_xcalloc(recipientsc + 4, sizeof(char **));
325 *argp++ = "send-mail";
326 *argp++ = "-i"; /* don't stop on "." */
331 while (recipients != NULL) {
332 cp = mh_xstrdup(recipients->m_mbox);
333 if (recipients->m_host) {
335 cp = add(recipients->m_host, cp);
339 recipients = recipients->m_next;
342 execvp(sendmail, sargv);
344 if (errno == E2BIG) {
345 adios(EX_DATAERR, sendmail, "too much arguments, probably to much recipients");
348 adios(EX_OSERR, sendmail, "can't exec");
353 /* DRAFT GENERATION */
356 putfmt(char *name, char *str, FILE *out)
360 struct mailname addr_start, *addr_end;
364 addr_end = &addr_start;
365 addr_end->m_next = NULL;
367 /* remove leading whitespace */
368 while (*str==' ' || *str=='\t') {
372 if ((i = get_header(name, hdrtab)) == NOTOK) {
373 /* no header we would care for */
374 if (mh_strcasecmp(name, attach_hdr)==0) {
377 if (mh_strcasecmp(name, sign_hdr)==0) {
380 if (mh_strcasecmp(name, enc_hdr)==0) {
383 /* push it through */
384 fprintf(out, "%s: %s", name, str);
387 /* it's one of the interesting headers */
390 if (hdr->flags & HIGN || strcmp(str, "\n")==0) {
394 if (hdr->flags & HBAD) {
395 advise(NULL, "illegal header line -- %s:", name);
400 msgflags |= hdr->set;
402 if (hdr->flags & HFCC) {
407 if (hdr->flags & HSUB) {
408 subject = mh_xstrdup(str);
411 if (!(hdr->flags & HADR)) {
412 fprintf(out, "%s: %s", name, str);
416 if ((ret = getmboxes(str, &addr_end)) < 0) {
417 adios(EX_DATAERR, NULL, "can't parse address: %s", str);
423 addrc += do_aliasing(&addr_start, &addr_end);
426 if (hdr->flags & HBCC) {
427 addr_end->m_next = bccs;
428 bccs = addr_start.m_next;
432 if (hdr->set & MFRM) {
433 struct mailname *mp = NULL;
434 struct mailname *my = NULL;
436 /* needed because the address parser holds global state */
439 for (mp = addr_start.m_next; mp; mp = mp->m_next) {
453 if (!(hdr->flags & HDCC)) {
454 putadr(name, addr_start.m_next);
457 if (hdr->flags & HTRY) {
458 addr_end->m_next = recipients;
459 recipients = addr_start.m_next;
466 ** Add yet missing headers.
469 finish_headers(FILE *out)
472 char from[BUFSIZ]; /* my network address */
473 char signature[BUFSIZ]; /* my signature */
474 char *resentstr = (msgstate == resent) ? "Resent-" : "";
476 if (!(msgflags & MDAT)) {
477 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
480 if (sender != NULL) {
481 snprintf(signature, sizeof(signature), "%s", sender->m_text);
482 } else if ((cp = context_find("Default-From")) != NULL) {
483 snprintf(signature, sizeof(signature), "%s", cp);
485 snprintf(from, sizeof(from), "%s@%s", getusername(), LocalName());
486 if ((cp = getfullname()) && *cp) {
487 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
489 snprintf(signature, sizeof(signature), "%s", from);
492 if (!(msgflags & MFRM)) {
493 fprintf(out, "%sFrom: %s\n", resentstr, signature);
496 ** Add a Sender: header because the From: header could
497 ** be fake or contain multiple addresses.
499 if (!(msgflags & MFMM) || sender != NULL) {
500 fprintf(out, "%sSender: %s\n", resentstr, signature);
503 if (!(msgflags & MVIS)) {
504 fprintf(out, "%sBcc: undisclosed-recipients:;\n", resentstr);
508 adios(EX_DATAERR, NULL, "re-format message and try again");
514 ** Return index of the requested header in the table, or NOTOK if missing.
517 get_header(char *header, struct headers *table)
521 for (h=table; h->value; h++) {
522 if (mh_strcasecmp(header, h->value)==0) {
531 ** output the address list for header "name". The address list
532 ** is a linked list of mailname structs. "nl" points to the head
533 ** of the list. Alias substitution should be done on nl.
536 putadr(char *name, struct mailname *nl)
543 fprintf(out, "%s: ", name);
544 namelen = strlen(name) + 2;
547 for (mp = nl; mp; ) {
548 if (linepos > MAX_SM_FIELD) {
549 fprintf(out, "\n%s: ", name);
553 if (mp->m_gname != NULL) {
554 cp = mh_xstrdup(mp->m_gname);
556 linepos = putone(cp, linepos, namelen);
561 linepos = putone(mp->m_text, linepos, namelen);
569 putone(char *adr, int pos, int indent)
577 } else if (linepos+len > OUTPUTLINELEN) {
578 fprintf(out, ",\n%*s", indent, "");
594 process_fcc(char *str)
599 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
600 adios(EX_DATAERR, NULL, "Too much Fcc data");
602 /* todo: better have three states: SEPARATOR, PLUS, WORD */
603 for (cp=pp=str; *cp; cp++) {
612 if (*pp=='+' || *pp=='@') {
632 fcc(char *file, char *folders)
638 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
642 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
643 adios(EX_DATAERR, NULL, "Too much Fcc data");
645 /* hack: read from /dev/null and refile(1) won't question us */
646 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
648 status = system(cmd);
650 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
651 msgstate == resent ? "Resent-" : "", folders);
652 } else if (status != 0) {
653 fprintf(stderr, "%sFcc %s: Problems occurred.\n",
654 msgstate == resent ? "Resent-" : "", folders);
662 process_bccs(char *origmsg)
664 char *bccdraft = NULL;
665 struct mailname *mp = NULL;
668 for (mp=bccs; mp; mp=mp->m_next) {
669 bccdraft = mh_xstrdup(m_mktemp2("/tmp/", invo_name, NULL, &out));
670 fprintf(out, "To: %s\n", mp->m_text);
672 fprintf(out, "From: %s\n", from->m_text);
674 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
675 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
676 fprintf(out, "------------\n");
679 if (execprogl("send", "send", bccdraft, (char *)NULL) != 0) {
680 admonish(invo_name, "Problems to send Bcc to %s",
688 * Do aliasing on a mailname linked list
689 * Begin at start->m_next
690 * End if m_next == NULL
691 * **end is set to the new end.
692 * Return the number of new mainames in the list
696 do_aliasing(struct mailname *start, struct mailname **end)
698 struct mailname *prev, *cur;
706 while (cur != NULL) {
708 cp = akvalue(cur->m_mbox);
709 if (strcmp(cp, cur->m_mbox) != 0) {
710 prev->m_next = cur->m_next;
711 if ((e = getmboxes(cp, &prev)) < 0) {
728 adios(EX_CONFIG, NULL, "can't parse alias %s: %s", cur->m_mbox, cp);
729 return 0; /* not reached */