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