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