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