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