1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #ifndef lint 10 static char sccsid[] = "@(#)readcf.c 8.19 (Berkeley) 01/22/94"; 11 #endif /* not lint */ 12 13 # include "sendmail.h" 14 # include <pwd.h> 15 # include <grp.h> 16 #ifdef NAMED_BIND 17 # include <arpa/nameser.h> 18 # include <resolv.h> 19 #endif 20 21 /* 22 ** READCF -- read control file. 23 ** 24 ** This routine reads the control file and builds the internal 25 ** form. 26 ** 27 ** The file is formatted as a sequence of lines, each taken 28 ** atomically. The first character of each line describes how 29 ** the line is to be interpreted. The lines are: 30 ** Dxval Define macro x to have value val. 31 ** Cxword Put word into class x. 32 ** Fxfile [fmt] Read file for lines to put into 33 ** class x. Use scanf string 'fmt' 34 ** or "%s" if not present. Fmt should 35 ** only produce one string-valued result. 36 ** Hname: value Define header with field-name 'name' 37 ** and value as specified; this will be 38 ** macro expanded immediately before 39 ** use. 40 ** Sn Use rewriting set n. 41 ** Rlhs rhs Rewrite addresses that match lhs to 42 ** be rhs. 43 ** Mn arg=val... Define mailer. n is the internal name. 44 ** Args specify mailer parameters. 45 ** Oxvalue Set option x to value. 46 ** Pname=value Set precedence name to value. 47 ** Vversioncode[/vendorcode] 48 ** Version level/vendor name of 49 ** configuration syntax. 50 ** Kmapname mapclass arguments.... 51 ** Define keyed lookup of a given class. 52 ** Arguments are class dependent. 53 ** 54 ** Parameters: 55 ** cfname -- control file name. 56 ** safe -- TRUE if this is the system config file; 57 ** FALSE otherwise. 58 ** e -- the main envelope. 59 ** 60 ** Returns: 61 ** none. 62 ** 63 ** Side Effects: 64 ** Builds several internal tables. 65 */ 66 67 readcf(cfname, safe, e) 68 char *cfname; 69 bool safe; 70 register ENVELOPE *e; 71 { 72 FILE *cf; 73 int ruleset = 0; 74 char *q; 75 struct rewrite *rwp = NULL; 76 char *bp; 77 auto char *ep; 78 int nfuzzy; 79 char *file; 80 bool optional; 81 char buf[MAXLINE]; 82 register char *p; 83 extern char **copyplist(); 84 struct stat statb; 85 char exbuf[MAXLINE]; 86 char pvpbuf[MAXLINE + MAXATOM]; 87 extern char *munchstring(); 88 extern void makemapentry(); 89 90 FileName = cfname; 91 LineNumber = 0; 92 93 cf = fopen(cfname, "r"); 94 if (cf == NULL) 95 { 96 syserr("cannot open"); 97 exit(EX_OSFILE); 98 } 99 100 if (fstat(fileno(cf), &statb) < 0) 101 { 102 syserr("cannot fstat"); 103 exit(EX_OSFILE); 104 } 105 106 if (!S_ISREG(statb.st_mode)) 107 { 108 syserr("not a plain file"); 109 exit(EX_OSFILE); 110 } 111 112 if (OpMode != MD_TEST && bitset(S_IWGRP|S_IWOTH, statb.st_mode)) 113 { 114 if (OpMode == MD_DAEMON || OpMode == MD_FREEZE) 115 fprintf(stderr, "%s: WARNING: dangerous write permissions\n", 116 FileName); 117 #ifdef LOG 118 if (LogLevel > 0) 119 syslog(LOG_CRIT, "%s: WARNING: dangerous write permissions", 120 FileName); 121 #endif 122 } 123 124 #ifdef XLA 125 xla_zero(); 126 #endif 127 128 while ((bp = fgetfolded(buf, sizeof buf, cf)) != NULL) 129 { 130 if (bp[0] == '#') 131 { 132 if (bp != buf) 133 free(bp); 134 continue; 135 } 136 137 /* map $ into \201 for macro expansion */ 138 for (p = bp; *p != '\0'; p++) 139 { 140 if (*p == '#' && p > bp && ConfigLevel >= 3) 141 { 142 /* this is an on-line comment */ 143 register char *e; 144 145 switch (*--p & 0377) 146 { 147 case MACROEXPAND: 148 /* it's from $# -- let it go through */ 149 p++; 150 break; 151 152 case '\\': 153 /* it's backslash escaped */ 154 (void) strcpy(p, p + 1); 155 break; 156 157 default: 158 /* delete preceeding white space */ 159 while (isascii(*p) && isspace(*p) && p > bp) 160 p--; 161 if ((e = strchr(++p, '\n')) != NULL) 162 (void) strcpy(p, e); 163 else 164 p[0] = p[1] = '\0'; 165 break; 166 } 167 continue; 168 } 169 170 if (*p != '$') 171 continue; 172 173 if (p[1] == '$') 174 { 175 /* actual dollar sign.... */ 176 (void) strcpy(p, p + 1); 177 continue; 178 } 179 180 /* convert to macro expansion character */ 181 *p = MACROEXPAND; 182 } 183 184 /* interpret this line */ 185 errno = 0; 186 switch (bp[0]) 187 { 188 case '\0': 189 case '#': /* comment */ 190 break; 191 192 case 'R': /* rewriting rule */ 193 for (p = &bp[1]; *p != '\0' && *p != '\t'; p++) 194 continue; 195 196 if (*p == '\0') 197 { 198 syserr("invalid rewrite line \"%s\" (tab expected)", bp); 199 break; 200 } 201 202 /* allocate space for the rule header */ 203 if (rwp == NULL) 204 { 205 RewriteRules[ruleset] = rwp = 206 (struct rewrite *) xalloc(sizeof *rwp); 207 } 208 else 209 { 210 rwp->r_next = (struct rewrite *) xalloc(sizeof *rwp); 211 rwp = rwp->r_next; 212 } 213 rwp->r_next = NULL; 214 215 /* expand and save the LHS */ 216 *p = '\0'; 217 expand(&bp[1], exbuf, &exbuf[sizeof exbuf], e); 218 rwp->r_lhs = prescan(exbuf, '\t', pvpbuf, 219 sizeof pvpbuf, NULL); 220 nfuzzy = 0; 221 if (rwp->r_lhs != NULL) 222 { 223 register char **ap; 224 225 rwp->r_lhs = copyplist(rwp->r_lhs, TRUE); 226 227 /* count the number of fuzzy matches in LHS */ 228 for (ap = rwp->r_lhs; *ap != NULL; ap++) 229 { 230 char *botch; 231 232 botch = NULL; 233 switch (**ap & 0377) 234 { 235 case MATCHZANY: 236 case MATCHANY: 237 case MATCHONE: 238 case MATCHCLASS: 239 case MATCHNCLASS: 240 nfuzzy++; 241 break; 242 243 case MATCHREPL: 244 botch = "$0-$9"; 245 break; 246 247 case CANONNET: 248 botch = "$#"; 249 break; 250 251 case CANONUSER: 252 botch = "$:"; 253 break; 254 255 case CALLSUBR: 256 botch = "$>"; 257 break; 258 259 case CONDIF: 260 botch = "$?"; 261 break; 262 263 case CONDELSE: 264 botch = "$|"; 265 break; 266 267 case CONDFI: 268 botch = "$."; 269 break; 270 271 case HOSTBEGIN: 272 botch = "$["; 273 break; 274 275 case HOSTEND: 276 botch = "$]"; 277 break; 278 279 case LOOKUPBEGIN: 280 botch = "$("; 281 break; 282 283 case LOOKUPEND: 284 botch = "$)"; 285 break; 286 } 287 if (botch != NULL) 288 syserr("Inappropriate use of %s on LHS", 289 botch); 290 } 291 } 292 else 293 syserr("R line: null LHS"); 294 295 /* expand and save the RHS */ 296 while (*++p == '\t') 297 continue; 298 q = p; 299 while (*p != '\0' && *p != '\t') 300 p++; 301 *p = '\0'; 302 expand(q, exbuf, &exbuf[sizeof exbuf], e); 303 rwp->r_rhs = prescan(exbuf, '\t', pvpbuf, 304 sizeof pvpbuf, NULL); 305 if (rwp->r_rhs != NULL) 306 { 307 register char **ap; 308 309 rwp->r_rhs = copyplist(rwp->r_rhs, TRUE); 310 311 /* check no out-of-bounds replacements */ 312 nfuzzy += '0'; 313 for (ap = rwp->r_rhs; *ap != NULL; ap++) 314 { 315 char *botch; 316 317 botch = NULL; 318 switch (**ap & 0377) 319 { 320 case MATCHREPL: 321 if ((*ap)[1] <= '0' || (*ap)[1] > nfuzzy) 322 { 323 syserr("replacement $%c out of bounds", 324 (*ap)[1]); 325 } 326 break; 327 328 case MATCHZANY: 329 botch = "$*"; 330 break; 331 332 case MATCHANY: 333 botch = "$+"; 334 break; 335 336 case MATCHONE: 337 botch = "$-"; 338 break; 339 340 case MATCHCLASS: 341 botch = "$="; 342 break; 343 344 case MATCHNCLASS: 345 botch = "$~"; 346 break; 347 } 348 if (botch != NULL) 349 syserr("Inappropriate use of %s on RHS", 350 botch); 351 } 352 } 353 else 354 syserr("R line: null RHS"); 355 break; 356 357 case 'S': /* select rewriting set */ 358 for (p = &bp[1]; isascii(*p) && isspace(*p); p++) 359 continue; 360 if (!isascii(*p) || !isdigit(*p)) 361 { 362 syserr("invalid argument to S line: \"%.20s\"", 363 &bp[1]); 364 break; 365 } 366 ruleset = atoi(p); 367 if (ruleset >= MAXRWSETS || ruleset < 0) 368 { 369 syserr("bad ruleset %d (%d max)", ruleset, MAXRWSETS); 370 ruleset = 0; 371 } 372 rwp = NULL; 373 break; 374 375 case 'D': /* macro definition */ 376 p = munchstring(&bp[2], NULL); 377 define(bp[1], newstr(p), e); 378 break; 379 380 case 'H': /* required header line */ 381 (void) chompheader(&bp[1], TRUE, e); 382 break; 383 384 case 'C': /* word class */ 385 /* scan the list of words and set class for all */ 386 expand(&bp[2], exbuf, &exbuf[sizeof exbuf], e); 387 for (p = exbuf; *p != '\0'; ) 388 { 389 register char *wd; 390 char delim; 391 392 while (*p != '\0' && isascii(*p) && isspace(*p)) 393 p++; 394 wd = p; 395 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 396 p++; 397 delim = *p; 398 *p = '\0'; 399 if (wd[0] != '\0') 400 setclass(bp[1], wd); 401 *p = delim; 402 } 403 break; 404 405 case 'F': /* word class from file */ 406 for (p = &bp[2]; isascii(*p) && isspace(*p); ) 407 p++; 408 if (p[0] == '-' && p[1] == 'o') 409 { 410 optional = TRUE; 411 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 412 p++; 413 while (isascii(*p) && isspace(*p)) 414 *p++; 415 } 416 else 417 optional = FALSE; 418 file = p; 419 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 420 p++; 421 if (*p == '\0') 422 p = "%s"; 423 else 424 { 425 *p = '\0'; 426 while (isascii(*++p) && isspace(*p)) 427 continue; 428 } 429 fileclass(bp[1], file, p, safe, optional); 430 break; 431 432 #ifdef XLA 433 case 'L': /* extended load average description */ 434 xla_init(&bp[1]); 435 break; 436 #endif 437 438 case 'M': /* define mailer */ 439 makemailer(&bp[1]); 440 break; 441 442 case 'O': /* set option */ 443 setoption(bp[1], &bp[2], safe, FALSE, e); 444 break; 445 446 case 'P': /* set precedence */ 447 if (NumPriorities >= MAXPRIORITIES) 448 { 449 toomany('P', MAXPRIORITIES); 450 break; 451 } 452 for (p = &bp[1]; *p != '\0' && *p != '=' && *p != '\t'; p++) 453 continue; 454 if (*p == '\0') 455 goto badline; 456 *p = '\0'; 457 Priorities[NumPriorities].pri_name = newstr(&bp[1]); 458 Priorities[NumPriorities].pri_val = atoi(++p); 459 NumPriorities++; 460 break; 461 462 case 'T': /* trusted user(s) */ 463 /* this option is obsolete, but will be ignored */ 464 break; 465 466 case 'V': /* configuration syntax version */ 467 for (p = &bp[1]; isascii(*p) && isspace(*p); p++) 468 continue; 469 if (!isascii(*p) || !isdigit(*p)) 470 { 471 syserr("invalid argument to V line: \"%.20s\"", 472 &bp[1]); 473 break; 474 } 475 ConfigLevel = strtol(p, &ep, 10); 476 if (ConfigLevel >= 5) 477 { 478 /* level 5 configs have short name in $w */ 479 p = macvalue('w', e); 480 if (p != NULL && (p = strchr(p, '.')) != NULL) 481 *p = '\0'; 482 } 483 if (*ep++ == '/') 484 { 485 /* extract vendor code */ 486 for (p = ep; isascii(*p) && isalpha(*p); ) 487 p++; 488 *p = '\0'; 489 490 if (!setvendor(ep)) 491 syserr("invalid V line vendor code: \"%s\"", 492 ep); 493 } 494 break; 495 496 case 'K': 497 makemapentry(&bp[1]); 498 break; 499 500 default: 501 badline: 502 syserr("unknown control line \"%s\"", bp); 503 } 504 if (bp != buf) 505 free(bp); 506 } 507 if (ferror(cf)) 508 { 509 syserr("I/O read error", cfname); 510 exit(EX_OSFILE); 511 } 512 fclose(cf); 513 FileName = NULL; 514 515 if (stab("host", ST_MAP, ST_FIND) == NULL) 516 { 517 /* user didn't initialize: set up host map */ 518 strcpy(buf, "host host"); 519 #ifdef NAMED_BIND 520 if (ConfigLevel >= 2) 521 strcat(buf, " -a."); 522 #endif 523 makemapentry(buf); 524 } 525 } 526 /* 527 ** TOOMANY -- signal too many of some option 528 ** 529 ** Parameters: 530 ** id -- the id of the error line 531 ** maxcnt -- the maximum possible values 532 ** 533 ** Returns: 534 ** none. 535 ** 536 ** Side Effects: 537 ** gives a syserr. 538 */ 539 540 toomany(id, maxcnt) 541 char id; 542 int maxcnt; 543 { 544 syserr("too many %c lines, %d max", id, maxcnt); 545 } 546 /* 547 ** FILECLASS -- read members of a class from a file 548 ** 549 ** Parameters: 550 ** class -- class to define. 551 ** filename -- name of file to read. 552 ** fmt -- scanf string to use for match. 553 ** safe -- if set, this is a safe read. 554 ** optional -- if set, it is not an error for the file to 555 ** not exist. 556 ** 557 ** Returns: 558 ** none 559 ** 560 ** Side Effects: 561 ** 562 ** puts all lines in filename that match a scanf into 563 ** the named class. 564 */ 565 566 fileclass(class, filename, fmt, safe, optional) 567 int class; 568 char *filename; 569 char *fmt; 570 bool safe; 571 bool optional; 572 { 573 FILE *f; 574 struct stat stbuf; 575 char buf[MAXLINE]; 576 577 if (stat(filename, &stbuf) < 0) 578 { 579 if (!optional) 580 syserr("fileclass: cannot stat %s", filename); 581 return; 582 } 583 if (!S_ISREG(stbuf.st_mode)) 584 { 585 syserr("fileclass: %s not a regular file", filename); 586 return; 587 } 588 if (!safe && access(filename, R_OK) < 0) 589 { 590 syserr("fileclass: access denied on %s", filename); 591 return; 592 } 593 f = fopen(filename, "r"); 594 if (f == NULL) 595 { 596 syserr("fileclass: cannot open %s", filename); 597 return; 598 } 599 600 while (fgets(buf, sizeof buf, f) != NULL) 601 { 602 register STAB *s; 603 register char *p; 604 # ifdef SCANF 605 char wordbuf[MAXNAME+1]; 606 607 if (sscanf(buf, fmt, wordbuf) != 1) 608 continue; 609 p = wordbuf; 610 # else /* SCANF */ 611 p = buf; 612 # endif /* SCANF */ 613 614 /* 615 ** Break up the match into words. 616 */ 617 618 while (*p != '\0') 619 { 620 register char *q; 621 622 /* strip leading spaces */ 623 while (isascii(*p) && isspace(*p)) 624 p++; 625 if (*p == '\0') 626 break; 627 628 /* find the end of the word */ 629 q = p; 630 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 631 p++; 632 if (*p != '\0') 633 *p++ = '\0'; 634 635 /* enter the word in the symbol table */ 636 s = stab(q, ST_CLASS, ST_ENTER); 637 setbitn(class, s->s_class); 638 } 639 } 640 641 (void) fclose(f); 642 } 643 /* 644 ** MAKEMAILER -- define a new mailer. 645 ** 646 ** Parameters: 647 ** line -- description of mailer. This is in labeled 648 ** fields. The fields are: 649 ** P -- the path to the mailer 650 ** F -- the flags associated with the mailer 651 ** A -- the argv for this mailer 652 ** S -- the sender rewriting set 653 ** R -- the recipient rewriting set 654 ** E -- the eol string 655 ** The first word is the canonical name of the mailer. 656 ** 657 ** Returns: 658 ** none. 659 ** 660 ** Side Effects: 661 ** enters the mailer into the mailer table. 662 */ 663 664 makemailer(line) 665 char *line; 666 { 667 register char *p; 668 register struct mailer *m; 669 register STAB *s; 670 int i; 671 char fcode; 672 auto char *endp; 673 extern int NextMailer; 674 extern char **makeargv(); 675 extern char *munchstring(); 676 extern long atol(); 677 678 /* allocate a mailer and set up defaults */ 679 m = (struct mailer *) xalloc(sizeof *m); 680 bzero((char *) m, sizeof *m); 681 m->m_eol = "\n"; 682 683 /* collect the mailer name */ 684 for (p = line; *p != '\0' && *p != ',' && !(isascii(*p) && isspace(*p)); p++) 685 continue; 686 if (*p != '\0') 687 *p++ = '\0'; 688 m->m_name = newstr(line); 689 690 /* now scan through and assign info from the fields */ 691 while (*p != '\0') 692 { 693 auto char *delimptr; 694 695 while (*p != '\0' && (*p == ',' || (isascii(*p) && isspace(*p)))) 696 p++; 697 698 /* p now points to field code */ 699 fcode = *p; 700 while (*p != '\0' && *p != '=' && *p != ',') 701 p++; 702 if (*p++ != '=') 703 { 704 syserr("mailer %s: `=' expected", m->m_name); 705 return; 706 } 707 while (isascii(*p) && isspace(*p)) 708 p++; 709 710 /* p now points to the field body */ 711 p = munchstring(p, &delimptr); 712 713 /* install the field into the mailer struct */ 714 switch (fcode) 715 { 716 case 'P': /* pathname */ 717 m->m_mailer = newstr(p); 718 break; 719 720 case 'F': /* flags */ 721 for (; *p != '\0'; p++) 722 if (!(isascii(*p) && isspace(*p))) 723 setbitn(*p, m->m_flags); 724 break; 725 726 case 'S': /* sender rewriting ruleset */ 727 case 'R': /* recipient rewriting ruleset */ 728 i = strtol(p, &endp, 10); 729 if (i < 0 || i >= MAXRWSETS) 730 { 731 syserr("invalid rewrite set, %d max", MAXRWSETS); 732 return; 733 } 734 if (fcode == 'S') 735 m->m_sh_rwset = m->m_se_rwset = i; 736 else 737 m->m_rh_rwset = m->m_re_rwset = i; 738 739 p = endp; 740 if (*p++ == '/') 741 { 742 i = strtol(p, NULL, 10); 743 if (i < 0 || i >= MAXRWSETS) 744 { 745 syserr("invalid rewrite set, %d max", 746 MAXRWSETS); 747 return; 748 } 749 if (fcode == 'S') 750 m->m_sh_rwset = i; 751 else 752 m->m_rh_rwset = i; 753 } 754 break; 755 756 case 'E': /* end of line string */ 757 m->m_eol = newstr(p); 758 break; 759 760 case 'A': /* argument vector */ 761 m->m_argv = makeargv(p); 762 break; 763 764 case 'M': /* maximum message size */ 765 m->m_maxsize = atol(p); 766 break; 767 768 case 'L': /* maximum line length */ 769 m->m_linelimit = atoi(p); 770 break; 771 772 case 'D': /* working directory */ 773 m->m_execdir = newstr(p); 774 break; 775 } 776 777 p = delimptr; 778 } 779 780 /* do some heuristic cleanup for back compatibility */ 781 if (bitnset(M_LIMITS, m->m_flags)) 782 { 783 if (m->m_linelimit == 0) 784 m->m_linelimit = SMTPLINELIM; 785 if (ConfigLevel < 2) 786 setbitn(M_7BITS, m->m_flags); 787 } 788 789 /* do some rationality checking */ 790 if (m->m_argv == NULL) 791 { 792 syserr("M%s: A= argument required", m->m_name); 793 return; 794 } 795 if (m->m_mailer == NULL) 796 { 797 syserr("M%s: P= argument required", m->m_name); 798 return; 799 } 800 801 if (NextMailer >= MAXMAILERS) 802 { 803 syserr("too many mailers defined (%d max)", MAXMAILERS); 804 return; 805 } 806 807 s = stab(m->m_name, ST_MAILER, ST_ENTER); 808 if (s->s_mailer != NULL) 809 { 810 i = s->s_mailer->m_mno; 811 free(s->s_mailer); 812 } 813 else 814 { 815 i = NextMailer++; 816 } 817 Mailer[i] = s->s_mailer = m; 818 m->m_mno = i; 819 } 820 /* 821 ** MUNCHSTRING -- translate a string into internal form. 822 ** 823 ** Parameters: 824 ** p -- the string to munch. 825 ** delimptr -- if non-NULL, set to the pointer of the 826 ** field delimiter character. 827 ** 828 ** Returns: 829 ** the munched string. 830 */ 831 832 char * 833 munchstring(p, delimptr) 834 register char *p; 835 char **delimptr; 836 { 837 register char *q; 838 bool backslash = FALSE; 839 bool quotemode = FALSE; 840 static char buf[MAXLINE]; 841 842 for (q = buf; *p != '\0'; p++) 843 { 844 if (backslash) 845 { 846 /* everything is roughly literal */ 847 backslash = FALSE; 848 switch (*p) 849 { 850 case 'r': /* carriage return */ 851 *q++ = '\r'; 852 continue; 853 854 case 'n': /* newline */ 855 *q++ = '\n'; 856 continue; 857 858 case 'f': /* form feed */ 859 *q++ = '\f'; 860 continue; 861 862 case 'b': /* backspace */ 863 *q++ = '\b'; 864 continue; 865 } 866 *q++ = *p; 867 } 868 else 869 { 870 if (*p == '\\') 871 backslash = TRUE; 872 else if (*p == '"') 873 quotemode = !quotemode; 874 else if (quotemode || *p != ',') 875 *q++ = *p; 876 else 877 break; 878 } 879 } 880 881 if (delimptr != NULL) 882 *delimptr = p; 883 *q++ = '\0'; 884 return (buf); 885 } 886 /* 887 ** MAKEARGV -- break up a string into words 888 ** 889 ** Parameters: 890 ** p -- the string to break up. 891 ** 892 ** Returns: 893 ** a char **argv (dynamically allocated) 894 ** 895 ** Side Effects: 896 ** munges p. 897 */ 898 899 char ** 900 makeargv(p) 901 register char *p; 902 { 903 char *q; 904 int i; 905 char **avp; 906 char *argv[MAXPV + 1]; 907 908 /* take apart the words */ 909 i = 0; 910 while (*p != '\0' && i < MAXPV) 911 { 912 q = p; 913 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 914 p++; 915 while (isascii(*p) && isspace(*p)) 916 *p++ = '\0'; 917 argv[i++] = newstr(q); 918 } 919 argv[i++] = NULL; 920 921 /* now make a copy of the argv */ 922 avp = (char **) xalloc(sizeof *avp * i); 923 bcopy((char *) argv, (char *) avp, sizeof *avp * i); 924 925 return (avp); 926 } 927 /* 928 ** PRINTRULES -- print rewrite rules (for debugging) 929 ** 930 ** Parameters: 931 ** none. 932 ** 933 ** Returns: 934 ** none. 935 ** 936 ** Side Effects: 937 ** prints rewrite rules. 938 */ 939 940 printrules() 941 { 942 register struct rewrite *rwp; 943 register int ruleset; 944 945 for (ruleset = 0; ruleset < 10; ruleset++) 946 { 947 if (RewriteRules[ruleset] == NULL) 948 continue; 949 printf("\n----Rule Set %d:", ruleset); 950 951 for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next) 952 { 953 printf("\nLHS:"); 954 printav(rwp->r_lhs); 955 printf("RHS:"); 956 printav(rwp->r_rhs); 957 } 958 } 959 } 960 961 /* 962 ** SETOPTION -- set global processing option 963 ** 964 ** Parameters: 965 ** opt -- option name. 966 ** val -- option value (as a text string). 967 ** safe -- set if this came from a configuration file. 968 ** Some options (if set from the command line) will 969 ** reset the user id to avoid security problems. 970 ** sticky -- if set, don't let other setoptions override 971 ** this value. 972 ** e -- the main envelope. 973 ** 974 ** Returns: 975 ** none. 976 ** 977 ** Side Effects: 978 ** Sets options as implied by the arguments. 979 */ 980 981 static BITMAP StickyOpt; /* set if option is stuck */ 982 983 984 #ifdef NAMED_BIND 985 986 struct resolverflags 987 { 988 char *rf_name; /* name of the flag */ 989 long rf_bits; /* bits to set/clear */ 990 } ResolverFlags[] = 991 { 992 "debug", RES_DEBUG, 993 "aaonly", RES_AAONLY, 994 "usevc", RES_USEVC, 995 "primary", RES_PRIMARY, 996 "igntc", RES_IGNTC, 997 "recurse", RES_RECURSE, 998 "defnames", RES_DEFNAMES, 999 "stayopen", RES_STAYOPEN, 1000 "dnsrch", RES_DNSRCH, 1001 "true", 0, /* to avoid error on old syntax */ 1002 NULL, 0 1003 }; 1004 1005 #endif 1006 1007 setoption(opt, val, safe, sticky, e) 1008 char opt; 1009 char *val; 1010 bool safe; 1011 bool sticky; 1012 register ENVELOPE *e; 1013 { 1014 register char *p; 1015 extern bool atobool(); 1016 extern time_t convtime(); 1017 extern int QueueLA; 1018 extern int RefuseLA; 1019 extern bool Warn_Q_option; 1020 extern bool trusteduser(); 1021 1022 if (tTd(37, 1)) 1023 printf("setoption %c=%s", opt, val); 1024 1025 /* 1026 ** See if this option is preset for us. 1027 */ 1028 1029 if (!sticky && bitnset(opt, StickyOpt)) 1030 { 1031 if (tTd(37, 1)) 1032 printf(" (ignored)\n"); 1033 return; 1034 } 1035 1036 /* 1037 ** Check to see if this option can be specified by this user. 1038 */ 1039 1040 if (!safe && RealUid == 0) 1041 safe = TRUE; 1042 if (!safe && strchr("bCdeEijLmoprsvw7", opt) == NULL) 1043 { 1044 if (opt != 'M' || (val[0] != 'r' && val[0] != 's')) 1045 { 1046 if (tTd(37, 1)) 1047 printf(" (unsafe)"); 1048 if (RealUid != geteuid()) 1049 { 1050 if (tTd(37, 1)) 1051 printf("(Resetting uid)"); 1052 (void) setgid(RealGid); 1053 (void) setuid(RealUid); 1054 } 1055 } 1056 } 1057 if (tTd(37, 1)) 1058 printf("\n"); 1059 1060 switch (opt) 1061 { 1062 case '7': /* force seven-bit input */ 1063 SevenBit = atobool(val); 1064 break; 1065 1066 case 'A': /* set default alias file */ 1067 if (val[0] == '\0') 1068 setalias("aliases"); 1069 else 1070 setalias(val); 1071 break; 1072 1073 case 'a': /* look N minutes for "@:@" in alias file */ 1074 if (val[0] == '\0') 1075 SafeAlias = 5 * 60; /* five minutes */ 1076 else 1077 SafeAlias = convtime(val, 'm'); 1078 break; 1079 1080 case 'B': /* substitution for blank character */ 1081 SpaceSub = val[0]; 1082 if (SpaceSub == '\0') 1083 SpaceSub = ' '; 1084 break; 1085 1086 case 'b': /* min blocks free on queue fs/max msg size */ 1087 p = strchr(val, '/'); 1088 if (p != NULL) 1089 { 1090 *p++ = '\0'; 1091 MaxMessageSize = atol(p); 1092 } 1093 MinBlocksFree = atol(val); 1094 break; 1095 1096 case 'c': /* don't connect to "expensive" mailers */ 1097 NoConnect = atobool(val); 1098 break; 1099 1100 case 'C': /* checkpoint every N addresses */ 1101 CheckpointInterval = atoi(val); 1102 break; 1103 1104 case 'd': /* delivery mode */ 1105 switch (*val) 1106 { 1107 case '\0': 1108 e->e_sendmode = SM_DELIVER; 1109 break; 1110 1111 case SM_QUEUE: /* queue only */ 1112 #ifndef QUEUE 1113 syserr("need QUEUE to set -odqueue"); 1114 #endif /* QUEUE */ 1115 /* fall through..... */ 1116 1117 case SM_DELIVER: /* do everything */ 1118 case SM_FORK: /* fork after verification */ 1119 e->e_sendmode = *val; 1120 break; 1121 1122 default: 1123 syserr("Unknown delivery mode %c", *val); 1124 exit(EX_USAGE); 1125 } 1126 break; 1127 1128 case 'D': /* rebuild alias database as needed */ 1129 AutoRebuild = atobool(val); 1130 break; 1131 1132 case 'E': /* error message header/header file */ 1133 if (*val != '\0') 1134 ErrMsgFile = newstr(val); 1135 break; 1136 1137 case 'e': /* set error processing mode */ 1138 switch (*val) 1139 { 1140 case EM_QUIET: /* be silent about it */ 1141 case EM_MAIL: /* mail back */ 1142 case EM_BERKNET: /* do berknet error processing */ 1143 case EM_WRITE: /* write back (or mail) */ 1144 HoldErrs = TRUE; 1145 /* fall through... */ 1146 1147 case EM_PRINT: /* print errors normally (default) */ 1148 e->e_errormode = *val; 1149 break; 1150 } 1151 break; 1152 1153 case 'F': /* file mode */ 1154 FileMode = atooct(val) & 0777; 1155 break; 1156 1157 case 'f': /* save Unix-style From lines on front */ 1158 SaveFrom = atobool(val); 1159 break; 1160 1161 case 'G': /* match recipients against GECOS field */ 1162 MatchGecos = atobool(val); 1163 break; 1164 1165 case 'g': /* default gid */ 1166 if (isascii(*val) && isdigit(*val)) 1167 DefGid = atoi(val); 1168 else 1169 { 1170 register struct group *gr; 1171 1172 DefGid = -1; 1173 gr = getgrnam(val); 1174 if (gr == NULL) 1175 syserr("readcf: option g: unknown group %s", val); 1176 else 1177 DefGid = gr->gr_gid; 1178 } 1179 break; 1180 1181 case 'H': /* help file */ 1182 if (val[0] == '\0') 1183 HelpFile = "sendmail.hf"; 1184 else 1185 HelpFile = newstr(val); 1186 break; 1187 1188 case 'h': /* maximum hop count */ 1189 MaxHopCount = atoi(val); 1190 break; 1191 1192 case 'I': /* use internet domain name server */ 1193 #ifdef NAMED_BIND 1194 UseNameServer = TRUE; 1195 for (p = val; *p != 0; ) 1196 { 1197 bool clearmode; 1198 char *q; 1199 struct resolverflags *rfp; 1200 1201 while (*p == ' ') 1202 p++; 1203 if (*p == '\0') 1204 break; 1205 clearmode = FALSE; 1206 if (*p == '-') 1207 clearmode = TRUE; 1208 else if (*p != '+') 1209 p--; 1210 p++; 1211 q = p; 1212 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 1213 p++; 1214 if (*p != '\0') 1215 *p++ = '\0'; 1216 for (rfp = ResolverFlags; rfp->rf_name != NULL; rfp++) 1217 { 1218 if (strcasecmp(q, rfp->rf_name) == 0) 1219 break; 1220 } 1221 if (rfp->rf_name == NULL) 1222 syserr("readcf: I option value %s unrecognized", q); 1223 else if (clearmode) 1224 _res.options &= ~rfp->rf_bits; 1225 else 1226 _res.options |= rfp->rf_bits; 1227 } 1228 if (tTd(8, 2)) 1229 printf("_res.options = %x\n", _res.options); 1230 #else 1231 usrerr("name server (I option) specified but BIND not compiled in"); 1232 #endif 1233 break; 1234 1235 case 'i': /* ignore dot lines in message */ 1236 IgnrDot = atobool(val); 1237 break; 1238 1239 case 'j': /* send errors in MIME (RFC 1341) format */ 1240 SendMIMEErrors = atobool(val); 1241 break; 1242 1243 case 'J': /* .forward search path */ 1244 ForwardPath = newstr(val); 1245 break; 1246 1247 case 'k': /* connection cache size */ 1248 MaxMciCache = atoi(val); 1249 if (MaxMciCache < 0) 1250 MaxMciCache = 0; 1251 break; 1252 1253 case 'K': /* connection cache timeout */ 1254 MciCacheTimeout = convtime(val, 'm'); 1255 break; 1256 1257 case 'l': /* use Errors-To: header */ 1258 UseErrorsTo = atobool(val); 1259 break; 1260 1261 case 'L': /* log level */ 1262 if (safe || LogLevel < atoi(val)) 1263 LogLevel = atoi(val); 1264 break; 1265 1266 case 'M': /* define macro */ 1267 define(val[0], newstr(&val[1]), CurEnv); 1268 sticky = FALSE; 1269 break; 1270 1271 case 'm': /* send to me too */ 1272 MeToo = atobool(val); 1273 break; 1274 1275 case 'n': /* validate RHS in newaliases */ 1276 CheckAliases = atobool(val); 1277 break; 1278 1279 /* 'N' available -- was "net name" */ 1280 1281 case 'O': /* daemon options */ 1282 setdaemonoptions(val); 1283 break; 1284 1285 case 'o': /* assume old style headers */ 1286 if (atobool(val)) 1287 CurEnv->e_flags |= EF_OLDSTYLE; 1288 else 1289 CurEnv->e_flags &= ~EF_OLDSTYLE; 1290 break; 1291 1292 case 'p': /* select privacy level */ 1293 p = val; 1294 for (;;) 1295 { 1296 register struct prival *pv; 1297 extern struct prival PrivacyValues[]; 1298 1299 while (isascii(*p) && (isspace(*p) || ispunct(*p))) 1300 p++; 1301 if (*p == '\0') 1302 break; 1303 val = p; 1304 while (isascii(*p) && isalnum(*p)) 1305 p++; 1306 if (*p != '\0') 1307 *p++ = '\0'; 1308 1309 for (pv = PrivacyValues; pv->pv_name != NULL; pv++) 1310 { 1311 if (strcasecmp(val, pv->pv_name) == 0) 1312 break; 1313 } 1314 if (pv->pv_name == NULL) 1315 syserr("readcf: Op line: %s unrecognized", val); 1316 PrivacyFlags |= pv->pv_flag; 1317 } 1318 break; 1319 1320 case 'P': /* postmaster copy address for returned mail */ 1321 PostMasterCopy = newstr(val); 1322 break; 1323 1324 case 'q': /* slope of queue only function */ 1325 QueueFactor = atoi(val); 1326 break; 1327 1328 case 'Q': /* queue directory */ 1329 if (val[0] == '\0') 1330 QueueDir = "mqueue"; 1331 else 1332 QueueDir = newstr(val); 1333 if (RealUid != 0 && !safe) 1334 Warn_Q_option = TRUE; 1335 break; 1336 1337 case 'R': /* don't prune routes */ 1338 DontPruneRoutes = atobool(val); 1339 break; 1340 1341 case 'r': /* read timeout */ 1342 settimeouts(val); 1343 break; 1344 1345 case 'S': /* status file */ 1346 if (val[0] == '\0') 1347 StatFile = "sendmail.st"; 1348 else 1349 StatFile = newstr(val); 1350 break; 1351 1352 case 's': /* be super safe, even if expensive */ 1353 SuperSafe = atobool(val); 1354 break; 1355 1356 case 'T': /* queue timeout */ 1357 p = strchr(val, '/'); 1358 if (p != NULL) 1359 { 1360 *p++ = '\0'; 1361 TimeOuts.to_q_warning = convtime(p, 'd'); 1362 } 1363 TimeOuts.to_q_return = convtime(val, 'h'); 1364 break; 1365 1366 case 't': /* time zone name */ 1367 TimeZoneSpec = newstr(val); 1368 break; 1369 1370 case 'U': /* location of user database */ 1371 UdbSpec = newstr(val); 1372 break; 1373 1374 case 'u': /* set default uid */ 1375 if (isascii(*val) && isdigit(*val)) 1376 DefUid = atoi(val); 1377 else 1378 { 1379 register struct passwd *pw; 1380 1381 DefUid = -1; 1382 pw = getpwnam(val); 1383 if (pw == NULL) 1384 syserr("readcf: option u: unknown user %s", val); 1385 else 1386 DefUid = pw->pw_uid; 1387 } 1388 setdefuser(); 1389 break; 1390 1391 case 'V': /* fallback MX host */ 1392 FallBackMX = newstr(val); 1393 break; 1394 1395 case 'v': /* run in verbose mode */ 1396 Verbose = atobool(val); 1397 break; 1398 1399 case 'w': /* if we are best MX, try host directly */ 1400 TryNullMXList = atobool(val); 1401 break; 1402 1403 /* 'W' available -- was wizard password */ 1404 1405 case 'x': /* load avg at which to auto-queue msgs */ 1406 QueueLA = atoi(val); 1407 break; 1408 1409 case 'X': /* load avg at which to auto-reject connections */ 1410 RefuseLA = atoi(val); 1411 break; 1412 1413 case 'y': /* work recipient factor */ 1414 WkRecipFact = atoi(val); 1415 break; 1416 1417 case 'Y': /* fork jobs during queue runs */ 1418 ForkQueueRuns = atobool(val); 1419 break; 1420 1421 case 'z': /* work message class factor */ 1422 WkClassFact = atoi(val); 1423 break; 1424 1425 case 'Z': /* work time factor */ 1426 WkTimeFact = atoi(val); 1427 break; 1428 1429 default: 1430 break; 1431 } 1432 if (sticky) 1433 setbitn(opt, StickyOpt); 1434 return; 1435 } 1436 /* 1437 ** SETCLASS -- set a word into a class 1438 ** 1439 ** Parameters: 1440 ** class -- the class to put the word in. 1441 ** word -- the word to enter 1442 ** 1443 ** Returns: 1444 ** none. 1445 ** 1446 ** Side Effects: 1447 ** puts the word into the symbol table. 1448 */ 1449 1450 setclass(class, word) 1451 int class; 1452 char *word; 1453 { 1454 register STAB *s; 1455 1456 if (tTd(37, 8)) 1457 printf("setclass(%c, %s)\n", class, word); 1458 s = stab(word, ST_CLASS, ST_ENTER); 1459 setbitn(class, s->s_class); 1460 } 1461 /* 1462 ** MAKEMAPENTRY -- create a map entry 1463 ** 1464 ** Parameters: 1465 ** line -- the config file line 1466 ** 1467 ** Returns: 1468 ** TRUE if it successfully entered the map entry. 1469 ** FALSE otherwise (usually syntax error). 1470 ** 1471 ** Side Effects: 1472 ** Enters the map into the dictionary. 1473 */ 1474 1475 void 1476 makemapentry(line) 1477 char *line; 1478 { 1479 register char *p; 1480 char *mapname; 1481 char *classname; 1482 register STAB *s; 1483 STAB *class; 1484 1485 for (p = line; isascii(*p) && isspace(*p); p++) 1486 continue; 1487 if (!(isascii(*p) && isalnum(*p))) 1488 { 1489 syserr("readcf: config K line: no map name"); 1490 return; 1491 } 1492 1493 mapname = p; 1494 while (isascii(*++p) && isalnum(*p)) 1495 continue; 1496 if (*p != '\0') 1497 *p++ = '\0'; 1498 while (isascii(*p) && isspace(*p)) 1499 p++; 1500 if (!(isascii(*p) && isalnum(*p))) 1501 { 1502 syserr("readcf: config K line, map %s: no map class", mapname); 1503 return; 1504 } 1505 classname = p; 1506 while (isascii(*++p) && isalnum(*p)) 1507 continue; 1508 if (*p != '\0') 1509 *p++ = '\0'; 1510 while (isascii(*p) && isspace(*p)) 1511 p++; 1512 1513 /* look up the class */ 1514 class = stab(classname, ST_MAPCLASS, ST_FIND); 1515 if (class == NULL) 1516 { 1517 syserr("readcf: map %s: class %s not available", mapname, classname); 1518 return; 1519 } 1520 1521 /* enter the map */ 1522 s = stab(mapname, ST_MAP, ST_ENTER); 1523 s->s_map.map_class = &class->s_mapclass; 1524 s->s_map.map_mname = newstr(mapname); 1525 1526 if (class->s_mapclass.map_parse(&s->s_map, p)) 1527 s->s_map.map_mflags |= MF_VALID; 1528 1529 if (tTd(37, 5)) 1530 { 1531 printf("map %s, class %s, flags %x, file %s,\n", 1532 s->s_map.map_mname, s->s_map.map_class->map_cname, 1533 s->s_map.map_mflags, 1534 s->s_map.map_file == NULL ? "(null)" : s->s_map.map_file); 1535 printf("\tapp %s, domain %s, rebuild %s\n", 1536 s->s_map.map_app == NULL ? "(null)" : s->s_map.map_app, 1537 s->s_map.map_domain == NULL ? "(null)" : s->s_map.map_domain, 1538 s->s_map.map_rebuild == NULL ? "(null)" : s->s_map.map_rebuild); 1539 } 1540 } 1541 /* 1542 ** SETTIMEOUTS -- parse and set timeout values 1543 ** 1544 ** Parameters: 1545 ** val -- a pointer to the values. If NULL, do initial 1546 ** settings. 1547 ** 1548 ** Returns: 1549 ** none. 1550 ** 1551 ** Side Effects: 1552 ** Initializes the TimeOuts structure 1553 */ 1554 1555 #define SECONDS 1556 #define MINUTES * 60 1557 #define HOUR * 3600 1558 1559 settimeouts(val) 1560 register char *val; 1561 { 1562 register char *p; 1563 extern time_t convtime(); 1564 1565 if (val == NULL) 1566 { 1567 TimeOuts.to_initial = (time_t) 5 MINUTES; 1568 TimeOuts.to_helo = (time_t) 5 MINUTES; 1569 TimeOuts.to_mail = (time_t) 10 MINUTES; 1570 TimeOuts.to_rcpt = (time_t) 1 HOUR; 1571 TimeOuts.to_datainit = (time_t) 5 MINUTES; 1572 TimeOuts.to_datablock = (time_t) 1 HOUR; 1573 TimeOuts.to_datafinal = (time_t) 1 HOUR; 1574 TimeOuts.to_rset = (time_t) 5 MINUTES; 1575 TimeOuts.to_quit = (time_t) 2 MINUTES; 1576 TimeOuts.to_nextcommand = (time_t) 1 HOUR; 1577 TimeOuts.to_miscshort = (time_t) 2 MINUTES; 1578 TimeOuts.to_ident = (time_t) 30 SECONDS; 1579 return; 1580 } 1581 1582 for (;; val = p) 1583 { 1584 while (isascii(*val) && isspace(*val)) 1585 val++; 1586 if (*val == '\0') 1587 break; 1588 for (p = val; *p != '\0' && *p != ','; p++) 1589 continue; 1590 if (*p != '\0') 1591 *p++ = '\0'; 1592 1593 if (isascii(*val) && isdigit(*val)) 1594 { 1595 /* old syntax -- set everything */ 1596 TimeOuts.to_mail = convtime(val, 'm'); 1597 TimeOuts.to_rcpt = TimeOuts.to_mail; 1598 TimeOuts.to_datainit = TimeOuts.to_mail; 1599 TimeOuts.to_datablock = TimeOuts.to_mail; 1600 TimeOuts.to_datafinal = TimeOuts.to_mail; 1601 TimeOuts.to_nextcommand = TimeOuts.to_mail; 1602 continue; 1603 } 1604 else 1605 { 1606 register char *q = strchr(val, '='); 1607 time_t to; 1608 1609 if (q == NULL) 1610 { 1611 /* syntax error */ 1612 continue; 1613 } 1614 *q++ = '\0'; 1615 to = convtime(q, 'm'); 1616 1617 if (strcasecmp(val, "initial") == 0) 1618 TimeOuts.to_initial = to; 1619 else if (strcasecmp(val, "mail") == 0) 1620 TimeOuts.to_mail = to; 1621 else if (strcasecmp(val, "rcpt") == 0) 1622 TimeOuts.to_rcpt = to; 1623 else if (strcasecmp(val, "datainit") == 0) 1624 TimeOuts.to_datainit = to; 1625 else if (strcasecmp(val, "datablock") == 0) 1626 TimeOuts.to_datablock = to; 1627 else if (strcasecmp(val, "datafinal") == 0) 1628 TimeOuts.to_datafinal = to; 1629 else if (strcasecmp(val, "command") == 0) 1630 TimeOuts.to_nextcommand = to; 1631 else if (strcasecmp(val, "rset") == 0) 1632 TimeOuts.to_rset = to; 1633 else if (strcasecmp(val, "helo") == 0) 1634 TimeOuts.to_helo = to; 1635 else if (strcasecmp(val, "quit") == 0) 1636 TimeOuts.to_quit = to; 1637 else if (strcasecmp(val, "misc") == 0) 1638 TimeOuts.to_miscshort = to; 1639 else if (strcasecmp(val, "ident") == 0) 1640 TimeOuts.to_ident = to; 1641 else 1642 syserr("settimeouts: invalid timeout %s", val); 1643 } 1644 } 1645 } 1646