Home | History | Annotate | Line # | Download | only in mail
send.c revision 1.26
      1  1.26  christos /*	$NetBSD: send.c,v 1.26 2006/10/21 21:37:21 christos Exp $	*/
      2   1.6  christos 
      3   1.1       cgd /*
      4   1.4   deraadt  * Copyright (c) 1980, 1993
      5   1.4   deraadt  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.21       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.7     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.6  christos #if 0
     35   1.6  christos static char sccsid[] = "@(#)send.c	8.1 (Berkeley) 6/6/93";
     36   1.6  christos #else
     37  1.26  christos __RCSID("$NetBSD: send.c,v 1.26 2006/10/21 21:37:21 christos Exp $");
     38   1.6  christos #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #include "rcv.h"
     42   1.4   deraadt #include "extern.h"
     43  1.26  christos #ifdef MIME_SUPPORT
     44  1.26  christos #include "mime.h"
     45  1.26  christos #endif
     46  1.26  christos 
     47   1.1       cgd 
     48   1.1       cgd /*
     49   1.1       cgd  * Mail -- a mail program
     50   1.1       cgd  *
     51   1.1       cgd  * Mail to others.
     52   1.1       cgd  */
     53   1.1       cgd 
     54   1.1       cgd /*
     55   1.1       cgd  * Send message described by the passed pointer to the
     56   1.1       cgd  * passed output buffer.  Return -1 on error.
     57   1.1       cgd  * Adjust the status: field if need be.
     58   1.1       cgd  * If doign is given, suppress ignored header fields.
     59   1.1       cgd  * prefix is a string to prepend to each output line.
     60   1.1       cgd  */
     61   1.4   deraadt int
     62  1.26  christos #ifdef MIME_SUPPORT
     63  1.26  christos sendmessage(struct message *mp, FILE *obuf, struct ignoretab *doign,
     64  1.26  christos     const char *prefix, struct mime_info *mip)
     65  1.26  christos #else
     66  1.14       wiz sendmessage(struct message *mp, FILE *obuf, struct ignoretab *doign,
     67  1.22  christos     const char *prefix)
     68  1.26  christos #endif /* MIME_SUPPORT */
     69   1.1       cgd {
     70  1.23  christos 	off_t len;
     71   1.7     lukem 	FILE *ibuf;
     72   1.1       cgd 	char line[LINESIZE];
     73  1.15       wiz 	int isheadflag, infld, ignoring = 0, dostat, firstline;
     74   1.7     lukem 	char *cp, *cp2;
     75   1.7     lukem 	int c = 0;
     76  1.23  christos 	size_t length;
     77  1.23  christos 	size_t prefixlen = 0;
     78   1.1       cgd 
     79   1.1       cgd 	/*
     80   1.1       cgd 	 * Compute the prefix string, without trailing whitespace
     81   1.1       cgd 	 */
     82  1.16       wiz 	if (prefix != NULL) {
     83  1.22  christos 		const char *dp, *dp2 = NULL;
     84  1.22  christos 		for (dp = prefix; *dp; dp++)
     85  1.22  christos 			if (*dp != ' ' && *dp != '\t')
     86  1.22  christos 				dp2 = dp;
     87  1.22  christos 		prefixlen = dp2 == 0 ? 0 : dp2 - prefix + 1;
     88   1.1       cgd 	}
     89   1.1       cgd 	ibuf = setinput(mp);
     90  1.15       wiz 	len = mp->m_size;
     91  1.15       wiz 	isheadflag = 1;
     92   1.1       cgd 	dostat = doign == 0 || !isign("status", doign);
     93   1.1       cgd 	infld = 0;
     94   1.1       cgd 	firstline = 1;
     95   1.1       cgd 	/*
     96   1.1       cgd 	 * Process headers first
     97   1.1       cgd 	 */
     98  1.26  christos #ifdef MIME_SUPPORT
     99  1.26  christos 	if (mip)
    100  1.26  christos 		obuf = mime_decode_header(mip);
    101  1.26  christos #endif
    102  1.15       wiz 	while (len > 0 && isheadflag) {
    103   1.1       cgd 		if (fgets(line, LINESIZE, ibuf) == NULL)
    104   1.1       cgd 			break;
    105  1.15       wiz 		len -= length = strlen(line);
    106   1.1       cgd 		if (firstline) {
    107   1.1       cgd 			/*
    108   1.1       cgd 			 * First line is the From line, so no headers
    109   1.1       cgd 			 * there to worry about
    110   1.1       cgd 			 */
    111   1.1       cgd 			firstline = 0;
    112   1.1       cgd 			ignoring = doign == ignoreall;
    113  1.26  christos #ifdef MIME_SUPPORT
    114  1.26  christos 			/* XXX - ignore multipart boundary lines! */
    115  1.26  christos 			if (line[0] == '-' && line[1] == '-')
    116  1.26  christos 				ignoring = 1;
    117  1.26  christos #endif
    118   1.1       cgd 		} else if (line[0] == '\n') {
    119   1.1       cgd 			/*
    120   1.1       cgd 			 * If line is blank, we've reached end of
    121   1.1       cgd 			 * headers, so force out status: field
    122   1.1       cgd 			 * and note that we are no longer in header
    123   1.1       cgd 			 * fields
    124   1.1       cgd 			 */
    125   1.1       cgd 			if (dostat) {
    126   1.1       cgd 				statusput(mp, obuf, prefix);
    127   1.1       cgd 				dostat = 0;
    128   1.1       cgd 			}
    129  1.15       wiz 			isheadflag = 0;
    130   1.1       cgd 			ignoring = doign == ignoreall;
    131   1.1       cgd 		} else if (infld && (line[0] == ' ' || line[0] == '\t')) {
    132   1.1       cgd 			/*
    133   1.1       cgd 			 * If this line is a continuation (via space or tab)
    134   1.1       cgd 			 * of a previous header field, just echo it
    135   1.1       cgd 			 * (unless the field should be ignored).
    136   1.1       cgd 			 * In other words, nothing to do.
    137   1.1       cgd 			 */
    138   1.1       cgd 		} else {
    139   1.1       cgd 			/*
    140   1.1       cgd 			 * Pick up the header field if we have one.
    141   1.1       cgd 			 */
    142   1.1       cgd 			for (cp = line; (c = *cp++) && c != ':' && !isspace(c);)
    143   1.1       cgd 				;
    144   1.1       cgd 			cp2 = --cp;
    145  1.10  christos 			while (isspace((unsigned char)*cp++))
    146   1.1       cgd 				;
    147   1.1       cgd 			if (cp[-1] != ':') {
    148   1.1       cgd 				/*
    149   1.1       cgd 				 * Not a header line, force out status:
    150   1.1       cgd 				 * This happens in uucp style mail where
    151   1.1       cgd 				 * there are no headers at all.
    152   1.1       cgd 				 */
    153   1.1       cgd 				if (dostat) {
    154   1.1       cgd 					statusput(mp, obuf, prefix);
    155   1.1       cgd 					dostat = 0;
    156   1.1       cgd 				}
    157   1.1       cgd 				if (doign != ignoreall)
    158   1.1       cgd 					/* add blank line */
    159  1.18       wiz 					(void)putc('\n', obuf);
    160  1.15       wiz 				isheadflag = 0;
    161   1.1       cgd 				ignoring = 0;
    162   1.1       cgd 			} else {
    163   1.1       cgd 				/*
    164   1.1       cgd 				 * If it is an ignored field and
    165   1.1       cgd 				 * we care about such things, skip it.
    166   1.1       cgd 				 */
    167   1.1       cgd 				*cp2 = 0;	/* temporarily null terminate */
    168   1.1       cgd 				if (doign && isign(line, doign))
    169   1.1       cgd 					ignoring = 1;
    170   1.1       cgd 				else if ((line[0] == 's' || line[0] == 'S') &&
    171   1.1       cgd 					 strcasecmp(line, "status") == 0) {
    172   1.1       cgd 					/*
    173   1.1       cgd 					 * If the field is "status," go compute
    174   1.1       cgd 					 * and print the real Status: field
    175   1.1       cgd 					 */
    176   1.1       cgd 					if (dostat) {
    177   1.1       cgd 						statusput(mp, obuf, prefix);
    178   1.1       cgd 						dostat = 0;
    179   1.1       cgd 					}
    180   1.1       cgd 					ignoring = 1;
    181   1.1       cgd 				} else {
    182   1.1       cgd 					ignoring = 0;
    183   1.1       cgd 					*cp2 = c;	/* restore */
    184   1.1       cgd 				}
    185   1.1       cgd 				infld = 1;
    186   1.1       cgd 			}
    187   1.1       cgd 		}
    188   1.1       cgd 		if (!ignoring) {
    189   1.1       cgd 			/*
    190   1.1       cgd 			 * Strip trailing whitespace from prefix
    191   1.1       cgd 			 * if line is blank.
    192   1.1       cgd 			 */
    193  1.16       wiz 			if (prefix != NULL) {
    194   1.1       cgd 				if (length > 1)
    195  1.23  christos 					(void)fputs(prefix, obuf);
    196   1.1       cgd 				else
    197  1.18       wiz 					(void)fwrite(prefix, sizeof *prefix,
    198   1.1       cgd 							prefixlen, obuf);
    199   1.9      ross 			}
    200  1.18       wiz 			(void)fwrite(line, sizeof *line, length, obuf);
    201   1.1       cgd 			if (ferror(obuf))
    202   1.1       cgd 				return -1;
    203   1.1       cgd 		}
    204   1.1       cgd 	}
    205   1.1       cgd 	/*
    206   1.1       cgd 	 * Copy out message body
    207   1.1       cgd 	 */
    208  1.26  christos #ifdef MIME_SUPPORT
    209  1.26  christos 	if (mip) {
    210  1.26  christos 		obuf = mime_decode_body(mip);
    211  1.26  christos 		if (obuf == NULL) /* XXX - early out */
    212  1.26  christos 			return 0;
    213  1.26  christos 	}
    214  1.26  christos #endif
    215   1.1       cgd 	if (doign == ignoreall)
    216  1.15       wiz 		len--;		/* skip final blank line */
    217  1.16       wiz 	if (prefix != NULL)
    218  1.15       wiz 		while (len > 0) {
    219   1.1       cgd 			if (fgets(line, LINESIZE, ibuf) == NULL) {
    220   1.1       cgd 				c = 0;
    221   1.1       cgd 				break;
    222   1.1       cgd 			}
    223  1.15       wiz 			len -= c = strlen(line);
    224   1.1       cgd 			/*
    225   1.1       cgd 			 * Strip trailing whitespace from prefix
    226   1.1       cgd 			 * if line is blank.
    227   1.1       cgd 			 */
    228   1.1       cgd 			if (c > 1)
    229  1.23  christos 				(void)fputs(prefix, obuf);
    230   1.1       cgd 			else
    231  1.18       wiz 				(void)fwrite(prefix, sizeof *prefix,
    232   1.1       cgd 						prefixlen, obuf);
    233  1.23  christos 			(void)fwrite(line, sizeof *line, (size_t)c, obuf);
    234   1.1       cgd 			if (ferror(obuf))
    235   1.1       cgd 				return -1;
    236   1.1       cgd 		}
    237   1.1       cgd 	else
    238  1.15       wiz 		while (len > 0) {
    239  1.23  christos 			c = (int)(len < LINESIZE ? len : LINESIZE);
    240  1.23  christos 			if ((c = fread(line, sizeof *line, (size_t)c, ibuf)) <= 0)
    241   1.1       cgd 				break;
    242  1.15       wiz 			len -= c;
    243  1.23  christos 			if (fwrite(line, sizeof *line, (size_t)c, obuf) != c)
    244   1.1       cgd 				return -1;
    245   1.1       cgd 		}
    246   1.1       cgd 	if (doign == ignoreall && c > 0 && line[c - 1] != '\n')
    247   1.1       cgd 		/* no final blank line */
    248   1.1       cgd 		if ((c = getc(ibuf)) != EOF && putc(c, obuf) == EOF)
    249  1.26  christos 				return -1;
    250   1.1       cgd 	return 0;
    251   1.1       cgd }
    252   1.1       cgd 
    253   1.1       cgd /*
    254   1.1       cgd  * Output a reasonable looking status field.
    255   1.1       cgd  */
    256   1.4   deraadt void
    257  1.22  christos statusput(struct message *mp, FILE *obuf, const char *prefix)
    258   1.1       cgd {
    259   1.1       cgd 	char statout[3];
    260   1.7     lukem 	char *cp = statout;
    261   1.1       cgd 
    262   1.1       cgd 	if (mp->m_flag & MREAD)
    263   1.1       cgd 		*cp++ = 'R';
    264   1.1       cgd 	if ((mp->m_flag & MNEW) == 0)
    265   1.1       cgd 		*cp++ = 'O';
    266   1.1       cgd 	*cp = 0;
    267   1.1       cgd 	if (statout[0])
    268  1.23  christos 		(void)fprintf(obuf, "%sStatus: %s\n",
    269  1.16       wiz 			prefix == NULL ? "" : prefix, statout);
    270   1.1       cgd }
    271   1.1       cgd 
    272   1.1       cgd /*
    273   1.1       cgd  * Interface between the argument list and the mail1 routine
    274   1.1       cgd  * which does all the dirty work.
    275   1.1       cgd  */
    276  1.26  christos #ifdef MIME_SUPPORT
    277  1.26  christos int
    278  1.26  christos mail(struct name *to, struct name *cc, struct name *bcc,
    279  1.26  christos      struct name *smopts, char *subject, struct attachment *attach)
    280  1.26  christos #else
    281   1.4   deraadt int
    282  1.14       wiz mail(struct name *to, struct name *cc, struct name *bcc,
    283  1.14       wiz      struct name *smopts, char *subject)
    284  1.26  christos #endif
    285   1.1       cgd {
    286   1.1       cgd 	struct header head;
    287   1.1       cgd 
    288   1.1       cgd 	head.h_to = to;
    289   1.1       cgd 	head.h_subject = subject;
    290   1.1       cgd 	head.h_cc = cc;
    291   1.1       cgd 	head.h_bcc = bcc;
    292   1.1       cgd 	head.h_smopts = smopts;
    293  1.26  christos #ifdef MIME_SUPPORT
    294  1.26  christos 	head.h_attach = attach;
    295  1.26  christos #endif
    296   1.1       cgd 	mail1(&head, 0);
    297   1.1       cgd 	return(0);
    298   1.1       cgd }
    299   1.1       cgd 
    300   1.1       cgd 
    301   1.1       cgd /*
    302   1.1       cgd  * Send mail to a bunch of user names.  The interface is through
    303   1.1       cgd  * the mail routine below.
    304   1.1       cgd  */
    305   1.4   deraadt int
    306  1.14       wiz sendmail(void *v)
    307   1.1       cgd {
    308   1.6  christos 	char *str = v;
    309   1.1       cgd 	struct header head;
    310   1.1       cgd 
    311   1.1       cgd 	head.h_to = extract(str, GTO);
    312  1.16       wiz 	head.h_subject = NULL;
    313  1.17       wiz 	head.h_cc = NULL;
    314  1.17       wiz 	head.h_bcc = NULL;
    315  1.17       wiz 	head.h_smopts = NULL;
    316  1.26  christos #ifdef MIME_SUPPORT
    317  1.26  christos 	head.h_attach = NULL;
    318  1.26  christos #endif
    319   1.1       cgd 	mail1(&head, 0);
    320   1.1       cgd 	return(0);
    321   1.1       cgd }
    322   1.1       cgd 
    323   1.1       cgd /*
    324   1.1       cgd  * Mail a message on standard input to the people indicated
    325   1.1       cgd  * in the passed header.  (Internal interface).
    326   1.1       cgd  */
    327   1.4   deraadt void
    328  1.14       wiz mail1(struct header *hp, int printheaders)
    329   1.1       cgd {
    330  1.22  christos 	const char *cp;
    331   1.1       cgd 	int pid;
    332  1.22  christos 	const char **namelist;
    333   1.1       cgd 	struct name *to;
    334   1.1       cgd 	FILE *mtf;
    335   1.1       cgd 
    336   1.1       cgd 	/*
    337   1.1       cgd 	 * Collect user's mail from standard input.
    338   1.1       cgd 	 * Get the result as mtf.
    339   1.1       cgd 	 */
    340   1.1       cgd 	if ((mtf = collect(hp, printheaders)) == NULL)
    341   1.1       cgd 		return;
    342  1.16       wiz 	if (value("interactive") != NULL) {
    343  1.16       wiz 		if (value("askcc") != NULL || value("askbcc") != NULL) {
    344  1.16       wiz 			if (value("askcc") != NULL)
    345  1.23  christos 				(void)grabh(hp, GCC);
    346  1.16       wiz 			if (value("askbcc") != NULL)
    347  1.23  christos 				(void)grabh(hp, GBCC);
    348   1.3       jtc 		} else {
    349  1.23  christos 			(void)printf("EOT\n");
    350  1.18       wiz 			(void)fflush(stdout);
    351   1.1       cgd 		}
    352   1.9      ross 	}
    353   1.9      ross 	if (fsize(mtf) == 0) {
    354  1.16       wiz 		if (value("dontsendempty") != NULL)
    355  1.12  christos 			goto out;
    356  1.16       wiz 		if (hp->h_subject == NULL)
    357  1.23  christos 			(void)printf("No message, no subject; hope that's ok\n");
    358   1.1       cgd 		else
    359  1.23  christos 			(void)printf("Null message body; hope that's ok\n");
    360   1.9      ross 	}
    361   1.1       cgd 	/*
    362   1.1       cgd 	 * Now, take the user names from the combined
    363   1.1       cgd 	 * to and cc lists and do all the alias
    364   1.1       cgd 	 * processing.
    365   1.1       cgd 	 */
    366   1.1       cgd 	senderr = 0;
    367   1.1       cgd 	to = usermap(cat(hp->h_bcc, cat(hp->h_to, hp->h_cc)));
    368  1.17       wiz 	if (to == NULL) {
    369  1.23  christos 		(void)printf("No recipients specified\n");
    370   1.1       cgd 		senderr++;
    371   1.1       cgd 	}
    372  1.26  christos #ifdef MIME_SUPPORT
    373  1.26  christos 	/*
    374  1.26  christos 	 * If there are attachments, repackage the mail as a
    375  1.26  christos 	 * multi-part MIME message.
    376  1.26  christos 	 */
    377  1.26  christos 	if (hp->h_attach || value(ENAME_MIME_ENCODE_MSG))
    378  1.26  christos 		mtf = mime_encode(mtf, hp);
    379  1.26  christos #endif
    380   1.1       cgd 	/*
    381   1.1       cgd 	 * Look through the recipient list for names with /'s
    382   1.1       cgd 	 * in them which we write to as files directly.
    383   1.1       cgd 	 */
    384   1.1       cgd 	to = outof(to, mtf, hp);
    385   1.1       cgd 	if (senderr)
    386   1.1       cgd 		savedeadletter(mtf);
    387   1.1       cgd 	to = elide(to);
    388   1.1       cgd 	if (count(to) == 0)
    389   1.1       cgd 		goto out;
    390   1.1       cgd 	fixhead(hp, to);
    391   1.1       cgd 	if ((mtf = infix(hp, mtf)) == NULL) {
    392  1.23  christos 		(void)fprintf(stderr, ". . . message lost, sorry.\n");
    393   1.1       cgd 		return;
    394   1.1       cgd 	}
    395   1.1       cgd 	namelist = unpack(cat(hp->h_smopts, to));
    396   1.1       cgd 	if (debug) {
    397  1.22  christos 		const char **t;
    398   1.1       cgd 
    399  1.23  christos 		(void)printf("Sendmail arguments:");
    400  1.16       wiz 		for (t = namelist; *t != NULL; t++)
    401  1.23  christos 			(void)printf(" \"%s\"", *t);
    402  1.23  christos 		(void)printf("\n");
    403   1.1       cgd 		goto out;
    404   1.1       cgd 	}
    405  1.16       wiz 	if ((cp = value("record")) != NULL)
    406  1.25  christos 		(void)savemail(expand(cp), mtf, hp->h_to);
    407   1.1       cgd 	/*
    408   1.1       cgd 	 * Fork, set up the temporary mail file as standard
    409   1.1       cgd 	 * input for "mail", and exec with the user list we generated
    410   1.1       cgd 	 * far above.
    411   1.1       cgd 	 */
    412   1.1       cgd 	pid = fork();
    413   1.1       cgd 	if (pid == -1) {
    414  1.19       wiz 		warn("fork");
    415   1.1       cgd 		savedeadletter(mtf);
    416   1.1       cgd 		goto out;
    417   1.1       cgd 	}
    418   1.1       cgd 	if (pid == 0) {
    419   1.6  christos 		sigset_t nset;
    420  1.23  christos 		(void)sigemptyset(&nset);
    421  1.23  christos 		(void)sigaddset(&nset, SIGHUP);
    422  1.23  christos 		(void)sigaddset(&nset, SIGINT);
    423  1.23  christos 		(void)sigaddset(&nset, SIGQUIT);
    424  1.23  christos 		(void)sigaddset(&nset, SIGTSTP);
    425  1.23  christos 		(void)sigaddset(&nset, SIGTTIN);
    426  1.23  christos 		(void)sigaddset(&nset, SIGTTOU);
    427   1.6  christos 		prepare_child(&nset, fileno(mtf), -1);
    428  1.16       wiz 		if ((cp = value("sendmail")) != NULL)
    429   1.1       cgd 			cp = expand(cp);
    430   1.1       cgd 		else
    431   1.1       cgd 			cp = _PATH_SENDMAIL;
    432  1.23  christos 		(void)execv(cp, (char *const *)__UNCONST(namelist));
    433  1.19       wiz 		warn("%s", cp);
    434   1.1       cgd 		_exit(1);
    435   1.1       cgd 	}
    436  1.16       wiz 	if (value("verbose") != NULL)
    437  1.18       wiz 		(void)wait_child(pid);
    438   1.1       cgd 	else
    439   1.1       cgd 		free_child(pid);
    440   1.1       cgd out:
    441  1.18       wiz 	(void)Fclose(mtf);
    442   1.1       cgd }
    443   1.1       cgd 
    444   1.1       cgd /*
    445   1.1       cgd  * Fix the header by glopping all of the expanded names from
    446   1.1       cgd  * the distribution list into the appropriate fields.
    447   1.1       cgd  */
    448   1.4   deraadt void
    449  1.14       wiz fixhead(struct header *hp, struct name *tolist)
    450   1.1       cgd {
    451   1.7     lukem 	struct name *np;
    452   1.1       cgd 
    453  1.17       wiz 	hp->h_to = NULL;
    454  1.17       wiz 	hp->h_cc = NULL;
    455  1.17       wiz 	hp->h_bcc = NULL;
    456  1.17       wiz 	for (np = tolist; np != NULL; np = np->n_flink) {
    457   1.8       bad 		if (np->n_type & GDEL)
    458   1.8       bad 			continue;	/* Don't copy deleted addresses to the header */
    459   1.1       cgd 		if ((np->n_type & GMASK) == GTO)
    460   1.1       cgd 			hp->h_to =
    461   1.1       cgd 				cat(hp->h_to, nalloc(np->n_name, np->n_type));
    462   1.1       cgd 		else if ((np->n_type & GMASK) == GCC)
    463   1.1       cgd 			hp->h_cc =
    464   1.1       cgd 				cat(hp->h_cc, nalloc(np->n_name, np->n_type));
    465   1.1       cgd 		else if ((np->n_type & GMASK) == GBCC)
    466   1.1       cgd 			hp->h_bcc =
    467   1.1       cgd 				cat(hp->h_bcc, nalloc(np->n_name, np->n_type));
    468   1.8       bad 	}
    469   1.1       cgd }
    470   1.1       cgd 
    471   1.1       cgd /*
    472   1.1       cgd  * Prepend a header in front of the collected stuff
    473   1.1       cgd  * and return the new file.
    474   1.1       cgd  */
    475   1.1       cgd FILE *
    476  1.14       wiz infix(struct header *hp, FILE *fi)
    477   1.1       cgd {
    478   1.7     lukem 	FILE *nfo, *nfi;
    479  1.20       wiz 	int c, fd;
    480  1.20       wiz 	char tempname[PATHSIZE];
    481   1.1       cgd 
    482  1.20       wiz 	(void)snprintf(tempname, sizeof(tempname),
    483  1.20       wiz 	    "%s/mail.RsXXXXXXXXXX", tmpdir);
    484  1.20       wiz 	if ((fd = mkstemp(tempname)) == -1 ||
    485  1.20       wiz 	    (nfo = Fdopen(fd, "w")) == NULL) {
    486  1.20       wiz 		if (fd != -1)
    487  1.23  christos 			(void)close(fd);
    488  1.20       wiz 		warn("%s", tempname);
    489   1.1       cgd 		return(fi);
    490   1.1       cgd 	}
    491  1.20       wiz 	if ((nfi = Fopen(tempname, "r")) == NULL) {
    492  1.20       wiz 		warn("%s", tempname);
    493  1.18       wiz 		(void)Fclose(nfo);
    494  1.20       wiz 		(void)rm(tempname);
    495   1.1       cgd 		return(fi);
    496   1.1       cgd 	}
    497  1.20       wiz 	(void)rm(tempname);
    498  1.26  christos #ifdef MIME_SUPPORT
    499  1.26  christos 	(void)puthead(hp, nfo, GTO|GSUBJECT|GCC|GBCC|GMIME|GNL|GCOMMA);
    500  1.26  christos #else
    501  1.18       wiz 	(void)puthead(hp, nfo, GTO|GSUBJECT|GCC|GBCC|GNL|GCOMMA);
    502  1.26  christos #endif
    503  1.26  christos 
    504   1.1       cgd 	c = getc(fi);
    505   1.1       cgd 	while (c != EOF) {
    506  1.18       wiz 		(void)putc(c, nfo);
    507   1.1       cgd 		c = getc(fi);
    508   1.1       cgd 	}
    509   1.1       cgd 	if (ferror(fi)) {
    510  1.19       wiz 		warn("read");
    511   1.1       cgd 		rewind(fi);
    512   1.1       cgd 		return(fi);
    513   1.1       cgd 	}
    514  1.18       wiz 	(void)fflush(nfo);
    515   1.1       cgd 	if (ferror(nfo)) {
    516  1.20       wiz 		warn("%s", tempname);
    517  1.18       wiz 		(void)Fclose(nfo);
    518  1.18       wiz 		(void)Fclose(nfi);
    519   1.1       cgd 		rewind(fi);
    520   1.1       cgd 		return(fi);
    521   1.1       cgd 	}
    522  1.18       wiz 	(void)Fclose(nfo);
    523  1.18       wiz 	(void)Fclose(fi);
    524   1.1       cgd 	rewind(nfi);
    525   1.1       cgd 	return(nfi);
    526   1.1       cgd }
    527   1.1       cgd 
    528   1.1       cgd /*
    529   1.1       cgd  * Dump the to, subject, cc header on the
    530   1.1       cgd  * passed file buffer.
    531   1.1       cgd  */
    532   1.4   deraadt int
    533  1.14       wiz puthead(struct header *hp, FILE *fo, int w)
    534   1.1       cgd {
    535   1.7     lukem 	int gotcha;
    536   1.1       cgd 
    537   1.1       cgd 	gotcha = 0;
    538  1.17       wiz 	if (hp->h_to != NULL && w & GTO)
    539  1.25  christos 		if (!Bflag)
    540  1.25  christos 			fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
    541  1.16       wiz 	if (hp->h_subject != NULL && w & GSUBJECT)
    542  1.23  christos 		(void)fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
    543  1.25  christos 	if (hp->h_smopts != NULL && w & GSMOPTS)
    544  1.25  christos 		(void)fprintf(fo, "(sendmail options: %s)\n", detract(hp->h_smopts, GSMOPTS)), gotcha++;
    545  1.17       wiz 	if (hp->h_cc != NULL && w & GCC)
    546   1.1       cgd 		fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
    547  1.17       wiz 	if (hp->h_bcc != NULL && w & GBCC)
    548   1.1       cgd 		fmt("Bcc:", hp->h_bcc, fo, w&GCOMMA), gotcha++;
    549  1.26  christos #ifdef MIME_SUPPORT
    550  1.26  christos 	if (w & GMIME && (hp->h_attach || value(ENAME_MIME_ENCODE_MSG)))
    551  1.26  christos 		mime_putheader(fo, hp), gotcha++;
    552  1.26  christos #endif
    553   1.1       cgd 	if (gotcha && w & GNL)
    554  1.18       wiz 		(void)putc('\n', fo);
    555   1.1       cgd 	return(0);
    556   1.1       cgd }
    557   1.1       cgd 
    558   1.1       cgd /*
    559   1.1       cgd  * Format the given header line to not exceed 72 characters.
    560   1.1       cgd  */
    561   1.4   deraadt void
    562  1.22  christos fmt(const char *str, struct name *np, FILE *fo, int comma)
    563   1.1       cgd {
    564   1.7     lukem 	int col, len;
    565   1.1       cgd 
    566   1.1       cgd 	comma = comma ? 1 : 0;
    567   1.1       cgd 	col = strlen(str);
    568   1.1       cgd 	if (col)
    569  1.23  christos 		(void)fputs(str, fo);
    570  1.17       wiz 	for (; np != NULL; np = np->n_flink) {
    571  1.17       wiz 		if (np->n_flink == NULL)
    572   1.1       cgd 			comma = 0;
    573   1.1       cgd 		len = strlen(np->n_name);
    574   1.1       cgd 		col++;		/* for the space */
    575   1.1       cgd 		if (col + len + comma > 72 && col > 4) {
    576  1.23  christos 			(void)fputs("\n    ", fo);
    577   1.1       cgd 			col = 4;
    578   1.1       cgd 		} else
    579  1.23  christos 			(void)putc(' ', fo);
    580  1.23  christos 		(void)fputs(np->n_name, fo);
    581   1.1       cgd 		if (comma)
    582  1.23  christos 			(void)putc(',', fo);
    583   1.1       cgd 		col += len + comma;
    584   1.1       cgd 	}
    585  1.23  christos 	(void)putc('\n', fo);
    586   1.1       cgd }
    587   1.1       cgd 
    588   1.1       cgd /*
    589   1.1       cgd  * Save the outgoing mail on the passed file.
    590   1.1       cgd  */
    591   1.1       cgd 
    592   1.1       cgd /*ARGSUSED*/
    593   1.4   deraadt int
    594  1.25  christos savemail(const char name[], FILE *fi, struct name *to)
    595   1.1       cgd {
    596   1.7     lukem 	FILE *fo;
    597   1.1       cgd 	char buf[BUFSIZ];
    598   1.7     lukem 	int i;
    599   1.6  christos 	time_t now;
    600  1.24  christos 	mode_t m;
    601   1.1       cgd 
    602  1.24  christos 	m = umask(077);
    603  1.24  christos 	fo = Fopen(name, "a");
    604  1.24  christos 	(void)umask(m);
    605  1.24  christos 	if (fo == NULL) {
    606  1.19       wiz 		warn("%s", name);
    607   1.1       cgd 		return (-1);
    608   1.1       cgd 	}
    609  1.18       wiz 	(void)time(&now);
    610  1.23  christos 	(void)fprintf(fo, "From %s %s", myname, ctime(&now));
    611  1.25  christos 
    612  1.25  christos 	if (Bflag) { /* Make sure we save a "To:" line if -B is set */
    613  1.25  christos 		struct name *n;
    614  1.25  christos 		(void)fputs("To: undisclosed-recipients:", fo);
    615  1.25  christos 		for (n = to; n != NULL; n = n->n_flink)
    616  1.25  christos 			if ((n->n_type & GDEL) == 0)
    617  1.25  christos 				(void)fprintf(fo, " %s", n->n_name);
    618  1.25  christos 		(void)putc('\n', fo);
    619  1.25  christos 	}
    620  1.25  christos 
    621   1.1       cgd 	while ((i = fread(buf, 1, sizeof buf, fi)) > 0)
    622  1.23  christos 		(void)fwrite(buf, 1, (size_t)i, fo);
    623  1.18       wiz 	(void)putc('\n', fo);
    624  1.18       wiz 	(void)fflush(fo);
    625   1.1       cgd 	if (ferror(fo))
    626  1.19       wiz 		warn("%s", name);
    627  1.18       wiz 	(void)Fclose(fo);
    628   1.1       cgd 	rewind(fi);
    629   1.1       cgd 	return (0);
    630   1.1       cgd }
    631