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 struct field f = {{0}};
149 char *cp, *msg = NULL, **argp, **arguments;
150 char **sargv, buf[BUFSIZ];
153 setlocale(LC_ALL, "");
154 invo_name = mhbasename(argv[0]);
158 arguments = getarguments(invo_name, argc, argv, 0);
161 while ((cp = *argp++)) {
163 switch (smatch(++cp, switches)) {
165 ambigsw(cp, switches);
168 adios(EX_USAGE, NULL, "-%s unknown", cp);
171 snprintf(buf, sizeof(buf),
172 "%s [switches] file",
174 print_help(buf, switches, 1);
175 exit(argc == 2 ? EX_OK : EX_USAGE);
177 print_version(invo_name);
178 exit(argc == 2 ? EX_OK : EX_USAGE);
197 adios(EX_USAGE, NULL, "only one message at a time!");
204 adios(EX_USAGE, NULL, "usage: %s [switches] file", invo_name);
207 if ((in = fopen(msg, "r")) == NULL) {
208 adios(EX_IOERR, msg, "unable to open");
215 tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
218 /* check for "Aliasfile:" profile entry */
219 if ((cp = context_find("Aliasfile"))) {
223 for (ap=brkstring(dp=getcpy(cp), " ", "\n"); ap && *ap;
225 if ((state = alias(etcpath(*ap))) != AK_OK) {
226 adios(EX_IOERR, NULL, "aliasing error in file %s: %s",
227 *ap, akerror(state));
233 hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
235 for (compnum = 1, state = FLD2;;) {
236 switch (state = m_getfld2(state, &f, in)) {
239 putfmt(f.name, f.value, out);
244 fprintf(out, "\n%s", f.value);
245 while ((state = m_getfld2(state, &f, in)) == BODY2) {
257 adios(EX_DATAERR, NULL, "message format error in component #%d",
261 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
268 struct mailname *i = recipients;
272 fputs(i->m_mbox, stdout);
275 fputs(i->m_host, stdout);
293 process_bccs(tmpfil);
294 if (!(msgflags & MVIS)) {
295 /* only Bcc rcpts: we're finished here */
302 ** re-open the temp file, unlink it and exec sendmail, giving it
303 ** the msg temp file as std in.
305 if (!freopen(tmpfil, "r", stdin)) {
306 adios(EX_IOERR, tmpfil, "can't reopen for sendmail");
310 if (recipientsc == 0) {
311 adios(EX_DATAERR, NULL, "message has no recipients");
314 sargv = mh_xmalloc(sizeof(char **) * (recipientsc + 4));
317 *argp++ = "send-mail";
318 *argp++ = "-i"; /* don't stop on "." */
323 while (recipients != NULL) {
324 cp = getcpy(recipients->m_mbox);
325 if (recipients->m_host) {
327 cp = add(recipients->m_host, cp);
331 recipients = recipients->m_next;
334 execvp(sendmail, sargv);
336 if (errno == E2BIG) {
337 adios(EX_DATAERR, sendmail, "too much arguments, probably to much recipients");
340 adios(EX_OSERR, sendmail, "can't exec");
345 /* DRAFT GENERATION */
348 putfmt(char *name, char *str, FILE *out)
352 struct mailname addr_start, *addr_end;
356 addr_end = &addr_start;
357 addr_end->m_next = NULL;
359 /* remove leading whitespace */
360 while (*str==' ' || *str=='\t') {
364 if ((i = get_header(name, hdrtab)) == NOTOK) {
365 /* no header we would care for */
366 if (mh_strcasecmp(name, attach_hdr)==0) {
369 if (mh_strcasecmp(name, sign_hdr)==0) {
372 if (mh_strcasecmp(name, enc_hdr)==0) {
375 /* push it through */
376 fprintf(out, "%s: %s", name, str);
379 /* it's one of the interesting headers */
382 if (hdr->flags & HIGN || strcmp(str, "\n")==0) {
386 if (hdr->flags & HBAD) {
387 advise(NULL, "illegal header line -- %s:", name);
392 msgflags |= hdr->set;
394 if (hdr->flags & HFCC) {
399 if (hdr->flags & HSUB) {
400 subject = getcpy(str);
403 if (!(hdr->flags & HADR)) {
404 fprintf(out, "%s: %s", name, str);
408 if ((ret = getmboxes(str, &addr_end)) < 0) {
409 adios(EX_DATAERR, NULL, "can't parse address: %s", str);
415 addrc += do_aliasing(&addr_start, &addr_end);
418 if (hdr->flags & HBCC) {
419 addr_end->m_next = bccs;
420 bccs = addr_start.m_next;
424 if (hdr->set & MFRM) {
425 struct mailname *mp = NULL;
426 struct mailname *my = NULL;
428 /* needed because the address parser holds global state */
431 for ( mp = addr_start.m_next; mp; mp = mp->m_next) {
445 if (!(hdr->flags & HDCC)) {
446 putadr(name, addr_start.m_next);
449 if (hdr->flags & HTRY) {
450 addr_end->m_next = recipients;
451 recipients = addr_start.m_next;
458 ** Add yet missing headers.
461 finish_headers(FILE *out)
464 char from[BUFSIZ]; /* my network address */
465 char signature[BUFSIZ]; /* my signature */
466 char *resentstr = (msgstate == resent) ? "Resent-" : "";
468 if (!(msgflags & MDAT)) {
469 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
472 if (sender != NULL) {
473 snprintf(signature, sizeof(signature), "%s", sender->m_text);
474 } else if ((cp = context_find("Default-From")) != NULL) {
475 snprintf(signature, sizeof(signature), "%s", cp);
477 snprintf(from, sizeof(from), "%s@%s", getusername(), LocalName());
478 if ((cp = getfullname()) && *cp) {
479 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
481 snprintf(signature, sizeof(signature), "%s", from);
484 if (!(msgflags & MFRM)) {
485 fprintf(out, "%sFrom: %s\n", resentstr, signature);
488 ** Add a Sender: header because the From: header could
489 ** be fake or contain multiple addresses.
491 if (!(msgflags & MFMM) || sender != NULL) {
492 fprintf(out, "%sSender: %s\n", resentstr, signature);
495 if (!(msgflags & MVIS)) {
496 fprintf(out, "%sBcc: undisclosed-recipients:;\n", resentstr);
500 adios(EX_DATAERR, NULL, "re-format message and try again");
506 ** Return index of the requested header in the table, or NOTOK if missing.
509 get_header(char *header, struct headers *table)
513 for (h=table; h->value; h++) {
514 if (mh_strcasecmp(header, h->value)==0) {
523 ** output the address list for header "name". The address list
524 ** is a linked list of mailname structs. "nl" points to the head
525 ** of the list. Alias substitution should be done on nl.
528 putadr(char *name, struct mailname *nl)
535 fprintf(out, "%s: ", name);
536 namelen = strlen(name) + 2;
539 for (mp = nl; mp; ) {
540 if (linepos > MAX_SM_FIELD) {
541 fprintf(out, "\n%s: ", name);
545 if (mp->m_gname != NULL) {
546 cp = getcpy(mp->m_gname);
548 linepos = putone(cp, linepos, namelen);
553 linepos = putone(mp->m_text, linepos, namelen);
561 putone(char *adr, int pos, int indent)
569 } else if (linepos+len > OUTPUTLINELEN) {
570 fprintf(out, ",\n%*s", indent, "");
586 process_fcc(char *str)
591 if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
592 adios(EX_DATAERR, NULL, "Too much Fcc data");
594 /* todo: better have three states: SEPARATOR, PLUS, WORD */
595 for (cp=pp=str; *cp; cp++) {
604 if (*pp=='+' || *pp=='@') {
624 fcc(char *file, char *folders)
630 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
634 if (100+strlen(file)+strlen(folders) > sizeof cmd) {
635 adios(EX_DATAERR, NULL, "Too much Fcc data");
637 /* hack: read from /dev/null and refile(1) won't question us */
638 snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
640 status = system(cmd);
642 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
643 msgstate == resent ? "Resent-" : "", folders);
644 } else if (status != 0) {
645 fprintf(stderr, "%sFcc %s: Problems occured.\n",
646 msgstate == resent ? "Resent-" : "", folders);
654 process_bccs(char *origmsg)
656 char *bccdraft = NULL;
657 struct mailname *mp = NULL;
660 for (mp=bccs; mp; mp=mp->m_next) {
661 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
662 fprintf(out, "To: %s\n", mp->m_text);
663 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
664 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
665 fprintf(out, "------------\n");
668 if (execprogl("send", "send", bccdraft, (char *)NULL) != 0) {
669 admonish(invo_name, "Problems to send Bcc to %s",
677 * Do aliasing on a mailname linked list
678 * Begin at start->m_next
679 * End if m_next == NULL
680 * **end is set to the new end.
681 * Return the number of new mainames in the list
685 do_aliasing(struct mailname *start, struct mailname **end)
687 struct mailname *prev, *cur;
695 while (cur != NULL) {
697 cp = akvalue(cur->m_mbox);
698 if (strcmp(cp, cur->m_mbox) != 0) {
699 prev->m_next = cur->m_next;
700 if ((e = getmboxes(cp, &prev)) < 0) {
717 adios(EX_CONFIG, NULL, "can't parse alias %s: %s", cur->m_mbox, cp);
718 return 0; /* not reached */