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.20 (Berkeley) 02/08/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 (filename[0] == '|') 578 { 579 syserr("fileclass: pipes (F%c%s) not supported due to security problems", 580 class, filename); 581 return; 582 } 583 if (stat(filename, &stbuf) < 0) 584 { 585 if (!optional) 586 syserr("fileclass: cannot stat %s", filename); 587 return; 588 } 589 if (!S_ISREG(stbuf.st_mode)) 590 { 591 syserr("fileclass: %s not a regular file", filename); 592 return; 593 } 594 if (!safe && access(filename, R_OK) < 0) 595 { 596 syserr("fileclass: access denied on %s", filename); 597 return; 598 } 599 f = fopen(filename, "r"); 600 if (f == NULL) 601 { 602 syserr("fileclass: cannot open %s", filename); 603 return; 604 } 605 606 while (fgets(buf, sizeof buf, f) != NULL) 607 { 608 register STAB *s; 609 register char *p; 610 # ifdef SCANF 611 char wordbuf[MAXNAME+1]; 612 613 if (sscanf(buf, fmt, wordbuf) != 1) 614 continue; 615 p = wordbuf; 616 # else /* SCANF */ 617 p = buf; 618 # endif /* SCANF */ 619 620 /* 621 ** Break up the match into words. 622 */ 623 624 while (*p != '\0') 625 { 626 register char *q; 627 628 /* strip leading spaces */ 629 while (isascii(*p) && isspace(*p)) 630 p++; 631 if (*p == '\0') 632 break; 633 634 /* find the end of the word */ 635 q = p; 636 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 637 p++; 638 if (*p != '\0') 639 *p++ = '\0'; 640 641 /* enter the word in the symbol table */ 642 s = stab(q, ST_CLASS, ST_ENTER); 643 setbitn(class, s->s_class); 644 } 645 } 646 647 (void) fclose(f); 648 } 649 /* 650 ** MAKEMAILER -- define a new mailer. 651 ** 652 ** Parameters: 653 ** line -- description of mailer. This is in labeled 654 ** fields. The fields are: 655 ** P -- the path to the mailer 656 ** F -- the flags associated with the mailer 657 ** A -- the argv for this mailer 658 ** S -- the sender rewriting set 659 ** R -- the recipient rewriting set 660 ** E -- the eol string 661 ** The first word is the canonical name of the mailer. 662 ** 663 ** Returns: 664 ** none. 665 ** 666 ** Side Effects: 667 ** enters the mailer into the mailer table. 668 */ 669 670 makemailer(line) 671 char *line; 672 { 673 register char *p; 674 register struct mailer *m; 675 register STAB *s; 676 int i; 677 char fcode; 678 auto char *endp; 679 extern int NextMailer; 680 extern char **makeargv(); 681 extern char *munchstring(); 682 extern long atol(); 683 684 /* allocate a mailer and set up defaults */ 685 m = (struct mailer *) xalloc(sizeof *m); 686 bzero((char *) m, sizeof *m); 687 m->m_eol = "\n"; 688 689 /* collect the mailer name */ 690 for (p = line; *p != '\0' && *p != ',' && !(isascii(*p) && isspace(*p)); p++) 691 continue; 692 if (*p != '\0') 693 *p++ = '\0'; 694 m->m_name = newstr(line); 695 696 /* now scan through and assign info from the fields */ 697 while (*p != '\0') 698 { 699 auto char *delimptr; 700 701 while (*p != '\0' && (*p == ',' || (isascii(*p) && isspace(*p)))) 702 p++; 703 704 /* p now points to field code */ 705 fcode = *p; 706 while (*p != '\0' && *p != '=' && *p != ',') 707 p++; 708 if (*p++ != '=') 709 { 710 syserr("mailer %s: `=' expected", m->m_name); 711 return; 712 } 713 while (isascii(*p) && isspace(*p)) 714 p++; 715 716 /* p now points to the field body */ 717 p = munchstring(p, &delimptr); 718 719 /* install the field into the mailer struct */ 720 switch (fcode) 721 { 722 case 'P': /* pathname */ 723 m->m_mailer = newstr(p); 724 break; 725 726 case 'F': /* flags */ 727 for (; *p != '\0'; p++) 728 if (!(isascii(*p) && isspace(*p))) 729 setbitn(*p, m->m_flags); 730 break; 731 732 case 'S': /* sender rewriting ruleset */ 733 case 'R': /* recipient rewriting ruleset */ 734 i = strtol(p, &endp, 10); 735 if (i < 0 || i >= MAXRWSETS) 736 { 737 syserr("invalid rewrite set, %d max", MAXRWSETS); 738 return; 739 } 740 if (fcode == 'S') 741 m->m_sh_rwset = m->m_se_rwset = i; 742 else 743 m->m_rh_rwset = m->m_re_rwset = i; 744 745 p = endp; 746 if (*p++ == '/') 747 { 748 i = strtol(p, NULL, 10); 749 if (i < 0 || i >= MAXRWSETS) 750 { 751 syserr("invalid rewrite set, %d max", 752 MAXRWSETS); 753 return; 754 } 755 if (fcode == 'S') 756 m->m_sh_rwset = i; 757 else 758 m->m_rh_rwset = i; 759 } 760 break; 761 762 case 'E': /* end of line string */ 763 m->m_eol = newstr(p); 764 break; 765 766 case 'A': /* argument vector */ 767 m->m_argv = makeargv(p); 768 break; 769 770 case 'M': /* maximum message size */ 771 m->m_maxsize = atol(p); 772 break; 773 774 case 'L': /* maximum line length */ 775 m->m_linelimit = atoi(p); 776 break; 777 778 case 'D': /* working directory */ 779 m->m_execdir = newstr(p); 780 break; 781 } 782 783 p = delimptr; 784 } 785 786 /* do some heuristic cleanup for back compatibility */ 787 if (bitnset(M_LIMITS, m->m_flags)) 788 { 789 if (m->m_linelimit == 0) 790 m->m_linelimit = SMTPLINELIM; 791 if (ConfigLevel < 2) 792 setbitn(M_7BITS, m->m_flags); 793 } 794 795 /* do some rationality checking */ 796 if (m->m_argv == NULL) 797 { 798 syserr("M%s: A= argument required", m->m_name); 799 return; 800 } 801 if (m->m_mailer == NULL) 802 { 803 syserr("M%s: P= argument required", m->m_name); 804 return; 805 } 806 807 if (NextMailer >= MAXMAILERS) 808 { 809 syserr("too many mailers defined (%d max)", MAXMAILERS); 810 return; 811 } 812 813 s = stab(m->m_name, ST_MAILER, ST_ENTER); 814 if (s->s_mailer != NULL) 815 { 816 i = s->s_mailer->m_mno; 817 free(s->s_mailer); 818 } 819 else 820 { 821 i = NextMailer++; 822 } 823 Mailer[i] = s->s_mailer = m; 824 m->m_mno = i; 825 } 826 /* 827 ** MUNCHSTRING -- translate a string into internal form. 828 ** 829 ** Parameters: 830 ** p -- the string to munch. 831 ** delimptr -- if non-NULL, set to the pointer of the 832 ** field delimiter character. 833 ** 834 ** Returns: 835 ** the munched string. 836 */ 837 838 char * 839 munchstring(p, delimptr) 840 register char *p; 841 char **delimptr; 842 { 843 register char *q; 844 bool backslash = FALSE; 845 bool quotemode = FALSE; 846 static char buf[MAXLINE]; 847 848 for (q = buf; *p != '\0'; p++) 849 { 850 if (backslash) 851 { 852 /* everything is roughly literal */ 853 backslash = FALSE; 854 switch (*p) 855 { 856 case 'r': /* carriage return */ 857 *q++ = '\r'; 858 continue; 859 860 case 'n': /* newline */ 861 *q++ = '\n'; 862 continue; 863 864 case 'f': /* form feed */ 865 *q++ = '\f'; 866 continue; 867 868 case 'b': /* backspace */ 869 *q++ = '\b'; 870 continue; 871 } 872 *q++ = *p; 873 } 874 else 875 { 876 if (*p == '\\') 877 backslash = TRUE; 878 else if (*p == '"') 879 quotemode = !quotemode; 880 else if (quotemode || *p != ',') 881 *q++ = *p; 882 else 883 break; 884 } 885 } 886 887 if (delimptr != NULL) 888 *delimptr = p; 889 *q++ = '\0'; 890 return (buf); 891 } 892 /* 893 ** MAKEARGV -- break up a string into words 894 ** 895 ** Parameters: 896 ** p -- the string to break up. 897 ** 898 ** Returns: 899 ** a char **argv (dynamically allocated) 900 ** 901 ** Side Effects: 902 ** munges p. 903 */ 904 905 char ** 906 makeargv(p) 907 register char *p; 908 { 909 char *q; 910 int i; 911 char **avp; 912 char *argv[MAXPV + 1]; 913 914 /* take apart the words */ 915 i = 0; 916 while (*p != '\0' && i < MAXPV) 917 { 918 q = p; 919 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 920 p++; 921 while (isascii(*p) && isspace(*p)) 922 *p++ = '\0'; 923 argv[i++] = newstr(q); 924 } 925 argv[i++] = NULL; 926 927 /* now make a copy of the argv */ 928 avp = (char **) xalloc(sizeof *avp * i); 929 bcopy((char *) argv, (char *) avp, sizeof *avp * i); 930 931 return (avp); 932 } 933 /* 934 ** PRINTRULES -- print rewrite rules (for debugging) 935 ** 936 ** Parameters: 937 ** none. 938 ** 939 ** Returns: 940 ** none. 941 ** 942 ** Side Effects: 943 ** prints rewrite rules. 944 */ 945 946 printrules() 947 { 948 register struct rewrite *rwp; 949 register int ruleset; 950 951 for (ruleset = 0; ruleset < 10; ruleset++) 952 { 953 if (RewriteRules[ruleset] == NULL) 954 continue; 955 printf("\n----Rule Set %d:", ruleset); 956 957 for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next) 958 { 959 printf("\nLHS:"); 960 printav(rwp->r_lhs); 961 printf("RHS:"); 962 printav(rwp->r_rhs); 963 } 964 } 965 } 966 967 /* 968 ** SETOPTION -- set global processing option 969 ** 970 ** Parameters: 971 ** opt -- option name. 972 ** val -- option value (as a text string). 973 ** safe -- set if this came from a configuration file. 974 ** Some options (if set from the command line) will 975 ** reset the user id to avoid security problems. 976 ** sticky -- if set, don't let other setoptions override 977 ** this value. 978 ** e -- the main envelope. 979 ** 980 ** Returns: 981 ** none. 982 ** 983 ** Side Effects: 984 ** Sets options as implied by the arguments. 985 */ 986 987 static BITMAP StickyOpt; /* set if option is stuck */ 988 989 990 #ifdef NAMED_BIND 991 992 struct resolverflags 993 { 994 char *rf_name; /* name of the flag */ 995 long rf_bits; /* bits to set/clear */ 996 } ResolverFlags[] = 997 { 998 "debug", RES_DEBUG, 999 "aaonly", RES_AAONLY, 1000 "usevc", RES_USEVC, 1001 "primary", RES_PRIMARY, 1002 "igntc", RES_IGNTC, 1003 "recurse", RES_RECURSE, 1004 "defnames", RES_DEFNAMES, 1005 "stayopen", RES_STAYOPEN, 1006 "dnsrch", RES_DNSRCH, 1007 "true", 0, /* to avoid error on old syntax */ 1008 NULL, 0 1009 }; 1010 1011 #endif 1012 1013 setoption(opt, val, safe, sticky, e) 1014 char opt; 1015 char *val; 1016 bool safe; 1017 bool sticky; 1018 register ENVELOPE *e; 1019 { 1020 register char *p; 1021 extern bool atobool(); 1022 extern time_t convtime(); 1023 extern int QueueLA; 1024 extern int RefuseLA; 1025 extern bool Warn_Q_option; 1026 extern bool trusteduser(); 1027 1028 if (tTd(37, 1)) 1029 printf("setoption %c=%s", opt, val); 1030 1031 /* 1032 ** See if this option is preset for us. 1033 */ 1034 1035 if (!sticky && bitnset(opt, StickyOpt)) 1036 { 1037 if (tTd(37, 1)) 1038 printf(" (ignored)\n"); 1039 return; 1040 } 1041 1042 /* 1043 ** Check to see if this option can be specified by this user. 1044 */ 1045 1046 if (!safe && RealUid == 0) 1047 safe = TRUE; 1048 if (!safe && strchr("bCdeEijLmoprsvw7", opt) == NULL) 1049 { 1050 if (opt != 'M' || (val[0] != 'r' && val[0] != 's')) 1051 { 1052 if (tTd(37, 1)) 1053 printf(" (unsafe)"); 1054 if (RealUid != geteuid()) 1055 { 1056 if (tTd(37, 1)) 1057 printf("(Resetting uid)"); 1058 (void) setgid(RealGid); 1059 (void) setuid(RealUid); 1060 } 1061 } 1062 } 1063 if (tTd(37, 1)) 1064 printf("\n"); 1065 1066 switch (opt) 1067 { 1068 case '7': /* force seven-bit input */ 1069 SevenBit = atobool(val); 1070 break; 1071 1072 case 'A': /* set default alias file */ 1073 if (val[0] == '\0') 1074 setalias("aliases"); 1075 else 1076 setalias(val); 1077 break; 1078 1079 case 'a': /* look N minutes for "@:@" in alias file */ 1080 if (val[0] == '\0') 1081 SafeAlias = 5 * 60; /* five minutes */ 1082 else 1083 SafeAlias = convtime(val, 'm'); 1084 break; 1085 1086 case 'B': /* substitution for blank character */ 1087 SpaceSub = val[0]; 1088 if (SpaceSub == '\0') 1089 SpaceSub = ' '; 1090 break; 1091 1092 case 'b': /* min blocks free on queue fs/max msg size */ 1093 p = strchr(val, '/'); 1094 if (p != NULL) 1095 { 1096 *p++ = '\0'; 1097 MaxMessageSize = atol(p); 1098 } 1099 MinBlocksFree = atol(val); 1100 break; 1101 1102 case 'c': /* don't connect to "expensive" mailers */ 1103 NoConnect = atobool(val); 1104 break; 1105 1106 case 'C': /* checkpoint every N addresses */ 1107 CheckpointInterval = atoi(val); 1108 break; 1109 1110 case 'd': /* delivery mode */ 1111 switch (*val) 1112 { 1113 case '\0': 1114 e->e_sendmode = SM_DELIVER; 1115 break; 1116 1117 case SM_QUEUE: /* queue only */ 1118 #ifndef QUEUE 1119 syserr("need QUEUE to set -odqueue"); 1120 #endif /* QUEUE */ 1121 /* fall through..... */ 1122 1123 case SM_DELIVER: /* do everything */ 1124 case SM_FORK: /* fork after verification */ 1125 e->e_sendmode = *val; 1126 break; 1127 1128 default: 1129 syserr("Unknown delivery mode %c", *val); 1130 exit(EX_USAGE); 1131 } 1132 break; 1133 1134 case 'D': /* rebuild alias database as needed */ 1135 AutoRebuild = atobool(val); 1136 break; 1137 1138 case 'E': /* error message header/header file */ 1139 if (*val != '\0') 1140 ErrMsgFile = newstr(val); 1141 break; 1142 1143 case 'e': /* set error processing mode */ 1144 switch (*val) 1145 { 1146 case EM_QUIET: /* be silent about it */ 1147 case EM_MAIL: /* mail back */ 1148 case EM_BERKNET: /* do berknet error processing */ 1149 case EM_WRITE: /* write back (or mail) */ 1150 HoldErrs = TRUE; 1151 /* fall through... */ 1152 1153 case EM_PRINT: /* print errors normally (default) */ 1154 e->e_errormode = *val; 1155 break; 1156 } 1157 break; 1158 1159 case 'F': /* file mode */ 1160 FileMode = atooct(val) & 0777; 1161 break; 1162 1163 case 'f': /* save Unix-style From lines on front */ 1164 SaveFrom = atobool(val); 1165 break; 1166 1167 case 'G': /* match recipients against GECOS field */ 1168 MatchGecos = atobool(val); 1169 break; 1170 1171 case 'g': /* default gid */ 1172 if (isascii(*val) && isdigit(*val)) 1173 DefGid = atoi(val); 1174 else 1175 { 1176 register struct group *gr; 1177 1178 DefGid = -1; 1179 gr = getgrnam(val); 1180 if (gr == NULL) 1181 syserr("readcf: option g: unknown group %s", val); 1182 else 1183 DefGid = gr->gr_gid; 1184 } 1185 break; 1186 1187 case 'H': /* help file */ 1188 if (val[0] == '\0') 1189 HelpFile = "sendmail.hf"; 1190 else 1191 HelpFile = newstr(val); 1192 break; 1193 1194 case 'h': /* maximum hop count */ 1195 MaxHopCount = atoi(val); 1196 break; 1197 1198 case 'I': /* use internet domain name server */ 1199 #ifdef NAMED_BIND 1200 UseNameServer = TRUE; 1201 for (p = val; *p != 0; ) 1202 { 1203 bool clearmode; 1204 char *q; 1205 struct resolverflags *rfp; 1206 1207 while (*p == ' ') 1208 p++; 1209 if (*p == '\0') 1210 break; 1211 clearmode = FALSE; 1212 if (*p == '-') 1213 clearmode = TRUE; 1214 else if (*p != '+') 1215 p--; 1216 p++; 1217 q = p; 1218 while (*p != '\0' && !(isascii(*p) && isspace(*p))) 1219 p++; 1220 if (*p != '\0') 1221 *p++ = '\0'; 1222 for (rfp = ResolverFlags; rfp->rf_name != NULL; rfp++) 1223 { 1224 if (strcasecmp(q, rfp->rf_name) == 0) 1225 break; 1226 } 1227 if (rfp->rf_name == NULL) 1228 syserr("readcf: I option value %s unrecognized", q); 1229 else if (clearmode) 1230 _res.options &= ~rfp->rf_bits; 1231 else 1232 _res.options |= rfp->rf_bits; 1233 } 1234 if (tTd(8, 2)) 1235 printf("_res.options = %x\n", _res.options); 1236 #else 1237 usrerr("name server (I option) specified but BIND not compiled in"); 1238 #endif 1239 break; 1240 1241 case 'i': /* ignore dot lines in message */ 1242 IgnrDot = atobool(val); 1243 break; 1244 1245 case 'j': /* send errors in MIME (RFC 1341) format */ 1246 SendMIMEErrors = atobool(val); 1247 break; 1248 1249 case 'J': /* .forward search path */ 1250 ForwardPath = newstr(val); 1251 break; 1252 1253 case 'k': /* connection cache size */ 1254 MaxMciCache = atoi(val); 1255 if (MaxMciCache < 0) 1256 MaxMciCache = 0; 1257 break; 1258 1259 case 'K': /* connection cache timeout */ 1260 MciCacheTimeout = convtime(val, 'm'); 1261 break; 1262 1263 case 'l': /* use Errors-To: header */ 1264 UseErrorsTo = atobool(val); 1265 break; 1266 1267 case 'L': /* log level */ 1268 if (safe || LogLevel < atoi(val)) 1269 LogLevel = atoi(val); 1270 break; 1271 1272 case 'M': /* define macro */ 1273 define(val[0], newstr(&val[1]), CurEnv); 1274 sticky = FALSE; 1275 break; 1276 1277 case 'm': /* send to me too */ 1278 MeToo = atobool(val); 1279 break; 1280 1281 case 'n': /* validate RHS in newaliases */ 1282 CheckAliases = atobool(val); 1283 break; 1284 1285 /* 'N' available -- was "net name" */ 1286 1287 case 'O': /* daemon options */ 1288 setdaemonoptions(val); 1289 break; 1290 1291 case 'o': /* assume old style headers */ 1292 if (atobool(val)) 1293 CurEnv->e_flags |= EF_OLDSTYLE; 1294 else 1295 CurEnv->e_flags &= ~EF_OLDSTYLE; 1296 break; 1297 1298 case 'p': /* select privacy level */ 1299 p = val; 1300 for (;;) 1301 { 1302 register struct prival *pv; 1303 extern struct prival PrivacyValues[]; 1304 1305 while (isascii(*p) && (isspace(*p) || ispunct(*p))) 1306 p++; 1307 if (*p == '\0') 1308 break; 1309 val = p; 1310 while (isascii(*p) && isalnum(*p)) 1311 p++; 1312 if (*p != '\0') 1313 *p++ = '\0'; 1314 1315 for (pv = PrivacyValues; pv->pv_name != NULL; pv++) 1316 { 1317 if (strcasecmp(val, pv->pv_name) == 0) 1318 break; 1319 } 1320 if (pv->pv_name == NULL) 1321 syserr("readcf: Op line: %s unrecognized", val); 1322 PrivacyFlags |= pv->pv_flag; 1323 } 1324 break; 1325 1326 case 'P': /* postmaster copy address for returned mail */ 1327 PostMasterCopy = newstr(val); 1328 break; 1329 1330 case 'q': /* slope of queue only function */ 1331 QueueFactor = atoi(val); 1332 break; 1333 1334 case 'Q': /* queue directory */ 1335 if (val[0] == '\0') 1336 QueueDir = "mqueue"; 1337 else 1338 QueueDir = newstr(val); 1339 if (RealUid != 0 && !safe) 1340 Warn_Q_option = TRUE; 1341 break; 1342 1343 case 'R': /* don't prune routes */ 1344 DontPruneRoutes = atobool(val); 1345 break; 1346 1347 case 'r': /* read timeout */ 1348 settimeouts(val); 1349 break; 1350 1351 case 'S': /* status file */ 1352 if (val[0] == '\0') 1353 StatFile = "sendmail.st"; 1354 else 1355 StatFile = newstr(val); 1356 break; 1357 1358 case 's': /* be super safe, even if expensive */ 1359 SuperSafe = atobool(val); 1360 break; 1361 1362 case 'T': /* queue timeout */ 1363 p = strchr(val, '/'); 1364 if (p != NULL) 1365 { 1366 *p++ = '\0'; 1367 TimeOuts.to_q_warning = convtime(p, 'd'); 1368 } 1369 TimeOuts.to_q_return = convtime(val, 'h'); 1370 break; 1371 1372 case 't': /* time zone name */ 1373 TimeZoneSpec = newstr(val); 1374 break; 1375 1376 case 'U': /* location of user database */ 1377 UdbSpec = newstr(val); 1378 break; 1379 1380 case 'u': /* set default uid */ 1381 if (isascii(*val) && isdigit(*val)) 1382 DefUid = atoi(val); 1383 else 1384 { 1385 register struct passwd *pw; 1386 1387 DefUid = -1; 1388 pw = getpwnam(val); 1389 if (pw == NULL) 1390 syserr("readcf: option u: unknown user %s", val); 1391 else 1392 DefUid = pw->pw_uid; 1393 } 1394 setdefuser(); 1395 break; 1396 1397 case 'V': /* fallback MX host */ 1398 FallBackMX = newstr(val); 1399 break; 1400 1401 case 'v': /* run in verbose mode */ 1402 Verbose = atobool(val); 1403 break; 1404 1405 case 'w': /* if we are best MX, try host directly */ 1406 TryNullMXList = atobool(val); 1407 break; 1408 1409 /* 'W' available -- was wizard password */ 1410 1411 case 'x': /* load avg at which to auto-queue msgs */ 1412 QueueLA = atoi(val); 1413 break; 1414 1415 case 'X': /* load avg at which to auto-reject connections */ 1416 RefuseLA = atoi(val); 1417 break; 1418 1419 case 'y': /* work recipient factor */ 1420 WkRecipFact = atoi(val); 1421 break; 1422 1423 case 'Y': /* fork jobs during queue runs */ 1424 ForkQueueRuns = atobool(val); 1425 break; 1426 1427 case 'z': /* work message class factor */ 1428 WkClassFact = atoi(val); 1429 break; 1430 1431 case 'Z': /* work time factor */ 1432 WkTimeFact = atoi(val); 1433 break; 1434 1435 default: 1436 break; 1437 } 1438 if (sticky) 1439 setbitn(opt, StickyOpt); 1440 return; 1441 } 1442 /* 1443 ** SETCLASS -- set a word into a class 1444 ** 1445 ** Parameters: 1446 ** class -- the class to put the word in. 1447 ** word -- the word to enter 1448 ** 1449 ** Returns: 1450 ** none. 1451 ** 1452 ** Side Effects: 1453 ** puts the word into the symbol table. 1454 */ 1455 1456 setclass(class, word) 1457 int class; 1458 char *word; 1459 { 1460 register STAB *s; 1461 1462 if (tTd(37, 8)) 1463 printf("setclass(%c, %s)\n", class, word); 1464 s = stab(word, ST_CLASS, ST_ENTER); 1465 setbitn(class, s->s_class); 1466 } 1467 /* 1468 ** MAKEMAPENTRY -- create a map entry 1469 ** 1470 ** Parameters: 1471 ** line -- the config file line 1472 ** 1473 ** Returns: 1474 ** TRUE if it successfully entered the map entry. 1475 ** FALSE otherwise (usually syntax error). 1476 ** 1477 ** Side Effects: 1478 ** Enters the map into the dictionary. 1479 */ 1480 1481 void 1482 makemapentry(line) 1483 char *line; 1484 { 1485 register char *p; 1486 char *mapname; 1487 char *classname; 1488 register STAB *s; 1489 STAB *class; 1490 1491 for (p = line; isascii(*p) && isspace(*p); p++) 1492 continue; 1493 if (!(isascii(*p) && isalnum(*p))) 1494 { 1495 syserr("readcf: config K line: no map name"); 1496 return; 1497 } 1498 1499 mapname = p; 1500 while (isascii(*++p) && isalnum(*p)) 1501 continue; 1502 if (*p != '\0') 1503 *p++ = '\0'; 1504 while (isascii(*p) && isspace(*p)) 1505 p++; 1506 if (!(isascii(*p) && isalnum(*p))) 1507 { 1508 syserr("readcf: config K line, map %s: no map class", mapname); 1509 return; 1510 } 1511 classname = p; 1512 while (isascii(*++p) && isalnum(*p)) 1513 continue; 1514 if (*p != '\0') 1515 *p++ = '\0'; 1516 while (isascii(*p) && isspace(*p)) 1517 p++; 1518 1519 /* look up the class */ 1520 class = stab(classname, ST_MAPCLASS, ST_FIND); 1521 if (class == NULL) 1522 { 1523 syserr("readcf: map %s: class %s not available", mapname, classname); 1524 return; 1525 } 1526 1527 /* enter the map */ 1528 s = stab(mapname, ST_MAP, ST_ENTER); 1529 s->s_map.map_class = &class->s_mapclass; 1530 s->s_map.map_mname = newstr(mapname); 1531 1532 if (class->s_mapclass.map_parse(&s->s_map, p)) 1533 s->s_map.map_mflags |= MF_VALID; 1534 1535 if (tTd(37, 5)) 1536 { 1537 printf("map %s, class %s, flags %x, file %s,\n", 1538 s->s_map.map_mname, s->s_map.map_class->map_cname, 1539 s->s_map.map_mflags, 1540 s->s_map.map_file == NULL ? "(null)" : s->s_map.map_file); 1541 printf("\tapp %s, domain %s, rebuild %s\n", 1542 s->s_map.map_app == NULL ? "(null)" : s->s_map.map_app, 1543 s->s_map.map_domain == NULL ? "(null)" : s->s_map.map_domain, 1544 s->s_map.map_rebuild == NULL ? "(null)" : s->s_map.map_rebuild); 1545 } 1546 } 1547 /* 1548 ** SETTIMEOUTS -- parse and set timeout values 1549 ** 1550 ** Parameters: 1551 ** val -- a pointer to the values. If NULL, do initial 1552 ** settings. 1553 ** 1554 ** Returns: 1555 ** none. 1556 ** 1557 ** Side Effects: 1558 ** Initializes the TimeOuts structure 1559 */ 1560 1561 #define SECONDS 1562 #define MINUTES * 60 1563 #define HOUR * 3600 1564 1565 settimeouts(val) 1566 register char *val; 1567 { 1568 register char *p; 1569 extern time_t convtime(); 1570 1571 if (val == NULL) 1572 { 1573 TimeOuts.to_initial = (time_t) 5 MINUTES; 1574 TimeOuts.to_helo = (time_t) 5 MINUTES; 1575 TimeOuts.to_mail = (time_t) 10 MINUTES; 1576 TimeOuts.to_rcpt = (time_t) 1 HOUR; 1577 TimeOuts.to_datainit = (time_t) 5 MINUTES; 1578 TimeOuts.to_datablock = (time_t) 1 HOUR; 1579 TimeOuts.to_datafinal = (time_t) 1 HOUR; 1580 TimeOuts.to_rset = (time_t) 5 MINUTES; 1581 TimeOuts.to_quit = (time_t) 2 MINUTES; 1582 TimeOuts.to_nextcommand = (time_t) 1 HOUR; 1583 TimeOuts.to_miscshort = (time_t) 2 MINUTES; 1584 TimeOuts.to_ident = (time_t) 30 SECONDS; 1585 return; 1586 } 1587 1588 for (;; val = p) 1589 { 1590 while (isascii(*val) && isspace(*val)) 1591 val++; 1592 if (*val == '\0') 1593 break; 1594 for (p = val; *p != '\0' && *p != ','; p++) 1595 continue; 1596 if (*p != '\0') 1597 *p++ = '\0'; 1598 1599 if (isascii(*val) && isdigit(*val)) 1600 { 1601 /* old syntax -- set everything */ 1602 TimeOuts.to_mail = convtime(val, 'm'); 1603 TimeOuts.to_rcpt = TimeOuts.to_mail; 1604 TimeOuts.to_datainit = TimeOuts.to_mail; 1605 TimeOuts.to_datablock = TimeOuts.to_mail; 1606 TimeOuts.to_datafinal = TimeOuts.to_mail; 1607 TimeOuts.to_nextcommand = TimeOuts.to_mail; 1608 continue; 1609 } 1610 else 1611 { 1612 register char *q = strchr(val, '='); 1613 time_t to; 1614 1615 if (q == NULL) 1616 { 1617 /* syntax error */ 1618 continue; 1619 } 1620 *q++ = '\0'; 1621 to = convtime(q, 'm'); 1622 1623 if (strcasecmp(val, "initial") == 0) 1624 TimeOuts.to_initial = to; 1625 else if (strcasecmp(val, "mail") == 0) 1626 TimeOuts.to_mail = to; 1627 else if (strcasecmp(val, "rcpt") == 0) 1628 TimeOuts.to_rcpt = to; 1629 else if (strcasecmp(val, "datainit") == 0) 1630 TimeOuts.to_datainit = to; 1631 else if (strcasecmp(val, "datablock") == 0) 1632 TimeOuts.to_datablock = to; 1633 else if (strcasecmp(val, "datafinal") == 0) 1634 TimeOuts.to_datafinal = to; 1635 else if (strcasecmp(val, "command") == 0) 1636 TimeOuts.to_nextcommand = to; 1637 else if (strcasecmp(val, "rset") == 0) 1638 TimeOuts.to_rset = to; 1639 else if (strcasecmp(val, "helo") == 0) 1640 TimeOuts.to_helo = to; 1641 else if (strcasecmp(val, "quit") == 0) 1642 TimeOuts.to_quit = to; 1643 else if (strcasecmp(val, "misc") == 0) 1644 TimeOuts.to_miscshort = to; 1645 else if (strcasecmp(val, "ident") == 0) 1646 TimeOuts.to_ident = to; 1647 else 1648 syserr("settimeouts: invalid timeout %s", val); 1649 } 1650 } 1651 } 1652