xref: /freebsd/usr.bin/mail/send.c (revision 5e3934b1)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1980, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
329b50d902SRodney W. Grimes #include "rcv.h"
339b50d902SRodney W. Grimes #include "extern.h"
349b50d902SRodney W. Grimes 
359b50d902SRodney W. Grimes /*
369b50d902SRodney W. Grimes  * Mail -- a mail program
379b50d902SRodney W. Grimes  *
389b50d902SRodney W. Grimes  * Mail to others.
399b50d902SRodney W. Grimes  */
409b50d902SRodney W. Grimes 
419b50d902SRodney W. Grimes /*
429b50d902SRodney W. Grimes  * Send message described by the passed pointer to the
439b50d902SRodney W. Grimes  * passed output buffer.  Return -1 on error.
449b50d902SRodney W. Grimes  * Adjust the status: field if need be.
459b50d902SRodney W. Grimes  * If doign is given, suppress ignored header fields.
469b50d902SRodney W. Grimes  * prefix is a string to prepend to each output line.
479b50d902SRodney W. Grimes  */
489b50d902SRodney W. Grimes int
sendmessage(struct message * mp,FILE * obuf,struct ignoretab * doign,char * prefix)496d8484b0SPhilippe Charnier sendmessage(struct message *mp, FILE *obuf, struct ignoretab *doign,
506d8484b0SPhilippe Charnier 	char *prefix)
519b50d902SRodney W. Grimes {
529b50d902SRodney W. Grimes 	long count;
539ce73e90SMike Heffner 	FILE *ibuf;
549ce73e90SMike Heffner 	char *cp, *cp2, line[LINESIZE];
559b50d902SRodney W. Grimes 	int ishead, infld, ignoring, dostat, firstline;
564994a9b8SXin LI 	int c = 0, length, prefixlen;
579b50d902SRodney W. Grimes 
589b50d902SRodney W. Grimes 	/*
599b50d902SRodney W. Grimes 	 * Compute the prefix string, without trailing whitespace
609b50d902SRodney W. Grimes 	 */
619ce73e90SMike Heffner 	if (prefix != NULL) {
629b50d902SRodney W. Grimes 		cp2 = 0;
639ce73e90SMike Heffner 		for (cp = prefix; *cp != '\0'; cp++)
649b50d902SRodney W. Grimes 			if (*cp != ' ' && *cp != '\t')
659b50d902SRodney W. Grimes 				cp2 = cp;
669ce73e90SMike Heffner 		prefixlen = cp2 == NULL ? 0 : cp2 - prefix + 1;
679b50d902SRodney W. Grimes 	}
689b50d902SRodney W. Grimes 	ibuf = setinput(mp);
699b50d902SRodney W. Grimes 	count = mp->m_size;
709b50d902SRodney W. Grimes 	ishead = 1;
719b50d902SRodney W. Grimes 	dostat = doign == 0 || !isign("status", doign);
729b50d902SRodney W. Grimes 	infld = 0;
739b50d902SRodney W. Grimes 	firstline = 1;
749b50d902SRodney W. Grimes 	/*
759b50d902SRodney W. Grimes 	 * Process headers first
769b50d902SRodney W. Grimes 	 */
779b50d902SRodney W. Grimes 	while (count > 0 && ishead) {
780c3a8314SMike Heffner 		if (fgets(line, sizeof(line), ibuf) == NULL)
799b50d902SRodney W. Grimes 			break;
809b50d902SRodney W. Grimes 		count -= length = strlen(line);
819b50d902SRodney W. Grimes 		if (firstline) {
829b50d902SRodney W. Grimes 			/*
839b50d902SRodney W. Grimes 			 * First line is the From line, so no headers
849b50d902SRodney W. Grimes 			 * there to worry about
859b50d902SRodney W. Grimes 			 */
869b50d902SRodney W. Grimes 			firstline = 0;
879b50d902SRodney W. Grimes 			ignoring = doign == ignoreall;
889b50d902SRodney W. Grimes 		} else if (line[0] == '\n') {
899b50d902SRodney W. Grimes 			/*
909b50d902SRodney W. Grimes 			 * If line is blank, we've reached end of
919b50d902SRodney W. Grimes 			 * headers, so force out status: field
929b50d902SRodney W. Grimes 			 * and note that we are no longer in header
939b50d902SRodney W. Grimes 			 * fields
949b50d902SRodney W. Grimes 			 */
959b50d902SRodney W. Grimes 			if (dostat) {
969b50d902SRodney W. Grimes 				statusput(mp, obuf, prefix);
979b50d902SRodney W. Grimes 				dostat = 0;
989b50d902SRodney W. Grimes 			}
999b50d902SRodney W. Grimes 			ishead = 0;
1009b50d902SRodney W. Grimes 			ignoring = doign == ignoreall;
1019b50d902SRodney W. Grimes 		} else if (infld && (line[0] == ' ' || line[0] == '\t')) {
1029b50d902SRodney W. Grimes 			/*
1039b50d902SRodney W. Grimes 			 * If this line is a continuation (via space or tab)
1049b50d902SRodney W. Grimes 			 * of a previous header field, just echo it
1059b50d902SRodney W. Grimes 			 * (unless the field should be ignored).
1069b50d902SRodney W. Grimes 			 * In other words, nothing to do.
1079b50d902SRodney W. Grimes 			 */
1089b50d902SRodney W. Grimes 		} else {
1099b50d902SRodney W. Grimes 			/*
1109b50d902SRodney W. Grimes 			 * Pick up the header field if we have one.
1119b50d902SRodney W. Grimes 			 */
1129ce73e90SMike Heffner 			for (cp = line; (c = *cp++) != '\0' && c != ':' &&
1136d48fa43SAndrey A. Chernov 			    !isspace((unsigned char)c);)
1149b50d902SRodney W. Grimes 				;
1159b50d902SRodney W. Grimes 			cp2 = --cp;
1166d48fa43SAndrey A. Chernov 			while (isspace((unsigned char)*cp++))
1179b50d902SRodney W. Grimes 				;
1189b50d902SRodney W. Grimes 			if (cp[-1] != ':') {
1199b50d902SRodney W. Grimes 				/*
1209b50d902SRodney W. Grimes 				 * Not a header line, force out status:
1219b50d902SRodney W. Grimes 				 * This happens in uucp style mail where
1229b50d902SRodney W. Grimes 				 * there are no headers at all.
1239b50d902SRodney W. Grimes 				 */
1249b50d902SRodney W. Grimes 				if (dostat) {
1259b50d902SRodney W. Grimes 					statusput(mp, obuf, prefix);
1269b50d902SRodney W. Grimes 					dostat = 0;
1279b50d902SRodney W. Grimes 				}
1289b50d902SRodney W. Grimes 				if (doign != ignoreall)
1299b50d902SRodney W. Grimes 					/* add blank line */
1309b50d902SRodney W. Grimes 					(void)putc('\n', obuf);
1319b50d902SRodney W. Grimes 				ishead = 0;
1329b50d902SRodney W. Grimes 				ignoring = 0;
1339b50d902SRodney W. Grimes 			} else {
1349b50d902SRodney W. Grimes 				/*
1359b50d902SRodney W. Grimes 				 * If it is an ignored field and
1369b50d902SRodney W. Grimes 				 * we care about such things, skip it.
1379b50d902SRodney W. Grimes 				 */
1389ce73e90SMike Heffner 				*cp2 = '\0';	/* temporarily null terminate */
1399b50d902SRodney W. Grimes 				if (doign && isign(line, doign))
1409b50d902SRodney W. Grimes 					ignoring = 1;
1419b50d902SRodney W. Grimes 				else if ((line[0] == 's' || line[0] == 'S') &&
1429b50d902SRodney W. Grimes 					 strcasecmp(line, "status") == 0) {
1439b50d902SRodney W. Grimes 					/*
1449b50d902SRodney W. Grimes 					 * If the field is "status," go compute
1459b50d902SRodney W. Grimes 					 * and print the real Status: field
1469b50d902SRodney W. Grimes 					 */
1479b50d902SRodney W. Grimes 					if (dostat) {
1489b50d902SRodney W. Grimes 						statusput(mp, obuf, prefix);
1499b50d902SRodney W. Grimes 						dostat = 0;
1509b50d902SRodney W. Grimes 					}
1519b50d902SRodney W. Grimes 					ignoring = 1;
1529b50d902SRodney W. Grimes 				} else {
1539b50d902SRodney W. Grimes 					ignoring = 0;
1549b50d902SRodney W. Grimes 					*cp2 = c;	/* restore */
1559b50d902SRodney W. Grimes 				}
1569b50d902SRodney W. Grimes 				infld = 1;
1579b50d902SRodney W. Grimes 			}
1589b50d902SRodney W. Grimes 		}
1599b50d902SRodney W. Grimes 		if (!ignoring) {
1609b50d902SRodney W. Grimes 			/*
1619b50d902SRodney W. Grimes 			 * Strip trailing whitespace from prefix
1629b50d902SRodney W. Grimes 			 * if line is blank.
1639b50d902SRodney W. Grimes 			 */
1649ce73e90SMike Heffner 			if (prefix != NULL) {
1659b50d902SRodney W. Grimes 				if (length > 1)
1669b50d902SRodney W. Grimes 					fputs(prefix, obuf);
1679b50d902SRodney W. Grimes 				else
1689ce73e90SMike Heffner 					(void)fwrite(prefix, sizeof(*prefix),
1699b50d902SRodney W. Grimes 					    prefixlen, obuf);
1700c3a8314SMike Heffner 			}
1719ce73e90SMike Heffner 			(void)fwrite(line, sizeof(*line), length, obuf);
1729b50d902SRodney W. Grimes 			if (ferror(obuf))
1739ce73e90SMike Heffner 				return (-1);
1749b50d902SRodney W. Grimes 		}
1759b50d902SRodney W. Grimes 	}
1769b50d902SRodney W. Grimes 	/*
1779b50d902SRodney W. Grimes 	 * Copy out message body
1789b50d902SRodney W. Grimes 	 */
1799b50d902SRodney W. Grimes 	if (doign == ignoreall)
1809b50d902SRodney W. Grimes 		count--;		/* skip final blank line */
1819ce73e90SMike Heffner 	if (prefix != NULL)
1829b50d902SRodney W. Grimes 		while (count > 0) {
1830c3a8314SMike Heffner 			if (fgets(line, sizeof(line), ibuf) == NULL) {
1849b50d902SRodney W. Grimes 				c = 0;
1859b50d902SRodney W. Grimes 				break;
1869b50d902SRodney W. Grimes 			}
1879b50d902SRodney W. Grimes 			count -= c = strlen(line);
1889b50d902SRodney W. Grimes 			/*
1899b50d902SRodney W. Grimes 			 * Strip trailing whitespace from prefix
1909b50d902SRodney W. Grimes 			 * if line is blank.
1919b50d902SRodney W. Grimes 			 */
1929b50d902SRodney W. Grimes 			if (c > 1)
1939b50d902SRodney W. Grimes 				fputs(prefix, obuf);
1949b50d902SRodney W. Grimes 			else
1959ce73e90SMike Heffner 				(void)fwrite(prefix, sizeof(*prefix),
1969b50d902SRodney W. Grimes 				    prefixlen, obuf);
1979ce73e90SMike Heffner 			(void)fwrite(line, sizeof(*line), c, obuf);
1989b50d902SRodney W. Grimes 			if (ferror(obuf))
1999ce73e90SMike Heffner 				return (-1);
2009b50d902SRodney W. Grimes 		}
2019b50d902SRodney W. Grimes 	else
2029b50d902SRodney W. Grimes 		while (count > 0) {
2039b50d902SRodney W. Grimes 			c = count < LINESIZE ? count : LINESIZE;
2049ce73e90SMike Heffner 			if ((c = fread(line, sizeof(*line), c, ibuf)) <= 0)
2059b50d902SRodney W. Grimes 				break;
2069b50d902SRodney W. Grimes 			count -= c;
2079ce73e90SMike Heffner 			if (fwrite(line, sizeof(*line), c, obuf) != c)
2089ce73e90SMike Heffner 				return (-1);
2099b50d902SRodney W. Grimes 		}
2109b50d902SRodney W. Grimes 	if (doign == ignoreall && c > 0 && line[c - 1] != '\n')
2119b50d902SRodney W. Grimes 		/* no final blank line */
2129b50d902SRodney W. Grimes 		if ((c = getc(ibuf)) != EOF && putc(c, obuf) == EOF)
2139ce73e90SMike Heffner 			return (-1);
2149ce73e90SMike Heffner 	return (0);
2159b50d902SRodney W. Grimes }
2169b50d902SRodney W. Grimes 
2179b50d902SRodney W. Grimes /*
2189b50d902SRodney W. Grimes  * Output a reasonable looking status field.
2199b50d902SRodney W. Grimes  */
2209b50d902SRodney W. Grimes void
statusput(struct message * mp,FILE * obuf,char * prefix)2216d8484b0SPhilippe Charnier statusput(struct message *mp, FILE *obuf, char *prefix)
2229b50d902SRodney W. Grimes {
2239b50d902SRodney W. Grimes 	char statout[3];
2249ce73e90SMike Heffner 	char *cp = statout;
2259b50d902SRodney W. Grimes 
2269b50d902SRodney W. Grimes 	if (mp->m_flag & MREAD)
2279b50d902SRodney W. Grimes 		*cp++ = 'R';
2289b50d902SRodney W. Grimes 	if ((mp->m_flag & MNEW) == 0)
2299b50d902SRodney W. Grimes 		*cp++ = 'O';
2309ce73e90SMike Heffner 	*cp = '\0';
2319ce73e90SMike Heffner 	if (statout[0] != '\0')
2329b50d902SRodney W. Grimes 		fprintf(obuf, "%sStatus: %s\n",
2339ce73e90SMike Heffner 			prefix == NULL ? "" : prefix, statout);
2349b50d902SRodney W. Grimes }
2359b50d902SRodney W. Grimes 
2369b50d902SRodney W. Grimes /*
2379b50d902SRodney W. Grimes  * Interface between the argument list and the mail1 routine
2389b50d902SRodney W. Grimes  * which does all the dirty work.
2399b50d902SRodney W. Grimes  */
2409b50d902SRodney W. Grimes int
mail(struct name * to,struct name * cc,struct name * bcc,struct name * smopts,char * subject,char * replyto)2416d8484b0SPhilippe Charnier mail(struct name *to, struct name *cc, struct name *bcc, struct name *smopts,
2426d8484b0SPhilippe Charnier 	char *subject, char *replyto)
2439b50d902SRodney W. Grimes {
2449b50d902SRodney W. Grimes 	struct header head;
2459b50d902SRodney W. Grimes 
2469b50d902SRodney W. Grimes 	head.h_to = to;
2479b50d902SRodney W. Grimes 	head.h_subject = subject;
2489b50d902SRodney W. Grimes 	head.h_cc = cc;
2499b50d902SRodney W. Grimes 	head.h_bcc = bcc;
2509b50d902SRodney W. Grimes 	head.h_smopts = smopts;
25199bd6601SJoerg Wunsch 	head.h_replyto = replyto;
2529ce73e90SMike Heffner 	head.h_inreplyto = NULL;
2539b50d902SRodney W. Grimes 	mail1(&head, 0);
2549b50d902SRodney W. Grimes 	return (0);
2559b50d902SRodney W. Grimes }
2569b50d902SRodney W. Grimes 
2579b50d902SRodney W. Grimes 
2589b50d902SRodney W. Grimes /*
2599b50d902SRodney W. Grimes  * Send mail to a bunch of user names.  The interface is through
2609b50d902SRodney W. Grimes  * the mail routine below.
2619b50d902SRodney W. Grimes  */
2629b50d902SRodney W. Grimes int
sendmail(void * str)263d28a9551SJohn Baldwin sendmail(void *str)
2649b50d902SRodney W. Grimes {
2659b50d902SRodney W. Grimes 	struct header head;
2669b50d902SRodney W. Grimes 
2679b50d902SRodney W. Grimes 	head.h_to = extract(str, GTO);
2689ce73e90SMike Heffner 	head.h_subject = NULL;
2699ce73e90SMike Heffner 	head.h_cc = NULL;
2709ce73e90SMike Heffner 	head.h_bcc = NULL;
2719ce73e90SMike Heffner 	head.h_smopts = NULL;
27259c3f4f7SMike Heffner 	head.h_replyto = value("REPLYTO");
2739ce73e90SMike Heffner 	head.h_inreplyto = NULL;
2749b50d902SRodney W. Grimes 	mail1(&head, 0);
2759b50d902SRodney W. Grimes 	return (0);
2769b50d902SRodney W. Grimes }
2779b50d902SRodney W. Grimes 
2789b50d902SRodney W. Grimes /*
2799b50d902SRodney W. Grimes  * Mail a message on standard input to the people indicated
2809b50d902SRodney W. Grimes  * in the passed header.  (Internal interface).
2819b50d902SRodney W. Grimes  */
2829b50d902SRodney W. Grimes void
mail1(struct header * hp,int printheaders)2836d8484b0SPhilippe Charnier mail1(struct header *hp, int printheaders)
2849b50d902SRodney W. Grimes {
2859b50d902SRodney W. Grimes 	char *cp;
286c92dfc23SMike Heffner 	char *nbuf;
2879b50d902SRodney W. Grimes 	int pid;
2889b50d902SRodney W. Grimes 	char **namelist;
289c92dfc23SMike Heffner 	struct name *to, *nsto;
2909b50d902SRodney W. Grimes 	FILE *mtf;
2919b50d902SRodney W. Grimes 
2929b50d902SRodney W. Grimes 	/*
2939b50d902SRodney W. Grimes 	 * Collect user's mail from standard input.
2949b50d902SRodney W. Grimes 	 * Get the result as mtf.
2959b50d902SRodney W. Grimes 	 */
2969b50d902SRodney W. Grimes 	if ((mtf = collect(hp, printheaders)) == NULL)
2979b50d902SRodney W. Grimes 		return;
2989ce73e90SMike Heffner 	if (value("interactive") != NULL) {
299856f23edSMike Heffner 		if (value("askcc") != NULL || value("askbcc") != NULL) {
3009ce73e90SMike Heffner 			if (value("askcc") != NULL)
3019b50d902SRodney W. Grimes 				grabh(hp, GCC);
302856f23edSMike Heffner 			if (value("askbcc") != NULL)
303856f23edSMike Heffner 				grabh(hp, GBCC);
304856f23edSMike Heffner 		} else {
3059b50d902SRodney W. Grimes 			printf("EOT\n");
3069b50d902SRodney W. Grimes 			(void)fflush(stdout);
3079b50d902SRodney W. Grimes 		}
3080c3a8314SMike Heffner 	}
3090c3a8314SMike Heffner 	if (fsize(mtf) == 0) {
310dcd24e27SMike Heffner 		if (value("dontsendempty") != NULL)
311dcd24e27SMike Heffner 			goto out;
3129ce73e90SMike Heffner 		if (hp->h_subject == NULL)
3139b50d902SRodney W. Grimes 			printf("No message, no subject; hope that's ok\n");
3149b50d902SRodney W. Grimes 		else
3159b50d902SRodney W. Grimes 			printf("Null message body; hope that's ok\n");
3160c3a8314SMike Heffner 	}
3179b50d902SRodney W. Grimes 	/*
3189b50d902SRodney W. Grimes 	 * Now, take the user names from the combined
3199b50d902SRodney W. Grimes 	 * to and cc lists and do all the alias
3209b50d902SRodney W. Grimes 	 * processing.
3219b50d902SRodney W. Grimes 	 */
3229b50d902SRodney W. Grimes 	senderr = 0;
3239b50d902SRodney W. Grimes 	to = usermap(cat(hp->h_bcc, cat(hp->h_to, hp->h_cc)));
3249ce73e90SMike Heffner 	if (to == NULL) {
3259b50d902SRodney W. Grimes 		printf("No recipients specified\n");
3269b50d902SRodney W. Grimes 		senderr++;
3279b50d902SRodney W. Grimes 	}
3289b50d902SRodney W. Grimes 	/*
3299b50d902SRodney W. Grimes 	 * Look through the recipient list for names with /'s
3309b50d902SRodney W. Grimes 	 * in them which we write to as files directly.
3319b50d902SRodney W. Grimes 	 */
3329b50d902SRodney W. Grimes 	to = outof(to, mtf, hp);
3339b50d902SRodney W. Grimes 	if (senderr)
3349b50d902SRodney W. Grimes 		savedeadletter(mtf);
3359b50d902SRodney W. Grimes 	to = elide(to);
3369b50d902SRodney W. Grimes 	if (count(to) == 0)
3379b50d902SRodney W. Grimes 		goto out;
338c92dfc23SMike Heffner 	if (value("recordrecip") != NULL) {
339c92dfc23SMike Heffner 		/*
340c92dfc23SMike Heffner 		 * Before fixing the header, save old To:.
341c92dfc23SMike Heffner 		 * We do this because elide above has sorted To: list, and
342c92dfc23SMike Heffner 		 * we would like to save message in a file named by the first
343c92dfc23SMike Heffner 		 * recipient the user has entered, not the one being the first
344c92dfc23SMike Heffner 		 * after sorting happened.
345c92dfc23SMike Heffner 		 */
346c92dfc23SMike Heffner 		if ((nsto = malloc(sizeof(struct name))) == NULL)
347c92dfc23SMike Heffner 			err(1, "Out of memory");
348c92dfc23SMike Heffner 		bcopy(hp->h_to, nsto, sizeof(struct name));
349c92dfc23SMike Heffner 	}
3509b50d902SRodney W. Grimes 	fixhead(hp, to);
3519b50d902SRodney W. Grimes 	if ((mtf = infix(hp, mtf)) == NULL) {
3529b50d902SRodney W. Grimes 		fprintf(stderr, ". . . message lost, sorry.\n");
3539b50d902SRodney W. Grimes 		return;
3549b50d902SRodney W. Grimes 	}
3559b50d902SRodney W. Grimes 	namelist = unpack(cat(hp->h_smopts, to));
3569b50d902SRodney W. Grimes 	if (debug) {
3579b50d902SRodney W. Grimes 		char **t;
3589b50d902SRodney W. Grimes 
3599b50d902SRodney W. Grimes 		printf("Sendmail arguments:");
3609ce73e90SMike Heffner 		for (t = namelist; *t != NULL; t++)
3619b50d902SRodney W. Grimes 			printf(" \"%s\"", *t);
3629b50d902SRodney W. Grimes 		printf("\n");
3639b50d902SRodney W. Grimes 		goto out;
3649b50d902SRodney W. Grimes 	}
365c92dfc23SMike Heffner 	if (value("recordrecip") != NULL) {
366c92dfc23SMike Heffner 		/*
367c92dfc23SMike Heffner 		 * Extract first recipient username from saved To: and use it
368c92dfc23SMike Heffner 		 * as a filename.
369c92dfc23SMike Heffner 		 */
370c92dfc23SMike Heffner 		if ((nbuf = malloc(strlen(detract(nsto, 0)) + 1)) == NULL)
371c92dfc23SMike Heffner 			err(1, "Out of memory");
372c92dfc23SMike Heffner 		if ((cp = yanklogin(detract(nsto, 0), nbuf)) != NULL)
373c92dfc23SMike Heffner 			(void)savemail(expand(nbuf), mtf);
374c92dfc23SMike Heffner 		free(nbuf);
375c92dfc23SMike Heffner 		free(nsto);
376c92dfc23SMike Heffner 	} else if ((cp = value("record")) != NULL)
3779b50d902SRodney W. Grimes 		(void)savemail(expand(cp), mtf);
3789b50d902SRodney W. Grimes 	/*
3799b50d902SRodney W. Grimes 	 * Fork, set up the temporary mail file as standard
3809b50d902SRodney W. Grimes 	 * input for "mail", and exec with the user list we generated
3819b50d902SRodney W. Grimes 	 * far above.
3829b50d902SRodney W. Grimes 	 */
3839b50d902SRodney W. Grimes 	pid = fork();
3849b50d902SRodney W. Grimes 	if (pid == -1) {
3850c3a8314SMike Heffner 		warn("fork");
3869b50d902SRodney W. Grimes 		savedeadletter(mtf);
3879b50d902SRodney W. Grimes 		goto out;
3889b50d902SRodney W. Grimes 	}
3899b50d902SRodney W. Grimes 	if (pid == 0) {
390856f23edSMike Heffner 		sigset_t nset;
391856f23edSMike Heffner 		(void)sigemptyset(&nset);
392856f23edSMike Heffner 		(void)sigaddset(&nset, SIGHUP);
393856f23edSMike Heffner 		(void)sigaddset(&nset, SIGINT);
394856f23edSMike Heffner 		(void)sigaddset(&nset, SIGQUIT);
395856f23edSMike Heffner 		(void)sigaddset(&nset, SIGTSTP);
396856f23edSMike Heffner 		(void)sigaddset(&nset, SIGTTIN);
397856f23edSMike Heffner 		(void)sigaddset(&nset, SIGTTOU);
398856f23edSMike Heffner 		prepare_child(&nset, fileno(mtf), -1);
3999ce73e90SMike Heffner 		if ((cp = value("sendmail")) != NULL)
4009b50d902SRodney W. Grimes 			cp = expand(cp);
4019b50d902SRodney W. Grimes 		else
4029b50d902SRodney W. Grimes 			cp = _PATH_SENDMAIL;
4039b50d902SRodney W. Grimes 		execv(cp, namelist);
4040c3a8314SMike Heffner 		warn("%s", cp);
4059b50d902SRodney W. Grimes 		_exit(1);
4069b50d902SRodney W. Grimes 	}
4079ce73e90SMike Heffner 	if (value("verbose") != NULL)
4089b50d902SRodney W. Grimes 		(void)wait_child(pid);
4099b50d902SRodney W. Grimes 	else
4109b50d902SRodney W. Grimes 		free_child(pid);
4119b50d902SRodney W. Grimes out:
4129b50d902SRodney W. Grimes 	(void)Fclose(mtf);
4139b50d902SRodney W. Grimes }
4149b50d902SRodney W. Grimes 
4159b50d902SRodney W. Grimes /*
4169b50d902SRodney W. Grimes  * Fix the header by glopping all of the expanded names from
4179b50d902SRodney W. Grimes  * the distribution list into the appropriate fields.
4189b50d902SRodney W. Grimes  */
4199b50d902SRodney W. Grimes void
fixhead(struct header * hp,struct name * tolist)4206d8484b0SPhilippe Charnier fixhead(struct header *hp, struct name *tolist)
4219b50d902SRodney W. Grimes {
4229ce73e90SMike Heffner 	struct name *np;
4239b50d902SRodney W. Grimes 
4249ce73e90SMike Heffner 	hp->h_to = NULL;
4259ce73e90SMike Heffner 	hp->h_cc = NULL;
4269ce73e90SMike Heffner 	hp->h_bcc = NULL;
42750892679SAndrey A. Chernov 	for (np = tolist; np != NULL; np = np->n_flink) {
428856f23edSMike Heffner 		/* Don't copy deleted addresses to the header */
429856f23edSMike Heffner 		if (np->n_type & GDEL)
430856f23edSMike Heffner 			continue;
4319b50d902SRodney W. Grimes 		if ((np->n_type & GMASK) == GTO)
4329b50d902SRodney W. Grimes 			hp->h_to =
4339b50d902SRodney W. Grimes 			    cat(hp->h_to, nalloc(np->n_name, np->n_type));
4349b50d902SRodney W. Grimes 		else if ((np->n_type & GMASK) == GCC)
4359b50d902SRodney W. Grimes 			hp->h_cc =
4369b50d902SRodney W. Grimes 			    cat(hp->h_cc, nalloc(np->n_name, np->n_type));
4379b50d902SRodney W. Grimes 		else if ((np->n_type & GMASK) == GBCC)
4389b50d902SRodney W. Grimes 			hp->h_bcc =
4399b50d902SRodney W. Grimes 			    cat(hp->h_bcc, nalloc(np->n_name, np->n_type));
4409b50d902SRodney W. Grimes 	}
44150892679SAndrey A. Chernov }
4429b50d902SRodney W. Grimes 
4439b50d902SRodney W. Grimes /*
4449b50d902SRodney W. Grimes  * Prepend a header in front of the collected stuff
4459b50d902SRodney W. Grimes  * and return the new file.
4469b50d902SRodney W. Grimes  */
4479b50d902SRodney W. Grimes FILE *
infix(struct header * hp,FILE * fi)4486d8484b0SPhilippe Charnier infix(struct header *hp, FILE *fi)
4499b50d902SRodney W. Grimes {
4509ce73e90SMike Heffner 	FILE *nfo, *nfi;
4519ce73e90SMike Heffner 	int c, fd;
4520c3a8314SMike Heffner 	char tempname[PATHSIZE];
4539b50d902SRodney W. Grimes 
4549ce73e90SMike Heffner 	(void)snprintf(tempname, sizeof(tempname),
4559ce73e90SMike Heffner 	    "%s/mail.RsXXXXXXXXXX", tmpdir);
4560c3a8314SMike Heffner 	if ((fd = mkstemp(tempname)) == -1 ||
4570c3a8314SMike Heffner 	    (nfo = Fdopen(fd, "w")) == NULL) {
4580c3a8314SMike Heffner 		warn("%s", tempname);
4599b50d902SRodney W. Grimes 		return (fi);
4609b50d902SRodney W. Grimes 	}
4610c3a8314SMike Heffner 	if ((nfi = Fopen(tempname, "r")) == NULL) {
4620c3a8314SMike Heffner 		warn("%s", tempname);
4639b50d902SRodney W. Grimes 		(void)Fclose(nfo);
4640c3a8314SMike Heffner 		(void)rm(tempname);
4659b50d902SRodney W. Grimes 		return (fi);
4669b50d902SRodney W. Grimes 	}
4670c3a8314SMike Heffner 	(void)rm(tempname);
46857392071SJoerg Wunsch 	(void)puthead(hp, nfo,
46957392071SJoerg Wunsch 	    GTO|GSUBJECT|GCC|GBCC|GREPLYTO|GINREPLYTO|GNL|GCOMMA);
4709b50d902SRodney W. Grimes 	c = getc(fi);
4719b50d902SRodney W. Grimes 	while (c != EOF) {
4729b50d902SRodney W. Grimes 		(void)putc(c, nfo);
4739b50d902SRodney W. Grimes 		c = getc(fi);
4749b50d902SRodney W. Grimes 	}
4759b50d902SRodney W. Grimes 	if (ferror(fi)) {
4760c3a8314SMike Heffner 		warnx("read");
4779b50d902SRodney W. Grimes 		rewind(fi);
4789b50d902SRodney W. Grimes 		return (fi);
4799b50d902SRodney W. Grimes 	}
4809b50d902SRodney W. Grimes 	(void)fflush(nfo);
4819b50d902SRodney W. Grimes 	if (ferror(nfo)) {
4820c3a8314SMike Heffner 		warn("%s", tempname);
4839b50d902SRodney W. Grimes 		(void)Fclose(nfo);
4849b50d902SRodney W. Grimes 		(void)Fclose(nfi);
4859b50d902SRodney W. Grimes 		rewind(fi);
4869b50d902SRodney W. Grimes 		return (fi);
4879b50d902SRodney W. Grimes 	}
4889b50d902SRodney W. Grimes 	(void)Fclose(nfo);
4899b50d902SRodney W. Grimes 	(void)Fclose(fi);
4909b50d902SRodney W. Grimes 	rewind(nfi);
4919b50d902SRodney W. Grimes 	return (nfi);
4929b50d902SRodney W. Grimes }
4939b50d902SRodney W. Grimes 
4949b50d902SRodney W. Grimes /*
4959b50d902SRodney W. Grimes  * Dump the to, subject, cc header on the
4969b50d902SRodney W. Grimes  * passed file buffer.
4979b50d902SRodney W. Grimes  */
4989b50d902SRodney W. Grimes int
puthead(struct header * hp,FILE * fo,int w)4996d8484b0SPhilippe Charnier puthead(struct header *hp, FILE *fo, int w)
5009b50d902SRodney W. Grimes {
5019ce73e90SMike Heffner 	int gotcha;
5029b50d902SRodney W. Grimes 
5039b50d902SRodney W. Grimes 	gotcha = 0;
5049ce73e90SMike Heffner 	if (hp->h_to != NULL && w & GTO)
5059b50d902SRodney W. Grimes 		fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
5069ce73e90SMike Heffner 	if (hp->h_subject != NULL && w & GSUBJECT)
5079b50d902SRodney W. Grimes 		fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
5089ce73e90SMike Heffner 	if (hp->h_cc != NULL && w & GCC)
5099b50d902SRodney W. Grimes 		fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
5109ce73e90SMike Heffner 	if (hp->h_bcc != NULL && w & GBCC)
5119b50d902SRodney W. Grimes 		fmt("Bcc:", hp->h_bcc, fo, w&GCOMMA), gotcha++;
5129ce73e90SMike Heffner 	if (hp->h_replyto != NULL && w & GREPLYTO)
51399bd6601SJoerg Wunsch 		fprintf(fo, "Reply-To: %s\n", hp->h_replyto), gotcha++;
5149ce73e90SMike Heffner 	if (hp->h_inreplyto != NULL && w & GINREPLYTO)
51599bd6601SJoerg Wunsch 		fprintf(fo, "In-Reply-To: <%s>\n", hp->h_inreplyto), gotcha++;
5169b50d902SRodney W. Grimes 	if (gotcha && w & GNL)
5179b50d902SRodney W. Grimes 		(void)putc('\n', fo);
5189b50d902SRodney W. Grimes 	return (0);
5199b50d902SRodney W. Grimes }
5209b50d902SRodney W. Grimes 
5219b50d902SRodney W. Grimes /*
5229b50d902SRodney W. Grimes  * Format the given header line to not exceed 72 characters.
5239b50d902SRodney W. Grimes  */
5249b50d902SRodney W. Grimes void
fmt(const char * str,struct name * np,FILE * fo,int comma)5256d8484b0SPhilippe Charnier fmt(const char *str, struct name *np, FILE *fo, int comma)
5269b50d902SRodney W. Grimes {
5279ce73e90SMike Heffner 	int col, len;
5289b50d902SRodney W. Grimes 
5299b50d902SRodney W. Grimes 	comma = comma ? 1 : 0;
5309b50d902SRodney W. Grimes 	col = strlen(str);
5319b50d902SRodney W. Grimes 	if (col)
5329b50d902SRodney W. Grimes 		fputs(str, fo);
5339ce73e90SMike Heffner 	for (; np != NULL; np = np->n_flink) {
5349ce73e90SMike Heffner 		if (np->n_flink == NULL)
5359b50d902SRodney W. Grimes 			comma = 0;
5369b50d902SRodney W. Grimes 		len = strlen(np->n_name);
5379b50d902SRodney W. Grimes 		col++;		/* for the space */
5389b50d902SRodney W. Grimes 		if (col + len + comma > 72 && col > 4) {
5399ce73e90SMike Heffner 			fprintf(fo, "\n    ");
5409b50d902SRodney W. Grimes 			col = 4;
5419b50d902SRodney W. Grimes 		} else
5429ce73e90SMike Heffner 			fprintf(fo, " ");
5439b50d902SRodney W. Grimes 		fputs(np->n_name, fo);
5449b50d902SRodney W. Grimes 		if (comma)
5459ce73e90SMike Heffner 			fprintf(fo, ",");
5469b50d902SRodney W. Grimes 		col += len + comma;
5479b50d902SRodney W. Grimes 	}
5489ce73e90SMike Heffner 	fprintf(fo, "\n");
5499b50d902SRodney W. Grimes }
5509b50d902SRodney W. Grimes 
5519b50d902SRodney W. Grimes /*
5529b50d902SRodney W. Grimes  * Save the outgoing mail on the passed file.
5539b50d902SRodney W. Grimes  */
5549b50d902SRodney W. Grimes 
5559b50d902SRodney W. Grimes /*ARGSUSED*/
5569b50d902SRodney W. Grimes int
savemail(char name[],FILE * fi)5576d8484b0SPhilippe Charnier savemail(char name[], FILE *fi)
5589b50d902SRodney W. Grimes {
5599ce73e90SMike Heffner 	FILE *fo;
5609b50d902SRodney W. Grimes 	char buf[BUFSIZ];
5619ce73e90SMike Heffner 	int i;
5629ce73e90SMike Heffner 	time_t now;
563c0d0b766SXin LI 	mode_t saved_umask;
5649b50d902SRodney W. Grimes 
565c0d0b766SXin LI 	saved_umask = umask(077);
566c0d0b766SXin LI 	fo = Fopen(name, "a");
567c0d0b766SXin LI 	umask(saved_umask);
568c0d0b766SXin LI 
569c0d0b766SXin LI 	if (fo == NULL) {
5700c3a8314SMike Heffner 		warn("%s", name);
5719b50d902SRodney W. Grimes 		return (-1);
5729b50d902SRodney W. Grimes 	}
5739b50d902SRodney W. Grimes 	(void)time(&now);
5749b50d902SRodney W. Grimes 	fprintf(fo, "From %s %s", myname, ctime(&now));
5759ce73e90SMike Heffner 	while ((i = fread(buf, 1, sizeof(buf), fi)) > 0)
5769b50d902SRodney W. Grimes 		(void)fwrite(buf, 1, i, fo);
5779ce73e90SMike Heffner 	fprintf(fo, "\n");
5789b50d902SRodney W. Grimes 	(void)fflush(fo);
5799b50d902SRodney W. Grimes 	if (ferror(fo))
5800c3a8314SMike Heffner 		warn("%s", name);
5819b50d902SRodney W. Grimes 	(void)Fclose(fo);
5829b50d902SRodney W. Grimes 	rewind(fi);
5839b50d902SRodney W. Grimes 	return (0);
5849b50d902SRodney W. Grimes }
585