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