xref: /titanic_44/usr/src/cmd/sendmail/src/collect.c (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
1 /*
2  * Copyright (c) 1998-2006, 2008 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.282 2008/01/31 18:48:29 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 	set_tls_rd_tmo(TimeOuts.to_datablock);
314 	c = SM_IO_EOF;
315 	inputerr = false;
316 	headeronly = hdrp != NULL;
317 	hdrslen = 0;
318 	numhdrs = 0;
319 	HasEightBits = false;
320 	buf = bp = bufbuf;
321 	buflen = sizeof(bufbuf);
322 	pbp = peekbuf;
323 	istate = IS_BOL;
324 	mstate = SaveFrom ? MS_HEADER : MS_UFROM;
325 
326 	/*
327 	**  Tell ARPANET to go ahead.
328 	*/
329 
330 	if (smtpmode)
331 		message("354 Enter mail, end with \".\" on a line by itself");
332 
333 	/* simulate an I/O timeout when used as sink */
334 	if (tTd(83, 101))
335 		sleep(319);
336 
337 	if (tTd(30, 2))
338 		sm_dprintf("collect\n");
339 
340 	/*
341 	**  Read the message.
342 	**
343 	**	This is done using two interleaved state machines.
344 	**	The input state machine is looking for things like
345 	**	hidden dots; the message state machine is handling
346 	**	the larger picture (e.g., header versus body).
347 	*/
348 
349 	if (rsetsize)
350 		e->e_msgsize = 0;
351 	for (;;)
352 	{
353 		if (tTd(30, 35))
354 			sm_dprintf("top, istate=%d, mstate=%d\n", istate,
355 				   mstate);
356 		for (;;)
357 		{
358 			if (pbp > peekbuf)
359 				c = *--pbp;
360 			else
361 			{
362 				while (!sm_io_eof(fp) && !sm_io_error(fp))
363 				{
364 					errno = 0;
365 					c = sm_io_getc(fp, SM_TIME_DEFAULT);
366 					if (c == SM_IO_EOF && errno == EINTR)
367 					{
368 						/* Interrupted, retry */
369 						sm_io_clearerr(fp);
370 						continue;
371 					}
372 
373 					/* timeout? */
374 					if (c == SM_IO_EOF && errno == EAGAIN
375 					    && smtpmode)
376 					{
377 						/*
378 						**  Override e_message in
379 						**  usrerr() as this is the
380 						**  reason for failure that
381 						**  should be logged for
382 						**  undelivered recipients.
383 						*/
384 
385 						e->e_message = NULL;
386 						errno = 0;
387 						inputerr = true;
388 						goto readabort;
389 					}
390 					break;
391 				}
392 				if (TrafficLogFile != NULL && !headeronly)
393 				{
394 					if (istate == IS_BOL)
395 						(void) sm_io_fprintf(TrafficLogFile,
396 							SM_TIME_DEFAULT,
397 							"%05d <<< ",
398 							(int) CurrentPid);
399 					if (c == SM_IO_EOF)
400 						(void) sm_io_fprintf(TrafficLogFile,
401 							SM_TIME_DEFAULT,
402 							"[EOF]\n");
403 					else
404 						(void) sm_io_putc(TrafficLogFile,
405 							SM_TIME_DEFAULT,
406 							c);
407 				}
408 				if (c == SM_IO_EOF)
409 					goto readerr;
410 				if (SevenBitInput)
411 					c &= 0x7f;
412 				else
413 					HasEightBits |= bitset(0x80, c);
414 			}
415 			if (tTd(30, 94))
416 				sm_dprintf("istate=%d, c=%c (0x%x)\n",
417 					istate, (char) c, c);
418 			switch (istate)
419 			{
420 			  case IS_BOL:
421 				if (c == '.')
422 				{
423 					istate = IS_DOT;
424 					continue;
425 				}
426 				break;
427 
428 			  case IS_DOT:
429 				if (c == '\n' && !ignrdot &&
430 				    !bitset(EF_NL_NOT_EOL, e->e_flags))
431 					goto readerr;
432 				else if (c == '\r' &&
433 					 !bitset(EF_CRLF_NOT_EOL, e->e_flags))
434 				{
435 					istate = IS_DOTCR;
436 					continue;
437 				}
438 				else if (ignrdot ||
439 					 (c != '.' &&
440 					  OpMode != MD_SMTP &&
441 					  OpMode != MD_DAEMON &&
442 					  OpMode != MD_ARPAFTP))
443 
444 				{
445 					SM_ASSERT(pbp < peekbuf +
446 							sizeof(peekbuf));
447 					*pbp++ = c;
448 					c = '.';
449 				}
450 				break;
451 
452 			  case IS_DOTCR:
453 				if (c == '\n' && !ignrdot)
454 					goto readerr;
455 				else
456 				{
457 					/* push back the ".\rx" */
458 					SM_ASSERT(pbp < peekbuf +
459 							sizeof(peekbuf));
460 					*pbp++ = c;
461 					if (OpMode != MD_SMTP &&
462 					    OpMode != MD_DAEMON &&
463 					    OpMode != MD_ARPAFTP)
464 					{
465 						SM_ASSERT(pbp < peekbuf +
466 							 sizeof(peekbuf));
467 						*pbp++ = '\r';
468 						c = '.';
469 					}
470 					else
471 						c = '\r';
472 				}
473 				break;
474 
475 			  case IS_CR:
476 				if (c == '\n')
477 					istate = IS_BOL;
478 				else
479 				{
480 					(void) sm_io_ungetc(fp, SM_TIME_DEFAULT,
481 							    c);
482 					c = '\r';
483 					istate = IS_NORM;
484 				}
485 				goto bufferchar;
486 			}
487 
488 			if (c == '\r' && !bitset(EF_CRLF_NOT_EOL, e->e_flags))
489 			{
490 				istate = IS_CR;
491 				continue;
492 			}
493 			else if (c == '\n' && !bitset(EF_NL_NOT_EOL,
494 						      e->e_flags))
495 				istate = IS_BOL;
496 			else
497 				istate = IS_NORM;
498 
499 bufferchar:
500 			if (!headeronly)
501 			{
502 				/* no overflow? */
503 				if (e->e_msgsize >= 0)
504 				{
505 					e->e_msgsize++;
506 					if (MaxMessageSize > 0 &&
507 					    !bitset(EF_TOOBIG, e->e_flags) &&
508 					    e->e_msgsize > MaxMessageSize)
509 						 e->e_flags |= EF_TOOBIG;
510 				}
511 			}
512 			switch (mstate)
513 			{
514 			  case MS_BODY:
515 				/* just put the character out */
516 				if (!bitset(EF_TOOBIG, e->e_flags))
517 					(void) sm_io_putc(df, SM_TIME_DEFAULT,
518 							  c);
519 
520 				/* FALLTHROUGH */
521 
522 			  case MS_DISCARD:
523 				continue;
524 			}
525 
526 			SM_ASSERT(mstate == MS_UFROM || mstate == MS_HEADER);
527 
528 			/* header -- buffer up */
529 			if (bp >= &buf[buflen - 2])
530 			{
531 				char *obuf;
532 
533 				/* out of space for header */
534 				obuf = buf;
535 				if (buflen < MEMCHUNKSIZE)
536 					buflen *= 2;
537 				else
538 					buflen += MEMCHUNKSIZE;
539 				if (buflen <= 0)
540 				{
541 					sm_syslog(LOG_NOTICE, e->e_id,
542 						  "header overflow from %s during message collect",
543 						  CURHOSTNAME);
544 					errno = 0;
545 					e->e_flags |= EF_CLRQUEUE;
546 					e->e_status = "5.6.0";
547 					usrerrenh(e->e_status,
548 						  "552 Headers too large");
549 					goto discard;
550 				}
551 				buf = xalloc(buflen);
552 				memmove(buf, obuf, bp - obuf);
553 				bp = &buf[bp - obuf];
554 				if (obuf != bufbuf)
555 					sm_free(obuf);  /* XXX */
556 			}
557 
558 			if (c != '\0')
559 			{
560 				*bp++ = c;
561 				++hdrslen;
562 				if (!headeronly &&
563 				    MaxHeadersLength > 0 &&
564 				    hdrslen > MaxHeadersLength)
565 				{
566 					sm_syslog(LOG_NOTICE, e->e_id,
567 						  "headers too large (%d max) from %s during message collect",
568 						  MaxHeadersLength,
569 						  CURHOSTNAME);
570 					errno = 0;
571 					e->e_flags |= EF_CLRQUEUE;
572 					e->e_status = "5.6.0";
573 					usrerrenh(e->e_status,
574 						  "552 Headers too large (%d max)",
575 						  MaxHeadersLength);
576   discard:
577 					mstate = MS_DISCARD;
578 				}
579 			}
580 			if (istate == IS_BOL)
581 				break;
582 		}
583 		*bp = '\0';
584 
585 nextstate:
586 		if (tTd(30, 35))
587 			sm_dprintf("nextstate, istate=%d, mstate=%d, line=\"%s\"\n",
588 				istate, mstate, buf);
589 		switch (mstate)
590 		{
591 		  case MS_UFROM:
592 			mstate = MS_HEADER;
593 #ifndef NOTUNIX
594 			if (strncmp(buf, "From ", 5) == 0)
595 			{
596 				bp = buf;
597 				eatfrom(buf, e);
598 				continue;
599 			}
600 #endif /* ! NOTUNIX */
601 			/* FALLTHROUGH */
602 
603 		  case MS_HEADER:
604 			if (!isheader(buf))
605 			{
606 				mstate = MS_BODY;
607 				goto nextstate;
608 			}
609 
610 			/* check for possible continuation line */
611 			do
612 			{
613 				sm_io_clearerr(fp);
614 				errno = 0;
615 				c = sm_io_getc(fp, SM_TIME_DEFAULT);
616 
617 				/* timeout? */
618 				if (c == SM_IO_EOF && errno == EAGAIN
619 				    && smtpmode)
620 				{
621 					/*
622 					**  Override e_message in
623 					**  usrerr() as this is the
624 					**  reason for failure that
625 					**  should be logged for
626 					**  undelivered recipients.
627 					*/
628 
629 					e->e_message = NULL;
630 					errno = 0;
631 					inputerr = true;
632 					goto readabort;
633 				}
634 			} while (c == SM_IO_EOF && errno == EINTR);
635 			if (c != SM_IO_EOF)
636 				(void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
637 			if (c == ' ' || c == '\t')
638 			{
639 				/* yep -- defer this */
640 				continue;
641 			}
642 
643 			SM_ASSERT(bp > buf);
644 
645 			/* guaranteed by isheader(buf) */
646 			SM_ASSERT(*(bp - 1) != '\n' || bp > buf + 1);
647 
648 			/* trim off trailing CRLF or NL */
649 			if (*--bp != '\n' || *--bp != '\r')
650 				bp++;
651 			*bp = '\0';
652 
653 			if (bitset(H_EOH, chompheader(buf,
654 						      CHHDR_CHECK | CHHDR_USER,
655 						      hdrp, e)))
656 			{
657 				mstate = MS_BODY;
658 				goto nextstate;
659 			}
660 			numhdrs++;
661 			break;
662 
663 		  case MS_BODY:
664 			if (tTd(30, 1))
665 				sm_dprintf("EOH\n");
666 
667 			if (headeronly)
668 				goto readerr;
669 
670 			df = collect_eoh(e, numhdrs, hdrslen);
671 			if (df == NULL)
672 				e->e_flags |= EF_TOOBIG;
673 
674 			bp = buf;
675 
676 			/* toss blank line */
677 			if ((!bitset(EF_CRLF_NOT_EOL, e->e_flags) &&
678 				bp[0] == '\r' && bp[1] == '\n') ||
679 			    (!bitset(EF_NL_NOT_EOL, e->e_flags) &&
680 				bp[0] == '\n'))
681 			{
682 				break;
683 			}
684 
685 			/* if not a blank separator, write it out */
686 			if (!bitset(EF_TOOBIG, e->e_flags))
687 			{
688 				while (*bp != '\0')
689 					(void) sm_io_putc(df, SM_TIME_DEFAULT,
690 							  *bp++);
691 			}
692 			break;
693 		}
694 		bp = buf;
695 	}
696 
697 readerr:
698 	if ((sm_io_eof(fp) && smtpmode) || sm_io_error(fp))
699 	{
700 		const char *errmsg;
701 
702 		if (sm_io_eof(fp))
703 			errmsg = "unexpected close";
704 		else
705 			errmsg = sm_errstring(errno);
706 		if (tTd(30, 1))
707 			sm_dprintf("collect: premature EOM: %s\n", errmsg);
708 		if (LogLevel > 1)
709 			sm_syslog(LOG_WARNING, e->e_id,
710 				"collect: premature EOM: %s", errmsg);
711 		inputerr = true;
712 	}
713 
714 	if (headeronly)
715 		return;
716 
717 	if (mstate != MS_BODY)
718 	{
719 		/* no body or discard, so we never opened the data file */
720 		SM_ASSERT(df == NULL);
721 		df = collect_eoh(e, numhdrs, hdrslen);
722 	}
723 
724 	if (df == NULL)
725 	{
726 		/* skip next few clauses */
727 		/* EMPTY */
728 	}
729 	else if (sm_io_flush(df, SM_TIME_DEFAULT) != 0 || sm_io_error(df))
730 	{
731 		dferror(df, "sm_io_flush||sm_io_error", e);
732 		flush_errors(true);
733 		finis(true, true, ExitStat);
734 		/* NOTREACHED */
735 	}
736 	else if (SuperSafe == SAFE_NO ||
737 		 SuperSafe == SAFE_INTERACTIVE ||
738 		 (SuperSafe == SAFE_REALLY_POSTMILTER && smtpmode))
739 	{
740 		/* skip next few clauses */
741 		/* EMPTY */
742 		/* Note: updfs() is not called in this case! */
743 	}
744 	else if (sm_io_setinfo(df, SM_BF_COMMIT, NULL) < 0 && errno != EINVAL)
745 	{
746 		int save_errno = errno;
747 
748 		if (save_errno == EEXIST)
749 		{
750 			char *dfile;
751 			struct stat st;
752 			int dfd;
753 
754 			dfile = queuename(e, DATAFL_LETTER);
755 			if (stat(dfile, &st) < 0)
756 				st.st_size = -1;
757 			errno = EEXIST;
758 			syserr("@collect: bfcommit(%s): already on disk, size=%ld",
759 			       dfile, (long) st.st_size);
760 			dfd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL);
761 			if (dfd >= 0)
762 				dumpfd(dfd, true, true);
763 		}
764 		errno = save_errno;
765 		dferror(df, "bfcommit", e);
766 		flush_errors(true);
767 		finis(save_errno != EEXIST, true, ExitStat);
768 	}
769 	else if ((afd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL)) < 0)
770 	{
771 		dferror(df, "sm_io_getinfo", e);
772 		flush_errors(true);
773 		finis(true, true, ExitStat);
774 		/* NOTREACHED */
775 	}
776 	else if (fsync(afd) < 0)
777 	{
778 		dferror(df, "fsync", e);
779 		flush_errors(true);
780 		finis(true, true, ExitStat);
781 		/* NOTREACHED */
782 	}
783 	else if (sm_io_close(df, SM_TIME_DEFAULT) < 0)
784 	{
785 		dferror(df, "sm_io_close", e);
786 		flush_errors(true);
787 		finis(true, true, ExitStat);
788 		/* NOTREACHED */
789 	}
790 	else
791 	{
792 		/* everything is happily flushed to disk */
793 		df = NULL;
794 
795 		/* remove from available space in filesystem */
796 		updfs(e, 0, 1, "collect");
797 	}
798 
799 	/* An EOF when running SMTP is an error */
800   readabort:
801 	if (inputerr && (OpMode == MD_SMTP || OpMode == MD_DAEMON))
802 	{
803 		char *host;
804 		char *problem;
805 		ADDRESS *q;
806 
807 		host = RealHostName;
808 		if (host == NULL)
809 			host = "localhost";
810 
811 		if (sm_io_eof(fp))
812 			problem = "unexpected close";
813 		else if (sm_io_error(fp))
814 			problem = "I/O error";
815 		else
816 			problem = "read timeout";
817 		if (LogLevel > 0 && sm_io_eof(fp))
818 			sm_syslog(LOG_NOTICE, e->e_id,
819 				"collect: %s on connection from %.100s, sender=%s",
820 				problem, host,
821 				shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
822 		if (sm_io_eof(fp))
823 			usrerr("421 4.4.1 collect: %s on connection from %s, from=%s",
824 				problem, host,
825 				shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
826 		else
827 			syserr("421 4.4.1 collect: %s on connection from %s, from=%s",
828 				problem, host,
829 				shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
830 		flush_errors(true);
831 
832 		/* don't return an error indication */
833 		e->e_to = NULL;
834 		e->e_flags &= ~EF_FATALERRS;
835 		e->e_flags |= EF_CLRQUEUE;
836 
837 		/* Don't send any message notification to sender */
838 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
839 		{
840 			if (QS_IS_DEAD(q->q_state))
841 				continue;
842 			q->q_state = QS_FATALERR;
843 		}
844 
845 		(void) sm_io_close(df, SM_TIME_DEFAULT);
846 		df = NULL;
847 		finis(true, true, ExitStat);
848 		/* NOTREACHED */
849 	}
850 
851 	/* Log collection information. */
852 	if (bitset(EF_LOGSENDER, e->e_flags) && LogLevel > 4)
853 	{
854 		logsender(e, e->e_msgid);
855 		e->e_flags &= ~EF_LOGSENDER;
856 	}
857 
858 	/* check for message too large */
859 	if (bitset(EF_TOOBIG, e->e_flags))
860 	{
861 		e->e_flags |= EF_NO_BODY_RETN|EF_CLRQUEUE;
862 		if (!bitset(EF_FATALERRS, e->e_flags))
863 		{
864 			e->e_status = "5.2.3";
865 			usrerrenh(e->e_status,
866 				"552 Message exceeds maximum fixed size (%ld)",
867 				MaxMessageSize);
868 			if (LogLevel > 6)
869 				sm_syslog(LOG_NOTICE, e->e_id,
870 					"message size (%ld) exceeds maximum (%ld)",
871 					e->e_msgsize, MaxMessageSize);
872 		}
873 	}
874 
875 	/* check for illegal 8-bit data */
876 	if (HasEightBits)
877 	{
878 		e->e_flags |= EF_HAS8BIT;
879 		if (!bitset(MM_PASS8BIT|MM_MIME8BIT, MimeMode) &&
880 		    !bitset(EF_IS_MIME, e->e_flags))
881 		{
882 			e->e_status = "5.6.1";
883 			usrerrenh(e->e_status, "554 Eight bit data not allowed");
884 		}
885 	}
886 	else
887 	{
888 		/* if it claimed to be 8 bits, well, it lied.... */
889 		if (e->e_bodytype != NULL &&
890 		    sm_strcasecmp(e->e_bodytype, "8BITMIME") == 0)
891 			e->e_bodytype = "7BIT";
892 	}
893 
894 	if (SuperSafe == SAFE_REALLY && !bitset(EF_FATALERRS, e->e_flags))
895 	{
896 		char *dfname = queuename(e, DATAFL_LETTER);
897 		if ((e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, dfname,
898 					   SM_IO_RDONLY_B, NULL)) == NULL)
899 		{
900 			/* we haven't acked receipt yet, so just chuck this */
901 			syserr("@Cannot reopen %s", dfname);
902 			finis(true, true, ExitStat);
903 			/* NOTREACHED */
904 		}
905 	}
906 	else
907 		e->e_dfp = df;
908 
909 	/* collect statistics */
910 	if (OpMode != MD_VERIFY)
911 	{
912 		/*
913 		**  Recalculate e_msgpriority, it is done at in eatheader()
914 		**  which is called (in 8.12) after the header is collected,
915 		**  hence e_msgsize is (most likely) incorrect.
916 		*/
917 
918 		e->e_msgpriority = e->e_msgsize
919 				 - e->e_class * WkClassFact
920 				 + e->e_nrcpts * WkRecipFact;
921 		markstats(e, (ADDRESS *) NULL, STATS_NORMAL);
922 	}
923 }
924 
925 /*
926 **  DFERROR -- signal error on writing the data file.
927 **
928 **	Called by collect().  Collect() always terminates the process
929 **	immediately after calling dferror(), which means that the SMTP
930 **	session will be terminated, which means that any error message
931 **	issued by dferror must be a 421 error, as per RFC 821.
932 **
933 **	Parameters:
934 **		df -- the file pointer for the data file.
935 **		msg -- detailed message.
936 **		e -- the current envelope.
937 **
938 **	Returns:
939 **		none.
940 **
941 **	Side Effects:
942 **		Gives an error message.
943 **		Arranges for following output to go elsewhere.
944 */
945 
946 void
947 dferror(df, msg, e)
948 	SM_FILE_T *volatile df;
949 	char *msg;
950 	register ENVELOPE *e;
951 {
952 	char *dfname;
953 
954 	dfname = queuename(e, DATAFL_LETTER);
955 	setstat(EX_IOERR);
956 	if (errno == ENOSPC)
957 	{
958 #if STAT64 > 0
959 		struct stat64 st;
960 #else /* STAT64 > 0 */
961 		struct stat st;
962 #endif /* STAT64 > 0 */
963 		long avail;
964 		long bsize;
965 
966 		e->e_flags |= EF_NO_BODY_RETN;
967 
968 		if (
969 #if STAT64 > 0
970 		    fstat64(sm_io_getinfo(df, SM_IO_WHAT_FD, NULL), &st)
971 #else /* STAT64 > 0 */
972 		    fstat(sm_io_getinfo(df, SM_IO_WHAT_FD, NULL), &st)
973 #endif /* STAT64 > 0 */
974 		    < 0)
975 		  st.st_size = 0;
976 		(void) sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, dfname,
977 				    SM_IO_WRONLY_B, NULL, df);
978 		if (st.st_size <= 0)
979 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
980 				"\n*** Mail could not be accepted");
981 		else
982 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
983 				"\n*** Mail of at least %llu bytes could not be accepted\n",
984 				(ULONGLONG_T) st.st_size);
985 		(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
986 			"*** at %s due to lack of disk space for temp file.\n",
987 			MyHostName);
988 		avail = freediskspace(qid_printqueue(e->e_qgrp, e->e_qdir),
989 				      &bsize);
990 		if (avail > 0)
991 		{
992 			if (bsize > 1024)
993 				avail *= bsize / 1024;
994 			else if (bsize < 1024)
995 				avail /= 1024 / bsize;
996 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
997 				"*** Currently, %ld kilobytes are available for mail temp files.\n",
998 				avail);
999 		}
1000 #if 0
1001 		/* Wrong response code; should be 421. */
1002 		e->e_status = "4.3.1";
1003 		usrerrenh(e->e_status, "452 Out of disk space for temp file");
1004 #else /* 0 */
1005 		syserr("421 4.3.1 Out of disk space for temp file");
1006 #endif /* 0 */
1007 	}
1008 	else
1009 		syserr("421 4.3.0 collect: Cannot write %s (%s, uid=%d, gid=%d)",
1010 			dfname, msg, (int) geteuid(), (int) getegid());
1011 	if (sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, SM_PATH_DEVNULL,
1012 			 SM_IO_WRONLY, NULL, df) == NULL)
1013 		sm_syslog(LOG_ERR, e->e_id,
1014 			  "dferror: sm_io_reopen(\"/dev/null\") failed: %s",
1015 			  sm_errstring(errno));
1016 }
1017 /*
1018 **  EATFROM -- chew up a UNIX style from line and process
1019 **
1020 **	This does indeed make some assumptions about the format
1021 **	of UNIX messages.
1022 **
1023 **	Parameters:
1024 **		fm -- the from line.
1025 **		e -- envelope
1026 **
1027 **	Returns:
1028 **		none.
1029 **
1030 **	Side Effects:
1031 **		extracts what information it can from the header,
1032 **		such as the date.
1033 */
1034 
1035 #ifndef NOTUNIX
1036 
1037 static char	*DowList[] =
1038 {
1039 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
1040 };
1041 
1042 static char	*MonthList[] =
1043 {
1044 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
1045 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
1046 	NULL
1047 };
1048 
1049 static void
1050 eatfrom(fm, e)
1051 	char *volatile fm;
1052 	register ENVELOPE *e;
1053 {
1054 	register char *p;
1055 	register char **dt;
1056 
1057 	if (tTd(30, 2))
1058 		sm_dprintf("eatfrom(%s)\n", fm);
1059 
1060 	/* find the date part */
1061 	p = fm;
1062 	while (*p != '\0')
1063 	{
1064 		/* skip a word */
1065 		while (*p != '\0' && *p != ' ')
1066 			p++;
1067 		while (*p == ' ')
1068 			p++;
1069 		if (strlen(p) < 17)
1070 		{
1071 			/* no room for the date */
1072 			return;
1073 		}
1074 		if (!(isascii(*p) && isupper(*p)) ||
1075 		    p[3] != ' ' || p[13] != ':' || p[16] != ':')
1076 			continue;
1077 
1078 		/* we have a possible date */
1079 		for (dt = DowList; *dt != NULL; dt++)
1080 			if (strncmp(*dt, p, 3) == 0)
1081 				break;
1082 		if (*dt == NULL)
1083 			continue;
1084 
1085 		for (dt = MonthList; *dt != NULL; dt++)
1086 		{
1087 			if (strncmp(*dt, &p[4], 3) == 0)
1088 				break;
1089 		}
1090 		if (*dt != NULL)
1091 			break;
1092 	}
1093 
1094 	if (*p != '\0')
1095 	{
1096 		char *q, buf[25];
1097 
1098 		/* we have found a date */
1099 		(void) sm_strlcpy(buf, p, sizeof(buf));
1100 		q = arpadate(buf);
1101 		macdefine(&e->e_macro, A_TEMP, 'a', q);
1102 	}
1103 }
1104 #endif /* ! NOTUNIX */
1105