Home | History | Annotate | Line # | Download | only in mail
send.c revision 1.32
      1  1.31  christos /*	$NetBSD: send.c,v 1.32 2007/10/30 02:28:32 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.31  christos __RCSID("$NetBSD: send.c,v 1.32 2007/10/30 02:28:32 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 	size_t prefixlen;
    127  1.30  christos 	size_t linelen;
    128  1.28  christos 
    129  1.28  christos 	ignoring = 0;
    130  1.28  christos 	prefixlen = 0;
    131   1.1       cgd 
    132   1.1       cgd 	/*
    133   1.1       cgd 	 * Compute the prefix string, without trailing whitespace
    134   1.1       cgd 	 */
    135  1.16       wiz 	if (prefix != NULL) {
    136  1.22  christos 		const char *dp, *dp2 = NULL;
    137  1.22  christos 		for (dp = prefix; *dp; dp++)
    138  1.29  christos 			if (!is_WSP(*dp))
    139  1.22  christos 				dp2 = dp;
    140  1.22  christos 		prefixlen = dp2 == 0 ? 0 : dp2 - prefix + 1;
    141   1.1       cgd 	}
    142   1.1       cgd 	ibuf = setinput(mp);
    143  1.15       wiz 	len = mp->m_size;
    144  1.15       wiz 	isheadflag = 1;
    145   1.1       cgd 	dostat = doign == 0 || !isign("status", doign);
    146   1.1       cgd 	infld = 0;
    147   1.1       cgd 	firstline = 1;
    148   1.1       cgd 	/*
    149   1.1       cgd 	 * Process headers first
    150   1.1       cgd 	 */
    151  1.26  christos #ifdef MIME_SUPPORT
    152  1.26  christos 	if (mip)
    153  1.26  christos 		obuf = mime_decode_header(mip);
    154  1.26  christos #endif
    155  1.15       wiz 	while (len > 0 && isheadflag) {
    156  1.30  christos 		if (fgets(line, sizeof(line), ibuf) == NULL)
    157   1.1       cgd 			break;
    158  1.30  christos 		len -= linelen = strlen(line);
    159   1.1       cgd 		if (firstline) {
    160  1.29  christos 			/*
    161   1.1       cgd 			 * First line is the From line, so no headers
    162   1.1       cgd 			 * there to worry about
    163   1.1       cgd 			 */
    164   1.1       cgd 			firstline = 0;
    165  1.30  christos 			ignoring = doign == ignoreall || doign == bouncetab;
    166  1.26  christos #ifdef MIME_SUPPORT
    167  1.26  christos 			/* XXX - ignore multipart boundary lines! */
    168  1.26  christos 			if (line[0] == '-' && line[1] == '-')
    169  1.26  christos 				ignoring = 1;
    170  1.26  christos #endif
    171   1.1       cgd 		} else if (line[0] == '\n') {
    172   1.1       cgd 			/*
    173   1.1       cgd 			 * If line is blank, we've reached end of
    174   1.1       cgd 			 * headers, so force out status: field
    175   1.1       cgd 			 * and note that we are no longer in header
    176   1.1       cgd 			 * fields
    177   1.1       cgd 			 */
    178   1.1       cgd 			if (dostat) {
    179   1.1       cgd 				statusput(mp, obuf, prefix);
    180   1.1       cgd 				dostat = 0;
    181   1.1       cgd 			}
    182  1.15       wiz 			isheadflag = 0;
    183   1.1       cgd 			ignoring = doign == ignoreall;
    184  1.29  christos 		} else if (infld && is_WSP(line[0])) {
    185   1.1       cgd 			/*
    186   1.1       cgd 			 * If this line is a continuation (via space or tab)
    187   1.1       cgd 			 * of a previous header field, just echo it
    188   1.1       cgd 			 * (unless the field should be ignored).
    189   1.1       cgd 			 * In other words, nothing to do.
    190   1.1       cgd 			 */
    191   1.1       cgd 		} else {
    192   1.1       cgd 			/*
    193   1.1       cgd 			 * Pick up the header field if we have one.
    194   1.1       cgd 			 */
    195  1.29  christos 			char *save_cp;
    196  1.29  christos 			for (cp = line; *cp && *cp != ':' && !is_WSP(*cp); cp++)
    197  1.28  christos 				continue;
    198  1.29  christos 			save_cp = cp;
    199  1.29  christos 			cp = skip_WSP(cp);
    200  1.29  christos 			if (*cp++ != ':') {
    201   1.1       cgd 				/*
    202   1.1       cgd 				 * Not a header line, force out status:
    203   1.1       cgd 				 * This happens in uucp style mail where
    204   1.1       cgd 				 * there are no headers at all.
    205   1.1       cgd 				 */
    206   1.1       cgd 				if (dostat) {
    207   1.1       cgd 					statusput(mp, obuf, prefix);
    208   1.1       cgd 					dostat = 0;
    209   1.1       cgd 				}
    210   1.1       cgd 				if (doign != ignoreall)
    211   1.1       cgd 					/* add blank line */
    212  1.18       wiz 					(void)putc('\n', obuf);
    213  1.15       wiz 				isheadflag = 0;
    214   1.1       cgd 				ignoring = 0;
    215   1.1       cgd 			} else {
    216   1.1       cgd 				/*
    217   1.1       cgd 				 * If it is an ignored field and
    218   1.1       cgd 				 * we care about such things, skip it.
    219   1.1       cgd 				 */
    220  1.29  christos 				int save_c;
    221  1.29  christos 				save_c = *save_cp;
    222  1.29  christos 				*save_cp = 0;	/* temporarily null terminate */
    223   1.1       cgd 				if (doign && isign(line, doign))
    224   1.1       cgd 					ignoring = 1;
    225   1.1       cgd 				else if ((line[0] == 's' || line[0] == 'S') &&
    226   1.1       cgd 					 strcasecmp(line, "status") == 0) {
    227   1.1       cgd 					/*
    228   1.1       cgd 					 * If the field is "status," go compute
    229   1.1       cgd 					 * and print the real Status: field
    230   1.1       cgd 					 */
    231   1.1       cgd 					if (dostat) {
    232   1.1       cgd 						statusput(mp, obuf, prefix);
    233   1.1       cgd 						dostat = 0;
    234   1.1       cgd 					}
    235   1.1       cgd 					ignoring = 1;
    236   1.1       cgd 				} else {
    237   1.1       cgd 					ignoring = 0;
    238   1.1       cgd 				}
    239   1.1       cgd 				infld = 1;
    240  1.30  christos 				*save_cp = save_c;	/* restore the line */
    241   1.1       cgd 			}
    242   1.1       cgd 		}
    243   1.1       cgd 		if (!ignoring) {
    244   1.1       cgd 			/*
    245   1.1       cgd 			 * Strip trailing whitespace from prefix
    246   1.1       cgd 			 * if line is blank.
    247   1.1       cgd 			 */
    248  1.16       wiz 			if (prefix != NULL) {
    249  1.30  christos 				if (linelen > 1)
    250  1.23  christos 					(void)fputs(prefix, obuf);
    251   1.1       cgd 				else
    252  1.30  christos 					(void)fwrite(prefix, sizeof(*prefix),
    253   1.1       cgd 							prefixlen, obuf);
    254   1.9      ross 			}
    255  1.30  christos 			(void)fwrite(line, sizeof(*line), linelen, obuf);
    256   1.1       cgd 			if (ferror(obuf))
    257   1.1       cgd 				return -1;
    258   1.1       cgd 		}
    259   1.1       cgd 	}
    260   1.1       cgd 	/*
    261   1.1       cgd 	 * Copy out message body
    262   1.1       cgd 	 */
    263  1.26  christos #ifdef MIME_SUPPORT
    264  1.26  christos 	if (mip) {
    265  1.26  christos 		obuf = mime_decode_body(mip);
    266  1.26  christos 		if (obuf == NULL) /* XXX - early out */
    267  1.26  christos 			return 0;
    268  1.26  christos 	}
    269  1.26  christos #endif
    270   1.1       cgd 	if (doign == ignoreall)
    271  1.15       wiz 		len--;		/* skip final blank line */
    272  1.28  christos 
    273  1.30  christos 	linelen = 0;		/* needed for in case len == 0 */
    274  1.16       wiz 	if (prefix != NULL)
    275  1.15       wiz 		while (len > 0) {
    276  1.30  christos 			if (fgets(line, sizeof(line), ibuf) == NULL) {
    277  1.30  christos 				linelen = 0;
    278   1.1       cgd 				break;
    279   1.1       cgd 			}
    280  1.30  christos 			len -= linelen = strlen(line);
    281   1.1       cgd 			/*
    282   1.1       cgd 			 * Strip trailing whitespace from prefix
    283   1.1       cgd 			 * if line is blank.
    284   1.1       cgd 			 */
    285  1.30  christos 			if (linelen > 1)
    286  1.23  christos 				(void)fputs(prefix, obuf);
    287   1.1       cgd 			else
    288  1.30  christos 				(void)fwrite(prefix, sizeof(*prefix),
    289   1.1       cgd 						prefixlen, obuf);
    290  1.31  christos 			(void)fwrite(line, sizeof(*line), linelen, obuf);
    291   1.1       cgd 			if (ferror(obuf))
    292   1.1       cgd 				return -1;
    293   1.1       cgd 		}
    294   1.1       cgd 	else
    295  1.15       wiz 		while (len > 0) {
    296  1.30  christos 			linelen = len < sizeof(line) ? (size_t)len : sizeof(line);
    297  1.30  christos 			if ((linelen = fread(line, sizeof(*line), linelen, ibuf)) == 0)
    298   1.1       cgd 				break;
    299  1.30  christos 			len -= linelen;
    300  1.30  christos 			if (fwrite(line, sizeof(*line), linelen, obuf) != linelen)
    301   1.1       cgd 				return -1;
    302   1.1       cgd 		}
    303  1.30  christos 	if (doign == ignoreall && linelen > 0 && line[linelen - 1] != '\n') {
    304  1.30  christos 		int c;
    305   1.1       cgd 		/* no final blank line */
    306   1.1       cgd 		if ((c = getc(ibuf)) != EOF && putc(c, obuf) == EOF)
    307  1.30  christos 			return -1;
    308  1.30  christos 	}
    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.32  christos  *
    462  1.32  christos  * Take care not to save a valid headline or the mbox file will be
    463  1.32  christos  * broken.  Prefix valid headlines with '>'.
    464  1.32  christos  *
    465  1.32  christos  * Note: most servers prefix any line starting with "From " in the
    466  1.32  christos  * body of the message.  We are more restrictive to avoid header/body
    467  1.32  christos  * issues.
    468   1.1       cgd  */
    469  1.28  christos /*ARGSUSED*/
    470  1.28  christos static int
    471  1.28  christos savemail(const char name[], FILE *fi)
    472   1.1       cgd {
    473  1.28  christos 	FILE *fo;
    474  1.28  christos 	time_t now;
    475  1.28  christos 	mode_t m;
    476  1.32  christos 	char *line;
    477  1.32  christos 	size_t linelen;
    478  1.32  christos 	int afterblank;
    479   1.1       cgd 
    480  1.28  christos 	m = umask(077);
    481  1.28  christos 	fo = Fopen(name, "a");
    482  1.28  christos 	(void)umask(m);
    483  1.28  christos 	if (fo == NULL) {
    484  1.28  christos 		warn("%s", name);
    485  1.28  christos 		return -1;
    486  1.28  christos 	}
    487  1.28  christos 	(void)time(&now);
    488  1.28  christos 	(void)fprintf(fo, "From %s %s", myname, ctime(&now));
    489  1.32  christos 	afterblank = 0;
    490  1.32  christos 	while ((line = fgetln(fi, &linelen)) != NULL) {
    491  1.32  christos 		char c, *cp;
    492  1.32  christos 		cp = line + linelen - 1;
    493  1.32  christos 		if (afterblank &&
    494  1.32  christos 		    linelen > sizeof("From . Aaa Aaa O0 00:00 0000") &&
    495  1.32  christos 		    line[0] == 'F' &&
    496  1.32  christos 		    line[1] == 'r' &&
    497  1.32  christos 		    line[2] == 'o' &&
    498  1.32  christos 		    line[3] == 'm' &&
    499  1.32  christos 		    line[4] == ' ' &&
    500  1.32  christos 		    *cp == '\n') {
    501  1.32  christos 			c = *cp;
    502  1.32  christos 			*cp = '\0';
    503  1.32  christos 			if (ishead(line))
    504  1.32  christos 				(void)fputc('>', fo);
    505  1.32  christos 			*cp = c;
    506  1.32  christos 		}
    507  1.32  christos 		(void)fwrite(line, 1, linelen, fo);
    508  1.32  christos 		afterblank = linelen == 1 && line[0] == '\n';
    509  1.32  christos 	}
    510  1.28  christos 	(void)putc('\n', fo);
    511  1.28  christos 	(void)fflush(fo);
    512  1.28  christos 	if (ferror(fo))
    513  1.28  christos 		warn("%s", name);
    514  1.28  christos 	(void)Fclose(fo);
    515  1.28  christos 	rewind(fi);
    516  1.28  christos 	return 0;
    517   1.1       cgd }
    518   1.1       cgd 
    519   1.1       cgd /*
    520  1.30  christos  * Mail a message that is already prepared in a file.
    521  1.30  christos  */
    522  1.30  christos PUBLIC void
    523  1.30  christos mail2(FILE *mtf, const char **namelist)
    524  1.30  christos {
    525  1.30  christos 	int pid;
    526  1.30  christos 	const char *cp;
    527  1.30  christos 
    528  1.30  christos 	if (debug) {
    529  1.30  christos 		const char **t;
    530  1.30  christos 
    531  1.30  christos 		(void)printf("Sendmail arguments:");
    532  1.30  christos 		for (t = namelist; *t != NULL; t++)
    533  1.30  christos 			(void)printf(" \"%s\"", *t);
    534  1.30  christos 		(void)printf("\n");
    535  1.30  christos 		return;
    536  1.30  christos 	}
    537  1.30  christos 	if ((cp = value(ENAME_RECORD)) != NULL)
    538  1.30  christos 		(void)savemail(expand(cp), mtf);
    539  1.30  christos 	/*
    540  1.30  christos 	 * Fork, set up the temporary mail file as standard
    541  1.30  christos 	 * input for "mail", and exec with the user list we generated
    542  1.30  christos 	 * far above.
    543  1.30  christos 	 */
    544  1.30  christos 	pid = fork();
    545  1.30  christos 	switch (pid) {
    546  1.30  christos 	case -1:
    547  1.30  christos 		warn("fork");
    548  1.30  christos 		savedeadletter(mtf);
    549  1.30  christos 		return;
    550  1.30  christos 	case 0:
    551  1.30  christos 	{
    552  1.30  christos 		sigset_t nset;
    553  1.30  christos 		(void)sigemptyset(&nset);
    554  1.30  christos 		(void)sigaddset(&nset, SIGHUP);
    555  1.30  christos 		(void)sigaddset(&nset, SIGINT);
    556  1.30  christos 		(void)sigaddset(&nset, SIGQUIT);
    557  1.30  christos 		(void)sigaddset(&nset, SIGTSTP);
    558  1.30  christos 		(void)sigaddset(&nset, SIGTTIN);
    559  1.30  christos 		(void)sigaddset(&nset, SIGTTOU);
    560  1.30  christos 		prepare_child(&nset, fileno(mtf), -1);
    561  1.30  christos 		if ((cp = value(ENAME_SENDMAIL)) != NULL)
    562  1.30  christos 			cp = expand(cp);
    563  1.30  christos 		else
    564  1.30  christos 			cp = _PATH_SENDMAIL;
    565  1.30  christos 		(void)execv(cp, (char *const *)__UNCONST(namelist));
    566  1.30  christos 		warn("%s", cp);
    567  1.30  christos 		_exit(1);
    568  1.30  christos 		break;		/* Appease GCC */
    569  1.30  christos 	}
    570  1.30  christos 	default:
    571  1.30  christos 		if (value(ENAME_VERBOSE) != NULL)
    572  1.30  christos 			(void)wait_child(pid);
    573  1.30  christos 		else
    574  1.30  christos 			free_child(pid);
    575  1.30  christos 		break;
    576  1.30  christos 	}
    577  1.30  christos }
    578  1.30  christos 
    579  1.30  christos /*
    580   1.1       cgd  * Mail a message on standard input to the people indicated
    581   1.1       cgd  * in the passed header.  (Internal interface).
    582   1.1       cgd  */
    583  1.28  christos PUBLIC void
    584  1.14       wiz mail1(struct header *hp, int printheaders)
    585   1.1       cgd {
    586  1.22  christos 	const char **namelist;
    587   1.1       cgd 	struct name *to;
    588   1.1       cgd 	FILE *mtf;
    589   1.1       cgd 
    590   1.1       cgd 	/*
    591   1.1       cgd 	 * Collect user's mail from standard input.
    592   1.1       cgd 	 * Get the result as mtf.
    593   1.1       cgd 	 */
    594   1.1       cgd 	if ((mtf = collect(hp, printheaders)) == NULL)
    595   1.1       cgd 		return;
    596  1.32  christos 
    597  1.28  christos 	if (value(ENAME_INTERACTIVE) != NULL) {
    598  1.28  christos 		if (value(ENAME_ASKCC) != NULL || value(ENAME_ASKBCC) != NULL) {
    599  1.28  christos 			if (value(ENAME_ASKCC) != NULL)
    600  1.23  christos 				(void)grabh(hp, GCC);
    601  1.28  christos 			if (value(ENAME_ASKBCC) != NULL)
    602  1.23  christos 				(void)grabh(hp, GBCC);
    603   1.3       jtc 		} else {
    604  1.23  christos 			(void)printf("EOT\n");
    605  1.18       wiz 			(void)fflush(stdout);
    606   1.1       cgd 		}
    607   1.9      ross 	}
    608   1.9      ross 	if (fsize(mtf) == 0) {
    609  1.28  christos 		if (value(ENAME_DONTSENDEMPTY) != NULL)
    610  1.12  christos 			goto out;
    611  1.16       wiz 		if (hp->h_subject == NULL)
    612  1.23  christos 			(void)printf("No message, no subject; hope that's ok\n");
    613   1.1       cgd 		else
    614  1.23  christos 			(void)printf("Null message body; hope that's ok\n");
    615   1.9      ross 	}
    616   1.1       cgd 	/*
    617   1.1       cgd 	 * Now, take the user names from the combined
    618   1.1       cgd 	 * to and cc lists and do all the alias
    619   1.1       cgd 	 * processing.
    620   1.1       cgd 	 */
    621   1.1       cgd 	senderr = 0;
    622   1.1       cgd 	to = usermap(cat(hp->h_bcc, cat(hp->h_to, hp->h_cc)));
    623  1.17       wiz 	if (to == NULL) {
    624  1.23  christos 		(void)printf("No recipients specified\n");
    625   1.1       cgd 		senderr++;
    626   1.1       cgd 	}
    627  1.26  christos #ifdef MIME_SUPPORT
    628  1.26  christos 	/*
    629  1.26  christos 	 * If there are attachments, repackage the mail as a
    630  1.26  christos 	 * multi-part MIME message.
    631  1.26  christos 	 */
    632  1.26  christos 	if (hp->h_attach || value(ENAME_MIME_ENCODE_MSG))
    633  1.26  christos 		mtf = mime_encode(mtf, hp);
    634  1.26  christos #endif
    635   1.1       cgd 	/*
    636   1.1       cgd 	 * Look through the recipient list for names with /'s
    637   1.1       cgd 	 * in them which we write to as files directly.
    638   1.1       cgd 	 */
    639   1.1       cgd 	to = outof(to, mtf, hp);
    640   1.1       cgd 	if (senderr)
    641   1.1       cgd 		savedeadletter(mtf);
    642   1.1       cgd 	to = elide(to);
    643   1.1       cgd 	if (count(to) == 0)
    644   1.1       cgd 		goto out;
    645   1.1       cgd 	fixhead(hp, to);
    646   1.1       cgd 	if ((mtf = infix(hp, mtf)) == NULL) {
    647  1.23  christos 		(void)fprintf(stderr, ". . . message lost, sorry.\n");
    648   1.1       cgd 		return;
    649   1.1       cgd 	}
    650  1.27  christos 	if (hp->h_smopts == NULL) {
    651  1.27  christos 		hp->h_smopts = get_smopts(to);
    652  1.27  christos 		if (hp->h_smopts != NULL &&
    653  1.27  christos 		    hp->h_smopts->n_name[0] != '\0' &&
    654  1.27  christos 		    value(ENAME_SMOPTS_VERIFY) != NULL)
    655  1.27  christos 			if (grabh(hp, GSMOPTS)) {
    656  1.27  christos 				(void)printf("mail aborted!\n");
    657  1.27  christos 				savedeadletter(mtf);
    658  1.27  christos 				goto out;
    659  1.27  christos 			}
    660  1.27  christos 	}
    661   1.1       cgd 	namelist = unpack(cat(hp->h_smopts, to));
    662  1.30  christos 	mail2(mtf, namelist);
    663  1.30  christos  out:
    664  1.18       wiz 	(void)Fclose(mtf);
    665   1.1       cgd }
    666   1.1       cgd 
    667   1.1       cgd /*
    668  1.28  christos  * Interface between the argument list and the mail1 routine
    669  1.28  christos  * which does all the dirty work.
    670   1.1       cgd  */
    671  1.28  christos PUBLIC int
    672  1.28  christos mail(struct name *to, struct name *cc, struct name *bcc,
    673  1.28  christos      struct name *smopts, char *subject, struct attachment *attach)
    674   1.1       cgd {
    675  1.28  christos 	struct header head;
    676   1.1       cgd 
    677  1.28  christos 	/* ensure that all header fields are initially NULL */
    678  1.28  christos 	(void)memset(&head, 0, sizeof(head));
    679   1.1       cgd 
    680  1.28  christos 	head.h_to = to;
    681  1.28  christos 	head.h_subject = subject;
    682  1.28  christos 	head.h_cc = cc;
    683  1.28  christos 	head.h_bcc = bcc;
    684  1.28  christos 	head.h_smopts = smopts;
    685  1.26  christos #ifdef MIME_SUPPORT
    686  1.28  christos 	head.h_attach = attach;
    687  1.26  christos #endif
    688  1.28  christos 	mail1(&head, 0);
    689  1.28  christos 	return 0;
    690   1.1       cgd }
    691   1.1       cgd 
    692   1.1       cgd /*
    693  1.28  christos  * Send mail to a bunch of user names.  The interface is through
    694  1.30  christos  * the mail1 routine above.
    695   1.1       cgd  */
    696  1.28  christos PUBLIC int
    697  1.28  christos sendmail(void *v)
    698   1.1       cgd {
    699  1.28  christos 	char *str = v;
    700  1.28  christos 	struct header head;
    701   1.1       cgd 
    702  1.28  christos 	/* ensure that all header fields are initially NULL */
    703  1.28  christos 	(void)memset(&head, 0, sizeof(head));
    704   1.1       cgd 
    705  1.28  christos 	head.h_to = extract(str, GTO);
    706   1.1       cgd 
    707  1.28  christos 	mail1(&head, 0);
    708  1.28  christos 	return 0;
    709   1.1       cgd }
    710