122697Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
362522Sbostic  * Copyright (c) 1988, 1993
462522Sbostic  *	The Regents of the University of California.  All rights reserved.
533728Sbostic  *
642824Sbostic  * %sccs.include.redist.c%
733728Sbostic  */
822697Sdist 
922697Sdist #ifndef lint
10*64124Seric static char sccsid[] = "@(#)collect.c	8.4 (Berkeley) 08/06/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 
5664086Seric 	e->e_df = queuename(e, 'd');
5764086Seric 	e->e_df = newstr(e->e_df);
5859745Seric 	if ((tf = dfopen(e->e_df, O_WRONLY|O_CREAT, FileMode)) == NULL)
591392Seric 	{
6055012Seric 		syserr("Cannot create %s", e->e_df);
615366Seric 		NoReturn = TRUE;
625366Seric 		finis();
631392Seric 	}
641392Seric 
654316Seric 	/*
664322Seric 	**  Tell ARPANET to go ahead.
674322Seric 	*/
684322Seric 
6952105Seric 	if (smtpmode)
7058151Seric 		message("354 Enter mail, end with \".\" on a line by itself");
714322Seric 
724322Seric 	/*
734316Seric 	**  Try to read a UNIX-style From line
744316Seric 	*/
754316Seric 
7661093Seric 	if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock,
7761093Seric 			"initial message read") == NULL)
7840965Sbostic 		goto readerr;
794557Seric 	fixcrlf(buf, FALSE);
804321Seric # ifndef NOTUNIX
814322Seric 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
822900Seric 	{
8340965Sbostic 		if (!flusheol(buf, InChannel))
8440965Sbostic 			goto readerr;
8555012Seric 		eatfrom(buf, e);
8661093Seric 		if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock,
8761093Seric 				"message header read") == NULL)
8840965Sbostic 			goto readerr;
894557Seric 		fixcrlf(buf, FALSE);
902900Seric 	}
9156795Seric # endif /* NOTUNIX */
922900Seric 
931392Seric 	/*
945975Seric 	**  Copy InChannel to temp file & do message editing.
951392Seric 	**	To keep certain mailers from getting confused,
961392Seric 	**	and to keep the output clean, lines that look
9713932Seric 	**	like UNIX "From" lines are deleted in the header.
981392Seric 	*/
991392Seric 
10040965Sbostic 	workbuf = buf;		/* `workbuf' contains a header field */
10140965Sbostic 	freebuf = buf2;		/* `freebuf' can be used for read-ahead */
10240965Sbostic 	for (;;)
1031392Seric 	{
10457135Seric 		char *curbuf;
10557135Seric 		int curbuffree;
10657135Seric 		register int curbuflen;
10757135Seric 		char *p;
10857135Seric 
10940965Sbostic 		/* first, see if the header is over */
11040965Sbostic 		if (!isheader(workbuf))
11140965Sbostic 		{
11240965Sbostic 			fixcrlf(workbuf, TRUE);
11319036Seric 			break;
11440965Sbostic 		}
11519036Seric 
1167681Seric 		/* if the line is too long, throw the rest away */
11740965Sbostic 		if (!flusheol(workbuf, InChannel))
11840965Sbostic 			goto readerr;
1197681Seric 
12040965Sbostic 		/* it's okay to toss '\n' now (flusheol() needed it) */
12140965Sbostic 		fixcrlf(workbuf, TRUE);
1224557Seric 
12357135Seric 		curbuf = workbuf;
12457135Seric 		curbuflen = strlen(curbuf);
12557135Seric 		curbuffree = MAXLINE - curbuflen;
12657135Seric 		p = curbuf + curbuflen;
1272900Seric 
1282900Seric 		/* get the rest of this field */
12940965Sbostic 		for (;;)
1301392Seric 		{
13157135Seric 			int clen;
13257135Seric 
13361093Seric 			if (sfgets(freebuf, MAXLINE, InChannel,
13461093Seric 					TimeOuts.to_datablock,
13561093Seric 					"message header read") == NULL)
13640965Sbostic 				goto readerr;
13740965Sbostic 
13840965Sbostic 			/* is this a continuation line? */
13940965Sbostic 			if (*freebuf != ' ' && *freebuf != '\t')
1402900Seric 				break;
14140965Sbostic 
14240965Sbostic 			if (!flusheol(freebuf, InChannel))
14340965Sbostic 				goto readerr;
14440965Sbostic 
14557135Seric 			fixcrlf(freebuf, TRUE);
14657135Seric 			clen = strlen(freebuf) + 1;
14757135Seric 
14857135Seric 			/* if insufficient room, dynamically allocate buffer */
14957135Seric 			if (clen >= curbuffree)
15040965Sbostic 			{
15157135Seric 				/* reallocate buffer */
15257135Seric 				int nbuflen = ((p - curbuf) + clen) * 2;
15357135Seric 				char *nbuf = xalloc(nbuflen);
15440965Sbostic 
15557135Seric 				p = nbuf + curbuflen;
15657135Seric 				curbuffree = nbuflen - curbuflen;
15757135Seric 				bcopy(curbuf, nbuf, curbuflen);
15857135Seric 				if (curbuf != buf && curbuf != buf2)
15957135Seric 					free(curbuf);
16057135Seric 				curbuf = nbuf;
16140965Sbostic 			}
16257135Seric 			*p++ = '\n';
16357135Seric 			bcopy(freebuf, p, clen - 1);
16457135Seric 			p += clen - 1;
16557135Seric 			curbuffree -= clen;
16657135Seric 			curbuflen += clen;
1671392Seric 		}
16857135Seric 		*p++ = '\0';
1691392Seric 
17057135Seric 		e->e_msgsize += curbuflen;
1711392Seric 
1722900Seric 		/*
17340965Sbostic 		**  The working buffer now becomes the free buffer, since
17440965Sbostic 		**  the free buffer contains a new header field.
17540965Sbostic 		**
17640965Sbostic 		**  This is premature, since we still havent called
17740965Sbostic 		**  chompheader() to process the field we just created
17840965Sbostic 		**  (so the call to chompheader() will use `freebuf').
17940965Sbostic 		**  This convolution is necessary so that if we break out
18040965Sbostic 		**  of the loop due to H_EOH, `workbuf' will always be
18140965Sbostic 		**  the next unprocessed buffer.
18240965Sbostic 		*/
18340965Sbostic 
18440965Sbostic 		{
18540965Sbostic 			register char *tmp = workbuf;
18640965Sbostic 			workbuf = freebuf;
18740965Sbostic 			freebuf = tmp;
18840965Sbostic 		}
18940965Sbostic 
19040965Sbostic 		/*
1912900Seric 		**  Snarf header away.
1922900Seric 		*/
1932900Seric 
19457135Seric 		if (bitset(H_EOH, chompheader(curbuf, FALSE, e)))
1953058Seric 			break;
19657135Seric 
19757135Seric 		/*
19857135Seric 		**  If the buffer was dynamically allocated, free it.
19957135Seric 		*/
20057135Seric 
20157135Seric 		if (curbuf != buf && curbuf != buf2)
20257135Seric 			free(curbuf);
20340965Sbostic 	}
2041392Seric 
2057673Seric 	if (tTd(30, 1))
2062900Seric 		printf("EOH\n");
2072900Seric 
20840965Sbostic 	if (*workbuf == '\0')
20940965Sbostic 	{
21040965Sbostic 		/* throw away a blank line */
21161093Seric 		if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock,
21261093Seric 				"message separator read") == NULL)
21340965Sbostic 			goto readerr;
21440965Sbostic 	}
21540965Sbostic 	else if (workbuf == buf2)	/* guarantee `buf' contains data */
21640965Sbostic 		(void) strcpy(buf, buf2);
2172900Seric 
2182900Seric 	/*
2192900Seric 	**  Collect the body of the message.
2202900Seric 	*/
2212900Seric 
22215532Seric 	do
2232900Seric 	{
2244551Seric 		register char *bp = buf;
2254156Seric 
2267852Seric 		fixcrlf(buf, TRUE);
2274557Seric 
2282900Seric 		/* check for end-of-message */
22952105Seric 		if (!ignrdot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
2302900Seric 			break;
2312900Seric 
2324551Seric 		/* check for transparent dot */
23352105Seric 		if (OpMode == MD_SMTP && bp[0] == '.' && bp[1] == '.')
2344551Seric 			bp++;
2354551Seric 
2364156Seric 		/*
2374156Seric 		**  Figure message length, output the line to the temp
2384156Seric 		**  file, and insert a newline if missing.
2394156Seric 		*/
2404156Seric 
24155012Seric 		e->e_msgsize += strlen(bp) + 1;
2424551Seric 		fputs(bp, tf);
2437852Seric 		fputs("\n", tf);
2441392Seric 		if (ferror(tf))
24555012Seric 			tferror(tf, e);
24661093Seric 	} while (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock,
24761093Seric 			"message body read") != NULL);
24840965Sbostic 
24940965Sbostic readerr:
25011544Seric 	if (fflush(tf) != 0)
25155012Seric 		tferror(tf, e);
25260603Seric 	(void) fsync(fileno(tf));
2534083Seric 	(void) fclose(tf);
2542900Seric 
25511145Seric 	/* An EOF when running SMTP is an error */
25619036Seric 	if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP)
25716136Seric 	{
25858308Seric 		char *host;
25958082Seric 
26058308Seric 		host = RealHostName;
26158308Seric 		if (host == NULL)
26258308Seric 			host = "localhost";
26358308Seric 
26436233Skarels # ifdef LOG
26558308Seric 		if (LogLevel > 0 && feof(InChannel))
26636230Skarels 			syslog(LOG_NOTICE,
26758308Seric 			    "collect: unexpected close on connection from %s, sender=%s: %m\n",
26858308Seric 			    host, e->e_from.q_paddr);
26936233Skarels # endif
27058082Seric 		(feof(InChannel) ? usrerr : syserr)
27158308Seric 			("451 collect: unexpected close on connection from %s, from=%s",
27258308Seric 				host, e->e_from.q_paddr);
27311145Seric 
27416136Seric 		/* don't return an error indication */
27555012Seric 		e->e_to = NULL;
27655012Seric 		e->e_flags &= ~EF_FATALERRS;
277*64124Seric 		e->e_flags |= EF_CLRQUEUE;
27816136Seric 
27916136Seric 		/* and don't try to deliver the partial message either */
28016136Seric 		finis();
28116136Seric 	}
28216136Seric 
2832900Seric 	/*
2842900Seric 	**  Find out some information from the headers.
2853386Seric 	**	Examples are who is the from person & the date.
2862900Seric 	*/
2872900Seric 
28858929Seric 	eatheader(e, !requeueflag);
2897673Seric 
29064068Seric 	/* collect statistics */
29164068Seric 	if (OpMode != MD_VERIFY)
29264068Seric 		markstats(e, (ADDRESS *) NULL);
29364068Seric 
2947782Seric 	/*
2957782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
2967782Seric 	*/
2974622Seric 
29855012Seric 	if (hvalue("to", e) == NULL && hvalue("cc", e) == NULL &&
29955012Seric 	    hvalue("bcc", e) == NULL && hvalue("apparently-to", e) == NULL)
3007367Seric 	{
3017367Seric 		register ADDRESS *q;
3027367Seric 
3037367Seric 		/* create an Apparently-To: field */
3047367Seric 		/*    that or reject the message.... */
30555012Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
3067367Seric 		{
3077389Seric 			if (q->q_alias != NULL)
3087389Seric 				continue;
3097673Seric 			if (tTd(30, 3))
3107367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
31159579Seric 			addheader("Apparently-To", q->q_paddr, e);
3127367Seric 		}
3137367Seric 	}
3147367Seric 
31559320Seric 	/* check for message too large */
31659320Seric 	if (MaxMessageSize > 0 && e->e_msgsize > MaxMessageSize)
31759320Seric 	{
31859320Seric 		usrerr("552 Message exceeds maximum fixed size (%ld)",
31959320Seric 			MaxMessageSize);
32059320Seric 	}
32159320Seric 
32255012Seric 	if ((e->e_dfp = fopen(e->e_df, "r")) == NULL)
32358690Seric 	{
32458690Seric 		/* we haven't acked receipt yet, so just chuck this */
32555012Seric 		syserr("Cannot reopen %s", e->e_df);
32658690Seric 		finis();
32758690Seric 	}
3281392Seric }
3291392Seric /*
33040965Sbostic **  FLUSHEOL -- if not at EOL, throw away rest of input line.
33140965Sbostic **
33240965Sbostic **	Parameters:
33340965Sbostic **		buf -- last line read in (checked for '\n'),
33440965Sbostic **		fp -- file to be read from.
33540965Sbostic **
33640965Sbostic **	Returns:
33740965Sbostic **		FALSE on error from sfgets(), TRUE otherwise.
33840965Sbostic **
33940965Sbostic **	Side Effects:
34040965Sbostic **		none.
34140965Sbostic */
34240965Sbostic 
34340965Sbostic bool
34440965Sbostic flusheol(buf, fp)
34540965Sbostic 	char *buf;
34640965Sbostic 	FILE *fp;
34740965Sbostic {
34840965Sbostic 	register char *p = buf;
34957134Seric 	bool printmsg = TRUE;
35057134Seric 	char junkbuf[MAXLINE];
35140965Sbostic 
35257134Seric 	while (strchr(p, '\n') == NULL)
35357134Seric 	{
35457134Seric 		if (printmsg)
35558151Seric 			usrerr("553 header line too long");
35657134Seric 		printmsg = FALSE;
35761093Seric 		if (sfgets(junkbuf, MAXLINE, fp, TimeOuts.to_datablock,
35861093Seric 				"long line flush") == NULL)
35957134Seric 			return (FALSE);
36040965Sbostic 		p = junkbuf;
36140965Sbostic 	}
36240965Sbostic 
36357134Seric 	return (TRUE);
36440965Sbostic }
36540965Sbostic /*
36611544Seric **  TFERROR -- signal error on writing the temporary file.
36711544Seric **
36811544Seric **	Parameters:
36911544Seric **		tf -- the file pointer for the temporary file.
37011544Seric **
37111544Seric **	Returns:
37211544Seric **		none.
37311544Seric **
37411544Seric **	Side Effects:
37511544Seric **		Gives an error message.
37611544Seric **		Arranges for following output to go elsewhere.
37711544Seric */
37811544Seric 
37955012Seric tferror(tf, e)
38011544Seric 	FILE *tf;
38155012Seric 	register ENVELOPE *e;
38211544Seric {
38311544Seric 	if (errno == ENOSPC)
38411544Seric 	{
38555012Seric 		(void) freopen(e->e_df, "w", tf);
38611544Seric 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
38711544Seric 		usrerr("452 Out of disk space for temp file");
38811544Seric 	}
38911544Seric 	else
39055012Seric 		syserr("collect: Cannot write %s", e->e_df);
39111544Seric 	(void) freopen("/dev/null", "w", tf);
39211544Seric }
39311544Seric /*
3942900Seric **  EATFROM -- chew up a UNIX style from line and process
3952900Seric **
3962900Seric **	This does indeed make some assumptions about the format
3972900Seric **	of UNIX messages.
3982900Seric **
3992900Seric **	Parameters:
4002900Seric **		fm -- the from line.
4012900Seric **
4022900Seric **	Returns:
4032900Seric **		none.
4042900Seric **
4052900Seric **	Side Effects:
4062900Seric **		extracts what information it can from the header,
4073386Seric **		such as the date.
4082900Seric */
4092900Seric 
4104321Seric # ifndef NOTUNIX
4114321Seric 
4124203Seric char	*DowList[] =
4134203Seric {
4144203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
4154203Seric };
4164203Seric 
4172900Seric char	*MonthList[] =
4182900Seric {
4192900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4202900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4212900Seric 	NULL
4222900Seric };
4232900Seric 
42455012Seric eatfrom(fm, e)
4252900Seric 	char *fm;
42655012Seric 	register ENVELOPE *e;
4272900Seric {
4282900Seric 	register char *p;
4292900Seric 	register char **dt;
4302900Seric 
4317673Seric 	if (tTd(30, 2))
4324203Seric 		printf("eatfrom(%s)\n", fm);
4334203Seric 
4342900Seric 	/* find the date part */
4352900Seric 	p = fm;
4362900Seric 	while (*p != '\0')
4372900Seric 	{
4382900Seric 		/* skip a word */
4392900Seric 		while (*p != '\0' && *p != ' ')
44016896Seric 			p++;
4412900Seric 		while (*p == ' ')
44216896Seric 			p++;
44358050Seric 		if (!(isascii(*p) && isupper(*p)) ||
44458050Seric 		    p[3] != ' ' || p[13] != ':' || p[16] != ':')
4452900Seric 			continue;
4462900Seric 
4472900Seric 		/* we have a possible date */
4484203Seric 		for (dt = DowList; *dt != NULL; dt++)
4492900Seric 			if (strncmp(*dt, p, 3) == 0)
4502900Seric 				break;
4514203Seric 		if (*dt == NULL)
4524203Seric 			continue;
4532900Seric 
4544203Seric 		for (dt = MonthList; *dt != NULL; dt++)
4554203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
4564203Seric 				break;
4572900Seric 		if (*dt != NULL)
4582900Seric 			break;
4592900Seric 	}
4602900Seric 
46160502Seric 	if (*p != '\0')
4622900Seric 	{
4633386Seric 		char *q;
4645366Seric 		extern char *arpadate();
4653386Seric 
4662900Seric 		/* we have found a date */
4673386Seric 		q = xalloc(25);
46823103Seric 		(void) strncpy(q, p, 25);
4693386Seric 		q[24] = '\0';
4705366Seric 		q = arpadate(q);
47155012Seric 		define('a', newstr(q), e);
4722900Seric 	}
4732900Seric }
4744321Seric 
47556795Seric # endif /* NOTUNIX */
476