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