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