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