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