xref: /freebsd/contrib/sendmail/src/collect.c (revision d39bd2c1388b520fcba9abed1932acacead60fba)
1c2aa98e2SPeter Wemm /*
2*d39bd2c1SGregory Neil Shapiro  * Copyright (c) 1998-2006, 2008, 2023, 2024 Proofpoint, Inc. and its suppliers.
306f25ae9SGregory Neil Shapiro  *	All rights reserved.
4c2aa98e2SPeter Wemm  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
5c2aa98e2SPeter Wemm  * Copyright (c) 1988, 1993
6c2aa98e2SPeter Wemm  *	The Regents of the University of California.  All rights reserved.
7c2aa98e2SPeter Wemm  *
8c2aa98e2SPeter Wemm  * By using this file, you agree to the terms and conditions set
9c2aa98e2SPeter Wemm  * forth in the LICENSE file which can be found at the top level of
10c2aa98e2SPeter Wemm  * the sendmail distribution.
11c2aa98e2SPeter Wemm  *
12c2aa98e2SPeter Wemm  */
13c2aa98e2SPeter Wemm 
1406f25ae9SGregory Neil Shapiro #include <sendmail.h>
15c2aa98e2SPeter Wemm 
164313cc83SGregory Neil Shapiro SM_RCSID("@(#)$Id: collect.c,v 8.287 2013-11-22 20:51:55 ca Exp $")
1706f25ae9SGregory Neil Shapiro 
182fb4f839SGregory Neil Shapiro #include <sm/sendmail.h>
192fb4f839SGregory Neil Shapiro 
2006f25ae9SGregory Neil Shapiro static void	eatfrom __P((char *volatile, ENVELOPE *));
2140266059SGregory Neil Shapiro static void	collect_doheader __P((ENVELOPE *));
2240266059SGregory Neil Shapiro static SM_FILE_T *collect_dfopen __P((ENVELOPE *));
2340266059SGregory Neil Shapiro static SM_FILE_T *collect_eoh __P((ENVELOPE *, int, int));
2406f25ae9SGregory Neil Shapiro 
2540266059SGregory Neil Shapiro /*
2640266059SGregory Neil Shapiro **  COLLECT_EOH -- end-of-header processing in collect()
2740266059SGregory Neil Shapiro **
2840266059SGregory Neil Shapiro **	Called by collect() when it encounters the blank line
2940266059SGregory Neil Shapiro **	separating the header from the message body, or when it
3040266059SGregory Neil Shapiro **	encounters EOF in a message that contains only a header.
3140266059SGregory Neil Shapiro **
3240266059SGregory Neil Shapiro **	Parameters:
3340266059SGregory Neil Shapiro **		e -- envelope
3440266059SGregory Neil Shapiro **		numhdrs -- number of headers
3540266059SGregory Neil Shapiro **		hdrslen -- length of headers
3640266059SGregory Neil Shapiro **
37*d39bd2c1SGregory Neil Shapiro **	Returns:
3840266059SGregory Neil Shapiro **		NULL, or handle to open data file
3940266059SGregory Neil Shapiro **
4040266059SGregory Neil Shapiro **	Side Effects:
4140266059SGregory Neil Shapiro **		end-of-header check ruleset is invoked.
4240266059SGregory Neil Shapiro **		envelope state is updated.
4340266059SGregory Neil Shapiro **		headers may be added and deleted.
4440266059SGregory Neil Shapiro **		selects the queue.
4540266059SGregory Neil Shapiro **		opens the data file.
4640266059SGregory Neil Shapiro */
4740266059SGregory Neil Shapiro 
4840266059SGregory Neil Shapiro static SM_FILE_T *
collect_eoh(e,numhdrs,hdrslen)4940266059SGregory Neil Shapiro collect_eoh(e, numhdrs, hdrslen)
5040266059SGregory Neil Shapiro 	ENVELOPE *e;
5140266059SGregory Neil Shapiro 	int numhdrs;
5240266059SGregory Neil Shapiro 	int hdrslen;
5340266059SGregory Neil Shapiro {
5440266059SGregory Neil Shapiro 	char hnum[16];
5540266059SGregory Neil Shapiro 	char hsize[16];
5640266059SGregory Neil Shapiro 
5740266059SGregory Neil Shapiro 	/* call the end-of-header check ruleset */
58d0cef73dSGregory Neil Shapiro 	(void) sm_snprintf(hnum, sizeof(hnum), "%d", numhdrs);
59d0cef73dSGregory Neil Shapiro 	(void) sm_snprintf(hsize, sizeof(hsize), "%d", hdrslen);
6040266059SGregory Neil Shapiro 	if (tTd(30, 10))
6140266059SGregory Neil Shapiro 		sm_dprintf("collect: rscheck(\"check_eoh\", \"%s $| %s\")\n",
6240266059SGregory Neil Shapiro 			   hnum, hsize);
63959366dcSGregory Neil Shapiro 	(void) rscheck("check_eoh", hnum, hsize, e, RSF_UNSTRUCTURED|RSF_COUNT,
64da7d7b9cSGregory Neil Shapiro 			3, NULL, e->e_id, NULL, NULL);
6540266059SGregory Neil Shapiro 
6640266059SGregory Neil Shapiro 	/*
6740266059SGregory Neil Shapiro 	**  Process the header,
6840266059SGregory Neil Shapiro 	**  select the queue, open the data file.
6940266059SGregory Neil Shapiro 	*/
7040266059SGregory Neil Shapiro 
7140266059SGregory Neil Shapiro 	collect_doheader(e);
7240266059SGregory Neil Shapiro 	return collect_dfopen(e);
7340266059SGregory Neil Shapiro }
7440266059SGregory Neil Shapiro 
7540266059SGregory Neil Shapiro /*
7640266059SGregory Neil Shapiro **  COLLECT_DOHEADER -- process header in collect()
7740266059SGregory Neil Shapiro **
7840266059SGregory Neil Shapiro **	Called by collect() after it has finished parsing the header,
7940266059SGregory Neil Shapiro **	but before it selects the queue and creates the data file.
8040266059SGregory Neil Shapiro **	The results of processing the header will affect queue selection.
8140266059SGregory Neil Shapiro **
8240266059SGregory Neil Shapiro **	Parameters:
8340266059SGregory Neil Shapiro **		e -- envelope
8440266059SGregory Neil Shapiro **
85*d39bd2c1SGregory Neil Shapiro **	Returns:
8640266059SGregory Neil Shapiro **		none.
8740266059SGregory Neil Shapiro **
8840266059SGregory Neil Shapiro **	Side Effects:
8940266059SGregory Neil Shapiro **		envelope state is updated.
9040266059SGregory Neil Shapiro **		headers may be added and deleted.
9140266059SGregory Neil Shapiro */
9240266059SGregory Neil Shapiro 
9340266059SGregory Neil Shapiro static void
collect_doheader(e)9440266059SGregory Neil Shapiro collect_doheader(e)
9540266059SGregory Neil Shapiro 	ENVELOPE *e;
9640266059SGregory Neil Shapiro {
9740266059SGregory Neil Shapiro 	/*
9840266059SGregory Neil Shapiro 	**  Find out some information from the headers.
9940266059SGregory Neil Shapiro 	**	Examples are who is the from person & the date.
10040266059SGregory Neil Shapiro 	*/
10140266059SGregory Neil Shapiro 
10240266059SGregory Neil Shapiro 	eatheader(e, true, false);
10340266059SGregory Neil Shapiro 
10440266059SGregory Neil Shapiro 	if (GrabTo && e->e_sendqueue == NULL)
10540266059SGregory Neil Shapiro 		usrerr("No recipient addresses found in header");
10640266059SGregory Neil Shapiro 
10740266059SGregory Neil Shapiro 	/*
10840266059SGregory Neil Shapiro 	**  If we have a Return-Receipt-To:, turn it into a DSN.
10940266059SGregory Neil Shapiro 	*/
11040266059SGregory Neil Shapiro 
11140266059SGregory Neil Shapiro 	if (RrtImpliesDsn && hvalue("return-receipt-to", e->e_header) != NULL)
11240266059SGregory Neil Shapiro 	{
11340266059SGregory Neil Shapiro 		ADDRESS *q;
11440266059SGregory Neil Shapiro 
11540266059SGregory Neil Shapiro 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
11640266059SGregory Neil Shapiro 			if (!bitset(QHASNOTIFY, q->q_flags))
11740266059SGregory Neil Shapiro 				q->q_flags |= QHASNOTIFY|QPINGONSUCCESS;
11840266059SGregory Neil Shapiro 	}
11940266059SGregory Neil Shapiro 
12040266059SGregory Neil Shapiro 	/*
12140266059SGregory Neil Shapiro 	**  Add an appropriate recipient line if we have none.
12240266059SGregory Neil Shapiro 	*/
12340266059SGregory Neil Shapiro 
12440266059SGregory Neil Shapiro 	if (hvalue("to", e->e_header) != NULL ||
12540266059SGregory Neil Shapiro 	    hvalue("cc", e->e_header) != NULL ||
12640266059SGregory Neil Shapiro 	    hvalue("apparently-to", e->e_header) != NULL)
12740266059SGregory Neil Shapiro 	{
12840266059SGregory Neil Shapiro 		/* have a valid recipient header -- delete Bcc: headers */
12940266059SGregory Neil Shapiro 		e->e_flags |= EF_DELETE_BCC;
13040266059SGregory Neil Shapiro 	}
13140266059SGregory Neil Shapiro 	else if (hvalue("bcc", e->e_header) == NULL)
13240266059SGregory Neil Shapiro 	{
13340266059SGregory Neil Shapiro 		/* no valid recipient headers */
13440266059SGregory Neil Shapiro 		register ADDRESS *q;
13540266059SGregory Neil Shapiro 		char *hdr = NULL;
13640266059SGregory Neil Shapiro 
13740266059SGregory Neil Shapiro 		/* create a recipient field */
13840266059SGregory Neil Shapiro 		switch (NoRecipientAction)
13940266059SGregory Neil Shapiro 		{
14040266059SGregory Neil Shapiro 		  case NRA_ADD_APPARENTLY_TO:
14140266059SGregory Neil Shapiro 			hdr = "Apparently-To";
14240266059SGregory Neil Shapiro 			break;
14340266059SGregory Neil Shapiro 
14440266059SGregory Neil Shapiro 		  case NRA_ADD_TO:
14540266059SGregory Neil Shapiro 			hdr = "To";
14640266059SGregory Neil Shapiro 			break;
14740266059SGregory Neil Shapiro 
14840266059SGregory Neil Shapiro 		  case NRA_ADD_BCC:
149d0cef73dSGregory Neil Shapiro 			addheader("Bcc", " ", 0, e, true);
15040266059SGregory Neil Shapiro 			break;
15140266059SGregory Neil Shapiro 
15240266059SGregory Neil Shapiro 		  case NRA_ADD_TO_UNDISCLOSED:
153d0cef73dSGregory Neil Shapiro 			addheader("To", "undisclosed-recipients:;", 0, e, true);
15440266059SGregory Neil Shapiro 			break;
15540266059SGregory Neil Shapiro 		}
15640266059SGregory Neil Shapiro 
15740266059SGregory Neil Shapiro 		if (hdr != NULL)
15840266059SGregory Neil Shapiro 		{
15940266059SGregory Neil Shapiro 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
16040266059SGregory Neil Shapiro 			{
16140266059SGregory Neil Shapiro 				if (q->q_alias != NULL)
16240266059SGregory Neil Shapiro 					continue;
16340266059SGregory Neil Shapiro 				if (tTd(30, 3))
16440266059SGregory Neil Shapiro 					sm_dprintf("Adding %s: %s\n",
16540266059SGregory Neil Shapiro 						hdr, q->q_paddr);
166d0cef73dSGregory Neil Shapiro 				addheader(hdr, q->q_paddr, 0, e, true);
16740266059SGregory Neil Shapiro 			}
16840266059SGregory Neil Shapiro 		}
16940266059SGregory Neil Shapiro 	}
17040266059SGregory Neil Shapiro }
17140266059SGregory Neil Shapiro 
17240266059SGregory Neil Shapiro /*
17340266059SGregory Neil Shapiro **  COLLECT_DFOPEN -- open the message data file
17440266059SGregory Neil Shapiro **
17540266059SGregory Neil Shapiro **	Called by collect() after it has finished processing the header.
17640266059SGregory Neil Shapiro **	Queue selection occurs at this point, possibly based on the
17740266059SGregory Neil Shapiro **	envelope's recipient list and on header information.
17840266059SGregory Neil Shapiro **
17940266059SGregory Neil Shapiro **	Parameters:
18040266059SGregory Neil Shapiro **		e -- envelope
18140266059SGregory Neil Shapiro **
182*d39bd2c1SGregory Neil Shapiro **	Returns:
18340266059SGregory Neil Shapiro **		NULL, or a pointer to an open data file,
18440266059SGregory Neil Shapiro **		into which the message body will be written by collect().
18540266059SGregory Neil Shapiro **
18640266059SGregory Neil Shapiro **	Side Effects:
18740266059SGregory Neil Shapiro **		Calls syserr, sets EF_FATALERRS and returns NULL
18840266059SGregory Neil Shapiro **		if there is insufficient disk space.
18940266059SGregory Neil Shapiro **		Aborts process if data file could not be opened.
19040266059SGregory Neil Shapiro **		Otherwise, the queue is selected,
19140266059SGregory Neil Shapiro **		e->e_{dfino,dfdev,msgsize,flags} are updated,
19240266059SGregory Neil Shapiro **		and a pointer to an open data file is returned.
19340266059SGregory Neil Shapiro */
19440266059SGregory Neil Shapiro 
19540266059SGregory Neil Shapiro static SM_FILE_T *
collect_dfopen(e)19640266059SGregory Neil Shapiro collect_dfopen(e)
19740266059SGregory Neil Shapiro 	ENVELOPE *e;
19840266059SGregory Neil Shapiro {
19940266059SGregory Neil Shapiro 	MODE_T oldumask = 0;
20040266059SGregory Neil Shapiro 	int dfd;
20140266059SGregory Neil Shapiro 	struct stat stbuf;
20240266059SGregory Neil Shapiro 	SM_FILE_T *df;
20340266059SGregory Neil Shapiro 	char *dfname;
20440266059SGregory Neil Shapiro 
20540266059SGregory Neil Shapiro 	if (!setnewqueue(e))
20640266059SGregory Neil Shapiro 		return NULL;
20740266059SGregory Neil Shapiro 
20840266059SGregory Neil Shapiro 	dfname = queuename(e, DATAFL_LETTER);
20940266059SGregory Neil Shapiro 	if (bitset(S_IWGRP, QueueFileMode))
21040266059SGregory Neil Shapiro 		oldumask = umask(002);
21140266059SGregory Neil Shapiro 	df = bfopen(dfname, QueueFileMode, DataFileBufferSize,
21240266059SGregory Neil Shapiro 		    SFF_OPENASROOT);
21340266059SGregory Neil Shapiro 	if (bitset(S_IWGRP, QueueFileMode))
21440266059SGregory Neil Shapiro 		(void) umask(oldumask);
21540266059SGregory Neil Shapiro 	if (df == NULL)
21640266059SGregory Neil Shapiro 	{
21740266059SGregory Neil Shapiro 		syserr("@Cannot create %s", dfname);
21840266059SGregory Neil Shapiro 		e->e_flags |= EF_NO_BODY_RETN;
21940266059SGregory Neil Shapiro 		flush_errors(true);
220a7ec597cSGregory Neil Shapiro 		finis(false, true, ExitStat);
22140266059SGregory Neil Shapiro 		/* NOTREACHED */
22240266059SGregory Neil Shapiro 	}
22340266059SGregory Neil Shapiro 	dfd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL);
22440266059SGregory Neil Shapiro 	if (dfd < 0 || fstat(dfd, &stbuf) < 0)
22540266059SGregory Neil Shapiro 		e->e_dfino = -1;
22640266059SGregory Neil Shapiro 	else
22740266059SGregory Neil Shapiro 	{
22840266059SGregory Neil Shapiro 		e->e_dfdev = stbuf.st_dev;
22940266059SGregory Neil Shapiro 		e->e_dfino = stbuf.st_ino;
23040266059SGregory Neil Shapiro 	}
23140266059SGregory Neil Shapiro 	e->e_flags |= EF_HAS_DF;
23240266059SGregory Neil Shapiro 	return df;
23340266059SGregory Neil Shapiro }
23440266059SGregory Neil Shapiro 
23540266059SGregory Neil Shapiro /*
236*d39bd2c1SGregory Neil Shapiro **  INCBUFLEN -- increase buflen for the header buffer in collect()
237*d39bd2c1SGregory Neil Shapiro **
238*d39bd2c1SGregory Neil Shapiro **	Parameters:
239*d39bd2c1SGregory Neil Shapiro **		buflen -- current size of buffer
240*d39bd2c1SGregory Neil Shapiro **
241*d39bd2c1SGregory Neil Shapiro **	Returns:
242*d39bd2c1SGregory Neil Shapiro **		new buflen
243*d39bd2c1SGregory Neil Shapiro */
244*d39bd2c1SGregory Neil Shapiro 
245*d39bd2c1SGregory Neil Shapiro static int incbuflen __P((int));
246*d39bd2c1SGregory Neil Shapiro static int
incbuflen(buflen)247*d39bd2c1SGregory Neil Shapiro incbuflen(buflen)
248*d39bd2c1SGregory Neil Shapiro 	int buflen;
249*d39bd2c1SGregory Neil Shapiro {
250*d39bd2c1SGregory Neil Shapiro 	int newlen;
251*d39bd2c1SGregory Neil Shapiro 
252*d39bd2c1SGregory Neil Shapiro 	/* this also handles the case of MaxMessageSize == 0 */
253*d39bd2c1SGregory Neil Shapiro 	if (MaxMessageSize <= MEMCHUNKSIZE)
254*d39bd2c1SGregory Neil Shapiro 	{
255*d39bd2c1SGregory Neil Shapiro 		if (buflen < MEMCHUNKSIZE)
256*d39bd2c1SGregory Neil Shapiro 			return buflen * 2;
257*d39bd2c1SGregory Neil Shapiro 		else
258*d39bd2c1SGregory Neil Shapiro 			return buflen + MEMCHUNKSIZE;
259*d39bd2c1SGregory Neil Shapiro 	}
260*d39bd2c1SGregory Neil Shapiro 
261*d39bd2c1SGregory Neil Shapiro 	/* MaxMessageSize > MEMCHUNKSIZE */
262*d39bd2c1SGregory Neil Shapiro 	newlen = buflen * 2;
263*d39bd2c1SGregory Neil Shapiro 	if (newlen > 0 && newlen < MaxMessageSize)
264*d39bd2c1SGregory Neil Shapiro 		return newlen;
265*d39bd2c1SGregory Neil Shapiro 	else
266*d39bd2c1SGregory Neil Shapiro 		return MaxMessageSize;
267*d39bd2c1SGregory Neil Shapiro }
268*d39bd2c1SGregory Neil Shapiro 
269*d39bd2c1SGregory Neil Shapiro #if _FFR_TESTS
270*d39bd2c1SGregory Neil Shapiro /* just for testing/debug output */
271*d39bd2c1SGregory Neil Shapiro static const char *
makeprint(c)272*d39bd2c1SGregory Neil Shapiro makeprint(c)
273*d39bd2c1SGregory Neil Shapiro 	char c;
274*d39bd2c1SGregory Neil Shapiro {
275*d39bd2c1SGregory Neil Shapiro 	static char prt[6];
276*d39bd2c1SGregory Neil Shapiro 
277*d39bd2c1SGregory Neil Shapiro 	prt[1] = '\0';
278*d39bd2c1SGregory Neil Shapiro 	prt[2] = '\0';
279*d39bd2c1SGregory Neil Shapiro 	if (isprint((unsigned char)c))
280*d39bd2c1SGregory Neil Shapiro 		prt[0] = c;
281*d39bd2c1SGregory Neil Shapiro 	else if ('\n' == c)
282*d39bd2c1SGregory Neil Shapiro 	{
283*d39bd2c1SGregory Neil Shapiro 		prt[0] = 'L';
284*d39bd2c1SGregory Neil Shapiro 		prt[1] = 'F';
285*d39bd2c1SGregory Neil Shapiro 	}
286*d39bd2c1SGregory Neil Shapiro 	else if ('\r' == c)
287*d39bd2c1SGregory Neil Shapiro 	{
288*d39bd2c1SGregory Neil Shapiro 		prt[0] = 'C';
289*d39bd2c1SGregory Neil Shapiro 		prt[1] = 'R';
290*d39bd2c1SGregory Neil Shapiro 	}
291*d39bd2c1SGregory Neil Shapiro 	else
292*d39bd2c1SGregory Neil Shapiro 		snprintf(prt, sizeof(prt), "%o", c);
293*d39bd2c1SGregory Neil Shapiro 	return prt;
294*d39bd2c1SGregory Neil Shapiro }
295*d39bd2c1SGregory Neil Shapiro #else /* _FFR_TESTS */
296*d39bd2c1SGregory Neil Shapiro # define makeprint(c)	"X"
297*d39bd2c1SGregory Neil Shapiro #endif /* _FFR_TESTS */
298*d39bd2c1SGregory Neil Shapiro 
299*d39bd2c1SGregory Neil Shapiro /*
300c2aa98e2SPeter Wemm **  COLLECT -- read & parse message header & make temp file.
301c2aa98e2SPeter Wemm **
302c2aa98e2SPeter Wemm **	Creates a temporary file name and copies the standard
303c2aa98e2SPeter Wemm **	input to that file.  Leading UNIX-style "From" lines are
304c2aa98e2SPeter Wemm **	stripped off (after important information is extracted).
305c2aa98e2SPeter Wemm **
306c2aa98e2SPeter Wemm **	Parameters:
307c2aa98e2SPeter Wemm **		fp -- file to read.
308*d39bd2c1SGregory Neil Shapiro **		smtpmode -- if >= SMTPMODE_LAX we are running SMTP:
309*d39bd2c1SGregory Neil Shapiro **			give an RFC821 style message to say we are
310*d39bd2c1SGregory Neil Shapiro **			ready to collect input, and never ignore
311*d39bd2c1SGregory Neil Shapiro **			a single dot to mean end of message.
312c2aa98e2SPeter Wemm **		hdrp -- the location to stash the header.
313c2aa98e2SPeter Wemm **		e -- the current envelope.
314a7ec597cSGregory Neil Shapiro **		rsetsize -- reset e_msgsize?
315c2aa98e2SPeter Wemm **
316c2aa98e2SPeter Wemm **	Returns:
317c2aa98e2SPeter Wemm **		none.
318c2aa98e2SPeter Wemm **
319c2aa98e2SPeter Wemm **	Side Effects:
32040266059SGregory Neil Shapiro **		If successful,
32140266059SGregory Neil Shapiro **		- Data file is created and filled, and e->e_dfp is set.
32240266059SGregory Neil Shapiro **		- The from person may be set.
32340266059SGregory Neil Shapiro **		If the "enough disk space" check fails,
32440266059SGregory Neil Shapiro **		- syserr is called.
32540266059SGregory Neil Shapiro **		- e->e_dfp is NULL.
32640266059SGregory Neil Shapiro **		- e->e_flags & EF_FATALERRS is set.
32740266059SGregory Neil Shapiro **		- collect() returns.
32840266059SGregory Neil Shapiro **		If data file cannot be created, the process is terminated.
329c2aa98e2SPeter Wemm */
330c2aa98e2SPeter Wemm 
331c2aa98e2SPeter Wemm /* values for input state machine */
332c2aa98e2SPeter Wemm #define IS_NORM		0	/* middle of line */
333c2aa98e2SPeter Wemm #define IS_BOL		1	/* beginning of line */
334*d39bd2c1SGregory Neil Shapiro #define IS_DOT		2	/* read "." at beginning of line */
335c2aa98e2SPeter Wemm #define IS_DOTCR	3	/* read ".\r" at beginning of line */
336*d39bd2c1SGregory Neil Shapiro #define IS_CR		4	/* read "\r" */
337*d39bd2c1SGregory Neil Shapiro 
338*d39bd2c1SGregory Neil Shapiro /* hack to enhance readability of debug output */
339*d39bd2c1SGregory Neil Shapiro static const char *istates[] = { "NORM", "BOL", "DOT", "DOTCR", "CR" };
340*d39bd2c1SGregory Neil Shapiro #define ISTATE istates[istate]
341c2aa98e2SPeter Wemm 
342c2aa98e2SPeter Wemm /* values for message state machine */
343c2aa98e2SPeter Wemm #define MS_UFROM	0	/* reading Unix from line */
344c2aa98e2SPeter Wemm #define MS_HEADER	1	/* reading message header */
345c2aa98e2SPeter Wemm #define MS_BODY		2	/* reading message body */
34625bab6e9SPeter Wemm #define MS_DISCARD	3	/* discarding rest of message */
347*d39bd2c1SGregory Neil Shapiro #define BARE_LF_MSG "Bare linefeed (LF) not allowed"
348*d39bd2c1SGregory Neil Shapiro #define BARE_CR_MSG "Bare carriage return (CR) not allowed"
349c2aa98e2SPeter Wemm 
350c2aa98e2SPeter Wemm void
collect(fp,smtpmode,hdrp,e,rsetsize)351a7ec597cSGregory Neil Shapiro collect(fp, smtpmode, hdrp, e, rsetsize)
35240266059SGregory Neil Shapiro 	SM_FILE_T *fp;
353*d39bd2c1SGregory Neil Shapiro 	int smtpmode;
354c2aa98e2SPeter Wemm 	HDR **hdrp;
355c2aa98e2SPeter Wemm 	register ENVELOPE *e;
356a7ec597cSGregory Neil Shapiro 	bool rsetsize;
357c2aa98e2SPeter Wemm {
3584e4196cbSGregory Neil Shapiro 	register SM_FILE_T *df;
3594e4196cbSGregory Neil Shapiro 	bool ignrdot;
3604e4196cbSGregory Neil Shapiro 	int dbto;
3614e4196cbSGregory Neil Shapiro 	register char *bp;
3624e4196cbSGregory Neil Shapiro 	int c;
3634e4196cbSGregory Neil Shapiro 	bool inputerr;
364c2aa98e2SPeter Wemm 	bool headeronly;
3654e4196cbSGregory Neil Shapiro 	char *buf;
3664e4196cbSGregory Neil Shapiro 	int buflen;
3674e4196cbSGregory Neil Shapiro 	int istate;
3684e4196cbSGregory Neil Shapiro 	int mstate;
3694e4196cbSGregory Neil Shapiro 	int hdrslen;
3704e4196cbSGregory Neil Shapiro 	int numhdrs;
3714e4196cbSGregory Neil Shapiro 	int afd;
372da7d7b9cSGregory Neil Shapiro 	int old_rd_tmo;
3734e4196cbSGregory Neil Shapiro 	unsigned char *pbp;
37440266059SGregory Neil Shapiro 	unsigned char peekbuf[8];
375c2aa98e2SPeter Wemm 	char bufbuf[MAXLINE];
376552d4955SGregory Neil Shapiro #if _FFR_REJECT_NUL_BYTE
377552d4955SGregory Neil Shapiro 	bool hasNUL;		/* has at least one NUL input byte */
3785b0945b5SGregory Neil Shapiro #endif
379*d39bd2c1SGregory Neil Shapiro 	int bare_lf, bare_cr;
380*d39bd2c1SGregory Neil Shapiro 
381*d39bd2c1SGregory Neil Shapiro #define SMTPMODE	(smtpmode >= SMTPMODE_LAX)
382*d39bd2c1SGregory Neil Shapiro #define SMTPMODE_STRICT	((smtpmode & SMTPMODE_CRLF) != 0)
383*d39bd2c1SGregory Neil Shapiro #define BARE_LF_421	((smtpmode & SMTPMODE_LF_421) != 0)
384*d39bd2c1SGregory Neil Shapiro #define BARE_CR_421	((smtpmode & SMTPMODE_CR_421) != 0)
385*d39bd2c1SGregory Neil Shapiro #define BARE_LF_SP	((smtpmode & SMTPMODE_LF_SP) != 0)
386*d39bd2c1SGregory Neil Shapiro #define BARE_CR_SP	((smtpmode & SMTPMODE_CR_SP) != 0)
387*d39bd2c1SGregory Neil Shapiro 
388*d39bd2c1SGregory Neil Shapiro /* for bare_{lf,cr} */
389*d39bd2c1SGregory Neil Shapiro #define BARE_IN_HDR	0x01
390*d39bd2c1SGregory Neil Shapiro #define BARE_IN_BDY	0x02
391*d39bd2c1SGregory Neil Shapiro #define BARE_WHERE	((MS_BODY == mstate) ? BARE_IN_BDY : BARE_IN_HDR)
392c2aa98e2SPeter Wemm 
39340266059SGregory Neil Shapiro 	df = NULL;
394*d39bd2c1SGregory Neil Shapiro 	ignrdot = SMTPMODE ? false : IgnrDot;
395*d39bd2c1SGregory Neil Shapiro 	bare_lf = bare_cr = 0;
3964e4196cbSGregory Neil Shapiro 
3974e4196cbSGregory Neil Shapiro 	/* timeout for I/O functions is in milliseconds */
398*d39bd2c1SGregory Neil Shapiro 	dbto = SMTPMODE ? ((int) TimeOuts.to_datablock * 1000)
3994e4196cbSGregory Neil Shapiro 			: SM_TIME_FOREVER;
4004e4196cbSGregory Neil Shapiro 	sm_io_setinfo(fp, SM_IO_WHAT_TIMEOUT, &dbto);
401da7d7b9cSGregory Neil Shapiro 	old_rd_tmo = set_tls_rd_tmo(TimeOuts.to_datablock);
40240266059SGregory Neil Shapiro 	c = SM_IO_EOF;
40340266059SGregory Neil Shapiro 	inputerr = false;
404c2aa98e2SPeter Wemm 	headeronly = hdrp != NULL;
40540266059SGregory Neil Shapiro 	hdrslen = 0;
40640266059SGregory Neil Shapiro 	numhdrs = 0;
40740266059SGregory Neil Shapiro 	HasEightBits = false;
408552d4955SGregory Neil Shapiro #if _FFR_REJECT_NUL_BYTE
409552d4955SGregory Neil Shapiro 	hasNUL = false;
4105b0945b5SGregory Neil Shapiro #endif
41140266059SGregory Neil Shapiro 	buf = bp = bufbuf;
412d0cef73dSGregory Neil Shapiro 	buflen = sizeof(bufbuf);
41340266059SGregory Neil Shapiro 	pbp = peekbuf;
41440266059SGregory Neil Shapiro 	istate = IS_BOL;
41540266059SGregory Neil Shapiro 	mstate = SaveFrom ? MS_HEADER : MS_UFROM;
416c2aa98e2SPeter Wemm 
417c2aa98e2SPeter Wemm 	/*
418c2aa98e2SPeter Wemm 	**  Tell ARPANET to go ahead.
419c2aa98e2SPeter Wemm 	*/
420c2aa98e2SPeter Wemm 
421*d39bd2c1SGregory Neil Shapiro 	if (SMTPMODE)
422*d39bd2c1SGregory Neil Shapiro 		message("354 End data with <CR><LF>.<CR><LF>");
423c2aa98e2SPeter Wemm 
4244e4196cbSGregory Neil Shapiro 	/* simulate an I/O timeout when used as sink */
4254e4196cbSGregory Neil Shapiro 	if (tTd(83, 101))
4264e4196cbSGregory Neil Shapiro 		sleep(319);
4274e4196cbSGregory Neil Shapiro 
428c2aa98e2SPeter Wemm 	if (tTd(30, 2))
429*d39bd2c1SGregory Neil Shapiro 		sm_dprintf("collect, smtpmode=%#x\n", smtpmode);
430c2aa98e2SPeter Wemm 
431c2aa98e2SPeter Wemm 	/*
432c2aa98e2SPeter Wemm 	**  Read the message.
433c2aa98e2SPeter Wemm 	**
434c2aa98e2SPeter Wemm 	**	This is done using two interleaved state machines.
435c2aa98e2SPeter Wemm 	**	The input state machine is looking for things like
436c2aa98e2SPeter Wemm 	**	hidden dots; the message state machine is handling
437c2aa98e2SPeter Wemm 	**	the larger picture (e.g., header versus body).
438c2aa98e2SPeter Wemm 	*/
439c2aa98e2SPeter Wemm 
440a7ec597cSGregory Neil Shapiro 	if (rsetsize)
44140266059SGregory Neil Shapiro 		e->e_msgsize = 0;
442c2aa98e2SPeter Wemm 	for (;;)
443c2aa98e2SPeter Wemm 	{
444c2aa98e2SPeter Wemm 		if (tTd(30, 35))
445*d39bd2c1SGregory Neil Shapiro 			sm_dprintf("top, istate=%s, mstate=%d\n", ISTATE,
44640266059SGregory Neil Shapiro 				   mstate);
447c2aa98e2SPeter Wemm 		for (;;)
448c2aa98e2SPeter Wemm 		{
449c2aa98e2SPeter Wemm 			if (pbp > peekbuf)
450c2aa98e2SPeter Wemm 				c = *--pbp;
451c2aa98e2SPeter Wemm 			else
452c2aa98e2SPeter Wemm 			{
45340266059SGregory Neil Shapiro 				while (!sm_io_eof(fp) && !sm_io_error(fp))
454c2aa98e2SPeter Wemm 				{
455c2aa98e2SPeter Wemm 					errno = 0;
45640266059SGregory Neil Shapiro 					c = sm_io_getc(fp, SM_TIME_DEFAULT);
45740266059SGregory Neil Shapiro 					if (c == SM_IO_EOF && errno == EINTR)
45842e5d165SGregory Neil Shapiro 					{
45942e5d165SGregory Neil Shapiro 						/* Interrupted, retry */
46040266059SGregory Neil Shapiro 						sm_io_clearerr(fp);
46142e5d165SGregory Neil Shapiro 						continue;
46242e5d165SGregory Neil Shapiro 					}
4634e4196cbSGregory Neil Shapiro 
4644e4196cbSGregory Neil Shapiro 					/* timeout? */
4654e4196cbSGregory Neil Shapiro 					if (c == SM_IO_EOF && errno == EAGAIN
466*d39bd2c1SGregory Neil Shapiro 					    && SMTPMODE)
4674e4196cbSGregory Neil Shapiro 					{
4684e4196cbSGregory Neil Shapiro 						/*
4694e4196cbSGregory Neil Shapiro 						**  Override e_message in
4704e4196cbSGregory Neil Shapiro 						**  usrerr() as this is the
4714e4196cbSGregory Neil Shapiro 						**  reason for failure that
4724e4196cbSGregory Neil Shapiro 						**  should be logged for
4734e4196cbSGregory Neil Shapiro 						**  undelivered recipients.
4744e4196cbSGregory Neil Shapiro 						*/
4754e4196cbSGregory Neil Shapiro 
4764e4196cbSGregory Neil Shapiro 						e->e_message = NULL;
4774e4196cbSGregory Neil Shapiro 						errno = 0;
4784e4196cbSGregory Neil Shapiro 						inputerr = true;
4794e4196cbSGregory Neil Shapiro 						goto readabort;
4804e4196cbSGregory Neil Shapiro 					}
48142e5d165SGregory Neil Shapiro 					break;
482c2aa98e2SPeter Wemm 				}
483c2aa98e2SPeter Wemm 				if (TrafficLogFile != NULL && !headeronly)
484c2aa98e2SPeter Wemm 				{
485c2aa98e2SPeter Wemm 					if (istate == IS_BOL)
48640266059SGregory Neil Shapiro 						(void) sm_io_fprintf(TrafficLogFile,
48740266059SGregory Neil Shapiro 							SM_TIME_DEFAULT,
4888774250cSGregory Neil Shapiro 							"%05d <<< ",
48940266059SGregory Neil Shapiro 							(int) CurrentPid);
49040266059SGregory Neil Shapiro 					if (c == SM_IO_EOF)
49140266059SGregory Neil Shapiro 						(void) sm_io_fprintf(TrafficLogFile,
49240266059SGregory Neil Shapiro 							SM_TIME_DEFAULT,
4938774250cSGregory Neil Shapiro 							"[EOF]\n");
494c2aa98e2SPeter Wemm 					else
49540266059SGregory Neil Shapiro 						(void) sm_io_putc(TrafficLogFile,
49640266059SGregory Neil Shapiro 							SM_TIME_DEFAULT,
49740266059SGregory Neil Shapiro 							c);
498c2aa98e2SPeter Wemm 				}
499552d4955SGregory Neil Shapiro #if _FFR_REJECT_NUL_BYTE
500552d4955SGregory Neil Shapiro 				if (c == '\0')
501552d4955SGregory Neil Shapiro 					hasNUL = true;
5025b0945b5SGregory Neil Shapiro #endif
50340266059SGregory Neil Shapiro 				if (c == SM_IO_EOF)
504*d39bd2c1SGregory Neil Shapiro 					goto readdone;
505*d39bd2c1SGregory Neil Shapiro 				if (SevenBitInput ||
506*d39bd2c1SGregory Neil Shapiro 				    bitset(EF_7BITBODY, e->e_flags))
507c2aa98e2SPeter Wemm 					c &= 0x7f;
508c2aa98e2SPeter Wemm 				else
509c2aa98e2SPeter Wemm 					HasEightBits |= bitset(0x80, c);
510c2aa98e2SPeter Wemm 			}
511c2aa98e2SPeter Wemm 			if (tTd(30, 94))
512*d39bd2c1SGregory Neil Shapiro 				sm_dprintf("istate=%s, c=%s (0x%x)\n",
513*d39bd2c1SGregory Neil Shapiro 					ISTATE, makeprint((char) c), c);
514*d39bd2c1SGregory Neil Shapiro 			if ('\n' == c && SMTPMODE &&
515*d39bd2c1SGregory Neil Shapiro 			    !(IS_CR == istate || IS_DOTCR == istate))
516*d39bd2c1SGregory Neil Shapiro 			{
517*d39bd2c1SGregory Neil Shapiro 				bare_lf |= BARE_WHERE;
518*d39bd2c1SGregory Neil Shapiro 				if (BARE_LF_421)
519*d39bd2c1SGregory Neil Shapiro 				{
520*d39bd2c1SGregory Neil Shapiro 					inputerr = true;
521*d39bd2c1SGregory Neil Shapiro 					goto readabort;
522*d39bd2c1SGregory Neil Shapiro 				}
523*d39bd2c1SGregory Neil Shapiro 				if (BARE_LF_SP)
524*d39bd2c1SGregory Neil Shapiro 				{
525*d39bd2c1SGregory Neil Shapiro 					if (TTD(30, 64))
526*d39bd2c1SGregory Neil Shapiro 						sm_dprintf("LF: c=%s %#x\n", makeprint((char) c), c);
527*d39bd2c1SGregory Neil Shapiro 					c = ' ';
528*d39bd2c1SGregory Neil Shapiro 				}
529*d39bd2c1SGregory Neil Shapiro 			}
530c2aa98e2SPeter Wemm 			switch (istate)
531c2aa98e2SPeter Wemm 			{
532c2aa98e2SPeter Wemm 			  case IS_BOL:
533c2aa98e2SPeter Wemm 				if (c == '.')
534c2aa98e2SPeter Wemm 				{
535c2aa98e2SPeter Wemm 					istate = IS_DOT;
536c2aa98e2SPeter Wemm 					continue;
537c2aa98e2SPeter Wemm 				}
538c2aa98e2SPeter Wemm 				break;
539c2aa98e2SPeter Wemm 
540c2aa98e2SPeter Wemm 			  case IS_DOT:
541*d39bd2c1SGregory Neil Shapiro 				if (c == '\n' && !ignrdot && !SMTPMODE_STRICT)
542*d39bd2c1SGregory Neil Shapiro 					goto readdone;
5435b0945b5SGregory Neil Shapiro 				else if (c == '\r')
544c2aa98e2SPeter Wemm 				{
545c2aa98e2SPeter Wemm 					istate = IS_DOTCR;
546c2aa98e2SPeter Wemm 					continue;
547c2aa98e2SPeter Wemm 				}
548605302a5SGregory Neil Shapiro 				else if (ignrdot ||
549605302a5SGregory Neil Shapiro 					 (c != '.' &&
550605302a5SGregory Neil Shapiro 					  OpMode != MD_SMTP &&
551c2aa98e2SPeter Wemm 					  OpMode != MD_DAEMON &&
552c2aa98e2SPeter Wemm 					  OpMode != MD_ARPAFTP))
553605302a5SGregory Neil Shapiro 
554c2aa98e2SPeter Wemm 				{
5554e4196cbSGregory Neil Shapiro 					SM_ASSERT(pbp < peekbuf +
5564e4196cbSGregory Neil Shapiro 							sizeof(peekbuf));
557c2aa98e2SPeter Wemm 					*pbp++ = c;
558c2aa98e2SPeter Wemm 					c = '.';
559c2aa98e2SPeter Wemm 				}
560c2aa98e2SPeter Wemm 				break;
561c2aa98e2SPeter Wemm 
562c2aa98e2SPeter Wemm 			  case IS_DOTCR:
563c2aa98e2SPeter Wemm 				if (c == '\n' && !ignrdot)
564*d39bd2c1SGregory Neil Shapiro 					goto readdone;
565c2aa98e2SPeter Wemm 				else
566c2aa98e2SPeter Wemm 				{
567c2aa98e2SPeter Wemm 					/* push back the ".\rx" */
5684e4196cbSGregory Neil Shapiro 					SM_ASSERT(pbp < peekbuf +
5694e4196cbSGregory Neil Shapiro 							sizeof(peekbuf));
570c2aa98e2SPeter Wemm 					*pbp++ = c;
571605302a5SGregory Neil Shapiro 					if (OpMode != MD_SMTP &&
572605302a5SGregory Neil Shapiro 					    OpMode != MD_DAEMON &&
573605302a5SGregory Neil Shapiro 					    OpMode != MD_ARPAFTP)
574605302a5SGregory Neil Shapiro 					{
5755ef517c0SGregory Neil Shapiro 						SM_ASSERT(pbp < peekbuf +
5765ef517c0SGregory Neil Shapiro 							 sizeof(peekbuf));
577c2aa98e2SPeter Wemm 						*pbp++ = '\r';
578c2aa98e2SPeter Wemm 						c = '.';
579c2aa98e2SPeter Wemm 					}
580605302a5SGregory Neil Shapiro 					else
581605302a5SGregory Neil Shapiro 						c = '\r';
582605302a5SGregory Neil Shapiro 				}
583c2aa98e2SPeter Wemm 				break;
584c2aa98e2SPeter Wemm 
585c2aa98e2SPeter Wemm 			  case IS_CR:
586c2aa98e2SPeter Wemm 				if (c == '\n')
587*d39bd2c1SGregory Neil Shapiro 				{
588*d39bd2c1SGregory Neil Shapiro 					if (TTD(30, 64))
589*d39bd2c1SGregory Neil Shapiro 						sm_dprintf("state=CR, c=%s %#x -> BOL\n", makeprint((char) c), c);
590c2aa98e2SPeter Wemm 					istate = IS_BOL;
591*d39bd2c1SGregory Neil Shapiro 				}
592c2aa98e2SPeter Wemm 				else
593c2aa98e2SPeter Wemm 				{
594*d39bd2c1SGregory Neil Shapiro 					if (TTD(30, 64))
595*d39bd2c1SGregory Neil Shapiro 						sm_dprintf("state=CR, c=%s %#x -> NORM\n", makeprint((char) c), c);
596*d39bd2c1SGregory Neil Shapiro 					if (SMTPMODE)
597*d39bd2c1SGregory Neil Shapiro 					{
598*d39bd2c1SGregory Neil Shapiro 						bare_cr |= BARE_WHERE;
599*d39bd2c1SGregory Neil Shapiro 						if (BARE_CR_421)
600*d39bd2c1SGregory Neil Shapiro 						{
601*d39bd2c1SGregory Neil Shapiro 							inputerr = true;
602*d39bd2c1SGregory Neil Shapiro 							goto readabort;
603*d39bd2c1SGregory Neil Shapiro 						}
604*d39bd2c1SGregory Neil Shapiro 					}
60540266059SGregory Neil Shapiro 					(void) sm_io_ungetc(fp, SM_TIME_DEFAULT,
60640266059SGregory Neil Shapiro 							    c);
607*d39bd2c1SGregory Neil Shapiro 					if (BARE_CR_SP)
608*d39bd2c1SGregory Neil Shapiro 						c = ' ';
609*d39bd2c1SGregory Neil Shapiro 					else
610c2aa98e2SPeter Wemm 						c = '\r';
611c2aa98e2SPeter Wemm 					istate = IS_NORM;
612c2aa98e2SPeter Wemm 				}
613c2aa98e2SPeter Wemm 				goto bufferchar;
614c2aa98e2SPeter Wemm 			}
615c2aa98e2SPeter Wemm 
6165b0945b5SGregory Neil Shapiro 			if (c == '\r')
617c2aa98e2SPeter Wemm 			{
618c2aa98e2SPeter Wemm 				istate = IS_CR;
619c2aa98e2SPeter Wemm 				continue;
620c2aa98e2SPeter Wemm 			}
621*d39bd2c1SGregory Neil Shapiro 			else if (c == '\n' && !SMTPMODE_STRICT)
622c2aa98e2SPeter Wemm 				istate = IS_BOL;
623c2aa98e2SPeter Wemm 			else
624c2aa98e2SPeter Wemm 				istate = IS_NORM;
625c2aa98e2SPeter Wemm 
626c2aa98e2SPeter Wemm bufferchar:
627c2aa98e2SPeter Wemm 			if (!headeronly)
62842e5d165SGregory Neil Shapiro 			{
62942e5d165SGregory Neil Shapiro 				/* no overflow? */
63042e5d165SGregory Neil Shapiro 				if (e->e_msgsize >= 0)
63142e5d165SGregory Neil Shapiro 				{
632c2aa98e2SPeter Wemm 					e->e_msgsize++;
63342e5d165SGregory Neil Shapiro 					if (MaxMessageSize > 0 &&
63442e5d165SGregory Neil Shapiro 					    !bitset(EF_TOOBIG, e->e_flags) &&
63542e5d165SGregory Neil Shapiro 					    e->e_msgsize > MaxMessageSize)
63642e5d165SGregory Neil Shapiro 						 e->e_flags |= EF_TOOBIG;
63742e5d165SGregory Neil Shapiro 				}
63842e5d165SGregory Neil Shapiro 			}
63925bab6e9SPeter Wemm 			switch (mstate)
640c2aa98e2SPeter Wemm 			{
64125bab6e9SPeter Wemm 			  case MS_BODY:
642c2aa98e2SPeter Wemm 				/* just put the character out */
64342e5d165SGregory Neil Shapiro 				if (!bitset(EF_TOOBIG, e->e_flags))
64440266059SGregory Neil Shapiro 					(void) sm_io_putc(df, SM_TIME_DEFAULT,
64540266059SGregory Neil Shapiro 							  c);
646*d39bd2c1SGregory Neil Shapiro 				if (TTD(30, 64))
647*d39bd2c1SGregory Neil Shapiro 					sm_dprintf("state=%s, put=%s %#x\n", ISTATE, makeprint((char) c), c);
64806f25ae9SGregory Neil Shapiro 				/* FALLTHROUGH */
64925bab6e9SPeter Wemm 
65025bab6e9SPeter Wemm 			  case MS_DISCARD:
651c2aa98e2SPeter Wemm 				continue;
652c2aa98e2SPeter Wemm 			}
653c2aa98e2SPeter Wemm 
654b6bacd31SGregory Neil Shapiro 			SM_ASSERT(mstate == MS_UFROM || mstate == MS_HEADER);
655b6bacd31SGregory Neil Shapiro 
656c2aa98e2SPeter Wemm 			/* header -- buffer up */
657c2aa98e2SPeter Wemm 			if (bp >= &buf[buflen - 2])
658c2aa98e2SPeter Wemm 			{
659c2aa98e2SPeter Wemm 				char *obuf;
660c2aa98e2SPeter Wemm 
661c2aa98e2SPeter Wemm 				/* out of space for header */
662c2aa98e2SPeter Wemm 				obuf = buf;
663*d39bd2c1SGregory Neil Shapiro 				buflen = incbuflen(buflen);
664*d39bd2c1SGregory Neil Shapiro 				if (tTd(30, 32))
665*d39bd2c1SGregory Neil Shapiro 					sm_dprintf("buflen=%d, hdrslen=%d\n", buflen, hdrslen);
6664e4196cbSGregory Neil Shapiro 				if (buflen <= 0)
6674e4196cbSGregory Neil Shapiro 				{
6684e4196cbSGregory Neil Shapiro 					sm_syslog(LOG_NOTICE, e->e_id,
6694e4196cbSGregory Neil Shapiro 						  "header overflow from %s during message collect",
6704e4196cbSGregory Neil Shapiro 						  CURHOSTNAME);
6714e4196cbSGregory Neil Shapiro 					errno = 0;
6724e4196cbSGregory Neil Shapiro 					e->e_flags |= EF_CLRQUEUE;
6734e4196cbSGregory Neil Shapiro 					e->e_status = "5.6.0";
6744e4196cbSGregory Neil Shapiro 					usrerrenh(e->e_status,
6754e4196cbSGregory Neil Shapiro 						  "552 Headers too large");
6764e4196cbSGregory Neil Shapiro 					goto discard;
6774e4196cbSGregory Neil Shapiro 				}
678c2aa98e2SPeter Wemm 				buf = xalloc(buflen);
67906f25ae9SGregory Neil Shapiro 				memmove(buf, obuf, bp - obuf);
680c2aa98e2SPeter Wemm 				bp = &buf[bp - obuf];
681c2aa98e2SPeter Wemm 				if (obuf != bufbuf)
68240266059SGregory Neil Shapiro 					sm_free(obuf);  /* XXX */
683c2aa98e2SPeter Wemm 			}
68440266059SGregory Neil Shapiro 
685d0cef73dSGregory Neil Shapiro 			if (c != '\0')
68625bab6e9SPeter Wemm 			{
687c2aa98e2SPeter Wemm 				*bp++ = c;
68840266059SGregory Neil Shapiro 				++hdrslen;
68913058a91SGregory Neil Shapiro 				if (!headeronly &&
69013058a91SGregory Neil Shapiro 				    MaxHeadersLength > 0 &&
691602a2b1bSGregory Neil Shapiro 				    hdrslen > MaxHeadersLength)
69225bab6e9SPeter Wemm 				{
69325bab6e9SPeter Wemm 					sm_syslog(LOG_NOTICE, e->e_id,
6942e43090eSPeter Wemm 						  "headers too large (%d max) from %s during message collect",
6952e43090eSPeter Wemm 						  MaxHeadersLength,
69640266059SGregory Neil Shapiro 						  CURHOSTNAME);
69725bab6e9SPeter Wemm 					errno = 0;
69825bab6e9SPeter Wemm 					e->e_flags |= EF_CLRQUEUE;
69925bab6e9SPeter Wemm 					e->e_status = "5.6.0";
70006f25ae9SGregory Neil Shapiro 					usrerrenh(e->e_status,
70106f25ae9SGregory Neil Shapiro 						  "552 Headers too large (%d max)",
7022e43090eSPeter Wemm 						  MaxHeadersLength);
7034e4196cbSGregory Neil Shapiro   discard:
70425bab6e9SPeter Wemm 					mstate = MS_DISCARD;
70525bab6e9SPeter Wemm 				}
70625bab6e9SPeter Wemm 			}
707c2aa98e2SPeter Wemm 			if (istate == IS_BOL)
708c2aa98e2SPeter Wemm 				break;
709c2aa98e2SPeter Wemm 		}
710c2aa98e2SPeter Wemm 		*bp = '\0';
711c2aa98e2SPeter Wemm 
712c2aa98e2SPeter Wemm nextstate:
713c2aa98e2SPeter Wemm 		if (tTd(30, 35))
714*d39bd2c1SGregory Neil Shapiro 			sm_dprintf("nextstate, istate=%s, mstate=%d, line=\"%s\"\n",
715*d39bd2c1SGregory Neil Shapiro 				ISTATE, mstate, buf);
716c2aa98e2SPeter Wemm 		switch (mstate)
717c2aa98e2SPeter Wemm 		{
718c2aa98e2SPeter Wemm 		  case MS_UFROM:
719c2aa98e2SPeter Wemm 			mstate = MS_HEADER;
720c2aa98e2SPeter Wemm #ifndef NOTUNIX
721c2aa98e2SPeter Wemm 			if (strncmp(buf, "From ", 5) == 0)
722c2aa98e2SPeter Wemm 			{
723c2aa98e2SPeter Wemm 				bp = buf;
724c2aa98e2SPeter Wemm 				eatfrom(buf, e);
725c2aa98e2SPeter Wemm 				continue;
726c2aa98e2SPeter Wemm 			}
72706f25ae9SGregory Neil Shapiro #endif /* ! NOTUNIX */
72806f25ae9SGregory Neil Shapiro 			/* FALLTHROUGH */
729c2aa98e2SPeter Wemm 
730c2aa98e2SPeter Wemm 		  case MS_HEADER:
731c2aa98e2SPeter Wemm 			if (!isheader(buf))
732c2aa98e2SPeter Wemm 			{
733c2aa98e2SPeter Wemm 				mstate = MS_BODY;
734c2aa98e2SPeter Wemm 				goto nextstate;
735c2aa98e2SPeter Wemm 			}
736c2aa98e2SPeter Wemm 
737c2aa98e2SPeter Wemm 			/* check for possible continuation line */
738c2aa98e2SPeter Wemm 			do
739c2aa98e2SPeter Wemm 			{
74040266059SGregory Neil Shapiro 				sm_io_clearerr(fp);
741c2aa98e2SPeter Wemm 				errno = 0;
74240266059SGregory Neil Shapiro 				c = sm_io_getc(fp, SM_TIME_DEFAULT);
7434e4196cbSGregory Neil Shapiro 
7444e4196cbSGregory Neil Shapiro 				/* timeout? */
7454e4196cbSGregory Neil Shapiro 				if (c == SM_IO_EOF && errno == EAGAIN
746*d39bd2c1SGregory Neil Shapiro 				    && SMTPMODE)
7474e4196cbSGregory Neil Shapiro 				{
7484e4196cbSGregory Neil Shapiro 					/*
7494e4196cbSGregory Neil Shapiro 					**  Override e_message in
7504e4196cbSGregory Neil Shapiro 					**  usrerr() as this is the
7514e4196cbSGregory Neil Shapiro 					**  reason for failure that
7524e4196cbSGregory Neil Shapiro 					**  should be logged for
7534e4196cbSGregory Neil Shapiro 					**  undelivered recipients.
7544e4196cbSGregory Neil Shapiro 					*/
7554e4196cbSGregory Neil Shapiro 
7564e4196cbSGregory Neil Shapiro 					e->e_message = NULL;
7574e4196cbSGregory Neil Shapiro 					errno = 0;
7584e4196cbSGregory Neil Shapiro 					inputerr = true;
7594e4196cbSGregory Neil Shapiro 					goto readabort;
7604e4196cbSGregory Neil Shapiro 				}
76140266059SGregory Neil Shapiro 			} while (c == SM_IO_EOF && errno == EINTR);
76240266059SGregory Neil Shapiro 			if (c != SM_IO_EOF)
76340266059SGregory Neil Shapiro 				(void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
764c2aa98e2SPeter Wemm 			if (c == ' ' || c == '\t')
765c2aa98e2SPeter Wemm 			{
766c2aa98e2SPeter Wemm 				/* yep -- defer this */
767c2aa98e2SPeter Wemm 				continue;
768c2aa98e2SPeter Wemm 			}
769c2aa98e2SPeter Wemm 
7705ef517c0SGregory Neil Shapiro 			SM_ASSERT(bp > buf);
7714e4196cbSGregory Neil Shapiro 
7724e4196cbSGregory Neil Shapiro 			/* guaranteed by isheader(buf) */
7734e4196cbSGregory Neil Shapiro 			SM_ASSERT(*(bp - 1) != '\n' || bp > buf + 1);
7744e4196cbSGregory Neil Shapiro 
775*d39bd2c1SGregory Neil Shapiro 			/* trim off trailing CRLF or LF */
776c2aa98e2SPeter Wemm 			if (*--bp != '\n' || *--bp != '\r')
777c2aa98e2SPeter Wemm 				bp++;
778c2aa98e2SPeter Wemm 			*bp = '\0';
77925bab6e9SPeter Wemm 
78006f25ae9SGregory Neil Shapiro 			if (bitset(H_EOH, chompheader(buf,
78106f25ae9SGregory Neil Shapiro 						      CHHDR_CHECK | CHHDR_USER,
78206f25ae9SGregory Neil Shapiro 						      hdrp, e)))
783c2aa98e2SPeter Wemm 			{
784c2aa98e2SPeter Wemm 				mstate = MS_BODY;
785c2aa98e2SPeter Wemm 				goto nextstate;
786c2aa98e2SPeter Wemm 			}
78706f25ae9SGregory Neil Shapiro 			numhdrs++;
788c2aa98e2SPeter Wemm 			break;
789c2aa98e2SPeter Wemm 
790c2aa98e2SPeter Wemm 		  case MS_BODY:
791c2aa98e2SPeter Wemm 			if (tTd(30, 1))
79240266059SGregory Neil Shapiro 				sm_dprintf("EOH\n");
79306f25ae9SGregory Neil Shapiro 
794c2aa98e2SPeter Wemm 			if (headeronly)
795*d39bd2c1SGregory Neil Shapiro 				goto readdone;
79606f25ae9SGregory Neil Shapiro 
79740266059SGregory Neil Shapiro 			df = collect_eoh(e, numhdrs, hdrslen);
79840266059SGregory Neil Shapiro 			if (df == NULL)
79940266059SGregory Neil Shapiro 				e->e_flags |= EF_TOOBIG;
80006f25ae9SGregory Neil Shapiro 
801c2aa98e2SPeter Wemm 			bp = buf;
802c2aa98e2SPeter Wemm 
803c2aa98e2SPeter Wemm 			/* toss blank line */
8045b0945b5SGregory Neil Shapiro 			if ((bp[0] == '\r' && bp[1] == '\n') ||
8055b0945b5SGregory Neil Shapiro 			    (bp[0] == '\n'))
806c2aa98e2SPeter Wemm 			{
807c2aa98e2SPeter Wemm 				break;
808c2aa98e2SPeter Wemm 			}
809c2aa98e2SPeter Wemm 
810c2aa98e2SPeter Wemm 			/* if not a blank separator, write it out */
81142e5d165SGregory Neil Shapiro 			if (!bitset(EF_TOOBIG, e->e_flags))
812c2aa98e2SPeter Wemm 			{
813c2aa98e2SPeter Wemm 				while (*bp != '\0')
81440266059SGregory Neil Shapiro 					(void) sm_io_putc(df, SM_TIME_DEFAULT,
81540266059SGregory Neil Shapiro 							  *bp++);
816c2aa98e2SPeter Wemm 			}
817c2aa98e2SPeter Wemm 			break;
818c2aa98e2SPeter Wemm 		}
819c2aa98e2SPeter Wemm 		bp = buf;
820c2aa98e2SPeter Wemm 	}
821c2aa98e2SPeter Wemm 
822*d39bd2c1SGregory Neil Shapiro readdone:
823*d39bd2c1SGregory Neil Shapiro 	if ((sm_io_eof(fp) && SMTPMODE) || sm_io_error(fp))
824c2aa98e2SPeter Wemm 	{
82540266059SGregory Neil Shapiro 		const char *errmsg;
826c2aa98e2SPeter Wemm 
82740266059SGregory Neil Shapiro 		if (sm_io_eof(fp))
82840266059SGregory Neil Shapiro 			errmsg = "unexpected close";
82940266059SGregory Neil Shapiro 		else
83040266059SGregory Neil Shapiro 			errmsg = sm_errstring(errno);
831c2aa98e2SPeter Wemm 		if (tTd(30, 1))
83240266059SGregory Neil Shapiro 			sm_dprintf("collect: premature EOM: %s\n", errmsg);
83340266059SGregory Neil Shapiro 		if (LogLevel > 1)
834c2aa98e2SPeter Wemm 			sm_syslog(LOG_WARNING, e->e_id,
835c2aa98e2SPeter Wemm 				"collect: premature EOM: %s", errmsg);
83640266059SGregory Neil Shapiro 		inputerr = true;
837c2aa98e2SPeter Wemm 	}
838c2aa98e2SPeter Wemm 
839c2aa98e2SPeter Wemm 	if (headeronly)
840da7d7b9cSGregory Neil Shapiro 		goto end;
841c2aa98e2SPeter Wemm 
84240266059SGregory Neil Shapiro 	if (mstate != MS_BODY)
84340266059SGregory Neil Shapiro 	{
84440266059SGregory Neil Shapiro 		/* no body or discard, so we never opened the data file */
84540266059SGregory Neil Shapiro 		SM_ASSERT(df == NULL);
84640266059SGregory Neil Shapiro 		df = collect_eoh(e, numhdrs, hdrslen);
84740266059SGregory Neil Shapiro 	}
84840266059SGregory Neil Shapiro 
84906f25ae9SGregory Neil Shapiro 	if (df == NULL)
850c2aa98e2SPeter Wemm 	{
85106f25ae9SGregory Neil Shapiro 		/* skip next few clauses */
85206f25ae9SGregory Neil Shapiro 		/* EMPTY */
85306f25ae9SGregory Neil Shapiro 	}
85440266059SGregory Neil Shapiro 	else if (sm_io_flush(df, SM_TIME_DEFAULT) != 0 || sm_io_error(df))
85506f25ae9SGregory Neil Shapiro 	{
85640266059SGregory Neil Shapiro 		dferror(df, "sm_io_flush||sm_io_error", e);
85740266059SGregory Neil Shapiro 		flush_errors(true);
85840266059SGregory Neil Shapiro 		finis(true, true, ExitStat);
85906f25ae9SGregory Neil Shapiro 		/* NOTREACHED */
86006f25ae9SGregory Neil Shapiro 	}
861e92d3f3fSGregory Neil Shapiro 	else if (SuperSafe == SAFE_NO ||
862e92d3f3fSGregory Neil Shapiro 		 SuperSafe == SAFE_INTERACTIVE ||
863*d39bd2c1SGregory Neil Shapiro 		 (SuperSafe == SAFE_REALLY_POSTMILTER && SMTPMODE))
86406f25ae9SGregory Neil Shapiro 	{
86506f25ae9SGregory Neil Shapiro 		/* skip next few clauses */
86606f25ae9SGregory Neil Shapiro 		/* EMPTY */
86713d88268SGregory Neil Shapiro 		/* Note: updfs() is not called in this case! */
86806f25ae9SGregory Neil Shapiro 	}
86940266059SGregory Neil Shapiro 	else if (sm_io_setinfo(df, SM_BF_COMMIT, NULL) < 0 && errno != EINVAL)
87006f25ae9SGregory Neil Shapiro 	{
87106f25ae9SGregory Neil Shapiro 		int save_errno = errno;
87206f25ae9SGregory Neil Shapiro 
87306f25ae9SGregory Neil Shapiro 		if (save_errno == EEXIST)
87406f25ae9SGregory Neil Shapiro 		{
87506f25ae9SGregory Neil Shapiro 			char *dfile;
87606f25ae9SGregory Neil Shapiro 			struct stat st;
87740266059SGregory Neil Shapiro 			int dfd;
87806f25ae9SGregory Neil Shapiro 
87940266059SGregory Neil Shapiro 			dfile = queuename(e, DATAFL_LETTER);
88006f25ae9SGregory Neil Shapiro 			if (stat(dfile, &st) < 0)
88106f25ae9SGregory Neil Shapiro 				st.st_size = -1;
88206f25ae9SGregory Neil Shapiro 			errno = EEXIST;
88340266059SGregory Neil Shapiro 			syserr("@collect: bfcommit(%s): already on disk, size=%ld",
88442e5d165SGregory Neil Shapiro 			       dfile, (long) st.st_size);
88540266059SGregory Neil Shapiro 			dfd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL);
88606f25ae9SGregory Neil Shapiro 			if (dfd >= 0)
88740266059SGregory Neil Shapiro 				dumpfd(dfd, true, true);
88806f25ae9SGregory Neil Shapiro 		}
88906f25ae9SGregory Neil Shapiro 		errno = save_errno;
89006f25ae9SGregory Neil Shapiro 		dferror(df, "bfcommit", e);
89140266059SGregory Neil Shapiro 		flush_errors(true);
89240266059SGregory Neil Shapiro 		finis(save_errno != EEXIST, true, ExitStat);
89306f25ae9SGregory Neil Shapiro 	}
894e92d3f3fSGregory Neil Shapiro 	else if ((afd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL)) < 0)
895e92d3f3fSGregory Neil Shapiro 	{
896e92d3f3fSGregory Neil Shapiro 		dferror(df, "sm_io_getinfo", e);
897e92d3f3fSGregory Neil Shapiro 		flush_errors(true);
898e92d3f3fSGregory Neil Shapiro 		finis(true, true, ExitStat);
899e92d3f3fSGregory Neil Shapiro 		/* NOTREACHED */
900e92d3f3fSGregory Neil Shapiro 	}
901e92d3f3fSGregory Neil Shapiro 	else if (fsync(afd) < 0)
902602a2b1bSGregory Neil Shapiro 	{
90340266059SGregory Neil Shapiro 		dferror(df, "fsync", e);
90440266059SGregory Neil Shapiro 		flush_errors(true);
90540266059SGregory Neil Shapiro 		finis(true, true, ExitStat);
906602a2b1bSGregory Neil Shapiro 		/* NOTREACHED */
907602a2b1bSGregory Neil Shapiro 	}
90840266059SGregory Neil Shapiro 	else if (sm_io_close(df, SM_TIME_DEFAULT) < 0)
90906f25ae9SGregory Neil Shapiro 	{
91040266059SGregory Neil Shapiro 		dferror(df, "sm_io_close", e);
91140266059SGregory Neil Shapiro 		flush_errors(true);
91240266059SGregory Neil Shapiro 		finis(true, true, ExitStat);
91306f25ae9SGregory Neil Shapiro 		/* NOTREACHED */
91406f25ae9SGregory Neil Shapiro 	}
91506f25ae9SGregory Neil Shapiro 	else
91606f25ae9SGregory Neil Shapiro 	{
91706f25ae9SGregory Neil Shapiro 		/* everything is happily flushed to disk */
91806f25ae9SGregory Neil Shapiro 		df = NULL;
91940266059SGregory Neil Shapiro 
92040266059SGregory Neil Shapiro 		/* remove from available space in filesystem */
92113d88268SGregory Neil Shapiro 		updfs(e, 0, 1, "collect");
922c2aa98e2SPeter Wemm 	}
923c2aa98e2SPeter Wemm 
924c2aa98e2SPeter Wemm 	/* An EOF when running SMTP is an error */
9254e4196cbSGregory Neil Shapiro   readabort:
926c2aa98e2SPeter Wemm 	if (inputerr && (OpMode == MD_SMTP || OpMode == MD_DAEMON))
927c2aa98e2SPeter Wemm 	{
928c2aa98e2SPeter Wemm 		char *problem;
929959366dcSGregory Neil Shapiro 		ADDRESS *q;
930c2aa98e2SPeter Wemm 
93140266059SGregory Neil Shapiro 		if (sm_io_eof(fp))
932c2aa98e2SPeter Wemm 			problem = "unexpected close";
93340266059SGregory Neil Shapiro 		else if (sm_io_error(fp))
934c2aa98e2SPeter Wemm 			problem = "I/O error";
935*d39bd2c1SGregory Neil Shapiro 		else if (0 != bare_lf)
936*d39bd2c1SGregory Neil Shapiro 			problem = BARE_LF_MSG;
937*d39bd2c1SGregory Neil Shapiro 		else if (0 != bare_cr)
938*d39bd2c1SGregory Neil Shapiro 			problem = BARE_CR_MSG;
939c2aa98e2SPeter Wemm 		else
940c2aa98e2SPeter Wemm 			problem = "read timeout";
941*d39bd2c1SGregory Neil Shapiro 
942*d39bd2c1SGregory Neil Shapiro #define LOG_CLT ((NULL != RealHostName) ? RealHostName: "localhost")
943*d39bd2c1SGregory Neil Shapiro #define CONN_ERR_TXT	"collect: relay=%s, from=%s, info=%s%s%s%s"
944*d39bd2c1SGregory Neil Shapiro #define CONN_ERR_CODE	"421 4.4.1 "
945*d39bd2c1SGregory Neil Shapiro #define CONN_LOG_FROM	shortenstring(e->e_from.q_paddr, MAXSHORTSTR)
946*d39bd2c1SGregory Neil Shapiro #define CONN_ERR_BARE (0 != bare_lf) ? BARE_LF_MSG : ((0 != bare_cr) ? BARE_CR_MSG : "")
947*d39bd2c1SGregory Neil Shapiro #define CONN_ERR_WHERE(bare_xy) (BARE_IN_HDR==(bare_xy) ? "header" : \
948*d39bd2c1SGregory Neil Shapiro 	(BARE_IN_BDY==(bare_xy) ? "body" : "header+body"))
949*d39bd2c1SGregory Neil Shapiro 
950*d39bd2c1SGregory Neil Shapiro #define HAS_BARE_XY (0 != (bare_lf | bare_cr))
951*d39bd2c1SGregory Neil Shapiro #define CONN_ERR_ARGS LOG_CLT, CONN_LOG_FROM, problem, \
952*d39bd2c1SGregory Neil Shapiro 	HAS_BARE_XY ? ", where=" : "", \
953*d39bd2c1SGregory Neil Shapiro 	HAS_BARE_XY ? CONN_ERR_WHERE(bare_lf|bare_cr) : "", \
954*d39bd2c1SGregory Neil Shapiro 	HAS_BARE_XY ? ", status=tempfail" : ""
955*d39bd2c1SGregory Neil Shapiro 
956*d39bd2c1SGregory Neil Shapiro 		if (LogLevel > 0 && (sm_io_eof(fp) || (0 != (bare_lf | bare_cr))))
957c2aa98e2SPeter Wemm 			sm_syslog(LOG_NOTICE, e->e_id,
958*d39bd2c1SGregory Neil Shapiro 				CONN_ERR_TXT, CONN_ERR_ARGS);
959*d39bd2c1SGregory Neil Shapiro 		if (0 != (bare_lf | bare_cr))
960*d39bd2c1SGregory Neil Shapiro 			usrerr("421 4.5.0 %s", CONN_ERR_BARE);
961*d39bd2c1SGregory Neil Shapiro 		else if (sm_io_eof(fp))
962*d39bd2c1SGregory Neil Shapiro 			usrerr(CONN_ERR_CODE CONN_ERR_TXT, CONN_ERR_ARGS);
963c2aa98e2SPeter Wemm 		else
964*d39bd2c1SGregory Neil Shapiro 			syserr(CONN_ERR_CODE CONN_ERR_TXT, CONN_ERR_ARGS);
9654e4196cbSGregory Neil Shapiro 		flush_errors(true);
966c2aa98e2SPeter Wemm 
967c2aa98e2SPeter Wemm 		/* don't return an error indication */
968c2aa98e2SPeter Wemm 		e->e_to = NULL;
969c2aa98e2SPeter Wemm 		e->e_flags &= ~EF_FATALERRS;
970c2aa98e2SPeter Wemm 		e->e_flags |= EF_CLRQUEUE;
971c2aa98e2SPeter Wemm 
972959366dcSGregory Neil Shapiro 		/* Don't send any message notification to sender */
973959366dcSGregory Neil Shapiro 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
974959366dcSGregory Neil Shapiro 		{
975959366dcSGregory Neil Shapiro 			if (QS_IS_DEAD(q->q_state))
976959366dcSGregory Neil Shapiro 				continue;
977959366dcSGregory Neil Shapiro 			q->q_state = QS_FATALERR;
978959366dcSGregory Neil Shapiro 		}
979959366dcSGregory Neil Shapiro 
9802fb4f839SGregory Neil Shapiro 		SM_CLOSE_FP(df);
98140266059SGregory Neil Shapiro 		finis(true, true, ExitStat);
98206f25ae9SGregory Neil Shapiro 		/* NOTREACHED */
983c2aa98e2SPeter Wemm 	}
984c2aa98e2SPeter Wemm 
98540266059SGregory Neil Shapiro 	/* Log collection information. */
9869bd497b8SGregory Neil Shapiro 	if (tTd(92, 2))
9879bd497b8SGregory Neil Shapiro 		sm_dprintf("collect: e_id=%s, EF_LOGSENDER=%d, LogLevel=%d\n",
9889bd497b8SGregory Neil Shapiro 			e->e_id, bitset(EF_LOGSENDER, e->e_flags), LogLevel);
98940266059SGregory Neil Shapiro 	if (bitset(EF_LOGSENDER, e->e_flags) && LogLevel > 4)
990c2aa98e2SPeter Wemm 	{
99140266059SGregory Neil Shapiro 		logsender(e, e->e_msgid);
99240266059SGregory Neil Shapiro 		e->e_flags &= ~EF_LOGSENDER;
993c2aa98e2SPeter Wemm 	}
994c2aa98e2SPeter Wemm 
995*d39bd2c1SGregory Neil Shapiro #define LOG_BARE_XY(bare_xy, bare_xy_sp, bare_xy_msg)	\
996*d39bd2c1SGregory Neil Shapiro 	do	\
997*d39bd2c1SGregory Neil Shapiro 	{	\
998*d39bd2c1SGregory Neil Shapiro 		if ((0 != bare_xy) && LogLevel > 8)	\
999*d39bd2c1SGregory Neil Shapiro 			sm_syslog(LOG_NOTICE, e->e_id, \
1000*d39bd2c1SGregory Neil Shapiro 				"collect: relay=%s, from=%s, info=%s, where=%s%s" \
1001*d39bd2c1SGregory Neil Shapiro 				, LOG_CLT, CONN_LOG_FROM, bare_xy_msg	\
1002*d39bd2c1SGregory Neil Shapiro 				, CONN_ERR_WHERE(bare_xy)	\
1003*d39bd2c1SGregory Neil Shapiro 				, bare_xy_sp ? ", status=replaced" : ""	\
1004*d39bd2c1SGregory Neil Shapiro 				);	\
1005*d39bd2c1SGregory Neil Shapiro 	} while (0)
1006*d39bd2c1SGregory Neil Shapiro 
1007*d39bd2c1SGregory Neil Shapiro 	LOG_BARE_XY(bare_lf, BARE_LF_SP, BARE_LF_MSG);
1008*d39bd2c1SGregory Neil Shapiro 	LOG_BARE_XY(bare_cr, BARE_CR_SP, BARE_CR_MSG);
1009*d39bd2c1SGregory Neil Shapiro 
1010c2aa98e2SPeter Wemm 	/* check for message too large */
101142e5d165SGregory Neil Shapiro 	if (bitset(EF_TOOBIG, e->e_flags))
1012c2aa98e2SPeter Wemm 	{
1013c2aa98e2SPeter Wemm 		e->e_flags |= EF_NO_BODY_RETN|EF_CLRQUEUE;
101440266059SGregory Neil Shapiro 		if (!bitset(EF_FATALERRS, e->e_flags))
101540266059SGregory Neil Shapiro 		{
1016c2aa98e2SPeter Wemm 			e->e_status = "5.2.3";
101706f25ae9SGregory Neil Shapiro 			usrerrenh(e->e_status,
101806f25ae9SGregory Neil Shapiro 				"552 Message exceeds maximum fixed size (%ld)",
1019c2aa98e2SPeter Wemm 				MaxMessageSize);
1020c2aa98e2SPeter Wemm 			if (LogLevel > 6)
1021c2aa98e2SPeter Wemm 				sm_syslog(LOG_NOTICE, e->e_id,
1022c2aa98e2SPeter Wemm 					"message size (%ld) exceeds maximum (%ld)",
1023ba00ec3dSGregory Neil Shapiro 					PRT_NONNEGL(e->e_msgsize),
1024ba00ec3dSGregory Neil Shapiro 					MaxMessageSize);
1025c2aa98e2SPeter Wemm 		}
102640266059SGregory Neil Shapiro 	}
1027c2aa98e2SPeter Wemm 
1028c2aa98e2SPeter Wemm 	/* check for illegal 8-bit data */
1029c2aa98e2SPeter Wemm 	if (HasEightBits)
1030c2aa98e2SPeter Wemm 	{
1031c2aa98e2SPeter Wemm 		e->e_flags |= EF_HAS8BIT;
1032c2aa98e2SPeter Wemm 		if (!bitset(MM_PASS8BIT|MM_MIME8BIT, MimeMode) &&
1033c2aa98e2SPeter Wemm 		    !bitset(EF_IS_MIME, e->e_flags))
1034c2aa98e2SPeter Wemm 		{
1035c2aa98e2SPeter Wemm 			e->e_status = "5.6.1";
103606f25ae9SGregory Neil Shapiro 			usrerrenh(e->e_status, "554 Eight bit data not allowed");
1037c2aa98e2SPeter Wemm 		}
1038c2aa98e2SPeter Wemm 	}
1039c2aa98e2SPeter Wemm 	else
1040c2aa98e2SPeter Wemm 	{
1041c2aa98e2SPeter Wemm 		/* if it claimed to be 8 bits, well, it lied.... */
1042c2aa98e2SPeter Wemm 		if (e->e_bodytype != NULL &&
10432fb4f839SGregory Neil Shapiro 		    SM_STRCASEEQ(e->e_bodytype, "8bitmime"))
1044c2aa98e2SPeter Wemm 			e->e_bodytype = "7BIT";
1045c2aa98e2SPeter Wemm 	}
1046c2aa98e2SPeter Wemm 
1047552d4955SGregory Neil Shapiro #if _FFR_REJECT_NUL_BYTE
1048552d4955SGregory Neil Shapiro 	if (hasNUL && RejectNUL)
1049552d4955SGregory Neil Shapiro 	{
1050552d4955SGregory Neil Shapiro 		e->e_status = "5.6.1";
1051552d4955SGregory Neil Shapiro 		usrerrenh(e->e_status, "554 NUL byte not allowed");
1052552d4955SGregory Neil Shapiro 	}
1053552d4955SGregory Neil Shapiro #endif /* _FFR_REJECT_NUL_BYTE */
1054552d4955SGregory Neil Shapiro 
105540266059SGregory Neil Shapiro 	if (SuperSafe == SAFE_REALLY && !bitset(EF_FATALERRS, e->e_flags))
105606f25ae9SGregory Neil Shapiro 	{
105740266059SGregory Neil Shapiro 		char *dfname = queuename(e, DATAFL_LETTER);
105840266059SGregory Neil Shapiro 		if ((e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, dfname,
1059e92d3f3fSGregory Neil Shapiro 					   SM_IO_RDONLY_B, NULL)) == NULL)
1060c2aa98e2SPeter Wemm 		{
1061c2aa98e2SPeter Wemm 			/* we haven't acked receipt yet, so just chuck this */
106240266059SGregory Neil Shapiro 			syserr("@Cannot reopen %s", dfname);
106340266059SGregory Neil Shapiro 			finis(true, true, ExitStat);
106406f25ae9SGregory Neil Shapiro 			/* NOTREACHED */
1065c2aa98e2SPeter Wemm 		}
1066c2aa98e2SPeter Wemm 	}
106706f25ae9SGregory Neil Shapiro 	else
106806f25ae9SGregory Neil Shapiro 		e->e_dfp = df;
1069605302a5SGregory Neil Shapiro 
1070605302a5SGregory Neil Shapiro 	/* collect statistics */
1071605302a5SGregory Neil Shapiro 	if (OpMode != MD_VERIFY)
1072a7ec597cSGregory Neil Shapiro 	{
1073a7ec597cSGregory Neil Shapiro 		/*
1074a7ec597cSGregory Neil Shapiro 		**  Recalculate e_msgpriority, it is done at in eatheader()
1075a7ec597cSGregory Neil Shapiro 		**  which is called (in 8.12) after the header is collected,
1076a7ec597cSGregory Neil Shapiro 		**  hence e_msgsize is (most likely) incorrect.
1077a7ec597cSGregory Neil Shapiro 		*/
1078a7ec597cSGregory Neil Shapiro 
1079a7ec597cSGregory Neil Shapiro 		e->e_msgpriority = e->e_msgsize
1080a7ec597cSGregory Neil Shapiro 				 - e->e_class * WkClassFact
1081a7ec597cSGregory Neil Shapiro 				 + e->e_nrcpts * WkRecipFact;
1082605302a5SGregory Neil Shapiro 		markstats(e, (ADDRESS *) NULL, STATS_NORMAL);
108306f25ae9SGregory Neil Shapiro 	}
1084da7d7b9cSGregory Neil Shapiro 
1085da7d7b9cSGregory Neil Shapiro   end:
1086da7d7b9cSGregory Neil Shapiro 	(void) set_tls_rd_tmo(old_rd_tmo);
10872fb4f839SGregory Neil Shapiro 	if (buf != bufbuf)
10882fb4f839SGregory Neil Shapiro 		SM_FREE(buf);
1089a7ec597cSGregory Neil Shapiro }
1090c2aa98e2SPeter Wemm 
109140266059SGregory Neil Shapiro /*
109206f25ae9SGregory Neil Shapiro **  DFERROR -- signal error on writing the data file.
1093c2aa98e2SPeter Wemm **
1094*d39bd2c1SGregory Neil Shapiro **	Called by collect().  collect() always terminates the process
109540266059SGregory Neil Shapiro **	immediately after calling dferror(), which means that the SMTP
109640266059SGregory Neil Shapiro **	session will be terminated, which means that any error message
109740266059SGregory Neil Shapiro **	issued by dferror must be a 421 error, as per RFC 821.
109840266059SGregory Neil Shapiro **
1099c2aa98e2SPeter Wemm **	Parameters:
110006f25ae9SGregory Neil Shapiro **		df -- the file pointer for the data file.
110106f25ae9SGregory Neil Shapiro **		msg -- detailed message.
1102c2aa98e2SPeter Wemm **		e -- the current envelope.
1103c2aa98e2SPeter Wemm **
1104c2aa98e2SPeter Wemm **	Returns:
1105c2aa98e2SPeter Wemm **		none.
1106c2aa98e2SPeter Wemm **
1107c2aa98e2SPeter Wemm **	Side Effects:
1108c2aa98e2SPeter Wemm **		Gives an error message.
1109c2aa98e2SPeter Wemm **		Arranges for following output to go elsewhere.
1110c2aa98e2SPeter Wemm */
1111c2aa98e2SPeter Wemm 
1112e92d3f3fSGregory Neil Shapiro void
dferror(df,msg,e)111306f25ae9SGregory Neil Shapiro dferror(df, msg, e)
111440266059SGregory Neil Shapiro 	SM_FILE_T *volatile df;
111506f25ae9SGregory Neil Shapiro 	char *msg;
1116c2aa98e2SPeter Wemm 	register ENVELOPE *e;
1117c2aa98e2SPeter Wemm {
111806f25ae9SGregory Neil Shapiro 	char *dfname;
111906f25ae9SGregory Neil Shapiro 
112040266059SGregory Neil Shapiro 	dfname = queuename(e, DATAFL_LETTER);
1121c2aa98e2SPeter Wemm 	setstat(EX_IOERR);
1122c2aa98e2SPeter Wemm 	if (errno == ENOSPC)
1123c2aa98e2SPeter Wemm 	{
1124c2aa98e2SPeter Wemm #if STAT64 > 0
1125c2aa98e2SPeter Wemm 		struct stat64 st;
11265b0945b5SGregory Neil Shapiro #else
1127c2aa98e2SPeter Wemm 		struct stat st;
11285b0945b5SGregory Neil Shapiro #endif
1129c2aa98e2SPeter Wemm 		long avail;
1130c2aa98e2SPeter Wemm 		long bsize;
1131c2aa98e2SPeter Wemm 
1132c2aa98e2SPeter Wemm 		e->e_flags |= EF_NO_BODY_RETN;
1133c2aa98e2SPeter Wemm 
1134c2aa98e2SPeter Wemm 		if (
1135c2aa98e2SPeter Wemm #if STAT64 > 0
113640266059SGregory Neil Shapiro 		    fstat64(sm_io_getinfo(df, SM_IO_WHAT_FD, NULL), &st)
11375b0945b5SGregory Neil Shapiro #else
113840266059SGregory Neil Shapiro 		    fstat(sm_io_getinfo(df, SM_IO_WHAT_FD, NULL), &st)
11395b0945b5SGregory Neil Shapiro #endif
1140c2aa98e2SPeter Wemm 		    < 0)
1141c2aa98e2SPeter Wemm 		  st.st_size = 0;
114240266059SGregory Neil Shapiro 		(void) sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, dfname,
1143e92d3f3fSGregory Neil Shapiro 				    SM_IO_WRONLY_B, NULL, df);
1144c2aa98e2SPeter Wemm 		if (st.st_size <= 0)
114540266059SGregory Neil Shapiro 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
114640266059SGregory Neil Shapiro 				"\n*** Mail could not be accepted");
1147c2aa98e2SPeter Wemm 		else
114840266059SGregory Neil Shapiro 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
114940266059SGregory Neil Shapiro 				"\n*** Mail of at least %llu bytes could not be accepted\n",
115040266059SGregory Neil Shapiro 				(ULONGLONG_T) st.st_size);
115140266059SGregory Neil Shapiro 		(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
115240266059SGregory Neil Shapiro 			"*** at %s due to lack of disk space for temp file.\n",
1153c2aa98e2SPeter Wemm 			MyHostName);
115440266059SGregory Neil Shapiro 		avail = freediskspace(qid_printqueue(e->e_qgrp, e->e_qdir),
115540266059SGregory Neil Shapiro 				      &bsize);
1156c2aa98e2SPeter Wemm 		if (avail > 0)
1157c2aa98e2SPeter Wemm 		{
1158c2aa98e2SPeter Wemm 			if (bsize > 1024)
1159c2aa98e2SPeter Wemm 				avail *= bsize / 1024;
1160c2aa98e2SPeter Wemm 			else if (bsize < 1024)
1161c2aa98e2SPeter Wemm 				avail /= 1024 / bsize;
116240266059SGregory Neil Shapiro 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
116340266059SGregory Neil Shapiro 				"*** Currently, %ld kilobytes are available for mail temp files.\n",
1164c2aa98e2SPeter Wemm 				avail);
1165c2aa98e2SPeter Wemm 		}
116640266059SGregory Neil Shapiro #if 0
116740266059SGregory Neil Shapiro 		/* Wrong response code; should be 421. */
1168c2aa98e2SPeter Wemm 		e->e_status = "4.3.1";
116906f25ae9SGregory Neil Shapiro 		usrerrenh(e->e_status, "452 Out of disk space for temp file");
117040266059SGregory Neil Shapiro #else /* 0 */
117140266059SGregory Neil Shapiro 		syserr("421 4.3.1 Out of disk space for temp file");
117240266059SGregory Neil Shapiro #endif /* 0 */
1173c2aa98e2SPeter Wemm 	}
1174c2aa98e2SPeter Wemm 	else
1175da7d7b9cSGregory Neil Shapiro 		syserr("421 4.3.0 collect: Cannot write %s (%s, uid=%ld, gid=%ld)",
1176da7d7b9cSGregory Neil Shapiro 			dfname, msg, (long) geteuid(), (long) getegid());
117740266059SGregory Neil Shapiro 	if (sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, SM_PATH_DEVNULL,
117840266059SGregory Neil Shapiro 			 SM_IO_WRONLY, NULL, df) == NULL)
1179c2aa98e2SPeter Wemm 		sm_syslog(LOG_ERR, e->e_id,
118040266059SGregory Neil Shapiro 			  "dferror: sm_io_reopen(\"/dev/null\") failed: %s",
118140266059SGregory Neil Shapiro 			  sm_errstring(errno));
1182c2aa98e2SPeter Wemm }
118340266059SGregory Neil Shapiro /*
1184c2aa98e2SPeter Wemm **  EATFROM -- chew up a UNIX style from line and process
1185c2aa98e2SPeter Wemm **
1186c2aa98e2SPeter Wemm **	This does indeed make some assumptions about the format
1187c2aa98e2SPeter Wemm **	of UNIX messages.
1188c2aa98e2SPeter Wemm **
1189c2aa98e2SPeter Wemm **	Parameters:
1190c2aa98e2SPeter Wemm **		fm -- the from line.
1191a7ec597cSGregory Neil Shapiro **		e -- envelope
1192c2aa98e2SPeter Wemm **
1193c2aa98e2SPeter Wemm **	Returns:
1194c2aa98e2SPeter Wemm **		none.
1195c2aa98e2SPeter Wemm **
1196c2aa98e2SPeter Wemm **	Side Effects:
1197c2aa98e2SPeter Wemm **		extracts what information it can from the header,
1198c2aa98e2SPeter Wemm **		such as the date.
1199c2aa98e2SPeter Wemm */
1200c2aa98e2SPeter Wemm 
1201c2aa98e2SPeter Wemm #ifndef NOTUNIX
1202c2aa98e2SPeter Wemm 
120306f25ae9SGregory Neil Shapiro static char	*DowList[] =
1204c2aa98e2SPeter Wemm {
1205c2aa98e2SPeter Wemm 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
1206c2aa98e2SPeter Wemm };
1207c2aa98e2SPeter Wemm 
120806f25ae9SGregory Neil Shapiro static char	*MonthList[] =
1209c2aa98e2SPeter Wemm {
1210c2aa98e2SPeter Wemm 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
1211c2aa98e2SPeter Wemm 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
1212c2aa98e2SPeter Wemm 	NULL
1213c2aa98e2SPeter Wemm };
1214c2aa98e2SPeter Wemm 
121506f25ae9SGregory Neil Shapiro static void
eatfrom(fm,e)1216c2aa98e2SPeter Wemm eatfrom(fm, e)
1217c2aa98e2SPeter Wemm 	char *volatile fm;
1218c2aa98e2SPeter Wemm 	register ENVELOPE *e;
1219c2aa98e2SPeter Wemm {
1220c2aa98e2SPeter Wemm 	register char *p;
1221c2aa98e2SPeter Wemm 	register char **dt;
1222c2aa98e2SPeter Wemm 
1223c2aa98e2SPeter Wemm 	if (tTd(30, 2))
122440266059SGregory Neil Shapiro 		sm_dprintf("eatfrom(%s)\n", fm);
1225c2aa98e2SPeter Wemm 
1226c2aa98e2SPeter Wemm 	/* find the date part */
1227c2aa98e2SPeter Wemm 	p = fm;
1228c2aa98e2SPeter Wemm 	while (*p != '\0')
1229c2aa98e2SPeter Wemm 	{
1230c2aa98e2SPeter Wemm 		/* skip a word */
1231c2aa98e2SPeter Wemm 		while (*p != '\0' && *p != ' ')
1232c2aa98e2SPeter Wemm 			p++;
1233c2aa98e2SPeter Wemm 		while (*p == ' ')
1234c2aa98e2SPeter Wemm 			p++;
1235193538b7SGregory Neil Shapiro 		if (strlen(p) < 17)
1236193538b7SGregory Neil Shapiro 		{
1237193538b7SGregory Neil Shapiro 			/* no room for the date */
1238193538b7SGregory Neil Shapiro 			return;
1239193538b7SGregory Neil Shapiro 		}
1240c2aa98e2SPeter Wemm 		if (!(isascii(*p) && isupper(*p)) ||
1241c2aa98e2SPeter Wemm 		    p[3] != ' ' || p[13] != ':' || p[16] != ':')
1242c2aa98e2SPeter Wemm 			continue;
1243c2aa98e2SPeter Wemm 
1244c2aa98e2SPeter Wemm 		/* we have a possible date */
1245c2aa98e2SPeter Wemm 		for (dt = DowList; *dt != NULL; dt++)
1246c2aa98e2SPeter Wemm 			if (strncmp(*dt, p, 3) == 0)
1247c2aa98e2SPeter Wemm 				break;
1248c2aa98e2SPeter Wemm 		if (*dt == NULL)
1249c2aa98e2SPeter Wemm 			continue;
1250c2aa98e2SPeter Wemm 
1251c2aa98e2SPeter Wemm 		for (dt = MonthList; *dt != NULL; dt++)
1252193538b7SGregory Neil Shapiro 		{
1253c2aa98e2SPeter Wemm 			if (strncmp(*dt, &p[4], 3) == 0)
1254c2aa98e2SPeter Wemm 				break;
1255193538b7SGregory Neil Shapiro 		}
1256c2aa98e2SPeter Wemm 		if (*dt != NULL)
1257c2aa98e2SPeter Wemm 			break;
1258c2aa98e2SPeter Wemm 	}
1259c2aa98e2SPeter Wemm 
1260c2aa98e2SPeter Wemm 	if (*p != '\0')
1261c2aa98e2SPeter Wemm 	{
126240266059SGregory Neil Shapiro 		char *q, buf[25];
1263c2aa98e2SPeter Wemm 
1264c2aa98e2SPeter Wemm 		/* we have found a date */
126540266059SGregory Neil Shapiro 		(void) sm_strlcpy(buf, p, sizeof(buf));
126640266059SGregory Neil Shapiro 		q = arpadate(buf);
126740266059SGregory Neil Shapiro 		macdefine(&e->e_macro, A_TEMP, 'a', q);
1268c2aa98e2SPeter Wemm 	}
1269c2aa98e2SPeter Wemm }
127006f25ae9SGregory Neil Shapiro #endif /* ! NOTUNIX */
1271