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