xref: /illumos-gate/usr/src/cmd/sendmail/src/collect.c (revision 058561cb)
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.280 2006/11/29 00:20:40 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, NULL);
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, true);
150 			break;
151 
152 		  case NRA_ADD_TO_UNDISCLOSED:
153 			addheader("To", "undisclosed-recipients:;", 0, e, true);
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, true);
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 			if (c != '\0')
558 			{
559 				*bp++ = c;
560 				++hdrslen;
561 				if (!headeronly &&
562 				    MaxHeadersLength > 0 &&
563 				    hdrslen > MaxHeadersLength)
564 				{
565 					sm_syslog(LOG_NOTICE, e->e_id,
566 						  "headers too large (%d max) from %s during message collect",
567 						  MaxHeadersLength,
568 						  CURHOSTNAME);
569 					errno = 0;
570 					e->e_flags |= EF_CLRQUEUE;
571 					e->e_status = "5.6.0";
572 					usrerrenh(e->e_status,
573 						  "552 Headers too large (%d max)",
574 						  MaxHeadersLength);
575   discard:
576 					mstate = MS_DISCARD;
577 				}
578 			}
579 			if (istate == IS_BOL)
580 				break;
581 		}
582 		*bp = '\0';
583 
584 nextstate:
585 		if (tTd(30, 35))
586 			sm_dprintf("nextstate, istate=%d, mstate=%d, line=\"%s\"\n",
587 				istate, mstate, buf);
588 		switch (mstate)
589 		{
590 		  case MS_UFROM:
591 			mstate = MS_HEADER;
592 #ifndef NOTUNIX
593 			if (strncmp(buf, "From ", 5) == 0)
594 			{
595 				bp = buf;
596 				eatfrom(buf, e);
597 				continue;
598 			}
599 #endif /* ! NOTUNIX */
600 			/* FALLTHROUGH */
601 
602 		  case MS_HEADER:
603 			if (!isheader(buf))
604 			{
605 				mstate = MS_BODY;
606 				goto nextstate;
607 			}
608 
609 			/* check for possible continuation line */
610 			do
611 			{
612 				sm_io_clearerr(fp);
613 				errno = 0;
614 				c = sm_io_getc(fp, SM_TIME_DEFAULT);
615 
616 				/* timeout? */
617 				if (c == SM_IO_EOF && errno == EAGAIN
618 				    && smtpmode)
619 				{
620 					/*
621 					**  Override e_message in
622 					**  usrerr() as this is the
623 					**  reason for failure that
624 					**  should be logged for
625 					**  undelivered recipients.
626 					*/
627 
628 					e->e_message = NULL;
629 					errno = 0;
630 					inputerr = true;
631 					goto readabort;
632 				}
633 			} while (c == SM_IO_EOF && errno == EINTR);
634 			if (c != SM_IO_EOF)
635 				(void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
636 			if (c == ' ' || c == '\t')
637 			{
638 				/* yep -- defer this */
639 				continue;
640 			}
641 
642 			SM_ASSERT(bp > buf);
643 
644 			/* guaranteed by isheader(buf) */
645 			SM_ASSERT(*(bp - 1) != '\n' || bp > buf + 1);
646 
647 			/* trim off trailing CRLF or NL */
648 			if (*--bp != '\n' || *--bp != '\r')
649 				bp++;
650 			*bp = '\0';
651 
652 			if (bitset(H_EOH, chompheader(buf,
653 						      CHHDR_CHECK | CHHDR_USER,
654 						      hdrp, e)))
655 			{
656 				mstate = MS_BODY;
657 				goto nextstate;
658 			}
659 			numhdrs++;
660 			break;
661 
662 		  case MS_BODY:
663 			if (tTd(30, 1))
664 				sm_dprintf("EOH\n");
665 
666 			if (headeronly)
667 				goto readerr;
668 
669 			df = collect_eoh(e, numhdrs, hdrslen);
670 			if (df == NULL)
671 				e->e_flags |= EF_TOOBIG;
672 
673 			bp = buf;
674 
675 			/* toss blank line */
676 			if ((!bitset(EF_CRLF_NOT_EOL, e->e_flags) &&
677 				bp[0] == '\r' && bp[1] == '\n') ||
678 			    (!bitset(EF_NL_NOT_EOL, e->e_flags) &&
679 				bp[0] == '\n'))
680 			{
681 				break;
682 			}
683 
684 			/* if not a blank separator, write it out */
685 			if (!bitset(EF_TOOBIG, e->e_flags))
686 			{
687 				while (*bp != '\0')
688 					(void) sm_io_putc(df, SM_TIME_DEFAULT,
689 							  *bp++);
690 			}
691 			break;
692 		}
693 		bp = buf;
694 	}
695 
696 readerr:
697 	if ((sm_io_eof(fp) && smtpmode) || sm_io_error(fp))
698 	{
699 		const char *errmsg;
700 
701 		if (sm_io_eof(fp))
702 			errmsg = "unexpected close";
703 		else
704 			errmsg = sm_errstring(errno);
705 		if (tTd(30, 1))
706 			sm_dprintf("collect: premature EOM: %s\n", errmsg);
707 		if (LogLevel > 1)
708 			sm_syslog(LOG_WARNING, e->e_id,
709 				"collect: premature EOM: %s", errmsg);
710 		inputerr = true;
711 	}
712 
713 	if (headeronly)
714 		return;
715 
716 	if (mstate != MS_BODY)
717 	{
718 		/* no body or discard, so we never opened the data file */
719 		SM_ASSERT(df == NULL);
720 		df = collect_eoh(e, numhdrs, hdrslen);
721 	}
722 
723 	if (df == NULL)
724 	{
725 		/* skip next few clauses */
726 		/* EMPTY */
727 	}
728 	else if (sm_io_flush(df, SM_TIME_DEFAULT) != 0 || sm_io_error(df))
729 	{
730 		dferror(df, "sm_io_flush||sm_io_error", e);
731 		flush_errors(true);
732 		finis(true, true, ExitStat);
733 		/* NOTREACHED */
734 	}
735 	else if (SuperSafe == SAFE_NO ||
736 		 SuperSafe == SAFE_INTERACTIVE ||
737 		 (SuperSafe == SAFE_REALLY_POSTMILTER && smtpmode))
738 	{
739 		/* skip next few clauses */
740 		/* EMPTY */
741 		/* Note: updfs() is not called in this case! */
742 	}
743 	else if (sm_io_setinfo(df, SM_BF_COMMIT, NULL) < 0 && errno != EINVAL)
744 	{
745 		int save_errno = errno;
746 
747 		if (save_errno == EEXIST)
748 		{
749 			char *dfile;
750 			struct stat st;
751 			int dfd;
752 
753 			dfile = queuename(e, DATAFL_LETTER);
754 			if (stat(dfile, &st) < 0)
755 				st.st_size = -1;
756 			errno = EEXIST;
757 			syserr("@collect: bfcommit(%s): already on disk, size=%ld",
758 			       dfile, (long) st.st_size);
759 			dfd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL);
760 			if (dfd >= 0)
761 				dumpfd(dfd, true, true);
762 		}
763 		errno = save_errno;
764 		dferror(df, "bfcommit", e);
765 		flush_errors(true);
766 		finis(save_errno != EEXIST, true, ExitStat);
767 	}
768 	else if ((afd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL)) < 0)
769 	{
770 		dferror(df, "sm_io_getinfo", e);
771 		flush_errors(true);
772 		finis(true, true, ExitStat);
773 		/* NOTREACHED */
774 	}
775 	else if (fsync(afd) < 0)
776 	{
777 		dferror(df, "fsync", e);
778 		flush_errors(true);
779 		finis(true, true, ExitStat);
780 		/* NOTREACHED */
781 	}
782 	else if (sm_io_close(df, SM_TIME_DEFAULT) < 0)
783 	{
784 		dferror(df, "sm_io_close", e);
785 		flush_errors(true);
786 		finis(true, true, ExitStat);
787 		/* NOTREACHED */
788 	}
789 	else
790 	{
791 		/* everything is happily flushed to disk */
792 		df = NULL;
793 
794 		/* remove from available space in filesystem */
795 		updfs(e, 0, 1, "collect");
796 	}
797 
798 	/* An EOF when running SMTP is an error */
799   readabort:
800 	if (inputerr && (OpMode == MD_SMTP || OpMode == MD_DAEMON))
801 	{
802 		char *host;
803 		char *problem;
804 		ADDRESS *q;
805 
806 		host = RealHostName;
807 		if (host == NULL)
808 			host = "localhost";
809 
810 		if (sm_io_eof(fp))
811 			problem = "unexpected close";
812 		else if (sm_io_error(fp))
813 			problem = "I/O error";
814 		else
815 			problem = "read timeout";
816 		if (LogLevel > 0 && sm_io_eof(fp))
817 			sm_syslog(LOG_NOTICE, e->e_id,
818 				"collect: %s on connection from %.100s, sender=%s",
819 				problem, host,
820 				shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
821 		if (sm_io_eof(fp))
822 			usrerr("421 4.4.1 collect: %s on connection from %s, from=%s",
823 				problem, host,
824 				shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
825 		else
826 			syserr("421 4.4.1 collect: %s on connection from %s, from=%s",
827 				problem, host,
828 				shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
829 		flush_errors(true);
830 
831 		/* don't return an error indication */
832 		e->e_to = NULL;
833 		e->e_flags &= ~EF_FATALERRS;
834 		e->e_flags |= EF_CLRQUEUE;
835 
836 		/* Don't send any message notification to sender */
837 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
838 		{
839 			if (QS_IS_DEAD(q->q_state))
840 				continue;
841 			q->q_state = QS_FATALERR;
842 		}
843 
844 		(void) sm_io_close(df, SM_TIME_DEFAULT);
845 		df = NULL;
846 		finis(true, true, ExitStat);
847 		/* NOTREACHED */
848 	}
849 
850 	/* Log collection information. */
851 	if (bitset(EF_LOGSENDER, e->e_flags) && LogLevel > 4)
852 	{
853 		logsender(e, e->e_msgid);
854 		e->e_flags &= ~EF_LOGSENDER;
855 	}
856 
857 	/* check for message too large */
858 	if (bitset(EF_TOOBIG, e->e_flags))
859 	{
860 		e->e_flags |= EF_NO_BODY_RETN|EF_CLRQUEUE;
861 		if (!bitset(EF_FATALERRS, e->e_flags))
862 		{
863 			e->e_status = "5.2.3";
864 			usrerrenh(e->e_status,
865 				"552 Message exceeds maximum fixed size (%ld)",
866 				MaxMessageSize);
867 			if (LogLevel > 6)
868 				sm_syslog(LOG_NOTICE, e->e_id,
869 					"message size (%ld) exceeds maximum (%ld)",
870 					e->e_msgsize, MaxMessageSize);
871 		}
872 	}
873 
874 	/* check for illegal 8-bit data */
875 	if (HasEightBits)
876 	{
877 		e->e_flags |= EF_HAS8BIT;
878 		if (!bitset(MM_PASS8BIT|MM_MIME8BIT, MimeMode) &&
879 		    !bitset(EF_IS_MIME, e->e_flags))
880 		{
881 			e->e_status = "5.6.1";
882 			usrerrenh(e->e_status, "554 Eight bit data not allowed");
883 		}
884 	}
885 	else
886 	{
887 		/* if it claimed to be 8 bits, well, it lied.... */
888 		if (e->e_bodytype != NULL &&
889 		    sm_strcasecmp(e->e_bodytype, "8BITMIME") == 0)
890 			e->e_bodytype = "7BIT";
891 	}
892 
893 	if (SuperSafe == SAFE_REALLY && !bitset(EF_FATALERRS, e->e_flags))
894 	{
895 		char *dfname = queuename(e, DATAFL_LETTER);
896 		if ((e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, dfname,
897 					   SM_IO_RDONLY_B, NULL)) == NULL)
898 		{
899 			/* we haven't acked receipt yet, so just chuck this */
900 			syserr("@Cannot reopen %s", dfname);
901 			finis(true, true, ExitStat);
902 			/* NOTREACHED */
903 		}
904 	}
905 	else
906 		e->e_dfp = df;
907 
908 	/* collect statistics */
909 	if (OpMode != MD_VERIFY)
910 	{
911 		/*
912 		**  Recalculate e_msgpriority, it is done at in eatheader()
913 		**  which is called (in 8.12) after the header is collected,
914 		**  hence e_msgsize is (most likely) incorrect.
915 		*/
916 
917 		e->e_msgpriority = e->e_msgsize
918 				 - e->e_class * WkClassFact
919 				 + e->e_nrcpts * WkRecipFact;
920 		markstats(e, (ADDRESS *) NULL, STATS_NORMAL);
921 	}
922 }
923 
924 /*
925 **  DFERROR -- signal error on writing the data file.
926 **
927 **	Called by collect().  Collect() always terminates the process
928 **	immediately after calling dferror(), which means that the SMTP
929 **	session will be terminated, which means that any error message
930 **	issued by dferror must be a 421 error, as per RFC 821.
931 **
932 **	Parameters:
933 **		df -- the file pointer for the data file.
934 **		msg -- detailed message.
935 **		e -- the current envelope.
936 **
937 **	Returns:
938 **		none.
939 **
940 **	Side Effects:
941 **		Gives an error message.
942 **		Arranges for following output to go elsewhere.
943 */
944 
945 void
946 dferror(df, msg, e)
947 	SM_FILE_T *volatile df;
948 	char *msg;
949 	register ENVELOPE *e;
950 {
951 	char *dfname;
952 
953 	dfname = queuename(e, DATAFL_LETTER);
954 	setstat(EX_IOERR);
955 	if (errno == ENOSPC)
956 	{
957 #if STAT64 > 0
958 		struct stat64 st;
959 #else /* STAT64 > 0 */
960 		struct stat st;
961 #endif /* STAT64 > 0 */
962 		long avail;
963 		long bsize;
964 
965 		e->e_flags |= EF_NO_BODY_RETN;
966 
967 		if (
968 #if STAT64 > 0
969 		    fstat64(sm_io_getinfo(df, SM_IO_WHAT_FD, NULL), &st)
970 #else /* STAT64 > 0 */
971 		    fstat(sm_io_getinfo(df, SM_IO_WHAT_FD, NULL), &st)
972 #endif /* STAT64 > 0 */
973 		    < 0)
974 		  st.st_size = 0;
975 		(void) sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, dfname,
976 				    SM_IO_WRONLY_B, NULL, df);
977 		if (st.st_size <= 0)
978 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
979 				"\n*** Mail could not be accepted");
980 		else
981 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
982 				"\n*** Mail of at least %llu bytes could not be accepted\n",
983 				(ULONGLONG_T) st.st_size);
984 		(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
985 			"*** at %s due to lack of disk space for temp file.\n",
986 			MyHostName);
987 		avail = freediskspace(qid_printqueue(e->e_qgrp, e->e_qdir),
988 				      &bsize);
989 		if (avail > 0)
990 		{
991 			if (bsize > 1024)
992 				avail *= bsize / 1024;
993 			else if (bsize < 1024)
994 				avail /= 1024 / bsize;
995 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
996 				"*** Currently, %ld kilobytes are available for mail temp files.\n",
997 				avail);
998 		}
999 #if 0
1000 		/* Wrong response code; should be 421. */
1001 		e->e_status = "4.3.1";
1002 		usrerrenh(e->e_status, "452 Out of disk space for temp file");
1003 #else /* 0 */
1004 		syserr("421 4.3.1 Out of disk space for temp file");
1005 #endif /* 0 */
1006 	}
1007 	else
1008 		syserr("421 4.3.0 collect: Cannot write %s (%s, uid=%d, gid=%d)",
1009 			dfname, msg, (int) geteuid(), (int) getegid());
1010 	if (sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, SM_PATH_DEVNULL,
1011 			 SM_IO_WRONLY, NULL, df) == NULL)
1012 		sm_syslog(LOG_ERR, e->e_id,
1013 			  "dferror: sm_io_reopen(\"/dev/null\") failed: %s",
1014 			  sm_errstring(errno));
1015 }
1016 /*
1017 **  EATFROM -- chew up a UNIX style from line and process
1018 **
1019 **	This does indeed make some assumptions about the format
1020 **	of UNIX messages.
1021 **
1022 **	Parameters:
1023 **		fm -- the from line.
1024 **		e -- envelope
1025 **
1026 **	Returns:
1027 **		none.
1028 **
1029 **	Side Effects:
1030 **		extracts what information it can from the header,
1031 **		such as the date.
1032 */
1033 
1034 #ifndef NOTUNIX
1035 
1036 static char	*DowList[] =
1037 {
1038 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
1039 };
1040 
1041 static char	*MonthList[] =
1042 {
1043 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
1044 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
1045 	NULL
1046 };
1047 
1048 static void
1049 eatfrom(fm, e)
1050 	char *volatile fm;
1051 	register ENVELOPE *e;
1052 {
1053 	register char *p;
1054 	register char **dt;
1055 
1056 	if (tTd(30, 2))
1057 		sm_dprintf("eatfrom(%s)\n", fm);
1058 
1059 	/* find the date part */
1060 	p = fm;
1061 	while (*p != '\0')
1062 	{
1063 		/* skip a word */
1064 		while (*p != '\0' && *p != ' ')
1065 			p++;
1066 		while (*p == ' ')
1067 			p++;
1068 		if (strlen(p) < 17)
1069 		{
1070 			/* no room for the date */
1071 			return;
1072 		}
1073 		if (!(isascii(*p) && isupper(*p)) ||
1074 		    p[3] != ' ' || p[13] != ':' || p[16] != ':')
1075 			continue;
1076 
1077 		/* we have a possible date */
1078 		for (dt = DowList; *dt != NULL; dt++)
1079 			if (strncmp(*dt, p, 3) == 0)
1080 				break;
1081 		if (*dt == NULL)
1082 			continue;
1083 
1084 		for (dt = MonthList; *dt != NULL; dt++)
1085 		{
1086 			if (strncmp(*dt, &p[4], 3) == 0)
1087 				break;
1088 		}
1089 		if (*dt != NULL)
1090 			break;
1091 	}
1092 
1093 	if (*p != '\0')
1094 	{
1095 		char *q, buf[25];
1096 
1097 		/* we have found a date */
1098 		(void) sm_strlcpy(buf, p, sizeof(buf));
1099 		q = arpadate(buf);
1100 		macdefine(&e->e_macro, A_TEMP, 'a', q);
1101 	}
1102 }
1103 #endif /* ! NOTUNIX */
1104