xref: /titanic_44/usr/src/cmd/sendmail/src/collect.c (revision 1a220b56b93ff1dc80855691548503117af4cc10)
1 /*
2  * Copyright (c) 1998-2006 Sendmail, Inc. and its suppliers.
3  *	All rights reserved.
4  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
5  * Copyright (c) 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * By using this file, you agree to the terms and conditions set
9  * forth in the LICENSE file which can be found at the top level of
10  * the sendmail distribution.
11  *
12  */
13 
14 #pragma ident	"%Z%%M%	%I%	%E% SMI"
15 
16 #include <sendmail.h>
17 
18 SM_RCSID("@(#)$Id: collect.c,v 8.273 2006/03/31 18:51:47 ca Exp $")
19 
20 static void	eatfrom __P((char *volatile, ENVELOPE *));
21 static void	collect_doheader __P((ENVELOPE *));
22 static SM_FILE_T *collect_dfopen __P((ENVELOPE *));
23 static SM_FILE_T *collect_eoh __P((ENVELOPE *, int, int));
24 
25 /*
26 **  COLLECT_EOH -- end-of-header processing in collect()
27 **
28 **	Called by collect() when it encounters the blank line
29 **	separating the header from the message body, or when it
30 **	encounters EOF in a message that contains only a header.
31 **
32 **	Parameters:
33 **		e -- envelope
34 **		numhdrs -- number of headers
35 **		hdrslen -- length of headers
36 **
37 **	Results:
38 **		NULL, or handle to open data file
39 **
40 **	Side Effects:
41 **		end-of-header check ruleset is invoked.
42 **		envelope state is updated.
43 **		headers may be added and deleted.
44 **		selects the queue.
45 **		opens the data file.
46 */
47 
48 static SM_FILE_T *
49 collect_eoh(e, numhdrs, hdrslen)
50 	ENVELOPE *e;
51 	int numhdrs;
52 	int hdrslen;
53 {
54 	char hnum[16];
55 	char hsize[16];
56 
57 	/* call the end-of-header check ruleset */
58 	(void) sm_snprintf(hnum, sizeof hnum, "%d", numhdrs);
59 	(void) sm_snprintf(hsize, sizeof hsize, "%d", hdrslen);
60 	if (tTd(30, 10))
61 		sm_dprintf("collect: rscheck(\"check_eoh\", \"%s $| %s\")\n",
62 			   hnum, hsize);
63 	(void) rscheck("check_eoh", hnum, hsize, e, RSF_UNSTRUCTURED|RSF_COUNT,
64 			3, NULL, e->e_id);
65 
66 	/*
67 	**  Process the header,
68 	**  select the queue, open the data file.
69 	*/
70 
71 	collect_doheader(e);
72 	return collect_dfopen(e);
73 }
74 
75 /*
76 **  COLLECT_DOHEADER -- process header in collect()
77 **
78 **	Called by collect() after it has finished parsing the header,
79 **	but before it selects the queue and creates the data file.
80 **	The results of processing the header will affect queue selection.
81 **
82 **	Parameters:
83 **		e -- envelope
84 **
85 **	Results:
86 **		none.
87 **
88 **	Side Effects:
89 **		envelope state is updated.
90 **		headers may be added and deleted.
91 */
92 
93 static void
94 collect_doheader(e)
95 	ENVELOPE *e;
96 {
97 	/*
98 	**  Find out some information from the headers.
99 	**	Examples are who is the from person & the date.
100 	*/
101 
102 	eatheader(e, true, false);
103 
104 	if (GrabTo && e->e_sendqueue == NULL)
105 		usrerr("No recipient addresses found in header");
106 
107 	/*
108 	**  If we have a Return-Receipt-To:, turn it into a DSN.
109 	*/
110 
111 	if (RrtImpliesDsn && hvalue("return-receipt-to", e->e_header) != NULL)
112 	{
113 		ADDRESS *q;
114 
115 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
116 			if (!bitset(QHASNOTIFY, q->q_flags))
117 				q->q_flags |= QHASNOTIFY|QPINGONSUCCESS;
118 	}
119 
120 	/*
121 	**  Add an appropriate recipient line if we have none.
122 	*/
123 
124 	if (hvalue("to", e->e_header) != NULL ||
125 	    hvalue("cc", e->e_header) != NULL ||
126 	    hvalue("apparently-to", e->e_header) != NULL)
127 	{
128 		/* have a valid recipient header -- delete Bcc: headers */
129 		e->e_flags |= EF_DELETE_BCC;
130 	}
131 	else if (hvalue("bcc", e->e_header) == NULL)
132 	{
133 		/* no valid recipient headers */
134 		register ADDRESS *q;
135 		char *hdr = NULL;
136 
137 		/* create a recipient field */
138 		switch (NoRecipientAction)
139 		{
140 		  case NRA_ADD_APPARENTLY_TO:
141 			hdr = "Apparently-To";
142 			break;
143 
144 		  case NRA_ADD_TO:
145 			hdr = "To";
146 			break;
147 
148 		  case NRA_ADD_BCC:
149 			addheader("Bcc", " ", 0, e);
150 			break;
151 
152 		  case NRA_ADD_TO_UNDISCLOSED:
153 			addheader("To", "undisclosed-recipients:;", 0, e);
154 			break;
155 		}
156 
157 		if (hdr != NULL)
158 		{
159 			for (q = e->e_sendqueue; q != NULL; q = q->q_next)
160 			{
161 				if (q->q_alias != NULL)
162 					continue;
163 				if (tTd(30, 3))
164 					sm_dprintf("Adding %s: %s\n",
165 						hdr, q->q_paddr);
166 				addheader(hdr, q->q_paddr, 0, e);
167 			}
168 		}
169 	}
170 }
171 
172 /*
173 **  COLLECT_DFOPEN -- open the message data file
174 **
175 **	Called by collect() after it has finished processing the header.
176 **	Queue selection occurs at this point, possibly based on the
177 **	envelope's recipient list and on header information.
178 **
179 **	Parameters:
180 **		e -- envelope
181 **
182 **	Results:
183 **		NULL, or a pointer to an open data file,
184 **		into which the message body will be written by collect().
185 **
186 **	Side Effects:
187 **		Calls syserr, sets EF_FATALERRS and returns NULL
188 **		if there is insufficient disk space.
189 **		Aborts process if data file could not be opened.
190 **		Otherwise, the queue is selected,
191 **		e->e_{dfino,dfdev,msgsize,flags} are updated,
192 **		and a pointer to an open data file is returned.
193 */
194 
195 static SM_FILE_T *
196 collect_dfopen(e)
197 	ENVELOPE *e;
198 {
199 	MODE_T oldumask = 0;
200 	int dfd;
201 	struct stat stbuf;
202 	SM_FILE_T *df;
203 	char *dfname;
204 
205 	if (!setnewqueue(e))
206 		return NULL;
207 
208 	dfname = queuename(e, DATAFL_LETTER);
209 	if (bitset(S_IWGRP, QueueFileMode))
210 		oldumask = umask(002);
211 	df = bfopen(dfname, QueueFileMode, DataFileBufferSize,
212 		    SFF_OPENASROOT);
213 	if (bitset(S_IWGRP, QueueFileMode))
214 		(void) umask(oldumask);
215 	if (df == NULL)
216 	{
217 		syserr("@Cannot create %s", dfname);
218 		e->e_flags |= EF_NO_BODY_RETN;
219 		flush_errors(true);
220 		finis(false, true, ExitStat);
221 		/* NOTREACHED */
222 	}
223 	dfd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL);
224 	if (dfd < 0 || fstat(dfd, &stbuf) < 0)
225 		e->e_dfino = -1;
226 	else
227 	{
228 		e->e_dfdev = stbuf.st_dev;
229 		e->e_dfino = stbuf.st_ino;
230 	}
231 	e->e_flags |= EF_HAS_DF;
232 	return df;
233 }
234 
235 /*
236 **  COLLECT -- read & parse message header & make temp file.
237 **
238 **	Creates a temporary file name and copies the standard
239 **	input to that file.  Leading UNIX-style "From" lines are
240 **	stripped off (after important information is extracted).
241 **
242 **	Parameters:
243 **		fp -- file to read.
244 **		smtpmode -- if set, we are running SMTP: give an RFC821
245 **			style message to say we are ready to collect
246 **			input, and never ignore a single dot to mean
247 **			end of message.
248 **		hdrp -- the location to stash the header.
249 **		e -- the current envelope.
250 **		rsetsize -- reset e_msgsize?
251 **
252 **	Returns:
253 **		none.
254 **
255 **	Side Effects:
256 **		If successful,
257 **		- Data file is created and filled, and e->e_dfp is set.
258 **		- The from person may be set.
259 **		If the "enough disk space" check fails,
260 **		- syserr is called.
261 **		- e->e_dfp is NULL.
262 **		- e->e_flags & EF_FATALERRS is set.
263 **		- collect() returns.
264 **		If data file cannot be created, the process is terminated.
265 */
266 
267 /* values for input state machine */
268 #define IS_NORM		0	/* middle of line */
269 #define IS_BOL		1	/* beginning of line */
270 #define IS_DOT		2	/* read a dot at beginning of line */
271 #define IS_DOTCR	3	/* read ".\r" at beginning of line */
272 #define IS_CR		4	/* read a carriage return */
273 
274 /* values for message state machine */
275 #define MS_UFROM	0	/* reading Unix from line */
276 #define MS_HEADER	1	/* reading message header */
277 #define MS_BODY		2	/* reading message body */
278 #define MS_DISCARD	3	/* discarding rest of message */
279 
280 void
281 collect(fp, smtpmode, hdrp, e, rsetsize)
282 	SM_FILE_T *fp;
283 	bool smtpmode;
284 	HDR **hdrp;
285 	register ENVELOPE *e;
286 	bool rsetsize;
287 {
288 	register SM_FILE_T *df;
289 	bool ignrdot;
290 	int dbto;
291 	register char *bp;
292 	int c;
293 	bool inputerr;
294 	bool headeronly;
295 	char *buf;
296 	int buflen;
297 	int istate;
298 	int mstate;
299 	int hdrslen;
300 	int numhdrs;
301 	int afd;
302 	unsigned char *pbp;
303 	unsigned char peekbuf[8];
304 	char bufbuf[MAXLINE];
305 
306 	df = NULL;
307 	ignrdot = smtpmode ? false : IgnrDot;
308 
309 	/* timeout for I/O functions is in milliseconds */
310 	dbto = smtpmode ? ((int) TimeOuts.to_datablock * 1000)
311 			: SM_TIME_FOREVER;
312 	sm_io_setinfo(fp, SM_IO_WHAT_TIMEOUT, &dbto);
313 	c = SM_IO_EOF;
314 	inputerr = false;
315 	headeronly = hdrp != NULL;
316 	hdrslen = 0;
317 	numhdrs = 0;
318 	HasEightBits = false;
319 	buf = bp = bufbuf;
320 	buflen = sizeof bufbuf;
321 	pbp = peekbuf;
322 	istate = IS_BOL;
323 	mstate = SaveFrom ? MS_HEADER : MS_UFROM;
324 
325 	/*
326 	**  Tell ARPANET to go ahead.
327 	*/
328 
329 	if (smtpmode)
330 		message("354 Enter mail, end with \".\" on a line by itself");
331 
332 	/* simulate an I/O timeout when used as sink */
333 	if (tTd(83, 101))
334 		sleep(319);
335 
336 	if (tTd(30, 2))
337 		sm_dprintf("collect\n");
338 
339 	/*
340 	**  Read the message.
341 	**
342 	**	This is done using two interleaved state machines.
343 	**	The input state machine is looking for things like
344 	**	hidden dots; the message state machine is handling
345 	**	the larger picture (e.g., header versus body).
346 	*/
347 
348 	if (rsetsize)
349 		e->e_msgsize = 0;
350 	for (;;)
351 	{
352 		if (tTd(30, 35))
353 			sm_dprintf("top, istate=%d, mstate=%d\n", istate,
354 				   mstate);
355 		for (;;)
356 		{
357 			if (pbp > peekbuf)
358 				c = *--pbp;
359 			else
360 			{
361 				while (!sm_io_eof(fp) && !sm_io_error(fp))
362 				{
363 					errno = 0;
364 					c = sm_io_getc(fp, SM_TIME_DEFAULT);
365 					if (c == SM_IO_EOF && errno == EINTR)
366 					{
367 						/* Interrupted, retry */
368 						sm_io_clearerr(fp);
369 						continue;
370 					}
371 
372 					/* timeout? */
373 					if (c == SM_IO_EOF && errno == EAGAIN
374 					    && smtpmode)
375 					{
376 						/*
377 						**  Override e_message in
378 						**  usrerr() as this is the
379 						**  reason for failure that
380 						**  should be logged for
381 						**  undelivered recipients.
382 						*/
383 
384 						e->e_message = NULL;
385 						errno = 0;
386 						inputerr = true;
387 						goto readabort;
388 					}
389 					break;
390 				}
391 				if (TrafficLogFile != NULL && !headeronly)
392 				{
393 					if (istate == IS_BOL)
394 						(void) sm_io_fprintf(TrafficLogFile,
395 							SM_TIME_DEFAULT,
396 							"%05d <<< ",
397 							(int) CurrentPid);
398 					if (c == SM_IO_EOF)
399 						(void) sm_io_fprintf(TrafficLogFile,
400 							SM_TIME_DEFAULT,
401 							"[EOF]\n");
402 					else
403 						(void) sm_io_putc(TrafficLogFile,
404 							SM_TIME_DEFAULT,
405 							c);
406 				}
407 				if (c == SM_IO_EOF)
408 					goto readerr;
409 				if (SevenBitInput)
410 					c &= 0x7f;
411 				else
412 					HasEightBits |= bitset(0x80, c);
413 			}
414 			if (tTd(30, 94))
415 				sm_dprintf("istate=%d, c=%c (0x%x)\n",
416 					istate, (char) c, c);
417 			switch (istate)
418 			{
419 			  case IS_BOL:
420 				if (c == '.')
421 				{
422 					istate = IS_DOT;
423 					continue;
424 				}
425 				break;
426 
427 			  case IS_DOT:
428 				if (c == '\n' && !ignrdot &&
429 				    !bitset(EF_NL_NOT_EOL, e->e_flags))
430 					goto readerr;
431 				else if (c == '\r' &&
432 					 !bitset(EF_CRLF_NOT_EOL, e->e_flags))
433 				{
434 					istate = IS_DOTCR;
435 					continue;
436 				}
437 				else if (ignrdot ||
438 					 (c != '.' &&
439 					  OpMode != MD_SMTP &&
440 					  OpMode != MD_DAEMON &&
441 					  OpMode != MD_ARPAFTP))
442 
443 				{
444 					SM_ASSERT(pbp < peekbuf +
445 							sizeof(peekbuf));
446 					*pbp++ = c;
447 					c = '.';
448 				}
449 				break;
450 
451 			  case IS_DOTCR:
452 				if (c == '\n' && !ignrdot)
453 					goto readerr;
454 				else
455 				{
456 					/* push back the ".\rx" */
457 					SM_ASSERT(pbp < peekbuf +
458 							sizeof(peekbuf));
459 					*pbp++ = c;
460 					if (OpMode != MD_SMTP &&
461 					    OpMode != MD_DAEMON &&
462 					    OpMode != MD_ARPAFTP)
463 					{
464 						SM_ASSERT(pbp < peekbuf +
465 							 sizeof(peekbuf));
466 						*pbp++ = '\r';
467 						c = '.';
468 					}
469 					else
470 						c = '\r';
471 				}
472 				break;
473 
474 			  case IS_CR:
475 				if (c == '\n')
476 					istate = IS_BOL;
477 				else
478 				{
479 					(void) sm_io_ungetc(fp, SM_TIME_DEFAULT,
480 							    c);
481 					c = '\r';
482 					istate = IS_NORM;
483 				}
484 				goto bufferchar;
485 			}
486 
487 			if (c == '\r' && !bitset(EF_CRLF_NOT_EOL, e->e_flags))
488 			{
489 				istate = IS_CR;
490 				continue;
491 			}
492 			else if (c == '\n' && !bitset(EF_NL_NOT_EOL,
493 						      e->e_flags))
494 				istate = IS_BOL;
495 			else
496 				istate = IS_NORM;
497 
498 bufferchar:
499 			if (!headeronly)
500 			{
501 				/* no overflow? */
502 				if (e->e_msgsize >= 0)
503 				{
504 					e->e_msgsize++;
505 					if (MaxMessageSize > 0 &&
506 					    !bitset(EF_TOOBIG, e->e_flags) &&
507 					    e->e_msgsize > MaxMessageSize)
508 						 e->e_flags |= EF_TOOBIG;
509 				}
510 			}
511 			switch (mstate)
512 			{
513 			  case MS_BODY:
514 				/* just put the character out */
515 				if (!bitset(EF_TOOBIG, e->e_flags))
516 					(void) sm_io_putc(df, SM_TIME_DEFAULT,
517 							  c);
518 
519 				/* FALLTHROUGH */
520 
521 			  case MS_DISCARD:
522 				continue;
523 			}
524 
525 			SM_ASSERT(mstate == MS_UFROM || mstate == MS_HEADER);
526 
527 			/* header -- buffer up */
528 			if (bp >= &buf[buflen - 2])
529 			{
530 				char *obuf;
531 
532 				/* out of space for header */
533 				obuf = buf;
534 				if (buflen < MEMCHUNKSIZE)
535 					buflen *= 2;
536 				else
537 					buflen += MEMCHUNKSIZE;
538 				if (buflen <= 0)
539 				{
540 					sm_syslog(LOG_NOTICE, e->e_id,
541 						  "header overflow from %s during message collect",
542 						  CURHOSTNAME);
543 					errno = 0;
544 					e->e_flags |= EF_CLRQUEUE;
545 					e->e_status = "5.6.0";
546 					usrerrenh(e->e_status,
547 						  "552 Headers too large");
548 					goto discard;
549 				}
550 				buf = xalloc(buflen);
551 				memmove(buf, obuf, bp - obuf);
552 				bp = &buf[bp - obuf];
553 				if (obuf != bufbuf)
554 					sm_free(obuf);  /* XXX */
555 			}
556 
557 			/*
558 			**  XXX Notice: the logic here is broken.
559 			**  An input to sendmail that doesn't contain a
560 			**  header but starts immediately with the body whose
561 			**  first line contain characters which match the
562 			**  following "if" will cause problems: those
563 			**  characters will NOT appear in the output...
564 			**  Do we care?
565 			*/
566 
567 			if (c >= 0200 && c <= 0237)
568 			{
569 #if 0	/* causes complaints -- figure out something for 8.n+1 */
570 				usrerr("Illegal character 0x%x in header", c);
571 #else /* 0 */
572 				/* EMPTY */
573 #endif /* 0 */
574 			}
575 			else if (c != '\0')
576 			{
577 				*bp++ = c;
578 				++hdrslen;
579 				if (!headeronly &&
580 				    MaxHeadersLength > 0 &&
581 				    hdrslen > MaxHeadersLength)
582 				{
583 					sm_syslog(LOG_NOTICE, e->e_id,
584 						  "headers too large (%d max) from %s during message collect",
585 						  MaxHeadersLength,
586 						  CURHOSTNAME);
587 					errno = 0;
588 					e->e_flags |= EF_CLRQUEUE;
589 					e->e_status = "5.6.0";
590 					usrerrenh(e->e_status,
591 						  "552 Headers too large (%d max)",
592 						  MaxHeadersLength);
593   discard:
594 					mstate = MS_DISCARD;
595 				}
596 			}
597 			if (istate == IS_BOL)
598 				break;
599 		}
600 		*bp = '\0';
601 
602 nextstate:
603 		if (tTd(30, 35))
604 			sm_dprintf("nextstate, istate=%d, mstate=%d, line = \"%s\"\n",
605 				istate, mstate, buf);
606 		switch (mstate)
607 		{
608 		  case MS_UFROM:
609 			mstate = MS_HEADER;
610 #ifndef NOTUNIX
611 			if (strncmp(buf, "From ", 5) == 0)
612 			{
613 				bp = buf;
614 				eatfrom(buf, e);
615 				continue;
616 			}
617 #endif /* ! NOTUNIX */
618 			/* FALLTHROUGH */
619 
620 		  case MS_HEADER:
621 			if (!isheader(buf))
622 			{
623 				mstate = MS_BODY;
624 				goto nextstate;
625 			}
626 
627 			/* check for possible continuation line */
628 			do
629 			{
630 				sm_io_clearerr(fp);
631 				errno = 0;
632 				c = sm_io_getc(fp, SM_TIME_DEFAULT);
633 
634 				/* timeout? */
635 				if (c == SM_IO_EOF && errno == EAGAIN
636 				    && smtpmode)
637 				{
638 					/*
639 					**  Override e_message in
640 					**  usrerr() as this is the
641 					**  reason for failure that
642 					**  should be logged for
643 					**  undelivered recipients.
644 					*/
645 
646 					e->e_message = NULL;
647 					errno = 0;
648 					inputerr = true;
649 					goto readabort;
650 				}
651 			} while (c == SM_IO_EOF && errno == EINTR);
652 			if (c != SM_IO_EOF)
653 				(void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
654 			if (c == ' ' || c == '\t')
655 			{
656 				/* yep -- defer this */
657 				continue;
658 			}
659 
660 			SM_ASSERT(bp > buf);
661 
662 			/* guaranteed by isheader(buf) */
663 			SM_ASSERT(*(bp - 1) != '\n' || bp > buf + 1);
664 
665 			/* trim off trailing CRLF or NL */
666 			if (*--bp != '\n' || *--bp != '\r')
667 				bp++;
668 			*bp = '\0';
669 
670 			if (bitset(H_EOH, chompheader(buf,
671 						      CHHDR_CHECK | CHHDR_USER,
672 						      hdrp, e)))
673 			{
674 				mstate = MS_BODY;
675 				goto nextstate;
676 			}
677 			numhdrs++;
678 			break;
679 
680 		  case MS_BODY:
681 			if (tTd(30, 1))
682 				sm_dprintf("EOH\n");
683 
684 			if (headeronly)
685 				goto readerr;
686 
687 			df = collect_eoh(e, numhdrs, hdrslen);
688 			if (df == NULL)
689 				e->e_flags |= EF_TOOBIG;
690 
691 			bp = buf;
692 
693 			/* toss blank line */
694 			if ((!bitset(EF_CRLF_NOT_EOL, e->e_flags) &&
695 				bp[0] == '\r' && bp[1] == '\n') ||
696 			    (!bitset(EF_NL_NOT_EOL, e->e_flags) &&
697 				bp[0] == '\n'))
698 			{
699 				break;
700 			}
701 
702 			/* if not a blank separator, write it out */
703 			if (!bitset(EF_TOOBIG, e->e_flags))
704 			{
705 				while (*bp != '\0')
706 					(void) sm_io_putc(df, SM_TIME_DEFAULT,
707 							  *bp++);
708 			}
709 			break;
710 		}
711 		bp = buf;
712 	}
713 
714 readerr:
715 	if ((sm_io_eof(fp) && smtpmode) || sm_io_error(fp))
716 	{
717 		const char *errmsg;
718 
719 		if (sm_io_eof(fp))
720 			errmsg = "unexpected close";
721 		else
722 			errmsg = sm_errstring(errno);
723 		if (tTd(30, 1))
724 			sm_dprintf("collect: premature EOM: %s\n", errmsg);
725 		if (LogLevel > 1)
726 			sm_syslog(LOG_WARNING, e->e_id,
727 				"collect: premature EOM: %s", errmsg);
728 		inputerr = true;
729 	}
730 
731 	if (headeronly)
732 		return;
733 
734 	if (mstate != MS_BODY)
735 	{
736 		/* no body or discard, so we never opened the data file */
737 		SM_ASSERT(df == NULL);
738 		df = collect_eoh(e, numhdrs, hdrslen);
739 	}
740 
741 	if (df == NULL)
742 	{
743 		/* skip next few clauses */
744 		/* EMPTY */
745 	}
746 	else if (sm_io_flush(df, SM_TIME_DEFAULT) != 0 || sm_io_error(df))
747 	{
748 		dferror(df, "sm_io_flush||sm_io_error", e);
749 		flush_errors(true);
750 		finis(true, true, ExitStat);
751 		/* NOTREACHED */
752 	}
753 	else if (SuperSafe == SAFE_NO ||
754 		 SuperSafe == SAFE_INTERACTIVE ||
755 		 (SuperSafe == SAFE_REALLY_POSTMILTER && smtpmode))
756 	{
757 		/* skip next few clauses */
758 		/* EMPTY */
759 		/* Note: updfs() is not called in this case! */
760 	}
761 	else if (sm_io_setinfo(df, SM_BF_COMMIT, NULL) < 0 && errno != EINVAL)
762 	{
763 		int save_errno = errno;
764 
765 		if (save_errno == EEXIST)
766 		{
767 			char *dfile;
768 			struct stat st;
769 			int dfd;
770 
771 			dfile = queuename(e, DATAFL_LETTER);
772 			if (stat(dfile, &st) < 0)
773 				st.st_size = -1;
774 			errno = EEXIST;
775 			syserr("@collect: bfcommit(%s): already on disk, size=%ld",
776 			       dfile, (long) st.st_size);
777 			dfd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL);
778 			if (dfd >= 0)
779 				dumpfd(dfd, true, true);
780 		}
781 		errno = save_errno;
782 		dferror(df, "bfcommit", e);
783 		flush_errors(true);
784 		finis(save_errno != EEXIST, true, ExitStat);
785 	}
786 	else if ((afd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL)) < 0)
787 	{
788 		dferror(df, "sm_io_getinfo", e);
789 		flush_errors(true);
790 		finis(true, true, ExitStat);
791 		/* NOTREACHED */
792 	}
793 	else if (fsync(afd) < 0)
794 	{
795 		dferror(df, "fsync", e);
796 		flush_errors(true);
797 		finis(true, true, ExitStat);
798 		/* NOTREACHED */
799 	}
800 	else if (sm_io_close(df, SM_TIME_DEFAULT) < 0)
801 	{
802 		dferror(df, "sm_io_close", e);
803 		flush_errors(true);
804 		finis(true, true, ExitStat);
805 		/* NOTREACHED */
806 	}
807 	else
808 	{
809 		/* everything is happily flushed to disk */
810 		df = NULL;
811 
812 		/* remove from available space in filesystem */
813 		updfs(e, 0, 1, "collect");
814 	}
815 
816 	/* An EOF when running SMTP is an error */
817   readabort:
818 	if (inputerr && (OpMode == MD_SMTP || OpMode == MD_DAEMON))
819 	{
820 		char *host;
821 		char *problem;
822 		ADDRESS *q;
823 
824 		host = RealHostName;
825 		if (host == NULL)
826 			host = "localhost";
827 
828 		if (sm_io_eof(fp))
829 			problem = "unexpected close";
830 		else if (sm_io_error(fp))
831 			problem = "I/O error";
832 		else
833 			problem = "read timeout";
834 		if (LogLevel > 0 && sm_io_eof(fp))
835 			sm_syslog(LOG_NOTICE, e->e_id,
836 				"collect: %s on connection from %.100s, sender=%s",
837 				problem, host,
838 				shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
839 		if (sm_io_eof(fp))
840 			usrerr("421 4.4.1 collect: %s on connection from %s, from=%s",
841 				problem, host,
842 				shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
843 		else
844 			syserr("421 4.4.1 collect: %s on connection from %s, from=%s",
845 				problem, host,
846 				shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
847 		flush_errors(true);
848 
849 		/* don't return an error indication */
850 		e->e_to = NULL;
851 		e->e_flags &= ~EF_FATALERRS;
852 		e->e_flags |= EF_CLRQUEUE;
853 
854 		/* Don't send any message notification to sender */
855 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
856 		{
857 			if (QS_IS_DEAD(q->q_state))
858 				continue;
859 			q->q_state = QS_FATALERR;
860 		}
861 
862 		(void) sm_io_close(df, SM_TIME_DEFAULT);
863 		df = NULL;
864 		finis(true, true, ExitStat);
865 		/* NOTREACHED */
866 	}
867 
868 	/* Log collection information. */
869 	if (bitset(EF_LOGSENDER, e->e_flags) && LogLevel > 4)
870 	{
871 		logsender(e, e->e_msgid);
872 		e->e_flags &= ~EF_LOGSENDER;
873 	}
874 
875 	/* check for message too large */
876 	if (bitset(EF_TOOBIG, e->e_flags))
877 	{
878 		e->e_flags |= EF_NO_BODY_RETN|EF_CLRQUEUE;
879 		if (!bitset(EF_FATALERRS, e->e_flags))
880 		{
881 			e->e_status = "5.2.3";
882 			usrerrenh(e->e_status,
883 				"552 Message exceeds maximum fixed size (%ld)",
884 				MaxMessageSize);
885 			if (LogLevel > 6)
886 				sm_syslog(LOG_NOTICE, e->e_id,
887 					"message size (%ld) exceeds maximum (%ld)",
888 					e->e_msgsize, MaxMessageSize);
889 		}
890 	}
891 
892 	/* check for illegal 8-bit data */
893 	if (HasEightBits)
894 	{
895 		e->e_flags |= EF_HAS8BIT;
896 		if (!bitset(MM_PASS8BIT|MM_MIME8BIT, MimeMode) &&
897 		    !bitset(EF_IS_MIME, e->e_flags))
898 		{
899 			e->e_status = "5.6.1";
900 			usrerrenh(e->e_status, "554 Eight bit data not allowed");
901 		}
902 	}
903 	else
904 	{
905 		/* if it claimed to be 8 bits, well, it lied.... */
906 		if (e->e_bodytype != NULL &&
907 		    sm_strcasecmp(e->e_bodytype, "8BITMIME") == 0)
908 			e->e_bodytype = "7BIT";
909 	}
910 
911 	if (SuperSafe == SAFE_REALLY && !bitset(EF_FATALERRS, e->e_flags))
912 	{
913 		char *dfname = queuename(e, DATAFL_LETTER);
914 		if ((e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, dfname,
915 					   SM_IO_RDONLY_B, NULL)) == NULL)
916 		{
917 			/* we haven't acked receipt yet, so just chuck this */
918 			syserr("@Cannot reopen %s", dfname);
919 			finis(true, true, ExitStat);
920 			/* NOTREACHED */
921 		}
922 	}
923 	else
924 		e->e_dfp = df;
925 
926 	/* collect statistics */
927 	if (OpMode != MD_VERIFY)
928 	{
929 		/*
930 		**  Recalculate e_msgpriority, it is done at in eatheader()
931 		**  which is called (in 8.12) after the header is collected,
932 		**  hence e_msgsize is (most likely) incorrect.
933 		*/
934 
935 		e->e_msgpriority = e->e_msgsize
936 				 - e->e_class * WkClassFact
937 				 + e->e_nrcpts * WkRecipFact;
938 		markstats(e, (ADDRESS *) NULL, STATS_NORMAL);
939 	}
940 }
941 
942 /*
943 **  DFERROR -- signal error on writing the data file.
944 **
945 **	Called by collect().  Collect() always terminates the process
946 **	immediately after calling dferror(), which means that the SMTP
947 **	session will be terminated, which means that any error message
948 **	issued by dferror must be a 421 error, as per RFC 821.
949 **
950 **	Parameters:
951 **		df -- the file pointer for the data file.
952 **		msg -- detailed message.
953 **		e -- the current envelope.
954 **
955 **	Returns:
956 **		none.
957 **
958 **	Side Effects:
959 **		Gives an error message.
960 **		Arranges for following output to go elsewhere.
961 */
962 
963 void
964 dferror(df, msg, e)
965 	SM_FILE_T *volatile df;
966 	char *msg;
967 	register ENVELOPE *e;
968 {
969 	char *dfname;
970 
971 	dfname = queuename(e, DATAFL_LETTER);
972 	setstat(EX_IOERR);
973 	if (errno == ENOSPC)
974 	{
975 #if STAT64 > 0
976 		struct stat64 st;
977 #else /* STAT64 > 0 */
978 		struct stat st;
979 #endif /* STAT64 > 0 */
980 		long avail;
981 		long bsize;
982 
983 		e->e_flags |= EF_NO_BODY_RETN;
984 
985 		if (
986 #if STAT64 > 0
987 		    fstat64(sm_io_getinfo(df, SM_IO_WHAT_FD, NULL), &st)
988 #else /* STAT64 > 0 */
989 		    fstat(sm_io_getinfo(df, SM_IO_WHAT_FD, NULL), &st)
990 #endif /* STAT64 > 0 */
991 		    < 0)
992 		  st.st_size = 0;
993 		(void) sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, dfname,
994 				    SM_IO_WRONLY_B, NULL, df);
995 		if (st.st_size <= 0)
996 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
997 				"\n*** Mail could not be accepted");
998 		else
999 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
1000 				"\n*** Mail of at least %llu bytes could not be accepted\n",
1001 				(ULONGLONG_T) st.st_size);
1002 		(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
1003 			"*** at %s due to lack of disk space for temp file.\n",
1004 			MyHostName);
1005 		avail = freediskspace(qid_printqueue(e->e_qgrp, e->e_qdir),
1006 				      &bsize);
1007 		if (avail > 0)
1008 		{
1009 			if (bsize > 1024)
1010 				avail *= bsize / 1024;
1011 			else if (bsize < 1024)
1012 				avail /= 1024 / bsize;
1013 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
1014 				"*** Currently, %ld kilobytes are available for mail temp files.\n",
1015 				avail);
1016 		}
1017 #if 0
1018 		/* Wrong response code; should be 421. */
1019 		e->e_status = "4.3.1";
1020 		usrerrenh(e->e_status, "452 Out of disk space for temp file");
1021 #else /* 0 */
1022 		syserr("421 4.3.1 Out of disk space for temp file");
1023 #endif /* 0 */
1024 	}
1025 	else
1026 		syserr("421 4.3.0 collect: Cannot write %s (%s, uid=%d, gid=%d)",
1027 			dfname, msg, (int) geteuid(), (int) getegid());
1028 	if (sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, SM_PATH_DEVNULL,
1029 			 SM_IO_WRONLY, NULL, df) == NULL)
1030 		sm_syslog(LOG_ERR, e->e_id,
1031 			  "dferror: sm_io_reopen(\"/dev/null\") failed: %s",
1032 			  sm_errstring(errno));
1033 }
1034 /*
1035 **  EATFROM -- chew up a UNIX style from line and process
1036 **
1037 **	This does indeed make some assumptions about the format
1038 **	of UNIX messages.
1039 **
1040 **	Parameters:
1041 **		fm -- the from line.
1042 **		e -- envelope
1043 **
1044 **	Returns:
1045 **		none.
1046 **
1047 **	Side Effects:
1048 **		extracts what information it can from the header,
1049 **		such as the date.
1050 */
1051 
1052 #ifndef NOTUNIX
1053 
1054 static char	*DowList[] =
1055 {
1056 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
1057 };
1058 
1059 static char	*MonthList[] =
1060 {
1061 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
1062 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
1063 	NULL
1064 };
1065 
1066 static void
1067 eatfrom(fm, e)
1068 	char *volatile fm;
1069 	register ENVELOPE *e;
1070 {
1071 	register char *p;
1072 	register char **dt;
1073 
1074 	if (tTd(30, 2))
1075 		sm_dprintf("eatfrom(%s)\n", fm);
1076 
1077 	/* find the date part */
1078 	p = fm;
1079 	while (*p != '\0')
1080 	{
1081 		/* skip a word */
1082 		while (*p != '\0' && *p != ' ')
1083 			p++;
1084 		while (*p == ' ')
1085 			p++;
1086 		if (strlen(p) < 17)
1087 		{
1088 			/* no room for the date */
1089 			return;
1090 		}
1091 		if (!(isascii(*p) && isupper(*p)) ||
1092 		    p[3] != ' ' || p[13] != ':' || p[16] != ':')
1093 			continue;
1094 
1095 		/* we have a possible date */
1096 		for (dt = DowList; *dt != NULL; dt++)
1097 			if (strncmp(*dt, p, 3) == 0)
1098 				break;
1099 		if (*dt == NULL)
1100 			continue;
1101 
1102 		for (dt = MonthList; *dt != NULL; dt++)
1103 		{
1104 			if (strncmp(*dt, &p[4], 3) == 0)
1105 				break;
1106 		}
1107 		if (*dt != NULL)
1108 			break;
1109 	}
1110 
1111 	if (*p != '\0')
1112 	{
1113 		char *q, buf[25];
1114 
1115 		/* we have found a date */
1116 		(void) sm_strlcpy(buf, p, sizeof(buf));
1117 		q = arpadate(buf);
1118 		macdefine(&e->e_macro, A_TEMP, 'a', q);
1119 	}
1120 }
1121 #endif /* ! NOTUNIX */
1122