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.15 (Berkeley) 05/29/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 ** smtpmode -- if set, we are running SMTP: give an RFC821 25 ** style message to say we are ready to collect 26 ** input, and never ignore a single dot to mean 27 ** end of message. 28 ** requeueflag -- this message will be requeued later, so 29 ** don't do final processing on it. 30 ** e -- the current envelope. 31 ** 32 ** Returns: 33 ** none. 34 ** 35 ** Side Effects: 36 ** Temp file is created and filled. 37 ** The from person may be set. 38 */ 39 40 char *CollectErrorMessage; 41 bool CollectErrno; 42 43 collect(smtpmode, requeueflag, e) 44 bool smtpmode; 45 bool requeueflag; 46 register ENVELOPE *e; 47 { 48 register FILE *tf; 49 bool ignrdot = smtpmode ? FALSE : IgnrDot; 50 time_t dbto = smtpmode ? TimeOuts.to_datablock : 0; 51 register char *workbuf, *freebuf; 52 bool inputerr = FALSE; 53 char buf[MAXLINE], buf2[MAXLINE]; 54 extern char *hvalue(); 55 extern bool isheader(), flusheol(); 56 57 CollectErrorMessage = NULL; 58 CollectErrno = 0; 59 60 /* 61 ** Create the temp file name and create the file. 62 */ 63 64 e->e_df = queuename(e, 'd'); 65 e->e_df = newstr(e->e_df); 66 if ((tf = dfopen(e->e_df, O_WRONLY|O_CREAT|O_TRUNC, FileMode)) == NULL) 67 { 68 syserr("Cannot create %s", e->e_df); 69 NoReturn = TRUE; 70 finis(); 71 } 72 73 /* 74 ** Tell ARPANET to go ahead. 75 */ 76 77 if (smtpmode) 78 message("354 Enter mail, end with \".\" on a line by itself"); 79 80 /* set global timer to monitor progress */ 81 sfgetset(dbto); 82 83 /* 84 ** Try to read a UNIX-style From line 85 */ 86 87 if (sfgets(buf, MAXLINE, InChannel, dbto, 88 "initial message read") == NULL) 89 goto readerr; 90 fixcrlf(buf, FALSE); 91 # ifndef NOTUNIX 92 if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 93 { 94 if (!flusheol(buf, InChannel, dbto)) 95 goto readerr; 96 eatfrom(buf, e); 97 if (sfgets(buf, MAXLINE, InChannel, dbto, 98 "message header read") == NULL) 99 goto readerr; 100 fixcrlf(buf, FALSE); 101 } 102 # endif /* NOTUNIX */ 103 104 /* 105 ** Copy InChannel to temp file & do message editing. 106 ** To keep certain mailers from getting confused, 107 ** and to keep the output clean, lines that look 108 ** like UNIX "From" lines are deleted in the header. 109 */ 110 111 workbuf = buf; /* `workbuf' contains a header field */ 112 freebuf = buf2; /* `freebuf' can be used for read-ahead */ 113 for (;;) 114 { 115 char *curbuf; 116 int curbuffree; 117 register int curbuflen; 118 char *p; 119 120 /* first, see if the header is over */ 121 if (!isheader(workbuf)) 122 { 123 fixcrlf(workbuf, TRUE); 124 break; 125 } 126 127 /* if the line is too long, throw the rest away */ 128 if (!flusheol(workbuf, InChannel, dbto)) 129 goto readerr; 130 131 /* it's okay to toss '\n' now (flusheol() needed it) */ 132 fixcrlf(workbuf, TRUE); 133 134 curbuf = workbuf; 135 curbuflen = strlen(curbuf); 136 curbuffree = MAXLINE - curbuflen; 137 p = curbuf + curbuflen; 138 139 /* get the rest of this field */ 140 for (;;) 141 { 142 int clen; 143 144 if (sfgets(freebuf, MAXLINE, InChannel, 145 dbto, 146 "message header read") == NULL) 147 { 148 freebuf[0] = '\0'; 149 break; 150 } 151 152 /* is this a continuation line? */ 153 if (*freebuf != ' ' && *freebuf != '\t') 154 break; 155 156 if (!flusheol(freebuf, InChannel, dbto)) 157 goto readerr; 158 159 fixcrlf(freebuf, TRUE); 160 clen = strlen(freebuf) + 1; 161 162 /* if insufficient room, dynamically allocate buffer */ 163 if (clen >= curbuffree) 164 { 165 /* reallocate buffer */ 166 int nbuflen = ((p - curbuf) + clen) * 2; 167 char *nbuf = xalloc(nbuflen); 168 169 p = nbuf + curbuflen; 170 curbuffree = nbuflen - curbuflen; 171 bcopy(curbuf, nbuf, curbuflen); 172 if (curbuf != buf && curbuf != buf2) 173 free(curbuf); 174 curbuf = nbuf; 175 } 176 *p++ = '\n'; 177 bcopy(freebuf, p, clen - 1); 178 p += clen - 1; 179 curbuffree -= clen; 180 curbuflen += clen; 181 } 182 *p++ = '\0'; 183 184 e->e_msgsize += curbuflen; 185 186 /* 187 ** The working buffer now becomes the free buffer, since 188 ** the free buffer contains a new header field. 189 ** 190 ** This is premature, since we still havent called 191 ** chompheader() to process the field we just created 192 ** (so the call to chompheader() will use `freebuf'). 193 ** This convolution is necessary so that if we break out 194 ** of the loop due to H_EOH, `workbuf' will always be 195 ** the next unprocessed buffer. 196 */ 197 198 { 199 register char *tmp = workbuf; 200 workbuf = freebuf; 201 freebuf = tmp; 202 } 203 204 /* 205 ** Snarf header away. 206 */ 207 208 if (bitset(H_EOH, chompheader(curbuf, FALSE, e))) 209 break; 210 211 /* 212 ** If the buffer was dynamically allocated, free it. 213 */ 214 215 if (curbuf != buf && curbuf != buf2) 216 free(curbuf); 217 } 218 219 if (tTd(30, 1)) 220 printf("EOH\n"); 221 222 if (*workbuf == '\0') 223 { 224 /* throw away a blank line */ 225 if (sfgets(buf, MAXLINE, InChannel, dbto, 226 "message separator read") == NULL) 227 goto readerr; 228 } 229 else if (workbuf == buf2) /* guarantee `buf' contains data */ 230 (void) strcpy(buf, buf2); 231 232 /* 233 ** Collect the body of the message. 234 */ 235 236 for (;;) 237 { 238 register char *bp = buf; 239 240 fixcrlf(buf, TRUE); 241 242 /* check for end-of-message */ 243 if (!ignrdot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 244 break; 245 246 /* check for transparent dot */ 247 if ((OpMode == MD_SMTP || OpMode == MD_DAEMON) && 248 bp[0] == '.' && bp[1] == '.') 249 bp++; 250 251 /* 252 ** Figure message length, output the line to the temp 253 ** file, and insert a newline if missing. 254 */ 255 256 e->e_msgsize += strlen(bp) + 1; 257 fputs(bp, tf); 258 fputs("\n", tf); 259 if (ferror(tf)) 260 tferror(tf, e); 261 if (sfgets(buf, MAXLINE, InChannel, dbto, 262 "message body read") == NULL) 263 goto readerr; 264 } 265 266 if (feof(InChannel) || ferror(InChannel)) 267 { 268 readerr: 269 if (tTd(30, 1)) 270 printf("collect: read error\n"); 271 inputerr = TRUE; 272 } 273 274 /* reset global timer */ 275 sfgetset((time_t) 0); 276 277 if (fflush(tf) != 0) 278 tferror(tf, e); 279 if (fsync(fileno(tf)) < 0 || fclose(tf) < 0) 280 { 281 tferror(tf, e); 282 finis(); 283 } 284 285 if (CollectErrorMessage != NULL && Errors <= 0) 286 { 287 if (CollectErrno != 0) 288 { 289 errno = CollectErrno; 290 syserr(CollectErrorMessage, e->e_df); 291 finis(); 292 } 293 usrerr(CollectErrorMessage); 294 } 295 else if (inputerr && (OpMode == MD_SMTP || OpMode == MD_DAEMON)) 296 { 297 /* An EOF when running SMTP is an error */ 298 char *host; 299 char *problem; 300 301 host = RealHostName; 302 if (host == NULL) 303 host = "localhost"; 304 305 if (feof(InChannel)) 306 problem = "unexpected close"; 307 else if (ferror(InChannel)) 308 problem = "I/O error"; 309 else 310 problem = "read timeout"; 311 # ifdef LOG 312 if (LogLevel > 0 && feof(InChannel)) 313 syslog(LOG_NOTICE, 314 "collect: %s on connection from %s, sender=%s: %s\n", 315 problem, host, e->e_from.q_paddr, errstring(errno)); 316 # endif 317 if (feof(InChannel)) 318 usrerr("451 collect: %s on connection from %s, from=%s", 319 problem, host, e->e_from.q_paddr); 320 else 321 syserr("451 collect: %s on connection from %s, from=%s", 322 problem, host, e->e_from.q_paddr); 323 324 /* don't return an error indication */ 325 e->e_to = NULL; 326 e->e_flags &= ~EF_FATALERRS; 327 e->e_flags |= EF_CLRQUEUE; 328 329 /* and don't try to deliver the partial message either */ 330 if (InChild) 331 ExitStat = EX_QUIT; 332 finis(); 333 } 334 335 /* 336 ** Find out some information from the headers. 337 ** Examples are who is the from person & the date. 338 */ 339 340 eatheader(e, !requeueflag); 341 342 /* collect statistics */ 343 if (OpMode != MD_VERIFY) 344 markstats(e, (ADDRESS *) NULL); 345 346 /* 347 ** Add an Apparently-To: line if we have no recipient lines. 348 */ 349 350 if (hvalue("to", e) == NULL && hvalue("cc", e) == NULL && 351 hvalue("bcc", e) == NULL && hvalue("apparently-to", e) == NULL) 352 { 353 register ADDRESS *q; 354 355 /* create an Apparently-To: field */ 356 /* that or reject the message.... */ 357 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 358 { 359 if (q->q_alias != NULL) 360 continue; 361 if (tTd(30, 3)) 362 printf("Adding Apparently-To: %s\n", q->q_paddr); 363 addheader("Apparently-To", q->q_paddr, e); 364 } 365 } 366 367 /* check for message too large */ 368 if (MaxMessageSize > 0 && e->e_msgsize > MaxMessageSize) 369 { 370 usrerr("552 Message exceeds maximum fixed size (%ld)", 371 MaxMessageSize); 372 } 373 374 if ((e->e_dfp = fopen(e->e_df, "r")) == NULL) 375 { 376 /* we haven't acked receipt yet, so just chuck this */ 377 syserr("Cannot reopen %s", e->e_df); 378 finis(); 379 } 380 } 381 /* 382 ** FLUSHEOL -- if not at EOL, throw away rest of input line. 383 ** 384 ** Parameters: 385 ** buf -- last line read in (checked for '\n'), 386 ** fp -- file to be read from. 387 ** 388 ** Returns: 389 ** FALSE on error from sfgets(), TRUE otherwise. 390 ** 391 ** Side Effects: 392 ** none. 393 */ 394 395 bool 396 flusheol(buf, fp, dbto) 397 char *buf; 398 FILE *fp; 399 time_t dbto; 400 { 401 register char *p = buf; 402 char junkbuf[MAXLINE]; 403 404 while (strchr(p, '\n') == NULL) 405 { 406 CollectErrorMessage = "553 header line too long"; 407 CollectErrno = 0; 408 if (sfgets(junkbuf, MAXLINE, fp, dbto, 409 "long line flush") == NULL) 410 return (FALSE); 411 p = junkbuf; 412 } 413 414 return (TRUE); 415 } 416 /* 417 ** TFERROR -- signal error on writing the temporary file. 418 ** 419 ** Parameters: 420 ** tf -- the file pointer for the temporary file. 421 ** 422 ** Returns: 423 ** none. 424 ** 425 ** Side Effects: 426 ** Gives an error message. 427 ** Arranges for following output to go elsewhere. 428 */ 429 430 tferror(tf, e) 431 FILE *tf; 432 register ENVELOPE *e; 433 { 434 CollectErrno = errno; 435 if (errno == ENOSPC) 436 { 437 struct stat st; 438 long avail; 439 long bsize; 440 441 NoReturn = TRUE; 442 if (fstat(fileno(tf), &st) < 0) 443 st.st_size = 0; 444 (void) freopen(e->e_df, "w", tf); 445 if (st.st_size <= 0) 446 fprintf(tf, "\n*** Mail could not be accepted"); 447 else if (sizeof st.st_size > sizeof (long)) 448 fprintf(tf, "\n*** Mail of at least %qd bytes could not be accepted\n", 449 st.st_size); 450 else 451 fprintf(tf, "\n*** Mail of at least %ld bytes could not be accepted\n", 452 st.st_size); 453 fprintf(tf, "*** at %s due to lack of disk space for temp file.\n", 454 MyHostName); 455 avail = freespace(QueueDir, &bsize); 456 if (avail > 0) 457 { 458 if (bsize > 1024) 459 avail *= bsize / 1024; 460 else if (bsize < 1024) 461 avail /= 1024 / bsize; 462 fprintf(tf, "*** Currently, %ld kilobytes are available for mail temp files.\n", 463 avail); 464 } 465 CollectErrorMessage = "452 Out of disk space for temp file"; 466 } 467 else 468 { 469 CollectErrorMessage = "cannot write message body to disk (%s)"; 470 } 471 (void) freopen("/dev/null", "w", tf); 472 } 473 /* 474 ** EATFROM -- chew up a UNIX style from line and process 475 ** 476 ** This does indeed make some assumptions about the format 477 ** of UNIX messages. 478 ** 479 ** Parameters: 480 ** fm -- the from line. 481 ** 482 ** Returns: 483 ** none. 484 ** 485 ** Side Effects: 486 ** extracts what information it can from the header, 487 ** such as the date. 488 */ 489 490 # ifndef NOTUNIX 491 492 char *DowList[] = 493 { 494 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 495 }; 496 497 char *MonthList[] = 498 { 499 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 500 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 501 NULL 502 }; 503 504 eatfrom(fm, e) 505 char *fm; 506 register ENVELOPE *e; 507 { 508 register char *p; 509 register char **dt; 510 511 if (tTd(30, 2)) 512 printf("eatfrom(%s)\n", fm); 513 514 /* find the date part */ 515 p = fm; 516 while (*p != '\0') 517 { 518 /* skip a word */ 519 while (*p != '\0' && *p != ' ') 520 p++; 521 while (*p == ' ') 522 p++; 523 if (!(isascii(*p) && isupper(*p)) || 524 p[3] != ' ' || p[13] != ':' || p[16] != ':') 525 continue; 526 527 /* we have a possible date */ 528 for (dt = DowList; *dt != NULL; dt++) 529 if (strncmp(*dt, p, 3) == 0) 530 break; 531 if (*dt == NULL) 532 continue; 533 534 for (dt = MonthList; *dt != NULL; dt++) 535 if (strncmp(*dt, &p[4], 3) == 0) 536 break; 537 if (*dt != NULL) 538 break; 539 } 540 541 if (*p != '\0') 542 { 543 char *q; 544 extern char *arpadate(); 545 546 /* we have found a date */ 547 q = xalloc(25); 548 (void) strncpy(q, p, 25); 549 q[24] = '\0'; 550 q = arpadate(q); 551 define('a', newstr(q), e); 552 } 553 } 554 555 # endif /* NOTUNIX */ 556