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