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