1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #ifndef lint 10 static char sccsid[] = "@(#)collect.c 8.23 (Berkeley) 08/16/94"; 11 #endif /* not lint */ 12 13 # include <errno.h> 14 # include "sendmail.h" 15 16 /* 17 ** COLLECT -- read & parse message header & make temp file. 18 ** 19 ** Creates a temporary file name and copies the standard 20 ** input to that file. Leading UNIX-style "From" lines are 21 ** stripped off (after important information is extracted). 22 ** 23 ** Parameters: 24 ** fp -- file to read. 25 ** smtpmode -- if set, we are running SMTP: give an RFC821 26 ** style message to say we are ready to collect 27 ** input, and never ignore a single dot to mean 28 ** end of message. 29 ** requeueflag -- this message will be requeued later, so 30 ** don't do final processing on it. 31 ** hdrp -- the location to stash the header. 32 ** e -- the current envelope. 33 ** 34 ** Returns: 35 ** none. 36 ** 37 ** Side Effects: 38 ** Temp file is created and filled. 39 ** The from person may be set. 40 */ 41 42 char *CollectErrorMessage; 43 bool CollectErrno; 44 45 static jmp_buf CtxCollectTimeout; 46 static int collecttimeout(); 47 static bool CollectProgress; 48 static EVENT *CollectTimeout; 49 50 /* values for input state machine */ 51 #define IS_NORM 0 /* middle of line */ 52 #define IS_BOL 1 /* beginning of line */ 53 #define IS_DOT 2 /* read a dot at beginning of line */ 54 #define IS_DOTCR 3 /* read ".\r" at beginning of line */ 55 #define IS_CR 4 /* read a carriage return */ 56 57 /* values for message state machine */ 58 #define MS_UFROM 0 /* reading Unix from line */ 59 #define MS_HEADER 1 /* reading message header */ 60 #define MS_BODY 2 /* reading message body */ 61 62 63 collect(fp, smtpmode, requeueflag, hdrp, e) 64 FILE *fp; 65 bool smtpmode; 66 bool requeueflag; 67 HDR **hdrp; 68 register ENVELOPE *e; 69 { 70 register FILE *tf; 71 bool ignrdot = smtpmode ? FALSE : IgnrDot; 72 time_t dbto = smtpmode ? TimeOuts.to_datablock : 0; 73 register char *bp; 74 register int c; 75 bool inputerr = FALSE; 76 bool headeronly = FALSE; 77 char *buf; 78 int buflen; 79 int istate; 80 int mstate; 81 char *pbp; 82 char peekbuf[8]; 83 char bufbuf[MAXLINE]; 84 extern bool isheader(); 85 86 CollectErrorMessage = NULL; 87 CollectErrno = 0; 88 if (hdrp == NULL) 89 hdrp = &e->e_header; 90 else 91 headeronly = TRUE; 92 93 /* 94 ** Create the temp file name and create the file. 95 */ 96 97 if (!headeronly) 98 { 99 struct stat stbuf; 100 101 e->e_df = queuename(e, 'd'); 102 e->e_df = newstr(e->e_df); 103 if ((tf = dfopen(e->e_df, O_WRONLY|O_CREAT|O_TRUNC, FileMode)) == NULL) 104 { 105 syserr("Cannot create %s", e->e_df); 106 e->e_flags |= EF_NORETURN; 107 finis(); 108 } 109 if (fstat(fileno(tf), &stbuf) < 0) 110 e->e_dfino = -1; 111 else 112 { 113 e->e_dfdev = stbuf.st_dev; 114 e->e_dfino = stbuf.st_ino; 115 } 116 HasEightBits = FALSE; 117 } 118 119 /* 120 ** Tell ARPANET to go ahead. 121 */ 122 123 if (smtpmode) 124 message("354 Enter mail, end with \".\" on a line by itself"); 125 126 /* 127 ** Read the message. 128 ** 129 ** This is done using two interleaved state machines. 130 ** The input state machine is looking for things like 131 ** hidden dots; the message state machine is handling 132 ** the larger picture (e.g., header versus body). 133 */ 134 135 buf = bp = bufbuf; 136 buflen = sizeof bufbuf; 137 pbp = peekbuf; 138 istate = IS_BOL; 139 mstate = SaveFrom ? MS_HEADER : MS_UFROM; 140 CollectProgress = FALSE; 141 142 /* if transmitting binary, don't map NL to EOL */ 143 if (e->e_bodytype != NULL && strcasecmp(e->e_bodytype, "8BITMIME") == 0) 144 e->e_flags |= EF_NL_NOT_EOL; 145 146 if (dbto != 0) 147 { 148 /* handle possible input timeout */ 149 if (setjmp(CtxCollectTimeout) != 0) 150 { 151 #ifdef LOG 152 syslog(LOG_NOTICE, 153 "timeout waiting for input from %s during message collect", 154 CurHostName ? CurHostName : "<local machine>"); 155 #endif 156 errno = 0; 157 usrerr("451 timeout waiting for input during message collect"); 158 goto readerr; 159 } 160 CollectTimeout = setevent(dbto, collecttimeout, dbto); 161 } 162 163 for (;;) 164 { 165 if (tTd(30, 35)) 166 printf("top, istate=%d, mstate=%d\n", istate, mstate); 167 for (;;) 168 { 169 if (pbp > peekbuf) 170 c = *--pbp; 171 else 172 { 173 while (!feof(InChannel) && !ferror(InChannel)) 174 { 175 errno = 0; 176 c = fgetc(InChannel); 177 if (errno != EINTR) 178 break; 179 clearerr(InChannel); 180 } 181 CollectProgress = TRUE; 182 if (TrafficLogFile != NULL) 183 { 184 if (istate == IS_BOL) 185 fprintf(TrafficLogFile, "%05d <<< ", 186 getpid()); 187 if (c == EOF) 188 fprintf(TrafficLogFile, "[EOF]\n"); 189 else 190 fputc(c, TrafficLogFile); 191 } 192 if (c == EOF) 193 goto readerr; 194 if (SevenBitInput) 195 c &= 0x7f; 196 else 197 HasEightBits |= bitset(0x80, c); 198 e->e_msgsize++; 199 } 200 if (tTd(30, 94)) 201 printf("istate=%d, c=%c (0x%x)\n", 202 istate, c, c); 203 switch (istate) 204 { 205 case IS_BOL: 206 if (c == '.') 207 { 208 istate = IS_DOT; 209 continue; 210 } 211 break; 212 213 case IS_DOT: 214 if (c == '\n' && !ignrdot && 215 !bitset(EF_NL_NOT_EOL, e->e_flags)) 216 goto readerr; 217 else if (c == '\r' && 218 !bitset(EF_CRLF_NOT_EOL, e->e_flags)) 219 { 220 istate = IS_DOTCR; 221 continue; 222 } 223 else if (c != '.' || 224 (OpMode != MD_SMTP && 225 OpMode != MD_DAEMON && 226 OpMode != MD_ARPAFTP)) 227 { 228 *pbp++ = c; 229 c = '.'; 230 } 231 break; 232 233 case IS_DOTCR: 234 if (c == '\n') 235 goto readerr; 236 else 237 { 238 /* push back the ".\rx" */ 239 *pbp++ = c; 240 *pbp++ = '\r'; 241 c = '.'; 242 } 243 break; 244 245 case IS_CR: 246 if (c == '\n') 247 istate = IS_BOL; 248 else 249 { 250 ungetc(c, InChannel); 251 c = '\r'; 252 istate = IS_NORM; 253 } 254 goto bufferchar; 255 } 256 257 if (c == '\r' && !bitset(EF_CRLF_NOT_EOL, e->e_flags)) 258 { 259 istate = IS_CR; 260 continue; 261 } 262 else if (c == '\n' && !bitset(EF_NL_NOT_EOL, e->e_flags)) 263 istate = IS_BOL; 264 else 265 istate = IS_NORM; 266 267 bufferchar: 268 if (mstate == MS_BODY) 269 { 270 /* just put the character out */ 271 fputc(c, tf); 272 continue; 273 } 274 275 /* header -- buffer up */ 276 if (bp >= &buf[buflen - 2]) 277 { 278 char *obuf; 279 280 if (mstate != MS_HEADER) 281 break; 282 283 /* out of space for header */ 284 obuf = buf; 285 if (buflen < MEMCHUNKSIZE) 286 buflen *= 2; 287 else 288 buflen += MEMCHUNKSIZE; 289 buf = xalloc(buflen); 290 bcopy(obuf, buf, bp - obuf); 291 bp = &buf[bp - obuf]; 292 if (obuf != bufbuf) 293 free(obuf); 294 } 295 *bp++ = c; 296 if (istate == IS_BOL) 297 break; 298 } 299 *bp = '\0'; 300 301 nextstate: 302 if (tTd(30, 35)) 303 printf("nextstate, istate=%d, mstate=%d, line = \"%s\"\n", 304 istate, mstate, buf); 305 switch (mstate) 306 { 307 case MS_UFROM: 308 mstate = MS_HEADER; 309 if (strncmp(buf, "From ", 5) == 0) 310 { 311 eatfrom(buf, e); 312 continue; 313 } 314 /* fall through */ 315 316 case MS_HEADER: 317 if (!isheader(buf)) 318 { 319 mstate = MS_BODY; 320 goto nextstate; 321 } 322 323 /* check for possible continuation line */ 324 do 325 { 326 clearerr(InChannel); 327 errno = 0; 328 c = fgetc(InChannel); 329 } while (errno == EINTR); 330 if (c != EOF) 331 ungetc(c, InChannel); 332 if (c == ' ' || c == '\t') 333 { 334 /* yep -- defer this */ 335 continue; 336 } 337 338 /* trim off trailing CRLF or NL */ 339 if (*--bp != '\n' || *--bp != '\r') 340 bp++; 341 *bp = '\0'; 342 if (bitset(H_EOH, chompheader(buf, FALSE, e))) 343 mstate = MS_BODY; 344 break; 345 346 case MS_BODY: 347 if (tTd(30, 1)) 348 printf("EOH\n"); 349 if (headeronly) 350 goto readerr; 351 bp = buf; 352 353 /* toss blank line */ 354 if ((!bitset(EF_CRLF_NOT_EOL, e->e_flags) && 355 bp[0] == '\r' && bp[1] == '\n') || 356 (!bitset(EF_NL_NOT_EOL, e->e_flags) && 357 bp[0] == '\n')) 358 { 359 break; 360 } 361 362 /* if not a blank separator, write it out */ 363 while (*bp != '\0') 364 fputc(*bp++, tf); 365 break; 366 } 367 bp = buf; 368 } 369 370 readerr: 371 if ((feof(fp) && smtpmode) || ferror(fp)) 372 { 373 if (tTd(30, 1)) 374 printf("collect: read error\n"); 375 inputerr = TRUE; 376 } 377 378 /* reset global timer */ 379 clrevent(CollectTimeout); 380 381 if (headeronly) 382 return; 383 384 if (tf != NULL) 385 { 386 if (fflush(tf) != 0) 387 tferror(tf, e); 388 if (fsync(fileno(tf)) < 0 || fclose(tf) < 0) 389 { 390 tferror(tf, e); 391 finis(); 392 } 393 } 394 395 if (CollectErrorMessage != NULL && Errors <= 0) 396 { 397 if (CollectErrno != 0) 398 { 399 errno = CollectErrno; 400 syserr(CollectErrorMessage, e->e_df); 401 finis(); 402 } 403 usrerr(CollectErrorMessage); 404 } 405 else if (inputerr && (OpMode == MD_SMTP || OpMode == MD_DAEMON)) 406 { 407 /* An EOF when running SMTP is an error */ 408 char *host; 409 char *problem; 410 411 host = RealHostName; 412 if (host == NULL) 413 host = "localhost"; 414 415 if (feof(fp)) 416 problem = "unexpected close"; 417 else if (ferror(fp)) 418 problem = "I/O error"; 419 else 420 problem = "read timeout"; 421 # ifdef LOG 422 if (LogLevel > 0 && feof(fp)) 423 syslog(LOG_NOTICE, 424 "collect: %s on connection from %s, sender=%s: %s\n", 425 problem, host, e->e_from.q_paddr, errstring(errno)); 426 # endif 427 if (feof(fp)) 428 usrerr("451 collect: %s on connection from %s, from=%s", 429 problem, host, e->e_from.q_paddr); 430 else 431 syserr("451 collect: %s on connection from %s, from=%s", 432 problem, host, e->e_from.q_paddr); 433 434 /* don't return an error indication */ 435 e->e_to = NULL; 436 e->e_flags &= ~EF_FATALERRS; 437 e->e_flags |= EF_CLRQUEUE; 438 439 /* and don't try to deliver the partial message either */ 440 if (InChild) 441 ExitStat = EX_QUIT; 442 finis(); 443 } 444 445 /* 446 ** Find out some information from the headers. 447 ** Examples are who is the from person & the date. 448 */ 449 450 eatheader(e, !requeueflag); 451 452 /* collect statistics */ 453 if (OpMode != MD_VERIFY) 454 markstats(e, (ADDRESS *) NULL); 455 456 /* 457 ** Add an Apparently-To: line if we have no recipient lines. 458 */ 459 460 if (hvalue("to", e->e_header) == NULL && 461 hvalue("cc", e->e_header) == NULL && 462 hvalue("bcc", e->e_header) == NULL && 463 hvalue("apparently-to", e->e_header) == NULL) 464 { 465 register ADDRESS *q; 466 467 /* create an Apparently-To: field */ 468 /* that or reject the message.... */ 469 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 470 { 471 if (q->q_alias != NULL) 472 continue; 473 if (tTd(30, 3)) 474 printf("Adding Apparently-To: %s\n", q->q_paddr); 475 addheader("Apparently-To", q->q_paddr, &e->e_header); 476 } 477 } 478 479 /* check for message too large */ 480 if (MaxMessageSize > 0 && e->e_msgsize > MaxMessageSize) 481 { 482 usrerr("552 Message exceeds maximum fixed size (%ld)", 483 MaxMessageSize); 484 } 485 486 /* check for illegal 8-bit data */ 487 if (HasEightBits) 488 { 489 e->e_flags |= EF_HAS8BIT; 490 if (bitset(MM_MIME8BIT, MimeMode)) 491 { 492 /* convert it to MIME */ 493 if (hvalue("MIME-Version", e->e_header) == NULL) 494 { 495 char mimebuf[20]; 496 497 strcpy(mimebuf, "MIME-Version: 1.0"); 498 chompheader(mimebuf, FALSE, e); 499 } 500 if (e->e_bodytype == NULL) 501 e->e_bodytype = "8BITMIME"; 502 } 503 else if (!bitset(MM_PASS8BIT, MimeMode)) 504 usrerr("554 Eight bit data not allowed"); 505 } 506 507 if ((e->e_dfp = fopen(e->e_df, "r")) == NULL) 508 { 509 /* we haven't acked receipt yet, so just chuck this */ 510 syserr("Cannot reopen %s", e->e_df); 511 finis(); 512 } 513 } 514 515 516 static 517 collecttimeout(timeout) 518 time_t timeout; 519 { 520 /* if no progress was made, die now */ 521 if (!CollectProgress) 522 longjmp(CtxCollectTimeout, 1); 523 524 /* otherwise reset the timeout */ 525 CollectTimeout = setevent(timeout, collecttimeout, timeout); 526 CollectProgress = FALSE; 527 } 528 /* 529 ** TFERROR -- signal error on writing the temporary file. 530 ** 531 ** Parameters: 532 ** tf -- the file pointer for the temporary file. 533 ** 534 ** Returns: 535 ** none. 536 ** 537 ** Side Effects: 538 ** Gives an error message. 539 ** Arranges for following output to go elsewhere. 540 */ 541 542 tferror(tf, e) 543 FILE *tf; 544 register ENVELOPE *e; 545 { 546 CollectErrno = errno; 547 if (errno == ENOSPC) 548 { 549 struct stat st; 550 long avail; 551 long bsize; 552 553 e->e_flags |= EF_NORETURN; 554 if (fstat(fileno(tf), &st) < 0) 555 st.st_size = 0; 556 (void) freopen(e->e_df, "w", tf); 557 if (st.st_size <= 0) 558 fprintf(tf, "\n*** Mail could not be accepted"); 559 else if (sizeof st.st_size > sizeof (long)) 560 fprintf(tf, "\n*** Mail of at least %qd bytes could not be accepted\n", 561 st.st_size); 562 else 563 fprintf(tf, "\n*** Mail of at least %ld bytes could not be accepted\n", 564 st.st_size); 565 fprintf(tf, "*** at %s due to lack of disk space for temp file.\n", 566 MyHostName); 567 avail = freespace(QueueDir, &bsize); 568 if (avail > 0) 569 { 570 if (bsize > 1024) 571 avail *= bsize / 1024; 572 else if (bsize < 1024) 573 avail /= 1024 / bsize; 574 fprintf(tf, "*** Currently, %ld kilobytes are available for mail temp files.\n", 575 avail); 576 } 577 CollectErrorMessage = "452 Out of disk space for temp file"; 578 } 579 else 580 { 581 CollectErrorMessage = "cannot write message body to disk (%s)"; 582 } 583 (void) freopen("/dev/null", "w", tf); 584 } 585 /* 586 ** EATFROM -- chew up a UNIX style from line and process 587 ** 588 ** This does indeed make some assumptions about the format 589 ** of UNIX messages. 590 ** 591 ** Parameters: 592 ** fm -- the from line. 593 ** 594 ** Returns: 595 ** none. 596 ** 597 ** Side Effects: 598 ** extracts what information it can from the header, 599 ** such as the date. 600 */ 601 602 # ifndef NOTUNIX 603 604 char *DowList[] = 605 { 606 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 607 }; 608 609 char *MonthList[] = 610 { 611 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 612 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 613 NULL 614 }; 615 616 eatfrom(fm, e) 617 char *fm; 618 register ENVELOPE *e; 619 { 620 register char *p; 621 register char **dt; 622 623 if (tTd(30, 2)) 624 printf("eatfrom(%s)\n", fm); 625 626 /* find the date part */ 627 p = fm; 628 while (*p != '\0') 629 { 630 /* skip a word */ 631 while (*p != '\0' && *p != ' ') 632 p++; 633 while (*p == ' ') 634 p++; 635 if (!(isascii(*p) && isupper(*p)) || 636 p[3] != ' ' || p[13] != ':' || p[16] != ':') 637 continue; 638 639 /* we have a possible date */ 640 for (dt = DowList; *dt != NULL; dt++) 641 if (strncmp(*dt, p, 3) == 0) 642 break; 643 if (*dt == NULL) 644 continue; 645 646 for (dt = MonthList; *dt != NULL; dt++) 647 if (strncmp(*dt, &p[4], 3) == 0) 648 break; 649 if (*dt != NULL) 650 break; 651 } 652 653 if (*p != '\0') 654 { 655 char *q; 656 extern char *arpadate(); 657 658 /* we have found a date */ 659 q = xalloc(25); 660 (void) strncpy(q, p, 25); 661 q[24] = '\0'; 662 q = arpadate(q); 663 define('a', newstr(q), e); 664 } 665 } 666 667 # endif /* NOTUNIX */ 668