1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988 Regents of the University of California. 4 * All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #ifndef lint 10 static char sccsid[] = "@(#)readcf.c 5.29 (Berkeley) 10/11/91"; 11 #endif /* not lint */ 12 13 # include "sendmail.h" 14 15 /* 16 ** READCF -- read control file. 17 ** 18 ** This routine reads the control file and builds the internal 19 ** form. 20 ** 21 ** The file is formatted as a sequence of lines, each taken 22 ** atomically. The first character of each line describes how 23 ** the line is to be interpreted. The lines are: 24 ** Dxval Define macro x to have value val. 25 ** Cxword Put word into class x. 26 ** Fxfile [fmt] Read file for lines to put into 27 ** class x. Use scanf string 'fmt' 28 ** or "%s" if not present. Fmt should 29 ** only produce one string-valued result. 30 ** Hname: value Define header with field-name 'name' 31 ** and value as specified; this will be 32 ** macro expanded immediately before 33 ** use. 34 ** Sn Use rewriting set n. 35 ** Rlhs rhs Rewrite addresses that match lhs to 36 ** be rhs. 37 ** Mn arg=val... Define mailer. n is the internal name. 38 ** Args specify mailer parameters. 39 ** Oxvalue Set option x to value. 40 ** Pname=value Set precedence name to value. 41 ** 42 ** Parameters: 43 ** cfname -- control file name. 44 ** 45 ** Returns: 46 ** none. 47 ** 48 ** Side Effects: 49 ** Builds several internal tables. 50 */ 51 52 readcf(cfname) 53 char *cfname; 54 { 55 FILE *cf; 56 int ruleset = 0; 57 char *q; 58 char **pv; 59 struct rewrite *rwp = NULL; 60 char buf[MAXLINE]; 61 register char *p; 62 extern char **prescan(); 63 extern char **copyplist(); 64 char exbuf[MAXLINE]; 65 char pvpbuf[PSBUFSIZE]; 66 extern char *fgetfolded(); 67 extern char *munchstring(); 68 69 cf = fopen(cfname, "r"); 70 if (cf == NULL) 71 { 72 syserr("cannot open %s", cfname); 73 exit(EX_OSFILE); 74 } 75 76 FileName = cfname; 77 LineNumber = 0; 78 while (fgetfolded(buf, sizeof buf, cf) != NULL) 79 { 80 /* map $ into \001 (ASCII SOH) for macro expansion */ 81 for (p = buf; *p != '\0'; p++) 82 { 83 if (*p != '$') 84 continue; 85 86 if (p[1] == '$') 87 { 88 /* actual dollar sign.... */ 89 (void) strcpy(p, p + 1); 90 continue; 91 } 92 93 /* convert to macro expansion character */ 94 *p = '\001'; 95 } 96 97 /* interpret this line */ 98 switch (buf[0]) 99 { 100 case '\0': 101 case '#': /* comment */ 102 break; 103 104 case 'R': /* rewriting rule */ 105 for (p = &buf[1]; *p != '\0' && *p != '\t'; p++) 106 continue; 107 108 if (*p == '\0') 109 { 110 syserr("invalid rewrite line \"%s\"", buf); 111 break; 112 } 113 114 /* allocate space for the rule header */ 115 if (rwp == NULL) 116 { 117 RewriteRules[ruleset] = rwp = 118 (struct rewrite *) xalloc(sizeof *rwp); 119 } 120 else 121 { 122 rwp->r_next = (struct rewrite *) xalloc(sizeof *rwp); 123 rwp = rwp->r_next; 124 } 125 rwp->r_next = NULL; 126 127 /* expand and save the LHS */ 128 *p = '\0'; 129 expand(&buf[1], exbuf, &exbuf[sizeof exbuf], CurEnv); 130 rwp->r_lhs = prescan(exbuf, '\t', pvpbuf); 131 if (rwp->r_lhs != NULL) 132 rwp->r_lhs = copyplist(rwp->r_lhs, TRUE); 133 134 /* expand and save the RHS */ 135 while (*++p == '\t') 136 continue; 137 q = p; 138 while (*p != '\0' && *p != '\t') 139 p++; 140 *p = '\0'; 141 expand(q, exbuf, &exbuf[sizeof exbuf], CurEnv); 142 rwp->r_rhs = prescan(exbuf, '\t', pvpbuf); 143 if (rwp->r_rhs != NULL) 144 rwp->r_rhs = copyplist(rwp->r_rhs, TRUE); 145 break; 146 147 case 'S': /* select rewriting set */ 148 ruleset = atoi(&buf[1]); 149 if (ruleset >= MAXRWSETS || ruleset < 0) 150 { 151 syserr("bad ruleset %d (%d max)", ruleset, MAXRWSETS); 152 ruleset = 0; 153 } 154 rwp = NULL; 155 break; 156 157 case 'D': /* macro definition */ 158 define(buf[1], newstr(munchstring(&buf[2])), CurEnv); 159 break; 160 161 case 'H': /* required header line */ 162 (void) chompheader(&buf[1], TRUE); 163 break; 164 165 case 'C': /* word class */ 166 case 'F': /* word class from file */ 167 /* read list of words from argument or file */ 168 if (buf[0] == 'F') 169 { 170 /* read from file */ 171 for (p = &buf[2]; *p != '\0' && !isspace(*p); p++) 172 continue; 173 if (*p == '\0') 174 p = "%s"; 175 else 176 { 177 *p = '\0'; 178 while (isspace(*++p)) 179 continue; 180 } 181 fileclass(buf[1], &buf[2], p); 182 break; 183 } 184 185 /* scan the list of words and set class for all */ 186 for (p = &buf[2]; *p != '\0'; ) 187 { 188 register char *wd; 189 char delim; 190 191 while (*p != '\0' && isspace(*p)) 192 p++; 193 wd = p; 194 while (*p != '\0' && !isspace(*p)) 195 p++; 196 delim = *p; 197 *p = '\0'; 198 if (wd[0] != '\0') 199 setclass(buf[1], wd); 200 *p = delim; 201 } 202 break; 203 204 case 'M': /* define mailer */ 205 makemailer(&buf[1]); 206 break; 207 208 case 'O': /* set option */ 209 setoption(buf[1], &buf[2], TRUE, FALSE); 210 break; 211 212 case 'P': /* set precedence */ 213 if (NumPriorities >= MAXPRIORITIES) 214 { 215 toomany('P', MAXPRIORITIES); 216 break; 217 } 218 for (p = &buf[1]; *p != '\0' && *p != '=' && *p != '\t'; p++) 219 continue; 220 if (*p == '\0') 221 goto badline; 222 *p = '\0'; 223 Priorities[NumPriorities].pri_name = newstr(&buf[1]); 224 Priorities[NumPriorities].pri_val = atoi(++p); 225 NumPriorities++; 226 break; 227 228 case 'T': /* trusted user(s) */ 229 p = &buf[1]; 230 while (*p != '\0') 231 { 232 while (isspace(*p)) 233 p++; 234 q = p; 235 while (*p != '\0' && !isspace(*p)) 236 p++; 237 if (*p != '\0') 238 *p++ = '\0'; 239 if (*q == '\0') 240 continue; 241 for (pv = TrustedUsers; *pv != NULL; pv++) 242 continue; 243 if (pv >= &TrustedUsers[MAXTRUST]) 244 { 245 toomany('T', MAXTRUST); 246 break; 247 } 248 *pv = newstr(q); 249 } 250 break; 251 252 default: 253 badline: 254 syserr("unknown control line \"%s\"", buf); 255 } 256 } 257 FileName = NULL; 258 } 259 /* 260 ** TOOMANY -- signal too many of some option 261 ** 262 ** Parameters: 263 ** id -- the id of the error line 264 ** maxcnt -- the maximum possible values 265 ** 266 ** Returns: 267 ** none. 268 ** 269 ** Side Effects: 270 ** gives a syserr. 271 */ 272 273 toomany(id, maxcnt) 274 char id; 275 int maxcnt; 276 { 277 syserr("too many %c lines, %d max", id, maxcnt); 278 } 279 /* 280 ** FILECLASS -- read members of a class from a file 281 ** 282 ** Parameters: 283 ** class -- class to define. 284 ** filename -- name of file to read. 285 ** fmt -- scanf string to use for match. 286 ** 287 ** Returns: 288 ** none 289 ** 290 ** Side Effects: 291 ** 292 ** puts all lines in filename that match a scanf into 293 ** the named class. 294 */ 295 296 fileclass(class, filename, fmt) 297 int class; 298 char *filename; 299 char *fmt; 300 { 301 FILE *f; 302 char buf[MAXLINE]; 303 304 f = fopen(filename, "r"); 305 if (f == NULL) 306 { 307 syserr("cannot open %s", filename); 308 return; 309 } 310 311 while (fgets(buf, sizeof buf, f) != NULL) 312 { 313 register STAB *s; 314 register char *p; 315 # ifdef SCANF 316 char wordbuf[MAXNAME+1]; 317 318 if (sscanf(buf, fmt, wordbuf) != 1) 319 continue; 320 p = wordbuf; 321 # else SCANF 322 p = buf; 323 # endif SCANF 324 325 /* 326 ** Break up the match into words. 327 */ 328 329 while (*p != '\0') 330 { 331 register char *q; 332 333 /* strip leading spaces */ 334 while (isspace(*p)) 335 p++; 336 if (*p == '\0') 337 break; 338 339 /* find the end of the word */ 340 q = p; 341 while (*p != '\0' && !isspace(*p)) 342 p++; 343 if (*p != '\0') 344 *p++ = '\0'; 345 346 /* enter the word in the symbol table */ 347 s = stab(q, ST_CLASS, ST_ENTER); 348 setbitn(class, s->s_class); 349 } 350 } 351 352 (void) fclose(f); 353 } 354 /* 355 ** MAKEMAILER -- define a new mailer. 356 ** 357 ** Parameters: 358 ** line -- description of mailer. This is in labeled 359 ** fields. The fields are: 360 ** P -- the path to the mailer 361 ** F -- the flags associated with the mailer 362 ** A -- the argv for this mailer 363 ** S -- the sender rewriting set 364 ** R -- the recipient rewriting set 365 ** E -- the eol string 366 ** The first word is the canonical name of the mailer. 367 ** 368 ** Returns: 369 ** none. 370 ** 371 ** Side Effects: 372 ** enters the mailer into the mailer table. 373 */ 374 375 makemailer(line) 376 char *line; 377 { 378 register char *p; 379 register struct mailer *m; 380 register STAB *s; 381 int i; 382 char fcode; 383 extern int NextMailer; 384 extern char **makeargv(); 385 extern char *munchstring(); 386 extern char *DelimChar; 387 extern long atol(); 388 389 /* allocate a mailer and set up defaults */ 390 m = (struct mailer *) xalloc(sizeof *m); 391 bzero((char *) m, sizeof *m); 392 m->m_mno = NextMailer; 393 m->m_eol = "\n"; 394 395 /* collect the mailer name */ 396 for (p = line; *p != '\0' && *p != ',' && !isspace(*p); p++) 397 continue; 398 if (*p != '\0') 399 *p++ = '\0'; 400 m->m_name = newstr(line); 401 402 /* now scan through and assign info from the fields */ 403 while (*p != '\0') 404 { 405 while (*p != '\0' && (*p == ',' || isspace(*p))) 406 p++; 407 408 /* p now points to field code */ 409 fcode = *p; 410 while (*p != '\0' && *p != '=' && *p != ',') 411 p++; 412 if (*p++ != '=') 413 { 414 syserr("`=' expected"); 415 return; 416 } 417 while (isspace(*p)) 418 p++; 419 420 /* p now points to the field body */ 421 p = munchstring(p); 422 423 /* install the field into the mailer struct */ 424 switch (fcode) 425 { 426 case 'P': /* pathname */ 427 m->m_mailer = newstr(p); 428 break; 429 430 case 'F': /* flags */ 431 for (; *p != '\0'; p++) 432 setbitn(*p, m->m_flags); 433 break; 434 435 case 'S': /* sender rewriting ruleset */ 436 case 'R': /* recipient rewriting ruleset */ 437 i = atoi(p); 438 if (i < 0 || i >= MAXRWSETS) 439 { 440 syserr("invalid rewrite set, %d max", MAXRWSETS); 441 return; 442 } 443 if (fcode == 'S') 444 m->m_s_rwset = i; 445 else 446 m->m_r_rwset = i; 447 break; 448 449 case 'E': /* end of line string */ 450 m->m_eol = newstr(p); 451 break; 452 453 case 'A': /* argument vector */ 454 m->m_argv = makeargv(p); 455 break; 456 457 case 'M': /* maximum message size */ 458 m->m_maxsize = atol(p); 459 break; 460 } 461 462 p = DelimChar; 463 } 464 465 /* now store the mailer away */ 466 if (NextMailer >= MAXMAILERS) 467 { 468 syserr("too many mailers defined (%d max)", MAXMAILERS); 469 return; 470 } 471 Mailer[NextMailer++] = m; 472 s = stab(m->m_name, ST_MAILER, ST_ENTER); 473 s->s_mailer = m; 474 } 475 /* 476 ** MUNCHSTRING -- translate a string into internal form. 477 ** 478 ** Parameters: 479 ** p -- the string to munch. 480 ** 481 ** Returns: 482 ** the munched string. 483 ** 484 ** Side Effects: 485 ** Sets "DelimChar" to point to the string that caused us 486 ** to stop. 487 */ 488 489 char * 490 munchstring(p) 491 register char *p; 492 { 493 register char *q; 494 bool backslash = FALSE; 495 bool quotemode = FALSE; 496 static char buf[MAXLINE]; 497 extern char *DelimChar; 498 499 for (q = buf; *p != '\0'; p++) 500 { 501 if (backslash) 502 { 503 /* everything is roughly literal */ 504 backslash = FALSE; 505 switch (*p) 506 { 507 case 'r': /* carriage return */ 508 *q++ = '\r'; 509 continue; 510 511 case 'n': /* newline */ 512 *q++ = '\n'; 513 continue; 514 515 case 'f': /* form feed */ 516 *q++ = '\f'; 517 continue; 518 519 case 'b': /* backspace */ 520 *q++ = '\b'; 521 continue; 522 } 523 *q++ = *p; 524 } 525 else 526 { 527 if (*p == '\\') 528 backslash = TRUE; 529 else if (*p == '"') 530 quotemode = !quotemode; 531 else if (quotemode || *p != ',') 532 *q++ = *p; 533 else 534 break; 535 } 536 } 537 538 DelimChar = p; 539 *q++ = '\0'; 540 return (buf); 541 } 542 /* 543 ** MAKEARGV -- break up a string into words 544 ** 545 ** Parameters: 546 ** p -- the string to break up. 547 ** 548 ** Returns: 549 ** a char **argv (dynamically allocated) 550 ** 551 ** Side Effects: 552 ** munges p. 553 */ 554 555 char ** 556 makeargv(p) 557 register char *p; 558 { 559 char *q; 560 int i; 561 char **avp; 562 char *argv[MAXPV + 1]; 563 564 /* take apart the words */ 565 i = 0; 566 while (*p != '\0' && i < MAXPV) 567 { 568 q = p; 569 while (*p != '\0' && !isspace(*p)) 570 p++; 571 while (isspace(*p)) 572 *p++ = '\0'; 573 argv[i++] = newstr(q); 574 } 575 argv[i++] = NULL; 576 577 /* now make a copy of the argv */ 578 avp = (char **) xalloc(sizeof *avp * i); 579 bcopy((char *) argv, (char *) avp, sizeof *avp * i); 580 581 return (avp); 582 } 583 /* 584 ** PRINTRULES -- print rewrite rules (for debugging) 585 ** 586 ** Parameters: 587 ** none. 588 ** 589 ** Returns: 590 ** none. 591 ** 592 ** Side Effects: 593 ** prints rewrite rules. 594 */ 595 596 printrules() 597 { 598 register struct rewrite *rwp; 599 register int ruleset; 600 601 for (ruleset = 0; ruleset < 10; ruleset++) 602 { 603 if (RewriteRules[ruleset] == NULL) 604 continue; 605 printf("\n----Rule Set %d:", ruleset); 606 607 for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next) 608 { 609 printf("\nLHS:"); 610 printav(rwp->r_lhs); 611 printf("RHS:"); 612 printav(rwp->r_rhs); 613 } 614 } 615 } 616 617 /* 618 ** SETOPTION -- set global processing option 619 ** 620 ** Parameters: 621 ** opt -- option name. 622 ** val -- option value (as a text string). 623 ** safe -- set if this came from a configuration file. 624 ** Some options (if set from the command line) will 625 ** reset the user id to avoid security problems. 626 ** sticky -- if set, don't let other setoptions override 627 ** this value. 628 ** 629 ** Returns: 630 ** none. 631 ** 632 ** Side Effects: 633 ** Sets options as implied by the arguments. 634 */ 635 636 static BITMAP StickyOpt; /* set if option is stuck */ 637 extern char *NetName; /* name of home (local) network */ 638 639 setoption(opt, val, safe, sticky) 640 char opt; 641 char *val; 642 bool safe; 643 bool sticky; 644 { 645 extern bool atobool(); 646 extern time_t convtime(); 647 extern int QueueLA; 648 extern int RefuseLA; 649 extern bool trusteduser(); 650 extern char *username(); 651 652 if (tTd(37, 1)) 653 printf("setoption %c=%s", opt, val); 654 655 /* 656 ** See if this option is preset for us. 657 */ 658 659 if (bitnset(opt, StickyOpt)) 660 { 661 if (tTd(37, 1)) 662 printf(" (ignored)\n"); 663 return; 664 } 665 666 /* 667 ** Check to see if this option can be specified by this user. 668 */ 669 670 if (!safe && getuid() == 0) 671 safe = TRUE; 672 if (!safe && index("deiLmorsv", opt) == NULL) 673 { 674 if (opt != 'M' || (val[0] != 'r' && val[0] != 's')) 675 { 676 if (tTd(37, 1)) 677 printf(" (unsafe)"); 678 if (getuid() != geteuid()) 679 { 680 if (tTd(37, 1)) 681 printf("(Resetting uid)"); 682 (void) setgid(getgid()); 683 (void) setuid(getuid()); 684 } 685 } 686 } 687 if (tTd(37, 1)) 688 printf("\n"); 689 690 switch (opt) 691 { 692 case '=': /* config file generation level */ 693 ConfigLevel = atoi(val); 694 break; 695 696 case 'A': /* set default alias file */ 697 if (val[0] == '\0') 698 AliasFile = "aliases"; 699 else 700 AliasFile = newstr(val); 701 break; 702 703 case 'a': /* look N minutes for "@:@" in alias file */ 704 if (val[0] == '\0') 705 SafeAlias = 5; 706 else 707 SafeAlias = atoi(val); 708 break; 709 710 case 'B': /* substitution for blank character */ 711 SpaceSub = val[0]; 712 if (SpaceSub == '\0') 713 SpaceSub = ' '; 714 break; 715 716 case 'c': /* don't connect to "expensive" mailers */ 717 NoConnect = atobool(val); 718 break; 719 720 case 'C': /* checkpoint every N addresses */ 721 CheckpointInterval = atoi(val); 722 break; 723 724 case 'd': /* delivery mode */ 725 switch (*val) 726 { 727 case '\0': 728 SendMode = SM_DELIVER; 729 break; 730 731 case SM_QUEUE: /* queue only */ 732 #ifndef QUEUE 733 syserr("need QUEUE to set -odqueue"); 734 #endif QUEUE 735 /* fall through..... */ 736 737 case SM_DELIVER: /* do everything */ 738 case SM_FORK: /* fork after verification */ 739 SendMode = *val; 740 break; 741 742 default: 743 syserr("Unknown delivery mode %c", *val); 744 exit(EX_USAGE); 745 } 746 break; 747 748 case 'D': /* rebuild alias database as needed */ 749 AutoRebuild = atobool(val); 750 break; 751 752 case 'e': /* set error processing mode */ 753 switch (*val) 754 { 755 case EM_QUIET: /* be silent about it */ 756 case EM_MAIL: /* mail back */ 757 case EM_BERKNET: /* do berknet error processing */ 758 case EM_WRITE: /* write back (or mail) */ 759 HoldErrs = TRUE; 760 /* fall through... */ 761 762 case EM_PRINT: /* print errors normally (default) */ 763 ErrorMode = *val; 764 break; 765 } 766 break; 767 768 case 'F': /* file mode */ 769 FileMode = atooct(val) & 0777; 770 break; 771 772 case 'f': /* save Unix-style From lines on front */ 773 SaveFrom = atobool(val); 774 break; 775 776 case 'g': /* default gid */ 777 DefGid = atoi(val); 778 break; 779 780 case 'H': /* help file */ 781 if (val[0] == '\0') 782 HelpFile = "sendmail.hf"; 783 else 784 HelpFile = newstr(val); 785 break; 786 787 case 'h': /* maximum hop count */ 788 MaxHopCount = atoi(val); 789 break; 790 791 case 'I': /* use internet domain name server */ 792 UseNameServer = atobool(val); 793 break; 794 795 case 'i': /* ignore dot lines in message */ 796 IgnrDot = atobool(val); 797 break; 798 799 case 'L': /* log level */ 800 LogLevel = atoi(val); 801 break; 802 803 case 'M': /* define macro */ 804 define(val[0], newstr(&val[1]), CurEnv); 805 sticky = FALSE; 806 break; 807 808 case 'm': /* send to me too */ 809 MeToo = atobool(val); 810 break; 811 812 case 'n': /* validate RHS in newaliases */ 813 CheckAliases = atobool(val); 814 break; 815 816 # ifdef DAEMON 817 case 'N': /* home (local?) network name */ 818 NetName = newstr(val); 819 break; 820 # endif DAEMON 821 822 case 'o': /* assume old style headers */ 823 if (atobool(val)) 824 CurEnv->e_flags |= EF_OLDSTYLE; 825 else 826 CurEnv->e_flags &= ~EF_OLDSTYLE; 827 break; 828 829 case 'P': /* postmaster copy address for returned mail */ 830 PostMasterCopy = newstr(val); 831 break; 832 833 case 'q': /* slope of queue only function */ 834 QueueFactor = atoi(val); 835 break; 836 837 case 'Q': /* queue directory */ 838 if (val[0] == '\0') 839 QueueDir = "mqueue"; 840 else 841 QueueDir = newstr(val); 842 break; 843 844 case 'r': /* read timeout */ 845 ReadTimeout = convtime(val); 846 break; 847 848 case 'S': /* status file */ 849 if (val[0] == '\0') 850 StatFile = "sendmail.st"; 851 else 852 StatFile = newstr(val); 853 break; 854 855 case 's': /* be super safe, even if expensive */ 856 SuperSafe = atobool(val); 857 break; 858 859 case 'T': /* queue timeout */ 860 TimeOut = convtime(val); 861 /*FALLTHROUGH*/ 862 863 case 't': /* time zone name */ 864 break; 865 866 case 'U': /* location of user database */ 867 UdbSpec = newstr(val); 868 break; 869 870 case 'u': /* set default uid */ 871 DefUid = atoi(val); 872 setdefuser(); 873 break; 874 875 case 'v': /* run in verbose mode */ 876 Verbose = atobool(val); 877 break; 878 879 case 'w': /* we don't have wildcard MX records */ 880 NoWildcardMX = atobool(val); 881 break; 882 883 case 'x': /* load avg at which to auto-queue msgs */ 884 QueueLA = atoi(val); 885 break; 886 887 case 'X': /* load avg at which to auto-reject connections */ 888 RefuseLA = atoi(val); 889 break; 890 891 case 'y': /* work recipient factor */ 892 WkRecipFact = atoi(val); 893 break; 894 895 case 'Y': /* fork jobs during queue runs */ 896 ForkQueueRuns = atobool(val); 897 break; 898 899 case 'z': /* work message class factor */ 900 WkClassFact = atoi(val); 901 break; 902 903 case 'Z': /* work time factor */ 904 WkTimeFact = atoi(val); 905 break; 906 907 default: 908 break; 909 } 910 if (sticky) 911 setbitn(opt, StickyOpt); 912 return; 913 } 914 /* 915 ** SETCLASS -- set a word into a class 916 ** 917 ** Parameters: 918 ** class -- the class to put the word in. 919 ** word -- the word to enter 920 ** 921 ** Returns: 922 ** none. 923 ** 924 ** Side Effects: 925 ** puts the word into the symbol table. 926 */ 927 928 setclass(class, word) 929 int class; 930 char *word; 931 { 932 register STAB *s; 933 934 s = stab(word, ST_CLASS, ST_ENTER); 935 setbitn(class, s->s_class); 936 } 937