2 * Copyright (c) 1998-2006, 2008 Proofpoint, Inc. and its suppliers.
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.
16 SM_RCSID("@(#)$Id: collect.c,v 8.287 2013-11-22 20:51:55 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.
32 ** numhdrs -- number of headers
33 ** hdrslen -- length of headers
36 ** NULL, or handle to open data file
39 ** end-of-header check ruleset is invoked.
40 ** envelope state is updated.
41 ** headers may be added and deleted.
43 ** opens the data file.
47 collect_eoh(e, numhdrs, hdrslen)
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);
59 sm_dprintf("collect: rscheck(\"check_eoh\", \"%s $| %s\")\n",
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.
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.
87 ** envelope state is updated.
88 ** headers may be added and deleted.
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)
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 */
135 /* create a recipient field */
136 switch (NoRecipientAction)
138 case NRA_ADD_APPARENTLY_TO:
139 hdr = "Apparently-To";
147 addheader("Bcc", " ", 0, e, true);
150 case NRA_ADD_TO_UNDISCLOSED:
151 addheader("To", "undisclosed-recipients:;", 0, e, true);
157 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
159 if (q->q_alias != NULL)
162 sm_dprintf("Adding %s: %s\n",
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.
181 ** NULL, or a pointer to an open data file,
182 ** into which the message body will be written by collect().
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.
206 dfname = queuename(e, DATAFL_LETTER);
207 if (bitset(S_IWGRP, QueueFileMode))
208 oldumask = umask(002);
209 df = bfopen(dfname, QueueFileMode, DataFileBufferSize,
211 if (bitset(S_IWGRP, QueueFileMode))
212 (void) umask(oldumask);
215 syserr("@Cannot create %s", dfname);
216 e->e_flags |= EF_NO_BODY_RETN;
218 finis(false, true, ExitStat);
221 dfd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL);
222 if (dfd < 0 || fstat(dfd, &stbuf) < 0)
226 e->e_dfdev = stbuf.st_dev;
227 e->e_dfino = stbuf.st_ino;
229 e->e_flags |= EF_HAS_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).
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
246 ** hdrp -- the location to stash the header.
247 ** e -- the current envelope.
248 ** rsetsize -- reset e_msgsize?
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 */
279 collect(fp, smtpmode, hdrp, e, rsetsize)
283 register ENVELOPE *e;
286 register SM_FILE_T *df;
301 unsigned char peekbuf[8];
302 char bufbuf[MAXLINE];
303 #if _FFR_REJECT_NUL_BYTE
304 bool hasNUL; /* has at least one NUL input byte */
305 #endif /* _FFR_REJECT_NUL_BYTE */
308 ignrdot = smtpmode ? false : IgnrDot;
310 /* timeout for I/O functions is in milliseconds */
311 dbto = smtpmode ? ((int) TimeOuts.to_datablock * 1000)
313 sm_io_setinfo(fp, SM_IO_WHAT_TIMEOUT, &dbto);
314 set_tls_rd_tmo(TimeOuts.to_datablock);
317 headeronly = hdrp != NULL;
320 HasEightBits = false;
321 #if _FFR_REJECT_NUL_BYTE
323 #endif /* _FFR_REJECT_NUL_BYTE */
325 buflen = sizeof(bufbuf);
328 mstate = SaveFrom ? MS_HEADER : MS_UFROM;
331 ** Tell ARPANET to go ahead.
335 message("354 Enter mail, end with \".\" on a line by itself");
337 /* simulate an I/O timeout when used as sink */
342 sm_dprintf("collect\n");
347 ** This is done using two interleaved state machines.
348 ** The input state machine is looking for things like
349 ** hidden dots; the message state machine is handling
350 ** the larger picture (e.g., header versus body).
358 sm_dprintf("top, istate=%d, mstate=%d\n", istate,
366 while (!sm_io_eof(fp) && !sm_io_error(fp))
369 c = sm_io_getc(fp, SM_TIME_DEFAULT);
370 if (c == SM_IO_EOF && errno == EINTR)
372 /* Interrupted, retry */
378 if (c == SM_IO_EOF && errno == EAGAIN
382 ** Override e_message in
383 ** usrerr() as this is the
384 ** reason for failure that
385 ** should be logged for
386 ** undelivered recipients.
396 if (TrafficLogFile != NULL && !headeronly)
398 if (istate == IS_BOL)
399 (void) sm_io_fprintf(TrafficLogFile,
404 (void) sm_io_fprintf(TrafficLogFile,
408 (void) sm_io_putc(TrafficLogFile,
412 #if _FFR_REJECT_NUL_BYTE
415 #endif /* _FFR_REJECT_NUL_BYTE */
421 HasEightBits |= bitset(0x80, c);
424 sm_dprintf("istate=%d, c=%c (0x%x)\n",
425 istate, (char) c, c);
437 if (c == '\n' && !ignrdot &&
438 !bitset(EF_NL_NOT_EOL, e->e_flags))
440 else if (c == '\r' &&
441 !bitset(EF_CRLF_NOT_EOL, e->e_flags))
449 OpMode != MD_DAEMON &&
450 OpMode != MD_ARPAFTP))
453 SM_ASSERT(pbp < peekbuf +
461 if (c == '\n' && !ignrdot)
465 /* push back the ".\rx" */
466 SM_ASSERT(pbp < peekbuf +
469 if (OpMode != MD_SMTP &&
470 OpMode != MD_DAEMON &&
471 OpMode != MD_ARPAFTP)
473 SM_ASSERT(pbp < peekbuf +
488 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT,
496 if (c == '\r' && !bitset(EF_CRLF_NOT_EOL, e->e_flags))
501 else if (c == '\n' && !bitset(EF_NL_NOT_EOL,
511 if (e->e_msgsize >= 0)
514 if (MaxMessageSize > 0 &&
515 !bitset(EF_TOOBIG, e->e_flags) &&
516 e->e_msgsize > MaxMessageSize)
517 e->e_flags |= EF_TOOBIG;
523 /* just put the character out */
524 if (!bitset(EF_TOOBIG, e->e_flags))
525 (void) sm_io_putc(df, SM_TIME_DEFAULT,
534 SM_ASSERT(mstate == MS_UFROM || mstate == MS_HEADER);
536 /* header -- buffer up */
537 if (bp >= &buf[buflen - 2])
541 /* out of space for header */
543 if (buflen < MEMCHUNKSIZE)
546 buflen += MEMCHUNKSIZE;
549 sm_syslog(LOG_NOTICE, e->e_id,
550 "header overflow from %s during message collect",
553 e->e_flags |= EF_CLRQUEUE;
554 e->e_status = "5.6.0";
555 usrerrenh(e->e_status,
556 "552 Headers too large");
559 buf = xalloc(buflen);
560 memmove(buf, obuf, bp - obuf);
561 bp = &buf[bp - obuf];
563 sm_free(obuf); /* XXX */
571 MaxHeadersLength > 0 &&
572 hdrslen > MaxHeadersLength)
574 sm_syslog(LOG_NOTICE, e->e_id,
575 "headers too large (%d max) from %s during message collect",
579 e->e_flags |= EF_CLRQUEUE;
580 e->e_status = "5.6.0";
581 usrerrenh(e->e_status,
582 "552 Headers too large (%d max)",
588 if (istate == IS_BOL)
595 sm_dprintf("nextstate, istate=%d, mstate=%d, line=\"%s\"\n",
596 istate, mstate, buf);
602 if (strncmp(buf, "From ", 5) == 0)
608 #endif /* ! NOTUNIX */
618 /* check for possible continuation line */
623 c = sm_io_getc(fp, SM_TIME_DEFAULT);
626 if (c == SM_IO_EOF && errno == EAGAIN
630 ** Override e_message in
631 ** usrerr() as this is the
632 ** reason for failure that
633 ** should be logged for
634 ** undelivered recipients.
642 } while (c == SM_IO_EOF && errno == EINTR);
644 (void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
645 if (c == ' ' || c == '\t')
647 /* yep -- defer this */
653 /* guaranteed by isheader(buf) */
654 SM_ASSERT(*(bp - 1) != '\n' || bp > buf + 1);
656 /* trim off trailing CRLF or NL */
657 if (*--bp != '\n' || *--bp != '\r')
661 if (bitset(H_EOH, chompheader(buf,
662 CHHDR_CHECK | CHHDR_USER,
678 df = collect_eoh(e, numhdrs, hdrslen);
680 e->e_flags |= EF_TOOBIG;
684 /* toss blank line */
685 if ((!bitset(EF_CRLF_NOT_EOL, e->e_flags) &&
686 bp[0] == '\r' && bp[1] == '\n') ||
687 (!bitset(EF_NL_NOT_EOL, e->e_flags) &&
693 /* if not a blank separator, write it out */
694 if (!bitset(EF_TOOBIG, e->e_flags))
697 (void) sm_io_putc(df, SM_TIME_DEFAULT,
706 if ((sm_io_eof(fp) && smtpmode) || sm_io_error(fp))
711 errmsg = "unexpected close";
713 errmsg = sm_errstring(errno);
715 sm_dprintf("collect: premature EOM: %s\n", errmsg);
717 sm_syslog(LOG_WARNING, e->e_id,
718 "collect: premature EOM: %s", errmsg);
725 if (mstate != MS_BODY)
727 /* no body or discard, so we never opened the data file */
728 SM_ASSERT(df == NULL);
729 df = collect_eoh(e, numhdrs, hdrslen);
734 /* skip next few clauses */
737 else if (sm_io_flush(df, SM_TIME_DEFAULT) != 0 || sm_io_error(df))
739 dferror(df, "sm_io_flush||sm_io_error", e);
741 finis(true, true, ExitStat);
744 else if (SuperSafe == SAFE_NO ||
745 SuperSafe == SAFE_INTERACTIVE ||
746 (SuperSafe == SAFE_REALLY_POSTMILTER && smtpmode))
748 /* skip next few clauses */
750 /* Note: updfs() is not called in this case! */
752 else if (sm_io_setinfo(df, SM_BF_COMMIT, NULL) < 0 && errno != EINVAL)
754 int save_errno = errno;
756 if (save_errno == EEXIST)
762 dfile = queuename(e, DATAFL_LETTER);
763 if (stat(dfile, &st) < 0)
766 syserr("@collect: bfcommit(%s): already on disk, size=%ld",
767 dfile, (long) st.st_size);
768 dfd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL);
770 dumpfd(dfd, true, true);
773 dferror(df, "bfcommit", e);
775 finis(save_errno != EEXIST, true, ExitStat);
777 else if ((afd = sm_io_getinfo(df, SM_IO_WHAT_FD, NULL)) < 0)
779 dferror(df, "sm_io_getinfo", e);
781 finis(true, true, ExitStat);
784 else if (fsync(afd) < 0)
786 dferror(df, "fsync", e);
788 finis(true, true, ExitStat);
791 else if (sm_io_close(df, SM_TIME_DEFAULT) < 0)
793 dferror(df, "sm_io_close", e);
795 finis(true, true, ExitStat);
800 /* everything is happily flushed to disk */
803 /* remove from available space in filesystem */
804 updfs(e, 0, 1, "collect");
807 /* An EOF when running SMTP is an error */
809 if (inputerr && (OpMode == MD_SMTP || OpMode == MD_DAEMON))
820 problem = "unexpected close";
821 else if (sm_io_error(fp))
822 problem = "I/O error";
824 problem = "read timeout";
825 if (LogLevel > 0 && sm_io_eof(fp))
826 sm_syslog(LOG_NOTICE, e->e_id,
827 "collect: %s on connection from %.100s, sender=%s",
829 shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
831 usrerr("421 4.4.1 collect: %s on connection from %s, from=%s",
833 shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
835 syserr("421 4.4.1 collect: %s on connection from %s, from=%s",
837 shortenstring(e->e_from.q_paddr, MAXSHORTSTR));
840 /* don't return an error indication */
842 e->e_flags &= ~EF_FATALERRS;
843 e->e_flags |= EF_CLRQUEUE;
845 /* Don't send any message notification to sender */
846 for (q = e->e_sendqueue; q != NULL; q = q->q_next)
848 if (QS_IS_DEAD(q->q_state))
850 q->q_state = QS_FATALERR;
853 (void) sm_io_close(df, SM_TIME_DEFAULT);
855 finis(true, true, ExitStat);
859 /* Log collection information. */
861 sm_dprintf("collect: e_id=%s, EF_LOGSENDER=%d, LogLevel=%d\n",
862 e->e_id, bitset(EF_LOGSENDER, e->e_flags), LogLevel);
863 if (bitset(EF_LOGSENDER, e->e_flags) && LogLevel > 4)
865 logsender(e, e->e_msgid);
866 e->e_flags &= ~EF_LOGSENDER;
869 /* check for message too large */
870 if (bitset(EF_TOOBIG, e->e_flags))
872 e->e_flags |= EF_NO_BODY_RETN|EF_CLRQUEUE;
873 if (!bitset(EF_FATALERRS, e->e_flags))
875 e->e_status = "5.2.3";
876 usrerrenh(e->e_status,
877 "552 Message exceeds maximum fixed size (%ld)",
880 sm_syslog(LOG_NOTICE, e->e_id,
881 "message size (%ld) exceeds maximum (%ld)",
882 PRT_NONNEGL(e->e_msgsize),
887 /* check for illegal 8-bit data */
890 e->e_flags |= EF_HAS8BIT;
891 if (!bitset(MM_PASS8BIT|MM_MIME8BIT, MimeMode) &&
892 !bitset(EF_IS_MIME, e->e_flags))
894 e->e_status = "5.6.1";
895 usrerrenh(e->e_status, "554 Eight bit data not allowed");
900 /* if it claimed to be 8 bits, well, it lied.... */
901 if (e->e_bodytype != NULL &&
902 sm_strcasecmp(e->e_bodytype, "8BITMIME") == 0)
903 e->e_bodytype = "7BIT";
906 #if _FFR_REJECT_NUL_BYTE
907 if (hasNUL && RejectNUL)
909 e->e_status = "5.6.1";
910 usrerrenh(e->e_status, "554 NUL byte not allowed");
912 #endif /* _FFR_REJECT_NUL_BYTE */
914 if (SuperSafe == SAFE_REALLY && !bitset(EF_FATALERRS, e->e_flags))
916 char *dfname = queuename(e, DATAFL_LETTER);
917 if ((e->e_dfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, dfname,
918 SM_IO_RDONLY_B, NULL)) == NULL)
920 /* we haven't acked receipt yet, so just chuck this */
921 syserr("@Cannot reopen %s", dfname);
922 finis(true, true, ExitStat);
929 /* collect statistics */
930 if (OpMode != MD_VERIFY)
933 ** Recalculate e_msgpriority, it is done at in eatheader()
934 ** which is called (in 8.12) after the header is collected,
935 ** hence e_msgsize is (most likely) incorrect.
938 e->e_msgpriority = e->e_msgsize
939 - e->e_class * WkClassFact
940 + e->e_nrcpts * WkRecipFact;
941 markstats(e, (ADDRESS *) NULL, STATS_NORMAL);
946 ** DFERROR -- signal error on writing the data file.
948 ** Called by collect(). Collect() always terminates the process
949 ** immediately after calling dferror(), which means that the SMTP
950 ** session will be terminated, which means that any error message
951 ** issued by dferror must be a 421 error, as per RFC 821.
954 ** df -- the file pointer for the data file.
955 ** msg -- detailed message.
956 ** e -- the current envelope.
962 ** Gives an error message.
963 ** Arranges for following output to go elsewhere.
968 SM_FILE_T *volatile df;
970 register ENVELOPE *e;
974 dfname = queuename(e, DATAFL_LETTER);
980 #else /* STAT64 > 0 */
982 #endif /* STAT64 > 0 */
986 e->e_flags |= EF_NO_BODY_RETN;
990 fstat64(sm_io_getinfo(df, SM_IO_WHAT_FD, NULL), &st)
991 #else /* STAT64 > 0 */
992 fstat(sm_io_getinfo(df, SM_IO_WHAT_FD, NULL), &st)
993 #endif /* STAT64 > 0 */
996 (void) sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, dfname,
997 SM_IO_WRONLY_B, NULL, df);
999 (void) sm_io_fprintf(df, SM_TIME_DEFAULT,
1000 "\n*** Mail could not be accepted");
1002 (void) sm_io_fprintf(df, SM_TIME_DEFAULT,
1003 "\n*** Mail of at least %llu bytes could not be accepted\n",
1004 (ULONGLONG_T) st.st_size);
1005 (void) sm_io_fprintf(df, SM_TIME_DEFAULT,
1006 "*** at %s due to lack of disk space for temp file.\n",
1008 avail = freediskspace(qid_printqueue(e->e_qgrp, e->e_qdir),
1013 avail *= bsize / 1024;
1014 else if (bsize < 1024)
1015 avail /= 1024 / bsize;
1016 (void) sm_io_fprintf(df, SM_TIME_DEFAULT,
1017 "*** Currently, %ld kilobytes are available for mail temp files.\n",
1021 /* Wrong response code; should be 421. */
1022 e->e_status = "4.3.1";
1023 usrerrenh(e->e_status, "452 Out of disk space for temp file");
1025 syserr("421 4.3.1 Out of disk space for temp file");
1029 syserr("421 4.3.0 collect: Cannot write %s (%s, uid=%d, gid=%d)",
1030 dfname, msg, (int) geteuid(), (int) getegid());
1031 if (sm_io_reopen(SmFtStdio, SM_TIME_DEFAULT, SM_PATH_DEVNULL,
1032 SM_IO_WRONLY, NULL, df) == NULL)
1033 sm_syslog(LOG_ERR, e->e_id,
1034 "dferror: sm_io_reopen(\"/dev/null\") failed: %s",
1035 sm_errstring(errno));
1038 ** EATFROM -- chew up a UNIX style from line and process
1040 ** This does indeed make some assumptions about the format
1041 ** of UNIX messages.
1044 ** fm -- the from line.
1051 ** extracts what information it can from the header,
1052 ** such as the date.
1057 static char *DowList[] =
1059 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
1062 static char *MonthList[] =
1064 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1065 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
1072 register ENVELOPE *e;
1078 sm_dprintf("eatfrom(%s)\n", fm);
1080 /* find the date part */
1085 while (*p != '\0' && *p != ' ')
1091 /* no room for the date */
1094 if (!(isascii(*p) && isupper(*p)) ||
1095 p[3] != ' ' || p[13] != ':' || p[16] != ':')
1098 /* we have a possible date */
1099 for (dt = DowList; *dt != NULL; dt++)
1100 if (strncmp(*dt, p, 3) == 0)
1105 for (dt = MonthList; *dt != NULL; dt++)
1107 if (strncmp(*dt, &p[4], 3) == 0)
1118 /* we have found a date */
1119 (void) sm_strlcpy(buf, p, sizeof(buf));
1121 macdefine(&e->e_macro, A_TEMP, 'a', q);
1124 #endif /* ! NOTUNIX */