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