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