0679ac6bb6fa20a637d4cdfad1c68dc85376ca66
[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/mts.h>
19 #include <h/utils.h>
20
21 #define MAX_SM_FIELD 1476  /* < largest hdr field sendmail will accept */
22 #define FCCS 10  /* max number of fccs allowed */
23
24 struct swit switches[] = {
25 #define FILTSW  0
26         { "filter filterfile", 0 },
27 #define NFILTSW  1
28         { "nofilter", 0 },
29 #define FRMTSW  2
30         { "format", 0 },
31 #define NFRMTSW  3
32         { "noformat", 0 },
33 #define REMVSW  4
34         { "remove", 0 },
35 #define NREMVSW  5
36         { "noremove", 0 },
37 #define VERBSW  6
38         { "verbose", 0 },
39 #define NVERBSW  7
40         { "noverbose", 0 },
41 #define WATCSW  8
42         { "watch", 0 },
43 #define NWATCSW  9
44         { "nowatch", 0 },
45 #define BACKSW  10
46         { "backup", 0 },
47 #define NBACKSW  11
48         { "nobackup", 0 },
49 #define ALIASW  12
50         { "alias aliasfile", 0 },
51 #define NALIASW  13
52         { "noalias", 0 },
53 #define WIDTHSW  14
54         { "width columns", 0 },
55 #define VERSIONSW  15
56         { "version", 0 },
57 #define HELPSW  16
58         { "help", 0 },
59 #define DEBUGSW  17
60         { "debug", -5 },
61 #define DISTSW  18
62         { "dist", -4 },  /* interface from dist */
63 #define PUSHSW  19  /* fork to sendmail then exit */
64         { "push", -4 },
65 #define NPUSHSW  20  /* exec sendmail */
66         { "nopush", -6 },
67 #define LIBSW  21
68         { "library directory", -7 },
69 #define ANNOSW  22
70         { "idanno number", -6 },
71         { NULL, 0 }
72 };
73
74
75 /* flags for headers->flags */
76 #define HNOP  0x0000  /* just used to keep .set around */
77 #define HBAD  0x0001  /* bad header - don't let it through */
78 #define HADR  0x0002  /* header has an address field */
79 #define HSUB  0x0004  /* Subject: header */
80 #define HTRY  0x0008  /* try to send to addrs on header */
81 #define HBCC  0x0010  /* don't output this header */
82 #define HMNG  0x0020  /* mung this header */
83 #define HNGR  0x0040  /* no groups allowed in this header */
84 #define HFCC  0x0080  /* FCC: type header */
85 #define HNIL  0x0100  /* okay for this header not to have addrs */
86 #define HIGN  0x0200  /* ignore this header */
87
88 /* flags for headers->set */
89 #define MFRM  0x0001  /* we've seen a From: */
90 #define MDAT  0x0002  /* we've seen a Date: */
91 #define MRFM  0x0004  /* we've seen a Resent-From: */
92 #define MVIS  0x0008  /* we've seen sighted addrs */
93 #define MINV  0x0010  /* we've seen blind addrs */
94 #define MRDT  0x0020  /* we've seen a Resent-Date: */
95
96 struct headers {
97         char *value;
98         unsigned int flags;
99         unsigned int set;
100 };
101
102
103 static struct headers NHeaders[] = {
104         { "Return-Path", HBAD, 0 },
105         { "Received", HBAD, 0 },
106         { "Reply-To", HADR|HNGR, 0 },
107         { "From", HADR|HNGR, MFRM },
108         { "Sender", HADR|HBAD, 0 },
109         { "Date", HNOP, MDAT },
110         { "Subject", HSUB, 0 },
111         { "To", HADR|HTRY, MVIS },
112         { "cc", HADR|HTRY, MVIS },
113         { "Bcc", HADR|HTRY|HBCC|HNIL, MINV },
114         { "Message-Id", HBAD, 0 },
115         { "Fcc", HFCC, 0 },
116         { NULL, 0, 0 }
117 };
118
119 static struct headers RHeaders[] = {
120         { "Resent-Reply-To",   HADR|HNGR, 0 },
121         { "Resent-From", HADR|HNGR, MRFM },
122         { "Resent-Sender", HADR|HBAD, 0 },
123         { "Resent-Date", HNOP, MRDT },
124         { "Resent-Subject", HSUB, 0 },
125         { "Resent-To", HADR|HTRY, MVIS },
126         { "Resent-cc", HADR|HTRY, MVIS },
127         { "Resent-Bcc", HADR|HTRY|HBCC, MINV },
128         { "Resent-Message-Id", HBAD, 0 },
129         { "Resent-Fcc", HFCC, 0 },
130         { "Reply-To", HADR, 0 },
131         { "Fcc", HIGN, 0 },
132         { NULL, 0, 0 }
133 };
134
135
136 static short fccind = 0;  /* index into fccfold[] */
137
138 static int badmsg = 0;  /* message has bad semantics */
139 static int verbose = 0;  /* spell it out */
140 static int debug = 0;  /* debugging post */
141 static int rmflg = 1;  /* remove temporary file when done */
142 static int watch = 0;  /* watch the delivery process */
143 static int backflg = 0;  /* rename input file as *.bak when done */
144 static int pushflg = 0;  /* if going to fork to sendmail */
145 static int aliasflg = -1;  /* if going to process aliases */
146 static int outputlinelen=72;
147
148 static unsigned msgflags = 0;  /* what we've seen */
149
150 static enum {
151         normal, resent
152 } msgstate = normal;
153
154 static char tmpfil[] = "/tmp/pstXXXXXX";
155
156 static char from[BUFSIZ];  /* my network address */
157 static char signature[BUFSIZ];  /* my signature */
158 static char *filter = NULL;  /* the filter for BCC'ing */
159 static char *subject = NULL;  /* the subject field for BCC'ing */
160 static char *fccfold[FCCS];  /* foldernames for FCC'ing */
161
162 static struct headers *hdrtab;  /* table for the message we're doing */
163 static FILE *out;  /* output (temp) file */
164
165 extern char *sendmail;
166
167 /*
168 ** external prototypes
169 */
170 extern char *getfullname(void);
171 extern char *getusername(void);
172
173 extern boolean  draft_from_masquerading;  /* defined in mts.c */
174
175 /*
176 ** static prototypes
177 */
178 static void putfmt(char *, char *, FILE *);
179 static void start_headers(void);
180 static void finish_headers(FILE *);
181 static int get_header(char *, struct headers *);
182 static void putadr(char *, struct mailname *);
183 static int putone(char *, int, int);
184 static void insert_fcc(struct headers *, unsigned char *);
185 static void file(char *);
186 static void fcc(char *, char *);
187
188 #if 0
189 static void die(char *, char *, ...);
190 static void make_bcc_file(void);
191 #endif
192
193
194 int
195 main(int argc, char **argv)
196 {
197         int state, i, pid, compnum;
198         char *cp, *msg = NULL, **argp, **arguments;
199         char *sargv[16], buf[BUFSIZ], name[NAMESZ];
200         FILE *in;
201
202 #ifdef LOCALE
203         setlocale(LC_ALL, "");
204 #endif
205         invo_name = mhbasename(argv[0]);
206
207         /* foil search of user profile/context */
208         if (context_foil(NULL) == -1)
209                 done(1);
210
211         mts_init(invo_name);
212         arguments = getarguments(invo_name, argc, argv, 0);
213         argp = arguments;
214
215         while ((cp = *argp++)) {
216                 if (*cp == '-') {
217                         switch (smatch(++cp, switches)) {
218                                 case AMBIGSW:
219                                         ambigsw(cp, switches);
220                                         done(1);
221                                 case UNKWNSW:
222                                         adios(NULL, "-%s unknown", cp);
223
224                                 case HELPSW:
225                                         snprintf(buf, sizeof(buf), "%s [switches] file", invo_name);
226                                         print_help(buf, switches, 1);
227                                         done(1);
228                                 case VERSIONSW:
229                                         print_version(invo_name);
230                                         done(1);
231
232                                 case DEBUGSW:
233                                         debug++;
234                                         continue;
235
236                                 case DISTSW:
237                                         msgstate = resent;
238                                         continue;
239
240                                 case FILTSW:
241                                         if (!(filter = *argp++) ||
242                                                         *filter == '-')
243                                                 adios(NULL, "missing argument to %s", argp[-2]);
244                                         continue;
245                                 case NFILTSW:
246                                         filter = NULL;
247                                         continue;
248
249                                 case REMVSW:
250                                         rmflg++;
251                                         continue;
252                                 case NREMVSW:
253                                         rmflg = 0;
254                                         continue;
255
256                                 case BACKSW:
257                                         backflg++;
258                                         continue;
259                                 case NBACKSW:
260                                         backflg = 0;
261                                         continue;
262
263                                 case VERBSW:
264                                         verbose++;
265                                         continue;
266                                 case NVERBSW:
267                                         verbose = 0;
268                                         continue;
269
270                                 case WATCSW:
271                                         watch++;
272                                         continue;
273                                 case NWATCSW:
274                                         watch = 0;
275                                         continue;
276
277                                 case PUSHSW:
278                                         pushflg++;
279                                         continue;
280                                 case NPUSHSW:
281                                         pushflg = 0;
282                                         continue;
283
284                                 case ALIASW:
285                                         if (!(cp = *argp++) || *cp == '-')
286                                                 adios(NULL, "missing argument to %s", argp[-2]);
287                                         if (aliasflg < 0) {
288                                                 /* load default aka's */
289                                                 alias(AliasFile);
290                                         }
291                                         aliasflg = 1;
292                                         if ((state = alias(cp)) != AK_OK)
293                                                 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state) );
294                                         continue;
295                                 case NALIASW:
296                                         aliasflg = 0;
297                                         continue;
298
299                                 case WIDTHSW:
300                                         if (!(cp = *argp++) || *cp == '-')
301                                                 adios(NULL, "missing argument to %s", argp[-2]);
302                                         outputlinelen = atoi(cp);
303                                         if (outputlinelen <= 10)
304                                                 outputlinelen = 72;
305                                         continue;
306
307                                 case LIBSW:
308                                         if (!(cp = *argp++) || *cp == '-')
309                                                 adios(NULL, "missing argument to %s", argp[-2]);
310                                         /* create a minimal context */
311                                         if (context_foil(cp) == -1)
312                                                 done(1);
313                                         continue;
314
315                                 case ANNOSW:
316                                         /* -idanno switch ignored */
317                                         if (!(cp = *argp++) || *cp == '-')
318                                                 adios(NULL, "missing argument to %s", argp[-2]);
319                                         continue;
320                         }
321                 }
322                 if (msg)
323                         adios(NULL, "only one message at a time!");
324                 else
325                         msg = cp;
326         }
327
328         if (aliasflg < 0)
329                 alias(AliasFile);  /* load default aka's */
330
331         if (!msg)
332                 adios(NULL, "usage: %s [switches] file", invo_name);
333
334         if ((in = fopen(msg, "r")) == NULL)
335                 adios(msg, "unable to open");
336
337         start_headers();
338         if (debug) {
339                 verbose++;
340                 out = stdout;
341         }
342         else {
343 #ifdef HAVE_MKSTEMP
344                         if ((out = fdopen( mkstemp(tmpfil), "w" )) == NULL )
345                                 adios(tmpfil, "unable to create");
346 #else
347                         mktemp(tmpfil);
348                         if ((out = fopen(tmpfil, "w")) == NULL)
349                                 adios(tmpfil, "unable to create");
350                         chmod(tmpfil, 0600);
351 #endif
352                 }
353
354         hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
355
356         for (compnum = 1, state = FLD;;) {
357                 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
358                         case FLD:
359                                 compnum++;
360                                 putfmt(name, buf, out);
361                                 continue;
362
363                         case FLDPLUS:
364                                 compnum++;
365                                 cp = add(buf, cp);
366                                 while (state == FLDPLUS) {
367                                         state = m_getfld(state, name, buf,
368                                                         sizeof(buf), in);
369                                         cp = add(buf, cp);
370                                 }
371                                 putfmt(name, cp, out);
372                                 free(cp);
373                                 continue;
374
375                         case BODY:
376                                 finish_headers(out);
377                                 fprintf(out, "\n%s", buf);
378                                 while (state == BODY) {
379                                         state = m_getfld(state, name, buf,
380                                                         sizeof(buf), in);
381                                         fputs(buf, out);
382                                 }
383                                 break;
384
385                         case FILEEOF:
386                                 finish_headers(out);
387                                 break;
388
389                         case LENERR:
390                         case FMTERR:
391                                 adios(NULL, "message format error in component #%d", compnum);
392
393                         default:
394                                 adios(NULL, "getfld() returned %d", state);
395                 }
396                 break;
397         }
398
399         fclose(in);
400         if (backflg) {
401                 strncpy(buf, m_backup(msg), sizeof(buf));
402                 if (rename(msg, buf) == NOTOK)
403                         advise(buf, "unable to rename %s to", msg);
404         }
405
406         if (debug) {
407                 done(0);
408         }
409         else
410                 fclose(out);
411
412         file(tmpfil);
413
414         /*
415         ** re-open the temp file, unlink it and exec sendmail, giving it
416         ** the msg temp file as std in.
417         */
418         if (freopen(tmpfil, "r", stdin) == NULL)
419                 adios(tmpfil, "can't reopen for sendmail");
420         if (rmflg)
421                 unlink(tmpfil);
422
423         argp = sargv;
424         *argp++ = "send-mail";
425         *argp++ = "-m";  /* send to me too */
426         *argp++ = "-t";  /* read msg for recipients */
427         *argp++ = "-i";  /* don't stop on "." */
428         if (watch || verbose)
429                 *argp++ = "-v";
430         *argp = NULL;
431
432         if (pushflg && !(watch || verbose)) {
433                 /* fork to a child to run sendmail */
434                 for (i=0; (pid = vfork()) == NOTOK && i < 5; i++)
435                         sleep(5);
436                 switch (pid) {
437                         case NOTOK:
438                                 fprintf(verbose ? stdout : stderr,
439                                                 "%s: can't fork to %s\n",
440                                                 invo_name, sendmail);
441                                 exit(-1);
442                         case OK:
443                                 /* we're the child .. */
444                                 break;
445                         default:
446                                 exit(0);
447                 }
448         }
449         execv(sendmail, sargv);
450         adios(sendmail, "can't exec");
451         return 0;  /* dead code to satisfy the compiler */
452 }
453
454 /* DRAFT GENERATION */
455
456 static void
457 putfmt(char *name, char *str, FILE *out)
458 {
459         int i;
460         char *cp, *pp;
461         struct headers *hdr;
462
463         while (*str == ' ' || *str == '\t')
464                 str++;
465
466         if ((i = get_header(name, hdrtab)) == NOTOK) {
467                 fprintf(out, "%s: %s", name, str);
468                 return;
469         }
470
471         hdr = &hdrtab[i];
472         if (hdr->flags & HIGN)
473                 return;
474         if (hdr->flags & HBAD) {
475                 advise(NULL, "illegal header line -- %s:", name);
476                 badmsg++;
477                 return;
478         }
479         msgflags |= hdr->set;
480
481         if (hdr->flags & HSUB)
482                 subject = subject ? add(str, add("\t", subject)) :
483                                 getcpy(str);
484
485         if (hdr->flags & HFCC) {
486                 if ((cp = strrchr(str, '\n')))
487                         *cp = 0;
488                 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
489                         *cp++ = 0;
490                         insert_fcc(hdr, pp);
491                 }
492                 insert_fcc(hdr, pp);
493                 return;
494         }
495
496 #ifdef notdef
497         if (hdr->flags & HBCC) {
498                 insert_bcc(str);
499                 return;
500         }
501 #endif /* notdef */
502
503         if (*str != '\n' && *str != '\0') {
504                 if (aliasflg && hdr->flags & HTRY) {
505                         /*
506                         ** this header contains address(es) that we have to do
507                         ** alias expansion on.  Because of the saved state in
508                         ** getname we have to put all the addresses into a
509                         ** list. We then let putadr munch on that list,
510                         ** possibly expanding aliases.
511                         **/
512                         register struct mailname *f = 0;
513                         register struct mailname *mp = 0;
514
515                         while ((cp = getname(str))) {
516                                 mp = getm( cp, NULL, 0, AD_HOST, NULL);
517                                 if (f == 0) {
518                                         f = mp;
519                                         mp->m_next = mp;
520                                 } else {
521                                         mp->m_next = f->m_next;
522                                         f->m_next = mp;
523                                         f = mp;
524                                 }
525                         }
526                         f = mp->m_next; mp->m_next = 0;
527                         putadr( name, f );
528                 } else {
529                         /*
530                         ** The author(s) of spost decided that alias
531                         ** substitution wasn't necessary for the non-HTRY
532                         ** headers.  Unfortunately, one of those headers
533                         ** is "From:", and having alias substitution
534                         ** work on that is extremely useful for someone
535                         ** with a lot of POP3 email accounts or aliases.
536                         ** post supports aliasing of "From:"...
537                         **
538                         ** Since "From:"-processing is incompletely
539                         ** implemented in this unsupported and
540                         ** undocumented spost backend, I'm not
541                         ** going to take the time to implement my new
542                         ** draft-From:-based email address masquerading.
543                         ** If I do ever implement it here, I'd almost
544                         ** certainly want to implement "From:" line
545                         ** alias processing as well.
546                         ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
547                         */
548                         fprintf(out, "%s: %s", name, str );
549                 }
550         }
551 }
552
553
554 static void
555 start_headers(void)
556 {
557         char *cp;
558         char sigbuf[BUFSIZ];
559
560         strncpy(from, getusername(), sizeof(from));
561
562         if ((cp = getfullname()) && *cp) {
563                 strncpy(sigbuf, cp, sizeof(sigbuf));
564                 snprintf(signature, sizeof(signature), "%s <%s>",
565                                 sigbuf,  from);
566         } else
567                 snprintf(signature, sizeof(signature), "%s",  from);
568 }
569
570
571 static void
572 finish_headers(FILE *out)
573 {
574         switch (msgstate) {
575                 case normal:
576                         if (!(msgflags & MDAT))
577                                 fprintf(out, "Date: %s\n", dtimenow(0));
578
579                         if (msgflags & MFRM) {
580                                 /*
581                                 ** There was already a From: in the draft.
582                                 ** Don't add one.
583                                 */
584                                 if (!draft_from_masquerading)
585                                         /*
586                                         ** mts.conf didn't contain
587                                         ** "masquerade:[...]draft_from[...]"
588                                         ** so we'll reveal the user's
589                                         ** actual account@thismachine
590                                         ** address in a Sender: header
591                                         ** (and use it as the envelope
592                                         ** From: later).
593                                         */
594                                         fprintf(out, "Sender: %s\n", from);
595                         } else
596                                 fprintf(out, "From: %s\n", signature);
597
598 #ifdef notdef
599                         if (!(msgflags & MVIS))
600                                 fprintf(out, "Bcc: Blind Distribution List: ;\n");
601 #endif /* notdef */
602                         break;
603
604                 case resent:
605                         if (!(msgflags & MRDT))
606                                 fprintf(out, "Resent-Date: %s\n", dtimenow(0));
607                         if (msgflags & MRFM) {
608                                 /*
609                                 ** There was already a Resent-From: in draft.
610                                 ** Don't add one.
611                                 */
612                                 if (!draft_from_masquerading)
613                                         /*
614                                         ** mts.conf didn't contain
615                                         ** "masquerade:[...]draft_from[...]"
616                                         ** so we'll reveal the user's
617                                         ** actual account@thismachine
618                                         ** address in a Sender: header
619                                         ** (and use it as the envelope
620                                         ** From: later).
621                                         */
622                                         fprintf(out, "Resent-Sender: %s\n",
623                                                         from);
624                         } else
625                                 /* Construct a Resent-From: header. */
626                                 fprintf(out, "Resent-From: %s\n", signature);
627 #ifdef notdef
628                         if (!(msgflags & MVIS))
629                                 fprintf(out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
630 #endif /* notdef */
631                         break;
632         }
633
634         if (badmsg)
635                 adios(NULL, "re-format message and try again");
636 }
637
638
639 static int
640 get_header(char *header, struct headers *table)
641 {
642         struct headers *h;
643
644         for (h = table; h->value; h++)
645                 if (!mh_strcasecmp(header, h->value))
646                         return (h - table);
647
648         return NOTOK;
649 }
650
651
652 /*
653 ** output the address list for header "name".  The address list
654 ** is a linked list of mailname structs.  "nl" points to the head
655 ** of the list.  Alias substitution should be done on nl.
656 */
657 static void
658 putadr(char *name, struct mailname *nl)
659 {
660         register struct mailname *mp, *mp2;
661         register int linepos;
662         register char *cp;
663         int namelen;
664
665         fprintf(out, "%s: ", name);
666         namelen = strlen(name) + 2;
667         linepos = namelen;
668
669         for (mp = nl; mp; ) {
670                 if (linepos > MAX_SM_FIELD) {
671                         fprintf(out, "\n%s: ", name);
672                         linepos = namelen;
673                 }
674                 if (mp->m_nohost) {
675                         /* a local name - see if it's an alias */
676                         cp = akvalue(mp->m_mbox);
677                         if (cp == mp->m_mbox)
678                                 /* wasn't an alias - use what the user typed */
679                                 linepos = putone(mp->m_text, linepos, namelen);
680                         else
681                                 /* an alias - expand it */
682                                 while ((cp = getname(cp))) {
683                                         if (linepos > MAX_SM_FIELD) {
684                                                         fprintf(out, "\n%s: ", name);
685                                                         linepos = namelen;
686                                         }
687                                         mp2 = getm( cp, NULL, 0, AD_HOST, NULL);
688                                         if (akvisible()) {
689                                                 mp2->m_pers = getcpy(mp->m_mbox);
690                                                 linepos = putone( adrformat(mp2), linepos, namelen );
691                                         } else {
692                                                 linepos = putone( mp2->m_text, linepos, namelen );
693                                         }
694                                         mnfree( mp2 );
695                                 }
696                 } else {
697                         /* not a local name - use what the user typed */
698                         linepos = putone( mp->m_text, linepos, namelen );
699                 }
700                 mp2 = mp;
701                 mp = mp->m_next;
702                 mnfree( mp2 );
703         }
704         putc( '\n', out );
705 }
706
707 static int
708 putone(char *adr, int pos, int indent)
709 {
710         register int len;
711         static int linepos;
712
713         len = strlen( adr );
714         if (pos == indent)
715                 linepos = pos;
716         else if (linepos+len > outputlinelen) {
717                 fprintf( out, ",\n%*s", indent, "");
718                 linepos = indent;
719                 pos += indent + 2;
720         } else {
721                 fputs( ", ", out );
722                 linepos += 2;
723                 pos += 2;
724         }
725         fputs( adr, out );
726
727         linepos += len;
728         return (pos+len);
729 }
730
731
732 static void
733 insert_fcc(struct headers *hdr, unsigned char *pp)
734 {
735         unsigned char   *cp;
736
737         for (cp = pp; isspace(*cp); cp++)
738                 continue;
739         for (pp += strlen(pp) - 1; pp > cp && isspace(*pp); pp--)
740                 continue;
741         if (pp >= cp)
742                 *++pp = 0;
743         if (*cp == 0)
744                 return;
745
746         if (fccind >= FCCS)
747                 adios(NULL, "too many %ss", hdr->value);
748         fccfold[fccind++] = getcpy(cp);
749 }
750
751 #if 0
752 /* BCC GENERATION */
753
754 static void
755 make_bcc_file(void)
756 {
757         pid_t child_id;
758         int fd, i, status;
759         char *vec[6];
760         FILE * in, *out;
761
762 #ifdef HAVE_MKSTEMP
763         fd = mkstemp(bccfil);
764         if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
765                 adios(bccfil, "unable to create");
766 #else
767         mktemp(bccfil);
768         if ((out = fopen(bccfil, "w")) == NULL)
769                 adios(bccfil, "unable to create");
770 #endif
771         chmod(bccfil, 0600);
772
773         fprintf(out, "Date: %s\n", dtimenow(0));
774         if (msgflags & MFRM) {
775           /* There was already a From: in the draft.  Don't add one. */
776           if (!draft_from_masquerading)
777                 /*
778                 ** mts.conf didn't contain "masquerade:[...]draft_from[...]"
779                 ** so we'll reveal the user's actual account@thismachine
780                 ** address in a Sender: header (and use it as the envelope
781                 ** From: later).
782                 */
783                 fprintf(out, "Sender: %s\n", from);
784         } else
785                 /* Construct a From: header. */
786                 fprintf(out, "From: %s\n", signature);
787         if (subject)
788                 fprintf(out, "Subject: %s", subject);
789         fprintf(out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
790         fflush(out);
791
792         if (filter == NULL) {
793                 if ((fd = open(tmpfil, O_RDONLY)) == NOTOK)
794                         adios(NULL, "unable to re-open");
795                 cpydgst(fd, fileno(out), tmpfil, bccfil);
796                 close(fd);
797         } else {
798                 vec[0] = mhbasename(mhlproc);
799
800                 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
801                         sleep(5);
802                 switch (child_id) {
803                         case NOTOK:
804                                 adios("vfork", "unable to");
805
806                         case OK:
807                                 dup2(fileno(out), 1);
808
809                                 i = 1;
810                                 vec[i++] = "-forward";
811                                 vec[i++] = "-form";
812                                 vec[i++] = filter;
813                                 vec[i++] = tmpfil;
814                                 vec[i] = NULL;
815
816                                 execvp(mhlproc, vec);
817                                 adios(mhlproc, "unable to exec");
818
819                         default:
820                                 if (status = pidwait(child_id, OK))
821                                         admonish(NULL, "%s lost (status=0%o)",
822                                                         vec[0], status);
823                                 break;
824                 }
825         }
826
827         fseek(out, 0L, SEEK_END);
828         fprintf(out, "\n------- End of Blind-Carbon-Copy\n");
829         fclose(out);
830 }
831 #endif /* if 0 */
832
833 /* FCC INTERACTION */
834
835 static void
836 file(char *path)
837 {
838         int i;
839
840         if (fccind == 0)
841                 return;
842
843         for (i = 0; i < fccind; i++)
844                 fcc(path, fccfold[i]);
845 }
846
847
848 static void
849 fcc(char *file, char *folder)
850 {
851         pid_t child_id;
852         int i, status;
853         char fold[BUFSIZ];
854
855         if (verbose)
856                 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
857                                 folder);
858         fflush(stdout);
859
860         for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
861                 sleep(5);
862         switch (child_id) {
863                 case NOTOK:
864                         if (!verbose)
865                                 fprintf(stderr, "  %sFcc %s: ",
866                                                 msgstate == resent ?
867                                                 "Resent-" : "", folder);
868                         fprintf(verbose ? stdout : stderr, "no forks, so not ok\n");
869                         break;
870
871                 case OK:
872                         snprintf(fold, sizeof(fold), "%s%s",
873                                 *folder == '+' || *folder == '@' ? "" : "+", folder);
874                         execlp(fileproc, mhbasename(fileproc),
875                                 "-link", "-file", file, fold, NULL);
876                         _exit(-1);
877
878                 default:
879                         if ((status = pidwait(child_id, OK))) {
880                                 if (!verbose)
881                                         fprintf(stderr, "  %sFcc %s: ",
882                                                         msgstate == resent ?
883                                                         "Resent-" : "",
884                                                         folder);
885                                 fprintf(verbose ? stdout : stderr,
886                                                 " errored (0%o)\n", status);
887                         }
888         }
889
890         fflush(stdout);
891 }
892
893
894 #if 0
895
896 /*
897 ** TERMINATION
898 */
899
900 static void
901 die(char *what, char *fmt, ...)
902 {
903         va_list ap;
904
905         va_start(ap, fmt);
906         advertise(what, NULL, fmt, ap);
907         va_end(ap);
908
909         done(1);
910 }
911 #endif