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