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.8 (Berkeley) 01/08/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 (feof(InChannel) ? usrerr : syserr) 295 ("451 collect: %s on connection from %s, from=%s", 296 problem, host, e->e_from.q_paddr); 297 298 /* don't return an error indication */ 299 e->e_to = NULL; 300 e->e_flags &= ~EF_FATALERRS; 301 e->e_flags |= EF_CLRQUEUE; 302 303 /* and don't try to deliver the partial message either */ 304 if (InChild) 305 ExitStat = EX_QUIT; 306 finis(); 307 } 308 309 /* 310 ** Find out some information from the headers. 311 ** Examples are who is the from person & the date. 312 */ 313 314 eatheader(e, !requeueflag); 315 316 /* collect statistics */ 317 if (OpMode != MD_VERIFY) 318 markstats(e, (ADDRESS *) NULL); 319 320 /* 321 ** Add an Apparently-To: line if we have no recipient lines. 322 */ 323 324 if (hvalue("to", e) == NULL && hvalue("cc", e) == NULL && 325 hvalue("bcc", e) == NULL && hvalue("apparently-to", e) == NULL) 326 { 327 register ADDRESS *q; 328 329 /* create an Apparently-To: field */ 330 /* that or reject the message.... */ 331 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 332 { 333 if (q->q_alias != NULL) 334 continue; 335 if (tTd(30, 3)) 336 printf("Adding Apparently-To: %s\n", q->q_paddr); 337 addheader("Apparently-To", q->q_paddr, e); 338 } 339 } 340 341 /* check for message too large */ 342 if (MaxMessageSize > 0 && e->e_msgsize > MaxMessageSize) 343 { 344 usrerr("552 Message exceeds maximum fixed size (%ld)", 345 MaxMessageSize); 346 } 347 348 if ((e->e_dfp = fopen(e->e_df, "r")) == NULL) 349 { 350 /* we haven't acked receipt yet, so just chuck this */ 351 syserr("Cannot reopen %s", e->e_df); 352 finis(); 353 } 354 } 355 /* 356 ** FLUSHEOL -- if not at EOL, throw away rest of input line. 357 ** 358 ** Parameters: 359 ** buf -- last line read in (checked for '\n'), 360 ** fp -- file to be read from. 361 ** 362 ** Returns: 363 ** FALSE on error from sfgets(), TRUE otherwise. 364 ** 365 ** Side Effects: 366 ** none. 367 */ 368 369 bool 370 flusheol(buf, fp) 371 char *buf; 372 FILE *fp; 373 { 374 register char *p = buf; 375 bool printmsg = TRUE; 376 char junkbuf[MAXLINE]; 377 378 while (strchr(p, '\n') == NULL) 379 { 380 if (printmsg) 381 usrerr("553 header line too long"); 382 printmsg = FALSE; 383 if (sfgets(junkbuf, MAXLINE, fp, TimeOuts.to_datablock, 384 "long line flush") == NULL) 385 return (FALSE); 386 p = junkbuf; 387 } 388 389 return (TRUE); 390 } 391 /* 392 ** TFERROR -- signal error on writing the temporary file. 393 ** 394 ** Parameters: 395 ** tf -- the file pointer for the temporary file. 396 ** 397 ** Returns: 398 ** none. 399 ** 400 ** Side Effects: 401 ** Gives an error message. 402 ** Arranges for following output to go elsewhere. 403 */ 404 405 tferror(tf, e) 406 FILE *tf; 407 register ENVELOPE *e; 408 { 409 if (errno == ENOSPC) 410 { 411 (void) freopen(e->e_df, "w", tf); 412 fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 413 usrerr("452 Out of disk space for temp file"); 414 } 415 else 416 syserr("collect: Cannot write %s", e->e_df); 417 (void) freopen("/dev/null", "w", tf); 418 } 419 /* 420 ** EATFROM -- chew up a UNIX style from line and process 421 ** 422 ** This does indeed make some assumptions about the format 423 ** of UNIX messages. 424 ** 425 ** Parameters: 426 ** fm -- the from line. 427 ** 428 ** Returns: 429 ** none. 430 ** 431 ** Side Effects: 432 ** extracts what information it can from the header, 433 ** such as the date. 434 */ 435 436 # ifndef NOTUNIX 437 438 char *DowList[] = 439 { 440 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 441 }; 442 443 char *MonthList[] = 444 { 445 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 446 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 447 NULL 448 }; 449 450 eatfrom(fm, e) 451 char *fm; 452 register ENVELOPE *e; 453 { 454 register char *p; 455 register char **dt; 456 457 if (tTd(30, 2)) 458 printf("eatfrom(%s)\n", fm); 459 460 /* find the date part */ 461 p = fm; 462 while (*p != '\0') 463 { 464 /* skip a word */ 465 while (*p != '\0' && *p != ' ') 466 p++; 467 while (*p == ' ') 468 p++; 469 if (!(isascii(*p) && isupper(*p)) || 470 p[3] != ' ' || p[13] != ':' || p[16] != ':') 471 continue; 472 473 /* we have a possible date */ 474 for (dt = DowList; *dt != NULL; dt++) 475 if (strncmp(*dt, p, 3) == 0) 476 break; 477 if (*dt == NULL) 478 continue; 479 480 for (dt = MonthList; *dt != NULL; dt++) 481 if (strncmp(*dt, &p[4], 3) == 0) 482 break; 483 if (*dt != NULL) 484 break; 485 } 486 487 if (*p != '\0') 488 { 489 char *q; 490 extern char *arpadate(); 491 492 /* we have found a date */ 493 q = xalloc(25); 494 (void) strncpy(q, p, 25); 495 q[24] = '\0'; 496 q = arpadate(q); 497 define('a', newstr(q), e); 498 } 499 } 500 501 # endif /* NOTUNIX */ 502