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