122697Sdist /*
234920Sbostic  * Copyright (c) 1983 Eric P. Allman
3*62522Sbostic  * Copyright (c) 1988, 1993
4*62522Sbostic  *	The Regents of the University of California.  All rights reserved.
533728Sbostic  *
642824Sbostic  * %sccs.include.redist.c%
733728Sbostic  */
822697Sdist 
922697Sdist #ifndef lint
10*62522Sbostic static char sccsid[] = "@(#)collect.c	8.1 (Berkeley) 06/07/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 
5655012Seric 	e->e_df = newstr(queuename(e, 'd'));
5759745Seric 	if ((tf = dfopen(e->e_df, O_WRONLY|O_CREAT, FileMode)) == NULL)
581392Seric 	{
5955012Seric 		syserr("Cannot create %s", e->e_df);
605366Seric 		NoReturn = TRUE;
615366Seric 		finis();
621392Seric 	}
631392Seric 
644316Seric 	/*
654322Seric 	**  Tell ARPANET to go ahead.
664322Seric 	*/
674322Seric 
6852105Seric 	if (smtpmode)
6958151Seric 		message("354 Enter mail, end with \".\" on a line by itself");
704322Seric 
714322Seric 	/*
724316Seric 	**  Try to read a UNIX-style From line
734316Seric 	*/
744316Seric 
7561093Seric 	if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock,
7661093Seric 			"initial message read") == NULL)
7740965Sbostic 		goto readerr;
784557Seric 	fixcrlf(buf, FALSE);
794321Seric # ifndef NOTUNIX
804322Seric 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
812900Seric 	{
8240965Sbostic 		if (!flusheol(buf, InChannel))
8340965Sbostic 			goto readerr;
8455012Seric 		eatfrom(buf, e);
8561093Seric 		if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock,
8661093Seric 				"message header read") == NULL)
8740965Sbostic 			goto readerr;
884557Seric 		fixcrlf(buf, FALSE);
892900Seric 	}
9056795Seric # endif /* NOTUNIX */
912900Seric 
921392Seric 	/*
935975Seric 	**  Copy InChannel to temp file & do message editing.
941392Seric 	**	To keep certain mailers from getting confused,
951392Seric 	**	and to keep the output clean, lines that look
9613932Seric 	**	like UNIX "From" lines are deleted in the header.
971392Seric 	*/
981392Seric 
9940965Sbostic 	workbuf = buf;		/* `workbuf' contains a header field */
10040965Sbostic 	freebuf = buf2;		/* `freebuf' can be used for read-ahead */
10140965Sbostic 	for (;;)
1021392Seric 	{
10357135Seric 		char *curbuf;
10457135Seric 		int curbuffree;
10557135Seric 		register int curbuflen;
10657135Seric 		char *p;
10757135Seric 
10840965Sbostic 		/* first, see if the header is over */
10940965Sbostic 		if (!isheader(workbuf))
11040965Sbostic 		{
11140965Sbostic 			fixcrlf(workbuf, TRUE);
11219036Seric 			break;
11340965Sbostic 		}
11419036Seric 
1157681Seric 		/* if the line is too long, throw the rest away */
11640965Sbostic 		if (!flusheol(workbuf, InChannel))
11740965Sbostic 			goto readerr;
1187681Seric 
11940965Sbostic 		/* it's okay to toss '\n' now (flusheol() needed it) */
12040965Sbostic 		fixcrlf(workbuf, TRUE);
1214557Seric 
12257135Seric 		curbuf = workbuf;
12357135Seric 		curbuflen = strlen(curbuf);
12457135Seric 		curbuffree = MAXLINE - curbuflen;
12557135Seric 		p = curbuf + curbuflen;
1262900Seric 
1272900Seric 		/* get the rest of this field */
12840965Sbostic 		for (;;)
1291392Seric 		{
13057135Seric 			int clen;
13157135Seric 
13261093Seric 			if (sfgets(freebuf, MAXLINE, InChannel,
13361093Seric 					TimeOuts.to_datablock,
13461093Seric 					"message header read") == NULL)
13540965Sbostic 				goto readerr;
13640965Sbostic 
13740965Sbostic 			/* is this a continuation line? */
13840965Sbostic 			if (*freebuf != ' ' && *freebuf != '\t')
1392900Seric 				break;
14040965Sbostic 
14140965Sbostic 			if (!flusheol(freebuf, InChannel))
14240965Sbostic 				goto readerr;
14340965Sbostic 
14457135Seric 			fixcrlf(freebuf, TRUE);
14557135Seric 			clen = strlen(freebuf) + 1;
14657135Seric 
14757135Seric 			/* if insufficient room, dynamically allocate buffer */
14857135Seric 			if (clen >= curbuffree)
14940965Sbostic 			{
15057135Seric 				/* reallocate buffer */
15157135Seric 				int nbuflen = ((p - curbuf) + clen) * 2;
15257135Seric 				char *nbuf = xalloc(nbuflen);
15340965Sbostic 
15457135Seric 				p = nbuf + curbuflen;
15557135Seric 				curbuffree = nbuflen - curbuflen;
15657135Seric 				bcopy(curbuf, nbuf, curbuflen);
15757135Seric 				if (curbuf != buf && curbuf != buf2)
15857135Seric 					free(curbuf);
15957135Seric 				curbuf = nbuf;
16040965Sbostic 			}
16157135Seric 			*p++ = '\n';
16257135Seric 			bcopy(freebuf, p, clen - 1);
16357135Seric 			p += clen - 1;
16457135Seric 			curbuffree -= clen;
16557135Seric 			curbuflen += clen;
1661392Seric 		}
16757135Seric 		*p++ = '\0';
1681392Seric 
16957135Seric 		e->e_msgsize += curbuflen;
1701392Seric 
1712900Seric 		/*
17240965Sbostic 		**  The working buffer now becomes the free buffer, since
17340965Sbostic 		**  the free buffer contains a new header field.
17440965Sbostic 		**
17540965Sbostic 		**  This is premature, since we still havent called
17640965Sbostic 		**  chompheader() to process the field we just created
17740965Sbostic 		**  (so the call to chompheader() will use `freebuf').
17840965Sbostic 		**  This convolution is necessary so that if we break out
17940965Sbostic 		**  of the loop due to H_EOH, `workbuf' will always be
18040965Sbostic 		**  the next unprocessed buffer.
18140965Sbostic 		*/
18240965Sbostic 
18340965Sbostic 		{
18440965Sbostic 			register char *tmp = workbuf;
18540965Sbostic 			workbuf = freebuf;
18640965Sbostic 			freebuf = tmp;
18740965Sbostic 		}
18840965Sbostic 
18940965Sbostic 		/*
1902900Seric 		**  Snarf header away.
1912900Seric 		*/
1922900Seric 
19357135Seric 		if (bitset(H_EOH, chompheader(curbuf, FALSE, e)))
1943058Seric 			break;
19557135Seric 
19657135Seric 		/*
19757135Seric 		**  If the buffer was dynamically allocated, free it.
19857135Seric 		*/
19957135Seric 
20057135Seric 		if (curbuf != buf && curbuf != buf2)
20157135Seric 			free(curbuf);
20240965Sbostic 	}
2031392Seric 
2047673Seric 	if (tTd(30, 1))
2052900Seric 		printf("EOH\n");
2062900Seric 
20740965Sbostic 	if (*workbuf == '\0')
20840965Sbostic 	{
20940965Sbostic 		/* throw away a blank line */
21061093Seric 		if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock,
21161093Seric 				"message separator read") == NULL)
21240965Sbostic 			goto readerr;
21340965Sbostic 	}
21440965Sbostic 	else if (workbuf == buf2)	/* guarantee `buf' contains data */
21540965Sbostic 		(void) strcpy(buf, buf2);
2162900Seric 
2172900Seric 	/*
2182900Seric 	**  Collect the body of the message.
2192900Seric 	*/
2202900Seric 
22115532Seric 	do
2222900Seric 	{
2234551Seric 		register char *bp = buf;
2244156Seric 
2257852Seric 		fixcrlf(buf, TRUE);
2264557Seric 
2272900Seric 		/* check for end-of-message */
22852105Seric 		if (!ignrdot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
2292900Seric 			break;
2302900Seric 
2314551Seric 		/* check for transparent dot */
23252105Seric 		if (OpMode == MD_SMTP && bp[0] == '.' && bp[1] == '.')
2334551Seric 			bp++;
2344551Seric 
2354156Seric 		/*
2364156Seric 		**  Figure message length, output the line to the temp
2374156Seric 		**  file, and insert a newline if missing.
2384156Seric 		*/
2394156Seric 
24055012Seric 		e->e_msgsize += strlen(bp) + 1;
2414551Seric 		fputs(bp, tf);
2427852Seric 		fputs("\n", tf);
2431392Seric 		if (ferror(tf))
24455012Seric 			tferror(tf, e);
24561093Seric 	} while (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock,
24661093Seric 			"message body read") != NULL);
24740965Sbostic 
24840965Sbostic readerr:
24911544Seric 	if (fflush(tf) != 0)
25055012Seric 		tferror(tf, e);
25160603Seric 	(void) fsync(fileno(tf));
2524083Seric 	(void) fclose(tf);
2532900Seric 
25411145Seric 	/* An EOF when running SMTP is an error */
25519036Seric 	if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP)
25616136Seric 	{
25758308Seric 		char *host;
25858082Seric 
25958308Seric 		host = RealHostName;
26058308Seric 		if (host == NULL)
26158308Seric 			host = "localhost";
26258308Seric 
26336233Skarels # ifdef LOG
26458308Seric 		if (LogLevel > 0 && feof(InChannel))
26536230Skarels 			syslog(LOG_NOTICE,
26658308Seric 			    "collect: unexpected close on connection from %s, sender=%s: %m\n",
26758308Seric 			    host, e->e_from.q_paddr);
26836233Skarels # endif
26958082Seric 		(feof(InChannel) ? usrerr : syserr)
27058308Seric 			("451 collect: unexpected close on connection from %s, from=%s",
27158308Seric 				host, e->e_from.q_paddr);
27211145Seric 
27316136Seric 		/* don't return an error indication */
27455012Seric 		e->e_to = NULL;
27555012Seric 		e->e_flags &= ~EF_FATALERRS;
27616136Seric 
27716136Seric 		/* and don't try to deliver the partial message either */
27816136Seric 		finis();
27916136Seric 	}
28016136Seric 
2812900Seric 	/*
2822900Seric 	**  Find out some information from the headers.
2833386Seric 	**	Examples are who is the from person & the date.
2842900Seric 	*/
2852900Seric 
28658929Seric 	eatheader(e, !requeueflag);
2877673Seric 
2887782Seric 	/*
2897782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
2907782Seric 	*/
2914622Seric 
29255012Seric 	if (hvalue("to", e) == NULL && hvalue("cc", e) == NULL &&
29355012Seric 	    hvalue("bcc", e) == NULL && hvalue("apparently-to", e) == NULL)
2947367Seric 	{
2957367Seric 		register ADDRESS *q;
2967367Seric 
2977367Seric 		/* create an Apparently-To: field */
2987367Seric 		/*    that or reject the message.... */
29955012Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
3007367Seric 		{
3017389Seric 			if (q->q_alias != NULL)
3027389Seric 				continue;
3037673Seric 			if (tTd(30, 3))
3047367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
30559579Seric 			addheader("Apparently-To", q->q_paddr, e);
3067367Seric 		}
3077367Seric 	}
3087367Seric 
30959320Seric 	/* check for message too large */
31059320Seric 	if (MaxMessageSize > 0 && e->e_msgsize > MaxMessageSize)
31159320Seric 	{
31259320Seric 		usrerr("552 Message exceeds maximum fixed size (%ld)",
31359320Seric 			MaxMessageSize);
31459320Seric 	}
31559320Seric 
31655012Seric 	if ((e->e_dfp = fopen(e->e_df, "r")) == NULL)
31758690Seric 	{
31858690Seric 		/* we haven't acked receipt yet, so just chuck this */
31955012Seric 		syserr("Cannot reopen %s", e->e_df);
32058690Seric 		finis();
32158690Seric 	}
3221392Seric }
3231392Seric /*
32440965Sbostic **  FLUSHEOL -- if not at EOL, throw away rest of input line.
32540965Sbostic **
32640965Sbostic **	Parameters:
32740965Sbostic **		buf -- last line read in (checked for '\n'),
32840965Sbostic **		fp -- file to be read from.
32940965Sbostic **
33040965Sbostic **	Returns:
33140965Sbostic **		FALSE on error from sfgets(), TRUE otherwise.
33240965Sbostic **
33340965Sbostic **	Side Effects:
33440965Sbostic **		none.
33540965Sbostic */
33640965Sbostic 
33740965Sbostic bool
33840965Sbostic flusheol(buf, fp)
33940965Sbostic 	char *buf;
34040965Sbostic 	FILE *fp;
34140965Sbostic {
34240965Sbostic 	register char *p = buf;
34357134Seric 	bool printmsg = TRUE;
34457134Seric 	char junkbuf[MAXLINE];
34540965Sbostic 
34657134Seric 	while (strchr(p, '\n') == NULL)
34757134Seric 	{
34857134Seric 		if (printmsg)
34958151Seric 			usrerr("553 header line too long");
35057134Seric 		printmsg = FALSE;
35161093Seric 		if (sfgets(junkbuf, MAXLINE, fp, TimeOuts.to_datablock,
35261093Seric 				"long line flush") == NULL)
35357134Seric 			return (FALSE);
35440965Sbostic 		p = junkbuf;
35540965Sbostic 	}
35640965Sbostic 
35757134Seric 	return (TRUE);
35840965Sbostic }
35940965Sbostic /*
36011544Seric **  TFERROR -- signal error on writing the temporary file.
36111544Seric **
36211544Seric **	Parameters:
36311544Seric **		tf -- the file pointer for the temporary file.
36411544Seric **
36511544Seric **	Returns:
36611544Seric **		none.
36711544Seric **
36811544Seric **	Side Effects:
36911544Seric **		Gives an error message.
37011544Seric **		Arranges for following output to go elsewhere.
37111544Seric */
37211544Seric 
37355012Seric tferror(tf, e)
37411544Seric 	FILE *tf;
37555012Seric 	register ENVELOPE *e;
37611544Seric {
37711544Seric 	if (errno == ENOSPC)
37811544Seric 	{
37955012Seric 		(void) freopen(e->e_df, "w", tf);
38011544Seric 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
38111544Seric 		usrerr("452 Out of disk space for temp file");
38211544Seric 	}
38311544Seric 	else
38455012Seric 		syserr("collect: Cannot write %s", e->e_df);
38511544Seric 	(void) freopen("/dev/null", "w", tf);
38611544Seric }
38711544Seric /*
3882900Seric **  EATFROM -- chew up a UNIX style from line and process
3892900Seric **
3902900Seric **	This does indeed make some assumptions about the format
3912900Seric **	of UNIX messages.
3922900Seric **
3932900Seric **	Parameters:
3942900Seric **		fm -- the from line.
3952900Seric **
3962900Seric **	Returns:
3972900Seric **		none.
3982900Seric **
3992900Seric **	Side Effects:
4002900Seric **		extracts what information it can from the header,
4013386Seric **		such as the date.
4022900Seric */
4032900Seric 
4044321Seric # ifndef NOTUNIX
4054321Seric 
4064203Seric char	*DowList[] =
4074203Seric {
4084203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
4094203Seric };
4104203Seric 
4112900Seric char	*MonthList[] =
4122900Seric {
4132900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4142900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4152900Seric 	NULL
4162900Seric };
4172900Seric 
41855012Seric eatfrom(fm, e)
4192900Seric 	char *fm;
42055012Seric 	register ENVELOPE *e;
4212900Seric {
4222900Seric 	register char *p;
4232900Seric 	register char **dt;
4242900Seric 
4257673Seric 	if (tTd(30, 2))
4264203Seric 		printf("eatfrom(%s)\n", fm);
4274203Seric 
4282900Seric 	/* find the date part */
4292900Seric 	p = fm;
4302900Seric 	while (*p != '\0')
4312900Seric 	{
4322900Seric 		/* skip a word */
4332900Seric 		while (*p != '\0' && *p != ' ')
43416896Seric 			p++;
4352900Seric 		while (*p == ' ')
43616896Seric 			p++;
43758050Seric 		if (!(isascii(*p) && isupper(*p)) ||
43858050Seric 		    p[3] != ' ' || p[13] != ':' || p[16] != ':')
4392900Seric 			continue;
4402900Seric 
4412900Seric 		/* we have a possible date */
4424203Seric 		for (dt = DowList; *dt != NULL; dt++)
4432900Seric 			if (strncmp(*dt, p, 3) == 0)
4442900Seric 				break;
4454203Seric 		if (*dt == NULL)
4464203Seric 			continue;
4472900Seric 
4484203Seric 		for (dt = MonthList; *dt != NULL; dt++)
4494203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
4504203Seric 				break;
4512900Seric 		if (*dt != NULL)
4522900Seric 			break;
4532900Seric 	}
4542900Seric 
45560502Seric 	if (*p != '\0')
4562900Seric 	{
4573386Seric 		char *q;
4585366Seric 		extern char *arpadate();
4593386Seric 
4602900Seric 		/* we have found a date */
4613386Seric 		q = xalloc(25);
46223103Seric 		(void) strncpy(q, p, 25);
4633386Seric 		q[24] = '\0';
4645366Seric 		q = arpadate(q);
46555012Seric 		define('a', newstr(q), e);
4662900Seric 	}
4672900Seric }
4684321Seric 
46956795Seric # endif /* NOTUNIX */
470