s/Conatainer/Container/
[dragonfly.git] / contrib / sendmail-8.14 / sendmail / collect.c
blob56fed0a6647663b3601ee893267a3bb935cea96f
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.
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.
14 #include <sendmail.h>
16 SM_RCSID("@(#)$Id: collect.c,v 8.280 2006/11/29 00:20:40 ca Exp $")
18 static void eatfrom __P((char *volatile, ENVELOPE *));
19 static void collect_doheader __P((ENVELOPE *));
20 static SM_FILE_T *collect_dfopen __P((ENVELOPE *));
21 static SM_FILE_T *collect_eoh __P((ENVELOPE *, int, int));
24 ** COLLECT_EOH -- end-of-header processing in collect()
26 ** Called by collect() when it encounters the blank line
27 ** separating the header from the message body, or when it
28 ** encounters EOF in a message that contains only a header.
30 ** Parameters:
31 ** e -- envelope
32 ** numhdrs -- number of headers
33 ** hdrslen -- length of headers
35 ** Results:
36 ** NULL, or handle to open data file
38 ** Side Effects:
39 ** end-of-header check ruleset is invoked.
40 ** envelope state is updated.
41 ** headers may be added and deleted.
42 ** selects the queue.
43 ** opens the data file.
46 static SM_FILE_T *
47 collect_eoh(e, numhdrs, hdrslen)
48 ENVELOPE *e;
49 int numhdrs;
50 int hdrslen;
52 char hnum[16];
53 char hsize[16];
55 /* call the end-of-header check ruleset */
56 (void) sm_snprintf(hnum, sizeof(hnum), "%d", numhdrs);
57 (void) sm_snprintf(hsize, sizeof(hsize), "%d", hdrslen);
58 if (tTd(30, 10))
59 sm_dprintf("collect: rscheck(\"check_eoh\", \"%s $| %s\")\n",
60 hnum, hsize);
61 (void) rscheck("check_eoh", hnum, hsize, e, RSF_UNSTRUCTURED|RSF_COUNT,
62 3, NULL, e->e_id, NULL);
65 ** Process the header,
66 ** select the queue, open the data file.
69 collect_doheader(e);
70 return collect_dfopen(e);
74 ** COLLECT_DOHEADER -- process header in collect()
76 ** Called by collect() after it has finished parsing the header,
77 ** but before it selects the queue and creates the data file.
78 ** The results of processing the header will affect queue selection.
80 ** Parameters:
81 ** e -- envelope
83 ** Results:
84 ** none.
86 ** Side Effects:
87 ** envelope state is updated.
88 ** headers may be added and deleted.
91 static void
92 collect_doheader(e)
93 ENVELOPE *e;
96 ** Find out some information from the headers.
97 ** Examples are who is the from person & the date.
100 eatheader(e, true, false);
102 if (GrabTo && e->e_sendqueue == NULL)
103 usrerr("No recipient addresses found in header");
106 ** If we have a Return-Receipt-To:, turn it into a DSN.
109 if (RrtImpliesDsn && hvalue("return-receipt-to", e->e_header) != NULL)
111 ADDRESS *q;
113 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
114 if (!bitset(QHASNOTIFY, q->q_flags))
115 q->q_flags |= QHASNOTIFY|QPINGONSUCCESS;
119 ** Add an appropriate recipient line if we have none.
122 if (hvalue("to", e->e_header) != NULL ||
123 hvalue("cc", e->e_header) != NULL ||
124 hvalue("apparently-to", e->e_header) != NULL)
126 /* have a valid recipient header -- delete Bcc: headers */
127 e->e_flags |= EF_DELETE_BCC;
129 else if (hvalue("bcc", e->e_header) == NULL)
131 /* no valid recipient headers */
132 register ADDRESS *q;
133 char *hdr = NULL;
135 /* create a recipient field */
136 switch (NoRecipientAction)
138 case NRA_ADD_APPARENTLY_TO:
139 hdr = "Apparently-To";
140 break;
142 case NRA_ADD_TO:
143 hdr = "To";
144 break;
146 case NRA_ADD_BCC:
147 addheader("Bcc", " ", 0, e, true);
148 break;
150 case NRA_ADD_TO_UNDISCLOSED:
151 addheader("To", "undisclosed-recipients:;", 0, e, true);
152 break;
155 if (hdr != NULL)
157 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
159 if (q->q_alias != NULL)
160 continue;
161 if (tTd(30, 3))
162 sm_dprintf("Adding %s: %s\n",
163 hdr, q->q_paddr);
164 addheader(hdr, q->q_paddr, 0, e, true);
171 ** COLLECT_DFOPEN -- open the message data file
173 ** Called by collect() after it has finished processing the header.
174 ** Queue selection occurs at this point, possibly based on the
175 ** envelope's recipient list and on header information.
177 ** Parameters:
178 ** e -- envelope
180 ** Results:
181 ** NULL, or a pointer to an open data file,
182 ** into which the message body will be written by collect().
184 ** Side Effects:
185 ** Calls syserr, sets EF_FATALERRS and returns NULL
186 ** if there is insufficient disk space.
187 ** Aborts process if data file could not be opened.
188 ** Otherwise, the queue is selected,
189 ** e->e_{dfino,dfdev,msgsize,flags} are updated,
190 ** and a pointer to an open data file is returned.
193 static SM_FILE_T *
194 collect_dfopen(e)
195 ENVELOPE *e;
197 MODE_T oldumask = 0;
198 int dfd;
199 struct stat stbuf;
200 SM_FILE_T *df;
201 char *dfname;
203 if (!setnewqueue(e))
204 return NULL;
206 dfname = queuename(e, DATAFL_LETTER);
207 if (bitset(S_IWGRP, QueueFileMode))
208 oldumask = umask(002);
209 df = bfopen(dfname, QueueFileMode, DataFileBufferSize,
210 SFF_OPENASROOT);
211 if (bitset(S_IWGRP, QueueFileMode))
212 (void) umask(oldumask);
213 if (df == NULL)
215 syserr("@Cannot create %s", dfname);
216 e->e_flags |= EF_NO_BODY_RETN;
217 flush_errors(true);
218 finis(false, true, ExitStat);
219 /* NOTREACHED */
221 dfd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL);
222 if (dfd < 0 || fstat(dfd, &stbuf) < 0)
223 e->e_dfino = -1;
224 else
226 e->e_dfdev = stbuf.st_dev;
227 e->e_dfino = stbuf.st_ino;
229 e->e_flags |= EF_HAS_DF;
230 return df;
234 ** COLLECT -- read & parse message header & make temp file.
236 ** Creates a temporary file name and copies the standard
237 ** input to that file. Leading UNIX-style "From" lines are
238 ** stripped off (after important information is extracted).
240 ** Parameters:
241 ** fp -- file to read.
242 ** smtpmode -- if set, we are running SMTP: give an RFC821
243 ** style message to say we are ready to collect
244 ** input, and never ignore a single dot to mean
245 ** end of message.
246 ** hdrp -- the location to stash the header.
247 ** e -- the current envelope.
248 ** rsetsize -- reset e_msgsize?
250 ** Returns:
251 ** none.
253 ** Side Effects:
254 ** If successful,
255 ** - Data file is created and filled, and e->e_dfp is set.
256 ** - The from person may be set.
257 ** If the "enough disk space" check fails,
258 ** - syserr is called.
259 ** - e->e_dfp is NULL.
260 ** - e->e_flags & EF_FATALERRS is set.
261 ** - collect() returns.
262 ** If data file cannot be created, the process is terminated.
265 /* values for input state machine */
266 #define IS_NORM 0 /* middle of line */
267 #define IS_BOL 1 /* beginning of line */
268 #define IS_DOT 2 /* read a dot at beginning of line */
269 #define IS_DOTCR 3 /* read ".\r" at beginning of line */
270 #define IS_CR 4 /* read a carriage return */
272 /* values for message state machine */
273 #define MS_UFROM 0 /* reading Unix from line */
274 #define MS_HEADER 1 /* reading message header */
275 #define MS_BODY 2 /* reading message body */
276 #define MS_DISCARD 3 /* discarding rest of message */
278 void
279 collect(fp, smtpmode, hdrp, e, rsetsize)
280 SM_FILE_T *fp;
281 bool smtpmode;
282 HDR **hdrp;
283 register ENVELOPE *e;
284 bool rsetsize;
286 register SM_FILE_T *df;
287 bool ignrdot;
288 int dbto;
289 register char *bp;
290 int c;
291 bool inputerr;
292 bool headeronly;
293 char *buf;
294 int buflen;
295 int istate;
296 int mstate;
297 int hdrslen;
298 int numhdrs;
299 int afd;
300 unsigned char *pbp;
301 unsigned char peekbuf[8];
302 char bufbuf[MAXLINE];
304 df = NULL;
305 ignrdot = smtpmode ? false : IgnrDot;
307 /* timeout for I/O functions is in milliseconds */
308 dbto = smtpmode ? ((int) TimeOuts.to_datablock * 1000)
309 : SM_TIME_FOREVER;
310 sm_io_setinfo(fp, SM_IO_WHAT_TIMEOUT, &dbto);
311 c = SM_IO_EOF;
312 inputerr = false;
313 headeronly = hdrp != NULL;
314 hdrslen = 0;
315 numhdrs = 0;
316 HasEightBits = false;
317 buf = bp = bufbuf;
318 buflen = sizeof(bufbuf);
319 pbp = peekbuf;
320 istate = IS_BOL;
321 mstate = SaveFrom ? MS_HEADER : MS_UFROM;
324 ** Tell ARPANET to go ahead.
327 if (smtpmode)
328 message("354 Enter mail, end with \".\" on a line by itself");
330 /* simulate an I/O timeout when used as sink */
331 if (tTd(83, 101))
332 sleep(319);
334 if (tTd(30, 2))
335 sm_dprintf("collect\n");
338 ** Read the message.
340 ** This is done using two interleaved state machines.
341 ** The input state machine is looking for things like
342 ** hidden dots; the message state machine is handling
343 ** the larger picture (e.g., header versus body).
346 if (rsetsize)
347 e->e_msgsize = 0;
348 for (;;)
350 if (tTd(30, 35))
351 sm_dprintf("top, istate=%d, mstate=%d\n", istate,
352 mstate);
353 for (;;)
355 if (pbp > peekbuf)
356 c = *--pbp;
357 else
359 while (!sm_io_eof(fp) && !sm_io_error(fp))
361 errno = 0;
362 c = sm_io_getc(fp, SM_TIME_DEFAULT);
363 if (c == SM_IO_EOF && errno == EINTR)
365 /* Interrupted, retry */
366 sm_io_clearerr(fp);
367 continue;
370 /* timeout? */
371 if (c == SM_IO_EOF && errno == EAGAIN
372 && smtpmode)
375 ** Override e_message in
376 ** usrerr() as this is the
377 ** reason for failure that
378 ** should be logged for
379 ** undelivered recipients.
382 e->e_message = NULL;
383 errno = 0;
384 inputerr = true;
385 goto readabort;
387 break;
389 if (TrafficLogFile != NULL && !headeronly)
391 if (istate == IS_BOL)
392 (void) sm_io_fprintf(TrafficLogFile,
393 SM_TIME_DEFAULT,
394 "%05d <<< ",
395 (int) CurrentPid);
396 if (c == SM_IO_EOF)
397 (void) sm_io_fprintf(TrafficLogFile,
398 SM_TIME_DEFAULT,
399 "[EOF]\n");
400 else
401 (void) sm_io_putc(TrafficLogFile,
402 SM_TIME_DEFAULT,
405 if (c == SM_IO_EOF)
406 goto readerr;
407 if (SevenBitInput)
408 c &= 0x7f;
409 else
410 HasEightBits |= bitset(0x80, c);
412 if (tTd(30, 94))
413 sm_dprintf("istate=%d, c=%c (0x%x)\n",
414 istate, (char) c, c);
415 switch (istate)
417 case IS_BOL:
418 if (c == '.')
420 istate = IS_DOT;
421 continue;
423 break;
425 case IS_DOT:
426 if (c == '\n' && !ignrdot &&
427 !bitset(EF_NL_NOT_EOL, e->e_flags))
428 goto readerr;
429 else if (c == '\r' &&
430 !bitset(EF_CRLF_NOT_EOL, e->e_flags))
432 istate = IS_DOTCR;
433 continue;
435 else if (ignrdot ||
436 (c != '.' &&
437 OpMode != MD_SMTP &&
438 OpMode != MD_DAEMON &&
439 OpMode != MD_ARPAFTP))
442 SM_ASSERT(pbp < peekbuf +
443 sizeof(peekbuf));
444 *pbp++ = c;
445 c = '.';
447 break;
449 case IS_DOTCR:
450 if (c == '\n' && !ignrdot)
451 goto readerr;
452 else
454 /* push back the ".\rx" */
455 SM_ASSERT(pbp < peekbuf +
456 sizeof(peekbuf));
457 *pbp++ = c;
458 if (OpMode != MD_SMTP &&
459 OpMode != MD_DAEMON &&
460 OpMode != MD_ARPAFTP)
462 SM_ASSERT(pbp < peekbuf +
463 sizeof(peekbuf));
464 *pbp++ = '\r';
465 c = '.';
467 else
468 c = '\r';
470 break;
472 case IS_CR:
473 if (c == '\n')
474 istate = IS_BOL;
475 else
477 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT,
479 c = '\r';
480 istate = IS_NORM;
482 goto bufferchar;
485 if (c == '\r' && !bitset(EF_CRLF_NOT_EOL, e->e_flags))
487 istate = IS_CR;
488 continue;
490 else if (c == '\n' && !bitset(EF_NL_NOT_EOL,
491 e->e_flags))
492 istate = IS_BOL;
493 else
494 istate = IS_NORM;
496 bufferchar:
497 if (!headeronly)
499 /* no overflow? */
500 if (e->e_msgsize >= 0)
502 e->e_msgsize++;
503 if (MaxMessageSize > 0 &&
504 !bitset(EF_TOOBIG, e->e_flags) &&
505 e->e_msgsize > MaxMessageSize)
506 e->e_flags |= EF_TOOBIG;
509 switch (mstate)
511 case MS_BODY:
512 /* just put the character out */
513 if (!bitset(EF_TOOBIG, e->e_flags))
514 (void) sm_io_putc(df, SM_TIME_DEFAULT,
517 /* FALLTHROUGH */
519 case MS_DISCARD:
520 continue;
523 SM_ASSERT(mstate == MS_UFROM || mstate == MS_HEADER);
525 /* header -- buffer up */
526 if (bp >= &buf[buflen - 2])
528 char *obuf;
530 /* out of space for header */
531 obuf = buf;
532 if (buflen < MEMCHUNKSIZE)
533 buflen *= 2;
534 else
535 buflen += MEMCHUNKSIZE;
536 if (buflen <= 0)
538 sm_syslog(LOG_NOTICE, e->e_id,
539 "header overflow from %s during message collect",
540 CURHOSTNAME);
541 errno = 0;
542 e->e_flags |= EF_CLRQUEUE;
543 e->e_status = "5.6.0";
544 usrerrenh(e->e_status,
545 "552 Headers too large");
546 goto discard;
548 buf = xalloc(buflen);
549 memmove(buf, obuf, bp - obuf);
550 bp = &buf[bp - obuf];
551 if (obuf != bufbuf)
552 sm_free(obuf); /* XXX */
555 if (c != '\0')
557 *bp++ = c;
558 ++hdrslen;
559 if (!headeronly &&
560 MaxHeadersLength > 0 &&
561 hdrslen > MaxHeadersLength)
563 sm_syslog(LOG_NOTICE, e->e_id,
564 "headers too large (%d max) from %s during message collect",
565 MaxHeadersLength,
566 CURHOSTNAME);
567 errno = 0;
568 e->e_flags |= EF_CLRQUEUE;
569 e->e_status = "5.6.0";
570 usrerrenh(e->e_status,
571 "552 Headers too large (%d max)",
572 MaxHeadersLength);
573 discard:
574 mstate = MS_DISCARD;
577 if (istate == IS_BOL)
578 break;
580 *bp = '\0';
582 nextstate:
583 if (tTd(30, 35))
584 sm_dprintf("nextstate, istate=%d, mstate=%d, line=\"%s\"\n",
585 istate, mstate, buf);
586 switch (mstate)
588 case MS_UFROM:
589 mstate = MS_HEADER;
590 #ifndef NOTUNIX
591 if (strncmp(buf, "From ", 5) == 0)
593 bp = buf;
594 eatfrom(buf, e);
595 continue;
597 #endif /* ! NOTUNIX */
598 /* FALLTHROUGH */
600 case MS_HEADER:
601 if (!isheader(buf))
603 mstate = MS_BODY;
604 goto nextstate;
607 /* check for possible continuation line */
610 sm_io_clearerr(fp);
611 errno = 0;
612 c = sm_io_getc(fp, SM_TIME_DEFAULT);
614 /* timeout? */
615 if (c == SM_IO_EOF && errno == EAGAIN
616 && smtpmode)
619 ** Override e_message in
620 ** usrerr() as this is the
621 ** reason for failure that
622 ** should be logged for
623 ** undelivered recipients.
626 e->e_message = NULL;
627 errno = 0;
628 inputerr = true;
629 goto readabort;
631 } while (c == SM_IO_EOF && errno == EINTR);
632 if (c != SM_IO_EOF)
633 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
634 if (c == ' ' || c == '\t')
636 /* yep -- defer this */
637 continue;
640 SM_ASSERT(bp > buf);
642 /* guaranteed by isheader(buf) */
643 SM_ASSERT(*(bp - 1) != '\n' || bp > buf + 1);
645 /* trim off trailing CRLF or NL */
646 if (*--bp != '\n' || *--bp != '\r')
647 bp++;
648 *bp = '\0';
650 if (bitset(H_EOH, chompheader(buf,
651 CHHDR_CHECK | CHHDR_USER,
652 hdrp, e)))
654 mstate = MS_BODY;
655 goto nextstate;
657 numhdrs++;
658 break;
660 case MS_BODY:
661 if (tTd(30, 1))
662 sm_dprintf("EOH\n");
664 if (headeronly)
665 goto readerr;
667 df = collect_eoh(e, numhdrs, hdrslen);
668 if (df == NULL)
669 e->e_flags |= EF_TOOBIG;
671 bp = buf;
673 /* toss blank line */
674 if ((!bitset(EF_CRLF_NOT_EOL, e->e_flags) &&
675 bp[0] == '\r' && bp[1] == '\n') ||
676 (!bitset(EF_NL_NOT_EOL, e->e_flags) &&
677 bp[0] == '\n'))
679 break;
682 /* if not a blank separator, write it out */
683 if (!bitset(EF_TOOBIG, e->e_flags))
685 while (*bp != '\0')
686 (void) sm_io_putc(df, SM_TIME_DEFAULT,
687 *bp++);
689 break;
691 bp = buf;
694 readerr:
695 if ((sm_io_eof(fp) && smtpmode) || sm_io_error(fp))
697 const char *errmsg;
699 if (sm_io_eof(fp))
700 errmsg = "unexpected close";
701 else
702 errmsg = sm_errstring(errno);
703 if (tTd(30, 1))
704 sm_dprintf("collect: premature EOM: %s\n", errmsg);
705 if (LogLevel > 1)
706 sm_syslog(LOG_WARNING, e->e_id,
707 "collect: premature EOM: %s", errmsg);
708 inputerr = true;
711 if (headeronly)
712 return;
714 if (mstate != MS_BODY)
716 /* no body or discard, so we never opened the data file */
717 SM_ASSERT(df == NULL);
718 df = collect_eoh(e, numhdrs, hdrslen);
721 if (df == NULL)
723 /* skip next few clauses */
724 /* EMPTY */
726 else if (sm_io_flush(df, SM_TIME_DEFAULT) != 0 || sm_io_error(df))
728 dferror(df, "sm_io_flush||sm_io_error", e);
729 flush_errors(true);
730 finis(true, true, ExitStat);
731 /* NOTREACHED */
733 else if (SuperSafe == SAFE_NO ||
734 SuperSafe == SAFE_INTERACTIVE ||
735 (SuperSafe == SAFE_REALLY_POSTMILTER && smtpmode))
737 /* skip next few clauses */
738 /* EMPTY */
739 /* Note: updfs() is not called in this case! */
741 else if (sm_io_setinfo(df, SM_BF_COMMIT, NULL) < 0 && errno != EINVAL)
743 int save_errno = errno;
745 if (save_errno == EEXIST)
747 char *dfile;
748 struct stat st;
749 int dfd;
751 dfile = queuename(e, DATAFL_LETTER);
752 if (stat(dfile, &st) < 0)
753 st.st_size = -1;
754 errno = EEXIST;
755 syserr("@collect: bfcommit(%s): already on disk, size=%ld",
756 dfile, (long) st.st_size);
757 dfd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL);
758 if (dfd >= 0)
759 dumpfd(dfd, true, true);
761 errno = save_errno;
762 dferror(df, "bfcommit", e);
763 flush_errors(true);
764 finis(save_errno != EEXIST, true, ExitStat);
766 else if ((afd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL)) < 0)
768 dferror(df, "sm_io_getinfo", e);
769 flush_errors(true);
770 finis(true, true, ExitStat);
771 /* NOTREACHED */
773 else if (fsync(afd) < 0)
775 dferror(df, "fsync", e);
776 flush_errors(true);
777 finis(true, true, ExitStat);
778 /* NOTREACHED */
780 else if (sm_io_close(df, SM_TIME_DEFAULT) < 0)
782 dferror(df, "sm_io_close", e);
783 flush_errors(true);
784 finis(true, true, ExitStat);
785 /* NOTREACHED */
787 else
789 /* everything is happily flushed to disk */
790 df = NULL;
792 /* remove from available space in filesystem */
793 updfs(e, 0, 1, "collect");
796 /* An EOF when running SMTP is an error */
797 readabort:
798 if (inputerr && (OpMode == MD_SMTP || OpMode == MD_DAEMON))
800 char *host;
801 char *problem;
802 ADDRESS *q;
804 host = RealHostName;
805 if (host == NULL)
806 host = "localhost";
808 if (sm_io_eof(fp))
809 problem = "unexpected close";
810 else if (sm_io_error(fp))
811 problem = "I/O error";
812 else
813 problem = "read timeout";
814 if (LogLevel > 0 && sm_io_eof(fp))
815 sm_syslog(LOG_NOTICE, e->e_id,
816 "collect: %s on connection from %.100s, sender=%s",
817 problem, host,
818 shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
819 if (sm_io_eof(fp))
820 usrerr("421 4.4.1 collect: %s on connection from %s, from=%s",
821 problem, host,
822 shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
823 else
824 syserr("421 4.4.1 collect: %s on connection from %s, from=%s",
825 problem, host,
826 shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
827 flush_errors(true);
829 /* don't return an error indication */
830 e->e_to = NULL;
831 e->e_flags &= ~EF_FATALERRS;
832 e->e_flags |= EF_CLRQUEUE;
834 /* Don't send any message notification to sender */
835 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
837 if (QS_IS_DEAD(q->q_state))
838 continue;
839 q->q_state = QS_FATALERR;
842 (void) sm_io_close(df, SM_TIME_DEFAULT);
843 df = NULL;
844 finis(true, true, ExitStat);
845 /* NOTREACHED */
848 /* Log collection information. */
849 if (bitset(EF_LOGSENDER, e->e_flags) && LogLevel > 4)
851 logsender(e, e->e_msgid);
852 e->e_flags &= ~EF_LOGSENDER;
855 /* check for message too large */
856 if (bitset(EF_TOOBIG, e->e_flags))
858 e->e_flags |= EF_NO_BODY_RETN|EF_CLRQUEUE;
859 if (!bitset(EF_FATALERRS, e->e_flags))
861 e->e_status = "5.2.3";
862 usrerrenh(e->e_status,
863 "552 Message exceeds maximum fixed size (%ld)",
864 MaxMessageSize);
865 if (LogLevel > 6)
866 sm_syslog(LOG_NOTICE, e->e_id,
867 "message size (%ld) exceeds maximum (%ld)",
868 e->e_msgsize, MaxMessageSize);
872 /* check for illegal 8-bit data */
873 if (HasEightBits)
875 e->e_flags |= EF_HAS8BIT;
876 if (!bitset(MM_PASS8BIT|MM_MIME8BIT, MimeMode) &&
877 !bitset(EF_IS_MIME, e->e_flags))
879 e->e_status = "5.6.1";
880 usrerrenh(e->e_status, "554 Eight bit data not allowed");
883 else
885 /* if it claimed to be 8 bits, well, it lied.... */
886 if (e->e_bodytype != NULL &&
887 sm_strcasecmp(e->e_bodytype, "8BITMIME") == 0)
888 e->e_bodytype = "7BIT";
891 if (SuperSafe == SAFE_REALLY && !bitset(EF_FATALERRS, e->e_flags))
893 char *dfname = queuename(e, DATAFL_LETTER);
894 if ((e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, dfname,
895 SM_IO_RDONLY_B, NULL)) == NULL)
897 /* we haven't acked receipt yet, so just chuck this */
898 syserr("@Cannot reopen %s", dfname);
899 finis(true, true, ExitStat);
900 /* NOTREACHED */
903 else
904 e->e_dfp = df;
906 /* collect statistics */
907 if (OpMode != MD_VERIFY)
910 ** Recalculate e_msgpriority, it is done at in eatheader()
911 ** which is called (in 8.12) after the header is collected,
912 ** hence e_msgsize is (most likely) incorrect.
915 e->e_msgpriority = e->e_msgsize
916 - e->e_class * WkClassFact
917 + e->e_nrcpts * WkRecipFact;
918 markstats(e, (ADDRESS *) NULL, STATS_NORMAL);
923 ** DFERROR -- signal error on writing the data file.
925 ** Called by collect(). Collect() always terminates the process
926 ** immediately after calling dferror(), which means that the SMTP
927 ** session will be terminated, which means that any error message
928 ** issued by dferror must be a 421 error, as per RFC 821.
930 ** Parameters:
931 ** df -- the file pointer for the data file.
932 ** msg -- detailed message.
933 ** e -- the current envelope.
935 ** Returns:
936 ** none.
938 ** Side Effects:
939 ** Gives an error message.
940 ** Arranges for following output to go elsewhere.
943 void
944 dferror(df, msg, e)
945 SM_FILE_T *volatile df;
946 char *msg;
947 register ENVELOPE *e;
949 char *dfname;
951 dfname = queuename(e, DATAFL_LETTER);
952 setstat(EX_IOERR);
953 if (errno == ENOSPC)
955 #if STAT64 > 0
956 struct stat64 st;
957 #else /* STAT64 > 0 */
958 struct stat st;
959 #endif /* STAT64 > 0 */
960 long avail;
961 long bsize;
963 e->e_flags |= EF_NO_BODY_RETN;
965 if (
966 #if STAT64 > 0
967 fstat64(sm_io_getinfo(df, SM_IO_WHAT_FD, NULL), &st)
968 #else /* STAT64 > 0 */
969 fstat(sm_io_getinfo(df, SM_IO_WHAT_FD, NULL), &st)
970 #endif /* STAT64 > 0 */
971 < 0)
972 st.st_size = 0;
973 (void) sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, dfname,
974 SM_IO_WRONLY_B, NULL, df);
975 if (st.st_size <= 0)
976 (void) sm_io_fprintf(df, SM_TIME_DEFAULT,
977 "\n*** Mail could not be accepted");
978 else
979 (void) sm_io_fprintf(df, SM_TIME_DEFAULT,
980 "\n*** Mail of at least %llu bytes could not be accepted\n",
981 (ULONGLONG_T) st.st_size);
982 (void) sm_io_fprintf(df, SM_TIME_DEFAULT,
983 "*** at %s due to lack of disk space for temp file.\n",
984 MyHostName);
985 avail = freediskspace(qid_printqueue(e->e_qgrp, e->e_qdir),
986 &bsize);
987 if (avail > 0)
989 if (bsize > 1024)
990 avail *= bsize / 1024;
991 else if (bsize < 1024)
992 avail /= 1024 / bsize;
993 (void) sm_io_fprintf(df, SM_TIME_DEFAULT,
994 "*** Currently, %ld kilobytes are available for mail temp files.\n",
995 avail);
997 #if 0
998 /* Wrong response code; should be 421. */
999 e->e_status = "4.3.1";
1000 usrerrenh(e->e_status, "452 Out of disk space for temp file");
1001 #else /* 0 */
1002 syserr("421 4.3.1 Out of disk space for temp file");
1003 #endif /* 0 */
1005 else
1006 syserr("421 4.3.0 collect: Cannot write %s (%s, uid=%d, gid=%d)",
1007 dfname, msg, (int) geteuid(), (int) getegid());
1008 if (sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, SM_PATH_DEVNULL,
1009 SM_IO_WRONLY, NULL, df) == NULL)
1010 sm_syslog(LOG_ERR, e->e_id,
1011 "dferror: sm_io_reopen(\"/dev/null\") failed: %s",
1012 sm_errstring(errno));
1015 ** EATFROM -- chew up a UNIX style from line and process
1017 ** This does indeed make some assumptions about the format
1018 ** of UNIX messages.
1020 ** Parameters:
1021 ** fm -- the from line.
1022 ** e -- envelope
1024 ** Returns:
1025 ** none.
1027 ** Side Effects:
1028 ** extracts what information it can from the header,
1029 ** such as the date.
1032 #ifndef NOTUNIX
1034 static char *DowList[] =
1036 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
1039 static char *MonthList[] =
1041 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1042 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
1043 NULL
1046 static void
1047 eatfrom(fm, e)
1048 char *volatile fm;
1049 register ENVELOPE *e;
1051 register char *p;
1052 register char **dt;
1054 if (tTd(30, 2))
1055 sm_dprintf("eatfrom(%s)\n", fm);
1057 /* find the date part */
1058 p = fm;
1059 while (*p != '\0')
1061 /* skip a word */
1062 while (*p != '\0' && *p != ' ')
1063 p++;
1064 while (*p == ' ')
1065 p++;
1066 if (strlen(p) < 17)
1068 /* no room for the date */
1069 return;
1071 if (!(isascii(*p) && isupper(*p)) ||
1072 p[3] != ' ' || p[13] != ':' || p[16] != ':')
1073 continue;
1075 /* we have a possible date */
1076 for (dt = DowList; *dt != NULL; dt++)
1077 if (strncmp(*dt, p, 3) == 0)
1078 break;
1079 if (*dt == NULL)
1080 continue;
1082 for (dt = MonthList; *dt != NULL; dt++)
1084 if (strncmp(*dt, &p[4], 3) == 0)
1085 break;
1087 if (*dt != NULL)
1088 break;
1091 if (*p != '\0')
1093 char *q, buf[25];
1095 /* we have found a date */
1096 (void) sm_strlcpy(buf, p, sizeof(buf));
1097 q = arpadate(buf);
1098 macdefine(&e->e_macro, A_TEMP, 'a', q);
1101 #endif /* ! NOTUNIX */