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