Fix autoconf check for mailspool world write permission
[mmh] / uip / spost.c
1 /*
2 ** spost.c -- feed messages to sendmail
3 **
4 ** This is a simpler, faster, replacement for "post" for use
5 ** when "sendmail" is the transport system.
6 **
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.
10 */
11
12 #include <h/mh.h>
13 #include <signal.h>
14 #include <h/addrsbr.h>
15 #include <h/aliasbr.h>
16 #include <h/dropsbr.h>
17 #include <h/tws.h>
18 #include <h/utils.h>
19 #include <unistd.h>
20 #include <locale.h>
21 #include <sysexits.h>
22
23 #define MAX_SM_FIELD 1476  /* < largest hdr field sendmail will accept */
24
25 static struct swit switches[] = {
26 #define VERBSW  0
27         { "verbose", 0 },
28 #define NVERBSW  1
29         { "noverbose", 2 },
30 #define VERSIONSW  2
31         { "Version", 0 },
32 #define HELPSW  3
33         { "help", 0 },
34 #define DEBUGSW  4
35         { "debug", -5 },
36 #define DISTSW  5
37         { "dist", -4 },  /* interface from dist */
38         { NULL, 0 }
39 };
40
41
42 /* flags for headers->flags */
43 #define HNOP  0x0000  /* just used to keep .set around */
44 #define HBAD  0x0001  /* bad header - don't let it through */
45 #define HADR  0x0002  /* header has an address field */
46 #define HSUB  0x0004  /* Subject: header */
47 #define HTRY  0x0008  /* try to send to addrs on header */
48 #define HBCC  0x0010  /* don't output this header */
49 #define HFCC  0x0020  /* FCC: type header */
50 #define HIGN  0x0040  /* ignore this header */
51
52 /* flags for headers->set */
53 #define MFRM  0x0001  /* we've seen a From: */
54 #define MDAT  0x0002  /* we've seen a Date: */
55 #define MRFM  0x0004  /* we've seen a Resent-From: */
56 #define MVIS  0x0008  /* we've seen sighted addrs */
57 #define MINV  0x0010  /* we've seen blind addrs */
58 #define MRDT  0x0020  /* we've seen a Resent-Date: */
59 #define MFMM  0x0040  /* The Mail is From a Alternative-Mailbox Addresse */
60
61 struct headers {
62         char *value;
63         unsigned int flags;
64         unsigned int set;
65 };
66
67 static struct headers NHeaders[] = {
68         { "Return-Path", HBAD, 0 },
69         { "Received", HBAD, 0 },
70         { "Reply-To", HADR, 0 },
71         { "From", HADR, MFRM },
72         { "Sender", HADR|HBAD, 0 },
73         { "Date", HNOP, MDAT },
74         { "Subject", HSUB, 0 },
75         { "To", HADR|HTRY, MVIS },
76         { "Cc", HADR|HTRY, MVIS },
77         { "Bcc", HADR|HTRY|HBCC, MINV },
78         { "Message-Id", HBAD, 0 },
79         { "Fcc", HFCC, 0 },
80         { "Envelope-From", HIGN, 0 },
81         { NULL, 0, 0 }
82 };
83
84 static struct headers RHeaders[] = {
85         { "Resent-Reply-To",   HADR, 0 },
86         { "Resent-From", HADR, MRFM },
87         { "Resent-Sender", HADR|HBAD, 0 },
88         { "Resent-Date", HNOP, MRDT },
89         { "Resent-Subject", HSUB, 0 },
90         { "Resent-To", HADR|HTRY, MVIS },
91         { "Resent-Cc", HADR|HTRY, MVIS },
92         { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
93         { "Resent-Message-Id", HBAD, 0 },
94         { "Resent-Fcc", HFCC, 0 },
95         { "Reply-To", HADR, 0 },
96         { "Fcc", HIGN, 0 },
97         { "Envelope-From", HIGN, 0 },
98         { NULL, 0, 0 }
99 };
100
101
102 static int badmsg = 0;
103 static int verbose = 0;
104 static int debug = 0;
105 static int aliasflg = 0;  /* if going to process aliases */
106
107 static unsigned msgflags = 0;  /* what we've seen */
108
109 static enum {
110         normal, resent
111 } msgstate = normal;
112
113 static char *tmpfil;
114
115 static char *subject = NULL;  /* the subject field for BCC'ing */
116 static char fccs[BUFSIZ] = "";
117 struct mailname *bccs = NULL;  /* list of the bcc recipients */
118 struct mailname *sender = NULL;
119
120 static struct headers *hdrtab;  /* table for the message we're doing */
121 static FILE *out;  /* output (temp) file */
122
123 /*
124 ** static prototypes
125 */
126 static void putfmt(char *, char *, FILE *);
127 static void finish_headers(FILE *);
128 static int get_header(char *, struct headers *);
129 static void putadr(char *, struct mailname *);
130 static int putone(char *, int, int);
131 static void process_fcc(char *);
132 static void fcc(char *, char *);
133 static void process_bccs(char *);
134
135
136 int
137 main(int argc, char **argv)
138 {
139         int state, compnum;
140         char *cp, *msg = NULL, **argp, **arguments;
141         char *sargv[16], buf[BUFSIZ], name[NAMESZ];
142         FILE *in;
143
144         setlocale(LC_ALL, "");
145         invo_name = mhbasename(argv[0]);
146
147         context_read();
148
149         arguments = getarguments(invo_name, argc, argv, 0);
150         argp = arguments;
151
152         while ((cp = *argp++)) {
153                 if (*cp == '-') {
154                         switch (smatch(++cp, switches)) {
155                         case AMBIGSW:
156                                 ambigsw(cp, switches);
157                                 exit(EX_USAGE);
158                         case UNKWNSW:
159                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
160
161                         case HELPSW:
162                                 snprintf(buf, sizeof(buf),
163                                                 "%s [switches] file",
164                                                 invo_name);
165                                 print_help(buf, switches, 1);
166                                 exit(argc == 2 ? EX_OK : EX_USAGE);
167                         case VERSIONSW:
168                                 print_version(invo_name);
169                                 exit(argc == 2 ? EX_OK : EX_USAGE);
170
171                         case DEBUGSW:
172                                 debug++;
173                                 continue;
174
175                         case DISTSW:
176                                 msgstate = resent;
177                                 continue;
178
179                         case VERBSW:
180                                 verbose++;
181                                 continue;
182                         case NVERBSW:
183                                 verbose = 0;
184                                 continue;
185                         }
186                 }
187                 if (msg)
188                         adios(EX_USAGE, NULL, "only one message at a time!");
189                 else
190                         msg = cp;
191         }
192
193         if (!msg)
194                 adios(EX_USAGE, NULL, "usage: %s [switches] file", invo_name);
195
196         if ((in = fopen(msg, "r")) == NULL)
197                 adios(EX_IOERR, msg, "unable to open");
198
199         if (debug) {
200                 verbose++;
201                 out = stdout;
202         } else {
203                 tmpfil = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
204         }
205
206         /* check for "Aliasfile:" profile entry */
207         if ((cp = context_find("Aliasfile"))) {
208                 char *dp, **ap; 
209
210                 aliasflg = 1;
211                 for (ap=brkstring(dp=getcpy(cp), " ", "\n"); ap && *ap;
212                                 ap++) {
213                         if ((state = alias(etcpath(*ap))) != AK_OK) {
214                                 adios(EX_IOERR, NULL, "aliasing error in file %s: %s",
215                                                 *ap, akerror(state));
216                         }
217                 }
218         }
219
220
221         hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
222
223         for (compnum = 1, state = FLD;;) {
224                 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
225                 case FLD:
226                 case FLDPLUS:
227                         compnum++;
228                         cp = getcpy(buf);
229                         while (state == FLDPLUS) {
230                                 state = m_getfld(state, name, buf,
231                                                 sizeof(buf), in);
232                                 cp = add(buf, cp);
233                         }
234                         putfmt(name, cp, out);
235                         free(cp);
236                         continue;
237
238                 case BODY:
239                         finish_headers(out);
240                         fprintf(out, "\n%s", buf);
241                         while (state == BODY) {
242                                 state = m_getfld(state, name, buf,
243                                                 sizeof(buf), in);
244                                 fputs(buf, out);
245                         }
246                         break;
247
248                 case FILEEOF:
249                         finish_headers(out);
250                         break;
251
252                 case LENERR:
253                 case FMTERR:
254                         adios(EX_DATAERR, NULL, "message format error in component #%d",
255                                         compnum);
256
257                 default:
258                         adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
259                 }
260                 break;
261         }
262         fclose(in);
263
264         if (debug) {
265                 /* stop here */
266                 exit(EX_OK);
267         }
268
269         fclose(out);
270
271         /* process Fcc */
272         if (*fccs) {
273                 fcc(tmpfil, fccs);
274         }
275
276         if (bccs) {
277                 process_bccs(tmpfil);
278                 if (!(msgflags & MVIS)) {
279                         /* only Bcc rcpts: we're finished here */
280                         unlink(tmpfil);
281                         exit(EX_OK);
282                 }
283         }
284
285         /*
286         ** re-open the temp file, unlink it and exec sendmail, giving it
287         ** the msg temp file as std in.
288         */
289         if (!freopen(tmpfil, "r", stdin)) {
290                 adios(EX_IOERR, tmpfil, "can't reopen for sendmail");
291         }
292         unlink(tmpfil);
293
294         argp = sargv;
295         *argp++ = "send-mail";
296         *argp++ = "-m";  /* send to me too */
297         *argp++ = "-t";  /* read msg for recipients */
298         *argp++ = "-i";  /* don't stop on "." */
299         if (verbose) {
300                 *argp++ = "-v";
301         }
302         *argp = NULL;
303         execv(sendmail, sargv);
304         adios(EX_OSERR, sendmail, "can't exec");
305         return -1;
306 }
307
308
309 /* DRAFT GENERATION */
310
311 static void
312 putfmt(char *name, char *str, FILE *out)
313 {
314         int i;
315         char *cp;
316         struct headers *hdr;
317
318         /* remove leading whitespace */
319         while (*str==' ' || *str=='\t') {
320                 str++;
321         }
322
323         if ((i = get_header(name, hdrtab)) == NOTOK) {
324                 /* no header we would care for */
325                 if (mh_strcasecmp(name, attach_hdr)!=0 &&
326                                 mh_strcasecmp(name, sign_hdr)!=0 &&
327                                 mh_strcasecmp(name, enc_hdr)!=0) {
328                         /* push it through */
329                         fprintf(out, "%s: %s", name, str);
330                 }
331                 return;
332         }
333         /* it's one of the interesting headers */
334         hdr = &hdrtab[i];
335
336         if (hdr->flags & HIGN || strcmp(str, "\n")==0) {
337                 return;
338         }
339
340         if (hdr->flags & HBAD) {
341                 advise(NULL, "illegal header line -- %s:", name);
342                 badmsg++;
343                 return;
344         }
345
346         msgflags |= hdr->set;
347
348         if (hdr->flags & HFCC) {
349                 process_fcc(str);
350                 return;
351         }
352
353         if (hdr->flags & HBCC) {
354                 struct mailname *mp = NULL;
355
356                 /* Create list of Bcc addrs. */
357                 while ((cp = getname(str))) {
358                         mp = getm(cp, NULL, 0, AD_HOST, NULL);
359                         mp->m_next = bccs;  /* push */
360                         bccs = mp;
361                 }
362                 return;
363         }
364
365         if (aliasflg && hdr->flags & HTRY) {
366                 /*
367                 ** This header contains address(es) that we have to do
368                 ** alias expansion on.  Because of the saved state in
369                 ** getname we have to put all the addresses into a list.
370                 **/
371                 struct mailname *f = NULL;
372                 struct mailname *mp = NULL;
373
374                 while ((cp = getname(str))) {
375                         mp = getm(cp, NULL, 0, AD_HOST, NULL);
376                         if (!f) {
377                                 f = mp;
378                                 mp->m_next = mp;
379                         } else {
380                                 mp->m_next = f->m_next;
381                                 f->m_next = mp;
382                                 f = mp;
383                         }
384                 }
385                 f = mp->m_next;
386                 mp->m_next = NULL;
387                 /* Now munch on the list, possibly expanding aliases */
388                 putadr(name, f);
389                 return;
390         }
391
392         /*
393         ** The author(s) of spost decided that alias substitution wasn't
394         ** necessary for the non-HTRY headers.  Unfortunately, one of
395         ** those headers is "From:", and having alias substitution work on
396         ** that is extremely useful for someone with a lot of POP3 email
397         ** accounts or aliases.  post supports aliasing of "From:"...
398         **
399         ** Since "From:"-processing is incompletely implemented in this
400         ** unsupported and undocumented spost backend, I'm not going
401         ** to take the time to implement my new draft-From:-based email
402         ** address masquerading.  If I do ever implement it here, I'd almost
403         ** certainly want to implement "From:" line alias processing as well.
404         ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
405         */
406         /*
407         ** Although there is no masquerading anymore in mmh, we might want
408         ** to have aliasing of From: addresses. Think about it.
409         ** -- meillo@marmaro.de  2012-02
410         */
411
412         if (hdr->set & MFRM) {
413
414                 struct mailname *mp = NULL;
415                 struct mailname *my = NULL;
416                 unsigned int fromcnt = 0;
417
418                 /* This is need because the addresse parser hold global state */
419                 ismymbox(NULL);
420
421                 while ((cp = getname(str)) != NULL) {
422                         fromcnt++;
423                         mp = getm(cp, NULL, 0, AD_NAME, NULL);
424                         if (ismymbox(mp)) {
425                                 msgflags |= MFMM;
426                                 if (my == NULL) {
427                                         my = mp;
428                                 } else {
429                                         mnfree(mp);
430                                         mp = NULL;
431                                 }
432                         } else {
433                                 mnfree(mp);
434                                 mp = NULL;
435                         }
436                 }
437
438                 if (fromcnt > 1) {
439                         sender = my;
440                 } else {
441                         mnfree(my);
442                 }
443
444                 free(cp);
445                 cp = NULL;
446
447         }
448
449         if (hdr->flags & HSUB) {
450                 subject = getcpy(str);
451         }
452         fprintf(out, "%s: %s", name, str);
453 }
454
455
456 /*
457 ** Add yet missing headers.
458 */
459 static void
460 finish_headers(FILE *out)
461 {
462         char *cp;
463         char from[BUFSIZ];  /* my network address */
464         char signature[BUFSIZ];  /* my signature */
465         char *resentstr = (msgstate == resent) ? "Resent-" : "";
466
467         if (!(msgflags & MDAT)) {
468                 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
469         }
470
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);
475         } else {
476                 snprintf(from, sizeof(from), "%s@%s", getusername(), LocalName());
477                 if ((cp = getfullname()) && *cp) {
478                         snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
479                 } else {
480                         snprintf(signature, sizeof(signature), "%s", from);
481                 }
482         }
483         if (!(msgflags & MFRM)) {
484                 fprintf(out, "%sFrom: %s\n", resentstr, signature);
485         } else {
486                 /*
487                 ** Add a Sender: header because the From: header could
488                 ** be fake or contain multiple addresses.
489                 */
490                 if (!(msgflags & MFMM) || sender != NULL) {
491                         fprintf(out, "%sSender: %s\n", resentstr, signature);
492                 }
493         }
494         if (!(msgflags & MVIS)) {
495                 fprintf(out, "%sBcc: undisclosed-recipients:;\n", resentstr);
496         }
497         if (badmsg) {
498                 unlink(tmpfil);
499                 adios(EX_DATAERR, NULL, "re-format message and try again");
500         }
501 }
502
503
504 /*
505 ** Return index of the requested header in the table, or NOTOK if missing.
506 */
507 static int
508 get_header(char *header, struct headers *table)
509 {
510         struct headers *h;
511
512         for (h=table; h->value; h++) {
513                 if (mh_strcasecmp(header, h->value)==0) {
514                         return (h - table);
515                 }
516         }
517
518         return NOTOK;
519 }
520
521
522 /*
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.
526 */
527 static void
528 putadr(char *name, struct mailname *nl)
529 {
530         struct mailname *mp, *mp2;
531         int linepos;
532         char *cp;
533         int namelen;
534
535         fprintf(out, "%s: ", name);
536         namelen = strlen(name) + 2;
537         linepos = namelen;
538
539         for (mp = nl; mp; ) {
540                 if (linepos > MAX_SM_FIELD) {
541                         fprintf(out, "\n%s: ", name);
542                         linepos = namelen;
543                 }
544                 if (mp->m_nohost) {
545                         /* a local name - see if it's an alias */
546                         cp = akvalue(mp->m_mbox);
547                         if (cp == mp->m_mbox) {
548                                 /* wasn't an alias - use what the user typed */
549                                 linepos = putone(mp->m_text, linepos, namelen);
550                         } else {
551                                 /* an alias - expand it */
552                                 while ((cp = getname(cp))) {
553                                         if (linepos > MAX_SM_FIELD) {
554                                                 fprintf(out, "\n%s: ", name);
555                                                 linepos = namelen;
556                                         }
557                                         mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
558                                         if (akvisible()) {
559                                                 mp2->m_pers = getcpy(mp->m_mbox);
560                                                 linepos = putone(adrformat(mp2), linepos, namelen);
561                                         } else {
562                                                 linepos = putone(mp2->m_text,
563                                                                 linepos,
564                                                                 namelen);
565                                         }
566                                         mnfree(mp2);
567                                 }
568                         }
569                 } else {
570                         /* not a local name - use what the user typed */
571                         linepos = putone(mp->m_text, linepos, namelen);
572                 }
573                 mp2 = mp;
574                 mp = mp->m_next;
575                 mnfree(mp2);
576         }
577         putc('\n', out);
578 }
579
580 static int
581 putone(char *adr, int pos, int indent)
582 {
583         register int len;
584         static int linepos;
585
586         len = strlen(adr);
587         if (pos == indent)
588                 linepos = pos;
589         else if (linepos+len > OUTPUTLINELEN) {
590                 fprintf(out, ",\n%*s", indent, "");
591                 linepos = indent;
592                 pos += indent + 2;
593         } else {
594                 fputs(", ", out);
595                 linepos += 2;
596                 pos += 2;
597         }
598         fputs(adr, out);
599
600         linepos += len;
601         return (pos+len);
602 }
603
604
605 static void
606 process_fcc(char *str)
607 {
608         char *cp, *pp;
609         int state = 0;
610
611         if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
612                 adios(EX_DATAERR, NULL, "Too much Fcc data");
613         }
614         /* todo: better have three states: SEPARATOR, PLUS, WORD */
615         for (cp=pp=str; *cp; cp++) {
616                 switch (*cp) {
617                 case ' ':
618                 case '\t':
619                 case '\n':
620                 case ',':
621                         if (state != 0) {
622                                 state = 0;
623                                 *cp = '\0';
624                                 if (*pp=='+' || *pp=='@') {
625                                         strcat(fccs, " ");
626                                 } else {
627                                         strcat(fccs, " +");
628                                 }
629                                 strcat(fccs, pp);
630                         }
631                         break;
632                 default:
633                         if (state == 0) {
634                                 state = 1;
635                                 pp = cp;
636                         }
637                         break;
638                 }
639         }
640 }
641
642
643 static void
644 fcc(char *file, char *folders)
645 {
646         int status;
647         char cmd[BUFSIZ];
648
649         if (verbose) {
650                 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
651                                 folders);
652                 fflush(stdout);
653         }
654         if (100+strlen(file)+strlen(folders) > sizeof cmd) {
655                 adios(EX_DATAERR, NULL, "Too much Fcc data");
656         }
657         /* hack: read from /dev/null and refile(1) won't question us */
658         snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
659                         file, folders);
660         status = system(cmd);
661         if (status == -1) {
662                 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
663                                 msgstate == resent ? "Resent-" : "", folders);
664         } else if (status != 0) {
665                 fprintf(stderr, "%sFcc %s: Problems occured.\n",
666                                 msgstate == resent ? "Resent-" : "", folders);
667         }
668 }
669
670
671 /* BCC GENERATION */
672
673 static void
674 process_bccs(char *origmsg)
675 {
676         char *bccdraft = NULL;
677         struct mailname *mp = NULL;
678         FILE *out = NULL;
679
680         for (mp=bccs; mp; mp=mp->m_next) {
681                 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
682                 fprintf(out, "To: %s\n", mp->m_text);
683                 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
684                 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
685                 fprintf(out, "------------\n");
686                 fclose(out);
687
688                 if (execprogl("send", "send", bccdraft, (char *)NULL) != 0) {
689                         admonish(invo_name, "Problems to send Bcc to %s",
690                                         mp->m_text);
691                         unlink(bccdraft);
692                 }
693         }
694 }