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*52106Seric static char sccsid[] = "@(#)collect.c	5.11 (Berkeley) 01/04/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:
24*52106Seric **		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 
3752105Seric collect(smtpmode)
3852105Seric 	bool smtpmode;
391392Seric {
401392Seric 	register FILE *tf;
4152105Seric 	bool ignrdot = smtpmode ? FALSE : IgnrDot;
4240965Sbostic 	char buf[MAXFIELD], buf2[MAXFIELD];
4340965Sbostic 	register char *workbuf, *freebuf;
4440965Sbostic 	register int workbuflen;
452900Seric 	extern char *hvalue();
4640965Sbostic 	extern bool isheader(), flusheol();
471392Seric 
481392Seric 	/*
491392Seric 	**  Create the temp file name and create the file.
501392Seric 	*/
511392Seric 
527809Seric 	CurEnv->e_df = newstr(queuename(CurEnv, 'd'));
537809Seric 	if ((tf = dfopen(CurEnv->e_df, "w")) == NULL)
541392Seric 	{
557809Seric 		syserr("Cannot create %s", CurEnv->e_df);
565366Seric 		NoReturn = TRUE;
575366Seric 		finis();
581392Seric 	}
599047Seric 	(void) chmod(CurEnv->e_df, FileMode);
601392Seric 
614316Seric 	/*
624322Seric 	**  Tell ARPANET to go ahead.
634322Seric 	*/
644322Seric 
6552105Seric 	if (smtpmode)
664710Seric 		message("354", "Enter mail, end with \".\" on a line by itself");
674322Seric 
684322Seric 	/*
694316Seric 	**  Try to read a UNIX-style From line
704316Seric 	*/
714316Seric 
7240965Sbostic 	if (sfgets(buf, MAXFIELD, InChannel) == 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;
802900Seric 		eatfrom(buf);
8140965Sbostic 		if (sfgets(buf, MAXFIELD, InChannel) == NULL)
8240965Sbostic 			goto readerr;
834557Seric 		fixcrlf(buf, FALSE);
842900Seric 	}
854321Seric # 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 	{
9840965Sbostic 		/* first, see if the header is over */
9940965Sbostic 		if (!isheader(workbuf))
10040965Sbostic 		{
10140965Sbostic 			fixcrlf(workbuf, TRUE);
10219036Seric 			break;
10340965Sbostic 		}
10419036Seric 
1057681Seric 		/* if the line is too long, throw the rest away */
10640965Sbostic 		if (!flusheol(workbuf, InChannel))
10740965Sbostic 			goto readerr;
1087681Seric 
10940965Sbostic 		/* it's okay to toss '\n' now (flusheol() needed it) */
11040965Sbostic 		fixcrlf(workbuf, TRUE);
1114557Seric 
11240965Sbostic 		workbuflen = strlen(workbuf);
1132900Seric 
1142900Seric 		/* get the rest of this field */
11540965Sbostic 		for (;;)
1161392Seric 		{
11740965Sbostic 			if (sfgets(freebuf, MAXFIELD, InChannel) == NULL)
11840965Sbostic 				goto readerr;
11940965Sbostic 
12040965Sbostic 			/* is this a continuation line? */
12140965Sbostic 			if (*freebuf != ' ' && *freebuf != '\t')
1222900Seric 				break;
12340965Sbostic 
12440965Sbostic 			if (!flusheol(freebuf, InChannel))
12540965Sbostic 				goto readerr;
12640965Sbostic 
12740965Sbostic 			/* yes; append line to `workbuf' if there's room */
12840965Sbostic 			if (workbuflen < MAXFIELD-3)
12940965Sbostic 			{
13040965Sbostic 				register char *p = workbuf + workbuflen;
13140965Sbostic 				register char *q = freebuf;
13240965Sbostic 
13340965Sbostic 				/* we have room for more of this field */
13440965Sbostic 				fixcrlf(freebuf, TRUE);
13540965Sbostic 				*p++ = '\n'; workbuflen++;
13640965Sbostic 				while(*q != '\0' && workbuflen < MAXFIELD-1)
13740965Sbostic 				{
13840965Sbostic 					*p++ = *q++;
13940965Sbostic 					workbuflen++;
14040965Sbostic 				}
14140965Sbostic 				*p = '\0';
14240965Sbostic 			}
1431392Seric 		}
1441392Seric 
14540965Sbostic 		CurEnv->e_msgsize += workbuflen;
1461392Seric 
1472900Seric 		/*
14840965Sbostic 		**  The working buffer now becomes the free buffer, since
14940965Sbostic 		**  the free buffer contains a new header field.
15040965Sbostic 		**
15140965Sbostic 		**  This is premature, since we still havent called
15240965Sbostic 		**  chompheader() to process the field we just created
15340965Sbostic 		**  (so the call to chompheader() will use `freebuf').
15440965Sbostic 		**  This convolution is necessary so that if we break out
15540965Sbostic 		**  of the loop due to H_EOH, `workbuf' will always be
15640965Sbostic 		**  the next unprocessed buffer.
15740965Sbostic 		*/
15840965Sbostic 
15940965Sbostic 		{
16040965Sbostic 			register char *tmp = workbuf;
16140965Sbostic 			workbuf = freebuf;
16240965Sbostic 			freebuf = tmp;
16340965Sbostic 		}
16440965Sbostic 
16540965Sbostic 		/*
1662900Seric 		**  Snarf header away.
1672900Seric 		*/
1682900Seric 
16940965Sbostic 		if (bitset(H_EOH, chompheader(freebuf, FALSE)))
1703058Seric 			break;
17140965Sbostic 	}
1721392Seric 
1737673Seric 	if (tTd(30, 1))
1742900Seric 		printf("EOH\n");
1752900Seric 
17640965Sbostic 	if (*workbuf == '\0')
17740965Sbostic 	{
17840965Sbostic 		/* throw away a blank line */
17940965Sbostic 		if (sfgets(buf, MAXFIELD, InChannel) == NULL)
18040965Sbostic 			goto readerr;
18140965Sbostic 	}
18240965Sbostic 	else if (workbuf == buf2)	/* guarantee `buf' contains data */
18340965Sbostic 		(void) strcpy(buf, buf2);
1842900Seric 
1852900Seric 	/*
1862900Seric 	**  Collect the body of the message.
1872900Seric 	*/
1882900Seric 
18915532Seric 	do
1902900Seric 	{
1914551Seric 		register char *bp = buf;
1924156Seric 
1937852Seric 		fixcrlf(buf, TRUE);
1944557Seric 
1952900Seric 		/* check for end-of-message */
19652105Seric 		if (!ignrdot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
1972900Seric 			break;
1982900Seric 
1994551Seric 		/* check for transparent dot */
20052105Seric 		if (OpMode == MD_SMTP && bp[0] == '.' && bp[1] == '.')
2014551Seric 			bp++;
2024551Seric 
2034156Seric 		/*
2044156Seric 		**  Figure message length, output the line to the temp
2054156Seric 		**  file, and insert a newline if missing.
2064156Seric 		*/
2074156Seric 
2089371Seric 		CurEnv->e_msgsize += strlen(bp) + 1;
2094551Seric 		fputs(bp, tf);
2107852Seric 		fputs("\n", tf);
2111392Seric 		if (ferror(tf))
21211544Seric 			tferror(tf);
21315532Seric 	} while (sfgets(buf, MAXFIELD, InChannel) != NULL);
21440965Sbostic 
21540965Sbostic readerr:
21611544Seric 	if (fflush(tf) != 0)
21711544Seric 		tferror(tf);
2184083Seric 	(void) fclose(tf);
2192900Seric 
22011145Seric 	/* An EOF when running SMTP is an error */
22119036Seric 	if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP)
22216136Seric 	{
22340965Sbostic 		int usrerr(), syserr();
22436233Skarels # ifdef LOG
22536233Skarels 		if (RealHostName != NULL && LogLevel > 0)
22636230Skarels 			syslog(LOG_NOTICE,
22736230Skarels 			    "collect: unexpected close on connection from %s: %m\n",
22836230Skarels 			    CurEnv->e_from.q_paddr, RealHostName);
22936233Skarels # endif
23040965Sbostic 		(feof(InChannel) ? usrerr: syserr)
23140965Sbostic 			("collect: unexpected close, from=%s", CurEnv->e_from.q_paddr);
23211145Seric 
23316136Seric 		/* don't return an error indication */
23416136Seric 		CurEnv->e_to = NULL;
23516136Seric 		CurEnv->e_flags &= ~EF_FATALERRS;
23616136Seric 
23716136Seric 		/* and don't try to deliver the partial message either */
23816136Seric 		finis();
23916136Seric 	}
24016136Seric 
2412900Seric 	/*
2422900Seric 	**  Find out some information from the headers.
2433386Seric 	**	Examples are who is the from person & the date.
2442900Seric 	*/
2452900Seric 
2469371Seric 	eatheader(CurEnv);
2477673Seric 
2487782Seric 	/*
2497782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
2507782Seric 	*/
2514622Seric 
2527367Seric 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
2537367Seric 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
2547367Seric 	{
2557367Seric 		register ADDRESS *q;
2567367Seric 
2577367Seric 		/* create an Apparently-To: field */
2587367Seric 		/*    that or reject the message.... */
2597367Seric 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
2607367Seric 		{
2617389Seric 			if (q->q_alias != NULL)
2627389Seric 				continue;
2637673Seric 			if (tTd(30, 3))
2647367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
2657367Seric 			addheader("apparently-to", q->q_paddr, CurEnv);
2667367Seric 		}
2677367Seric 	}
2687367Seric 
2699539Seric 	if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL)
2706986Seric 		syserr("Cannot reopen %s", CurEnv->e_df);
2711392Seric }
2721392Seric /*
27340965Sbostic **  FLUSHEOL -- if not at EOL, throw away rest of input line.
27440965Sbostic **
27540965Sbostic **	Parameters:
27640965Sbostic **		buf -- last line read in (checked for '\n'),
27740965Sbostic **		fp -- file to be read from.
27840965Sbostic **
27940965Sbostic **	Returns:
28040965Sbostic **		FALSE on error from sfgets(), TRUE otherwise.
28140965Sbostic **
28240965Sbostic **	Side Effects:
28340965Sbostic **		none.
28440965Sbostic */
28540965Sbostic 
28640965Sbostic bool
28740965Sbostic flusheol(buf, fp)
28840965Sbostic 	char *buf;
28940965Sbostic 	FILE *fp;
29040965Sbostic {
29140965Sbostic 	char junkbuf[MAXLINE], *sfgets();
29240965Sbostic 	register char *p = buf;
29340965Sbostic 
29440965Sbostic 	while (index(p, '\n') == NULL) {
29540965Sbostic 		if (sfgets(junkbuf,MAXLINE,fp) == NULL)
29640965Sbostic 			return(FALSE);
29740965Sbostic 		p = junkbuf;
29840965Sbostic 	}
29940965Sbostic 
30040965Sbostic 	return(TRUE);
30140965Sbostic }
30240965Sbostic /*
30311544Seric **  TFERROR -- signal error on writing the temporary file.
30411544Seric **
30511544Seric **	Parameters:
30611544Seric **		tf -- the file pointer for the temporary file.
30711544Seric **
30811544Seric **	Returns:
30911544Seric **		none.
31011544Seric **
31111544Seric **	Side Effects:
31211544Seric **		Gives an error message.
31311544Seric **		Arranges for following output to go elsewhere.
31411544Seric */
31511544Seric 
31611544Seric tferror(tf)
31711544Seric 	FILE *tf;
31811544Seric {
31911544Seric 	if (errno == ENOSPC)
32011544Seric 	{
32111544Seric 		(void) freopen(CurEnv->e_df, "w", tf);
32211544Seric 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
32311544Seric 		usrerr("452 Out of disk space for temp file");
32411544Seric 	}
32511544Seric 	else
32611544Seric 		syserr("collect: Cannot write %s", CurEnv->e_df);
32711544Seric 	(void) freopen("/dev/null", "w", tf);
32811544Seric }
32911544Seric /*
3302900Seric **  EATFROM -- chew up a UNIX style from line and process
3312900Seric **
3322900Seric **	This does indeed make some assumptions about the format
3332900Seric **	of UNIX messages.
3342900Seric **
3352900Seric **	Parameters:
3362900Seric **		fm -- the from line.
3372900Seric **
3382900Seric **	Returns:
3392900Seric **		none.
3402900Seric **
3412900Seric **	Side Effects:
3422900Seric **		extracts what information it can from the header,
3433386Seric **		such as the date.
3442900Seric */
3452900Seric 
3464321Seric # ifndef NOTUNIX
3474321Seric 
3484203Seric char	*DowList[] =
3494203Seric {
3504203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
3514203Seric };
3524203Seric 
3532900Seric char	*MonthList[] =
3542900Seric {
3552900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3562900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3572900Seric 	NULL
3582900Seric };
3592900Seric 
3602900Seric eatfrom(fm)
3612900Seric 	char *fm;
3622900Seric {
3632900Seric 	register char *p;
3642900Seric 	register char **dt;
3652900Seric 
3667673Seric 	if (tTd(30, 2))
3674203Seric 		printf("eatfrom(%s)\n", fm);
3684203Seric 
3692900Seric 	/* find the date part */
3702900Seric 	p = fm;
3712900Seric 	while (*p != '\0')
3722900Seric 	{
3732900Seric 		/* skip a word */
3742900Seric 		while (*p != '\0' && *p != ' ')
37516896Seric 			p++;
3762900Seric 		while (*p == ' ')
37716896Seric 			p++;
3782900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
3792900Seric 			continue;
3802900Seric 
3812900Seric 		/* we have a possible date */
3824203Seric 		for (dt = DowList; *dt != NULL; dt++)
3832900Seric 			if (strncmp(*dt, p, 3) == 0)
3842900Seric 				break;
3854203Seric 		if (*dt == NULL)
3864203Seric 			continue;
3872900Seric 
3884203Seric 		for (dt = MonthList; *dt != NULL; dt++)
3894203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
3904203Seric 				break;
3912900Seric 		if (*dt != NULL)
3922900Seric 			break;
3932900Seric 	}
3942900Seric 
3952900Seric 	if (*p != NULL)
3962900Seric 	{
3973386Seric 		char *q;
3985366Seric 		extern char *arpadate();
3993386Seric 
4002900Seric 		/* we have found a date */
4013386Seric 		q = xalloc(25);
40223103Seric 		(void) strncpy(q, p, 25);
4033386Seric 		q[24] = '\0';
4049371Seric 		define('d', q, CurEnv);
4055366Seric 		q = arpadate(q);
4069371Seric 		define('a', newstr(q), CurEnv);
4072900Seric 	}
4082900Seric }
4094321Seric 
4104321Seric # endif NOTUNIX
411