122697Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
333728Sbostic  * Copyright (c) 1988 Regents of the University of California.
433728Sbostic  * All rights reserved.
533728Sbostic  *
6*42824Sbostic  * %sccs.include.redist.c%
733728Sbostic  */
822697Sdist 
922697Sdist #ifndef lint
10*42824Sbostic static char sccsid[] = "@(#)collect.c	5.9 (Berkeley) 06/01/90";
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:
244710Seric **		sayok -- if set, give an ARPANET style message
254710Seric **			to say we are ready to collect input.
261392Seric **
271392Seric **	Returns:
284162Seric **		none.
291392Seric **
301392Seric **	Side Effects:
311392Seric **		Temp file is created and filled.
324162Seric **		The from person may be set.
331392Seric */
341392Seric 
354710Seric collect(sayok)
364710Seric 	bool sayok;
371392Seric {
381392Seric 	register FILE *tf;
3940965Sbostic 	char buf[MAXFIELD], buf2[MAXFIELD];
4040965Sbostic 	register char *workbuf, *freebuf;
4140965Sbostic 	register int workbuflen;
422900Seric 	extern char *hvalue();
4340965Sbostic 	extern bool isheader(), flusheol();
441392Seric 
451392Seric 	/*
461392Seric 	**  Create the temp file name and create the file.
471392Seric 	*/
481392Seric 
497809Seric 	CurEnv->e_df = newstr(queuename(CurEnv, 'd'));
507809Seric 	if ((tf = dfopen(CurEnv->e_df, "w")) == NULL)
511392Seric 	{
527809Seric 		syserr("Cannot create %s", CurEnv->e_df);
535366Seric 		NoReturn = TRUE;
545366Seric 		finis();
551392Seric 	}
569047Seric 	(void) chmod(CurEnv->e_df, FileMode);
571392Seric 
584316Seric 	/*
594322Seric 	**  Tell ARPANET to go ahead.
604322Seric 	*/
614322Seric 
624710Seric 	if (sayok)
634710Seric 		message("354", "Enter mail, end with \".\" on a line by itself");
644322Seric 
654322Seric 	/*
664316Seric 	**  Try to read a UNIX-style From line
674316Seric 	*/
684316Seric 
6940965Sbostic 	if (sfgets(buf, MAXFIELD, InChannel) == NULL)
7040965Sbostic 		goto readerr;
714557Seric 	fixcrlf(buf, FALSE);
724321Seric # ifndef NOTUNIX
734322Seric 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
742900Seric 	{
7540965Sbostic 		if (!flusheol(buf, InChannel))
7640965Sbostic 			goto readerr;
772900Seric 		eatfrom(buf);
7840965Sbostic 		if (sfgets(buf, MAXFIELD, InChannel) == NULL)
7940965Sbostic 			goto readerr;
804557Seric 		fixcrlf(buf, FALSE);
812900Seric 	}
824321Seric # endif NOTUNIX
832900Seric 
841392Seric 	/*
855975Seric 	**  Copy InChannel to temp file & do message editing.
861392Seric 	**	To keep certain mailers from getting confused,
871392Seric 	**	and to keep the output clean, lines that look
8813932Seric 	**	like UNIX "From" lines are deleted in the header.
891392Seric 	*/
901392Seric 
9140965Sbostic 	workbuf = buf;		/* `workbuf' contains a header field */
9240965Sbostic 	freebuf = buf2;		/* `freebuf' can be used for read-ahead */
9340965Sbostic 	for (;;)
941392Seric 	{
9540965Sbostic 		/* first, see if the header is over */
9640965Sbostic 		if (!isheader(workbuf))
9740965Sbostic 		{
9840965Sbostic 			fixcrlf(workbuf, TRUE);
9919036Seric 			break;
10040965Sbostic 		}
10119036Seric 
1027681Seric 		/* if the line is too long, throw the rest away */
10340965Sbostic 		if (!flusheol(workbuf, InChannel))
10440965Sbostic 			goto readerr;
1057681Seric 
10640965Sbostic 		/* it's okay to toss '\n' now (flusheol() needed it) */
10740965Sbostic 		fixcrlf(workbuf, TRUE);
1084557Seric 
10940965Sbostic 		workbuflen = strlen(workbuf);
1102900Seric 
1112900Seric 		/* get the rest of this field */
11240965Sbostic 		for (;;)
1131392Seric 		{
11440965Sbostic 			if (sfgets(freebuf, MAXFIELD, InChannel) == NULL)
11540965Sbostic 				goto readerr;
11640965Sbostic 
11740965Sbostic 			/* is this a continuation line? */
11840965Sbostic 			if (*freebuf != ' ' && *freebuf != '\t')
1192900Seric 				break;
12040965Sbostic 
12140965Sbostic 			if (!flusheol(freebuf, InChannel))
12240965Sbostic 				goto readerr;
12340965Sbostic 
12440965Sbostic 			/* yes; append line to `workbuf' if there's room */
12540965Sbostic 			if (workbuflen < MAXFIELD-3)
12640965Sbostic 			{
12740965Sbostic 				register char *p = workbuf + workbuflen;
12840965Sbostic 				register char *q = freebuf;
12940965Sbostic 
13040965Sbostic 				/* we have room for more of this field */
13140965Sbostic 				fixcrlf(freebuf, TRUE);
13240965Sbostic 				*p++ = '\n'; workbuflen++;
13340965Sbostic 				while(*q != '\0' && workbuflen < MAXFIELD-1)
13440965Sbostic 				{
13540965Sbostic 					*p++ = *q++;
13640965Sbostic 					workbuflen++;
13740965Sbostic 				}
13840965Sbostic 				*p = '\0';
13940965Sbostic 			}
1401392Seric 		}
1411392Seric 
14240965Sbostic 		CurEnv->e_msgsize += workbuflen;
1431392Seric 
1442900Seric 		/*
14540965Sbostic 		**  The working buffer now becomes the free buffer, since
14640965Sbostic 		**  the free buffer contains a new header field.
14740965Sbostic 		**
14840965Sbostic 		**  This is premature, since we still havent called
14940965Sbostic 		**  chompheader() to process the field we just created
15040965Sbostic 		**  (so the call to chompheader() will use `freebuf').
15140965Sbostic 		**  This convolution is necessary so that if we break out
15240965Sbostic 		**  of the loop due to H_EOH, `workbuf' will always be
15340965Sbostic 		**  the next unprocessed buffer.
15440965Sbostic 		*/
15540965Sbostic 
15640965Sbostic 		{
15740965Sbostic 			register char *tmp = workbuf;
15840965Sbostic 			workbuf = freebuf;
15940965Sbostic 			freebuf = tmp;
16040965Sbostic 		}
16140965Sbostic 
16240965Sbostic 		/*
1632900Seric 		**  Snarf header away.
1642900Seric 		*/
1652900Seric 
16640965Sbostic 		if (bitset(H_EOH, chompheader(freebuf, FALSE)))
1673058Seric 			break;
16840965Sbostic 	}
1691392Seric 
1707673Seric 	if (tTd(30, 1))
1712900Seric 		printf("EOH\n");
1722900Seric 
17340965Sbostic 	if (*workbuf == '\0')
17440965Sbostic 	{
17540965Sbostic 		/* throw away a blank line */
17640965Sbostic 		if (sfgets(buf, MAXFIELD, InChannel) == NULL)
17740965Sbostic 			goto readerr;
17840965Sbostic 	}
17940965Sbostic 	else if (workbuf == buf2)	/* guarantee `buf' contains data */
18040965Sbostic 		(void) strcpy(buf, buf2);
1812900Seric 
1822900Seric 	/*
1832900Seric 	**  Collect the body of the message.
1842900Seric 	*/
1852900Seric 
18615532Seric 	do
1872900Seric 	{
1884551Seric 		register char *bp = buf;
1894156Seric 
1907852Seric 		fixcrlf(buf, TRUE);
1914557Seric 
1922900Seric 		/* check for end-of-message */
1932900Seric 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
1942900Seric 			break;
1952900Seric 
1964551Seric 		/* check for transparent dot */
1979371Seric 		if (OpMode == MD_SMTP && !IgnrDot && bp[0] == '.' && bp[1] == '.')
1984551Seric 			bp++;
1994551Seric 
2004156Seric 		/*
2014156Seric 		**  Figure message length, output the line to the temp
2024156Seric 		**  file, and insert a newline if missing.
2034156Seric 		*/
2044156Seric 
2059371Seric 		CurEnv->e_msgsize += strlen(bp) + 1;
2064551Seric 		fputs(bp, tf);
2077852Seric 		fputs("\n", tf);
2081392Seric 		if (ferror(tf))
20911544Seric 			tferror(tf);
21015532Seric 	} while (sfgets(buf, MAXFIELD, InChannel) != NULL);
21140965Sbostic 
21240965Sbostic readerr:
21311544Seric 	if (fflush(tf) != 0)
21411544Seric 		tferror(tf);
2154083Seric 	(void) fclose(tf);
2162900Seric 
21711145Seric 	/* An EOF when running SMTP is an error */
21819036Seric 	if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP)
21916136Seric 	{
22040965Sbostic 		int usrerr(), syserr();
22136233Skarels # ifdef LOG
22236233Skarels 		if (RealHostName != NULL && LogLevel > 0)
22336230Skarels 			syslog(LOG_NOTICE,
22436230Skarels 			    "collect: unexpected close on connection from %s: %m\n",
22536230Skarels 			    CurEnv->e_from.q_paddr, RealHostName);
22636233Skarels # endif
22740965Sbostic 		(feof(InChannel) ? usrerr: syserr)
22840965Sbostic 			("collect: unexpected close, from=%s", CurEnv->e_from.q_paddr);
22911145Seric 
23016136Seric 		/* don't return an error indication */
23116136Seric 		CurEnv->e_to = NULL;
23216136Seric 		CurEnv->e_flags &= ~EF_FATALERRS;
23316136Seric 
23416136Seric 		/* and don't try to deliver the partial message either */
23516136Seric 		finis();
23616136Seric 	}
23716136Seric 
2382900Seric 	/*
2392900Seric 	**  Find out some information from the headers.
2403386Seric 	**	Examples are who is the from person & the date.
2412900Seric 	*/
2422900Seric 
2439371Seric 	eatheader(CurEnv);
2447673Seric 
2457782Seric 	/*
2467782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
2477782Seric 	*/
2484622Seric 
2497367Seric 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
2507367Seric 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
2517367Seric 	{
2527367Seric 		register ADDRESS *q;
2537367Seric 
2547367Seric 		/* create an Apparently-To: field */
2557367Seric 		/*    that or reject the message.... */
2567367Seric 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
2577367Seric 		{
2587389Seric 			if (q->q_alias != NULL)
2597389Seric 				continue;
2607673Seric 			if (tTd(30, 3))
2617367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
2627367Seric 			addheader("apparently-to", q->q_paddr, CurEnv);
2637367Seric 		}
2647367Seric 	}
2657367Seric 
2669539Seric 	if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL)
2676986Seric 		syserr("Cannot reopen %s", CurEnv->e_df);
2681392Seric }
2691392Seric /*
27040965Sbostic **  FLUSHEOL -- if not at EOL, throw away rest of input line.
27140965Sbostic **
27240965Sbostic **	Parameters:
27340965Sbostic **		buf -- last line read in (checked for '\n'),
27440965Sbostic **		fp -- file to be read from.
27540965Sbostic **
27640965Sbostic **	Returns:
27740965Sbostic **		FALSE on error from sfgets(), TRUE otherwise.
27840965Sbostic **
27940965Sbostic **	Side Effects:
28040965Sbostic **		none.
28140965Sbostic */
28240965Sbostic 
28340965Sbostic bool
28440965Sbostic flusheol(buf, fp)
28540965Sbostic 	char *buf;
28640965Sbostic 	FILE *fp;
28740965Sbostic {
28840965Sbostic 	char junkbuf[MAXLINE], *sfgets();
28940965Sbostic 	register char *p = buf;
29040965Sbostic 
29140965Sbostic 	while (index(p, '\n') == NULL) {
29240965Sbostic 		if (sfgets(junkbuf,MAXLINE,fp) == NULL)
29340965Sbostic 			return(FALSE);
29440965Sbostic 		p = junkbuf;
29540965Sbostic 	}
29640965Sbostic 
29740965Sbostic 	return(TRUE);
29840965Sbostic }
29940965Sbostic /*
30011544Seric **  TFERROR -- signal error on writing the temporary file.
30111544Seric **
30211544Seric **	Parameters:
30311544Seric **		tf -- the file pointer for the temporary file.
30411544Seric **
30511544Seric **	Returns:
30611544Seric **		none.
30711544Seric **
30811544Seric **	Side Effects:
30911544Seric **		Gives an error message.
31011544Seric **		Arranges for following output to go elsewhere.
31111544Seric */
31211544Seric 
31311544Seric tferror(tf)
31411544Seric 	FILE *tf;
31511544Seric {
31611544Seric 	if (errno == ENOSPC)
31711544Seric 	{
31811544Seric 		(void) freopen(CurEnv->e_df, "w", tf);
31911544Seric 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
32011544Seric 		usrerr("452 Out of disk space for temp file");
32111544Seric 	}
32211544Seric 	else
32311544Seric 		syserr("collect: Cannot write %s", CurEnv->e_df);
32411544Seric 	(void) freopen("/dev/null", "w", tf);
32511544Seric }
32611544Seric /*
3272900Seric **  EATFROM -- chew up a UNIX style from line and process
3282900Seric **
3292900Seric **	This does indeed make some assumptions about the format
3302900Seric **	of UNIX messages.
3312900Seric **
3322900Seric **	Parameters:
3332900Seric **		fm -- the from line.
3342900Seric **
3352900Seric **	Returns:
3362900Seric **		none.
3372900Seric **
3382900Seric **	Side Effects:
3392900Seric **		extracts what information it can from the header,
3403386Seric **		such as the date.
3412900Seric */
3422900Seric 
3434321Seric # ifndef NOTUNIX
3444321Seric 
3454203Seric char	*DowList[] =
3464203Seric {
3474203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
3484203Seric };
3494203Seric 
3502900Seric char	*MonthList[] =
3512900Seric {
3522900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3532900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3542900Seric 	NULL
3552900Seric };
3562900Seric 
3572900Seric eatfrom(fm)
3582900Seric 	char *fm;
3592900Seric {
3602900Seric 	register char *p;
3612900Seric 	register char **dt;
3622900Seric 
3637673Seric 	if (tTd(30, 2))
3644203Seric 		printf("eatfrom(%s)\n", fm);
3654203Seric 
3662900Seric 	/* find the date part */
3672900Seric 	p = fm;
3682900Seric 	while (*p != '\0')
3692900Seric 	{
3702900Seric 		/* skip a word */
3712900Seric 		while (*p != '\0' && *p != ' ')
37216896Seric 			p++;
3732900Seric 		while (*p == ' ')
37416896Seric 			p++;
3752900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
3762900Seric 			continue;
3772900Seric 
3782900Seric 		/* we have a possible date */
3794203Seric 		for (dt = DowList; *dt != NULL; dt++)
3802900Seric 			if (strncmp(*dt, p, 3) == 0)
3812900Seric 				break;
3824203Seric 		if (*dt == NULL)
3834203Seric 			continue;
3842900Seric 
3854203Seric 		for (dt = MonthList; *dt != NULL; dt++)
3864203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
3874203Seric 				break;
3882900Seric 		if (*dt != NULL)
3892900Seric 			break;
3902900Seric 	}
3912900Seric 
3922900Seric 	if (*p != NULL)
3932900Seric 	{
3943386Seric 		char *q;
3955366Seric 		extern char *arpadate();
3963386Seric 
3972900Seric 		/* we have found a date */
3983386Seric 		q = xalloc(25);
39923103Seric 		(void) strncpy(q, p, 25);
4003386Seric 		q[24] = '\0';
4019371Seric 		define('d', q, CurEnv);
4025366Seric 		q = arpadate(q);
4039371Seric 		define('a', newstr(q), CurEnv);
4042900Seric 	}
4052900Seric }
4064321Seric 
4074321Seric # endif NOTUNIX
408