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