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*55012Seric static char sccsid[] = "@(#)collect.c	5.12 (Berkeley) 07/12/92";
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 
37*55012Seric collect(smtpmode, e)
3852105Seric 	bool smtpmode;
39*55012Seric 	register ENVELOPE *e;
401392Seric {
411392Seric 	register FILE *tf;
4252105Seric 	bool ignrdot = smtpmode ? FALSE : IgnrDot;
4340965Sbostic 	char buf[MAXFIELD], buf2[MAXFIELD];
4440965Sbostic 	register char *workbuf, *freebuf;
4540965Sbostic 	register int workbuflen;
462900Seric 	extern char *hvalue();
4740965Sbostic 	extern bool isheader(), flusheol();
481392Seric 
491392Seric 	/*
501392Seric 	**  Create the temp file name and create the file.
511392Seric 	*/
521392Seric 
53*55012Seric 	e->e_df = newstr(queuename(e, 'd'));
54*55012Seric 	if ((tf = dfopen(e->e_df, "w")) == NULL)
551392Seric 	{
56*55012Seric 		syserr("Cannot create %s", e->e_df);
575366Seric 		NoReturn = TRUE;
585366Seric 		finis();
591392Seric 	}
60*55012Seric 	(void) chmod(e->e_df, FileMode);
611392Seric 
624316Seric 	/*
634322Seric 	**  Tell ARPANET to go ahead.
644322Seric 	*/
654322Seric 
6652105Seric 	if (smtpmode)
674710Seric 		message("354", "Enter mail, end with \".\" on a line by itself");
684322Seric 
694322Seric 	/*
704316Seric 	**  Try to read a UNIX-style From line
714316Seric 	*/
724316Seric 
7340965Sbostic 	if (sfgets(buf, MAXFIELD, InChannel) == NULL)
7440965Sbostic 		goto readerr;
754557Seric 	fixcrlf(buf, FALSE);
764321Seric # ifndef NOTUNIX
774322Seric 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
782900Seric 	{
7940965Sbostic 		if (!flusheol(buf, InChannel))
8040965Sbostic 			goto readerr;
81*55012Seric 		eatfrom(buf, e);
8240965Sbostic 		if (sfgets(buf, MAXFIELD, InChannel) == NULL)
8340965Sbostic 			goto readerr;
844557Seric 		fixcrlf(buf, FALSE);
852900Seric 	}
864321Seric # endif NOTUNIX
872900Seric 
881392Seric 	/*
895975Seric 	**  Copy InChannel to temp file & do message editing.
901392Seric 	**	To keep certain mailers from getting confused,
911392Seric 	**	and to keep the output clean, lines that look
9213932Seric 	**	like UNIX "From" lines are deleted in the header.
931392Seric 	*/
941392Seric 
9540965Sbostic 	workbuf = buf;		/* `workbuf' contains a header field */
9640965Sbostic 	freebuf = buf2;		/* `freebuf' can be used for read-ahead */
9740965Sbostic 	for (;;)
981392Seric 	{
9940965Sbostic 		/* first, see if the header is over */
10040965Sbostic 		if (!isheader(workbuf))
10140965Sbostic 		{
10240965Sbostic 			fixcrlf(workbuf, TRUE);
10319036Seric 			break;
10440965Sbostic 		}
10519036Seric 
1067681Seric 		/* if the line is too long, throw the rest away */
10740965Sbostic 		if (!flusheol(workbuf, InChannel))
10840965Sbostic 			goto readerr;
1097681Seric 
11040965Sbostic 		/* it's okay to toss '\n' now (flusheol() needed it) */
11140965Sbostic 		fixcrlf(workbuf, TRUE);
1124557Seric 
11340965Sbostic 		workbuflen = strlen(workbuf);
1142900Seric 
1152900Seric 		/* get the rest of this field */
11640965Sbostic 		for (;;)
1171392Seric 		{
11840965Sbostic 			if (sfgets(freebuf, MAXFIELD, InChannel) == NULL)
11940965Sbostic 				goto readerr;
12040965Sbostic 
12140965Sbostic 			/* is this a continuation line? */
12240965Sbostic 			if (*freebuf != ' ' && *freebuf != '\t')
1232900Seric 				break;
12440965Sbostic 
12540965Sbostic 			if (!flusheol(freebuf, InChannel))
12640965Sbostic 				goto readerr;
12740965Sbostic 
12840965Sbostic 			/* yes; append line to `workbuf' if there's room */
12940965Sbostic 			if (workbuflen < MAXFIELD-3)
13040965Sbostic 			{
13140965Sbostic 				register char *p = workbuf + workbuflen;
13240965Sbostic 				register char *q = freebuf;
13340965Sbostic 
13440965Sbostic 				/* we have room for more of this field */
13540965Sbostic 				fixcrlf(freebuf, TRUE);
13640965Sbostic 				*p++ = '\n'; workbuflen++;
13740965Sbostic 				while(*q != '\0' && workbuflen < MAXFIELD-1)
13840965Sbostic 				{
13940965Sbostic 					*p++ = *q++;
14040965Sbostic 					workbuflen++;
14140965Sbostic 				}
14240965Sbostic 				*p = '\0';
14340965Sbostic 			}
1441392Seric 		}
1451392Seric 
146*55012Seric 		e->e_msgsize += workbuflen;
1471392Seric 
1482900Seric 		/*
14940965Sbostic 		**  The working buffer now becomes the free buffer, since
15040965Sbostic 		**  the free buffer contains a new header field.
15140965Sbostic 		**
15240965Sbostic 		**  This is premature, since we still havent called
15340965Sbostic 		**  chompheader() to process the field we just created
15440965Sbostic 		**  (so the call to chompheader() will use `freebuf').
15540965Sbostic 		**  This convolution is necessary so that if we break out
15640965Sbostic 		**  of the loop due to H_EOH, `workbuf' will always be
15740965Sbostic 		**  the next unprocessed buffer.
15840965Sbostic 		*/
15940965Sbostic 
16040965Sbostic 		{
16140965Sbostic 			register char *tmp = workbuf;
16240965Sbostic 			workbuf = freebuf;
16340965Sbostic 			freebuf = tmp;
16440965Sbostic 		}
16540965Sbostic 
16640965Sbostic 		/*
1672900Seric 		**  Snarf header away.
1682900Seric 		*/
1692900Seric 
170*55012Seric 		if (bitset(H_EOH, chompheader(freebuf, FALSE, e)))
1713058Seric 			break;
17240965Sbostic 	}
1731392Seric 
1747673Seric 	if (tTd(30, 1))
1752900Seric 		printf("EOH\n");
1762900Seric 
17740965Sbostic 	if (*workbuf == '\0')
17840965Sbostic 	{
17940965Sbostic 		/* throw away a blank line */
18040965Sbostic 		if (sfgets(buf, MAXFIELD, InChannel) == NULL)
18140965Sbostic 			goto readerr;
18240965Sbostic 	}
18340965Sbostic 	else if (workbuf == buf2)	/* guarantee `buf' contains data */
18440965Sbostic 		(void) strcpy(buf, buf2);
1852900Seric 
1862900Seric 	/*
1872900Seric 	**  Collect the body of the message.
1882900Seric 	*/
1892900Seric 
19015532Seric 	do
1912900Seric 	{
1924551Seric 		register char *bp = buf;
1934156Seric 
1947852Seric 		fixcrlf(buf, TRUE);
1954557Seric 
1962900Seric 		/* check for end-of-message */
19752105Seric 		if (!ignrdot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
1982900Seric 			break;
1992900Seric 
2004551Seric 		/* check for transparent dot */
20152105Seric 		if (OpMode == MD_SMTP && bp[0] == '.' && bp[1] == '.')
2024551Seric 			bp++;
2034551Seric 
2044156Seric 		/*
2054156Seric 		**  Figure message length, output the line to the temp
2064156Seric 		**  file, and insert a newline if missing.
2074156Seric 		*/
2084156Seric 
209*55012Seric 		e->e_msgsize += strlen(bp) + 1;
2104551Seric 		fputs(bp, tf);
2117852Seric 		fputs("\n", tf);
2121392Seric 		if (ferror(tf))
213*55012Seric 			tferror(tf, e);
21415532Seric 	} while (sfgets(buf, MAXFIELD, InChannel) != NULL);
21540965Sbostic 
21640965Sbostic readerr:
21711544Seric 	if (fflush(tf) != 0)
218*55012Seric 		tferror(tf, e);
2194083Seric 	(void) fclose(tf);
2202900Seric 
22111145Seric 	/* An EOF when running SMTP is an error */
22219036Seric 	if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP)
22316136Seric 	{
22440965Sbostic 		int usrerr(), syserr();
22536233Skarels # ifdef LOG
22636233Skarels 		if (RealHostName != NULL && LogLevel > 0)
22736230Skarels 			syslog(LOG_NOTICE,
22836230Skarels 			    "collect: unexpected close on connection from %s: %m\n",
229*55012Seric 			    e->e_from.q_paddr, RealHostName);
23036233Skarels # endif
23140965Sbostic 		(feof(InChannel) ? usrerr: syserr)
232*55012Seric 			("collect: unexpected close, from=%s", e->e_from.q_paddr);
23311145Seric 
23416136Seric 		/* don't return an error indication */
235*55012Seric 		e->e_to = NULL;
236*55012Seric 		e->e_flags &= ~EF_FATALERRS;
23716136Seric 
23816136Seric 		/* and don't try to deliver the partial message either */
23916136Seric 		finis();
24016136Seric 	}
24116136Seric 
2422900Seric 	/*
2432900Seric 	**  Find out some information from the headers.
2443386Seric 	**	Examples are who is the from person & the date.
2452900Seric 	*/
2462900Seric 
247*55012Seric 	eatheader(e);
2487673Seric 
2497782Seric 	/*
2507782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
2517782Seric 	*/
2524622Seric 
253*55012Seric 	if (hvalue("to", e) == NULL && hvalue("cc", e) == NULL &&
254*55012Seric 	    hvalue("bcc", e) == NULL && hvalue("apparently-to", e) == NULL)
2557367Seric 	{
2567367Seric 		register ADDRESS *q;
2577367Seric 
2587367Seric 		/* create an Apparently-To: field */
2597367Seric 		/*    that or reject the message.... */
260*55012Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2617367Seric 		{
2627389Seric 			if (q->q_alias != NULL)
2637389Seric 				continue;
2647673Seric 			if (tTd(30, 3))
2657367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
266*55012Seric 			addheader("apparently-to", q->q_paddr, e);
2677367Seric 		}
2687367Seric 	}
2697367Seric 
270*55012Seric 	if ((e->e_dfp = fopen(e->e_df, "r")) == NULL)
271*55012Seric 		syserr("Cannot reopen %s", e->e_df);
2721392Seric }
2731392Seric /*
27440965Sbostic **  FLUSHEOL -- if not at EOL, throw away rest of input line.
27540965Sbostic **
27640965Sbostic **	Parameters:
27740965Sbostic **		buf -- last line read in (checked for '\n'),
27840965Sbostic **		fp -- file to be read from.
27940965Sbostic **
28040965Sbostic **	Returns:
28140965Sbostic **		FALSE on error from sfgets(), TRUE otherwise.
28240965Sbostic **
28340965Sbostic **	Side Effects:
28440965Sbostic **		none.
28540965Sbostic */
28640965Sbostic 
28740965Sbostic bool
28840965Sbostic flusheol(buf, fp)
28940965Sbostic 	char *buf;
29040965Sbostic 	FILE *fp;
29140965Sbostic {
29240965Sbostic 	char junkbuf[MAXLINE], *sfgets();
29340965Sbostic 	register char *p = buf;
29440965Sbostic 
29540965Sbostic 	while (index(p, '\n') == NULL) {
29640965Sbostic 		if (sfgets(junkbuf,MAXLINE,fp) == NULL)
29740965Sbostic 			return(FALSE);
29840965Sbostic 		p = junkbuf;
29940965Sbostic 	}
30040965Sbostic 
30140965Sbostic 	return(TRUE);
30240965Sbostic }
30340965Sbostic /*
30411544Seric **  TFERROR -- signal error on writing the temporary file.
30511544Seric **
30611544Seric **	Parameters:
30711544Seric **		tf -- the file pointer for the temporary file.
30811544Seric **
30911544Seric **	Returns:
31011544Seric **		none.
31111544Seric **
31211544Seric **	Side Effects:
31311544Seric **		Gives an error message.
31411544Seric **		Arranges for following output to go elsewhere.
31511544Seric */
31611544Seric 
317*55012Seric tferror(tf, e)
31811544Seric 	FILE *tf;
319*55012Seric 	register ENVELOPE *e;
32011544Seric {
32111544Seric 	if (errno == ENOSPC)
32211544Seric 	{
323*55012Seric 		(void) freopen(e->e_df, "w", tf);
32411544Seric 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
32511544Seric 		usrerr("452 Out of disk space for temp file");
32611544Seric 	}
32711544Seric 	else
328*55012Seric 		syserr("collect: Cannot write %s", e->e_df);
32911544Seric 	(void) freopen("/dev/null", "w", tf);
33011544Seric }
33111544Seric /*
3322900Seric **  EATFROM -- chew up a UNIX style from line and process
3332900Seric **
3342900Seric **	This does indeed make some assumptions about the format
3352900Seric **	of UNIX messages.
3362900Seric **
3372900Seric **	Parameters:
3382900Seric **		fm -- the from line.
3392900Seric **
3402900Seric **	Returns:
3412900Seric **		none.
3422900Seric **
3432900Seric **	Side Effects:
3442900Seric **		extracts what information it can from the header,
3453386Seric **		such as the date.
3462900Seric */
3472900Seric 
3484321Seric # ifndef NOTUNIX
3494321Seric 
3504203Seric char	*DowList[] =
3514203Seric {
3524203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
3534203Seric };
3544203Seric 
3552900Seric char	*MonthList[] =
3562900Seric {
3572900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3582900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3592900Seric 	NULL
3602900Seric };
3612900Seric 
362*55012Seric eatfrom(fm, e)
3632900Seric 	char *fm;
364*55012Seric 	register ENVELOPE *e;
3652900Seric {
3662900Seric 	register char *p;
3672900Seric 	register char **dt;
3682900Seric 
3697673Seric 	if (tTd(30, 2))
3704203Seric 		printf("eatfrom(%s)\n", fm);
3714203Seric 
3722900Seric 	/* find the date part */
3732900Seric 	p = fm;
3742900Seric 	while (*p != '\0')
3752900Seric 	{
3762900Seric 		/* skip a word */
3772900Seric 		while (*p != '\0' && *p != ' ')
37816896Seric 			p++;
3792900Seric 		while (*p == ' ')
38016896Seric 			p++;
3812900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
3822900Seric 			continue;
3832900Seric 
3842900Seric 		/* we have a possible date */
3854203Seric 		for (dt = DowList; *dt != NULL; dt++)
3862900Seric 			if (strncmp(*dt, p, 3) == 0)
3872900Seric 				break;
3884203Seric 		if (*dt == NULL)
3894203Seric 			continue;
3902900Seric 
3914203Seric 		for (dt = MonthList; *dt != NULL; dt++)
3924203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
3934203Seric 				break;
3942900Seric 		if (*dt != NULL)
3952900Seric 			break;
3962900Seric 	}
3972900Seric 
3982900Seric 	if (*p != NULL)
3992900Seric 	{
4003386Seric 		char *q;
4015366Seric 		extern char *arpadate();
4023386Seric 
4032900Seric 		/* we have found a date */
4043386Seric 		q = xalloc(25);
40523103Seric 		(void) strncpy(q, p, 25);
4063386Seric 		q[24] = '\0';
407*55012Seric 		define('d', q, e);
4085366Seric 		q = arpadate(q);
409*55012Seric 		define('a', newstr(q), e);
4102900Seric 	}
4112900Seric }
4124321Seric 
4134321Seric # endif NOTUNIX
414