1 # include <errno.h> 2 # include "sendmail.h" 3 4 SCCSID(@(#)collect.c 3.42 07/05/82); 5 6 /* 7 ** COLLECT -- read & parse message header & make temp file. 8 ** 9 ** Creates a temporary file name and copies the standard 10 ** input to that file. While it is doing it, it looks for 11 ** "From:" and "Sender:" fields to use as the from-person 12 ** (but only if the -a flag is specified). It prefers to 13 ** to use the "Sender:" field. 14 ** 15 ** MIT seems to like to produce "Sent-By:" fields instead 16 ** of "Sender:" fields. We used to catch this, but it turns 17 ** out that the "Sent-By:" field doesn't always correspond 18 ** to someone real ("___057", for instance), as required by 19 ** the protocol. So we limp by..... 20 ** 21 ** Parameters: 22 ** sayok -- if set, give an ARPANET style message 23 ** to say we are ready to collect input. 24 ** 25 ** Returns: 26 ** none. 27 ** 28 ** Side Effects: 29 ** Temp file is created and filled. 30 ** The from person may be set. 31 */ 32 33 collect(sayok) 34 bool sayok; 35 { 36 register FILE *tf; 37 char buf[MAXFIELD+1]; 38 register char *p; 39 char *xfrom; 40 extern char *hvalue(); 41 extern char *mktemp(); 42 static char tempfname[40]; 43 extern char *macvalue(); 44 45 /* 46 ** Create the temp file name and create the file. 47 */ 48 49 (void) strcpy(tempfname, QueueDir); 50 (void) strcat(tempfname, "/dfXXXXXX"); 51 (void) mktemp(tempfname); 52 if ((tf = dfopen(tempfname, "w")) == NULL) 53 { 54 syserr("Cannot create %s", tempfname); 55 NoReturn = TRUE; 56 finis(); 57 } 58 (void) chmod(tempfname, 0600); 59 CurEnv->e_df = tempfname; 60 61 /* 62 ** Create the Mail-From line if we want to. 63 */ 64 65 if (Smtp && macvalue('s') != NULL) 66 { 67 char xbuf[50]; 68 69 (void) sprintf(xbuf, "Mail-From: %s$s received by $i at $b", 70 macvalue('r') == NULL ? "" : "$r host "); 71 expand(xbuf, buf, &buf[sizeof buf - 1], CurEnv); 72 (void) chompheader(buf, FALSE); 73 } 74 75 /* 76 ** Tell ARPANET to go ahead. 77 */ 78 79 if (sayok) 80 message("354", "Enter mail, end with \".\" on a line by itself"); 81 82 /* 83 ** Try to read a UNIX-style From line 84 */ 85 86 if (fgets(buf, sizeof buf, InChannel) == NULL) 87 return; 88 fixcrlf(buf, FALSE); 89 # ifndef NOTUNIX 90 if (!SaveFrom && strncmp(buf, "From ", 5) == 0) 91 { 92 eatfrom(buf); 93 (void) fgets(buf, sizeof buf, InChannel); 94 fixcrlf(buf, FALSE); 95 } 96 # endif NOTUNIX 97 98 /* 99 ** Copy InChannel to temp file & do message editing. 100 ** To keep certain mailers from getting confused, 101 ** and to keep the output clean, lines that look 102 ** like UNIX "From" lines are deleted in the header, 103 ** and prepended with ">" in the body. 104 */ 105 106 for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL) 107 { 108 register char c; 109 extern bool isheader(); 110 111 fixcrlf(buf, FALSE); 112 113 /* see if the header is over */ 114 if (!isheader(buf)) 115 break; 116 117 /* get the rest of this field */ 118 while ((c = getc(InChannel)) == ' ' || c == '\t') 119 { 120 p = &buf[strlen(buf)]; 121 *p++ = c; 122 if (fgets(p, sizeof buf - (p - buf), InChannel) == NULL) 123 break; 124 fixcrlf(p, FALSE); 125 } 126 if (!feof(InChannel)) 127 (void) ungetc(c, InChannel); 128 129 CurEnv->e_msgsize += strlen(buf); 130 131 /* 132 ** Snarf header away. 133 */ 134 135 if (bitset(H_EOH, chompheader(buf, FALSE))) 136 break; 137 } 138 139 # ifdef DEBUG 140 if (Debug) 141 printf("EOH\n"); 142 # endif DEBUG 143 144 /* throw away a blank line */ 145 if (buf[0] == '\n') 146 { 147 (void) fgets(buf, sizeof buf, InChannel); 148 fixcrlf(buf, FALSE); 149 } 150 151 /* 152 ** Collect the body of the message. 153 */ 154 155 for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL) 156 { 157 register int i; 158 register char *bp = buf; 159 160 fixcrlf(buf, FALSE); 161 162 /* check for end-of-message */ 163 if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0')) 164 break; 165 166 /* check for transparent dot */ 167 if (Smtp && *bp == '.') 168 bp++; 169 170 # ifndef NOTUNIX 171 /* Hide UNIX-like From lines */ 172 if (strncmp(bp, "From ", 5) == 0) 173 { 174 fputs(">", tf); 175 CurEnv->e_msgsize++; 176 } 177 # endif NOTUNIX 178 179 /* 180 ** Figure message length, output the line to the temp 181 ** file, and insert a newline if missing. 182 */ 183 184 i = strlen(bp); 185 CurEnv->e_msgsize += i; 186 fputs(bp, tf); 187 if (bp[i - 1] != '\n') 188 fputs("\n", tf); 189 if (ferror(tf)) 190 { 191 if (errno == ENOSPC) 192 { 193 (void) freopen(CurEnv->e_df, "w", tf); 194 fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf); 195 usrerr("452 Out of disk space for temp file"); 196 } 197 else 198 syserr("collect: Cannot write %s", CurEnv->e_df); 199 (void) freopen("/dev/null", "w", tf); 200 } 201 } 202 (void) fclose(tf); 203 204 /* 205 ** Find out some information from the headers. 206 ** Examples are who is the from person & the date. 207 */ 208 209 /* message priority */ 210 if (!QueueRun) 211 { 212 /* adjust total priority by message priority */ 213 CurEnv->e_msgpriority = CurEnv->e_msgsize; 214 p = hvalue("priority"); 215 if (p != NULL) 216 CurEnv->e_class = priencode(p); 217 else 218 CurEnv->e_class = PRI_NORMAL; 219 CurEnv->e_msgpriority -= CurEnv->e_class * WKPRIFACT; 220 } 221 222 /* special handling */ 223 p = hvalue("special-handling"); 224 if (p != NULL) 225 spechandling(p); 226 227 /* from person */ 228 xfrom = hvalue("sender"); 229 if (xfrom == NULL) 230 xfrom = CurEnv->e_origfrom; 231 if (ArpaMode) 232 setfrom(xfrom, (char *) NULL); 233 234 /* full name of from person */ 235 p = hvalue("full-name"); 236 if (p != NULL) 237 define('x', p); 238 else 239 { 240 extern char *getxpart(); 241 242 /* 243 ** Try to extract the full name from a general From: 244 ** field. We take anything which is a comment as a 245 ** first choice. Failing in that, we see if there is 246 ** a "machine readable" name (in <angle brackets>); if 247 ** so we take anything preceeding that clause. 248 ** 249 ** If we blow it here it's not all that serious. 250 */ 251 252 p = hvalue("original-from"); 253 if (p == NULL) 254 p = CurEnv->e_origfrom; 255 p = getxpart(p); 256 if (p != NULL) 257 define('x', newstr(p)); 258 } 259 260 /* date message originated */ 261 p = hvalue("posted-date"); 262 if (p == NULL) 263 p = hvalue("date"); 264 if (p != NULL) 265 { 266 define('a', p); 267 /* we don't have a good way to do canonical conversion .... 268 define('d', newstr(arpatounix(p))); 269 .... so we will ignore the problem for the time being */ 270 } 271 272 if (hvalue("to") == NULL && hvalue("cc") == NULL && 273 hvalue("bcc") == NULL && hvalue("apparently-to") == NULL) 274 { 275 register ADDRESS *q; 276 277 /* create an Apparently-To: field */ 278 /* that or reject the message.... */ 279 for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next) 280 { 281 # ifdef DEBUG 282 if (Debug > 1) 283 printf("Adding Apparently-To: %s\n", q->q_paddr); 284 # endif DEBUG 285 addheader("apparently-to", q->q_paddr, CurEnv); 286 } 287 } 288 289 /* check for hop count overflow */ 290 if (HopCount > MAXHOP) 291 syserr("Too many hops (%d max); probably forwarding loop", MAXHOP); 292 293 if ((TempFile = fopen(CurEnv->e_df, "r")) == NULL) 294 syserr("Cannot reopen %s", CurEnv->e_df); 295 296 # ifdef DEBUG 297 if (Debug) 298 { 299 HDR *h; 300 extern char *capitalize(); 301 302 printf("----- collected header -----\n"); 303 for (h = CurEnv->e_header; h != NULL; h = h->h_link) 304 printf("%s: %s\n", capitalize(h->h_field), h->h_value); 305 printf("----------------------------\n"); 306 } 307 # endif DEBUG 308 return; 309 } 310 /* 311 ** EATFROM -- chew up a UNIX style from line and process 312 ** 313 ** This does indeed make some assumptions about the format 314 ** of UNIX messages. 315 ** 316 ** Parameters: 317 ** fm -- the from line. 318 ** 319 ** Returns: 320 ** none. 321 ** 322 ** Side Effects: 323 ** extracts what information it can from the header, 324 ** such as the date. 325 */ 326 327 # ifndef NOTUNIX 328 329 char *DowList[] = 330 { 331 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 332 }; 333 334 char *MonthList[] = 335 { 336 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 337 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 338 NULL 339 }; 340 341 eatfrom(fm) 342 char *fm; 343 { 344 register char *p; 345 register char **dt; 346 347 # ifdef DEBUG 348 if (Debug > 1) 349 printf("eatfrom(%s)\n", fm); 350 # endif DEBUG 351 352 /* find the date part */ 353 p = fm; 354 while (*p != '\0') 355 { 356 /* skip a word */ 357 while (*p != '\0' && *p != ' ') 358 *p++; 359 while (*p == ' ') 360 *p++; 361 if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 362 continue; 363 364 /* we have a possible date */ 365 for (dt = DowList; *dt != NULL; dt++) 366 if (strncmp(*dt, p, 3) == 0) 367 break; 368 if (*dt == NULL) 369 continue; 370 371 for (dt = MonthList; *dt != NULL; dt++) 372 if (strncmp(*dt, &p[4], 3) == 0) 373 break; 374 if (*dt != NULL) 375 break; 376 } 377 378 if (*p != NULL) 379 { 380 char *q; 381 extern char *arpadate(); 382 383 /* we have found a date */ 384 q = xalloc(25); 385 strncpy(q, p, 25); 386 q[24] = '\0'; 387 define('d', q); 388 q = arpadate(q); 389 define('a', newstr(q)); 390 } 391 } 392 393 # endif NOTUNIX 394 /* 395 ** PRIENCODE -- encode external priority names into internal values. 396 ** 397 ** Parameters: 398 ** p -- priority in ascii. 399 ** 400 ** Returns: 401 ** priority as a numeric level. 402 ** 403 ** Side Effects: 404 ** none. 405 */ 406 407 struct prio 408 { 409 char *pri_name; /* external name of priority */ 410 int pri_val; /* internal value for same */ 411 }; 412 413 static struct prio Prio[] = 414 { 415 "alert", PRI_ALERT, 416 "quick", PRI_QUICK, 417 "first-class", PRI_FIRSTCL, 418 "normal", PRI_NORMAL, 419 "second-class", PRI_SECONDCL, 420 "third-class", PRI_THIRDCL, 421 "junk", PRI_JUNK, 422 NULL, PRI_NORMAL, 423 }; 424 425 priencode(p) 426 char *p; 427 { 428 register struct prio *pl; 429 extern bool sameword(); 430 431 for (pl = Prio; pl->pri_name != NULL; pl++) 432 { 433 if (sameword(p, pl->pri_name)) 434 break; 435 } 436 return (pl->pri_val); 437 } 438 /* 439 ** SPECHANDLE -- do special handling 440 ** 441 ** Parameters: 442 ** p -- pointer to list of special handling words. 443 ** 444 ** Returns: 445 ** none. 446 ** 447 ** Side Effects: 448 ** Sets flags as indicated by p. 449 */ 450 451 struct handling 452 { 453 char *han_name; /* word to get this magic */ 454 int han_what; /* what to do, see below */ 455 }; 456 457 /* modes for han_what */ 458 # define HAN_NONE 0 /* nothing special */ 459 # define HAN_RRECEIPT 1 /* give return receipt */ 460 461 struct handling Handling[] = 462 { 463 "return-receipt-requested", HAN_RRECEIPT, 464 NULL, HAN_NONE 465 }; 466 467 spechandling(p) 468 register char *p; 469 { 470 register char *w; 471 register struct handling *h; 472 extern bool sameword(); 473 474 while (*p != '\0') 475 { 476 /* collect a word to compare to */ 477 while (*p != '\0' && (*p == ',' || isspace(*p))) 478 p++; 479 if (*p == '\0') 480 break; 481 w = p; 482 while (*p != '\0' && *p != ',' && !isspace(*p)) 483 p++; 484 if (*p != '\0') 485 *p++ = '\0'; 486 487 /* scan the special handling table */ 488 for (h = Handling; h->han_name != NULL; h++) 489 if (sameword(h->han_name, w)) 490 break; 491 492 /* see if we can do anything interesting */ 493 switch (h->han_what) 494 { 495 case HAN_NONE: /* nothing to be done */ 496 break; 497 498 case HAN_RRECEIPT: /* give return receipt */ 499 CurEnv->e_retreceipt = TRUE; 500 # ifdef DEBUG 501 if (Debug > 2) 502 printf(">>> Return receipt requested\n"); 503 # endif DEBUG 504 break; 505 506 default: 507 syserr("spechandling: handling %d (%s)", h->han_what, w); 508 } 509 } 510 } 511