122697Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
333728Sbostic  * Copyright (c) 1988 Regents of the University of California.
433728Sbostic  * All rights reserved.
533728Sbostic  *
642824Sbostic  * %sccs.include.redist.c%
733728Sbostic  */
822697Sdist 
922697Sdist #ifndef lint
10*59745Seric static char sccsid[] = "@(#)collect.c	6.16 (Berkeley) 05/05/93";
1133728Sbostic #endif /* not lint */
1222697Sdist 
131439Seric # include <errno.h>
143309Seric # include "sendmail.h"
151392Seric 
161392Seric /*
172969Seric **  COLLECT -- read & parse message header & make temp file.
181392Seric **
191392Seric **	Creates a temporary file name and copies the standard
209371Seric **	input to that file.  Leading UNIX-style "From" lines are
219371Seric **	stripped off (after important information is extracted).
221392Seric **
231392Seric **	Parameters:
2452106Seric **		smtpmode -- if set, we are running SMTP: give an RFC821
2552105Seric **			style message to say we are ready to collect
2652105Seric **			input, and never ignore a single dot to mean
2752105Seric **			end of message.
2858929Seric **		requeueflag -- this message will be requeued later, so
2958929Seric **			don't do final processing on it.
3058929Seric **		e -- the current envelope.
311392Seric **
321392Seric **	Returns:
334162Seric **		none.
341392Seric **
351392Seric **	Side Effects:
361392Seric **		Temp file is created and filled.
374162Seric **		The from person may be set.
381392Seric */
391392Seric 
4058929Seric collect(smtpmode, requeueflag, e)
4152105Seric 	bool smtpmode;
4258929Seric 	bool requeueflag;
4355012Seric 	register ENVELOPE *e;
441392Seric {
451392Seric 	register FILE *tf;
4652105Seric 	bool ignrdot = smtpmode ? FALSE : IgnrDot;
4757135Seric 	char buf[MAXLINE], buf2[MAXLINE];
4840965Sbostic 	register char *workbuf, *freebuf;
492900Seric 	extern char *hvalue();
5040965Sbostic 	extern bool isheader(), flusheol();
511392Seric 
521392Seric 	/*
531392Seric 	**  Create the temp file name and create the file.
541392Seric 	*/
551392Seric 
5655012Seric 	e->e_df = newstr(queuename(e, 'd'));
57*59745Seric 	if ((tf = dfopen(e->e_df, O_WRONLY|O_CREAT, FileMode)) == NULL)
581392Seric 	{
5955012Seric 		syserr("Cannot create %s", e->e_df);
605366Seric 		NoReturn = TRUE;
615366Seric 		finis();
621392Seric 	}
631392Seric 
644316Seric 	/*
654322Seric 	**  Tell ARPANET to go ahead.
664322Seric 	*/
674322Seric 
6852105Seric 	if (smtpmode)
6958151Seric 		message("354 Enter mail, end with \".\" on a line by itself");
704322Seric 
714322Seric 	/*
724316Seric 	**  Try to read a UNIX-style From line
734316Seric 	*/
744316Seric 
7558112Seric 	if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock) == NULL)
7640965Sbostic 		goto readerr;
774557Seric 	fixcrlf(buf, FALSE);
784321Seric # ifndef NOTUNIX
794322Seric 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
802900Seric 	{
8140965Sbostic 		if (!flusheol(buf, InChannel))
8240965Sbostic 			goto readerr;
8355012Seric 		eatfrom(buf, e);
8458112Seric 		if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock) == NULL)
8540965Sbostic 			goto readerr;
864557Seric 		fixcrlf(buf, FALSE);
872900Seric 	}
8856795Seric # endif /* NOTUNIX */
892900Seric 
901392Seric 	/*
915975Seric 	**  Copy InChannel to temp file & do message editing.
921392Seric 	**	To keep certain mailers from getting confused,
931392Seric 	**	and to keep the output clean, lines that look
9413932Seric 	**	like UNIX "From" lines are deleted in the header.
951392Seric 	*/
961392Seric 
9740965Sbostic 	workbuf = buf;		/* `workbuf' contains a header field */
9840965Sbostic 	freebuf = buf2;		/* `freebuf' can be used for read-ahead */
9940965Sbostic 	for (;;)
1001392Seric 	{
10157135Seric 		char *curbuf;
10257135Seric 		int curbuffree;
10357135Seric 		register int curbuflen;
10457135Seric 		char *p;
10557135Seric 
10640965Sbostic 		/* first, see if the header is over */
10740965Sbostic 		if (!isheader(workbuf))
10840965Sbostic 		{
10940965Sbostic 			fixcrlf(workbuf, TRUE);
11019036Seric 			break;
11140965Sbostic 		}
11219036Seric 
1137681Seric 		/* if the line is too long, throw the rest away */
11440965Sbostic 		if (!flusheol(workbuf, InChannel))
11540965Sbostic 			goto readerr;
1167681Seric 
11740965Sbostic 		/* it's okay to toss '\n' now (flusheol() needed it) */
11840965Sbostic 		fixcrlf(workbuf, TRUE);
1194557Seric 
12057135Seric 		curbuf = workbuf;
12157135Seric 		curbuflen = strlen(curbuf);
12257135Seric 		curbuffree = MAXLINE - curbuflen;
12357135Seric 		p = curbuf + curbuflen;
1242900Seric 
1252900Seric 		/* get the rest of this field */
12640965Sbostic 		for (;;)
1271392Seric 		{
12857135Seric 			int clen;
12957135Seric 
13058112Seric 			if (sfgets(freebuf, MAXLINE, InChannel, TimeOuts.to_datablock) == NULL)
13140965Sbostic 				goto readerr;
13240965Sbostic 
13340965Sbostic 			/* is this a continuation line? */
13440965Sbostic 			if (*freebuf != ' ' && *freebuf != '\t')
1352900Seric 				break;
13640965Sbostic 
13740965Sbostic 			if (!flusheol(freebuf, InChannel))
13840965Sbostic 				goto readerr;
13940965Sbostic 
14057135Seric 			fixcrlf(freebuf, TRUE);
14157135Seric 			clen = strlen(freebuf) + 1;
14257135Seric 
14357135Seric 			/* if insufficient room, dynamically allocate buffer */
14457135Seric 			if (clen >= curbuffree)
14540965Sbostic 			{
14657135Seric 				/* reallocate buffer */
14757135Seric 				int nbuflen = ((p - curbuf) + clen) * 2;
14857135Seric 				char *nbuf = xalloc(nbuflen);
14940965Sbostic 
15057135Seric 				p = nbuf + curbuflen;
15157135Seric 				curbuffree = nbuflen - curbuflen;
15257135Seric 				bcopy(curbuf, nbuf, curbuflen);
15357135Seric 				if (curbuf != buf && curbuf != buf2)
15457135Seric 					free(curbuf);
15557135Seric 				curbuf = nbuf;
15640965Sbostic 			}
15757135Seric 			*p++ = '\n';
15857135Seric 			bcopy(freebuf, p, clen - 1);
15957135Seric 			p += clen - 1;
16057135Seric 			curbuffree -= clen;
16157135Seric 			curbuflen += clen;
1621392Seric 		}
16357135Seric 		*p++ = '\0';
1641392Seric 
16557135Seric 		e->e_msgsize += curbuflen;
1661392Seric 
1672900Seric 		/*
16840965Sbostic 		**  The working buffer now becomes the free buffer, since
16940965Sbostic 		**  the free buffer contains a new header field.
17040965Sbostic 		**
17140965Sbostic 		**  This is premature, since we still havent called
17240965Sbostic 		**  chompheader() to process the field we just created
17340965Sbostic 		**  (so the call to chompheader() will use `freebuf').
17440965Sbostic 		**  This convolution is necessary so that if we break out
17540965Sbostic 		**  of the loop due to H_EOH, `workbuf' will always be
17640965Sbostic 		**  the next unprocessed buffer.
17740965Sbostic 		*/
17840965Sbostic 
17940965Sbostic 		{
18040965Sbostic 			register char *tmp = workbuf;
18140965Sbostic 			workbuf = freebuf;
18240965Sbostic 			freebuf = tmp;
18340965Sbostic 		}
18440965Sbostic 
18540965Sbostic 		/*
1862900Seric 		**  Snarf header away.
1872900Seric 		*/
1882900Seric 
18957135Seric 		if (bitset(H_EOH, chompheader(curbuf, FALSE, e)))
1903058Seric 			break;
19157135Seric 
19257135Seric 		/*
19357135Seric 		**  If the buffer was dynamically allocated, free it.
19457135Seric 		*/
19557135Seric 
19657135Seric 		if (curbuf != buf && curbuf != buf2)
19757135Seric 			free(curbuf);
19840965Sbostic 	}
1991392Seric 
2007673Seric 	if (tTd(30, 1))
2012900Seric 		printf("EOH\n");
2022900Seric 
20340965Sbostic 	if (*workbuf == '\0')
20440965Sbostic 	{
20540965Sbostic 		/* throw away a blank line */
20658112Seric 		if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock) == NULL)
20740965Sbostic 			goto readerr;
20840965Sbostic 	}
20940965Sbostic 	else if (workbuf == buf2)	/* guarantee `buf' contains data */
21040965Sbostic 		(void) strcpy(buf, buf2);
2112900Seric 
2122900Seric 	/*
2132900Seric 	**  Collect the body of the message.
2142900Seric 	*/
2152900Seric 
21615532Seric 	do
2172900Seric 	{
2184551Seric 		register char *bp = buf;
2194156Seric 
2207852Seric 		fixcrlf(buf, TRUE);
2214557Seric 
2222900Seric 		/* check for end-of-message */
22352105Seric 		if (!ignrdot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
2242900Seric 			break;
2252900Seric 
2264551Seric 		/* check for transparent dot */
22752105Seric 		if (OpMode == MD_SMTP && bp[0] == '.' && bp[1] == '.')
2284551Seric 			bp++;
2294551Seric 
2304156Seric 		/*
2314156Seric 		**  Figure message length, output the line to the temp
2324156Seric 		**  file, and insert a newline if missing.
2334156Seric 		*/
2344156Seric 
23555012Seric 		e->e_msgsize += strlen(bp) + 1;
2364551Seric 		fputs(bp, tf);
2377852Seric 		fputs("\n", tf);
2381392Seric 		if (ferror(tf))
23955012Seric 			tferror(tf, e);
24058112Seric 	} while (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock) != NULL);
24140965Sbostic 
24240965Sbostic readerr:
24311544Seric 	if (fflush(tf) != 0)
24455012Seric 		tferror(tf, e);
2454083Seric 	(void) fclose(tf);
2462900Seric 
24711145Seric 	/* An EOF when running SMTP is an error */
24819036Seric 	if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP)
24916136Seric 	{
25058308Seric 		char *host;
25158082Seric 
25258308Seric 		host = RealHostName;
25358308Seric 		if (host == NULL)
25458308Seric 			host = "localhost";
25558308Seric 
25636233Skarels # ifdef LOG
25758308Seric 		if (LogLevel > 0 && feof(InChannel))
25836230Skarels 			syslog(LOG_NOTICE,
25958308Seric 			    "collect: unexpected close on connection from %s, sender=%s: %m\n",
26058308Seric 			    host, e->e_from.q_paddr);
26136233Skarels # endif
26258082Seric 		(feof(InChannel) ? usrerr : syserr)
26358308Seric 			("451 collect: unexpected close on connection from %s, from=%s",
26458308Seric 				host, e->e_from.q_paddr);
26511145Seric 
26616136Seric 		/* don't return an error indication */
26755012Seric 		e->e_to = NULL;
26855012Seric 		e->e_flags &= ~EF_FATALERRS;
26916136Seric 
27016136Seric 		/* and don't try to deliver the partial message either */
27116136Seric 		finis();
27216136Seric 	}
27316136Seric 
2742900Seric 	/*
2752900Seric 	**  Find out some information from the headers.
2763386Seric 	**	Examples are who is the from person & the date.
2772900Seric 	*/
2782900Seric 
27958929Seric 	eatheader(e, !requeueflag);
2807673Seric 
2817782Seric 	/*
2827782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
2837782Seric 	*/
2844622Seric 
28555012Seric 	if (hvalue("to", e) == NULL && hvalue("cc", e) == NULL &&
28655012Seric 	    hvalue("bcc", e) == NULL && hvalue("apparently-to", e) == NULL)
2877367Seric 	{
2887367Seric 		register ADDRESS *q;
2897367Seric 
2907367Seric 		/* create an Apparently-To: field */
2917367Seric 		/*    that or reject the message.... */
29255012Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2937367Seric 		{
2947389Seric 			if (q->q_alias != NULL)
2957389Seric 				continue;
2967673Seric 			if (tTd(30, 3))
2977367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
29859579Seric 			addheader("Apparently-To", q->q_paddr, e);
2997367Seric 		}
3007367Seric 	}
3017367Seric 
30259320Seric 	/* check for message too large */
30359320Seric 	if (MaxMessageSize > 0 && e->e_msgsize > MaxMessageSize)
30459320Seric 	{
30559320Seric 		usrerr("552 Message exceeds maximum fixed size (%ld)",
30659320Seric 			MaxMessageSize);
30759320Seric 	}
30859320Seric 
30955012Seric 	if ((e->e_dfp = fopen(e->e_df, "r")) == NULL)
31058690Seric 	{
31158690Seric 		/* we haven't acked receipt yet, so just chuck this */
31255012Seric 		syserr("Cannot reopen %s", e->e_df);
31358690Seric 		finis();
31458690Seric 	}
3151392Seric }
3161392Seric /*
31740965Sbostic **  FLUSHEOL -- if not at EOL, throw away rest of input line.
31840965Sbostic **
31940965Sbostic **	Parameters:
32040965Sbostic **		buf -- last line read in (checked for '\n'),
32140965Sbostic **		fp -- file to be read from.
32240965Sbostic **
32340965Sbostic **	Returns:
32440965Sbostic **		FALSE on error from sfgets(), TRUE otherwise.
32540965Sbostic **
32640965Sbostic **	Side Effects:
32740965Sbostic **		none.
32840965Sbostic */
32940965Sbostic 
33040965Sbostic bool
33140965Sbostic flusheol(buf, fp)
33240965Sbostic 	char *buf;
33340965Sbostic 	FILE *fp;
33440965Sbostic {
33540965Sbostic 	register char *p = buf;
33657134Seric 	bool printmsg = TRUE;
33757134Seric 	char junkbuf[MAXLINE];
33857134Seric 	extern char *sfgets();
33940965Sbostic 
34057134Seric 	while (strchr(p, '\n') == NULL)
34157134Seric 	{
34257134Seric 		if (printmsg)
34358151Seric 			usrerr("553 header line too long");
34457134Seric 		printmsg = FALSE;
34558112Seric 		if (sfgets(junkbuf, MAXLINE, fp, TimeOuts.to_datablock) == NULL)
34657134Seric 			return (FALSE);
34740965Sbostic 		p = junkbuf;
34840965Sbostic 	}
34940965Sbostic 
35057134Seric 	return (TRUE);
35140965Sbostic }
35240965Sbostic /*
35311544Seric **  TFERROR -- signal error on writing the temporary file.
35411544Seric **
35511544Seric **	Parameters:
35611544Seric **		tf -- the file pointer for the temporary file.
35711544Seric **
35811544Seric **	Returns:
35911544Seric **		none.
36011544Seric **
36111544Seric **	Side Effects:
36211544Seric **		Gives an error message.
36311544Seric **		Arranges for following output to go elsewhere.
36411544Seric */
36511544Seric 
36655012Seric tferror(tf, e)
36711544Seric 	FILE *tf;
36855012Seric 	register ENVELOPE *e;
36911544Seric {
37011544Seric 	if (errno == ENOSPC)
37111544Seric 	{
37255012Seric 		(void) freopen(e->e_df, "w", tf);
37311544Seric 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
37411544Seric 		usrerr("452 Out of disk space for temp file");
37511544Seric 	}
37611544Seric 	else
37755012Seric 		syserr("collect: Cannot write %s", e->e_df);
37811544Seric 	(void) freopen("/dev/null", "w", tf);
37911544Seric }
38011544Seric /*
3812900Seric **  EATFROM -- chew up a UNIX style from line and process
3822900Seric **
3832900Seric **	This does indeed make some assumptions about the format
3842900Seric **	of UNIX messages.
3852900Seric **
3862900Seric **	Parameters:
3872900Seric **		fm -- the from line.
3882900Seric **
3892900Seric **	Returns:
3902900Seric **		none.
3912900Seric **
3922900Seric **	Side Effects:
3932900Seric **		extracts what information it can from the header,
3943386Seric **		such as the date.
3952900Seric */
3962900Seric 
3974321Seric # ifndef NOTUNIX
3984321Seric 
3994203Seric char	*DowList[] =
4004203Seric {
4014203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
4024203Seric };
4034203Seric 
4042900Seric char	*MonthList[] =
4052900Seric {
4062900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4072900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4082900Seric 	NULL
4092900Seric };
4102900Seric 
41155012Seric eatfrom(fm, e)
4122900Seric 	char *fm;
41355012Seric 	register ENVELOPE *e;
4142900Seric {
4152900Seric 	register char *p;
4162900Seric 	register char **dt;
4172900Seric 
4187673Seric 	if (tTd(30, 2))
4194203Seric 		printf("eatfrom(%s)\n", fm);
4204203Seric 
4212900Seric 	/* find the date part */
4222900Seric 	p = fm;
4232900Seric 	while (*p != '\0')
4242900Seric 	{
4252900Seric 		/* skip a word */
4262900Seric 		while (*p != '\0' && *p != ' ')
42716896Seric 			p++;
4282900Seric 		while (*p == ' ')
42916896Seric 			p++;
43058050Seric 		if (!(isascii(*p) && isupper(*p)) ||
43158050Seric 		    p[3] != ' ' || p[13] != ':' || p[16] != ':')
4322900Seric 			continue;
4332900Seric 
4342900Seric 		/* we have a possible date */
4354203Seric 		for (dt = DowList; *dt != NULL; dt++)
4362900Seric 			if (strncmp(*dt, p, 3) == 0)
4372900Seric 				break;
4384203Seric 		if (*dt == NULL)
4394203Seric 			continue;
4402900Seric 
4414203Seric 		for (dt = MonthList; *dt != NULL; dt++)
4424203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
4434203Seric 				break;
4442900Seric 		if (*dt != NULL)
4452900Seric 			break;
4462900Seric 	}
4472900Seric 
4482900Seric 	if (*p != NULL)
4492900Seric 	{
4503386Seric 		char *q;
4515366Seric 		extern char *arpadate();
4523386Seric 
4532900Seric 		/* we have found a date */
4543386Seric 		q = xalloc(25);
45523103Seric 		(void) strncpy(q, p, 25);
4563386Seric 		q[24] = '\0';
4575366Seric 		q = arpadate(q);
45855012Seric 		define('a', newstr(q), e);
4592900Seric 	}
4602900Seric }
4614321Seric 
46256795Seric # endif /* NOTUNIX */
463