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