Completely removed the backup-prefix (,). We move to +trash instead.
[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                                 done(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                                 done(1);
162                         case VERSIONSW:
163                                 print_version(invo_name);
164                                 done(1);
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                 done(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 all leading whitespace (even newlines) */
318         while (*str==' ' || *str=='\t' || *str=='\n') {
319                 str++;
320         }
321
322         if ((i = get_header(name, hdrtab)) == NOTOK) {
323                 /* no header we would care for: push it through */
324                 fprintf(out, "%s: %s", name, str);
325                 return;
326         }
327         /* it's one of the interesting headers */
328         hdr = &hdrtab[i];
329
330         if (hdr->flags & HIGN || !*str) {
331                 return;
332         }
333
334         if (hdr->flags & HBAD) {
335                 advise(NULL, "illegal header line -- %s:", name);
336                 badmsg++;
337                 return;
338         }
339
340         msgflags |= hdr->set;
341
342         if (hdr->flags & HFCC) {
343                 process_fcc(str);
344                 return;
345         }
346
347         if (hdr->flags & HBCC) {
348                 struct mailname *mp = NULL;
349
350                 /* Create list of Bcc addrs. */
351                 while ((cp = getname(str))) {
352                         mp = getm(cp, NULL, 0, AD_HOST, NULL);
353                         mp->m_next = bccs;  /* push */
354                         bccs = mp;
355                 }
356                 return;
357         }
358
359         if (aliasflg && hdr->flags & HTRY) {
360                 /*
361                 ** This header contains address(es) that we have to do
362                 ** alias expansion on.  Because of the saved state in
363                 ** getname we have to put all the addresses into a list.
364                 **/
365                 struct mailname *f = NULL;
366                 struct mailname *mp = NULL;
367
368                 while ((cp = getname(str))) {
369                         mp = getm(cp, NULL, 0, AD_HOST, NULL);
370                         if (!f) {
371                                 f = mp;
372                                 mp->m_next = mp;
373                         } else {
374                                 mp->m_next = f->m_next;
375                                 f->m_next = mp;
376                                 f = mp;
377                         }
378                 }
379                 f = mp->m_next;
380                 mp->m_next = NULL;
381                 /* Now munch on the list, possibly expanding aliases */
382                 putadr(name, f);
383                 return;
384         }
385
386         /*
387         ** The author(s) of spost decided that alias substitution wasn't
388         ** necessary for the non-HTRY headers.  Unfortunately, one of
389         ** those headers is "From:", and having alias substitution work on
390         ** that is extremely useful for someone with a lot of POP3 email
391         ** accounts or aliases.  post supports aliasing of "From:"...
392         **
393         ** Since "From:"-processing is incompletely implemented in this
394         ** unsupported and undocumented spost backend, I'm not going
395         ** to take the time to implement my new draft-From:-based email
396         ** address masquerading.  If I do ever implement it here, I'd almost
397         ** certainly want to implement "From:" line alias processing as well.
398         ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
399         */
400         /*
401         ** Although there is no masquerading anymore in mmh, we might want
402         ** to have aliasing of From: addresses. Think about it.
403         ** -- meillo@marmaro.de  2012-02
404         */
405
406         if (hdr->flags & HSUB) {
407                 subject = getcpy(str);
408         }
409         fprintf(out, "%s: %s", name, str);
410 }
411
412
413 /*
414 ** Add yet missing headers.
415 */
416 static void
417 finish_headers(FILE *out)
418 {
419         char *cp;
420         char from[BUFSIZ];  /* my network address */
421         char signature[BUFSIZ];  /* my signature */
422         char *resentstr = (msgstate == resent) ? "Resent-" : "";
423
424         if (!(msgflags & MDAT)) {
425                 fprintf(out, "%sDate: %s\n", resentstr, dtimenow());
426         }
427
428         strncpy(from, getusername(), sizeof(from));
429         if ((cp = getfullname()) && *cp) {
430                 snprintf(signature, sizeof(signature), "%s <%s>", cp, from);
431         } else {
432                 snprintf(signature, sizeof(signature), "%s", from);
433         }
434         if (!(msgflags & MFRM)) {
435                 fprintf(out, "%sFrom: %s\n", resentstr, signature);
436         } else {
437                 /* In case the From: header contains multiple addresses. */
438                 fprintf(out, "%sSender: %s\n", resentstr, from);
439         }
440         if (!(msgflags & MVIS)) {
441                 fprintf(out, "%sBcc: Blind Distribution List: ;\n", resentstr);
442         }
443         if (badmsg) {
444                 unlink(tmpfil);
445                 adios(NULL, "re-format message and try again");
446         }
447 }
448
449
450 /*
451 ** Return index of the requested header in the table, or NOTOK if missing.
452 */
453 static int
454 get_header(char *header, struct headers *table)
455 {
456         struct headers *h;
457
458         for (h=table; h->value; h++) {
459                 if (mh_strcasecmp(header, h->value)==0) {
460                         return (h - table);
461                 }
462         }
463
464         return NOTOK;
465 }
466
467
468 /*
469 ** output the address list for header "name".  The address list
470 ** is a linked list of mailname structs.  "nl" points to the head
471 ** of the list.  Alias substitution should be done on nl.
472 */
473 static void
474 putadr(char *name, struct mailname *nl)
475 {
476         struct mailname *mp, *mp2;
477         int linepos;
478         char *cp;
479         int namelen;
480
481         fprintf(out, "%s: ", name);
482         namelen = strlen(name) + 2;
483         linepos = namelen;
484
485         for (mp = nl; mp; ) {
486                 if (linepos > MAX_SM_FIELD) {
487                         fprintf(out, "\n%s: ", name);
488                         linepos = namelen;
489                 }
490                 if (mp->m_nohost) {
491                         /* a local name - see if it's an alias */
492                         cp = akvalue(mp->m_mbox);
493                         if (cp == mp->m_mbox) {
494                                 /* wasn't an alias - use what the user typed */
495                                 linepos = putone(mp->m_text, linepos, namelen);
496                         } else {
497                                 /* an alias - expand it */
498                                 while ((cp = getname(cp))) {
499                                         if (linepos > MAX_SM_FIELD) {
500                                                 fprintf(out, "\n%s: ", name);
501                                                 linepos = namelen;
502                                         }
503                                         mp2 = getm(cp, NULL, 0, AD_HOST, NULL);
504                                         if (akvisible()) {
505                                                 mp2->m_pers = getcpy(mp->m_mbox);
506                                                 linepos = putone(adrformat(mp2), linepos, namelen);
507                                         } else {
508                                                 linepos = putone(mp2->m_text,
509                                                                 linepos,
510                                                                 namelen);
511                                         }
512                                         mnfree(mp2);
513                                 }
514                         }
515                 } else {
516                         /* not a local name - use what the user typed */
517                         linepos = putone(mp->m_text, linepos, namelen);
518                 }
519                 mp2 = mp;
520                 mp = mp->m_next;
521                 mnfree(mp2);
522         }
523         putc('\n', out);
524 }
525
526 static int
527 putone(char *adr, int pos, int indent)
528 {
529         register int len;
530         static int linepos;
531
532         len = strlen(adr);
533         if (pos == indent)
534                 linepos = pos;
535         else if (linepos+len > OUTPUTLINELEN) {
536                 fprintf(out, ",\n%*s", indent, "");
537                 linepos = indent;
538                 pos += indent + 2;
539         } else {
540                 fputs(", ", out);
541                 linepos += 2;
542                 pos += 2;
543         }
544         fputs(adr, out);
545
546         linepos += len;
547         return (pos+len);
548 }
549
550
551 static void
552 process_fcc(char *str)
553 {
554         char *cp, *pp;
555         int state = 0;
556
557         if (strlen(str)+strlen(fccs) > sizeof fccs /2) {
558                 adios(NULL, "Too much Fcc data");
559         }
560         /* todo: better have three states: SEPARATOR, PLUS, WORD */
561         for (cp=pp=str; *cp; cp++) {
562                 switch (*cp) {
563                 case ' ':
564                 case '\t':
565                 case '\n':
566                 case ',':
567                         if (state != 0) {
568                                 state = 0;
569                                 *cp = '\0';
570                                 if (*pp=='+' || *pp=='@') {
571                                         strcat(fccs, " ");
572                                 } else {
573                                         strcat(fccs, " +");
574                                 }
575                                 strcat(fccs, pp);
576                         }
577                         break;
578                 default:
579                         if (state == 0) {
580                                 state = 1;
581                                 pp = cp;
582                         }
583                         break;
584                 }
585         }
586 }
587
588
589 static void
590 fcc(char *file, char *folders)
591 {
592         int status;
593         char cmd[BUFSIZ];
594
595         if (verbose) {
596                 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
597                                 folders);
598                 fflush(stdout);
599         }
600         if (100+strlen(file)+strlen(folders) > sizeof cmd) {
601                 adios(NULL, "Too much Fcc data");
602         }
603         /* hack: read from /dev/null and refile(1) won't question us */
604         snprintf(cmd, sizeof cmd, "</dev/null refile -link -file '%s' %s",
605                         file, folders);
606         status = system(cmd);
607         if (status == -1) {
608                 fprintf(stderr, "Skipped %sFcc %s: unable to system().\n",
609                                 msgstate == resent ? "Resent-" : "", folders);
610         } else if (status != 0) {
611                 fprintf(stderr, "%sFcc %s: Problems occured.\n",
612                                 msgstate == resent ? "Resent-" : "", folders);
613         }
614 }
615
616
617 /* BCC GENERATION */
618
619 static void
620 process_bccs(char *origmsg)
621 {
622         char *bccdraft = NULL;
623         char buf[BUFSIZ];
624         struct mailname *mp = NULL;
625         FILE *out = NULL;
626
627         for (mp=bccs; mp; mp=mp->m_next) {
628                 bccdraft = getcpy(m_mktemp2("/tmp/", invo_name, NULL, &out));
629                 fprintf(out, "To: %s\n", mp->m_text);
630                 fprintf(out, "Subject: [BCC] %s", subject ? subject : "");
631                 fprintf(out, "%s: %s\n", attach_hdr, origmsg);
632                 fprintf(out, "------------\n");
633                 fclose(out);
634
635                 snprintf(buf, sizeof buf, "send %s", bccdraft);
636                 if (system(buf) != 0) {
637                         admonish(invo_name, "Problems to send Bcc to %s",
638                                         mp->m_text);
639                         unlink(bccdraft);
640                 }
641         }
642 }