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*58151Seric static char sccsid[] = "@(#)collect.c	6.8 (Berkeley) 02/23/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.
281392Seric **
291392Seric **	Returns:
304162Seric **		none.
311392Seric **
321392Seric **	Side Effects:
331392Seric **		Temp file is created and filled.
344162Seric **		The from person may be set.
351392Seric */
361392Seric 
3755012Seric collect(smtpmode, e)
3852105Seric 	bool smtpmode;
3955012Seric 	register ENVELOPE *e;
401392Seric {
411392Seric 	register FILE *tf;
4252105Seric 	bool ignrdot = smtpmode ? FALSE : IgnrDot;
4357135Seric 	char buf[MAXLINE], buf2[MAXLINE];
4440965Sbostic 	register char *workbuf, *freebuf;
452900Seric 	extern char *hvalue();
4640965Sbostic 	extern bool isheader(), flusheol();
471392Seric 
481392Seric 	/*
491392Seric 	**  Create the temp file name and create the file.
501392Seric 	*/
511392Seric 
5255012Seric 	e->e_df = newstr(queuename(e, 'd'));
5355012Seric 	if ((tf = dfopen(e->e_df, "w")) == NULL)
541392Seric 	{
5555012Seric 		syserr("Cannot create %s", e->e_df);
565366Seric 		NoReturn = TRUE;
575366Seric 		finis();
581392Seric 	}
5955012Seric 	(void) chmod(e->e_df, FileMode);
601392Seric 
614316Seric 	/*
624322Seric 	**  Tell ARPANET to go ahead.
634322Seric 	*/
644322Seric 
6552105Seric 	if (smtpmode)
66*58151Seric 		message("354 Enter mail, end with \".\" on a line by itself");
674322Seric 
684322Seric 	/*
694316Seric 	**  Try to read a UNIX-style From line
704316Seric 	*/
714316Seric 
7258112Seric 	if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock) == NULL)
7340965Sbostic 		goto readerr;
744557Seric 	fixcrlf(buf, FALSE);
754321Seric # ifndef NOTUNIX
764322Seric 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
772900Seric 	{
7840965Sbostic 		if (!flusheol(buf, InChannel))
7940965Sbostic 			goto readerr;
8055012Seric 		eatfrom(buf, e);
8158112Seric 		if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock) == NULL)
8240965Sbostic 			goto readerr;
834557Seric 		fixcrlf(buf, FALSE);
842900Seric 	}
8556795Seric # endif /* NOTUNIX */
862900Seric 
871392Seric 	/*
885975Seric 	**  Copy InChannel to temp file & do message editing.
891392Seric 	**	To keep certain mailers from getting confused,
901392Seric 	**	and to keep the output clean, lines that look
9113932Seric 	**	like UNIX "From" lines are deleted in the header.
921392Seric 	*/
931392Seric 
9440965Sbostic 	workbuf = buf;		/* `workbuf' contains a header field */
9540965Sbostic 	freebuf = buf2;		/* `freebuf' can be used for read-ahead */
9640965Sbostic 	for (;;)
971392Seric 	{
9857135Seric 		char *curbuf;
9957135Seric 		int curbuffree;
10057135Seric 		register int curbuflen;
10157135Seric 		char *p;
10257135Seric 
10340965Sbostic 		/* first, see if the header is over */
10440965Sbostic 		if (!isheader(workbuf))
10540965Sbostic 		{
10640965Sbostic 			fixcrlf(workbuf, TRUE);
10719036Seric 			break;
10840965Sbostic 		}
10919036Seric 
1107681Seric 		/* if the line is too long, throw the rest away */
11140965Sbostic 		if (!flusheol(workbuf, InChannel))
11240965Sbostic 			goto readerr;
1137681Seric 
11440965Sbostic 		/* it's okay to toss '\n' now (flusheol() needed it) */
11540965Sbostic 		fixcrlf(workbuf, TRUE);
1164557Seric 
11757135Seric 		curbuf = workbuf;
11857135Seric 		curbuflen = strlen(curbuf);
11957135Seric 		curbuffree = MAXLINE - curbuflen;
12057135Seric 		p = curbuf + curbuflen;
1212900Seric 
1222900Seric 		/* get the rest of this field */
12340965Sbostic 		for (;;)
1241392Seric 		{
12557135Seric 			int clen;
12657135Seric 
12758112Seric 			if (sfgets(freebuf, MAXLINE, InChannel, TimeOuts.to_datablock) == NULL)
12840965Sbostic 				goto readerr;
12940965Sbostic 
13040965Sbostic 			/* is this a continuation line? */
13140965Sbostic 			if (*freebuf != ' ' && *freebuf != '\t')
1322900Seric 				break;
13340965Sbostic 
13440965Sbostic 			if (!flusheol(freebuf, InChannel))
13540965Sbostic 				goto readerr;
13640965Sbostic 
13757135Seric 			fixcrlf(freebuf, TRUE);
13857135Seric 			clen = strlen(freebuf) + 1;
13957135Seric 
14057135Seric 			/* if insufficient room, dynamically allocate buffer */
14157135Seric 			if (clen >= curbuffree)
14240965Sbostic 			{
14357135Seric 				/* reallocate buffer */
14457135Seric 				int nbuflen = ((p - curbuf) + clen) * 2;
14557135Seric 				char *nbuf = xalloc(nbuflen);
14640965Sbostic 
14757135Seric 				p = nbuf + curbuflen;
14857135Seric 				curbuffree = nbuflen - curbuflen;
14957135Seric 				bcopy(curbuf, nbuf, curbuflen);
15057135Seric 				if (curbuf != buf && curbuf != buf2)
15157135Seric 					free(curbuf);
15257135Seric 				curbuf = nbuf;
15340965Sbostic 			}
15457135Seric 			*p++ = '\n';
15557135Seric 			bcopy(freebuf, p, clen - 1);
15657135Seric 			p += clen - 1;
15757135Seric 			curbuffree -= clen;
15857135Seric 			curbuflen += clen;
1591392Seric 		}
16057135Seric 		*p++ = '\0';
1611392Seric 
16257135Seric 		e->e_msgsize += curbuflen;
1631392Seric 
1642900Seric 		/*
16540965Sbostic 		**  The working buffer now becomes the free buffer, since
16640965Sbostic 		**  the free buffer contains a new header field.
16740965Sbostic 		**
16840965Sbostic 		**  This is premature, since we still havent called
16940965Sbostic 		**  chompheader() to process the field we just created
17040965Sbostic 		**  (so the call to chompheader() will use `freebuf').
17140965Sbostic 		**  This convolution is necessary so that if we break out
17240965Sbostic 		**  of the loop due to H_EOH, `workbuf' will always be
17340965Sbostic 		**  the next unprocessed buffer.
17440965Sbostic 		*/
17540965Sbostic 
17640965Sbostic 		{
17740965Sbostic 			register char *tmp = workbuf;
17840965Sbostic 			workbuf = freebuf;
17940965Sbostic 			freebuf = tmp;
18040965Sbostic 		}
18140965Sbostic 
18240965Sbostic 		/*
1832900Seric 		**  Snarf header away.
1842900Seric 		*/
1852900Seric 
18657135Seric 		if (bitset(H_EOH, chompheader(curbuf, FALSE, e)))
1873058Seric 			break;
18857135Seric 
18957135Seric 		/*
19057135Seric 		**  If the buffer was dynamically allocated, free it.
19157135Seric 		*/
19257135Seric 
19357135Seric 		if (curbuf != buf && curbuf != buf2)
19457135Seric 			free(curbuf);
19540965Sbostic 	}
1961392Seric 
1977673Seric 	if (tTd(30, 1))
1982900Seric 		printf("EOH\n");
1992900Seric 
20040965Sbostic 	if (*workbuf == '\0')
20140965Sbostic 	{
20240965Sbostic 		/* throw away a blank line */
20358112Seric 		if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock) == NULL)
20440965Sbostic 			goto readerr;
20540965Sbostic 	}
20640965Sbostic 	else if (workbuf == buf2)	/* guarantee `buf' contains data */
20740965Sbostic 		(void) strcpy(buf, buf2);
2082900Seric 
2092900Seric 	/*
2102900Seric 	**  Collect the body of the message.
2112900Seric 	*/
2122900Seric 
21315532Seric 	do
2142900Seric 	{
2154551Seric 		register char *bp = buf;
2164156Seric 
2177852Seric 		fixcrlf(buf, TRUE);
2184557Seric 
2192900Seric 		/* check for end-of-message */
22052105Seric 		if (!ignrdot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
2212900Seric 			break;
2222900Seric 
2234551Seric 		/* check for transparent dot */
22452105Seric 		if (OpMode == MD_SMTP && bp[0] == '.' && bp[1] == '.')
2254551Seric 			bp++;
2264551Seric 
2274156Seric 		/*
2284156Seric 		**  Figure message length, output the line to the temp
2294156Seric 		**  file, and insert a newline if missing.
2304156Seric 		*/
2314156Seric 
23255012Seric 		e->e_msgsize += strlen(bp) + 1;
2334551Seric 		fputs(bp, tf);
2347852Seric 		fputs("\n", tf);
2351392Seric 		if (ferror(tf))
23655012Seric 			tferror(tf, e);
23758112Seric 	} while (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock) != NULL);
23840965Sbostic 
23940965Sbostic readerr:
24011544Seric 	if (fflush(tf) != 0)
24155012Seric 		tferror(tf, e);
2424083Seric 	(void) fclose(tf);
2432900Seric 
24411145Seric 	/* An EOF when running SMTP is an error */
24519036Seric 	if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP)
24616136Seric 	{
24740965Sbostic 		int usrerr(), syserr();
24858082Seric 
24936233Skarels # ifdef LOG
25058082Seric 		if (RealHostName != NULL && LogLevel > 0 && feof(InChannel))
25136230Skarels 			syslog(LOG_NOTICE,
25236230Skarels 			    "collect: unexpected close on connection from %s: %m\n",
25355012Seric 			    e->e_from.q_paddr, RealHostName);
25436233Skarels # endif
25558082Seric 		(feof(InChannel) ? usrerr : syserr)
256*58151Seric 			("451 collect: unexpected close on connection, from=%s",
25758082Seric 				e->e_from.q_paddr);
25811145Seric 
25916136Seric 		/* don't return an error indication */
26055012Seric 		e->e_to = NULL;
26155012Seric 		e->e_flags &= ~EF_FATALERRS;
26216136Seric 
26316136Seric 		/* and don't try to deliver the partial message either */
26416136Seric 		finis();
26516136Seric 	}
26616136Seric 
2672900Seric 	/*
2682900Seric 	**  Find out some information from the headers.
2693386Seric 	**	Examples are who is the from person & the date.
2702900Seric 	*/
2712900Seric 
27257642Seric 	eatheader(e, QueueRun);
2737673Seric 
2747782Seric 	/*
2757782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
2767782Seric 	*/
2774622Seric 
27855012Seric 	if (hvalue("to", e) == NULL && hvalue("cc", e) == NULL &&
27955012Seric 	    hvalue("bcc", e) == NULL && hvalue("apparently-to", e) == NULL)
2807367Seric 	{
2817367Seric 		register ADDRESS *q;
2827367Seric 
2837367Seric 		/* create an Apparently-To: field */
2847367Seric 		/*    that or reject the message.... */
28555012Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2867367Seric 		{
2877389Seric 			if (q->q_alias != NULL)
2887389Seric 				continue;
2897673Seric 			if (tTd(30, 3))
2907367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
29155012Seric 			addheader("apparently-to", q->q_paddr, e);
2927367Seric 		}
2937367Seric 	}
2947367Seric 
29555012Seric 	if ((e->e_dfp = fopen(e->e_df, "r")) == NULL)
29655012Seric 		syserr("Cannot reopen %s", e->e_df);
2971392Seric }
2981392Seric /*
29940965Sbostic **  FLUSHEOL -- if not at EOL, throw away rest of input line.
30040965Sbostic **
30140965Sbostic **	Parameters:
30240965Sbostic **		buf -- last line read in (checked for '\n'),
30340965Sbostic **		fp -- file to be read from.
30440965Sbostic **
30540965Sbostic **	Returns:
30640965Sbostic **		FALSE on error from sfgets(), TRUE otherwise.
30740965Sbostic **
30840965Sbostic **	Side Effects:
30940965Sbostic **		none.
31040965Sbostic */
31140965Sbostic 
31240965Sbostic bool
31340965Sbostic flusheol(buf, fp)
31440965Sbostic 	char *buf;
31540965Sbostic 	FILE *fp;
31640965Sbostic {
31740965Sbostic 	register char *p = buf;
31857134Seric 	bool printmsg = TRUE;
31957134Seric 	char junkbuf[MAXLINE];
32057134Seric 	extern char *sfgets();
32140965Sbostic 
32257134Seric 	while (strchr(p, '\n') == NULL)
32357134Seric 	{
32457134Seric 		if (printmsg)
325*58151Seric 			usrerr("553 header line too long");
32657134Seric 		printmsg = FALSE;
32758112Seric 		if (sfgets(junkbuf, MAXLINE, fp, TimeOuts.to_datablock) == NULL)
32857134Seric 			return (FALSE);
32940965Sbostic 		p = junkbuf;
33040965Sbostic 	}
33140965Sbostic 
33257134Seric 	return (TRUE);
33340965Sbostic }
33440965Sbostic /*
33511544Seric **  TFERROR -- signal error on writing the temporary file.
33611544Seric **
33711544Seric **	Parameters:
33811544Seric **		tf -- the file pointer for the temporary file.
33911544Seric **
34011544Seric **	Returns:
34111544Seric **		none.
34211544Seric **
34311544Seric **	Side Effects:
34411544Seric **		Gives an error message.
34511544Seric **		Arranges for following output to go elsewhere.
34611544Seric */
34711544Seric 
34855012Seric tferror(tf, e)
34911544Seric 	FILE *tf;
35055012Seric 	register ENVELOPE *e;
35111544Seric {
35211544Seric 	if (errno == ENOSPC)
35311544Seric 	{
35455012Seric 		(void) freopen(e->e_df, "w", tf);
35511544Seric 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
35611544Seric 		usrerr("452 Out of disk space for temp file");
35711544Seric 	}
35811544Seric 	else
35955012Seric 		syserr("collect: Cannot write %s", e->e_df);
36011544Seric 	(void) freopen("/dev/null", "w", tf);
36111544Seric }
36211544Seric /*
3632900Seric **  EATFROM -- chew up a UNIX style from line and process
3642900Seric **
3652900Seric **	This does indeed make some assumptions about the format
3662900Seric **	of UNIX messages.
3672900Seric **
3682900Seric **	Parameters:
3692900Seric **		fm -- the from line.
3702900Seric **
3712900Seric **	Returns:
3722900Seric **		none.
3732900Seric **
3742900Seric **	Side Effects:
3752900Seric **		extracts what information it can from the header,
3763386Seric **		such as the date.
3772900Seric */
3782900Seric 
3794321Seric # ifndef NOTUNIX
3804321Seric 
3814203Seric char	*DowList[] =
3824203Seric {
3834203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
3844203Seric };
3854203Seric 
3862900Seric char	*MonthList[] =
3872900Seric {
3882900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3892900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3902900Seric 	NULL
3912900Seric };
3922900Seric 
39355012Seric eatfrom(fm, e)
3942900Seric 	char *fm;
39555012Seric 	register ENVELOPE *e;
3962900Seric {
3972900Seric 	register char *p;
3982900Seric 	register char **dt;
3992900Seric 
4007673Seric 	if (tTd(30, 2))
4014203Seric 		printf("eatfrom(%s)\n", fm);
4024203Seric 
4032900Seric 	/* find the date part */
4042900Seric 	p = fm;
4052900Seric 	while (*p != '\0')
4062900Seric 	{
4072900Seric 		/* skip a word */
4082900Seric 		while (*p != '\0' && *p != ' ')
40916896Seric 			p++;
4102900Seric 		while (*p == ' ')
41116896Seric 			p++;
41258050Seric 		if (!(isascii(*p) && isupper(*p)) ||
41358050Seric 		    p[3] != ' ' || p[13] != ':' || p[16] != ':')
4142900Seric 			continue;
4152900Seric 
4162900Seric 		/* we have a possible date */
4174203Seric 		for (dt = DowList; *dt != NULL; dt++)
4182900Seric 			if (strncmp(*dt, p, 3) == 0)
4192900Seric 				break;
4204203Seric 		if (*dt == NULL)
4214203Seric 			continue;
4222900Seric 
4234203Seric 		for (dt = MonthList; *dt != NULL; dt++)
4244203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
4254203Seric 				break;
4262900Seric 		if (*dt != NULL)
4272900Seric 			break;
4282900Seric 	}
4292900Seric 
4302900Seric 	if (*p != NULL)
4312900Seric 	{
4323386Seric 		char *q;
4335366Seric 		extern char *arpadate();
4343386Seric 
4352900Seric 		/* we have found a date */
4363386Seric 		q = xalloc(25);
43723103Seric 		(void) strncpy(q, p, 25);
4383386Seric 		q[24] = '\0';
4395366Seric 		q = arpadate(q);
44055012Seric 		define('a', newstr(q), e);
4412900Seric 	}
4422900Seric }
4434321Seric 
44456795Seric # endif /* NOTUNIX */
445