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