xref: /illumos-gate/usr/src/cmd/sendmail/src/collect.c (revision 80ab886d233f514d54c2a6bdeb9fdfd951bd6881)
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.272 2006/03/02 19:09:26 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 		finis(true, true, ExitStat);
863 		/* NOTREACHED */
864 	}
865 
866 	/* Log collection information. */
867 	if (bitset(EF_LOGSENDER, e->e_flags) && LogLevel > 4)
868 	{
869 		logsender(e, e->e_msgid);
870 		e->e_flags &= ~EF_LOGSENDER;
871 	}
872 
873 	/* check for message too large */
874 	if (bitset(EF_TOOBIG, e->e_flags))
875 	{
876 		e->e_flags |= EF_NO_BODY_RETN|EF_CLRQUEUE;
877 		if (!bitset(EF_FATALERRS, e->e_flags))
878 		{
879 			e->e_status = "5.2.3";
880 			usrerrenh(e->e_status,
881 				"552 Message exceeds maximum fixed size (%ld)",
882 				MaxMessageSize);
883 			if (LogLevel > 6)
884 				sm_syslog(LOG_NOTICE, e->e_id,
885 					"message size (%ld) exceeds maximum (%ld)",
886 					e->e_msgsize, MaxMessageSize);
887 		}
888 	}
889 
890 	/* check for illegal 8-bit data */
891 	if (HasEightBits)
892 	{
893 		e->e_flags |= EF_HAS8BIT;
894 		if (!bitset(MM_PASS8BIT|MM_MIME8BIT, MimeMode) &&
895 		    !bitset(EF_IS_MIME, e->e_flags))
896 		{
897 			e->e_status = "5.6.1";
898 			usrerrenh(e->e_status, "554 Eight bit data not allowed");
899 		}
900 	}
901 	else
902 	{
903 		/* if it claimed to be 8 bits, well, it lied.... */
904 		if (e->e_bodytype != NULL &&
905 		    sm_strcasecmp(e->e_bodytype, "8BITMIME") == 0)
906 			e->e_bodytype = "7BIT";
907 	}
908 
909 	if (SuperSafe == SAFE_REALLY && !bitset(EF_FATALERRS, e->e_flags))
910 	{
911 		char *dfname = queuename(e, DATAFL_LETTER);
912 		if ((e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, dfname,
913 					   SM_IO_RDONLY_B, NULL)) == NULL)
914 		{
915 			/* we haven't acked receipt yet, so just chuck this */
916 			syserr("@Cannot reopen %s", dfname);
917 			finis(true, true, ExitStat);
918 			/* NOTREACHED */
919 		}
920 	}
921 	else
922 		e->e_dfp = df;
923 
924 	/* collect statistics */
925 	if (OpMode != MD_VERIFY)
926 	{
927 		/*
928 		**  Recalculate e_msgpriority, it is done at in eatheader()
929 		**  which is called (in 8.12) after the header is collected,
930 		**  hence e_msgsize is (most likely) incorrect.
931 		*/
932 
933 		e->e_msgpriority = e->e_msgsize
934 				 - e->e_class * WkClassFact
935 				 + e->e_nrcpts * WkRecipFact;
936 		markstats(e, (ADDRESS *) NULL, STATS_NORMAL);
937 	}
938 }
939 
940 /*
941 **  DFERROR -- signal error on writing the data file.
942 **
943 **	Called by collect().  Collect() always terminates the process
944 **	immediately after calling dferror(), which means that the SMTP
945 **	session will be terminated, which means that any error message
946 **	issued by dferror must be a 421 error, as per RFC 821.
947 **
948 **	Parameters:
949 **		df -- the file pointer for the data file.
950 **		msg -- detailed message.
951 **		e -- the current envelope.
952 **
953 **	Returns:
954 **		none.
955 **
956 **	Side Effects:
957 **		Gives an error message.
958 **		Arranges for following output to go elsewhere.
959 */
960 
961 void
962 dferror(df, msg, e)
963 	SM_FILE_T *volatile df;
964 	char *msg;
965 	register ENVELOPE *e;
966 {
967 	char *dfname;
968 
969 	dfname = queuename(e, DATAFL_LETTER);
970 	setstat(EX_IOERR);
971 	if (errno == ENOSPC)
972 	{
973 #if STAT64 > 0
974 		struct stat64 st;
975 #else /* STAT64 > 0 */
976 		struct stat st;
977 #endif /* STAT64 > 0 */
978 		long avail;
979 		long bsize;
980 
981 		e->e_flags |= EF_NO_BODY_RETN;
982 
983 		if (
984 #if STAT64 > 0
985 		    fstat64(sm_io_getinfo(df, SM_IO_WHAT_FD, NULL), &st)
986 #else /* STAT64 > 0 */
987 		    fstat(sm_io_getinfo(df, SM_IO_WHAT_FD, NULL), &st)
988 #endif /* STAT64 > 0 */
989 		    < 0)
990 		  st.st_size = 0;
991 		(void) sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, dfname,
992 				    SM_IO_WRONLY_B, NULL, df);
993 		if (st.st_size <= 0)
994 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
995 				"\n*** Mail could not be accepted");
996 		else
997 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
998 				"\n*** Mail of at least %llu bytes could not be accepted\n",
999 				(ULONGLONG_T) st.st_size);
1000 		(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
1001 			"*** at %s due to lack of disk space for temp file.\n",
1002 			MyHostName);
1003 		avail = freediskspace(qid_printqueue(e->e_qgrp, e->e_qdir),
1004 				      &bsize);
1005 		if (avail > 0)
1006 		{
1007 			if (bsize > 1024)
1008 				avail *= bsize / 1024;
1009 			else if (bsize < 1024)
1010 				avail /= 1024 / bsize;
1011 			(void) sm_io_fprintf(df, SM_TIME_DEFAULT,
1012 				"*** Currently, %ld kilobytes are available for mail temp files.\n",
1013 				avail);
1014 		}
1015 #if 0
1016 		/* Wrong response code; should be 421. */
1017 		e->e_status = "4.3.1";
1018 		usrerrenh(e->e_status, "452 Out of disk space for temp file");
1019 #else /* 0 */
1020 		syserr("421 4.3.1 Out of disk space for temp file");
1021 #endif /* 0 */
1022 	}
1023 	else
1024 		syserr("421 4.3.0 collect: Cannot write %s (%s, uid=%d, gid=%d)",
1025 			dfname, msg, (int) geteuid(), (int) getegid());
1026 	if (sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, SM_PATH_DEVNULL,
1027 			 SM_IO_WRONLY, NULL, df) == NULL)
1028 		sm_syslog(LOG_ERR, e->e_id,
1029 			  "dferror: sm_io_reopen(\"/dev/null\") failed: %s",
1030 			  sm_errstring(errno));
1031 }
1032 /*
1033 **  EATFROM -- chew up a UNIX style from line and process
1034 **
1035 **	This does indeed make some assumptions about the format
1036 **	of UNIX messages.
1037 **
1038 **	Parameters:
1039 **		fm -- the from line.
1040 **		e -- envelope
1041 **
1042 **	Returns:
1043 **		none.
1044 **
1045 **	Side Effects:
1046 **		extracts what information it can from the header,
1047 **		such as the date.
1048 */
1049 
1050 #ifndef NOTUNIX
1051 
1052 static char	*DowList[] =
1053 {
1054 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
1055 };
1056 
1057 static char	*MonthList[] =
1058 {
1059 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
1060 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
1061 	NULL
1062 };
1063 
1064 static void
1065 eatfrom(fm, e)
1066 	char *volatile fm;
1067 	register ENVELOPE *e;
1068 {
1069 	register char *p;
1070 	register char **dt;
1071 
1072 	if (tTd(30, 2))
1073 		sm_dprintf("eatfrom(%s)\n", fm);
1074 
1075 	/* find the date part */
1076 	p = fm;
1077 	while (*p != '\0')
1078 	{
1079 		/* skip a word */
1080 		while (*p != '\0' && *p != ' ')
1081 			p++;
1082 		while (*p == ' ')
1083 			p++;
1084 		if (strlen(p) < 17)
1085 		{
1086 			/* no room for the date */
1087 			return;
1088 		}
1089 		if (!(isascii(*p) && isupper(*p)) ||
1090 		    p[3] != ' ' || p[13] != ':' || p[16] != ':')
1091 			continue;
1092 
1093 		/* we have a possible date */
1094 		for (dt = DowList; *dt != NULL; dt++)
1095 			if (strncmp(*dt, p, 3) == 0)
1096 				break;
1097 		if (*dt == NULL)
1098 			continue;
1099 
1100 		for (dt = MonthList; *dt != NULL; dt++)
1101 		{
1102 			if (strncmp(*dt, &p[4], 3) == 0)
1103 				break;
1104 		}
1105 		if (*dt != NULL)
1106 			break;
1107 	}
1108 
1109 	if (*p != '\0')
1110 	{
1111 		char *q, buf[25];
1112 
1113 		/* we have found a date */
1114 		(void) sm_strlcpy(buf, p, sizeof(buf));
1115 		q = arpadate(buf);
1116 		macdefine(&e->e_macro, A_TEMP, 'a', q);
1117 	}
1118 }
1119 #endif /* ! NOTUNIX */
1120