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.41 (Berkeley) 05/23/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 } 519 520 /* check for illegal 8-bit data */ 521 if (HasEightBits) 522 { 523 e->e_flags |= EF_HAS8BIT; 524 if (!bitset(MM_PASS8BIT|MM_MIME8BIT, MimeMode)) 525 { 526 e->e_status = "5.6.1"; 527 usrerr("554 Eight bit data not allowed"); 528 } 529 } 530 else 531 { 532 /* if it claimed to be 8 bits, well, it lied.... */ 533 if (e->e_bodytype != NULL && 534 strcasecmp(e->e_bodytype, "8BITMIME") == 0) 535 e->e_bodytype = "7BIT"; 536 } 537 538 if ((e->e_dfp = fopen(dfname, "r")) == NULL) 539 { 540 /* we haven't acked receipt yet, so just chuck this */ 541 syserr("Cannot reopen %s", dfname); 542 finis(); 543 } 544 } 545 546 547 static void 548 collecttimeout(timeout) 549 time_t timeout; 550 { 551 /* if no progress was made, die now */ 552 if (!CollectProgress) 553 longjmp(CtxCollectTimeout, 1); 554 555 /* otherwise reset the timeout */ 556 CollectTimeout = setevent(timeout, collecttimeout, timeout); 557 CollectProgress = FALSE; 558 } 559 /* 560 ** TFERROR -- signal error on writing the temporary file. 561 ** 562 ** Parameters: 563 ** tf -- the file pointer for the temporary file. 564 ** e -- the current envelope. 565 ** 566 ** Returns: 567 ** none. 568 ** 569 ** Side Effects: 570 ** Gives an error message. 571 ** Arranges for following output to go elsewhere. 572 */ 573 574 void 575 tferror(tf, e) 576 FILE *tf; 577 register ENVELOPE *e; 578 { 579 if (errno == ENOSPC) 580 { 581 struct stat st; 582 long avail; 583 long bsize; 584 585 e->e_flags |= EF_NO_BODY_RETN; 586 if (fstat(fileno(tf), &st) < 0) 587 st.st_size = 0; 588 (void) freopen(queuename(e, 'd'), "w", tf); 589 if (st.st_size <= 0) 590 fprintf(tf, "\n*** Mail could not be accepted"); 591 else if (sizeof st.st_size > sizeof (long)) 592 fprintf(tf, "\n*** Mail of at least %qd bytes could not be accepted\n", 593 st.st_size); 594 else 595 fprintf(tf, "\n*** Mail of at least %ld bytes could not be accepted\n", 596 st.st_size); 597 fprintf(tf, "*** at %s due to lack of disk space for temp file.\n", 598 MyHostName); 599 avail = freespace(QueueDir, &bsize); 600 if (avail > 0) 601 { 602 if (bsize > 1024) 603 avail *= bsize / 1024; 604 else if (bsize < 1024) 605 avail /= 1024 / bsize; 606 fprintf(tf, "*** Currently, %ld kilobytes are available for mail temp files.\n", 607 avail); 608 } 609 e->e_status = "4.3.1"; 610 usrerr("452 Out of disk space for temp file"); 611 } 612 else 613 syserr("collect: Cannot write tf%s", e->e_id); 614 (void) freopen("/dev/null", "w", tf); 615 } 616 /* 617 ** EATFROM -- chew up a UNIX style from line and process 618 ** 619 ** This does indeed make some assumptions about the format 620 ** of UNIX messages. 621 ** 622 ** Parameters: 623 ** fm -- the from line. 624 ** 625 ** Returns: 626 ** none. 627 ** 628 ** Side Effects: 629 ** extracts what information it can from the header, 630 ** such as the date. 631 */ 632 633 # ifndef NOTUNIX 634 635 char *DowList[] = 636 { 637 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 638 }; 639 640 char *MonthList[] = 641 { 642 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 643 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 644 NULL 645 }; 646 647 void 648 eatfrom(fm, e) 649 char *fm; 650 register ENVELOPE *e; 651 { 652 register char *p; 653 register char **dt; 654 655 if (tTd(30, 2)) 656 printf("eatfrom(%s)\n", fm); 657 658 /* find the date part */ 659 p = fm; 660 while (*p != '\0') 661 { 662 /* skip a word */ 663 while (*p != '\0' && *p != ' ') 664 p++; 665 while (*p == ' ') 666 p++; 667 if (!(isascii(*p) && isupper(*p)) || 668 p[3] != ' ' || p[13] != ':' || p[16] != ':') 669 continue; 670 671 /* we have a possible date */ 672 for (dt = DowList; *dt != NULL; dt++) 673 if (strncmp(*dt, p, 3) == 0) 674 break; 675 if (*dt == NULL) 676 continue; 677 678 for (dt = MonthList; *dt != NULL; dt++) 679 if (strncmp(*dt, &p[4], 3) == 0) 680 break; 681 if (*dt != NULL) 682 break; 683 } 684 685 if (*p != '\0') 686 { 687 char *q; 688 extern char *arpadate(); 689 690 /* we have found a date */ 691 q = xalloc(25); 692 (void) strncpy(q, p, 25); 693 q[24] = '\0'; 694 q = arpadate(q); 695 define('a', newstr(q), e); 696 } 697 } 698 699 # endif /* NOTUNIX */ 700