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