1 # include <errno.h> 2 # include "sendmail.h" 3 4 SCCSID(@(#)collect.c 3.41 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 /* check for hop count overflow */ 273 if (HopCount > MAXHOP) 274 syserr("Too many hops (%d max); probably forwarding loop", MAXHOP); 275 276 if ((TempFile = fopen(CurEnv->e_df, "r")) == NULL) 277 syserr("Cannot reopen %s", CurEnv->e_df); 278 279 # ifdef DEBUG 280 if (Debug) 281 { 282 HDR *h; 283 extern char *capitalize(); 284 285 printf("----- collected header -----\n"); 286 for (h = CurEnv->e_header; h != NULL; h = h->h_link) 287 printf("%s: %s\n", capitalize(h->h_field), h->h_value); 288 printf("----------------------------\n"); 289 } 290 # endif DEBUG 291 return; 292 } 293 /* 294 ** EATFROM -- chew up a UNIX style from line and process 295 ** 296 ** This does indeed make some assumptions about the format 297 ** of UNIX messages. 298 ** 299 ** Parameters: 300 ** fm -- the from line. 301 ** 302 ** Returns: 303 ** none. 304 ** 305 ** Side Effects: 306 ** extracts what information it can from the header, 307 ** such as the date. 308 */ 309 310 # ifndef NOTUNIX 311 312 char *DowList[] = 313 { 314 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL 315 }; 316 317 char *MonthList[] = 318 { 319 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 320 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 321 NULL 322 }; 323 324 eatfrom(fm) 325 char *fm; 326 { 327 register char *p; 328 register char **dt; 329 330 # ifdef DEBUG 331 if (Debug > 1) 332 printf("eatfrom(%s)\n", fm); 333 # endif DEBUG 334 335 /* find the date part */ 336 p = fm; 337 while (*p != '\0') 338 { 339 /* skip a word */ 340 while (*p != '\0' && *p != ' ') 341 *p++; 342 while (*p == ' ') 343 *p++; 344 if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':') 345 continue; 346 347 /* we have a possible date */ 348 for (dt = DowList; *dt != NULL; dt++) 349 if (strncmp(*dt, p, 3) == 0) 350 break; 351 if (*dt == NULL) 352 continue; 353 354 for (dt = MonthList; *dt != NULL; dt++) 355 if (strncmp(*dt, &p[4], 3) == 0) 356 break; 357 if (*dt != NULL) 358 break; 359 } 360 361 if (*p != NULL) 362 { 363 char *q; 364 extern char *arpadate(); 365 366 /* we have found a date */ 367 q = xalloc(25); 368 strncpy(q, p, 25); 369 q[24] = '\0'; 370 define('d', q); 371 q = arpadate(q); 372 define('a', newstr(q)); 373 } 374 } 375 376 # endif NOTUNIX 377 /* 378 ** PRIENCODE -- encode external priority names into internal values. 379 ** 380 ** Parameters: 381 ** p -- priority in ascii. 382 ** 383 ** Returns: 384 ** priority as a numeric level. 385 ** 386 ** Side Effects: 387 ** none. 388 */ 389 390 struct prio 391 { 392 char *pri_name; /* external name of priority */ 393 int pri_val; /* internal value for same */ 394 }; 395 396 static struct prio Prio[] = 397 { 398 "alert", PRI_ALERT, 399 "quick", PRI_QUICK, 400 "first-class", PRI_FIRSTCL, 401 "normal", PRI_NORMAL, 402 "second-class", PRI_SECONDCL, 403 "third-class", PRI_THIRDCL, 404 "junk", PRI_JUNK, 405 NULL, PRI_NORMAL, 406 }; 407 408 priencode(p) 409 char *p; 410 { 411 register struct prio *pl; 412 extern bool sameword(); 413 414 for (pl = Prio; pl->pri_name != NULL; pl++) 415 { 416 if (sameword(p, pl->pri_name)) 417 break; 418 } 419 return (pl->pri_val); 420 } 421 /* 422 ** SPECHANDLE -- do special handling 423 ** 424 ** Parameters: 425 ** p -- pointer to list of special handling words. 426 ** 427 ** Returns: 428 ** none. 429 ** 430 ** Side Effects: 431 ** Sets flags as indicated by p. 432 */ 433 434 struct handling 435 { 436 char *han_name; /* word to get this magic */ 437 int han_what; /* what to do, see below */ 438 }; 439 440 /* modes for han_what */ 441 # define HAN_NONE 0 /* nothing special */ 442 # define HAN_RRECEIPT 1 /* give return receipt */ 443 444 struct handling Handling[] = 445 { 446 "return-receipt-requested", HAN_RRECEIPT, 447 NULL, HAN_NONE 448 }; 449 450 spechandling(p) 451 register char *p; 452 { 453 register char *w; 454 register struct handling *h; 455 extern bool sameword(); 456 457 while (*p != '\0') 458 { 459 /* collect a word to compare to */ 460 while (*p != '\0' && (*p == ',' || isspace(*p))) 461 p++; 462 if (*p == '\0') 463 break; 464 w = p; 465 while (*p != '\0' && *p != ',' && !isspace(*p)) 466 p++; 467 if (*p != '\0') 468 *p++ = '\0'; 469 470 /* scan the special handling table */ 471 for (h = Handling; h->han_name != NULL; h++) 472 if (sameword(h->han_name, w)) 473 break; 474 475 /* see if we can do anything interesting */ 476 switch (h->han_what) 477 { 478 case HAN_NONE: /* nothing to be done */ 479 break; 480 481 case HAN_RRECEIPT: /* give return receipt */ 482 CurEnv->e_retreceipt = TRUE; 483 # ifdef DEBUG 484 if (Debug > 2) 485 printf(">>> Return receipt requested\n"); 486 # endif DEBUG 487 break; 488 489 default: 490 syserr("spechandling: handling %d (%s)", h->han_what, w); 491 } 492 } 493 } 494