Removed the global alias file
[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 = 0;  /* 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                                         aliasflg = 1;
288                                         if ((state = alias(cp)) != AK_OK)
289                                                 adios(NULL, "aliasing error in file %s - %s", cp, akerror(state) );
290                                         continue;
291                                 case NALIASW:
292                                         aliasflg = 0;
293                                         continue;
294
295                                 case WIDTHSW:
296                                         if (!(cp = *argp++) || *cp == '-')
297                                                 adios(NULL, "missing argument to %s", argp[-2]);
298                                         outputlinelen = atoi(cp);
299                                         if (outputlinelen <= 10)
300                                                 outputlinelen = 72;
301                                         continue;
302
303                                 case LIBSW:
304                                         if (!(cp = *argp++) || *cp == '-')
305                                                 adios(NULL, "missing argument to %s", argp[-2]);
306                                         /* create a minimal context */
307                                         if (context_foil(cp) == -1)
308                                                 done(1);
309                                         continue;
310
311                                 case ANNOSW:
312                                         /* -idanno switch ignored */
313                                         if (!(cp = *argp++) || *cp == '-')
314                                                 adios(NULL, "missing argument to %s", argp[-2]);
315                                         continue;
316                         }
317                 }
318                 if (msg)
319                         adios(NULL, "only one message at a time!");
320                 else
321                         msg = cp;
322         }
323
324         if (!msg)
325                 adios(NULL, "usage: %s [switches] file", invo_name);
326
327         if ((in = fopen(msg, "r")) == NULL)
328                 adios(msg, "unable to open");
329
330         start_headers();
331         if (debug) {
332                 verbose++;
333                 out = stdout;
334         }
335         else {
336 #ifdef HAVE_MKSTEMP
337                         if ((out = fdopen( mkstemp(tmpfil), "w" )) == NULL )
338                                 adios(tmpfil, "unable to create");
339 #else
340                         mktemp(tmpfil);
341                         if ((out = fopen(tmpfil, "w")) == NULL)
342                                 adios(tmpfil, "unable to create");
343                         chmod(tmpfil, 0600);
344 #endif
345                 }
346
347         hdrtab = (msgstate == normal) ? NHeaders : RHeaders;
348
349         for (compnum = 1, state = FLD;;) {
350                 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
351                         case FLD:
352                                 compnum++;
353                                 putfmt(name, buf, out);
354                                 continue;
355
356                         case FLDPLUS:
357                                 compnum++;
358                                 cp = add(buf, cp);
359                                 while (state == FLDPLUS) {
360                                         state = m_getfld(state, name, buf,
361                                                         sizeof(buf), in);
362                                         cp = add(buf, cp);
363                                 }
364                                 putfmt(name, cp, out);
365                                 free(cp);
366                                 continue;
367
368                         case BODY:
369                                 finish_headers(out);
370                                 fprintf(out, "\n%s", buf);
371                                 while (state == BODY) {
372                                         state = m_getfld(state, name, buf,
373                                                         sizeof(buf), in);
374                                         fputs(buf, out);
375                                 }
376                                 break;
377
378                         case FILEEOF:
379                                 finish_headers(out);
380                                 break;
381
382                         case LENERR:
383                         case FMTERR:
384                                 adios(NULL, "message format error in component #%d", compnum);
385
386                         default:
387                                 adios(NULL, "getfld() returned %d", state);
388                 }
389                 break;
390         }
391
392         fclose(in);
393         if (backflg) {
394                 strncpy(buf, m_backup(msg), sizeof(buf));
395                 if (rename(msg, buf) == NOTOK)
396                         advise(buf, "unable to rename %s to", msg);
397         }
398
399         if (debug) {
400                 done(0);
401         }
402         else
403                 fclose(out);
404
405         file(tmpfil);
406
407         /*
408         ** re-open the temp file, unlink it and exec sendmail, giving it
409         ** the msg temp file as std in.
410         */
411         if (freopen(tmpfil, "r", stdin) == NULL)
412                 adios(tmpfil, "can't reopen for sendmail");
413         if (rmflg)
414                 unlink(tmpfil);
415
416         argp = sargv;
417         *argp++ = "send-mail";
418         *argp++ = "-m";  /* send to me too */
419         *argp++ = "-t";  /* read msg for recipients */
420         *argp++ = "-i";  /* don't stop on "." */
421         if (watch || verbose)
422                 *argp++ = "-v";
423         *argp = NULL;
424
425         if (pushflg && !(watch || verbose)) {
426                 /* fork to a child to run sendmail */
427                 for (i=0; (pid = vfork()) == NOTOK && i < 5; i++)
428                         sleep(5);
429                 switch (pid) {
430                         case NOTOK:
431                                 fprintf(verbose ? stdout : stderr,
432                                                 "%s: can't fork to %s\n",
433                                                 invo_name, sendmail);
434                                 exit(-1);
435                         case OK:
436                                 /* we're the child .. */
437                                 break;
438                         default:
439                                 exit(0);
440                 }
441         }
442         execv(sendmail, sargv);
443         adios(sendmail, "can't exec");
444         return 0;  /* dead code to satisfy the compiler */
445 }
446
447 /* DRAFT GENERATION */
448
449 static void
450 putfmt(char *name, char *str, FILE *out)
451 {
452         int i;
453         char *cp, *pp;
454         struct headers *hdr;
455
456         while (*str == ' ' || *str == '\t')
457                 str++;
458
459         if ((i = get_header(name, hdrtab)) == NOTOK) {
460                 fprintf(out, "%s: %s", name, str);
461                 return;
462         }
463
464         hdr = &hdrtab[i];
465         if (hdr->flags & HIGN)
466                 return;
467         if (hdr->flags & HBAD) {
468                 advise(NULL, "illegal header line -- %s:", name);
469                 badmsg++;
470                 return;
471         }
472         msgflags |= hdr->set;
473
474         if (hdr->flags & HSUB)
475                 subject = subject ? add(str, add("\t", subject)) :
476                                 getcpy(str);
477
478         if (hdr->flags & HFCC) {
479                 if ((cp = strrchr(str, '\n')))
480                         *cp = 0;
481                 for (cp = pp = str; (cp = strchr(pp, ',')); pp = cp) {
482                         *cp++ = 0;
483                         insert_fcc(hdr, pp);
484                 }
485                 insert_fcc(hdr, pp);
486                 return;
487         }
488
489 #ifdef notdef
490         if (hdr->flags & HBCC) {
491                 insert_bcc(str);
492                 return;
493         }
494 #endif /* notdef */
495
496         if (*str != '\n' && *str != '\0') {
497                 if (aliasflg && hdr->flags & HTRY) {
498                         /*
499                         ** this header contains address(es) that we have to do
500                         ** alias expansion on.  Because of the saved state in
501                         ** getname we have to put all the addresses into a
502                         ** list. We then let putadr munch on that list,
503                         ** possibly expanding aliases.
504                         **/
505                         register struct mailname *f = 0;
506                         register struct mailname *mp = 0;
507
508                         while ((cp = getname(str))) {
509                                 mp = getm( cp, NULL, 0, AD_HOST, NULL);
510                                 if (f == 0) {
511                                         f = mp;
512                                         mp->m_next = mp;
513                                 } else {
514                                         mp->m_next = f->m_next;
515                                         f->m_next = mp;
516                                         f = mp;
517                                 }
518                         }
519                         f = mp->m_next; mp->m_next = 0;
520                         putadr( name, f );
521                 } else {
522                         /*
523                         ** The author(s) of spost decided that alias
524                         ** substitution wasn't necessary for the non-HTRY
525                         ** headers.  Unfortunately, one of those headers
526                         ** is "From:", and having alias substitution
527                         ** work on that is extremely useful for someone
528                         ** with a lot of POP3 email accounts or aliases.
529                         ** post supports aliasing of "From:"...
530                         **
531                         ** Since "From:"-processing is incompletely
532                         ** implemented in this unsupported and
533                         ** undocumented spost backend, I'm not
534                         ** going to take the time to implement my new
535                         ** draft-From:-based email address masquerading.
536                         ** If I do ever implement it here, I'd almost
537                         ** certainly want to implement "From:" line
538                         ** alias processing as well.
539                         ** -- Dan Harkless <dan-nmh@dilvish.speed.net>
540                         */
541                         fprintf(out, "%s: %s", name, str );
542                 }
543         }
544 }
545
546
547 static void
548 start_headers(void)
549 {
550         char *cp;
551         char sigbuf[BUFSIZ];
552
553         strncpy(from, getusername(), sizeof(from));
554
555         if ((cp = getfullname()) && *cp) {
556                 strncpy(sigbuf, cp, sizeof(sigbuf));
557                 snprintf(signature, sizeof(signature), "%s <%s>",
558                                 sigbuf,  from);
559         } else
560                 snprintf(signature, sizeof(signature), "%s",  from);
561 }
562
563
564 static void
565 finish_headers(FILE *out)
566 {
567         switch (msgstate) {
568                 case normal:
569                         if (!(msgflags & MDAT))
570                                 fprintf(out, "Date: %s\n", dtimenow(0));
571
572                         if (msgflags & MFRM) {
573                                 /*
574                                 ** There was already a From: in the draft.
575                                 ** Don't add one.
576                                 */
577                                 if (!draft_from_masquerading)
578                                         /*
579                                         ** mts.conf didn't contain
580                                         ** "masquerade:[...]draft_from[...]"
581                                         ** so we'll reveal the user's
582                                         ** actual account@thismachine
583                                         ** address in a Sender: header
584                                         ** (and use it as the envelope
585                                         ** From: later).
586                                         */
587                                         fprintf(out, "Sender: %s\n", from);
588                         } else
589                                 fprintf(out, "From: %s\n", signature);
590
591 #ifdef notdef
592                         if (!(msgflags & MVIS))
593                                 fprintf(out, "Bcc: Blind Distribution List: ;\n");
594 #endif /* notdef */
595                         break;
596
597                 case resent:
598                         if (!(msgflags & MRDT))
599                                 fprintf(out, "Resent-Date: %s\n", dtimenow(0));
600                         if (msgflags & MRFM) {
601                                 /*
602                                 ** There was already a Resent-From: in draft.
603                                 ** Don't add one.
604                                 */
605                                 if (!draft_from_masquerading)
606                                         /*
607                                         ** mts.conf didn't contain
608                                         ** "masquerade:[...]draft_from[...]"
609                                         ** so we'll reveal the user's
610                                         ** actual account@thismachine
611                                         ** address in a Sender: header
612                                         ** (and use it as the envelope
613                                         ** From: later).
614                                         */
615                                         fprintf(out, "Resent-Sender: %s\n",
616                                                         from);
617                         } else
618                                 /* Construct a Resent-From: header. */
619                                 fprintf(out, "Resent-From: %s\n", signature);
620 #ifdef notdef
621                         if (!(msgflags & MVIS))
622                                 fprintf(out, "Resent-Bcc: Blind Re-Distribution List: ;\n");
623 #endif /* notdef */
624                         break;
625         }
626
627         if (badmsg)
628                 adios(NULL, "re-format message and try again");
629 }
630
631
632 static int
633 get_header(char *header, struct headers *table)
634 {
635         struct headers *h;
636
637         for (h = table; h->value; h++)
638                 if (!mh_strcasecmp(header, h->value))
639                         return (h - table);
640
641         return NOTOK;
642 }
643
644
645 /*
646 ** output the address list for header "name".  The address list
647 ** is a linked list of mailname structs.  "nl" points to the head
648 ** of the list.  Alias substitution should be done on nl.
649 */
650 static void
651 putadr(char *name, struct mailname *nl)
652 {
653         register struct mailname *mp, *mp2;
654         register int linepos;
655         register char *cp;
656         int namelen;
657
658         fprintf(out, "%s: ", name);
659         namelen = strlen(name) + 2;
660         linepos = namelen;
661
662         for (mp = nl; mp; ) {
663                 if (linepos > MAX_SM_FIELD) {
664                         fprintf(out, "\n%s: ", name);
665                         linepos = namelen;
666                 }
667                 if (mp->m_nohost) {
668                         /* a local name - see if it's an alias */
669                         cp = akvalue(mp->m_mbox);
670                         if (cp == mp->m_mbox)
671                                 /* wasn't an alias - use what the user typed */
672                                 linepos = putone(mp->m_text, linepos, namelen);
673                         else
674                                 /* an alias - expand it */
675                                 while ((cp = getname(cp))) {
676                                         if (linepos > MAX_SM_FIELD) {
677                                                         fprintf(out, "\n%s: ", name);
678                                                         linepos = namelen;
679                                         }
680                                         mp2 = getm( cp, NULL, 0, AD_HOST, NULL);
681                                         if (akvisible()) {
682                                                 mp2->m_pers = getcpy(mp->m_mbox);
683                                                 linepos = putone( adrformat(mp2), linepos, namelen );
684                                         } else {
685                                                 linepos = putone( mp2->m_text, linepos, namelen );
686                                         }
687                                         mnfree( mp2 );
688                                 }
689                 } else {
690                         /* not a local name - use what the user typed */
691                         linepos = putone( mp->m_text, linepos, namelen );
692                 }
693                 mp2 = mp;
694                 mp = mp->m_next;
695                 mnfree( mp2 );
696         }
697         putc( '\n', out );
698 }
699
700 static int
701 putone(char *adr, int pos, int indent)
702 {
703         register int len;
704         static int linepos;
705
706         len = strlen( adr );
707         if (pos == indent)
708                 linepos = pos;
709         else if (linepos+len > outputlinelen) {
710                 fprintf( out, ",\n%*s", indent, "");
711                 linepos = indent;
712                 pos += indent + 2;
713         } else {
714                 fputs( ", ", out );
715                 linepos += 2;
716                 pos += 2;
717         }
718         fputs( adr, out );
719
720         linepos += len;
721         return (pos+len);
722 }
723
724
725 static void
726 insert_fcc(struct headers *hdr, unsigned char *pp)
727 {
728         unsigned char   *cp;
729
730         for (cp = pp; isspace(*cp); cp++)
731                 continue;
732         for (pp += strlen(pp) - 1; pp > cp && isspace(*pp); pp--)
733                 continue;
734         if (pp >= cp)
735                 *++pp = 0;
736         if (*cp == 0)
737                 return;
738
739         if (fccind >= FCCS)
740                 adios(NULL, "too many %ss", hdr->value);
741         fccfold[fccind++] = getcpy(cp);
742 }
743
744 #if 0
745 /* BCC GENERATION */
746
747 static void
748 make_bcc_file(void)
749 {
750         pid_t child_id;
751         int fd, i, status;
752         char *vec[6];
753         FILE * in, *out;
754
755 #ifdef HAVE_MKSTEMP
756         fd = mkstemp(bccfil);
757         if (fd == -1 || (out = fdopen(fd, "w")) == NULL)
758                 adios(bccfil, "unable to create");
759 #else
760         mktemp(bccfil);
761         if ((out = fopen(bccfil, "w")) == NULL)
762                 adios(bccfil, "unable to create");
763 #endif
764         chmod(bccfil, 0600);
765
766         fprintf(out, "Date: %s\n", dtimenow(0));
767         if (msgflags & MFRM) {
768           /* There was already a From: in the draft.  Don't add one. */
769           if (!draft_from_masquerading)
770                 /*
771                 ** mts.conf didn't contain "masquerade:[...]draft_from[...]"
772                 ** so we'll reveal the user's actual account@thismachine
773                 ** address in a Sender: header (and use it as the envelope
774                 ** From: later).
775                 */
776                 fprintf(out, "Sender: %s\n", from);
777         } else
778                 /* Construct a From: header. */
779                 fprintf(out, "From: %s\n", signature);
780         if (subject)
781                 fprintf(out, "Subject: %s", subject);
782         fprintf(out, "BCC:\n\n------- Blind-Carbon-Copy\n\n");
783         fflush(out);
784
785         if (filter == NULL) {
786                 if ((fd = open(tmpfil, O_RDONLY)) == NOTOK)
787                         adios(NULL, "unable to re-open");
788                 cpydgst(fd, fileno(out), tmpfil, bccfil);
789                 close(fd);
790         } else {
791                 vec[0] = mhbasename(mhlproc);
792
793                 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
794                         sleep(5);
795                 switch (child_id) {
796                         case NOTOK:
797                                 adios("vfork", "unable to");
798
799                         case OK:
800                                 dup2(fileno(out), 1);
801
802                                 i = 1;
803                                 vec[i++] = "-forward";
804                                 vec[i++] = "-form";
805                                 vec[i++] = filter;
806                                 vec[i++] = tmpfil;
807                                 vec[i] = NULL;
808
809                                 execvp(mhlproc, vec);
810                                 adios(mhlproc, "unable to exec");
811
812                         default:
813                                 if (status = pidwait(child_id, OK))
814                                         admonish(NULL, "%s lost (status=0%o)",
815                                                         vec[0], status);
816                                 break;
817                 }
818         }
819
820         fseek(out, 0L, SEEK_END);
821         fprintf(out, "\n------- End of Blind-Carbon-Copy\n");
822         fclose(out);
823 }
824 #endif /* if 0 */
825
826 /* FCC INTERACTION */
827
828 static void
829 file(char *path)
830 {
831         int i;
832
833         if (fccind == 0)
834                 return;
835
836         for (i = 0; i < fccind; i++)
837                 fcc(path, fccfold[i]);
838 }
839
840
841 static void
842 fcc(char *file, char *folder)
843 {
844         pid_t child_id;
845         int i, status;
846         char fold[BUFSIZ];
847
848         if (verbose)
849                 printf("%sFcc: %s\n", msgstate == resent ? "Resent-" : "",
850                                 folder);
851         fflush(stdout);
852
853         for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
854                 sleep(5);
855         switch (child_id) {
856                 case NOTOK:
857                         if (!verbose)
858                                 fprintf(stderr, "  %sFcc %s: ",
859                                                 msgstate == resent ?
860                                                 "Resent-" : "", folder);
861                         fprintf(verbose ? stdout : stderr, "no forks, so not ok\n");
862                         break;
863
864                 case OK:
865                         snprintf(fold, sizeof(fold), "%s%s",
866                                 *folder == '+' || *folder == '@' ? "" : "+", folder);
867                         execlp(fileproc, mhbasename(fileproc),
868                                 "-link", "-file", file, fold, NULL);
869                         _exit(-1);
870
871                 default:
872                         if ((status = pidwait(child_id, OK))) {
873                                 if (!verbose)
874                                         fprintf(stderr, "  %sFcc %s: ",
875                                                         msgstate == resent ?
876                                                         "Resent-" : "",
877                                                         folder);
878                                 fprintf(verbose ? stdout : stderr,
879                                                 " errored (0%o)\n", status);
880                         }
881         }
882
883         fflush(stdout);
884 }
885
886
887 #if 0
888
889 /*
890 ** TERMINATION
891 */
892
893 static void
894 die(char *what, char *fmt, ...)
895 {
896         va_list ap;
897
898         va_start(ap, fmt);
899         advertise(what, NULL, fmt, ap);
900         va_end(ap);
901
902         done(1);
903 }
904 #endif