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