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[] = "@(#)headers.c 5.16 (Berkeley) 10/20/91"; 11 #endif /* not lint */ 12 13 # include <sys/param.h> 14 # include <errno.h> 15 # include "sendmail.h" 16 17 /* 18 ** CHOMPHEADER -- process and save a header line. 19 ** 20 ** Called by collect and by readcf to deal with header lines. 21 ** 22 ** Parameters: 23 ** line -- header as a text line. 24 ** def -- if set, this is a default value. 25 ** 26 ** Returns: 27 ** flags for this header. 28 ** 29 ** Side Effects: 30 ** The header is saved on the header list. 31 ** Contents of 'line' are destroyed. 32 */ 33 34 chompheader(line, def) 35 char *line; 36 bool def; 37 { 38 register char *p; 39 register HDR *h; 40 HDR **hp; 41 char *fname; 42 char *fvalue; 43 struct hdrinfo *hi; 44 bool cond = FALSE; 45 BITMAP mopts; 46 extern char *crackaddr(); 47 48 if (tTd(31, 6)) 49 printf("chompheader: %s\n", line); 50 51 /* strip off options */ 52 clrbitmap(mopts); 53 p = line; 54 if (*p == '?') 55 { 56 /* have some */ 57 register char *q = index(p + 1, *p); 58 59 if (q != NULL) 60 { 61 *q++ = '\0'; 62 while (*++p != '\0') 63 setbitn(*p, mopts); 64 p = q; 65 } 66 else 67 usrerr("chompheader: syntax error, line \"%s\"", line); 68 cond = TRUE; 69 } 70 71 /* find canonical name */ 72 fname = p; 73 p = index(p, ':'); 74 if (p == NULL) 75 { 76 syserr("chompheader: syntax error, line \"%s\"", line); 77 return (0); 78 } 79 fvalue = &p[1]; 80 while (isspace(*--p)) 81 continue; 82 *++p = '\0'; 83 makelower(fname); 84 85 /* strip field value on front */ 86 if (*fvalue == ' ') 87 fvalue++; 88 89 /* see if it is a known type */ 90 for (hi = HdrInfo; hi->hi_field != NULL; hi++) 91 { 92 if (strcmp(hi->hi_field, fname) == 0) 93 break; 94 } 95 96 /* see if this is a resent message */ 97 if (!def && bitset(H_RESENT, hi->hi_flags)) 98 CurEnv->e_flags |= EF_RESENT; 99 100 /* if this means "end of header" quit now */ 101 if (bitset(H_EOH, hi->hi_flags)) 102 return (hi->hi_flags); 103 104 /* drop explicit From: if same as what we would generate -- for MH */ 105 p = "resent-from"; 106 if (!bitset(EF_RESENT, CurEnv->e_flags)) 107 p += 7; 108 if (!def && !QueueRun && strcmp(fname, p) == 0) 109 { 110 if (CurEnv->e_from.q_paddr != NULL && 111 strcmp(fvalue, CurEnv->e_from.q_paddr) == 0) 112 return (hi->hi_flags); 113 } 114 115 /* delete default value for this header */ 116 for (hp = &CurEnv->e_header; (h = *hp) != NULL; hp = &h->h_link) 117 { 118 if (strcmp(fname, h->h_field) == 0 && 119 bitset(H_DEFAULT, h->h_flags) && 120 !bitset(H_FORCE, h->h_flags)) 121 h->h_value = NULL; 122 } 123 124 /* create a new node */ 125 h = (HDR *) xalloc(sizeof *h); 126 h->h_field = newstr(fname); 127 h->h_value = NULL; 128 h->h_link = NULL; 129 bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts); 130 *hp = h; 131 h->h_flags = hi->hi_flags; 132 if (def) 133 h->h_flags |= H_DEFAULT; 134 if (cond) 135 h->h_flags |= H_CHECK; 136 if (h->h_value != NULL) 137 free((char *) h->h_value); 138 h->h_value = newstr(fvalue); 139 140 /* hack to see if this is a new format message */ 141 if (!def && bitset(H_RCPT|H_FROM, h->h_flags) && 142 (index(fvalue, ',') != NULL || index(fvalue, '(') != NULL || 143 index(fvalue, '<') != NULL || index(fvalue, ';') != NULL)) 144 { 145 CurEnv->e_flags &= ~EF_OLDSTYLE; 146 } 147 148 return (h->h_flags); 149 } 150 /* 151 ** ADDHEADER -- add a header entry to the end of the queue. 152 ** 153 ** This bypasses the special checking of chompheader. 154 ** 155 ** Parameters: 156 ** field -- the name of the header field. 157 ** value -- the value of the field. It must be lower-cased. 158 ** e -- the envelope to add them to. 159 ** 160 ** Returns: 161 ** none. 162 ** 163 ** Side Effects: 164 ** adds the field on the list of headers for this envelope. 165 */ 166 167 addheader(field, value, e) 168 char *field; 169 char *value; 170 ENVELOPE *e; 171 { 172 register HDR *h; 173 register struct hdrinfo *hi; 174 HDR **hp; 175 176 /* find info struct */ 177 for (hi = HdrInfo; hi->hi_field != NULL; hi++) 178 { 179 if (strcmp(field, hi->hi_field) == 0) 180 break; 181 } 182 183 /* find current place in list -- keep back pointer? */ 184 for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link) 185 { 186 if (strcmp(field, h->h_field) == 0) 187 break; 188 } 189 190 /* allocate space for new header */ 191 h = (HDR *) xalloc(sizeof *h); 192 h->h_field = field; 193 h->h_value = newstr(value); 194 h->h_link = *hp; 195 h->h_flags = hi->hi_flags | H_DEFAULT; 196 clrbitmap(h->h_mflags); 197 *hp = h; 198 } 199 /* 200 ** HVALUE -- return value of a header. 201 ** 202 ** Only "real" fields (i.e., ones that have not been supplied 203 ** as a default) are used. 204 ** 205 ** Parameters: 206 ** field -- the field name. 207 ** 208 ** Returns: 209 ** pointer to the value part. 210 ** NULL if not found. 211 ** 212 ** Side Effects: 213 ** none. 214 */ 215 216 char * 217 hvalue(field) 218 char *field; 219 { 220 register HDR *h; 221 222 for (h = CurEnv->e_header; h != NULL; h = h->h_link) 223 { 224 if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0) 225 return (h->h_value); 226 } 227 return (NULL); 228 } 229 /* 230 ** ISHEADER -- predicate telling if argument is a header. 231 ** 232 ** A line is a header if it has a single word followed by 233 ** optional white space followed by a colon. 234 ** 235 ** Parameters: 236 ** s -- string to check for possible headerness. 237 ** 238 ** Returns: 239 ** TRUE if s is a header. 240 ** FALSE otherwise. 241 ** 242 ** Side Effects: 243 ** none. 244 */ 245 246 bool 247 isheader(s) 248 register char *s; 249 { 250 while (*s > ' ' && *s != ':' && *s != '\0') 251 s++; 252 253 /* following technically violates RFC822 */ 254 while (isspace(*s)) 255 s++; 256 257 return (*s == ':'); 258 } 259 /* 260 ** EATHEADER -- run through the stored header and extract info. 261 ** 262 ** Parameters: 263 ** e -- the envelope to process. 264 ** 265 ** Returns: 266 ** none. 267 ** 268 ** Side Effects: 269 ** Sets a bunch of global variables from information 270 ** in the collected header. 271 ** Aborts the message if the hop count is exceeded. 272 */ 273 274 eatheader(e) 275 register ENVELOPE *e; 276 { 277 register HDR *h; 278 register char *p; 279 int hopcnt = 0; 280 281 if (tTd(32, 1)) 282 printf("----- collected header -----\n"); 283 for (h = e->e_header; h != NULL; h = h->h_link) 284 { 285 extern char *capitalize(); 286 287 if (tTd(32, 1)) 288 printf("%s: %s\n", capitalize(h->h_field), h->h_value); 289 /* count the number of times it has been processed */ 290 if (bitset(H_TRACE, h->h_flags)) 291 hopcnt++; 292 293 /* send to this person if we so desire */ 294 if (GrabTo && bitset(H_RCPT, h->h_flags) && 295 !bitset(H_DEFAULT, h->h_flags) && 296 (!bitset(EF_RESENT, CurEnv->e_flags) || bitset(H_RESENT, h->h_flags))) 297 { 298 sendtolist(h->h_value, (ADDRESS *) NULL, &CurEnv->e_sendqueue); 299 } 300 301 /* log the message-id */ 302 #ifdef LOG 303 if (!QueueRun && LogLevel > 8 && h->h_value != NULL && 304 strcmp(h->h_field, "message-id") == 0) 305 { 306 char buf[MAXNAME]; 307 308 p = h->h_value; 309 if (bitset(H_DEFAULT, h->h_flags)) 310 { 311 expand(p, buf, &buf[sizeof buf], e); 312 p = buf; 313 } 314 syslog(LOG_INFO, "%s: message-id=%s", e->e_id, p); 315 } 316 #endif LOG 317 } 318 if (tTd(32, 1)) 319 printf("----------------------------\n"); 320 321 /* store hop count */ 322 if (hopcnt > e->e_hopcount) 323 e->e_hopcount = hopcnt; 324 325 /* message priority */ 326 p = hvalue("precedence"); 327 if (p != NULL) 328 e->e_class = priencode(p); 329 if (!QueueRun) 330 e->e_msgpriority = e->e_msgsize 331 - e->e_class * WkClassFact 332 + e->e_nrcpts * WkRecipFact; 333 334 /* return receipt to */ 335 p = hvalue("return-receipt-to"); 336 if (p != NULL) 337 e->e_receiptto = p; 338 339 /* errors to */ 340 p = hvalue("errors-to"); 341 if (p != NULL) 342 sendtolist(p, (ADDRESS *) NULL, &e->e_errorqueue); 343 344 /* from person */ 345 if (OpMode == MD_ARPAFTP) 346 { 347 register struct hdrinfo *hi = HdrInfo; 348 349 for (p = NULL; p == NULL && hi->hi_field != NULL; hi++) 350 { 351 if (bitset(H_FROM, hi->hi_flags)) 352 p = hvalue(hi->hi_field); 353 } 354 if (p != NULL) 355 setsender(p); 356 } 357 358 /* full name of from person */ 359 p = hvalue("full-name"); 360 if (p != NULL) 361 define('x', p, e); 362 363 /* date message originated */ 364 p = hvalue("posted-date"); 365 if (p == NULL) 366 p = hvalue("date"); 367 if (p != NULL) 368 { 369 define('a', p, e); 370 /* we don't have a good way to do canonical conversion .... 371 define('d', newstr(arpatounix(p)), e); 372 .... so we will ignore the problem for the time being */ 373 } 374 375 /* 376 ** Log collection information. 377 */ 378 379 # ifdef LOG 380 if (!QueueRun && LogLevel > 1) 381 { 382 char hbuf[100], *name = hbuf; 383 384 if (RealHostName == NULL) 385 name = "local"; 386 else if (RealHostName[0] == '[') 387 name = RealHostName; 388 else 389 (void)sprintf(hbuf, "%.90s (%s)", 390 RealHostName, inet_ntoa(RealHostAddr.sin_addr)); 391 syslog(LOG_INFO, 392 "%s: from=%s, size=%ld, class=%d, received from %s\n", 393 CurEnv->e_id, CurEnv->e_from.q_paddr, CurEnv->e_msgsize, 394 CurEnv->e_class, name); 395 } 396 # endif LOG 397 } 398 /* 399 ** PRIENCODE -- encode external priority names into internal values. 400 ** 401 ** Parameters: 402 ** p -- priority in ascii. 403 ** 404 ** Returns: 405 ** priority as a numeric level. 406 ** 407 ** Side Effects: 408 ** none. 409 */ 410 411 priencode(p) 412 char *p; 413 { 414 register int i; 415 416 for (i = 0; i < NumPriorities; i++) 417 { 418 if (!strcasecmp(p, Priorities[i].pri_name)) 419 return (Priorities[i].pri_val); 420 } 421 422 /* unknown priority */ 423 return (0); 424 } 425 /* 426 ** CRACKADDR -- parse an address and turn it into a macro 427 ** 428 ** This doesn't actually parse the address -- it just extracts 429 ** it and replaces it with "$g". The parse is totally ad hoc 430 ** and isn't even guaranteed to leave something syntactically 431 ** identical to what it started with. However, it does leave 432 ** something semantically identical. 433 ** 434 ** This algorithm has been cleaned up to handle a wider range 435 ** of cases -- notably quoted and backslash escaped strings. 436 ** This modification makes it substantially better at preserving 437 ** the original syntax. 438 ** 439 ** Parameters: 440 ** addr -- the address to be cracked. 441 ** 442 ** Returns: 443 ** a pointer to the new version. 444 ** 445 ** Side Effects: 446 ** none. 447 ** 448 ** Warning: 449 ** The return value is saved in local storage and should 450 ** be copied if it is to be reused. 451 */ 452 453 char * 454 crackaddr(addr) 455 register char *addr; 456 { 457 register char *p; 458 register char c; 459 int cmtlev; 460 int copylev; 461 bool qmode; 462 bool putgmac = FALSE; 463 register char *bp; 464 static char buf[MAXNAME]; 465 466 if (tTd(33, 1)) 467 printf("crackaddr(%s)\n", addr); 468 469 /* strip leading spaces */ 470 while (*addr != '\0' && isspace(*addr)) 471 addr++; 472 473 /* 474 ** Start by assuming we have no angle brackets. This will be 475 ** adjusted later if we find them. 476 */ 477 478 bp = buf; 479 p = addr; 480 copylev = cmtlev = 0; 481 qmode = FALSE; 482 483 while ((c = *p++) != '\0') 484 { 485 if (copylev > 0 || c == ' ') 486 *bp++ = c; 487 488 /* check for backslash escapes */ 489 if (c == '\\') 490 { 491 if ((c = *p++) == '\0') 492 { 493 /* too far */ 494 p--; 495 goto putg; 496 } 497 if (copylev > 0) 498 *bp++ = c; 499 goto putg; 500 } 501 502 /* check for quoted strings */ 503 if (c == '"') 504 { 505 qmode = !qmode; 506 continue; 507 } 508 if (qmode) 509 goto putg; 510 511 /* check for comments */ 512 if (c == '(') 513 { 514 cmtlev++; 515 if (copylev++ <= 0) 516 *bp++ = c; 517 } 518 if (cmtlev > 0) 519 { 520 if (c == ')') 521 { 522 cmtlev--; 523 copylev--; 524 } 525 continue; 526 } 527 528 /* check for angle brackets */ 529 if (c == '<') 530 { 531 /* oops -- have to change our mind */ 532 bcopy(addr, buf, p - addr); 533 bp = &buf[p - addr]; 534 copylev = 0; 535 putgmac = FALSE; 536 continue; 537 } 538 539 if (c == '>') 540 { 541 if (copylev++ <= 0) 542 *bp++ = c; 543 continue; 544 } 545 546 /* must be a real address character */ 547 putg: 548 if (copylev <= 0 && !putgmac) 549 { 550 *bp++ = '\001'; 551 *bp++ = 'g'; 552 putgmac = TRUE; 553 } 554 } 555 556 *bp++ = '\0'; 557 558 if (tTd(33, 1)) 559 printf("crackaddr=>`%s'\n", buf); 560 561 return (buf); 562 } 563 /* 564 ** PUTHEADER -- put the header part of a message from the in-core copy 565 ** 566 ** Parameters: 567 ** fp -- file to put it on. 568 ** m -- mailer to use. 569 ** e -- envelope to use. 570 ** 571 ** Returns: 572 ** none. 573 ** 574 ** Side Effects: 575 ** none. 576 */ 577 578 putheader(fp, m, e) 579 register FILE *fp; 580 register MAILER *m; 581 register ENVELOPE *e; 582 { 583 char buf[MAX(MAXFIELD,BUFSIZ)]; 584 register HDR *h; 585 extern char *arpadate(); 586 extern char *capitalize(); 587 char obuf[MAX(MAXFIELD,MAXLINE)]; 588 589 for (h = e->e_header; h != NULL; h = h->h_link) 590 { 591 register char *p; 592 extern bool bitintersect(); 593 594 if (bitset(H_CHECK|H_ACHECK, h->h_flags) && 595 !bitintersect(h->h_mflags, m->m_flags)) 596 continue; 597 598 /* handle Resent-... headers specially */ 599 if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags)) 600 continue; 601 602 p = h->h_value; 603 if (bitset(H_DEFAULT, h->h_flags)) 604 { 605 /* macro expand value if generated internally */ 606 expand(p, buf, &buf[sizeof buf], e); 607 p = buf; 608 if (p == NULL || *p == '\0') 609 continue; 610 } 611 612 if (bitset(H_FROM|H_RCPT, h->h_flags)) 613 { 614 /* address field */ 615 bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags); 616 617 if (bitset(H_FROM, h->h_flags)) 618 oldstyle = FALSE; 619 commaize(h, p, fp, oldstyle, m); 620 } 621 else 622 { 623 /* vanilla header line */ 624 register char *nlp; 625 626 (void) sprintf(obuf, "%s: ", capitalize(h->h_field)); 627 while ((nlp = index(p, '\n')) != NULL) 628 { 629 *nlp = '\0'; 630 (void) strcat(obuf, p); 631 *nlp = '\n'; 632 putline(obuf, fp, m); 633 p = ++nlp; 634 obuf[0] = '\0'; 635 } 636 (void) strcat(obuf, p); 637 putline(obuf, fp, m); 638 } 639 } 640 } 641 /* 642 ** COMMAIZE -- output a header field, making a comma-translated list. 643 ** 644 ** Parameters: 645 ** h -- the header field to output. 646 ** p -- the value to put in it. 647 ** fp -- file to put it to. 648 ** oldstyle -- TRUE if this is an old style header. 649 ** m -- a pointer to the mailer descriptor. If NULL, 650 ** don't transform the name at all. 651 ** 652 ** Returns: 653 ** none. 654 ** 655 ** Side Effects: 656 ** outputs "p" to file "fp". 657 */ 658 659 commaize(h, p, fp, oldstyle, m) 660 register HDR *h; 661 register char *p; 662 FILE *fp; 663 bool oldstyle; 664 register MAILER *m; 665 { 666 register char *obp; 667 int opos; 668 bool firstone = TRUE; 669 char obuf[MAXLINE + 3]; 670 671 /* 672 ** Output the address list translated by the 673 ** mailer and with commas. 674 */ 675 676 if (tTd(14, 2)) 677 printf("commaize(%s: %s)\n", h->h_field, p); 678 679 obp = obuf; 680 (void) sprintf(obp, "%s: ", capitalize(h->h_field)); 681 opos = strlen(h->h_field) + 2; 682 obp += opos; 683 684 /* 685 ** Run through the list of values. 686 */ 687 688 while (*p != '\0') 689 { 690 register char *name; 691 char savechar; 692 extern char *remotename(); 693 extern char *DelimChar; /* defined in prescan */ 694 695 /* 696 ** Find the end of the name. New style names 697 ** end with a comma, old style names end with 698 ** a space character. However, spaces do not 699 ** necessarily delimit an old-style name -- at 700 ** signs mean keep going. 701 */ 702 703 /* find end of name */ 704 while (isspace(*p) || *p == ',') 705 p++; 706 name = p; 707 for (;;) 708 { 709 char *oldp; 710 char pvpbuf[PSBUFSIZE]; 711 extern bool isatword(); 712 extern char **prescan(); 713 714 (void) prescan(p, oldstyle ? ' ' : ',', pvpbuf); 715 p = DelimChar; 716 717 /* look to see if we have an at sign */ 718 oldp = p; 719 while (*p != '\0' && isspace(*p)) 720 p++; 721 722 if (*p != '@' && !isatword(p)) 723 { 724 p = oldp; 725 break; 726 } 727 p += *p == '@' ? 1 : 2; 728 while (*p != '\0' && isspace(*p)) 729 p++; 730 } 731 /* at the end of one complete name */ 732 733 /* strip off trailing white space */ 734 while (p >= name && (isspace(*p) || *p == ',' || *p == '\0')) 735 p--; 736 if (++p == name) 737 continue; 738 savechar = *p; 739 *p = '\0'; 740 741 /* translate the name to be relative */ 742 name = remotename(name, m, bitset(H_FROM, h->h_flags), FALSE); 743 if (*name == '\0') 744 { 745 *p = savechar; 746 continue; 747 } 748 749 /* output the name with nice formatting */ 750 opos += qstrlen(name); 751 if (!firstone) 752 opos += 2; 753 if (opos > 78 && !firstone) 754 { 755 (void) strcpy(obp, ",\n"); 756 putline(obuf, fp, m); 757 obp = obuf; 758 (void) sprintf(obp, " "); 759 opos = strlen(obp); 760 obp += opos; 761 opos += qstrlen(name); 762 } 763 else if (!firstone) 764 { 765 (void) sprintf(obp, ", "); 766 obp += 2; 767 } 768 769 /* strip off quote bits as we output */ 770 while (*name != '\0' && obp < &obuf[MAXLINE]) 771 { 772 if (bitset(0200, *name)) 773 *obp++ = '\\'; 774 *obp++ = *name++ & ~0200; 775 } 776 firstone = FALSE; 777 *p = savechar; 778 } 779 (void) strcpy(obp, "\n"); 780 putline(obuf, fp, m); 781 } 782 /* 783 ** ISATWORD -- tell if the word we are pointing to is "at". 784 ** 785 ** Parameters: 786 ** p -- word to check. 787 ** 788 ** Returns: 789 ** TRUE -- if p is the word at. 790 ** FALSE -- otherwise. 791 ** 792 ** Side Effects: 793 ** none. 794 */ 795 796 bool 797 isatword(p) 798 register char *p; 799 { 800 extern char lower(); 801 802 if (lower(p[0]) == 'a' && lower(p[1]) == 't' && 803 p[2] != '\0' && isspace(p[2])) 804 return (TRUE); 805 return (FALSE); 806 } 807