xref: /onnv-gate/usr/src/cmd/mailx/send.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*13093SRoger.Faulkner@Oracle.COM  * Common Development and Distribution License (the "License").
6*13093SRoger.Faulkner@Oracle.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 1985, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
2618Srobbin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2718Srobbin /*	  All Rights Reserved  	*/
2818Srobbin 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include "rcv.h"
400Sstevel@tonic-gate #include <locale.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * mailx -- a modified version of a University of California at Berkeley
440Sstevel@tonic-gate  *	mail program
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * Mail to others.
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static void		fmt(register char *str, register FILE *fo);
500Sstevel@tonic-gate static FILE		*infix(struct header *hp, FILE *fi);
510Sstevel@tonic-gate static void		statusput(register struct message *mp, register FILE *obuf, int doign, int (*fp)(const char *, FILE *));
520Sstevel@tonic-gate static int		savemail(char name[], struct header *hp, FILE *fi);
530Sstevel@tonic-gate static int		sendmail(char *str);
540Sstevel@tonic-gate static int		Sendmail(char *str);
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static off_t textpos;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * Send message described by the passed pointer to the
600Sstevel@tonic-gate  * passed output buffer.  Return -1 on error, but normally
610Sstevel@tonic-gate  * the number of lines written.  Adjust the status: field
620Sstevel@tonic-gate  * if need be.  If doign is set, suppress ignored header fields.
630Sstevel@tonic-gate  * Call (*fp)(line, obuf) to print the line.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate long
msend(struct message * mailp,FILE * obuf,int flag,int (* fp)(const char *,FILE *))660Sstevel@tonic-gate msend(
670Sstevel@tonic-gate 	struct message *mailp,
680Sstevel@tonic-gate 	FILE *obuf,
690Sstevel@tonic-gate 	int flag,
700Sstevel@tonic-gate 	int (*fp)(const char *, FILE *))
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	register struct message *mp;
730Sstevel@tonic-gate 	long clen, n, c;
740Sstevel@tonic-gate 	FILE *ibuf;
750Sstevel@tonic-gate 	char line[LINESIZE], field[BUFSIZ];
760Sstevel@tonic-gate 	int ishead, infld, fline, dostat, doclen, nread, unused;
770Sstevel@tonic-gate 	char *cp, *cp2;
780Sstevel@tonic-gate 	int doign = flag & M_IGNORE;
790Sstevel@tonic-gate 	int oldign = 0;	/* previous line was ignored */
800Sstevel@tonic-gate 	long lc;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	mp = mailp;
830Sstevel@tonic-gate 	if (mp->m_clen == 0)
840Sstevel@tonic-gate 		setclen(mp);
850Sstevel@tonic-gate 	ibuf = setinput(mp);
860Sstevel@tonic-gate 	c = mp->m_size;
870Sstevel@tonic-gate 	ishead = 1;
880Sstevel@tonic-gate 	dostat = 1;
890Sstevel@tonic-gate 	doclen = 1;
900Sstevel@tonic-gate 	infld = 0;
910Sstevel@tonic-gate 	fline = 1;
920Sstevel@tonic-gate 	lc = 0;
930Sstevel@tonic-gate 	clearerr(obuf);
940Sstevel@tonic-gate 	while (c > 0L) {
95*13093SRoger.Faulkner@Oracle.COM 		nread = getaline(line, LINESIZE, ibuf, &unused);
960Sstevel@tonic-gate 		c -= nread;
970Sstevel@tonic-gate 		lc++;
980Sstevel@tonic-gate 		if (ishead) {
990Sstevel@tonic-gate 			/*
1000Sstevel@tonic-gate 			 * First line is the From line, so no headers
1010Sstevel@tonic-gate 			 * there to worry about
1020Sstevel@tonic-gate 			 */
1030Sstevel@tonic-gate 			if (fline) {
1040Sstevel@tonic-gate 				fline = 0;
1050Sstevel@tonic-gate 				goto writeit;
1060Sstevel@tonic-gate 			}
1070Sstevel@tonic-gate 			/*
1080Sstevel@tonic-gate 			 * If line is blank, we've reached end of
1090Sstevel@tonic-gate 			 * headers, so force out status: field
1100Sstevel@tonic-gate 			 * and note that we are no longer in header
1110Sstevel@tonic-gate 			 * fields.  Also force out Content-Length: field.
1120Sstevel@tonic-gate 			 */
1130Sstevel@tonic-gate 			if (line[0] == '\n') {
1140Sstevel@tonic-gate 				if (dostat) {
1150Sstevel@tonic-gate 					statusput(mailp, obuf, doign, fp);
1160Sstevel@tonic-gate 					dostat = 0;
1170Sstevel@tonic-gate 				}
1180Sstevel@tonic-gate 				if (doclen &&
1190Sstevel@tonic-gate 				    !isign("content-length", flag&M_SAVING)) {
1200Sstevel@tonic-gate 					snprintf(field, sizeof (field),
1210Sstevel@tonic-gate 						"Content-Length: %ld\n",
1220Sstevel@tonic-gate 						mp->m_clen - 1);
1230Sstevel@tonic-gate 					(*fp)(field, obuf);
1240Sstevel@tonic-gate 					if (ferror(obuf))
1250Sstevel@tonic-gate 						return(-1);
1260Sstevel@tonic-gate 					doclen = 0;
1270Sstevel@tonic-gate 				}
1280Sstevel@tonic-gate 				ishead = 0;
1290Sstevel@tonic-gate 				goto writeit;
1300Sstevel@tonic-gate 			}
1310Sstevel@tonic-gate 			/*
1320Sstevel@tonic-gate 			 * If this line is a continuation
1330Sstevel@tonic-gate 			 * of a previous header field, just echo it.
1340Sstevel@tonic-gate 			 */
1350Sstevel@tonic-gate 			if (isspace(line[0]) && infld)
1360Sstevel@tonic-gate 				if (oldign)
1370Sstevel@tonic-gate 					continue;
1380Sstevel@tonic-gate 				else
1390Sstevel@tonic-gate 					goto writeit;
1400Sstevel@tonic-gate 			infld = 0;
1410Sstevel@tonic-gate 			/*
1420Sstevel@tonic-gate 			 * If we are no longer looking at real
1430Sstevel@tonic-gate 			 * header lines, force out status:
1440Sstevel@tonic-gate 			 * This happens in uucp style mail where
1450Sstevel@tonic-gate 			 * there are no headers at all.
1460Sstevel@tonic-gate 			 */
1470Sstevel@tonic-gate 			if (!headerp(line)) {
1480Sstevel@tonic-gate 				if (dostat) {
1490Sstevel@tonic-gate 					statusput(mailp, obuf, doign, fp);
1500Sstevel@tonic-gate 					dostat = 0;
1510Sstevel@tonic-gate 				}
1520Sstevel@tonic-gate 				(*fp)("\n", obuf);
1530Sstevel@tonic-gate 				ishead = 0;
1540Sstevel@tonic-gate 				goto writeit;
1550Sstevel@tonic-gate 			}
1560Sstevel@tonic-gate 			infld++;
1570Sstevel@tonic-gate 			/*
1580Sstevel@tonic-gate 			 * Pick up the header field.
1590Sstevel@tonic-gate 			 * If it is an ignored field and
1600Sstevel@tonic-gate 			 * we care about such things, skip it.
1610Sstevel@tonic-gate 			 */
1620Sstevel@tonic-gate 			cp = line;
1630Sstevel@tonic-gate 			cp2 = field;
1640Sstevel@tonic-gate 			while (*cp && *cp != ':' && !isspace(*cp))
1650Sstevel@tonic-gate 				*cp2++ = *cp++;
1660Sstevel@tonic-gate 			*cp2 = 0;
1670Sstevel@tonic-gate 			oldign = doign && isign(field, flag&M_SAVING);
1680Sstevel@tonic-gate 			if (oldign)
1690Sstevel@tonic-gate 				continue;
1700Sstevel@tonic-gate 			/*
1710Sstevel@tonic-gate 			 * If the field is "status," go compute and print the
1720Sstevel@tonic-gate 			 * real Status: field
1730Sstevel@tonic-gate 			 */
1740Sstevel@tonic-gate 			if (icequal(field, "status")) {
1750Sstevel@tonic-gate 				if (dostat) {
1760Sstevel@tonic-gate 					statusput(mailp, obuf, doign, fp);
1770Sstevel@tonic-gate 					dostat = 0;
1780Sstevel@tonic-gate 				}
1790Sstevel@tonic-gate 				continue;
1800Sstevel@tonic-gate 			}
1810Sstevel@tonic-gate 			if (icequal(field, "content-length")) {
1820Sstevel@tonic-gate 				if (doclen) {
1830Sstevel@tonic-gate 					snprintf(line, sizeof (line),
1840Sstevel@tonic-gate 						"Content-Length: %ld\n",
1850Sstevel@tonic-gate 						mp->m_clen - 1);
1860Sstevel@tonic-gate 					(*fp)(line, obuf);
1870Sstevel@tonic-gate 					if (ferror(obuf))
1880Sstevel@tonic-gate 						return(-1);
1890Sstevel@tonic-gate 					doclen = 0;
1900Sstevel@tonic-gate 				}
1910Sstevel@tonic-gate 				continue;
1920Sstevel@tonic-gate 			}
1930Sstevel@tonic-gate 		}
1940Sstevel@tonic-gate writeit:
1950Sstevel@tonic-gate 		if (!ishead && !mp->m_text && mp->m_clen != 0) {
1960Sstevel@tonic-gate 			if (line[0] == '\n')
1970Sstevel@tonic-gate 				putc('\n', obuf);
1980Sstevel@tonic-gate 			clen = mp->m_clen-1;
1990Sstevel@tonic-gate 			for (;;) {
2000Sstevel@tonic-gate 				n = clen < sizeof line ? clen : sizeof line;
2010Sstevel@tonic-gate 				if ((n = fread(line, 1, (int)n, ibuf)) <= 0) {
2020Sstevel@tonic-gate 					fprintf(stderr, gettext(
2030Sstevel@tonic-gate 					    "\t(Unexpected end-of-file).\n"));
2040Sstevel@tonic-gate 					clen = 0;
2050Sstevel@tonic-gate 				} else {
2060Sstevel@tonic-gate 					if (fwrite(line, 1, (int)n, obuf) != n) {
2070Sstevel@tonic-gate 						fprintf(stderr, gettext(
2080Sstevel@tonic-gate 					"\tError writing to the new file.\n"));
2090Sstevel@tonic-gate 						fflush(obuf);
2100Sstevel@tonic-gate 						if (fferror(obuf))
2110Sstevel@tonic-gate 							return (-1);
2120Sstevel@tonic-gate 					}
2130Sstevel@tonic-gate 				}
2140Sstevel@tonic-gate 				clen -= n;
2150Sstevel@tonic-gate 				if (clen <= 0) {
2160Sstevel@tonic-gate 					break;
2170Sstevel@tonic-gate 				}
2180Sstevel@tonic-gate 			}
2190Sstevel@tonic-gate 			c = 0L;
2200Sstevel@tonic-gate 		} else {
2210Sstevel@tonic-gate 			(*fp)(line, obuf);
2220Sstevel@tonic-gate 			if (ferror(obuf))
2230Sstevel@tonic-gate 				return(-1);
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 	fflush(obuf);
2270Sstevel@tonic-gate 	if (ferror(obuf))
2280Sstevel@tonic-gate 		return(-1);
2290Sstevel@tonic-gate 	if (ishead && (mailp->m_flag & MSTATUS))
2300Sstevel@tonic-gate 		printf(gettext("failed to fix up status field\n"));
2310Sstevel@tonic-gate 	return(lc);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate  * Test if the passed line is a header line, RFC 822 style.
2360Sstevel@tonic-gate  */
2370Sstevel@tonic-gate int
headerp(register char * line)2380Sstevel@tonic-gate headerp(register char *line)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 	register char *cp = line;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	while (*cp && *cp != ' ' && *cp != '\t' && *cp != ':')
2430Sstevel@tonic-gate 		cp++;
2440Sstevel@tonic-gate 	return(*cp == ':');
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate  * Output a reasonable looking status field.
2490Sstevel@tonic-gate  * But if "status" is ignored and doign, forget it.
2500Sstevel@tonic-gate  */
2510Sstevel@tonic-gate static void
statusput(register struct message * mp,register FILE * obuf,int doign,int (* fp)(const char *,FILE *))2520Sstevel@tonic-gate statusput(
2530Sstevel@tonic-gate 	register struct message *mp,
2540Sstevel@tonic-gate 	register FILE *obuf,
2550Sstevel@tonic-gate 	int doign,
2560Sstevel@tonic-gate 	int (*fp)(const char *, FILE *))
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	char statout[12];
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	if (doign && isign("status", 0))
2610Sstevel@tonic-gate 		return;
2620Sstevel@tonic-gate 	if ((mp->m_flag & (MNEW|MREAD)) == MNEW)
2630Sstevel@tonic-gate 		return;
2640Sstevel@tonic-gate 	strcpy(statout, "Status: ");
2650Sstevel@tonic-gate 	if (mp->m_flag & MREAD)
2660Sstevel@tonic-gate 		strcat(statout, "R");
2670Sstevel@tonic-gate 	if ((mp->m_flag & MNEW) == 0)
2680Sstevel@tonic-gate 		strcat(statout, "O");
2690Sstevel@tonic-gate 	strcat(statout, "\n");
2700Sstevel@tonic-gate 	(*fp)(statout, obuf);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate  * Interface between the argument list and the mail1 routine
2750Sstevel@tonic-gate  * which does all the dirty work.
2760Sstevel@tonic-gate  */
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate int
mail(char ** people)2790Sstevel@tonic-gate mail(char **people)
2800Sstevel@tonic-gate {
2810Sstevel@tonic-gate 	register char *cp2, *cp3;
2820Sstevel@tonic-gate 	register int s;
2830Sstevel@tonic-gate 	char *buf, **ap;
2840Sstevel@tonic-gate 	struct header head;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	for (s = 0, ap = people; *ap; ap++)
2870Sstevel@tonic-gate 		s += strlen(*ap) + 2;
2880Sstevel@tonic-gate 	buf = (char *)salloc((unsigned)(s+1));
2890Sstevel@tonic-gate 	cp2 = buf;
2900Sstevel@tonic-gate 	for (ap = people; *ap; ap++) {
2910Sstevel@tonic-gate 		for (cp3 = *ap; *cp3; ) {
2920Sstevel@tonic-gate 			if (*cp3 == ' ' || *cp3 == '\t') {
2930Sstevel@tonic-gate 				*cp3++ = ',';
2940Sstevel@tonic-gate 				while (*cp3 == ' ' || *cp3 == '\t')
2950Sstevel@tonic-gate 					cp3++;
2960Sstevel@tonic-gate 			} else
2970Sstevel@tonic-gate 				cp3++;
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 		cp2 = copy(*ap, cp2);
3000Sstevel@tonic-gate 		*cp2++ = ',';
3010Sstevel@tonic-gate 		*cp2++ = ' ';
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 	*cp2 = '\0';
3040Sstevel@tonic-gate 	head.h_to = buf;
3050Sstevel@tonic-gate 	head.h_subject = head.h_cc = head.h_bcc = head.h_defopt = NOSTR;
3060Sstevel@tonic-gate 	head.h_others = NOSTRPTR;
3070Sstevel@tonic-gate 	head.h_seq = 0;
3080Sstevel@tonic-gate 	mail1(&head, Fflag, NOSTR);
3090Sstevel@tonic-gate 	return(0);
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate int
sendm(char * str)3130Sstevel@tonic-gate sendm(char *str)
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate 	if (value("flipm") != NOSTR)
3160Sstevel@tonic-gate 		return(Sendmail(str));
3170Sstevel@tonic-gate 	else return(sendmail(str));
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate int
Sendm(char * str)3210Sstevel@tonic-gate Sendm(char *str)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	if (value("flipm") != NOSTR)
3240Sstevel@tonic-gate 		return(sendmail(str));
3250Sstevel@tonic-gate 	else return(Sendmail(str));
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate /*
3290Sstevel@tonic-gate  * Interface to the mail1 routine for the -t flag
3300Sstevel@tonic-gate  * (read headers from text).
3310Sstevel@tonic-gate  */
3320Sstevel@tonic-gate int
tmail(void)3330Sstevel@tonic-gate tmail(void)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	struct header head;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	head.h_to = NOSTR;
3380Sstevel@tonic-gate 	head.h_subject = head.h_cc = head.h_bcc = head.h_defopt = NOSTR;
3390Sstevel@tonic-gate 	head.h_others = NOSTRPTR;
3400Sstevel@tonic-gate 	head.h_seq = 0;
3410Sstevel@tonic-gate 	mail1(&head, Fflag, NOSTR);
3420Sstevel@tonic-gate 	return(0);
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate /*
3460Sstevel@tonic-gate  * Send mail to a bunch of user names.  The interface is through
3470Sstevel@tonic-gate  * the mail routine below.
3480Sstevel@tonic-gate  */
3490Sstevel@tonic-gate static int
sendmail(char * str)3500Sstevel@tonic-gate sendmail(char *str)
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate 	struct header head;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	if (blankline(str))
3550Sstevel@tonic-gate 		head.h_to = NOSTR;
3560Sstevel@tonic-gate 	else
3570Sstevel@tonic-gate 		head.h_to = addto(NOSTR, str);
3580Sstevel@tonic-gate 	head.h_subject = head.h_cc = head.h_bcc = head.h_defopt = NOSTR;
3590Sstevel@tonic-gate 	head.h_others = NOSTRPTR;
3600Sstevel@tonic-gate 	head.h_seq = 0;
3610Sstevel@tonic-gate 	mail1(&head, 0, NOSTR);
3620Sstevel@tonic-gate 	return(0);
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate  * Send mail to a bunch of user names.  The interface is through
3670Sstevel@tonic-gate  * the mail routine below.
3680Sstevel@tonic-gate  * save a copy of the letter
3690Sstevel@tonic-gate  */
3700Sstevel@tonic-gate static int
Sendmail(char * str)3710Sstevel@tonic-gate Sendmail(char *str)
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate 	struct header head;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	if (blankline(str))
3760Sstevel@tonic-gate 		head.h_to = NOSTR;
3770Sstevel@tonic-gate 	else
3780Sstevel@tonic-gate 		head.h_to = addto(NOSTR, str);
3790Sstevel@tonic-gate 	head.h_subject = head.h_cc = head.h_bcc = head.h_defopt = NOSTR;
3800Sstevel@tonic-gate 	head.h_others = NOSTRPTR;
3810Sstevel@tonic-gate 	head.h_seq = 0;
3820Sstevel@tonic-gate 	mail1(&head, 1, NOSTR);
3830Sstevel@tonic-gate 	return(0);
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate /*
3870Sstevel@tonic-gate  * Walk the list of fds, closing all but one.
3880Sstevel@tonic-gate  */
3890Sstevel@tonic-gate static int
closefd_walk(void * special_fd,int fd)3900Sstevel@tonic-gate closefd_walk(void *special_fd, int fd)
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate 	if (fd > STDERR_FILENO && fd != *(int *)special_fd)
3930Sstevel@tonic-gate 		(void) close(fd);
3940Sstevel@tonic-gate 	return (0);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate  * Mail a message on standard input to the people indicated
3990Sstevel@tonic-gate  * in the passed header.  (Internal interface).
4000Sstevel@tonic-gate  */
4010Sstevel@tonic-gate void
mail1(struct header * hp,int use_to,char * orig_to)4020Sstevel@tonic-gate mail1(struct header *hp, int use_to, char *orig_to)
4030Sstevel@tonic-gate {
4040Sstevel@tonic-gate 	pid_t p, pid;
4050Sstevel@tonic-gate 	int i, s, gotcha;
4060Sstevel@tonic-gate 	char **namelist, *deliver;
4070Sstevel@tonic-gate 	struct name *to, *np;
4080Sstevel@tonic-gate 	FILE *mtf, *fp;
4090Sstevel@tonic-gate 	int remote = rflag != NOSTR || rmail;
4100Sstevel@tonic-gate 	char **t;
4110Sstevel@tonic-gate 	char *deadletter;
4120Sstevel@tonic-gate 	char recfile[PATHSIZE];
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	/*
4150Sstevel@tonic-gate 	 * Collect user's mail from standard input.
4160Sstevel@tonic-gate 	 * Get the result as mtf.
4170Sstevel@tonic-gate 	 */
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	pid = (pid_t)-1;
4200Sstevel@tonic-gate 	if ((mtf = collect(hp)) == NULL)
4210Sstevel@tonic-gate 		return;
4220Sstevel@tonic-gate 	hp->h_seq = 1;
4230Sstevel@tonic-gate 	if (hp->h_subject == NOSTR)
4240Sstevel@tonic-gate 		hp->h_subject = sflag;
4250Sstevel@tonic-gate 	if (fsize(mtf) == 0 && hp->h_subject == NOSTR) {
4260Sstevel@tonic-gate 		printf(gettext("No message !?!\n"));
4270Sstevel@tonic-gate 		goto out;
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 	if (intty) {
4300Sstevel@tonic-gate 		printf(gettext("EOT\n"));
4310Sstevel@tonic-gate 		flush();
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	/*
4350Sstevel@tonic-gate 	 * If we need to use the To: line to determine the record
4360Sstevel@tonic-gate 	 * file, save a copy of it before it's sorted below.
4370Sstevel@tonic-gate 	 */
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	if (use_to && orig_to == NOSTR && hp->h_to != NOSTR)
4400Sstevel@tonic-gate 		orig_to = strcpy((char *)salloc(strlen(hp->h_to)+1), hp->h_to);
4410Sstevel@tonic-gate 	else if (orig_to == NOSTR)
4420Sstevel@tonic-gate 		orig_to = "";
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	/*
4450Sstevel@tonic-gate 	 * Now, take the user names from the combined
4460Sstevel@tonic-gate 	 * to and cc lists and do all the alias
4470Sstevel@tonic-gate 	 * processing.
4480Sstevel@tonic-gate 	 */
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	senderr = 0;
4510Sstevel@tonic-gate 	to = cat(extract(hp->h_bcc, GBCC),
4520Sstevel@tonic-gate 	     cat(extract(hp->h_to, GTO),
4530Sstevel@tonic-gate 	     extract(hp->h_cc, GCC)));
4540Sstevel@tonic-gate 	to = translate(outpre(elide(usermap(to))));
4550Sstevel@tonic-gate 	if (!senderr)
4560Sstevel@tonic-gate 		mapf(to, myname);
4570Sstevel@tonic-gate 	mechk(to);
4580Sstevel@tonic-gate 	for (gotcha = 0, np = to; np != NIL; np = np->n_flink)
4590Sstevel@tonic-gate 		if ((np->n_type & GDEL) == 0)
4600Sstevel@tonic-gate 			gotcha++;
4610Sstevel@tonic-gate 	hp->h_to = detract(to, GTO);
4620Sstevel@tonic-gate 	hp->h_cc = detract(to, GCC);
4630Sstevel@tonic-gate 	hp->h_bcc = detract(to, GBCC);
4640Sstevel@tonic-gate 	if ((mtf = infix(hp, mtf)) == NULL) {
4650Sstevel@tonic-gate 		fprintf(stderr, gettext(". . . message lost, sorry.\n"));
4660Sstevel@tonic-gate 		return;
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 	rewind(mtf);
4690Sstevel@tonic-gate 	if (askme && isatty(0)) {
4700Sstevel@tonic-gate 		char ans[64];
4710Sstevel@tonic-gate 		puthead(hp, stdout, GTO|GCC|GBCC, 0);
4720Sstevel@tonic-gate 		printf(gettext("Send? "));
4730Sstevel@tonic-gate 		printf("[yes] ");
4740Sstevel@tonic-gate 		if (fgets(ans, sizeof(ans), stdin) && ans[0] &&
4750Sstevel@tonic-gate 				(tolower(ans[0]) != 'y' && ans[0] != '\n'))
4760Sstevel@tonic-gate 			goto dead;
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 	if (senderr)
4790Sstevel@tonic-gate 		goto dead;
4800Sstevel@tonic-gate 	/*
4810Sstevel@tonic-gate 	 * Look through the recipient list for names with /'s
4820Sstevel@tonic-gate 	 * in them which we write to as files directly.
4830Sstevel@tonic-gate 	 */
4840Sstevel@tonic-gate 	i = outof(to, mtf);
4850Sstevel@tonic-gate 	rewind(mtf);
4860Sstevel@tonic-gate 	if (!gotcha && !i) {
4870Sstevel@tonic-gate 		printf(gettext("No recipients specified\n"));
4880Sstevel@tonic-gate 		goto dead;
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 	if (senderr)
4910Sstevel@tonic-gate 		goto dead;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	getrecf(orig_to, recfile, use_to, sizeof (recfile));
4940Sstevel@tonic-gate 	if (recfile != NOSTR && *recfile)
4950Sstevel@tonic-gate 		savemail(safeexpand(recfile), hp, mtf);
4960Sstevel@tonic-gate 	if (!gotcha)
4970Sstevel@tonic-gate 		goto out;
4980Sstevel@tonic-gate 	namelist = unpack(to);
4990Sstevel@tonic-gate 	if (debug) {
5000Sstevel@tonic-gate 		fprintf(stderr, "Recipients of message:\n");
5010Sstevel@tonic-gate 		for (t = namelist; *t != NOSTR; t++)
5020Sstevel@tonic-gate 			fprintf(stderr, " \"%s\"", *t);
5030Sstevel@tonic-gate 		fprintf(stderr, "\n");
5040Sstevel@tonic-gate 		return;
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	/*
5080Sstevel@tonic-gate 	 * Wait, to absorb a potential zombie, then
5090Sstevel@tonic-gate 	 * fork, set up the temporary mail file as standard
5100Sstevel@tonic-gate 	 * input for "mail" and exec with the user list we generated
5110Sstevel@tonic-gate 	 * far above. Return the process id to caller in case he
5120Sstevel@tonic-gate 	 * wants to await the completion of mail.
5130Sstevel@tonic-gate 	 */
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate #ifdef VMUNIX
5160Sstevel@tonic-gate 	while (wait3((int *)0, WNOHANG, (struct rusage *)0) > 0)
5170Sstevel@tonic-gate 		;
5180Sstevel@tonic-gate #else
5190Sstevel@tonic-gate #ifdef preSVr4
5200Sstevel@tonic-gate 	wait((int *)0);
5210Sstevel@tonic-gate #else
5220Sstevel@tonic-gate 	while (waitpid((pid_t)-1, (int *)0, WNOHANG) > 0)
5230Sstevel@tonic-gate 		;
5240Sstevel@tonic-gate #endif
5250Sstevel@tonic-gate #endif
5260Sstevel@tonic-gate 	rewind(mtf);
5270Sstevel@tonic-gate 	pid = fork();
5280Sstevel@tonic-gate 	if (pid == (pid_t)-1) {
5290Sstevel@tonic-gate 		perror("fork");
5300Sstevel@tonic-gate dead:
5310Sstevel@tonic-gate 		deadletter = Getf("DEAD");
5320Sstevel@tonic-gate 		if (fp = fopen(deadletter,
5330Sstevel@tonic-gate 		    value("appenddeadletter") == NOSTR ? "w" : "a")) {
5340Sstevel@tonic-gate 			chmod(deadletter, DEADPERM);
5350Sstevel@tonic-gate 			puthead(hp, fp, GMASK|GCLEN, fsize(mtf) - textpos);
5360Sstevel@tonic-gate 			fseek(mtf, textpos, 0);
5370Sstevel@tonic-gate 			lcwrite(deadletter, mtf, fp,
5380Sstevel@tonic-gate 			    value("appenddeadletter") != NOSTR);
5390Sstevel@tonic-gate 			fclose(fp);
5400Sstevel@tonic-gate 		} else
5410Sstevel@tonic-gate 			perror(deadletter);
5420Sstevel@tonic-gate 		goto out;
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 	if (pid == 0) {
5450Sstevel@tonic-gate 		sigchild();
5460Sstevel@tonic-gate #ifdef SIGTSTP
5470Sstevel@tonic-gate 		if (remote == 0) {
5480Sstevel@tonic-gate 			sigset(SIGTSTP, SIG_IGN);
5490Sstevel@tonic-gate 			sigset(SIGTTIN, SIG_IGN);
5500Sstevel@tonic-gate 			sigset(SIGTTOU, SIG_IGN);
5510Sstevel@tonic-gate 		}
5520Sstevel@tonic-gate #endif
5530Sstevel@tonic-gate 		sigset(SIGHUP, SIG_IGN);
5540Sstevel@tonic-gate 		sigset(SIGINT, SIG_IGN);
5550Sstevel@tonic-gate 		sigset(SIGQUIT, SIG_IGN);
5560Sstevel@tonic-gate 		s = fileno(mtf);
5570Sstevel@tonic-gate 		(void) fdwalk(closefd_walk, &s);
5580Sstevel@tonic-gate 		close(0);
5590Sstevel@tonic-gate 		dup(s);
5600Sstevel@tonic-gate 		close(s);
5610Sstevel@tonic-gate #ifdef CC
5620Sstevel@tonic-gate 		submit(getpid());
5630Sstevel@tonic-gate #endif /* CC */
5640Sstevel@tonic-gate 		if ((deliver = value("sendmail")) == NOSTR)
5650Sstevel@tonic-gate #ifdef SENDMAIL
5660Sstevel@tonic-gate 			deliver = SENDMAIL;
5670Sstevel@tonic-gate #else
5680Sstevel@tonic-gate 			deliver = MAIL;
5690Sstevel@tonic-gate #endif
5700Sstevel@tonic-gate 		execvp(safeexpand(deliver), namelist);
5710Sstevel@tonic-gate 		perror(deliver);
5720Sstevel@tonic-gate 		exit(1);
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	if (value("sendwait")!=NOSTR)
5760Sstevel@tonic-gate 		remote++;
5770Sstevel@tonic-gate out:
5780Sstevel@tonic-gate 	if (remote) {
5790Sstevel@tonic-gate 		while ((p = wait(&s)) != pid && p != (pid_t)-1)
5800Sstevel@tonic-gate 			;
5810Sstevel@tonic-gate 		if (s != 0)
5820Sstevel@tonic-gate 			senderr++;
5830Sstevel@tonic-gate 		pid = 0;
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 	fclose(mtf);
5860Sstevel@tonic-gate 	return;
5870Sstevel@tonic-gate }
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate /*
5900Sstevel@tonic-gate  * Prepend a header in front of the collected stuff
5910Sstevel@tonic-gate  * and return the new file.
5920Sstevel@tonic-gate  */
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate static FILE *
infix(struct header * hp,FILE * fi)5950Sstevel@tonic-gate infix(struct header *hp, FILE *fi)
5960Sstevel@tonic-gate {
5970Sstevel@tonic-gate 	register FILE *nfo, *nfi;
5980Sstevel@tonic-gate 	register int c;
5990Sstevel@tonic-gate 	char *postmark, *returnaddr;
6000Sstevel@tonic-gate 	int fd = -1;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	rewind(fi);
6030Sstevel@tonic-gate 	if ((fd = open(tempMail, O_RDWR|O_CREAT|O_EXCL, 0600)) < 0 ||
6040Sstevel@tonic-gate 	(nfo = fdopen(fd, "w")) == NULL) {
6050Sstevel@tonic-gate 		perror(tempMail);
6060Sstevel@tonic-gate 		return(fi);
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 	if ((nfi = fopen(tempMail, "r")) == NULL) {
6090Sstevel@tonic-gate 		perror(tempMail);
6100Sstevel@tonic-gate 		fclose(nfo);
6110Sstevel@tonic-gate 		return(fi);
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 	removefile(tempMail);
6140Sstevel@tonic-gate 	postmark = value("postmark");
6150Sstevel@tonic-gate 	returnaddr = value("returnaddr");
6160Sstevel@tonic-gate 	if ((postmark != NOSTR) || (returnaddr != NOSTR)) {
6170Sstevel@tonic-gate 		if (returnaddr && *returnaddr)
6180Sstevel@tonic-gate 			fprintf(nfo, "From: %s", returnaddr);
6190Sstevel@tonic-gate 		else
6200Sstevel@tonic-gate 			fprintf(nfo, "From: %s@%s", myname, host);
6210Sstevel@tonic-gate 		if (postmark && *postmark)
6220Sstevel@tonic-gate 			fprintf(nfo, " (%s)", postmark);
6230Sstevel@tonic-gate 		putc('\n', nfo);
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate 	puthead(hp, nfo, (GMASK & ~GBCC) | GCLEN, fsize(fi));
6260Sstevel@tonic-gate 	textpos = ftell(nfo);
6270Sstevel@tonic-gate 	while ((c = getc(fi)) != EOF)
6280Sstevel@tonic-gate 		putc(c, nfo);
6290Sstevel@tonic-gate 	if (ferror(fi)) {
6300Sstevel@tonic-gate 		perror("read");
6310Sstevel@tonic-gate 		return(fi);
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 	fflush(nfo);
6340Sstevel@tonic-gate 	if (fferror(nfo)) {
6350Sstevel@tonic-gate 		perror(tempMail);
6360Sstevel@tonic-gate 		fclose(nfo);
6370Sstevel@tonic-gate 		fclose(nfi);
6380Sstevel@tonic-gate 		return(fi);
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 	fclose(nfo);
6410Sstevel@tonic-gate 	fclose(fi);
6420Sstevel@tonic-gate 	rewind(nfi);
6430Sstevel@tonic-gate 	return(nfi);
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate /*
6470Sstevel@tonic-gate  * Dump the message header on the
6480Sstevel@tonic-gate  * passed file buffer.
6490Sstevel@tonic-gate  */
6500Sstevel@tonic-gate 
65118Srobbin int
puthead(struct header * hp,FILE * fo,int w,long clen)6520Sstevel@tonic-gate puthead(struct header *hp, FILE *fo, int w, long clen)
6530Sstevel@tonic-gate {
6540Sstevel@tonic-gate 	register int gotcha;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	gotcha = 0;
6570Sstevel@tonic-gate 	if (hp->h_to != NOSTR && (w & GTO))
6580Sstevel@tonic-gate 		fprintf(fo, "To: "), fmt(hp->h_to, fo), gotcha++;
6590Sstevel@tonic-gate 	if ((w & GSUBJECT) && (int)value("bsdcompat"))
6600Sstevel@tonic-gate 		if (hp->h_subject != NOSTR && *hp->h_subject)
6610Sstevel@tonic-gate 			fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
6620Sstevel@tonic-gate 		else
6630Sstevel@tonic-gate 			if (sflag && *sflag)
6640Sstevel@tonic-gate 				fprintf(fo, "Subject: %s\n", sflag), gotcha++;
6650Sstevel@tonic-gate 	if (hp->h_cc != NOSTR && (w & GCC))
6660Sstevel@tonic-gate 		fprintf(fo, "Cc: "), fmt(hp->h_cc, fo), gotcha++;
6670Sstevel@tonic-gate 	if (hp->h_bcc != NOSTR && (w & GBCC))
6680Sstevel@tonic-gate 		fprintf(fo, "Bcc: "), fmt(hp->h_bcc, fo), gotcha++;
6690Sstevel@tonic-gate 	if (hp->h_defopt != NOSTR && (w & GDEFOPT))
6700Sstevel@tonic-gate 		if (receipt_flg)
6710Sstevel@tonic-gate 			fprintf(fo, "Return-Receipt-To: %s\n",
6720Sstevel@tonic-gate 				hp->h_defopt), gotcha++;
6730Sstevel@tonic-gate 		else
6740Sstevel@tonic-gate 		fprintf(fo, "Default-Options: %s\n", hp->h_defopt), gotcha++;
6750Sstevel@tonic-gate 	if ((w & GSUBJECT) && !(int)value("bsdcompat"))
6760Sstevel@tonic-gate 		if (hp->h_subject != NOSTR && *hp->h_subject)
6770Sstevel@tonic-gate 			fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
6780Sstevel@tonic-gate 		else
6790Sstevel@tonic-gate 			if (sflag && *sflag)
6800Sstevel@tonic-gate 				fprintf(fo, "Subject: %s\n", sflag), gotcha++;
6810Sstevel@tonic-gate 	if (hp->h_others != NOSTRPTR && (w & GOTHER)) {
6820Sstevel@tonic-gate 		char **p;
6830Sstevel@tonic-gate 		for (p = hp->h_others; *p; p++)
6840Sstevel@tonic-gate 			fprintf(fo, "%s\n", *p);
6850Sstevel@tonic-gate 		gotcha++;
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate #ifndef preSVr4
6880Sstevel@tonic-gate 	if (w & GCLEN)
6890Sstevel@tonic-gate 		fprintf(fo, "Content-Length: %ld\n", clen), gotcha++;
6900Sstevel@tonic-gate #endif
6910Sstevel@tonic-gate 	if (gotcha && (w & GNL))
6920Sstevel@tonic-gate 		putc('\n', fo);
6930Sstevel@tonic-gate 	return(0);
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate /*
6970Sstevel@tonic-gate  * Format the given text to not exceed 78 characters.
6980Sstevel@tonic-gate  */
6990Sstevel@tonic-gate static void
fmt(register char * str,register FILE * fo)7000Sstevel@tonic-gate fmt(register char *str, register FILE *fo)
7010Sstevel@tonic-gate {
7020Sstevel@tonic-gate 	register int col = 4;
7030Sstevel@tonic-gate 	char name[256];
7040Sstevel@tonic-gate 	int len;
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	str = strcpy((char *)salloc(strlen(str)+1), str);
7070Sstevel@tonic-gate 	while (str = yankword(str, name, sizeof (name), 1)) {
7080Sstevel@tonic-gate 		len = strlen(name);
7090Sstevel@tonic-gate 		if (col > 4) {
7100Sstevel@tonic-gate 			if (col + len > 76) {
7110Sstevel@tonic-gate 				fputs(",\n    ", fo);
7120Sstevel@tonic-gate 				col = 4;
7130Sstevel@tonic-gate 			} else {
7140Sstevel@tonic-gate 				fputs(", ", fo);
7150Sstevel@tonic-gate 				col += 2;
7160Sstevel@tonic-gate 			}
7170Sstevel@tonic-gate 		}
7180Sstevel@tonic-gate 		fputs(name, fo);
7190Sstevel@tonic-gate 		col += len;
7200Sstevel@tonic-gate 	}
7210Sstevel@tonic-gate 	putc('\n', fo);
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate /*
7250Sstevel@tonic-gate  * Save the outgoing mail on the passed file.
7260Sstevel@tonic-gate  */
7270Sstevel@tonic-gate static int
savemail(char name[],struct header * hp,FILE * fi)7280Sstevel@tonic-gate savemail(char name[], struct header *hp, FILE *fi)
7290Sstevel@tonic-gate {
7300Sstevel@tonic-gate 	register FILE *fo;
7310Sstevel@tonic-gate 	time_t now;
7320Sstevel@tonic-gate 	char *n;
7330Sstevel@tonic-gate #ifdef preSVr4
7340Sstevel@tonic-gate 	char line[BUFSIZ];
7350Sstevel@tonic-gate #else
7360Sstevel@tonic-gate 	int c;
7370Sstevel@tonic-gate #endif
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	if (debug)
7400Sstevel@tonic-gate 		fprintf(stderr, gettext("save in '%s'\n"), name);
7410Sstevel@tonic-gate 	if ((fo = fopen(name, "a")) == NULL) {
7420Sstevel@tonic-gate 		perror(name);
7430Sstevel@tonic-gate 		return(-1);
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 	time(&now);
7460Sstevel@tonic-gate 	n = rflag;
7470Sstevel@tonic-gate 	if (n == NOSTR)
7480Sstevel@tonic-gate 		n = myname;
7490Sstevel@tonic-gate 	fprintf(fo, "From %s %s", n, ctime(&now));
7500Sstevel@tonic-gate 	puthead(hp, fo, GMASK|GCLEN, fsize(fi) - textpos);
7510Sstevel@tonic-gate 	fseek(fi, textpos, 0);
7520Sstevel@tonic-gate #ifdef preSVr4
7530Sstevel@tonic-gate 	while (fgets(line, sizeof line, fi)) {
7540Sstevel@tonic-gate 		if (!strncmp(line, "From ", 5))
7550Sstevel@tonic-gate 			putc('>', fo);
7560Sstevel@tonic-gate 		fputs(line, fo);
7570Sstevel@tonic-gate 	}
7580Sstevel@tonic-gate #else
7590Sstevel@tonic-gate 	while ((c = getc(fi)) != EOF)
7600Sstevel@tonic-gate 		putc(c, fo);
7610Sstevel@tonic-gate #endif
7620Sstevel@tonic-gate 	putc('\n', fo);
7630Sstevel@tonic-gate 	fflush(fo);
7640Sstevel@tonic-gate 	if (fferror(fo))
7650Sstevel@tonic-gate 		perror(name);
7660Sstevel@tonic-gate 	fclose(fo);
7670Sstevel@tonic-gate 	return(0);
7680Sstevel@tonic-gate }
769