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.24 (Berkeley) 08/18/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 bp = buf; 312 eatfrom(buf, e); 313 continue; 314 } 315 /* fall through */ 316 317 case MS_HEADER: 318 if (!isheader(buf)) 319 { 320 mstate = MS_BODY; 321 goto nextstate; 322 } 323 324 /* check for possible continuation line */ 325 do 326 { 327 clearerr(InChannel); 328 errno = 0; 329 c = fgetc(InChannel); 330 } while (errno == EINTR); 331 if (c != EOF) 332 ungetc(c, InChannel); 333 if (c == ' ' || c == '\t') 334 { 335 /* yep -- defer this */ 336 continue; 337 } 338 339 /* trim off trailing CRLF or NL */ 340 if (*--bp != '\n' || *--bp != '\r') 341 bp++; 342 *bp = '\0'; 343 if (bitset(H_EOH, chompheader(buf, FALSE, e))) 344 mstate = MS_BODY; 345 break; 346 347 case MS_BODY: 348 if (tTd(30, 1)) 349 printf("EOH\n"); 350 if (headeronly) 351 goto readerr; 352 bp = buf; 353 354 /* toss blank line */ 355 if ((!bitset(EF_CRLF_NOT_EOL, e->e_flags) && 356 bp[0] == '\r' && bp[1] == '\n') || 357 (!bitset(EF_NL_NOT_EOL, e->e_flags) && 358 bp[0] == '\n')) 359 { 360 break; 361 } 362 363 /* if not a blank separator, write it out */ 364 while (*bp != '\0') 365 fputc(*bp++, tf); 366 break; 367 } 368 bp = buf; 369 } 370 371 readerr: 372 if ((feof(fp) && smtpmode) || ferror(fp)) 373 { 374 if (tTd(30, 1)) 375 printf("collect: read error\n"); 376 inputerr = TRUE; 377 } 378 379 /* reset global timer */ 380 clrevent(CollectTimeout); 381 382 if (headeronly) 383 return; 384 385 if (tf != NULL) 386 { 387 if (fflush(tf) != 0) 388 tferror(tf, e); 389 if (fsync(fileno(tf)) < 0 || fclose(tf) < 0) 390 { 391 tferror(tf, e); 392 finis(); 393 } 394 } 395 396 if (CollectErrorMessage != NULL && Errors <= 0) 397 { 398 if (CollectErrno != 0) 399 { 400 errno = CollectErrno; 401 syserr(CollectErrorMessage, e->e_df); 402 finis(); 403 } 404 usrerr(CollectErrorMessage); 405 } 406 else if (inputerr && (OpMode == MD_SMTP || OpMode == MD_DAEMON)) 407 { 408 /* An EOF when running SMTP is an error */ 409 char *host; 410 char *problem; 411 412 host = RealHostName; 413 if (host == NULL) 414 host = "localhost"; 415 416 if (feof(fp)) 417 problem = "unexpected close"; 418 else if (ferror(fp)) 419 problem = "I/O error"; 420 else 421 problem = "read timeout"; 422 # ifdef LOG 423 if (LogLevel > 0 && feof(fp)) 424 syslog(LOG_NOTICE, 425 "collect: %s on connection from %s, sender=%s: %s\n", 426 problem, host, e->e_from.q_paddr, errstring(errno)); 427 # endif 428 if (feof(fp)) 429 usrerr("451 collect: %s on connection from %s, from=%s", 430 problem, host, e->e_from.q_paddr); 431 else 432 syserr("451 collect: %s on connection from %s, from=%s", 433 problem, host, e->e_from.q_paddr); 434 435 /* don't return an error indication */ 436 e->e_to = NULL; 437 e->e_flags &= ~EF_FATALERRS; 438 e->e_flags |= EF_CLRQUEUE; 439 440 /* and don't try to deliver the partial message either */ 441 if (InChild) 442 ExitStat = EX_QUIT; 443 finis(); 444 } 445 446 /* 447 ** Find out some information from the headers. 448 ** Examples are who is the from person & the date. 449 */ 450 451 eatheader(e, !requeueflag); 452 453 /* collect statistics */ 454 if (OpMode != MD_VERIFY) 455 markstats(e, (ADDRESS *) NULL); 456 457 /* 458 ** Add an Apparently-To: line if we have no recipient lines. 459 */ 460 461 if (hvalue("to", e->e_header) == NULL && 462 hvalue("cc", e->e_header) == NULL && 463 hvalue("bcc", e->e_header) == NULL && 464 hvalue("apparently-to", e->e_header) == NULL) 465 { 466 register ADDRESS *q; 467 468 /* create an Apparently-To: field */ 469 /* that or reject the message.... */ 470 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 471 { 472 if (q->q_alias != NULL) 473 continue; 474 if (tTd(30, 3)) 475 printf("Adding Apparently-To: %s\n", q->q_paddr); 476 addheader("Apparently-To", q->q_paddr, &e->e_header); 477 } 478 } 479 480 /* check for message too large */ 481 if (MaxMessageSize > 0 && e->e_msgsize > MaxMessageSize) 482 { 483 usrerr("552 Message exceeds maximum fixed size (%ld)", 484 MaxMessageSize); 485 } 486 487 /* check for illegal 8-bit data */ 488 if (HasEightBits) 489 { 490 e->e_flags |= EF_HAS8BIT; 491 if (bitset(MM_MIME8BIT, MimeMode)) 492 { 493 /* convert it to MIME */ 494 if (hvalue("MIME-Version", e->e_header) == NULL) 495 { 496 char mimebuf[20]; 497 498 strcpy(mimebuf, "MIME-Version: 1.0"); 499 chompheader(mimebuf, FALSE, e); 500 } 501 if (e->e_bodytype == NULL) 502 e->e_bodytype = "8BITMIME"; 503 } 504 else if (!bitset(MM_PASS8BIT, MimeMode)) 505 usrerr("554 Eight bit data not allowed"); 506 } 507 508 if ((e->e_dfp = fopen(e->e_df, "r")) == NULL) 509 { 510 /* we haven't acked receipt yet, so just chuck this */ 511 syserr("Cannot reopen %s", e->e_df); 512 finis(); 513 } 514 } 515 516 517 static 518 collecttimeout(timeout) 519 time_t timeout; 520 { 521 /* if no progress was made, die now */ 522 if (!CollectProgress) 523 longjmp(CtxCollectTimeout, 1); 524 525 /* otherwise reset the timeout */ 526 CollectTimeout = setevent(timeout, collecttimeout, timeout); 527 CollectProgress = FALSE; 528 } 529 /* 530 ** TFERROR -- signal error on writing the temporary file. 531 ** 532 ** Parameters: 533 ** tf -- the file pointer for the temporary file. 534 ** 535 ** Returns: 536 ** none. 537 ** 538 ** Side Effects: 539 ** Gives an error message. 540 ** Arranges for following output to go elsewhere. 541 */ 542 543 tferror(tf, e) 544 FILE *tf; 545 register ENVELOPE *e; 546 { 547 CollectErrno = errno; 548 if (errno == ENOSPC) 549 { 550 struct stat st; 551 long avail; 552 long bsize; 553 554 e->e_flags |= EF_NORETURN; 555 if (fstat(fileno(tf), &st) < 0) 556 st.st_size = 0; 557 (void) freopen(e->e_df, "w", tf); 558 if (st.st_size <= 0) 559 fprintf(tf, "\n*** Mail could not be accepted"); 560 else if (sizeof st.st_size > sizeof (long)) 561 fprintf(tf, "\n*** Mail of at least %qd bytes could not be accepted\n", 562 st.st_size); 563 else 564 fprintf(tf, "\n*** Mail of at least %ld bytes could not be accepted\n", 565 st.st_size); 566 fprintf(tf, "*** at %s due to lack of disk space for temp file.\n", 567 MyHostName); 568 avail = freespace(QueueDir, &bsize); 569 if (avail > 0) 570 { 571 if (bsize > 1024) 572 avail *= bsize / 1024; 573 else if (bsize < 1024) 574 avail /= 1024 / bsize; 575 fprintf(tf, "*** Currently, %ld kilobytes are available for mail temp files.\n", 576 avail); 577 } 578 CollectErrorMessage = "452 Out of disk space for temp file"; 579 } 580 else 581 { 582 CollectErrorMessage = "cannot write message body to disk (%s)"; 583 } 584 (void) freopen("/dev/null", "w", tf); 585 } 586 /* 587 ** EATFROM -- chew up a UNIX style from line and process 588 ** 589 ** This does indeed make some assumptions about the format 590 ** of UNIX messages. 591 ** 592 ** Parameters: 593 ** fm -- the from line. 594 ** 595 ** Returns: 596 ** none. 597 ** 598 ** Side Effects: 599 ** extracts what information it can from the header, 600 ** such as the date. 601 */ 602 603 # ifndef NOTUNIX 604 605 char *DowList[] = 606 { 607 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 608 }; 609 610 char *MonthList[] = 611 { 612 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 613 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 614 NULL 615 }; 616 617 eatfrom(fm, e) 618 char *fm; 619 register ENVELOPE *e; 620 { 621 register char *p; 622 register char **dt; 623 624 if (tTd(30, 2)) 625 printf("eatfrom(%s)\n", fm); 626 627 /* find the date part */ 628 p = fm; 629 while (*p != '\0') 630 { 631 /* skip a word */ 632 while (*p != '\0' && *p != ' ') 633 p++; 634 while (*p == ' ') 635 p++; 636 if (!(isascii(*p) && isupper(*p)) || 637 p[3] != ' ' || p[13] != ':' || p[16] != ':') 638 continue; 639 640 /* we have a possible date */ 641 for (dt = DowList; *dt != NULL; dt++) 642 if (strncmp(*dt, p, 3) == 0) 643 break; 644 if (*dt == NULL) 645 continue; 646 647 for (dt = MonthList; *dt != NULL; dt++) 648 if (strncmp(*dt, &p[4], 3) == 0) 649 break; 650 if (*dt != NULL) 651 break; 652 } 653 654 if (*p != '\0') 655 { 656 char *q; 657 extern char *arpadate(); 658 659 /* we have found a date */ 660 q = xalloc(25); 661 (void) strncpy(q, p, 25); 662 q[24] = '\0'; 663 q = arpadate(q); 664 define('a', newstr(q), e); 665 } 666 } 667 668 # endif /* NOTUNIX */ 669