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