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