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*58929Seric static char sccsid[] = "@(#)collect.c	6.13 (Berkeley) 04/01/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.
28*58929Seric **		requeueflag -- this message will be requeued later, so
29*58929Seric **			don't do final processing on it.
30*58929Seric **		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 
40*58929Seric collect(smtpmode, requeueflag, e)
4152105Seric 	bool smtpmode;
42*58929Seric 	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'));
5755012Seric 	if ((tf = dfopen(e->e_df, "w")) == NULL)
581392Seric 	{
5955012Seric 		syserr("Cannot create %s", e->e_df);
605366Seric 		NoReturn = TRUE;
615366Seric 		finis();
621392Seric 	}
6355012Seric 	(void) chmod(e->e_df, FileMode);
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 
7658112Seric 	if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock) == 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);
8558112Seric 		if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock) == NULL)
8640965Sbostic 			goto readerr;
874557Seric 		fixcrlf(buf, FALSE);
882900Seric 	}
8956795Seric # endif /* NOTUNIX */
902900Seric 
911392Seric 	/*
925975Seric 	**  Copy InChannel to temp file & do message editing.
931392Seric 	**	To keep certain mailers from getting confused,
941392Seric 	**	and to keep the output clean, lines that look
9513932Seric 	**	like UNIX "From" lines are deleted in the header.
961392Seric 	*/
971392Seric 
9840965Sbostic 	workbuf = buf;		/* `workbuf' contains a header field */
9940965Sbostic 	freebuf = buf2;		/* `freebuf' can be used for read-ahead */
10040965Sbostic 	for (;;)
1011392Seric 	{
10257135Seric 		char *curbuf;
10357135Seric 		int curbuffree;
10457135Seric 		register int curbuflen;
10557135Seric 		char *p;
10657135Seric 
10740965Sbostic 		/* first, see if the header is over */
10840965Sbostic 		if (!isheader(workbuf))
10940965Sbostic 		{
11040965Sbostic 			fixcrlf(workbuf, TRUE);
11119036Seric 			break;
11240965Sbostic 		}
11319036Seric 
1147681Seric 		/* if the line is too long, throw the rest away */
11540965Sbostic 		if (!flusheol(workbuf, InChannel))
11640965Sbostic 			goto readerr;
1177681Seric 
11840965Sbostic 		/* it's okay to toss '\n' now (flusheol() needed it) */
11940965Sbostic 		fixcrlf(workbuf, TRUE);
1204557Seric 
12157135Seric 		curbuf = workbuf;
12257135Seric 		curbuflen = strlen(curbuf);
12357135Seric 		curbuffree = MAXLINE - curbuflen;
12457135Seric 		p = curbuf + curbuflen;
1252900Seric 
1262900Seric 		/* get the rest of this field */
12740965Sbostic 		for (;;)
1281392Seric 		{
12957135Seric 			int clen;
13057135Seric 
13158112Seric 			if (sfgets(freebuf, MAXLINE, InChannel, TimeOuts.to_datablock) == NULL)
13240965Sbostic 				goto readerr;
13340965Sbostic 
13440965Sbostic 			/* is this a continuation line? */
13540965Sbostic 			if (*freebuf != ' ' && *freebuf != '\t')
1362900Seric 				break;
13740965Sbostic 
13840965Sbostic 			if (!flusheol(freebuf, InChannel))
13940965Sbostic 				goto readerr;
14040965Sbostic 
14157135Seric 			fixcrlf(freebuf, TRUE);
14257135Seric 			clen = strlen(freebuf) + 1;
14357135Seric 
14457135Seric 			/* if insufficient room, dynamically allocate buffer */
14557135Seric 			if (clen >= curbuffree)
14640965Sbostic 			{
14757135Seric 				/* reallocate buffer */
14857135Seric 				int nbuflen = ((p - curbuf) + clen) * 2;
14957135Seric 				char *nbuf = xalloc(nbuflen);
15040965Sbostic 
15157135Seric 				p = nbuf + curbuflen;
15257135Seric 				curbuffree = nbuflen - curbuflen;
15357135Seric 				bcopy(curbuf, nbuf, curbuflen);
15457135Seric 				if (curbuf != buf && curbuf != buf2)
15557135Seric 					free(curbuf);
15657135Seric 				curbuf = nbuf;
15740965Sbostic 			}
15857135Seric 			*p++ = '\n';
15957135Seric 			bcopy(freebuf, p, clen - 1);
16057135Seric 			p += clen - 1;
16157135Seric 			curbuffree -= clen;
16257135Seric 			curbuflen += clen;
1631392Seric 		}
16457135Seric 		*p++ = '\0';
1651392Seric 
16657135Seric 		e->e_msgsize += curbuflen;
1671392Seric 
1682900Seric 		/*
16940965Sbostic 		**  The working buffer now becomes the free buffer, since
17040965Sbostic 		**  the free buffer contains a new header field.
17140965Sbostic 		**
17240965Sbostic 		**  This is premature, since we still havent called
17340965Sbostic 		**  chompheader() to process the field we just created
17440965Sbostic 		**  (so the call to chompheader() will use `freebuf').
17540965Sbostic 		**  This convolution is necessary so that if we break out
17640965Sbostic 		**  of the loop due to H_EOH, `workbuf' will always be
17740965Sbostic 		**  the next unprocessed buffer.
17840965Sbostic 		*/
17940965Sbostic 
18040965Sbostic 		{
18140965Sbostic 			register char *tmp = workbuf;
18240965Sbostic 			workbuf = freebuf;
18340965Sbostic 			freebuf = tmp;
18440965Sbostic 		}
18540965Sbostic 
18640965Sbostic 		/*
1872900Seric 		**  Snarf header away.
1882900Seric 		*/
1892900Seric 
19057135Seric 		if (bitset(H_EOH, chompheader(curbuf, FALSE, e)))
1913058Seric 			break;
19257135Seric 
19357135Seric 		/*
19457135Seric 		**  If the buffer was dynamically allocated, free it.
19557135Seric 		*/
19657135Seric 
19757135Seric 		if (curbuf != buf && curbuf != buf2)
19857135Seric 			free(curbuf);
19940965Sbostic 	}
2001392Seric 
2017673Seric 	if (tTd(30, 1))
2022900Seric 		printf("EOH\n");
2032900Seric 
20440965Sbostic 	if (*workbuf == '\0')
20540965Sbostic 	{
20640965Sbostic 		/* throw away a blank line */
20758112Seric 		if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock) == NULL)
20840965Sbostic 			goto readerr;
20940965Sbostic 	}
21040965Sbostic 	else if (workbuf == buf2)	/* guarantee `buf' contains data */
21140965Sbostic 		(void) strcpy(buf, buf2);
2122900Seric 
2132900Seric 	/*
2142900Seric 	**  Collect the body of the message.
2152900Seric 	*/
2162900Seric 
21715532Seric 	do
2182900Seric 	{
2194551Seric 		register char *bp = buf;
2204156Seric 
2217852Seric 		fixcrlf(buf, TRUE);
2224557Seric 
2232900Seric 		/* check for end-of-message */
22452105Seric 		if (!ignrdot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
2252900Seric 			break;
2262900Seric 
2274551Seric 		/* check for transparent dot */
22852105Seric 		if (OpMode == MD_SMTP && bp[0] == '.' && bp[1] == '.')
2294551Seric 			bp++;
2304551Seric 
2314156Seric 		/*
2324156Seric 		**  Figure message length, output the line to the temp
2334156Seric 		**  file, and insert a newline if missing.
2344156Seric 		*/
2354156Seric 
23655012Seric 		e->e_msgsize += strlen(bp) + 1;
2374551Seric 		fputs(bp, tf);
2387852Seric 		fputs("\n", tf);
2391392Seric 		if (ferror(tf))
24055012Seric 			tferror(tf, e);
24158112Seric 	} while (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock) != NULL);
24240965Sbostic 
24340965Sbostic readerr:
24411544Seric 	if (fflush(tf) != 0)
24555012Seric 		tferror(tf, e);
2464083Seric 	(void) fclose(tf);
2472900Seric 
24811145Seric 	/* An EOF when running SMTP is an error */
24919036Seric 	if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP)
25016136Seric 	{
25158308Seric 		char *host;
25258082Seric 
25358308Seric 		host = RealHostName;
25458308Seric 		if (host == NULL)
25558308Seric 			host = "localhost";
25658308Seric 
25736233Skarels # ifdef LOG
25858308Seric 		if (LogLevel > 0 && feof(InChannel))
25936230Skarels 			syslog(LOG_NOTICE,
26058308Seric 			    "collect: unexpected close on connection from %s, sender=%s: %m\n",
26158308Seric 			    host, e->e_from.q_paddr);
26236233Skarels # endif
26358082Seric 		(feof(InChannel) ? usrerr : syserr)
26458308Seric 			("451 collect: unexpected close on connection from %s, from=%s",
26558308Seric 				host, e->e_from.q_paddr);
26611145Seric 
26716136Seric 		/* don't return an error indication */
26855012Seric 		e->e_to = NULL;
26955012Seric 		e->e_flags &= ~EF_FATALERRS;
27016136Seric 
27116136Seric 		/* and don't try to deliver the partial message either */
27216136Seric 		finis();
27316136Seric 	}
27416136Seric 
2752900Seric 	/*
2762900Seric 	**  Find out some information from the headers.
2773386Seric 	**	Examples are who is the from person & the date.
2782900Seric 	*/
2792900Seric 
280*58929Seric 	eatheader(e, !requeueflag);
2817673Seric 
2827782Seric 	/*
2837782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
2847782Seric 	*/
2854622Seric 
28655012Seric 	if (hvalue("to", e) == NULL && hvalue("cc", e) == NULL &&
28755012Seric 	    hvalue("bcc", e) == NULL && hvalue("apparently-to", e) == NULL)
2887367Seric 	{
2897367Seric 		register ADDRESS *q;
2907367Seric 
2917367Seric 		/* create an Apparently-To: field */
2927367Seric 		/*    that or reject the message.... */
29355012Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
2947367Seric 		{
2957389Seric 			if (q->q_alias != NULL)
2967389Seric 				continue;
2977673Seric 			if (tTd(30, 3))
2987367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
29955012Seric 			addheader("apparently-to", q->q_paddr, e);
3007367Seric 		}
3017367Seric 	}
3027367Seric 
30355012Seric 	if ((e->e_dfp = fopen(e->e_df, "r")) == NULL)
30458690Seric 	{
30558690Seric 		/* we haven't acked receipt yet, so just chuck this */
30655012Seric 		syserr("Cannot reopen %s", e->e_df);
30758690Seric 		finis();
30858690Seric 	}
3091392Seric }
3101392Seric /*
31140965Sbostic **  FLUSHEOL -- if not at EOL, throw away rest of input line.
31240965Sbostic **
31340965Sbostic **	Parameters:
31440965Sbostic **		buf -- last line read in (checked for '\n'),
31540965Sbostic **		fp -- file to be read from.
31640965Sbostic **
31740965Sbostic **	Returns:
31840965Sbostic **		FALSE on error from sfgets(), TRUE otherwise.
31940965Sbostic **
32040965Sbostic **	Side Effects:
32140965Sbostic **		none.
32240965Sbostic */
32340965Sbostic 
32440965Sbostic bool
32540965Sbostic flusheol(buf, fp)
32640965Sbostic 	char *buf;
32740965Sbostic 	FILE *fp;
32840965Sbostic {
32940965Sbostic 	register char *p = buf;
33057134Seric 	bool printmsg = TRUE;
33157134Seric 	char junkbuf[MAXLINE];
33257134Seric 	extern char *sfgets();
33340965Sbostic 
33457134Seric 	while (strchr(p, '\n') == NULL)
33557134Seric 	{
33657134Seric 		if (printmsg)
33758151Seric 			usrerr("553 header line too long");
33857134Seric 		printmsg = FALSE;
33958112Seric 		if (sfgets(junkbuf, MAXLINE, fp, TimeOuts.to_datablock) == NULL)
34057134Seric 			return (FALSE);
34140965Sbostic 		p = junkbuf;
34240965Sbostic 	}
34340965Sbostic 
34457134Seric 	return (TRUE);
34540965Sbostic }
34640965Sbostic /*
34711544Seric **  TFERROR -- signal error on writing the temporary file.
34811544Seric **
34911544Seric **	Parameters:
35011544Seric **		tf -- the file pointer for the temporary file.
35111544Seric **
35211544Seric **	Returns:
35311544Seric **		none.
35411544Seric **
35511544Seric **	Side Effects:
35611544Seric **		Gives an error message.
35711544Seric **		Arranges for following output to go elsewhere.
35811544Seric */
35911544Seric 
36055012Seric tferror(tf, e)
36111544Seric 	FILE *tf;
36255012Seric 	register ENVELOPE *e;
36311544Seric {
36411544Seric 	if (errno == ENOSPC)
36511544Seric 	{
36655012Seric 		(void) freopen(e->e_df, "w", tf);
36711544Seric 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
36811544Seric 		usrerr("452 Out of disk space for temp file");
36911544Seric 	}
37011544Seric 	else
37155012Seric 		syserr("collect: Cannot write %s", e->e_df);
37211544Seric 	(void) freopen("/dev/null", "w", tf);
37311544Seric }
37411544Seric /*
3752900Seric **  EATFROM -- chew up a UNIX style from line and process
3762900Seric **
3772900Seric **	This does indeed make some assumptions about the format
3782900Seric **	of UNIX messages.
3792900Seric **
3802900Seric **	Parameters:
3812900Seric **		fm -- the from line.
3822900Seric **
3832900Seric **	Returns:
3842900Seric **		none.
3852900Seric **
3862900Seric **	Side Effects:
3872900Seric **		extracts what information it can from the header,
3883386Seric **		such as the date.
3892900Seric */
3902900Seric 
3914321Seric # ifndef NOTUNIX
3924321Seric 
3934203Seric char	*DowList[] =
3944203Seric {
3954203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
3964203Seric };
3974203Seric 
3982900Seric char	*MonthList[] =
3992900Seric {
4002900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
4012900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
4022900Seric 	NULL
4032900Seric };
4042900Seric 
40555012Seric eatfrom(fm, e)
4062900Seric 	char *fm;
40755012Seric 	register ENVELOPE *e;
4082900Seric {
4092900Seric 	register char *p;
4102900Seric 	register char **dt;
4112900Seric 
4127673Seric 	if (tTd(30, 2))
4134203Seric 		printf("eatfrom(%s)\n", fm);
4144203Seric 
4152900Seric 	/* find the date part */
4162900Seric 	p = fm;
4172900Seric 	while (*p != '\0')
4182900Seric 	{
4192900Seric 		/* skip a word */
4202900Seric 		while (*p != '\0' && *p != ' ')
42116896Seric 			p++;
4222900Seric 		while (*p == ' ')
42316896Seric 			p++;
42458050Seric 		if (!(isascii(*p) && isupper(*p)) ||
42558050Seric 		    p[3] != ' ' || p[13] != ':' || p[16] != ':')
4262900Seric 			continue;
4272900Seric 
4282900Seric 		/* we have a possible date */
4294203Seric 		for (dt = DowList; *dt != NULL; dt++)
4302900Seric 			if (strncmp(*dt, p, 3) == 0)
4312900Seric 				break;
4324203Seric 		if (*dt == NULL)
4334203Seric 			continue;
4342900Seric 
4354203Seric 		for (dt = MonthList; *dt != NULL; dt++)
4364203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
4374203Seric 				break;
4382900Seric 		if (*dt != NULL)
4392900Seric 			break;
4402900Seric 	}
4412900Seric 
4422900Seric 	if (*p != NULL)
4432900Seric 	{
4443386Seric 		char *q;
4455366Seric 		extern char *arpadate();
4463386Seric 
4472900Seric 		/* we have found a date */
4483386Seric 		q = xalloc(25);
44923103Seric 		(void) strncpy(q, p, 25);
4503386Seric 		q[24] = '\0';
4515366Seric 		q = arpadate(q);
45255012Seric 		define('a', newstr(q), e);
4532900Seric 	}
4542900Seric }
4554321Seric 
45656795Seric # endif /* NOTUNIX */
457