Home | History | Annotate | Line # | Download | only in mail
send.c revision 1.29
      1  1.29  christos /*	$NetBSD: send.c,v 1.29 2007/10/23 14:58:45 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.29  christos __RCSID("$NetBSD: send.c,v 1.29 2007/10/23 14:58:45 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.27  christos 
     48  1.28  christos /*
     49  1.28  christos  * Mail -- a mail program
     50  1.28  christos  *
     51  1.28  christos  * Mail to others.
     52  1.28  christos  */
     53  1.28  christos 
     54  1.28  christos /*
     55  1.28  christos  * compare twwo name structures.  Support for get_smopts.
     56  1.28  christos  */
     57  1.27  christos static int
     58  1.27  christos namecmp(struct name *np1, struct name *np2)
     59  1.27  christos {
     60  1.27  christos 	for (/*EMPTY*/; np1 && np2; np1 = np1->n_flink, np2 = np2->n_flink) {
     61  1.27  christos 		if (strcmp(np1->n_name, np2->n_name) != 0)
     62  1.27  christos 			return 1;
     63  1.27  christos 	}
     64  1.27  christos 	return np1 || np2;
     65  1.27  christos }
     66  1.27  christos 
     67  1.28  christos /*
     68  1.28  christos  * Get the sendmail options from the smopts list.
     69  1.28  christos  */
     70  1.27  christos static struct name *
     71  1.27  christos get_smopts(struct name *to)
     72  1.27  christos {
     73  1.27  christos 	struct smopts_s *sp;
     74  1.27  christos 	struct name *smargs, *np;
     75  1.27  christos 	smargs = NULL;
     76  1.27  christos 	for (np = to; np; np = np->n_flink) {
     77  1.27  christos 		if ((sp = findsmopts(np->n_name, 0)) == NULL)
     78  1.27  christos 			continue;
     79  1.27  christos 		if (smargs == NULL)
     80  1.27  christos 			smargs = sp->s_smopts;
     81  1.27  christos 		else if (namecmp(smargs, sp->s_smopts) != 0)
     82  1.27  christos 			return NULL;
     83  1.27  christos 	}
     84  1.27  christos 	if (smargs &&
     85  1.27  christos 	    smargs->n_flink == NULL &&
     86  1.27  christos 	    (smargs->n_name == NULL || smargs->n_name[0] == '\0'))
     87  1.27  christos 		return NULL;
     88  1.27  christos 	return smargs;
     89  1.27  christos }
     90   1.1       cgd 
     91   1.1       cgd /*
     92  1.28  christos  * Output a reasonable looking status field.
     93   1.1       cgd  */
     94  1.28  christos static void
     95  1.28  christos statusput(struct message *mp, FILE *obuf, const char *prefix)
     96  1.28  christos {
     97  1.28  christos 	char statout[3];
     98  1.28  christos 	char *cp = statout;
     99  1.28  christos 
    100  1.28  christos 	if (mp->m_flag & MREAD)
    101  1.28  christos 		*cp++ = 'R';
    102  1.28  christos 	if ((mp->m_flag & MNEW) == 0)
    103  1.28  christos 		*cp++ = 'O';
    104  1.28  christos 	*cp = 0;
    105  1.28  christos 	if (statout[0])
    106  1.28  christos 		(void)fprintf(obuf, "%sStatus: %s\n",
    107  1.28  christos 			prefix == NULL ? "" : prefix, statout);
    108  1.28  christos }
    109   1.1       cgd 
    110   1.1       cgd /*
    111   1.1       cgd  * Send message described by the passed pointer to the
    112   1.1       cgd  * passed output buffer.  Return -1 on error.
    113   1.1       cgd  * Adjust the status: field if need be.
    114   1.1       cgd  * If doign is given, suppress ignored header fields.
    115   1.1       cgd  * prefix is a string to prepend to each output line.
    116   1.1       cgd  */
    117  1.28  christos PUBLIC int
    118  1.26  christos sendmessage(struct message *mp, FILE *obuf, struct ignoretab *doign,
    119  1.26  christos     const char *prefix, struct mime_info *mip)
    120   1.1       cgd {
    121  1.23  christos 	off_t len;
    122   1.7     lukem 	FILE *ibuf;
    123   1.1       cgd 	char line[LINESIZE];
    124  1.28  christos 	int isheadflag, infld, ignoring, dostat, firstline;
    125  1.29  christos 	char *cp;
    126  1.28  christos 	int c;	/* XXX - this variable is horribly abused */
    127  1.23  christos 	size_t length;
    128  1.28  christos 	size_t prefixlen;
    129  1.28  christos 
    130  1.28  christos 	c = 0;
    131  1.28  christos 	ignoring = 0;
    132  1.28  christos 	prefixlen = 0;
    133   1.1       cgd 
    134   1.1       cgd 	/*
    135   1.1       cgd 	 * Compute the prefix string, without trailing whitespace
    136   1.1       cgd 	 */
    137  1.16       wiz 	if (prefix != NULL) {
    138  1.22  christos 		const char *dp, *dp2 = NULL;
    139  1.22  christos 		for (dp = prefix; *dp; dp++)
    140  1.29  christos 			if (!is_WSP(*dp))
    141  1.22  christos 				dp2 = dp;
    142  1.22  christos 		prefixlen = dp2 == 0 ? 0 : dp2 - prefix + 1;
    143   1.1       cgd 	}
    144   1.1       cgd 	ibuf = setinput(mp);
    145  1.15       wiz 	len = mp->m_size;
    146  1.15       wiz 	isheadflag = 1;
    147   1.1       cgd 	dostat = doign == 0 || !isign("status", doign);
    148   1.1       cgd 	infld = 0;
    149   1.1       cgd 	firstline = 1;
    150   1.1       cgd 	/*
    151   1.1       cgd 	 * Process headers first
    152   1.1       cgd 	 */
    153  1.26  christos #ifdef MIME_SUPPORT
    154  1.26  christos 	if (mip)
    155  1.26  christos 		obuf = mime_decode_header(mip);
    156  1.26  christos #endif
    157  1.15       wiz 	while (len > 0 && isheadflag) {
    158   1.1       cgd 		if (fgets(line, LINESIZE, ibuf) == NULL)
    159   1.1       cgd 			break;
    160  1.15       wiz 		len -= length = strlen(line);
    161   1.1       cgd 		if (firstline) {
    162  1.29  christos 			/*
    163   1.1       cgd 			 * First line is the From line, so no headers
    164   1.1       cgd 			 * there to worry about
    165   1.1       cgd 			 */
    166   1.1       cgd 			firstline = 0;
    167   1.1       cgd 			ignoring = doign == ignoreall;
    168  1.26  christos #ifdef MIME_SUPPORT
    169  1.26  christos 			/* XXX - ignore multipart boundary lines! */
    170  1.26  christos 			if (line[0] == '-' && line[1] == '-')
    171  1.26  christos 				ignoring = 1;
    172  1.26  christos #endif
    173   1.1       cgd 		} else if (line[0] == '\n') {
    174   1.1       cgd 			/*
    175   1.1       cgd 			 * If line is blank, we've reached end of
    176   1.1       cgd 			 * headers, so force out status: field
    177   1.1       cgd 			 * and note that we are no longer in header
    178   1.1       cgd 			 * fields
    179   1.1       cgd 			 */
    180   1.1       cgd 			if (dostat) {
    181   1.1       cgd 				statusput(mp, obuf, prefix);
    182   1.1       cgd 				dostat = 0;
    183   1.1       cgd 			}
    184  1.15       wiz 			isheadflag = 0;
    185   1.1       cgd 			ignoring = doign == ignoreall;
    186  1.29  christos 		} else if (infld && is_WSP(line[0])) {
    187   1.1       cgd 			/*
    188   1.1       cgd 			 * If this line is a continuation (via space or tab)
    189   1.1       cgd 			 * of a previous header field, just echo it
    190   1.1       cgd 			 * (unless the field should be ignored).
    191   1.1       cgd 			 * In other words, nothing to do.
    192   1.1       cgd 			 */
    193   1.1       cgd 		} else {
    194   1.1       cgd 			/*
    195   1.1       cgd 			 * Pick up the header field if we have one.
    196   1.1       cgd 			 */
    197  1.29  christos 			char *save_cp;
    198  1.29  christos 			for (cp = line; *cp && *cp != ':' && !is_WSP(*cp); cp++)
    199  1.28  christos 				continue;
    200  1.29  christos 			save_cp = cp;
    201  1.29  christos 			cp = skip_WSP(cp);
    202  1.29  christos 			if (*cp++ != ':') {
    203   1.1       cgd 				/*
    204   1.1       cgd 				 * Not a header line, force out status:
    205   1.1       cgd 				 * This happens in uucp style mail where
    206   1.1       cgd 				 * there are no headers at all.
    207   1.1       cgd 				 */
    208   1.1       cgd 				if (dostat) {
    209   1.1       cgd 					statusput(mp, obuf, prefix);
    210   1.1       cgd 					dostat = 0;
    211   1.1       cgd 				}
    212   1.1       cgd 				if (doign != ignoreall)
    213   1.1       cgd 					/* add blank line */
    214  1.18       wiz 					(void)putc('\n', obuf);
    215  1.15       wiz 				isheadflag = 0;
    216   1.1       cgd 				ignoring = 0;
    217   1.1       cgd 			} else {
    218   1.1       cgd 				/*
    219   1.1       cgd 				 * If it is an ignored field and
    220   1.1       cgd 				 * we care about such things, skip it.
    221   1.1       cgd 				 */
    222  1.29  christos 				int save_c;
    223  1.29  christos 				save_c = *save_cp;
    224  1.29  christos 				*save_cp = 0;	/* temporarily null terminate */
    225   1.1       cgd 				if (doign && isign(line, doign))
    226   1.1       cgd 					ignoring = 1;
    227   1.1       cgd 				else if ((line[0] == 's' || line[0] == 'S') &&
    228   1.1       cgd 					 strcasecmp(line, "status") == 0) {
    229   1.1       cgd 					/*
    230   1.1       cgd 					 * If the field is "status," go compute
    231   1.1       cgd 					 * and print the real Status: field
    232   1.1       cgd 					 */
    233   1.1       cgd 					if (dostat) {
    234   1.1       cgd 						statusput(mp, obuf, prefix);
    235   1.1       cgd 						dostat = 0;
    236   1.1       cgd 					}
    237   1.1       cgd 					ignoring = 1;
    238   1.1       cgd 				} else {
    239   1.1       cgd 					ignoring = 0;
    240  1.29  christos 					*save_cp = save_c;	/* restore */
    241   1.1       cgd 				}
    242   1.1       cgd 				infld = 1;
    243   1.1       cgd 			}
    244   1.1       cgd 		}
    245   1.1       cgd 		if (!ignoring) {
    246   1.1       cgd 			/*
    247   1.1       cgd 			 * Strip trailing whitespace from prefix
    248   1.1       cgd 			 * if line is blank.
    249   1.1       cgd 			 */
    250  1.16       wiz 			if (prefix != NULL) {
    251   1.1       cgd 				if (length > 1)
    252  1.23  christos 					(void)fputs(prefix, obuf);
    253   1.1       cgd 				else
    254  1.18       wiz 					(void)fwrite(prefix, sizeof *prefix,
    255   1.1       cgd 							prefixlen, obuf);
    256   1.9      ross 			}
    257  1.18       wiz 			(void)fwrite(line, sizeof *line, length, obuf);
    258   1.1       cgd 			if (ferror(obuf))
    259   1.1       cgd 				return -1;
    260   1.1       cgd 		}
    261   1.1       cgd 	}
    262   1.1       cgd 	/*
    263   1.1       cgd 	 * Copy out message body
    264   1.1       cgd 	 */
    265  1.26  christos #ifdef MIME_SUPPORT
    266  1.26  christos 	if (mip) {
    267  1.26  christos 		obuf = mime_decode_body(mip);
    268  1.26  christos 		if (obuf == NULL) /* XXX - early out */
    269  1.26  christos 			return 0;
    270  1.26  christos 	}
    271  1.26  christos #endif
    272  1.28  christos 	c = 0;	/* This is needed if len == 0.  It's used differently above. */
    273   1.1       cgd 	if (doign == ignoreall)
    274  1.15       wiz 		len--;		/* skip final blank line */
    275  1.28  christos 
    276  1.16       wiz 	if (prefix != NULL)
    277  1.15       wiz 		while (len > 0) {
    278   1.1       cgd 			if (fgets(line, LINESIZE, ibuf) == NULL) {
    279   1.1       cgd 				c = 0;
    280   1.1       cgd 				break;
    281   1.1       cgd 			}
    282  1.15       wiz 			len -= c = strlen(line);
    283   1.1       cgd 			/*
    284   1.1       cgd 			 * Strip trailing whitespace from prefix
    285   1.1       cgd 			 * if line is blank.
    286   1.1       cgd 			 */
    287   1.1       cgd 			if (c > 1)
    288  1.23  christos 				(void)fputs(prefix, obuf);
    289   1.1       cgd 			else
    290  1.18       wiz 				(void)fwrite(prefix, sizeof *prefix,
    291   1.1       cgd 						prefixlen, obuf);
    292  1.23  christos 			(void)fwrite(line, sizeof *line, (size_t)c, obuf);
    293   1.1       cgd 			if (ferror(obuf))
    294   1.1       cgd 				return -1;
    295   1.1       cgd 		}
    296   1.1       cgd 	else
    297  1.15       wiz 		while (len > 0) {
    298  1.23  christos 			c = (int)(len < LINESIZE ? len : LINESIZE);
    299  1.28  christos 			if ((c = fread(line, sizeof *line, (size_t)c, ibuf)) == 0)
    300   1.1       cgd 				break;
    301  1.15       wiz 			len -= c;
    302  1.28  christos 			if (fwrite(line, sizeof *line, (size_t)c, obuf) != (size_t)c)
    303   1.1       cgd 				return -1;
    304   1.1       cgd 		}
    305   1.1       cgd 	if (doign == ignoreall && c > 0 && line[c - 1] != '\n')
    306   1.1       cgd 		/* no final blank line */
    307   1.1       cgd 		if ((c = getc(ibuf)) != EOF && putc(c, obuf) == EOF)
    308  1.26  christos 				return -1;
    309   1.1       cgd 	return 0;
    310   1.1       cgd }
    311   1.1       cgd 
    312   1.1       cgd /*
    313  1.28  christos  * Fix the header by glopping all of the expanded names from
    314  1.28  christos  * the distribution list into the appropriate fields.
    315  1.28  christos  */
    316  1.28  christos static void
    317  1.28  christos fixhead(struct header *hp, struct name *tolist)
    318  1.28  christos {
    319  1.28  christos 	struct name *np;
    320  1.28  christos 
    321  1.28  christos 	hp->h_to = NULL;
    322  1.28  christos 	hp->h_cc = NULL;
    323  1.28  christos 	hp->h_bcc = NULL;
    324  1.28  christos 	for (np = tolist; np != NULL; np = np->n_flink) {
    325  1.28  christos 		if (np->n_type & GDEL)
    326  1.28  christos 			continue;	/* Don't copy deleted addresses to the header */
    327  1.28  christos 		if ((np->n_type & GMASK) == GTO)
    328  1.28  christos 			hp->h_to =
    329  1.28  christos 				cat(hp->h_to, nalloc(np->n_name, np->n_type));
    330  1.28  christos 		else if ((np->n_type & GMASK) == GCC)
    331  1.28  christos 			hp->h_cc =
    332  1.28  christos 				cat(hp->h_cc, nalloc(np->n_name, np->n_type));
    333  1.28  christos 		else if ((np->n_type & GMASK) == GBCC)
    334  1.28  christos 			hp->h_bcc =
    335  1.28  christos 				cat(hp->h_bcc, nalloc(np->n_name, np->n_type));
    336  1.28  christos 	}
    337  1.28  christos }
    338  1.28  christos 
    339  1.28  christos /*
    340  1.28  christos  * Format the given header line to not exceed 72 characters.
    341   1.1       cgd  */
    342  1.28  christos static void
    343  1.28  christos fmt(const char *str, struct name *np, FILE *fo, int comma)
    344   1.1       cgd {
    345  1.28  christos 	int col, len;
    346   1.1       cgd 
    347  1.28  christos 	comma = comma ? 1 : 0;
    348  1.28  christos 	col = strlen(str);
    349  1.28  christos 	if (col)
    350  1.28  christos 		(void)fputs(str, fo);
    351  1.29  christos 	for (/*EMPTY*/; np != NULL; np = np->n_flink) {
    352  1.28  christos 		if (np->n_flink == NULL)
    353  1.28  christos 			comma = 0;
    354  1.28  christos 		len = strlen(np->n_name);
    355  1.28  christos 		col++;		/* for the space */
    356  1.28  christos 		if (col + len + comma > 72 && col > 4) {
    357  1.28  christos 			(void)fputs("\n    ", fo);
    358  1.28  christos 			col = 4;
    359  1.28  christos 		} else
    360  1.28  christos 			(void)putc(' ', fo);
    361  1.28  christos 		(void)fputs(np->n_name, fo);
    362  1.28  christos 		if (comma)
    363  1.28  christos 			(void)putc(',', fo);
    364  1.28  christos 		col += len + comma;
    365  1.28  christos 	}
    366  1.28  christos 	(void)putc('\n', fo);
    367   1.1       cgd }
    368   1.1       cgd 
    369   1.1       cgd /*
    370  1.28  christos  * Dump the to, subject, cc header on the
    371  1.28  christos  * passed file buffer.
    372   1.1       cgd  */
    373  1.28  christos PUBLIC int
    374  1.28  christos puthead(struct header *hp, FILE *fo, int w)
    375  1.28  christos {
    376  1.28  christos 	int gotcha;
    377  1.28  christos 
    378  1.28  christos 	gotcha = 0;
    379  1.28  christos 	if (hp->h_to != NULL && w & GTO)
    380  1.28  christos 		fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
    381  1.28  christos 	if (hp->h_subject != NULL && w & GSUBJECT)
    382  1.28  christos 		(void)fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
    383  1.28  christos 	if (hp->h_smopts != NULL && w & GSMOPTS)
    384  1.28  christos 		(void)fprintf(fo, "(sendmail options: %s)\n", detract(hp->h_smopts, GSMOPTS)), gotcha++;
    385  1.28  christos 	if (hp->h_cc != NULL && w & GCC)
    386  1.28  christos 		fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
    387  1.28  christos 	if (hp->h_bcc != NULL && w & GBCC)
    388  1.28  christos 		fmt("Bcc:", hp->h_bcc, fo, w&GCOMMA), gotcha++;
    389  1.28  christos 	if (hp->h_in_reply_to != NULL && w & GMISC)
    390  1.28  christos 		(void)fprintf(fo, "In-Reply-To: %s\n", hp->h_in_reply_to), gotcha++;
    391  1.28  christos 	if (hp->h_references != NULL && w & GMISC)
    392  1.28  christos 		fmt("References:", hp->h_references, fo, w&GCOMMA), gotcha++;
    393  1.26  christos #ifdef MIME_SUPPORT
    394  1.28  christos 	if (w & GMIME && (hp->h_attach || value(ENAME_MIME_ENCODE_MSG)))
    395  1.28  christos 		mime_putheader(fo, hp), gotcha++;
    396  1.26  christos #endif
    397  1.28  christos 	if (gotcha && w & GNL)
    398  1.28  christos 		(void)putc('\n', fo);
    399  1.28  christos 	return 0;
    400  1.28  christos }
    401  1.28  christos 
    402  1.28  christos /*
    403  1.28  christos  * Prepend a header in front of the collected stuff
    404  1.28  christos  * and return the new file.
    405  1.28  christos  */
    406  1.28  christos static FILE *
    407  1.28  christos infix(struct header *hp, FILE *fi)
    408   1.1       cgd {
    409  1.28  christos 	FILE *nfo, *nfi;
    410  1.28  christos 	int c, fd;
    411  1.28  christos 	char tempname[PATHSIZE];
    412   1.1       cgd 
    413  1.28  christos 	(void)snprintf(tempname, sizeof(tempname),
    414  1.28  christos 	    "%s/mail.RsXXXXXXXXXX", tmpdir);
    415  1.28  christos 	if ((fd = mkstemp(tempname)) == -1 ||
    416  1.28  christos 	    (nfo = Fdopen(fd, "w")) == NULL) {
    417  1.28  christos 		if (fd != -1)
    418  1.28  christos 			(void)close(fd);
    419  1.28  christos 		warn("%s", tempname);
    420  1.28  christos 		return fi;
    421  1.28  christos 	}
    422  1.28  christos 	if ((nfi = Fopen(tempname, "r")) == NULL) {
    423  1.28  christos 		warn("%s", tempname);
    424  1.28  christos 		(void)Fclose(nfo);
    425  1.28  christos 		(void)rm(tempname);
    426  1.28  christos 		return fi;
    427  1.28  christos 	}
    428  1.28  christos 	(void)rm(tempname);
    429  1.26  christos #ifdef MIME_SUPPORT
    430  1.28  christos 	(void)puthead(hp, nfo, GTO|GSUBJECT|GCC|GBCC|GMISC|GMIME|GNL|GCOMMA);
    431  1.28  christos #else
    432  1.28  christos 	(void)puthead(hp, nfo, GTO|GSUBJECT|GCC|GBCC|GMISC|GNL|GCOMMA);
    433  1.26  christos #endif
    434  1.28  christos 
    435  1.28  christos 	c = getc(fi);
    436  1.28  christos 	while (c != EOF) {
    437  1.28  christos 		(void)putc(c, nfo);
    438  1.28  christos 		c = getc(fi);
    439  1.28  christos 	}
    440  1.28  christos 	if (ferror(fi)) {
    441  1.28  christos 		warn("read");
    442  1.28  christos 		rewind(fi);
    443  1.28  christos 		return fi;
    444  1.28  christos 	}
    445  1.28  christos 	(void)fflush(nfo);
    446  1.28  christos 	if (ferror(nfo)) {
    447  1.28  christos 		warn("%s", tempname);
    448  1.28  christos 		(void)Fclose(nfo);
    449  1.28  christos 		(void)Fclose(nfi);
    450  1.28  christos 		rewind(fi);
    451  1.28  christos 		return fi;
    452  1.28  christos 	}
    453  1.28  christos 	(void)Fclose(nfo);
    454  1.28  christos 	(void)Fclose(fi);
    455  1.28  christos 	rewind(nfi);
    456  1.28  christos 	return nfi;
    457   1.1       cgd }
    458   1.1       cgd 
    459   1.1       cgd /*
    460  1.28  christos  * Save the outgoing mail on the passed file.
    461   1.1       cgd  */
    462  1.28  christos /*ARGSUSED*/
    463  1.28  christos static int
    464  1.28  christos savemail(const char name[], FILE *fi)
    465   1.1       cgd {
    466  1.28  christos 	FILE *fo;
    467  1.28  christos 	char buf[LINESIZE];
    468  1.28  christos 	int i;
    469  1.28  christos 	time_t now;
    470  1.28  christos 	mode_t m;
    471   1.1       cgd 
    472  1.28  christos 	m = umask(077);
    473  1.28  christos 	fo = Fopen(name, "a");
    474  1.28  christos 	(void)umask(m);
    475  1.28  christos 	if (fo == NULL) {
    476  1.28  christos 		warn("%s", name);
    477  1.28  christos 		return -1;
    478  1.28  christos 	}
    479  1.28  christos 	(void)time(&now);
    480  1.28  christos 	(void)fprintf(fo, "From %s %s", myname, ctime(&now));
    481  1.28  christos 	while ((i = fread(buf, 1, sizeof buf, fi)) > 0)
    482  1.28  christos 		(void)fwrite(buf, 1, (size_t)i, fo);
    483  1.28  christos 	(void)putc('\n', fo);
    484  1.28  christos 	(void)fflush(fo);
    485  1.28  christos 	if (ferror(fo))
    486  1.28  christos 		warn("%s", name);
    487  1.28  christos 	(void)Fclose(fo);
    488  1.28  christos 	rewind(fi);
    489  1.28  christos 	return 0;
    490   1.1       cgd }
    491   1.1       cgd 
    492   1.1       cgd /*
    493   1.1       cgd  * Mail a message on standard input to the people indicated
    494   1.1       cgd  * in the passed header.  (Internal interface).
    495   1.1       cgd  */
    496  1.28  christos PUBLIC void
    497  1.14       wiz mail1(struct header *hp, int printheaders)
    498   1.1       cgd {
    499  1.22  christos 	const char *cp;
    500   1.1       cgd 	int pid;
    501  1.22  christos 	const char **namelist;
    502   1.1       cgd 	struct name *to;
    503   1.1       cgd 	FILE *mtf;
    504   1.1       cgd 
    505   1.1       cgd 	/*
    506   1.1       cgd 	 * Collect user's mail from standard input.
    507   1.1       cgd 	 * Get the result as mtf.
    508   1.1       cgd 	 */
    509   1.1       cgd 	if ((mtf = collect(hp, printheaders)) == NULL)
    510   1.1       cgd 		return;
    511  1.28  christos 	if (value(ENAME_INTERACTIVE) != NULL) {
    512  1.28  christos 		if (value(ENAME_ASKCC) != NULL || value(ENAME_ASKBCC) != NULL) {
    513  1.28  christos 			if (value(ENAME_ASKCC) != NULL)
    514  1.23  christos 				(void)grabh(hp, GCC);
    515  1.28  christos 			if (value(ENAME_ASKBCC) != NULL)
    516  1.23  christos 				(void)grabh(hp, GBCC);
    517   1.3       jtc 		} else {
    518  1.23  christos 			(void)printf("EOT\n");
    519  1.18       wiz 			(void)fflush(stdout);
    520   1.1       cgd 		}
    521   1.9      ross 	}
    522   1.9      ross 	if (fsize(mtf) == 0) {
    523  1.28  christos 		if (value(ENAME_DONTSENDEMPTY) != NULL)
    524  1.12  christos 			goto out;
    525  1.16       wiz 		if (hp->h_subject == NULL)
    526  1.23  christos 			(void)printf("No message, no subject; hope that's ok\n");
    527   1.1       cgd 		else
    528  1.23  christos 			(void)printf("Null message body; hope that's ok\n");
    529   1.9      ross 	}
    530   1.1       cgd 	/*
    531   1.1       cgd 	 * Now, take the user names from the combined
    532   1.1       cgd 	 * to and cc lists and do all the alias
    533   1.1       cgd 	 * processing.
    534   1.1       cgd 	 */
    535   1.1       cgd 	senderr = 0;
    536   1.1       cgd 	to = usermap(cat(hp->h_bcc, cat(hp->h_to, hp->h_cc)));
    537  1.17       wiz 	if (to == NULL) {
    538  1.23  christos 		(void)printf("No recipients specified\n");
    539   1.1       cgd 		senderr++;
    540   1.1       cgd 	}
    541  1.26  christos #ifdef MIME_SUPPORT
    542  1.26  christos 	/*
    543  1.26  christos 	 * If there are attachments, repackage the mail as a
    544  1.26  christos 	 * multi-part MIME message.
    545  1.26  christos 	 */
    546  1.26  christos 	if (hp->h_attach || value(ENAME_MIME_ENCODE_MSG))
    547  1.26  christos 		mtf = mime_encode(mtf, hp);
    548  1.26  christos #endif
    549   1.1       cgd 	/*
    550   1.1       cgd 	 * Look through the recipient list for names with /'s
    551   1.1       cgd 	 * in them which we write to as files directly.
    552   1.1       cgd 	 */
    553   1.1       cgd 	to = outof(to, mtf, hp);
    554   1.1       cgd 	if (senderr)
    555   1.1       cgd 		savedeadletter(mtf);
    556   1.1       cgd 	to = elide(to);
    557   1.1       cgd 	if (count(to) == 0)
    558   1.1       cgd 		goto out;
    559   1.1       cgd 	fixhead(hp, to);
    560   1.1       cgd 	if ((mtf = infix(hp, mtf)) == NULL) {
    561  1.23  christos 		(void)fprintf(stderr, ". . . message lost, sorry.\n");
    562   1.1       cgd 		return;
    563   1.1       cgd 	}
    564  1.27  christos 	if (hp->h_smopts == NULL) {
    565  1.27  christos 		hp->h_smopts = get_smopts(to);
    566  1.27  christos 		if (hp->h_smopts != NULL &&
    567  1.27  christos 		    hp->h_smopts->n_name[0] != '\0' &&
    568  1.27  christos 		    value(ENAME_SMOPTS_VERIFY) != NULL)
    569  1.27  christos 			if (grabh(hp, GSMOPTS)) {
    570  1.27  christos 				(void)printf("mail aborted!\n");
    571  1.27  christos 				savedeadletter(mtf);
    572  1.27  christos 				goto out;
    573  1.27  christos 			}
    574  1.27  christos 	}
    575   1.1       cgd 	namelist = unpack(cat(hp->h_smopts, to));
    576   1.1       cgd 	if (debug) {
    577  1.22  christos 		const char **t;
    578   1.1       cgd 
    579  1.23  christos 		(void)printf("Sendmail arguments:");
    580  1.16       wiz 		for (t = namelist; *t != NULL; t++)
    581  1.23  christos 			(void)printf(" \"%s\"", *t);
    582  1.23  christos 		(void)printf("\n");
    583   1.1       cgd 		goto out;
    584   1.1       cgd 	}
    585  1.27  christos 	if ((cp = value(ENAME_RECORD)) != NULL)
    586  1.27  christos 		(void)savemail(expand(cp), mtf);
    587   1.1       cgd 	/*
    588   1.1       cgd 	 * Fork, set up the temporary mail file as standard
    589   1.1       cgd 	 * input for "mail", and exec with the user list we generated
    590   1.1       cgd 	 * far above.
    591   1.1       cgd 	 */
    592   1.1       cgd 	pid = fork();
    593   1.1       cgd 	if (pid == -1) {
    594  1.19       wiz 		warn("fork");
    595   1.1       cgd 		savedeadletter(mtf);
    596   1.1       cgd 		goto out;
    597   1.1       cgd 	}
    598   1.1       cgd 	if (pid == 0) {
    599   1.6  christos 		sigset_t nset;
    600  1.23  christos 		(void)sigemptyset(&nset);
    601  1.23  christos 		(void)sigaddset(&nset, SIGHUP);
    602  1.23  christos 		(void)sigaddset(&nset, SIGINT);
    603  1.23  christos 		(void)sigaddset(&nset, SIGQUIT);
    604  1.23  christos 		(void)sigaddset(&nset, SIGTSTP);
    605  1.23  christos 		(void)sigaddset(&nset, SIGTTIN);
    606  1.23  christos 		(void)sigaddset(&nset, SIGTTOU);
    607   1.6  christos 		prepare_child(&nset, fileno(mtf), -1);
    608  1.28  christos 		if ((cp = value(ENAME_SENDMAIL)) != NULL)
    609   1.1       cgd 			cp = expand(cp);
    610   1.1       cgd 		else
    611   1.1       cgd 			cp = _PATH_SENDMAIL;
    612  1.23  christos 		(void)execv(cp, (char *const *)__UNCONST(namelist));
    613  1.19       wiz 		warn("%s", cp);
    614   1.1       cgd 		_exit(1);
    615   1.1       cgd 	}
    616  1.28  christos 	if (value(ENAME_VERBOSE) != NULL)
    617  1.18       wiz 		(void)wait_child(pid);
    618   1.1       cgd 	else
    619   1.1       cgd 		free_child(pid);
    620   1.1       cgd out:
    621  1.18       wiz 	(void)Fclose(mtf);
    622   1.1       cgd }
    623   1.1       cgd 
    624   1.1       cgd /*
    625  1.28  christos  * Interface between the argument list and the mail1 routine
    626  1.28  christos  * which does all the dirty work.
    627   1.1       cgd  */
    628  1.28  christos PUBLIC int
    629  1.28  christos mail(struct name *to, struct name *cc, struct name *bcc,
    630  1.28  christos      struct name *smopts, char *subject, struct attachment *attach)
    631   1.1       cgd {
    632  1.28  christos 	struct header head;
    633   1.1       cgd 
    634  1.28  christos 	/* ensure that all header fields are initially NULL */
    635  1.28  christos 	(void)memset(&head, 0, sizeof(head));
    636   1.1       cgd 
    637  1.28  christos 	head.h_to = to;
    638  1.28  christos 	head.h_subject = subject;
    639  1.28  christos 	head.h_cc = cc;
    640  1.28  christos 	head.h_bcc = bcc;
    641  1.28  christos 	head.h_smopts = smopts;
    642  1.26  christos #ifdef MIME_SUPPORT
    643  1.28  christos 	head.h_attach = attach;
    644  1.26  christos #endif
    645  1.28  christos 	mail1(&head, 0);
    646  1.28  christos 	return 0;
    647   1.1       cgd }
    648   1.1       cgd 
    649   1.1       cgd /*
    650  1.28  christos  * Send mail to a bunch of user names.  The interface is through
    651  1.28  christos  * the mail1 routine below.
    652   1.1       cgd  */
    653  1.28  christos PUBLIC int
    654  1.28  christos sendmail(void *v)
    655   1.1       cgd {
    656  1.28  christos 	char *str = v;
    657  1.28  christos 	struct header head;
    658   1.1       cgd 
    659  1.28  christos 	/* ensure that all header fields are initially NULL */
    660  1.28  christos 	(void)memset(&head, 0, sizeof(head));
    661   1.1       cgd 
    662  1.28  christos 	head.h_to = extract(str, GTO);
    663   1.1       cgd 
    664  1.28  christos 	mail1(&head, 0);
    665  1.28  christos 	return 0;
    666   1.1       cgd }
    667