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