xref: /titanic_44/usr/src/cmd/sendmail/src/collect.c (revision 058561cbaa119a6f2659bc27ef343e1b47266bb2)
17c478bd9Sstevel@tonic-gate /*
2445f2479Sjbeck  * Copyright (c) 1998-2006 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *	All rights reserved.
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
57c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 1993
67c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
97c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
107c478bd9Sstevel@tonic-gate  * the sendmail distribution.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  */
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate #include <sendmail.h>
177c478bd9Sstevel@tonic-gate 
18*058561cbSjbeck SM_RCSID("@(#)$Id: collect.c,v 8.280 2006/11/29 00:20:40 ca Exp $")
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate static void	eatfrom __P((char *volatile, ENVELOPE *));
217c478bd9Sstevel@tonic-gate static void	collect_doheader __P((ENVELOPE *));
227c478bd9Sstevel@tonic-gate static SM_FILE_T *collect_dfopen __P((ENVELOPE *));
237c478bd9Sstevel@tonic-gate static SM_FILE_T *collect_eoh __P((ENVELOPE *, int, int));
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate **  COLLECT_EOH -- end-of-header processing in collect()
277c478bd9Sstevel@tonic-gate **
287c478bd9Sstevel@tonic-gate **	Called by collect() when it encounters the blank line
297c478bd9Sstevel@tonic-gate **	separating the header from the message body, or when it
307c478bd9Sstevel@tonic-gate **	encounters EOF in a message that contains only a header.
317c478bd9Sstevel@tonic-gate **
327c478bd9Sstevel@tonic-gate **	Parameters:
337c478bd9Sstevel@tonic-gate **		e -- envelope
347c478bd9Sstevel@tonic-gate **		numhdrs -- number of headers
357c478bd9Sstevel@tonic-gate **		hdrslen -- length of headers
367c478bd9Sstevel@tonic-gate **
377c478bd9Sstevel@tonic-gate **	Results:
387c478bd9Sstevel@tonic-gate **		NULL, or handle to open data file
397c478bd9Sstevel@tonic-gate **
407c478bd9Sstevel@tonic-gate **	Side Effects:
417c478bd9Sstevel@tonic-gate **		end-of-header check ruleset is invoked.
427c478bd9Sstevel@tonic-gate **		envelope state is updated.
437c478bd9Sstevel@tonic-gate **		headers may be added and deleted.
447c478bd9Sstevel@tonic-gate **		selects the queue.
457c478bd9Sstevel@tonic-gate **		opens the data file.
467c478bd9Sstevel@tonic-gate */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static SM_FILE_T *
497c478bd9Sstevel@tonic-gate collect_eoh(e, numhdrs, hdrslen)
507c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
517c478bd9Sstevel@tonic-gate 	int numhdrs;
527c478bd9Sstevel@tonic-gate 	int hdrslen;
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	char hnum[16];
557c478bd9Sstevel@tonic-gate 	char hsize[16];
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	/* call the end-of-header check ruleset */
58*058561cbSjbeck 	(void) sm_snprintf(hnum, sizeof(hnum), "%d", numhdrs);
59*058561cbSjbeck 	(void) sm_snprintf(hsize, sizeof(hsize), "%d", hdrslen);
607c478bd9Sstevel@tonic-gate 	if (tTd(30, 10))
617c478bd9Sstevel@tonic-gate 		sm_dprintf("collect: rscheck(\"check_eoh\", \"%s $| %s\")\n",
627c478bd9Sstevel@tonic-gate 			   hnum, hsize);
637c478bd9Sstevel@tonic-gate 	(void) rscheck("check_eoh", hnum, hsize, e, RSF_UNSTRUCTURED|RSF_COUNT,
64*058561cbSjbeck 			3, NULL, e->e_id, NULL);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	/*
677c478bd9Sstevel@tonic-gate 	**  Process the header,
687c478bd9Sstevel@tonic-gate 	**  select the queue, open the data file.
697c478bd9Sstevel@tonic-gate 	*/
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	collect_doheader(e);
727c478bd9Sstevel@tonic-gate 	return collect_dfopen(e);
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate **  COLLECT_DOHEADER -- process header in collect()
777c478bd9Sstevel@tonic-gate **
787c478bd9Sstevel@tonic-gate **	Called by collect() after it has finished parsing the header,
797c478bd9Sstevel@tonic-gate **	but before it selects the queue and creates the data file.
807c478bd9Sstevel@tonic-gate **	The results of processing the header will affect queue selection.
817c478bd9Sstevel@tonic-gate **
827c478bd9Sstevel@tonic-gate **	Parameters:
837c478bd9Sstevel@tonic-gate **		e -- envelope
847c478bd9Sstevel@tonic-gate **
857c478bd9Sstevel@tonic-gate **	Results:
867c478bd9Sstevel@tonic-gate **		none.
877c478bd9Sstevel@tonic-gate **
887c478bd9Sstevel@tonic-gate **	Side Effects:
897c478bd9Sstevel@tonic-gate **		envelope state is updated.
907c478bd9Sstevel@tonic-gate **		headers may be added and deleted.
917c478bd9Sstevel@tonic-gate */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static void
947c478bd9Sstevel@tonic-gate collect_doheader(e)
957c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	/*
987c478bd9Sstevel@tonic-gate 	**  Find out some information from the headers.
997c478bd9Sstevel@tonic-gate 	**	Examples are who is the from person & the date.
1007c478bd9Sstevel@tonic-gate 	*/
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	eatheader(e, true, false);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (GrabTo && e->e_sendqueue == NULL)
1057c478bd9Sstevel@tonic-gate 		usrerr("No recipient addresses found in header");
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	/*
1087c478bd9Sstevel@tonic-gate 	**  If we have a Return-Receipt-To:, turn it into a DSN.
1097c478bd9Sstevel@tonic-gate 	*/
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (RrtImpliesDsn && hvalue("return-receipt-to", e->e_header) != NULL)
1127c478bd9Sstevel@tonic-gate 	{
1137c478bd9Sstevel@tonic-gate 		ADDRESS *q;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1167c478bd9Sstevel@tonic-gate 			if (!bitset(QHASNOTIFY, q->q_flags))
1177c478bd9Sstevel@tonic-gate 				q->q_flags |= QHASNOTIFY|QPINGONSUCCESS;
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/*
1217c478bd9Sstevel@tonic-gate 	**  Add an appropriate recipient line if we have none.
1227c478bd9Sstevel@tonic-gate 	*/
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if (hvalue("to", e->e_header) != NULL ||
1257c478bd9Sstevel@tonic-gate 	    hvalue("cc", e->e_header) != NULL ||
1267c478bd9Sstevel@tonic-gate 	    hvalue("apparently-to", e->e_header) != NULL)
1277c478bd9Sstevel@tonic-gate 	{
1287c478bd9Sstevel@tonic-gate 		/* have a valid recipient header -- delete Bcc: headers */
1297c478bd9Sstevel@tonic-gate 		e->e_flags |= EF_DELETE_BCC;
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 	else if (hvalue("bcc", e->e_header) == NULL)
1327c478bd9Sstevel@tonic-gate 	{
1337c478bd9Sstevel@tonic-gate 		/* no valid recipient headers */
1347c478bd9Sstevel@tonic-gate 		register ADDRESS *q;
1357c478bd9Sstevel@tonic-gate 		char *hdr = NULL;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		/* create a recipient field */
1387c478bd9Sstevel@tonic-gate 		switch (NoRecipientAction)
1397c478bd9Sstevel@tonic-gate 		{
1407c478bd9Sstevel@tonic-gate 		  case NRA_ADD_APPARENTLY_TO:
1417c478bd9Sstevel@tonic-gate 			hdr = "Apparently-To";
1427c478bd9Sstevel@tonic-gate 			break;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 		  case NRA_ADD_TO:
1457c478bd9Sstevel@tonic-gate 			hdr = "To";
1467c478bd9Sstevel@tonic-gate 			break;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		  case NRA_ADD_BCC:
149*058561cbSjbeck 			addheader("Bcc", " ", 0, e, true);
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		  case NRA_ADD_TO_UNDISCLOSED:
153*058561cbSjbeck 			addheader("To", "undisclosed-recipients:;", 0, e, true);
1547c478bd9Sstevel@tonic-gate 			break;
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		if (hdr != NULL)
1587c478bd9Sstevel@tonic-gate 		{
1597c478bd9Sstevel@tonic-gate 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1607c478bd9Sstevel@tonic-gate 			{
1617c478bd9Sstevel@tonic-gate 				if (q->q_alias != NULL)
1627c478bd9Sstevel@tonic-gate 					continue;
1637c478bd9Sstevel@tonic-gate 				if (tTd(30, 3))
1647c478bd9Sstevel@tonic-gate 					sm_dprintf("Adding %s: %s\n",
1657c478bd9Sstevel@tonic-gate 						hdr, q->q_paddr);
166*058561cbSjbeck 				addheader(hdr, q->q_paddr, 0, e, true);
1677c478bd9Sstevel@tonic-gate 			}
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate **  COLLECT_DFOPEN -- open the message data file
1747c478bd9Sstevel@tonic-gate **
1757c478bd9Sstevel@tonic-gate **	Called by collect() after it has finished processing the header.
1767c478bd9Sstevel@tonic-gate **	Queue selection occurs at this point, possibly based on the
1777c478bd9Sstevel@tonic-gate **	envelope's recipient list and on header information.
1787c478bd9Sstevel@tonic-gate **
1797c478bd9Sstevel@tonic-gate **	Parameters:
1807c478bd9Sstevel@tonic-gate **		e -- envelope
1817c478bd9Sstevel@tonic-gate **
1827c478bd9Sstevel@tonic-gate **	Results:
1837c478bd9Sstevel@tonic-gate **		NULL, or a pointer to an open data file,
1847c478bd9Sstevel@tonic-gate **		into which the message body will be written by collect().
1857c478bd9Sstevel@tonic-gate **
1867c478bd9Sstevel@tonic-gate **	Side Effects:
1877c478bd9Sstevel@tonic-gate **		Calls syserr, sets EF_FATALERRS and returns NULL
1887c478bd9Sstevel@tonic-gate **		if there is insufficient disk space.
1897c478bd9Sstevel@tonic-gate **		Aborts process if data file could not be opened.
1907c478bd9Sstevel@tonic-gate **		Otherwise, the queue is selected,
1917c478bd9Sstevel@tonic-gate **		e->e_{dfino,dfdev,msgsize,flags} are updated,
1927c478bd9Sstevel@tonic-gate **		and a pointer to an open data file is returned.
1937c478bd9Sstevel@tonic-gate */
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate static SM_FILE_T *
1967c478bd9Sstevel@tonic-gate collect_dfopen(e)
1977c478bd9Sstevel@tonic-gate 	ENVELOPE *e;
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	MODE_T oldumask = 0;
2007c478bd9Sstevel@tonic-gate 	int dfd;
2017c478bd9Sstevel@tonic-gate 	struct stat stbuf;
2027c478bd9Sstevel@tonic-gate 	SM_FILE_T *df;
2037c478bd9Sstevel@tonic-gate 	char *dfname;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	if (!setnewqueue(e))
2067c478bd9Sstevel@tonic-gate 		return NULL;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	dfname = queuename(e, DATAFL_LETTER);
2097c478bd9Sstevel@tonic-gate 	if (bitset(S_IWGRP, QueueFileMode))
2107c478bd9Sstevel@tonic-gate 		oldumask = umask(002);
2117c478bd9Sstevel@tonic-gate 	df = bfopen(dfname, QueueFileMode, DataFileBufferSize,
2127c478bd9Sstevel@tonic-gate 		    SFF_OPENASROOT);
2137c478bd9Sstevel@tonic-gate 	if (bitset(S_IWGRP, QueueFileMode))
2147c478bd9Sstevel@tonic-gate 		(void) umask(oldumask);
2157c478bd9Sstevel@tonic-gate 	if (df == NULL)
2167c478bd9Sstevel@tonic-gate 	{
2177c478bd9Sstevel@tonic-gate 		syserr("@Cannot create %s", dfname);
2187c478bd9Sstevel@tonic-gate 		e->e_flags |= EF_NO_BODY_RETN;
2197c478bd9Sstevel@tonic-gate 		flush_errors(true);
2207c478bd9Sstevel@tonic-gate 		finis(false, true, ExitStat);
2217c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 	dfd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL);
2247c478bd9Sstevel@tonic-gate 	if (dfd < 0 || fstat(dfd, &stbuf) < 0)
2257c478bd9Sstevel@tonic-gate 		e->e_dfino = -1;
2267c478bd9Sstevel@tonic-gate 	else
2277c478bd9Sstevel@tonic-gate 	{
2287c478bd9Sstevel@tonic-gate 		e->e_dfdev = stbuf.st_dev;
2297c478bd9Sstevel@tonic-gate 		e->e_dfino = stbuf.st_ino;
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 	e->e_flags |= EF_HAS_DF;
2327c478bd9Sstevel@tonic-gate 	return df;
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate /*
2367c478bd9Sstevel@tonic-gate **  COLLECT -- read & parse message header & make temp file.
2377c478bd9Sstevel@tonic-gate **
2387c478bd9Sstevel@tonic-gate **	Creates a temporary file name and copies the standard
2397c478bd9Sstevel@tonic-gate **	input to that file.  Leading UNIX-style "From" lines are
2407c478bd9Sstevel@tonic-gate **	stripped off (after important information is extracted).
2417c478bd9Sstevel@tonic-gate **
2427c478bd9Sstevel@tonic-gate **	Parameters:
2437c478bd9Sstevel@tonic-gate **		fp -- file to read.
2447c478bd9Sstevel@tonic-gate **		smtpmode -- if set, we are running SMTP: give an RFC821
2457c478bd9Sstevel@tonic-gate **			style message to say we are ready to collect
2467c478bd9Sstevel@tonic-gate **			input, and never ignore a single dot to mean
2477c478bd9Sstevel@tonic-gate **			end of message.
2487c478bd9Sstevel@tonic-gate **		hdrp -- the location to stash the header.
2497c478bd9Sstevel@tonic-gate **		e -- the current envelope.
2507c478bd9Sstevel@tonic-gate **		rsetsize -- reset e_msgsize?
2517c478bd9Sstevel@tonic-gate **
2527c478bd9Sstevel@tonic-gate **	Returns:
2537c478bd9Sstevel@tonic-gate **		none.
2547c478bd9Sstevel@tonic-gate **
2557c478bd9Sstevel@tonic-gate **	Side Effects:
2567c478bd9Sstevel@tonic-gate **		If successful,
2577c478bd9Sstevel@tonic-gate **		- Data file is created and filled, and e->e_dfp is set.
2587c478bd9Sstevel@tonic-gate **		- The from person may be set.
2597c478bd9Sstevel@tonic-gate **		If the "enough disk space" check fails,
2607c478bd9Sstevel@tonic-gate **		- syserr is called.
2617c478bd9Sstevel@tonic-gate **		- e->e_dfp is NULL.
2627c478bd9Sstevel@tonic-gate **		- e->e_flags & EF_FATALERRS is set.
2637c478bd9Sstevel@tonic-gate **		- collect() returns.
2647c478bd9Sstevel@tonic-gate **		If data file cannot be created, the process is terminated.
2657c478bd9Sstevel@tonic-gate */
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /* values for input state machine */
2687c478bd9Sstevel@tonic-gate #define IS_NORM		0	/* middle of line */
2697c478bd9Sstevel@tonic-gate #define IS_BOL		1	/* beginning of line */
2707c478bd9Sstevel@tonic-gate #define IS_DOT		2	/* read a dot at beginning of line */
2717c478bd9Sstevel@tonic-gate #define IS_DOTCR	3	/* read ".\r" at beginning of line */
2727c478bd9Sstevel@tonic-gate #define IS_CR		4	/* read a carriage return */
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate /* values for message state machine */
2757c478bd9Sstevel@tonic-gate #define MS_UFROM	0	/* reading Unix from line */
2767c478bd9Sstevel@tonic-gate #define MS_HEADER	1	/* reading message header */
2777c478bd9Sstevel@tonic-gate #define MS_BODY		2	/* reading message body */
2787c478bd9Sstevel@tonic-gate #define MS_DISCARD	3	/* discarding rest of message */
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate void
2817c478bd9Sstevel@tonic-gate collect(fp, smtpmode, hdrp, e, rsetsize)
2827c478bd9Sstevel@tonic-gate 	SM_FILE_T *fp;
2837c478bd9Sstevel@tonic-gate 	bool smtpmode;
2847c478bd9Sstevel@tonic-gate 	HDR **hdrp;
2857c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
2867c478bd9Sstevel@tonic-gate 	bool rsetsize;
2877c478bd9Sstevel@tonic-gate {
288445f2479Sjbeck 	register SM_FILE_T *df;
289445f2479Sjbeck 	bool ignrdot;
290445f2479Sjbeck 	int dbto;
291445f2479Sjbeck 	register char *bp;
292445f2479Sjbeck 	int c;
293445f2479Sjbeck 	bool inputerr;
2947c478bd9Sstevel@tonic-gate 	bool headeronly;
295445f2479Sjbeck 	char *buf;
296445f2479Sjbeck 	int buflen;
297445f2479Sjbeck 	int istate;
298445f2479Sjbeck 	int mstate;
299445f2479Sjbeck 	int hdrslen;
300445f2479Sjbeck 	int numhdrs;
301445f2479Sjbeck 	int afd;
302445f2479Sjbeck 	unsigned char *pbp;
3037c478bd9Sstevel@tonic-gate 	unsigned char peekbuf[8];
3047c478bd9Sstevel@tonic-gate 	char bufbuf[MAXLINE];
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	df = NULL;
3077c478bd9Sstevel@tonic-gate 	ignrdot = smtpmode ? false : IgnrDot;
308445f2479Sjbeck 
309445f2479Sjbeck 	/* timeout for I/O functions is in milliseconds */
310445f2479Sjbeck 	dbto = smtpmode ? ((int) TimeOuts.to_datablock * 1000)
311445f2479Sjbeck 			: SM_TIME_FOREVER;
312445f2479Sjbeck 	sm_io_setinfo(fp, SM_IO_WHAT_TIMEOUT, &dbto);
3137c478bd9Sstevel@tonic-gate 	c = SM_IO_EOF;
3147c478bd9Sstevel@tonic-gate 	inputerr = false;
3157c478bd9Sstevel@tonic-gate 	headeronly = hdrp != NULL;
3167c478bd9Sstevel@tonic-gate 	hdrslen = 0;
3177c478bd9Sstevel@tonic-gate 	numhdrs = 0;
3187c478bd9Sstevel@tonic-gate 	HasEightBits = false;
3197c478bd9Sstevel@tonic-gate 	buf = bp = bufbuf;
320*058561cbSjbeck 	buflen = sizeof(bufbuf);
3217c478bd9Sstevel@tonic-gate 	pbp = peekbuf;
3227c478bd9Sstevel@tonic-gate 	istate = IS_BOL;
3237c478bd9Sstevel@tonic-gate 	mstate = SaveFrom ? MS_HEADER : MS_UFROM;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	/*
3267c478bd9Sstevel@tonic-gate 	**  Tell ARPANET to go ahead.
3277c478bd9Sstevel@tonic-gate 	*/
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	if (smtpmode)
3307c478bd9Sstevel@tonic-gate 		message("354 Enter mail, end with \".\" on a line by itself");
3317c478bd9Sstevel@tonic-gate 
332445f2479Sjbeck 	/* simulate an I/O timeout when used as sink */
333445f2479Sjbeck 	if (tTd(83, 101))
334445f2479Sjbeck 		sleep(319);
335445f2479Sjbeck 
3367c478bd9Sstevel@tonic-gate 	if (tTd(30, 2))
3377c478bd9Sstevel@tonic-gate 		sm_dprintf("collect\n");
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	/*
3407c478bd9Sstevel@tonic-gate 	**  Read the message.
3417c478bd9Sstevel@tonic-gate 	**
3427c478bd9Sstevel@tonic-gate 	**	This is done using two interleaved state machines.
3437c478bd9Sstevel@tonic-gate 	**	The input state machine is looking for things like
3447c478bd9Sstevel@tonic-gate 	**	hidden dots; the message state machine is handling
3457c478bd9Sstevel@tonic-gate 	**	the larger picture (e.g., header versus body).
3467c478bd9Sstevel@tonic-gate 	*/
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	if (rsetsize)
3497c478bd9Sstevel@tonic-gate 		e->e_msgsize = 0;
3507c478bd9Sstevel@tonic-gate 	for (;;)
3517c478bd9Sstevel@tonic-gate 	{
3527c478bd9Sstevel@tonic-gate 		if (tTd(30, 35))
3537c478bd9Sstevel@tonic-gate 			sm_dprintf("top, istate=%d, mstate=%d\n", istate,
3547c478bd9Sstevel@tonic-gate 				   mstate);
3557c478bd9Sstevel@tonic-gate 		for (;;)
3567c478bd9Sstevel@tonic-gate 		{
3577c478bd9Sstevel@tonic-gate 			if (pbp > peekbuf)
3587c478bd9Sstevel@tonic-gate 				c = *--pbp;
3597c478bd9Sstevel@tonic-gate 			else
3607c478bd9Sstevel@tonic-gate 			{
3617c478bd9Sstevel@tonic-gate 				while (!sm_io_eof(fp) && !sm_io_error(fp))
3627c478bd9Sstevel@tonic-gate 				{
3637c478bd9Sstevel@tonic-gate 					errno = 0;
3647c478bd9Sstevel@tonic-gate 					c = sm_io_getc(fp, SM_TIME_DEFAULT);
3657c478bd9Sstevel@tonic-gate 					if (c == SM_IO_EOF && errno == EINTR)
3667c478bd9Sstevel@tonic-gate 					{
3677c478bd9Sstevel@tonic-gate 						/* Interrupted, retry */
3687c478bd9Sstevel@tonic-gate 						sm_io_clearerr(fp);
3697c478bd9Sstevel@tonic-gate 						continue;
3707c478bd9Sstevel@tonic-gate 					}
371445f2479Sjbeck 
372445f2479Sjbeck 					/* timeout? */
373445f2479Sjbeck 					if (c == SM_IO_EOF && errno == EAGAIN
374445f2479Sjbeck 					    && smtpmode)
375445f2479Sjbeck 					{
376445f2479Sjbeck 						/*
377445f2479Sjbeck 						**  Override e_message in
378445f2479Sjbeck 						**  usrerr() as this is the
379445f2479Sjbeck 						**  reason for failure that
380445f2479Sjbeck 						**  should be logged for
381445f2479Sjbeck 						**  undelivered recipients.
382445f2479Sjbeck 						*/
383445f2479Sjbeck 
384445f2479Sjbeck 						e->e_message = NULL;
385445f2479Sjbeck 						errno = 0;
386445f2479Sjbeck 						inputerr = true;
387445f2479Sjbeck 						goto readabort;
388445f2479Sjbeck 					}
3897c478bd9Sstevel@tonic-gate 					break;
3907c478bd9Sstevel@tonic-gate 				}
3917c478bd9Sstevel@tonic-gate 				if (TrafficLogFile != NULL && !headeronly)
3927c478bd9Sstevel@tonic-gate 				{
3937c478bd9Sstevel@tonic-gate 					if (istate == IS_BOL)
3947c478bd9Sstevel@tonic-gate 						(void) sm_io_fprintf(TrafficLogFile,
3957c478bd9Sstevel@tonic-gate 							SM_TIME_DEFAULT,
3967c478bd9Sstevel@tonic-gate 							"%05d <<< ",
3977c478bd9Sstevel@tonic-gate 							(int) CurrentPid);
3987c478bd9Sstevel@tonic-gate 					if (c == SM_IO_EOF)
3997c478bd9Sstevel@tonic-gate 						(void) sm_io_fprintf(TrafficLogFile,
4007c478bd9Sstevel@tonic-gate 							SM_TIME_DEFAULT,
4017c478bd9Sstevel@tonic-gate 							"[EOF]\n");
4027c478bd9Sstevel@tonic-gate 					else
4037c478bd9Sstevel@tonic-gate 						(void) sm_io_putc(TrafficLogFile,
4047c478bd9Sstevel@tonic-gate 							SM_TIME_DEFAULT,
4057c478bd9Sstevel@tonic-gate 							c);
4067c478bd9Sstevel@tonic-gate 				}
4077c478bd9Sstevel@tonic-gate 				if (c == SM_IO_EOF)
4087c478bd9Sstevel@tonic-gate 					goto readerr;
4097c478bd9Sstevel@tonic-gate 				if (SevenBitInput)
4107c478bd9Sstevel@tonic-gate 					c &= 0x7f;
4117c478bd9Sstevel@tonic-gate 				else
4127c478bd9Sstevel@tonic-gate 					HasEightBits |= bitset(0x80, c);
4137c478bd9Sstevel@tonic-gate 			}
4147c478bd9Sstevel@tonic-gate 			if (tTd(30, 94))
4157c478bd9Sstevel@tonic-gate 				sm_dprintf("istate=%d, c=%c (0x%x)\n",
4167c478bd9Sstevel@tonic-gate 					istate, (char) c, c);
4177c478bd9Sstevel@tonic-gate 			switch (istate)
4187c478bd9Sstevel@tonic-gate 			{
4197c478bd9Sstevel@tonic-gate 			  case IS_BOL:
4207c478bd9Sstevel@tonic-gate 				if (c == '.')
4217c478bd9Sstevel@tonic-gate 				{
4227c478bd9Sstevel@tonic-gate 					istate = IS_DOT;
4237c478bd9Sstevel@tonic-gate 					continue;
4247c478bd9Sstevel@tonic-gate 				}
4257c478bd9Sstevel@tonic-gate 				break;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 			  case IS_DOT:
4287c478bd9Sstevel@tonic-gate 				if (c == '\n' && !ignrdot &&
4297c478bd9Sstevel@tonic-gate 				    !bitset(EF_NL_NOT_EOL, e->e_flags))
4307c478bd9Sstevel@tonic-gate 					goto readerr;
4317c478bd9Sstevel@tonic-gate 				else if (c == '\r' &&
4327c478bd9Sstevel@tonic-gate 					 !bitset(EF_CRLF_NOT_EOL, e->e_flags))
4337c478bd9Sstevel@tonic-gate 				{
4347c478bd9Sstevel@tonic-gate 					istate = IS_DOTCR;
4357c478bd9Sstevel@tonic-gate 					continue;
4367c478bd9Sstevel@tonic-gate 				}
4377c478bd9Sstevel@tonic-gate 				else if (ignrdot ||
4387c478bd9Sstevel@tonic-gate 					 (c != '.' &&
4397c478bd9Sstevel@tonic-gate 					  OpMode != MD_SMTP &&
4407c478bd9Sstevel@tonic-gate 					  OpMode != MD_DAEMON &&
4417c478bd9Sstevel@tonic-gate 					  OpMode != MD_ARPAFTP))
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 				{
444445f2479Sjbeck 					SM_ASSERT(pbp < peekbuf +
445445f2479Sjbeck 							sizeof(peekbuf));
4467c478bd9Sstevel@tonic-gate 					*pbp++ = c;
4477c478bd9Sstevel@tonic-gate 					c = '.';
4487c478bd9Sstevel@tonic-gate 				}
4497c478bd9Sstevel@tonic-gate 				break;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 			  case IS_DOTCR:
4527c478bd9Sstevel@tonic-gate 				if (c == '\n' && !ignrdot)
4537c478bd9Sstevel@tonic-gate 					goto readerr;
4547c478bd9Sstevel@tonic-gate 				else
4557c478bd9Sstevel@tonic-gate 				{
4567c478bd9Sstevel@tonic-gate 					/* push back the ".\rx" */
457445f2479Sjbeck 					SM_ASSERT(pbp < peekbuf +
458445f2479Sjbeck 							sizeof(peekbuf));
4597c478bd9Sstevel@tonic-gate 					*pbp++ = c;
4607c478bd9Sstevel@tonic-gate 					if (OpMode != MD_SMTP &&
4617c478bd9Sstevel@tonic-gate 					    OpMode != MD_DAEMON &&
4627c478bd9Sstevel@tonic-gate 					    OpMode != MD_ARPAFTP)
4637c478bd9Sstevel@tonic-gate 					{
4647c478bd9Sstevel@tonic-gate 						SM_ASSERT(pbp < peekbuf +
4657c478bd9Sstevel@tonic-gate 							 sizeof(peekbuf));
4667c478bd9Sstevel@tonic-gate 						*pbp++ = '\r';
4677c478bd9Sstevel@tonic-gate 						c = '.';
4687c478bd9Sstevel@tonic-gate 					}
4697c478bd9Sstevel@tonic-gate 					else
4707c478bd9Sstevel@tonic-gate 						c = '\r';
4717c478bd9Sstevel@tonic-gate 				}
4727c478bd9Sstevel@tonic-gate 				break;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 			  case IS_CR:
4757c478bd9Sstevel@tonic-gate 				if (c == '\n')
4767c478bd9Sstevel@tonic-gate 					istate = IS_BOL;
4777c478bd9Sstevel@tonic-gate 				else
4787c478bd9Sstevel@tonic-gate 				{
4797c478bd9Sstevel@tonic-gate 					(void) sm_io_ungetc(fp, SM_TIME_DEFAULT,
4807c478bd9Sstevel@tonic-gate 							    c);
4817c478bd9Sstevel@tonic-gate 					c = '\r';
4827c478bd9Sstevel@tonic-gate 					istate = IS_NORM;
4837c478bd9Sstevel@tonic-gate 				}
4847c478bd9Sstevel@tonic-gate 				goto bufferchar;
4857c478bd9Sstevel@tonic-gate 			}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 			if (c == '\r' && !bitset(EF_CRLF_NOT_EOL, e->e_flags))
4887c478bd9Sstevel@tonic-gate 			{
4897c478bd9Sstevel@tonic-gate 				istate = IS_CR;
4907c478bd9Sstevel@tonic-gate 				continue;
4917c478bd9Sstevel@tonic-gate 			}
4927c478bd9Sstevel@tonic-gate 			else if (c == '\n' && !bitset(EF_NL_NOT_EOL,
4937c478bd9Sstevel@tonic-gate 						      e->e_flags))
4947c478bd9Sstevel@tonic-gate 				istate = IS_BOL;
4957c478bd9Sstevel@tonic-gate 			else
4967c478bd9Sstevel@tonic-gate 				istate = IS_NORM;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate bufferchar:
4997c478bd9Sstevel@tonic-gate 			if (!headeronly)
5007c478bd9Sstevel@tonic-gate 			{
5017c478bd9Sstevel@tonic-gate 				/* no overflow? */
5027c478bd9Sstevel@tonic-gate 				if (e->e_msgsize >= 0)
5037c478bd9Sstevel@tonic-gate 				{
5047c478bd9Sstevel@tonic-gate 					e->e_msgsize++;
5057c478bd9Sstevel@tonic-gate 					if (MaxMessageSize > 0 &&
5067c478bd9Sstevel@tonic-gate 					    !bitset(EF_TOOBIG, e->e_flags) &&
5077c478bd9Sstevel@tonic-gate 					    e->e_msgsize > MaxMessageSize)
5087c478bd9Sstevel@tonic-gate 						 e->e_flags |= EF_TOOBIG;
5097c478bd9Sstevel@tonic-gate 				}
5107c478bd9Sstevel@tonic-gate 			}
5117c478bd9Sstevel@tonic-gate 			switch (mstate)
5127c478bd9Sstevel@tonic-gate 			{
5137c478bd9Sstevel@tonic-gate 			  case MS_BODY:
5147c478bd9Sstevel@tonic-gate 				/* just put the character out */
5157c478bd9Sstevel@tonic-gate 				if (!bitset(EF_TOOBIG, e->e_flags))
5167c478bd9Sstevel@tonic-gate 					(void) sm_io_putc(df, SM_TIME_DEFAULT,
5177c478bd9Sstevel@tonic-gate 							  c);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 			  case MS_DISCARD:
5227c478bd9Sstevel@tonic-gate 				continue;
5237c478bd9Sstevel@tonic-gate 			}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 			SM_ASSERT(mstate == MS_UFROM || mstate == MS_HEADER);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 			/* header -- buffer up */
5287c478bd9Sstevel@tonic-gate 			if (bp >= &buf[buflen - 2])
5297c478bd9Sstevel@tonic-gate 			{
5307c478bd9Sstevel@tonic-gate 				char *obuf;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 				/* out of space for header */
5337c478bd9Sstevel@tonic-gate 				obuf = buf;
5347c478bd9Sstevel@tonic-gate 				if (buflen < MEMCHUNKSIZE)
5357c478bd9Sstevel@tonic-gate 					buflen *= 2;
5367c478bd9Sstevel@tonic-gate 				else
5377c478bd9Sstevel@tonic-gate 					buflen += MEMCHUNKSIZE;
538445f2479Sjbeck 				if (buflen <= 0)
539445f2479Sjbeck 				{
540445f2479Sjbeck 					sm_syslog(LOG_NOTICE, e->e_id,
541445f2479Sjbeck 						  "header overflow from %s during message collect",
542445f2479Sjbeck 						  CURHOSTNAME);
543445f2479Sjbeck 					errno = 0;
544445f2479Sjbeck 					e->e_flags |= EF_CLRQUEUE;
545445f2479Sjbeck 					e->e_status = "5.6.0";
546445f2479Sjbeck 					usrerrenh(e->e_status,
547445f2479Sjbeck 						  "552 Headers too large");
548445f2479Sjbeck 					goto discard;
549445f2479Sjbeck 				}
5507c478bd9Sstevel@tonic-gate 				buf = xalloc(buflen);
5517c478bd9Sstevel@tonic-gate 				memmove(buf, obuf, bp - obuf);
5527c478bd9Sstevel@tonic-gate 				bp = &buf[bp - obuf];
5537c478bd9Sstevel@tonic-gate 				if (obuf != bufbuf)
5547c478bd9Sstevel@tonic-gate 					sm_free(obuf);  /* XXX */
5557c478bd9Sstevel@tonic-gate 			}
5567c478bd9Sstevel@tonic-gate 
557*058561cbSjbeck 			if (c != '\0')
5587c478bd9Sstevel@tonic-gate 			{
5597c478bd9Sstevel@tonic-gate 				*bp++ = c;
5607c478bd9Sstevel@tonic-gate 				++hdrslen;
5617c478bd9Sstevel@tonic-gate 				if (!headeronly &&
5627c478bd9Sstevel@tonic-gate 				    MaxHeadersLength > 0 &&
5637c478bd9Sstevel@tonic-gate 				    hdrslen > MaxHeadersLength)
5647c478bd9Sstevel@tonic-gate 				{
5657c478bd9Sstevel@tonic-gate 					sm_syslog(LOG_NOTICE, e->e_id,
5667c478bd9Sstevel@tonic-gate 						  "headers too large (%d max) from %s during message collect",
5677c478bd9Sstevel@tonic-gate 						  MaxHeadersLength,
5687c478bd9Sstevel@tonic-gate 						  CURHOSTNAME);
5697c478bd9Sstevel@tonic-gate 					errno = 0;
5707c478bd9Sstevel@tonic-gate 					e->e_flags |= EF_CLRQUEUE;
5717c478bd9Sstevel@tonic-gate 					e->e_status = "5.6.0";
5727c478bd9Sstevel@tonic-gate 					usrerrenh(e->e_status,
5737c478bd9Sstevel@tonic-gate 						  "552 Headers too large (%d max)",
5747c478bd9Sstevel@tonic-gate 						  MaxHeadersLength);
575445f2479Sjbeck   discard:
5767c478bd9Sstevel@tonic-gate 					mstate = MS_DISCARD;
5777c478bd9Sstevel@tonic-gate 				}
5787c478bd9Sstevel@tonic-gate 			}
5797c478bd9Sstevel@tonic-gate 			if (istate == IS_BOL)
5807c478bd9Sstevel@tonic-gate 				break;
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 		*bp = '\0';
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate nextstate:
5857c478bd9Sstevel@tonic-gate 		if (tTd(30, 35))
5867c478bd9Sstevel@tonic-gate 			sm_dprintf("nextstate, istate=%d, mstate=%d, line=\"%s\"\n",
5877c478bd9Sstevel@tonic-gate 				istate, mstate, buf);
5887c478bd9Sstevel@tonic-gate 		switch (mstate)
5897c478bd9Sstevel@tonic-gate 		{
5907c478bd9Sstevel@tonic-gate 		  case MS_UFROM:
5917c478bd9Sstevel@tonic-gate 			mstate = MS_HEADER;
5927c478bd9Sstevel@tonic-gate #ifndef NOTUNIX
5937c478bd9Sstevel@tonic-gate 			if (strncmp(buf, "From ", 5) == 0)
5947c478bd9Sstevel@tonic-gate 			{
5957c478bd9Sstevel@tonic-gate 				bp = buf;
5967c478bd9Sstevel@tonic-gate 				eatfrom(buf, e);
5977c478bd9Sstevel@tonic-gate 				continue;
5987c478bd9Sstevel@tonic-gate 			}
5997c478bd9Sstevel@tonic-gate #endif /* ! NOTUNIX */
6007c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 		  case MS_HEADER:
6037c478bd9Sstevel@tonic-gate 			if (!isheader(buf))
6047c478bd9Sstevel@tonic-gate 			{
6057c478bd9Sstevel@tonic-gate 				mstate = MS_BODY;
6067c478bd9Sstevel@tonic-gate 				goto nextstate;
6077c478bd9Sstevel@tonic-gate 			}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 			/* check for possible continuation line */
6107c478bd9Sstevel@tonic-gate 			do
6117c478bd9Sstevel@tonic-gate 			{
6127c478bd9Sstevel@tonic-gate 				sm_io_clearerr(fp);
6137c478bd9Sstevel@tonic-gate 				errno = 0;
6147c478bd9Sstevel@tonic-gate 				c = sm_io_getc(fp, SM_TIME_DEFAULT);
615445f2479Sjbeck 
616445f2479Sjbeck 				/* timeout? */
617445f2479Sjbeck 				if (c == SM_IO_EOF && errno == EAGAIN
618445f2479Sjbeck 				    && smtpmode)
619445f2479Sjbeck 				{
620445f2479Sjbeck 					/*
621445f2479Sjbeck 					**  Override e_message in
622445f2479Sjbeck 					**  usrerr() as this is the
623445f2479Sjbeck 					**  reason for failure that
624445f2479Sjbeck 					**  should be logged for
625445f2479Sjbeck 					**  undelivered recipients.
626445f2479Sjbeck 					*/
627445f2479Sjbeck 
628445f2479Sjbeck 					e->e_message = NULL;
629445f2479Sjbeck 					errno = 0;
630445f2479Sjbeck 					inputerr = true;
631445f2479Sjbeck 					goto readabort;
632445f2479Sjbeck 				}
6337c478bd9Sstevel@tonic-gate 			} while (c == SM_IO_EOF && errno == EINTR);
6347c478bd9Sstevel@tonic-gate 			if (c != SM_IO_EOF)
6357c478bd9Sstevel@tonic-gate 				(void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
6367c478bd9Sstevel@tonic-gate 			if (c == ' ' || c == '\t')
6377c478bd9Sstevel@tonic-gate 			{
6387c478bd9Sstevel@tonic-gate 				/* yep -- defer this */
6397c478bd9Sstevel@tonic-gate 				continue;
6407c478bd9Sstevel@tonic-gate 			}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 			SM_ASSERT(bp > buf);
643445f2479Sjbeck 
644445f2479Sjbeck 			/* guaranteed by isheader(buf) */
645445f2479Sjbeck 			SM_ASSERT(*(bp - 1) != '\n' || bp > buf + 1);
646445f2479Sjbeck 
647445f2479Sjbeck 			/* trim off trailing CRLF or NL */
6487c478bd9Sstevel@tonic-gate 			if (*--bp != '\n' || *--bp != '\r')
6497c478bd9Sstevel@tonic-gate 				bp++;
6507c478bd9Sstevel@tonic-gate 			*bp = '\0';
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 			if (bitset(H_EOH, chompheader(buf,
6537c478bd9Sstevel@tonic-gate 						      CHHDR_CHECK | CHHDR_USER,
6547c478bd9Sstevel@tonic-gate 						      hdrp, e)))
6557c478bd9Sstevel@tonic-gate 			{
6567c478bd9Sstevel@tonic-gate 				mstate = MS_BODY;
6577c478bd9Sstevel@tonic-gate 				goto nextstate;
6587c478bd9Sstevel@tonic-gate 			}
6597c478bd9Sstevel@tonic-gate 			numhdrs++;
6607c478bd9Sstevel@tonic-gate 			break;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 		  case MS_BODY:
6637c478bd9Sstevel@tonic-gate 			if (tTd(30, 1))
6647c478bd9Sstevel@tonic-gate 				sm_dprintf("EOH\n");
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 			if (headeronly)
6677c478bd9Sstevel@tonic-gate 				goto readerr;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 			df = collect_eoh(e, numhdrs, hdrslen);
6707c478bd9Sstevel@tonic-gate 			if (df == NULL)
6717c478bd9Sstevel@tonic-gate 				e->e_flags |= EF_TOOBIG;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 			bp = buf;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 			/* toss blank line */
6767c478bd9Sstevel@tonic-gate 			if ((!bitset(EF_CRLF_NOT_EOL, e->e_flags) &&
6777c478bd9Sstevel@tonic-gate 				bp[0] == '\r' && bp[1] == '\n') ||
6787c478bd9Sstevel@tonic-gate 			    (!bitset(EF_NL_NOT_EOL, e->e_flags) &&
6797c478bd9Sstevel@tonic-gate 				bp[0] == '\n'))
6807c478bd9Sstevel@tonic-gate 			{
6817c478bd9Sstevel@tonic-gate 				break;
6827c478bd9Sstevel@tonic-gate 			}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 			/* if not a blank separator, write it out */
6857c478bd9Sstevel@tonic-gate 			if (!bitset(EF_TOOBIG, e->e_flags))
6867c478bd9Sstevel@tonic-gate 			{
6877c478bd9Sstevel@tonic-gate 				while (*bp != '\0')
6887c478bd9Sstevel@tonic-gate 					(void) sm_io_putc(df, SM_TIME_DEFAULT,
6897c478bd9Sstevel@tonic-gate 							  *bp++);
6907c478bd9Sstevel@tonic-gate 			}
6917c478bd9Sstevel@tonic-gate 			break;
6927c478bd9Sstevel@tonic-gate 		}
6937c478bd9Sstevel@tonic-gate 		bp = buf;
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate readerr:
6977c478bd9Sstevel@tonic-gate 	if ((sm_io_eof(fp) && smtpmode) || sm_io_error(fp))
6987c478bd9Sstevel@tonic-gate 	{
6997c478bd9Sstevel@tonic-gate 		const char *errmsg;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		if (sm_io_eof(fp))
7027c478bd9Sstevel@tonic-gate 			errmsg = "unexpected close";
7037c478bd9Sstevel@tonic-gate 		else
7047c478bd9Sstevel@tonic-gate 			errmsg = sm_errstring(errno);
7057c478bd9Sstevel@tonic-gate 		if (tTd(30, 1))
7067c478bd9Sstevel@tonic-gate 			sm_dprintf("collect: premature EOM: %s\n", errmsg);
7077c478bd9Sstevel@tonic-gate 		if (LogLevel > 1)
7087c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_WARNING, e->e_id,
7097c478bd9Sstevel@tonic-gate 				"collect: premature EOM: %s", errmsg);
7107c478bd9Sstevel@tonic-gate 		inputerr = true;
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	if (headeronly)
7147c478bd9Sstevel@tonic-gate 		return;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	if (mstate != MS_BODY)
7177c478bd9Sstevel@tonic-gate 	{
7187c478bd9Sstevel@tonic-gate 		/* no body or discard, so we never opened the data file */
7197c478bd9Sstevel@tonic-gate 		SM_ASSERT(df == NULL);
7207c478bd9Sstevel@tonic-gate 		df = collect_eoh(e, numhdrs, hdrslen);
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	if (df == NULL)
7247c478bd9Sstevel@tonic-gate 	{
7257c478bd9Sstevel@tonic-gate 		/* skip next few clauses */
7267c478bd9Sstevel@tonic-gate 		/* EMPTY */
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 	else if (sm_io_flush(df, SM_TIME_DEFAULT) != 0 || sm_io_error(df))
7297c478bd9Sstevel@tonic-gate 	{
7307c478bd9Sstevel@tonic-gate 		dferror(df, "sm_io_flush||sm_io_error", e);
7317c478bd9Sstevel@tonic-gate 		flush_errors(true);
7327c478bd9Sstevel@tonic-gate 		finis(true, true, ExitStat);
7337c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate 	else if (SuperSafe == SAFE_NO ||
7367c478bd9Sstevel@tonic-gate 		 SuperSafe == SAFE_INTERACTIVE ||
7377c478bd9Sstevel@tonic-gate 		 (SuperSafe == SAFE_REALLY_POSTMILTER && smtpmode))
7387c478bd9Sstevel@tonic-gate 	{
7397c478bd9Sstevel@tonic-gate 		/* skip next few clauses */
7407c478bd9Sstevel@tonic-gate 		/* EMPTY */
7417c478bd9Sstevel@tonic-gate 		/* Note: updfs() is not called in this case! */
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 	else if (sm_io_setinfo(df, SM_BF_COMMIT, NULL) < 0 && errno != EINVAL)
7447c478bd9Sstevel@tonic-gate 	{
7457c478bd9Sstevel@tonic-gate 		int save_errno = errno;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 		if (save_errno == EEXIST)
7487c478bd9Sstevel@tonic-gate 		{
7497c478bd9Sstevel@tonic-gate 			char *dfile;
7507c478bd9Sstevel@tonic-gate 			struct stat st;
7517c478bd9Sstevel@tonic-gate 			int dfd;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 			dfile = queuename(e, DATAFL_LETTER);
7547c478bd9Sstevel@tonic-gate 			if (stat(dfile, &st) < 0)
7557c478bd9Sstevel@tonic-gate 				st.st_size = -1;
7567c478bd9Sstevel@tonic-gate 			errno = EEXIST;
7577c478bd9Sstevel@tonic-gate 			syserr("@collect: bfcommit(%s): already on disk, size=%ld",
7587c478bd9Sstevel@tonic-gate 			       dfile, (long) st.st_size);
7597c478bd9Sstevel@tonic-gate 			dfd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL);
7607c478bd9Sstevel@tonic-gate 			if (dfd >= 0)
7617c478bd9Sstevel@tonic-gate 				dumpfd(dfd, true, true);
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 		errno = save_errno;
7647c478bd9Sstevel@tonic-gate 		dferror(df, "bfcommit", e);
7657c478bd9Sstevel@tonic-gate 		flush_errors(true);
7667c478bd9Sstevel@tonic-gate 		finis(save_errno != EEXIST, true, ExitStat);
7677c478bd9Sstevel@tonic-gate 	}
7687c478bd9Sstevel@tonic-gate 	else if ((afd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL)) < 0)
7697c478bd9Sstevel@tonic-gate 	{
7707c478bd9Sstevel@tonic-gate 		dferror(df, "sm_io_getinfo", e);
7717c478bd9Sstevel@tonic-gate 		flush_errors(true);
7727c478bd9Sstevel@tonic-gate 		finis(true, true, ExitStat);
7737c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 	else if (fsync(afd) < 0)
7767c478bd9Sstevel@tonic-gate 	{
7777c478bd9Sstevel@tonic-gate 		dferror(df, "fsync", e);
7787c478bd9Sstevel@tonic-gate 		flush_errors(true);
7797c478bd9Sstevel@tonic-gate 		finis(true, true, ExitStat);
7807c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 	else if (sm_io_close(df, SM_TIME_DEFAULT) < 0)
7837c478bd9Sstevel@tonic-gate 	{
7847c478bd9Sstevel@tonic-gate 		dferror(df, "sm_io_close", e);
7857c478bd9Sstevel@tonic-gate 		flush_errors(true);
7867c478bd9Sstevel@tonic-gate 		finis(true, true, ExitStat);
7877c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate 	else
7907c478bd9Sstevel@tonic-gate 	{
7917c478bd9Sstevel@tonic-gate 		/* everything is happily flushed to disk */
7927c478bd9Sstevel@tonic-gate 		df = NULL;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 		/* remove from available space in filesystem */
7957c478bd9Sstevel@tonic-gate 		updfs(e, 0, 1, "collect");
7967c478bd9Sstevel@tonic-gate 	}
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	/* An EOF when running SMTP is an error */
799445f2479Sjbeck   readabort:
8007c478bd9Sstevel@tonic-gate 	if (inputerr && (OpMode == MD_SMTP || OpMode == MD_DAEMON))
8017c478bd9Sstevel@tonic-gate 	{
8027c478bd9Sstevel@tonic-gate 		char *host;
8037c478bd9Sstevel@tonic-gate 		char *problem;
8047c478bd9Sstevel@tonic-gate 		ADDRESS *q;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 		host = RealHostName;
8077c478bd9Sstevel@tonic-gate 		if (host == NULL)
8087c478bd9Sstevel@tonic-gate 			host = "localhost";
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 		if (sm_io_eof(fp))
8117c478bd9Sstevel@tonic-gate 			problem = "unexpected close";
8127c478bd9Sstevel@tonic-gate 		else if (sm_io_error(fp))
8137c478bd9Sstevel@tonic-gate 			problem = "I/O error";
8147c478bd9Sstevel@tonic-gate 		else
8157c478bd9Sstevel@tonic-gate 			problem = "read timeout";
8167c478bd9Sstevel@tonic-gate 		if (LogLevel > 0 && sm_io_eof(fp))
8177c478bd9Sstevel@tonic-gate 			sm_syslog(LOG_NOTICE, e->e_id,
8187c478bd9Sstevel@tonic-gate 				"collect: %s on connection from %.100s, sender=%s",
8197c478bd9Sstevel@tonic-gate 				problem, host,
8207c478bd9Sstevel@tonic-gate 				shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
8217c478bd9Sstevel@tonic-gate 		if (sm_io_eof(fp))
822445f2479Sjbeck 			usrerr("421 4.4.1 collect: %s on connection from %s, from=%s",
8237c478bd9Sstevel@tonic-gate 				problem, host,
8247c478bd9Sstevel@tonic-gate 				shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
8257c478bd9Sstevel@tonic-gate 		else
826445f2479Sjbeck 			syserr("421 4.4.1 collect: %s on connection from %s, from=%s",
8277c478bd9Sstevel@tonic-gate 				problem, host,
8287c478bd9Sstevel@tonic-gate 				shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
829445f2479Sjbeck 		flush_errors(true);
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 		/* don't return an error indication */
8327c478bd9Sstevel@tonic-gate 		e->e_to = NULL;
8337c478bd9Sstevel@tonic-gate 		e->e_flags &= ~EF_FATALERRS;
8347c478bd9Sstevel@tonic-gate 		e->e_flags |= EF_CLRQUEUE;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 		/* Don't send any message notification to sender */
8377c478bd9Sstevel@tonic-gate 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
8387c478bd9Sstevel@tonic-gate 		{
8397c478bd9Sstevel@tonic-gate 			if (QS_IS_DEAD(q->q_state))
8407c478bd9Sstevel@tonic-gate 				continue;
8417c478bd9Sstevel@tonic-gate 			q->q_state = QS_FATALERR;
8427c478bd9Sstevel@tonic-gate 		}
8437c478bd9Sstevel@tonic-gate 
8443ee0e492Sjbeck 		(void) sm_io_close(df, SM_TIME_DEFAULT);
8453ee0e492Sjbeck 		df = NULL;
8467c478bd9Sstevel@tonic-gate 		finis(true, true, ExitStat);
8477c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	/* Log collection information. */
8517c478bd9Sstevel@tonic-gate 	if (bitset(EF_LOGSENDER, e->e_flags) && LogLevel > 4)
8527c478bd9Sstevel@tonic-gate 	{
8537c478bd9Sstevel@tonic-gate 		logsender(e, e->e_msgid);
8547c478bd9Sstevel@tonic-gate 		e->e_flags &= ~EF_LOGSENDER;
8557c478bd9Sstevel@tonic-gate 	}
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	/* check for message too large */
8587c478bd9Sstevel@tonic-gate 	if (bitset(EF_TOOBIG, e->e_flags))
8597c478bd9Sstevel@tonic-gate 	{
8607c478bd9Sstevel@tonic-gate 		e->e_flags |= EF_NO_BODY_RETN|EF_CLRQUEUE;
8617c478bd9Sstevel@tonic-gate 		if (!bitset(EF_FATALERRS, e->e_flags))
8627c478bd9Sstevel@tonic-gate 		{
8637c478bd9Sstevel@tonic-gate 			e->e_status = "5.2.3";
8647c478bd9Sstevel@tonic-gate 			usrerrenh(e->e_status,
8657c478bd9Sstevel@tonic-gate 				"552 Message exceeds maximum fixed size (%ld)",
8667c478bd9Sstevel@tonic-gate 				MaxMessageSize);
8677c478bd9Sstevel@tonic-gate 			if (LogLevel > 6)
8687c478bd9Sstevel@tonic-gate 				sm_syslog(LOG_NOTICE, e->e_id,
8697c478bd9Sstevel@tonic-gate 					"message size (%ld) exceeds maximum (%ld)",
8707c478bd9Sstevel@tonic-gate 					e->e_msgsize, MaxMessageSize);
8717c478bd9Sstevel@tonic-gate 		}
8727c478bd9Sstevel@tonic-gate 	}
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	/* check for illegal 8-bit data */
8757c478bd9Sstevel@tonic-gate 	if (HasEightBits)
8767c478bd9Sstevel@tonic-gate 	{
8777c478bd9Sstevel@tonic-gate 		e->e_flags |= EF_HAS8BIT;
8787c478bd9Sstevel@tonic-gate 		if (!bitset(MM_PASS8BIT|MM_MIME8BIT, MimeMode) &&
8797c478bd9Sstevel@tonic-gate 		    !bitset(EF_IS_MIME, e->e_flags))
8807c478bd9Sstevel@tonic-gate 		{
8817c478bd9Sstevel@tonic-gate 			e->e_status = "5.6.1";
8827c478bd9Sstevel@tonic-gate 			usrerrenh(e->e_status, "554 Eight bit data not allowed");
8837c478bd9Sstevel@tonic-gate 		}
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 	else
8867c478bd9Sstevel@tonic-gate 	{
8877c478bd9Sstevel@tonic-gate 		/* if it claimed to be 8 bits, well, it lied.... */
8887c478bd9Sstevel@tonic-gate 		if (e->e_bodytype != NULL &&
8897c478bd9Sstevel@tonic-gate 		    sm_strcasecmp(e->e_bodytype, "8BITMIME") == 0)
8907c478bd9Sstevel@tonic-gate 			e->e_bodytype = "7BIT";
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	if (SuperSafe == SAFE_REALLY && !bitset(EF_FATALERRS, e->e_flags))
8947c478bd9Sstevel@tonic-gate 	{
8957c478bd9Sstevel@tonic-gate 		char *dfname = queuename(e, DATAFL_LETTER);
8967c478bd9Sstevel@tonic-gate 		if ((e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, dfname,
8977c478bd9Sstevel@tonic-gate 					   SM_IO_RDONLY_B, NULL)) == NULL)
8987c478bd9Sstevel@tonic-gate 		{
8997c478bd9Sstevel@tonic-gate 			/* we haven't acked receipt yet, so just chuck this */
9007c478bd9Sstevel@tonic-gate 			syserr("@Cannot reopen %s", dfname);
9017c478bd9Sstevel@tonic-gate 			finis(true, true, ExitStat);
9027c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
9037c478bd9Sstevel@tonic-gate 		}
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 	else
9067c478bd9Sstevel@tonic-gate 		e->e_dfp = df;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	/* collect statistics */
9097c478bd9Sstevel@tonic-gate 	if (OpMode != MD_VERIFY)
9107c478bd9Sstevel@tonic-gate 	{
9117c478bd9Sstevel@tonic-gate 		/*
9127c478bd9Sstevel@tonic-gate 		**  Recalculate e_msgpriority, it is done at in eatheader()
9137c478bd9Sstevel@tonic-gate 		**  which is called (in 8.12) after the header is collected,
9147c478bd9Sstevel@tonic-gate 		**  hence e_msgsize is (most likely) incorrect.
9157c478bd9Sstevel@tonic-gate 		*/
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 		e->e_msgpriority = e->e_msgsize
9187c478bd9Sstevel@tonic-gate 				 - e->e_class * WkClassFact
9197c478bd9Sstevel@tonic-gate 				 + e->e_nrcpts * WkRecipFact;
9207c478bd9Sstevel@tonic-gate 		markstats(e, (ADDRESS *) NULL, STATS_NORMAL);
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate /*
9257c478bd9Sstevel@tonic-gate **  DFERROR -- signal error on writing the data file.
9267c478bd9Sstevel@tonic-gate **
9277c478bd9Sstevel@tonic-gate **	Called by collect().  Collect() always terminates the process
9287c478bd9Sstevel@tonic-gate **	immediately after calling dferror(), which means that the SMTP
9297c478bd9Sstevel@tonic-gate **	session will be terminated, which means that any error message
9307c478bd9Sstevel@tonic-gate **	issued by dferror must be a 421 error, as per RFC 821.
9317c478bd9Sstevel@tonic-gate **
9327c478bd9Sstevel@tonic-gate **	Parameters:
9337c478bd9Sstevel@tonic-gate **		df -- the file pointer for the data file.
9347c478bd9Sstevel@tonic-gate **		msg -- detailed message.
9357c478bd9Sstevel@tonic-gate **		e -- the current envelope.
9367c478bd9Sstevel@tonic-gate **
9377c478bd9Sstevel@tonic-gate **	Returns:
9387c478bd9Sstevel@tonic-gate **		none.
9397c478bd9Sstevel@tonic-gate **
9407c478bd9Sstevel@tonic-gate **	Side Effects:
9417c478bd9Sstevel@tonic-gate **		Gives an error message.
9427c478bd9Sstevel@tonic-gate **		Arranges for following output to go elsewhere.
9437c478bd9Sstevel@tonic-gate */
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate void
9467c478bd9Sstevel@tonic-gate dferror(df, msg, e)
9477c478bd9Sstevel@tonic-gate 	SM_FILE_T *volatile df;
9487c478bd9Sstevel@tonic-gate 	char *msg;
9497c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
9507c478bd9Sstevel@tonic-gate {
9517c478bd9Sstevel@tonic-gate 	char *dfname;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	dfname = queuename(e, DATAFL_LETTER);
9547c478bd9Sstevel@tonic-gate 	setstat(EX_IOERR);
9557c478bd9Sstevel@tonic-gate 	if (errno == ENOSPC)
9567c478bd9Sstevel@tonic-gate 	{
9577c478bd9Sstevel@tonic-gate #if STAT64 > 0
9587c478bd9Sstevel@tonic-gate 		struct stat64 st;
9597c478bd9Sstevel@tonic-gate #else /* STAT64 > 0 */
9607c478bd9Sstevel@tonic-gate 		struct stat st;
9617c478bd9Sstevel@tonic-gate #endif /* STAT64 > 0 */
9627c478bd9Sstevel@tonic-gate 		long avail;
9637c478bd9Sstevel@tonic-gate 		long bsize;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 		e->e_flags |= EF_NO_BODY_RETN;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 		if (
9687c478bd9Sstevel@tonic-gate #if STAT64 > 0
9697c478bd9Sstevel@tonic-gate 		    fstat64(sm_io_getinfo(df, SM_IO_WHAT_FD, NULL), &st)
9707c478bd9Sstevel@tonic-gate #else /* STAT64 > 0 */
9717c478bd9Sstevel@tonic-gate 		    fstat(sm_io_getinfo(df, SM_IO_WHAT_FD, NULL), &st)
9727c478bd9Sstevel@tonic-gate #endif /* STAT64 > 0 */
9737c478bd9Sstevel@tonic-gate 		    < 0)
9747c478bd9Sstevel@tonic-gate 		  st.st_size = 0;
9757c478bd9Sstevel@tonic-gate 		(void) sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, dfname,
9767c478bd9Sstevel@tonic-gate 				    SM_IO_WRONLY_B, NULL, df);
9777c478bd9Sstevel@tonic-gate 		if (st.st_size <= 0)
9787c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
9797c478bd9Sstevel@tonic-gate 				"\n*** Mail could not be accepted");
9807c478bd9Sstevel@tonic-gate 		else
9817c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
9827c478bd9Sstevel@tonic-gate 				"\n*** Mail of at least %llu bytes could not be accepted\n",
9837c478bd9Sstevel@tonic-gate 				(ULONGLONG_T) st.st_size);
9847c478bd9Sstevel@tonic-gate 		(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
9857c478bd9Sstevel@tonic-gate 			"*** at %s due to lack of disk space for temp file.\n",
9867c478bd9Sstevel@tonic-gate 			MyHostName);
9877c478bd9Sstevel@tonic-gate 		avail = freediskspace(qid_printqueue(e->e_qgrp, e->e_qdir),
9887c478bd9Sstevel@tonic-gate 				      &bsize);
9897c478bd9Sstevel@tonic-gate 		if (avail > 0)
9907c478bd9Sstevel@tonic-gate 		{
9917c478bd9Sstevel@tonic-gate 			if (bsize > 1024)
9927c478bd9Sstevel@tonic-gate 				avail *= bsize / 1024;
9937c478bd9Sstevel@tonic-gate 			else if (bsize < 1024)
9947c478bd9Sstevel@tonic-gate 				avail /= 1024 / bsize;
9957c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
9967c478bd9Sstevel@tonic-gate 				"*** Currently, %ld kilobytes are available for mail temp files.\n",
9977c478bd9Sstevel@tonic-gate 				avail);
9987c478bd9Sstevel@tonic-gate 		}
9997c478bd9Sstevel@tonic-gate #if 0
10007c478bd9Sstevel@tonic-gate 		/* Wrong response code; should be 421. */
10017c478bd9Sstevel@tonic-gate 		e->e_status = "4.3.1";
10027c478bd9Sstevel@tonic-gate 		usrerrenh(e->e_status, "452 Out of disk space for temp file");
10037c478bd9Sstevel@tonic-gate #else /* 0 */
10047c478bd9Sstevel@tonic-gate 		syserr("421 4.3.1 Out of disk space for temp file");
10057c478bd9Sstevel@tonic-gate #endif /* 0 */
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 	else
10087c478bd9Sstevel@tonic-gate 		syserr("421 4.3.0 collect: Cannot write %s (%s, uid=%d, gid=%d)",
10097c478bd9Sstevel@tonic-gate 			dfname, msg, (int) geteuid(), (int) getegid());
10107c478bd9Sstevel@tonic-gate 	if (sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, SM_PATH_DEVNULL,
10117c478bd9Sstevel@tonic-gate 			 SM_IO_WRONLY, NULL, df) == NULL)
10127c478bd9Sstevel@tonic-gate 		sm_syslog(LOG_ERR, e->e_id,
10137c478bd9Sstevel@tonic-gate 			  "dferror: sm_io_reopen(\"/dev/null\") failed: %s",
10147c478bd9Sstevel@tonic-gate 			  sm_errstring(errno));
10157c478bd9Sstevel@tonic-gate }
10167c478bd9Sstevel@tonic-gate /*
10177c478bd9Sstevel@tonic-gate **  EATFROM -- chew up a UNIX style from line and process
10187c478bd9Sstevel@tonic-gate **
10197c478bd9Sstevel@tonic-gate **	This does indeed make some assumptions about the format
10207c478bd9Sstevel@tonic-gate **	of UNIX messages.
10217c478bd9Sstevel@tonic-gate **
10227c478bd9Sstevel@tonic-gate **	Parameters:
10237c478bd9Sstevel@tonic-gate **		fm -- the from line.
10247c478bd9Sstevel@tonic-gate **		e -- envelope
10257c478bd9Sstevel@tonic-gate **
10267c478bd9Sstevel@tonic-gate **	Returns:
10277c478bd9Sstevel@tonic-gate **		none.
10287c478bd9Sstevel@tonic-gate **
10297c478bd9Sstevel@tonic-gate **	Side Effects:
10307c478bd9Sstevel@tonic-gate **		extracts what information it can from the header,
10317c478bd9Sstevel@tonic-gate **		such as the date.
10327c478bd9Sstevel@tonic-gate */
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate #ifndef NOTUNIX
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate static char	*DowList[] =
10377c478bd9Sstevel@tonic-gate {
10387c478bd9Sstevel@tonic-gate 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
10397c478bd9Sstevel@tonic-gate };
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate static char	*MonthList[] =
10427c478bd9Sstevel@tonic-gate {
10437c478bd9Sstevel@tonic-gate 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
10447c478bd9Sstevel@tonic-gate 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
10457c478bd9Sstevel@tonic-gate 	NULL
10467c478bd9Sstevel@tonic-gate };
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate static void
10497c478bd9Sstevel@tonic-gate eatfrom(fm, e)
10507c478bd9Sstevel@tonic-gate 	char *volatile fm;
10517c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
10527c478bd9Sstevel@tonic-gate {
10537c478bd9Sstevel@tonic-gate 	register char *p;
10547c478bd9Sstevel@tonic-gate 	register char **dt;
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	if (tTd(30, 2))
10577c478bd9Sstevel@tonic-gate 		sm_dprintf("eatfrom(%s)\n", fm);
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	/* find the date part */
10607c478bd9Sstevel@tonic-gate 	p = fm;
10617c478bd9Sstevel@tonic-gate 	while (*p != '\0')
10627c478bd9Sstevel@tonic-gate 	{
10637c478bd9Sstevel@tonic-gate 		/* skip a word */
10647c478bd9Sstevel@tonic-gate 		while (*p != '\0' && *p != ' ')
10657c478bd9Sstevel@tonic-gate 			p++;
10667c478bd9Sstevel@tonic-gate 		while (*p == ' ')
10677c478bd9Sstevel@tonic-gate 			p++;
10687c478bd9Sstevel@tonic-gate 		if (strlen(p) < 17)
10697c478bd9Sstevel@tonic-gate 		{
10707c478bd9Sstevel@tonic-gate 			/* no room for the date */
10717c478bd9Sstevel@tonic-gate 			return;
10727c478bd9Sstevel@tonic-gate 		}
10737c478bd9Sstevel@tonic-gate 		if (!(isascii(*p) && isupper(*p)) ||
10747c478bd9Sstevel@tonic-gate 		    p[3] != ' ' || p[13] != ':' || p[16] != ':')
10757c478bd9Sstevel@tonic-gate 			continue;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 		/* we have a possible date */
10787c478bd9Sstevel@tonic-gate 		for (dt = DowList; *dt != NULL; dt++)
10797c478bd9Sstevel@tonic-gate 			if (strncmp(*dt, p, 3) == 0)
10807c478bd9Sstevel@tonic-gate 				break;
10817c478bd9Sstevel@tonic-gate 		if (*dt == NULL)
10827c478bd9Sstevel@tonic-gate 			continue;
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 		for (dt = MonthList; *dt != NULL; dt++)
10857c478bd9Sstevel@tonic-gate 		{
10867c478bd9Sstevel@tonic-gate 			if (strncmp(*dt, &p[4], 3) == 0)
10877c478bd9Sstevel@tonic-gate 				break;
10887c478bd9Sstevel@tonic-gate 		}
10897c478bd9Sstevel@tonic-gate 		if (*dt != NULL)
10907c478bd9Sstevel@tonic-gate 			break;
10917c478bd9Sstevel@tonic-gate 	}
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	if (*p != '\0')
10947c478bd9Sstevel@tonic-gate 	{
10957c478bd9Sstevel@tonic-gate 		char *q, buf[25];
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 		/* we have found a date */
10987c478bd9Sstevel@tonic-gate 		(void) sm_strlcpy(buf, p, sizeof(buf));
10997c478bd9Sstevel@tonic-gate 		q = arpadate(buf);
11007c478bd9Sstevel@tonic-gate 		macdefine(&e->e_macro, A_TEMP, 'a', q);
11017c478bd9Sstevel@tonic-gate 	}
11027c478bd9Sstevel@tonic-gate }
11037c478bd9Sstevel@tonic-gate #endif /* ! NOTUNIX */
1104