122709Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 333731Sbostic * Copyright (c) 1988 Regents of the University of California. 433731Sbostic * All rights reserved. 533731Sbostic * 642829Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822709Sdist 922709Sdist #ifndef lint 10*53654Seric static char sccsid[] = "@(#)readcf.c 5.37 (Berkeley) 05/25/92"; 1133731Sbostic #endif /* not lint */ 1222709Sdist 133313Seric # include "sendmail.h" 1452647Seric # include <sys/stat.h> 153308Seric 163308Seric /* 173308Seric ** READCF -- read control file. 183308Seric ** 193308Seric ** This routine reads the control file and builds the internal 203308Seric ** form. 213308Seric ** 224432Seric ** The file is formatted as a sequence of lines, each taken 234432Seric ** atomically. The first character of each line describes how 244432Seric ** the line is to be interpreted. The lines are: 254432Seric ** Dxval Define macro x to have value val. 264432Seric ** Cxword Put word into class x. 274432Seric ** Fxfile [fmt] Read file for lines to put into 284432Seric ** class x. Use scanf string 'fmt' 294432Seric ** or "%s" if not present. Fmt should 304432Seric ** only produce one string-valued result. 314432Seric ** Hname: value Define header with field-name 'name' 324432Seric ** and value as specified; this will be 334432Seric ** macro expanded immediately before 344432Seric ** use. 354432Seric ** Sn Use rewriting set n. 364432Seric ** Rlhs rhs Rewrite addresses that match lhs to 374432Seric ** be rhs. 3824944Seric ** Mn arg=val... Define mailer. n is the internal name. 3924944Seric ** Args specify mailer parameters. 408252Seric ** Oxvalue Set option x to value. 418252Seric ** Pname=value Set precedence name to value. 4252645Seric ** Vversioncode Version level of configuration syntax. 43*53654Seric ** Kmapname mapclass arguments.... 44*53654Seric ** Define keyed lookup of a given class. 45*53654Seric ** Arguments are class dependent. 464432Seric ** 473308Seric ** Parameters: 483308Seric ** cfname -- control file name. 493308Seric ** 503308Seric ** Returns: 513308Seric ** none. 523308Seric ** 533308Seric ** Side Effects: 543308Seric ** Builds several internal tables. 553308Seric */ 563308Seric 5721066Seric readcf(cfname) 583308Seric char *cfname; 593308Seric { 603308Seric FILE *cf; 618547Seric int ruleset = 0; 628547Seric char *q; 638547Seric char **pv; 649350Seric struct rewrite *rwp = NULL; 653308Seric char buf[MAXLINE]; 663308Seric register char *p; 673308Seric extern char **prescan(); 683308Seric extern char **copyplist(); 6952647Seric struct stat statb; 705909Seric char exbuf[MAXLINE]; 7116915Seric char pvpbuf[PSBUFSIZE]; 729350Seric extern char *fgetfolded(); 7310709Seric extern char *munchstring(); 74*53654Seric extern void makemapentry(); 753308Seric 7652647Seric FileName = cfname; 7752647Seric LineNumber = 0; 7852647Seric 793308Seric cf = fopen(cfname, "r"); 803308Seric if (cf == NULL) 813308Seric { 8252647Seric syserr("cannot open"); 833308Seric exit(EX_OSFILE); 843308Seric } 853308Seric 8652647Seric if (fstat(fileno(cf), &statb) < 0) 8752647Seric { 8852647Seric syserr("cannot fstat"); 8952647Seric exit(EX_OSFILE); 9052647Seric } 9152647Seric 9252647Seric if (!S_ISREG(statb.st_mode)) 9352647Seric { 9452647Seric syserr("not a plain file"); 9552647Seric exit(EX_OSFILE); 9652647Seric } 9752647Seric 9852647Seric if (OpMode != MD_TEST && bitset(S_IWGRP|S_IWOTH, statb.st_mode)) 9952647Seric { 10053037Seric if (OpMode == MD_DAEMON || OpMode == MD_FREEZE) 10153037Seric fprintf(stderr, "%s: WARNING: dangerous write permissions\n", 10253037Seric FileName); 10353037Seric #ifdef LOG 10453037Seric if (LogLevel > 0) 10553037Seric syslog(LOG_CRIT, "%s: WARNING: dangerous write permissions", 10653037Seric FileName); 10753037Seric #endif 10852647Seric } 10952647Seric 1107854Seric while (fgetfolded(buf, sizeof buf, cf) != NULL) 1113308Seric { 11252637Seric if (buf[0] == '#') 11352637Seric continue; 11452637Seric 11516157Seric /* map $ into \001 (ASCII SOH) for macro expansion */ 11616157Seric for (p = buf; *p != '\0'; p++) 11716157Seric { 11852647Seric if (*p == '#' && p > buf && ConfigLevel >= 3) 11952647Seric { 12052647Seric /* this is an on-line comment */ 12152647Seric register char *e; 12252647Seric 12352647Seric switch (*--p) 12452647Seric { 12552647Seric case '\001': 12652647Seric /* it's from $# -- let it go through */ 12752647Seric p++; 12852647Seric break; 12952647Seric 13052647Seric case '\\': 13152647Seric /* it's backslash escaped */ 13252647Seric (void) strcpy(p, p + 1); 13352647Seric break; 13452647Seric 13552647Seric default: 13652647Seric /* delete preceeding white space */ 13752647Seric while (isspace(*p) && p > buf) 13852647Seric p--; 13952647Seric if ((e = index(++p, '\n')) != NULL) 14052647Seric (void) strcpy(p, e); 14152647Seric else 14252647Seric p[0] = p[1] = '\0'; 14352647Seric break; 14452647Seric } 14552647Seric continue; 14652647Seric } 14752647Seric 14816157Seric if (*p != '$') 14916157Seric continue; 15016157Seric 15116157Seric if (p[1] == '$') 15216157Seric { 15316157Seric /* actual dollar sign.... */ 15423111Seric (void) strcpy(p, p + 1); 15516157Seric continue; 15616157Seric } 15716157Seric 15816157Seric /* convert to macro expansion character */ 15916157Seric *p = '\001'; 16016157Seric } 16116157Seric 16216157Seric /* interpret this line */ 1633308Seric switch (buf[0]) 1643308Seric { 1653308Seric case '\0': 1663308Seric case '#': /* comment */ 1673308Seric break; 1683308Seric 1693308Seric case 'R': /* rewriting rule */ 1703308Seric for (p = &buf[1]; *p != '\0' && *p != '\t'; p++) 1713308Seric continue; 1723308Seric 1733308Seric if (*p == '\0') 1745909Seric { 1759381Seric syserr("invalid rewrite line \"%s\"", buf); 1765909Seric break; 1775909Seric } 1785909Seric 1795909Seric /* allocate space for the rule header */ 1805909Seric if (rwp == NULL) 1815909Seric { 1825909Seric RewriteRules[ruleset] = rwp = 1835909Seric (struct rewrite *) xalloc(sizeof *rwp); 1845909Seric } 1853308Seric else 1863308Seric { 1875909Seric rwp->r_next = (struct rewrite *) xalloc(sizeof *rwp); 1885909Seric rwp = rwp->r_next; 1895909Seric } 1905909Seric rwp->r_next = NULL; 1913308Seric 1925909Seric /* expand and save the LHS */ 1935909Seric *p = '\0'; 1946991Seric expand(&buf[1], exbuf, &exbuf[sizeof exbuf], CurEnv); 19516915Seric rwp->r_lhs = prescan(exbuf, '\t', pvpbuf); 1965909Seric if (rwp->r_lhs != NULL) 1975909Seric rwp->r_lhs = copyplist(rwp->r_lhs, TRUE); 1985909Seric 1995909Seric /* expand and save the RHS */ 2005909Seric while (*++p == '\t') 2015909Seric continue; 2027231Seric q = p; 2037231Seric while (*p != '\0' && *p != '\t') 2047231Seric p++; 2057231Seric *p = '\0'; 2067231Seric expand(q, exbuf, &exbuf[sizeof exbuf], CurEnv); 20716915Seric rwp->r_rhs = prescan(exbuf, '\t', pvpbuf); 2085909Seric if (rwp->r_rhs != NULL) 2095909Seric rwp->r_rhs = copyplist(rwp->r_rhs, TRUE); 2103308Seric break; 2113308Seric 2124072Seric case 'S': /* select rewriting set */ 2134072Seric ruleset = atoi(&buf[1]); 2148056Seric if (ruleset >= MAXRWSETS || ruleset < 0) 2158056Seric { 2169381Seric syserr("bad ruleset %d (%d max)", ruleset, MAXRWSETS); 2178056Seric ruleset = 0; 2188056Seric } 2194072Seric rwp = NULL; 2204072Seric break; 2214072Seric 2223308Seric case 'D': /* macro definition */ 22310709Seric define(buf[1], newstr(munchstring(&buf[2])), CurEnv); 2243308Seric break; 2253308Seric 2263387Seric case 'H': /* required header line */ 2274088Seric (void) chompheader(&buf[1], TRUE); 2283387Seric break; 2293387Seric 2304061Seric case 'C': /* word class */ 2314432Seric case 'F': /* word class from file */ 2324432Seric /* read list of words from argument or file */ 2334432Seric if (buf[0] == 'F') 2344432Seric { 2354432Seric /* read from file */ 2364432Seric for (p = &buf[2]; *p != '\0' && !isspace(*p); p++) 2374432Seric continue; 2384432Seric if (*p == '\0') 2394432Seric p = "%s"; 2404432Seric else 2414432Seric { 2424432Seric *p = '\0'; 2434432Seric while (isspace(*++p)) 2444432Seric continue; 2454432Seric } 24610687Seric fileclass(buf[1], &buf[2], p); 2474432Seric break; 2484432Seric } 2494061Seric 2504432Seric /* scan the list of words and set class for all */ 2514061Seric for (p = &buf[2]; *p != '\0'; ) 2524061Seric { 2534061Seric register char *wd; 2544061Seric char delim; 2554061Seric 2564061Seric while (*p != '\0' && isspace(*p)) 2574061Seric p++; 2584061Seric wd = p; 2594061Seric while (*p != '\0' && !isspace(*p)) 2604061Seric p++; 2614061Seric delim = *p; 2624061Seric *p = '\0'; 2634061Seric if (wd[0] != '\0') 26410687Seric setclass(buf[1], wd); 2654061Seric *p = delim; 2664061Seric } 2674061Seric break; 2684061Seric 2694096Seric case 'M': /* define mailer */ 27021066Seric makemailer(&buf[1]); 2714096Seric break; 2724096Seric 2738252Seric case 'O': /* set option */ 27421755Seric setoption(buf[1], &buf[2], TRUE, FALSE); 2758252Seric break; 2768252Seric 2778252Seric case 'P': /* set precedence */ 2788252Seric if (NumPriorities >= MAXPRIORITIES) 2798252Seric { 2808547Seric toomany('P', MAXPRIORITIES); 2818252Seric break; 2828252Seric } 2839381Seric for (p = &buf[1]; *p != '\0' && *p != '=' && *p != '\t'; p++) 2848252Seric continue; 2858252Seric if (*p == '\0') 2868252Seric goto badline; 2878252Seric *p = '\0'; 2888252Seric Priorities[NumPriorities].pri_name = newstr(&buf[1]); 2898252Seric Priorities[NumPriorities].pri_val = atoi(++p); 2908252Seric NumPriorities++; 2918252Seric break; 2928252Seric 2938547Seric case 'T': /* trusted user(s) */ 2948547Seric p = &buf[1]; 2958547Seric while (*p != '\0') 2968547Seric { 2978547Seric while (isspace(*p)) 2988547Seric p++; 2998547Seric q = p; 3008547Seric while (*p != '\0' && !isspace(*p)) 3018547Seric p++; 3028547Seric if (*p != '\0') 3038547Seric *p++ = '\0'; 3048547Seric if (*q == '\0') 3058547Seric continue; 3068547Seric for (pv = TrustedUsers; *pv != NULL; pv++) 3078547Seric continue; 3088547Seric if (pv >= &TrustedUsers[MAXTRUST]) 3098547Seric { 3108547Seric toomany('T', MAXTRUST); 3118547Seric break; 3128547Seric } 3138547Seric *pv = newstr(q); 3148547Seric } 3158547Seric break; 3168547Seric 31752645Seric case 'V': /* configuration syntax version */ 31852645Seric ConfigLevel = atoi(&buf[1]); 31952645Seric break; 32052645Seric 321*53654Seric case 'K': 322*53654Seric makemapentry(&buf[1]); 323*53654Seric break; 324*53654Seric 3253308Seric default: 3264061Seric badline: 3279381Seric syserr("unknown control line \"%s\"", buf); 3283308Seric } 3293308Seric } 33052637Seric if (ferror(cf)) 33152637Seric { 33252647Seric syserr("I/O read error", cfname); 33352637Seric exit(EX_OSFILE); 33452637Seric } 33552637Seric fclose(cf); 3369381Seric FileName = NULL; 3374096Seric } 3384096Seric /* 3398547Seric ** TOOMANY -- signal too many of some option 3408547Seric ** 3418547Seric ** Parameters: 3428547Seric ** id -- the id of the error line 3438547Seric ** maxcnt -- the maximum possible values 3448547Seric ** 3458547Seric ** Returns: 3468547Seric ** none. 3478547Seric ** 3488547Seric ** Side Effects: 3498547Seric ** gives a syserr. 3508547Seric */ 3518547Seric 3528547Seric toomany(id, maxcnt) 3538547Seric char id; 3548547Seric int maxcnt; 3558547Seric { 3569381Seric syserr("too many %c lines, %d max", id, maxcnt); 3578547Seric } 3588547Seric /* 3594432Seric ** FILECLASS -- read members of a class from a file 3604432Seric ** 3614432Seric ** Parameters: 3624432Seric ** class -- class to define. 3634432Seric ** filename -- name of file to read. 3644432Seric ** fmt -- scanf string to use for match. 3654432Seric ** 3664432Seric ** Returns: 3674432Seric ** none 3684432Seric ** 3694432Seric ** Side Effects: 3704432Seric ** 3714432Seric ** puts all lines in filename that match a scanf into 3724432Seric ** the named class. 3734432Seric */ 3744432Seric 3754432Seric fileclass(class, filename, fmt) 3764432Seric int class; 3774432Seric char *filename; 3784432Seric char *fmt; 3794432Seric { 38025808Seric FILE *f; 3814432Seric char buf[MAXLINE]; 3824432Seric 38352062Seric if (filename[0] == '|') 38452062Seric f = popen(filename + 1, "r"); 38552062Seric else 38652062Seric f = fopen(filename, "r"); 3874432Seric if (f == NULL) 3884432Seric { 3894432Seric syserr("cannot open %s", filename); 3904432Seric return; 3914432Seric } 3924432Seric 3934432Seric while (fgets(buf, sizeof buf, f) != NULL) 3944432Seric { 3954432Seric register STAB *s; 39625808Seric register char *p; 39725808Seric # ifdef SCANF 3984432Seric char wordbuf[MAXNAME+1]; 3994432Seric 4004432Seric if (sscanf(buf, fmt, wordbuf) != 1) 4014432Seric continue; 40225808Seric p = wordbuf; 40325808Seric # else SCANF 40425808Seric p = buf; 40525808Seric # endif SCANF 40625808Seric 40725808Seric /* 40825808Seric ** Break up the match into words. 40925808Seric */ 41025808Seric 41125808Seric while (*p != '\0') 41225808Seric { 41325808Seric register char *q; 41425808Seric 41525808Seric /* strip leading spaces */ 41625808Seric while (isspace(*p)) 41725808Seric p++; 41825808Seric if (*p == '\0') 41925808Seric break; 42025808Seric 42125808Seric /* find the end of the word */ 42225808Seric q = p; 42325808Seric while (*p != '\0' && !isspace(*p)) 42425808Seric p++; 42525808Seric if (*p != '\0') 42625808Seric *p++ = '\0'; 42725808Seric 42825808Seric /* enter the word in the symbol table */ 42925808Seric s = stab(q, ST_CLASS, ST_ENTER); 43025808Seric setbitn(class, s->s_class); 43125808Seric } 4324432Seric } 4334432Seric 43452062Seric if (filename[0] == '|') 43552062Seric (void) pclose(f); 43652062Seric else 43752062Seric (void) fclose(f); 4384432Seric } 4394432Seric /* 4404096Seric ** MAKEMAILER -- define a new mailer. 4414096Seric ** 4424096Seric ** Parameters: 44310327Seric ** line -- description of mailer. This is in labeled 44410327Seric ** fields. The fields are: 44510327Seric ** P -- the path to the mailer 44610327Seric ** F -- the flags associated with the mailer 44710327Seric ** A -- the argv for this mailer 44810327Seric ** S -- the sender rewriting set 44910327Seric ** R -- the recipient rewriting set 45010327Seric ** E -- the eol string 45110327Seric ** The first word is the canonical name of the mailer. 4524096Seric ** 4534096Seric ** Returns: 4544096Seric ** none. 4554096Seric ** 4564096Seric ** Side Effects: 4574096Seric ** enters the mailer into the mailer table. 4584096Seric */ 4593308Seric 46021066Seric makemailer(line) 4614096Seric char *line; 4624096Seric { 4634096Seric register char *p; 4648067Seric register struct mailer *m; 4658067Seric register STAB *s; 4668067Seric int i; 46710327Seric char fcode; 4684096Seric extern int NextMailer; 46910327Seric extern char **makeargv(); 47010327Seric extern char *munchstring(); 47110327Seric extern char *DelimChar; 47210701Seric extern long atol(); 4734096Seric 47410327Seric /* allocate a mailer and set up defaults */ 47510327Seric m = (struct mailer *) xalloc(sizeof *m); 47610327Seric bzero((char *) m, sizeof *m); 47710327Seric m->m_mno = NextMailer; 47810327Seric m->m_eol = "\n"; 47910327Seric 48010327Seric /* collect the mailer name */ 48110327Seric for (p = line; *p != '\0' && *p != ',' && !isspace(*p); p++) 48210327Seric continue; 48310327Seric if (*p != '\0') 48410327Seric *p++ = '\0'; 48510327Seric m->m_name = newstr(line); 48610327Seric 48710327Seric /* now scan through and assign info from the fields */ 48810327Seric while (*p != '\0') 48910327Seric { 49010327Seric while (*p != '\0' && (*p == ',' || isspace(*p))) 49110327Seric p++; 49210327Seric 49310327Seric /* p now points to field code */ 49410327Seric fcode = *p; 49510327Seric while (*p != '\0' && *p != '=' && *p != ',') 49610327Seric p++; 49710327Seric if (*p++ != '=') 49810327Seric { 49952637Seric syserr("mailer %s: `=' expected", m->m_name); 50010327Seric return; 50110327Seric } 50210327Seric while (isspace(*p)) 50310327Seric p++; 50410327Seric 50510327Seric /* p now points to the field body */ 50610327Seric p = munchstring(p); 50710327Seric 50810327Seric /* install the field into the mailer struct */ 50910327Seric switch (fcode) 51010327Seric { 51110327Seric case 'P': /* pathname */ 51210327Seric m->m_mailer = newstr(p); 51310327Seric break; 51410327Seric 51510327Seric case 'F': /* flags */ 51610687Seric for (; *p != '\0'; p++) 51752637Seric if (!isspace(*p)) 51852637Seric setbitn(*p, m->m_flags); 51910327Seric break; 52010327Seric 52110327Seric case 'S': /* sender rewriting ruleset */ 52210327Seric case 'R': /* recipient rewriting ruleset */ 52310327Seric i = atoi(p); 52410327Seric if (i < 0 || i >= MAXRWSETS) 52510327Seric { 52610327Seric syserr("invalid rewrite set, %d max", MAXRWSETS); 52710327Seric return; 52810327Seric } 52910327Seric if (fcode == 'S') 53010327Seric m->m_s_rwset = i; 53110327Seric else 53210327Seric m->m_r_rwset = i; 53310327Seric break; 53410327Seric 53510327Seric case 'E': /* end of line string */ 53610327Seric m->m_eol = newstr(p); 53710327Seric break; 53810327Seric 53910327Seric case 'A': /* argument vector */ 54010327Seric m->m_argv = makeargv(p); 54110327Seric break; 54210701Seric 54310701Seric case 'M': /* maximum message size */ 54410701Seric m->m_maxsize = atol(p); 54510701Seric break; 54652106Seric 54752106Seric case 'L': /* maximum line length */ 54852106Seric m->m_linelimit = atoi(p); 54952106Seric break; 55010327Seric } 55110327Seric 55210327Seric p = DelimChar; 55310327Seric } 55410327Seric 55552106Seric /* do some heuristic cleanup for back compatibility */ 55652106Seric if (bitnset(M_LIMITS, m->m_flags)) 55752106Seric { 55852106Seric if (m->m_linelimit == 0) 55952106Seric m->m_linelimit = SMTPLINELIM; 56052106Seric if (!bitnset(M_8BITS, m->m_flags)) 56152106Seric setbitn(M_7BITS, m->m_flags); 56252106Seric } 56352106Seric 56410327Seric /* now store the mailer away */ 5654096Seric if (NextMailer >= MAXMAILERS) 5664096Seric { 5679381Seric syserr("too many mailers defined (%d max)", MAXMAILERS); 5684096Seric return; 5694096Seric } 57010327Seric Mailer[NextMailer++] = m; 57110327Seric s = stab(m->m_name, ST_MAILER, ST_ENTER); 57210327Seric s->s_mailer = m; 57310327Seric } 57410327Seric /* 57510327Seric ** MUNCHSTRING -- translate a string into internal form. 57610327Seric ** 57710327Seric ** Parameters: 57810327Seric ** p -- the string to munch. 57910327Seric ** 58010327Seric ** Returns: 58110327Seric ** the munched string. 58210327Seric ** 58310327Seric ** Side Effects: 58410327Seric ** Sets "DelimChar" to point to the string that caused us 58510327Seric ** to stop. 58610327Seric */ 5874096Seric 58810327Seric char * 58910327Seric munchstring(p) 59010327Seric register char *p; 59110327Seric { 59210327Seric register char *q; 59310327Seric bool backslash = FALSE; 59410327Seric bool quotemode = FALSE; 59510327Seric static char buf[MAXLINE]; 59610327Seric extern char *DelimChar; 5974096Seric 59810327Seric for (q = buf; *p != '\0'; p++) 5994096Seric { 60010327Seric if (backslash) 60110327Seric { 60210327Seric /* everything is roughly literal */ 60310357Seric backslash = FALSE; 60410327Seric switch (*p) 60510327Seric { 60610327Seric case 'r': /* carriage return */ 60710327Seric *q++ = '\r'; 60810327Seric continue; 60910327Seric 61010327Seric case 'n': /* newline */ 61110327Seric *q++ = '\n'; 61210327Seric continue; 61310327Seric 61410327Seric case 'f': /* form feed */ 61510327Seric *q++ = '\f'; 61610327Seric continue; 61710327Seric 61810327Seric case 'b': /* backspace */ 61910327Seric *q++ = '\b'; 62010327Seric continue; 62110327Seric } 62210327Seric *q++ = *p; 62310327Seric } 62410327Seric else 62510327Seric { 62610327Seric if (*p == '\\') 62710327Seric backslash = TRUE; 62810327Seric else if (*p == '"') 62910327Seric quotemode = !quotemode; 63010327Seric else if (quotemode || *p != ',') 63110327Seric *q++ = *p; 63210327Seric else 63310327Seric break; 63410327Seric } 6354096Seric } 6364096Seric 63710327Seric DelimChar = p; 63810327Seric *q++ = '\0'; 63910327Seric return (buf); 64010327Seric } 64110327Seric /* 64210327Seric ** MAKEARGV -- break up a string into words 64310327Seric ** 64410327Seric ** Parameters: 64510327Seric ** p -- the string to break up. 64610327Seric ** 64710327Seric ** Returns: 64810327Seric ** a char **argv (dynamically allocated) 64910327Seric ** 65010327Seric ** Side Effects: 65110327Seric ** munges p. 65210327Seric */ 6534096Seric 65410327Seric char ** 65510327Seric makeargv(p) 65610327Seric register char *p; 65710327Seric { 65810327Seric char *q; 65910327Seric int i; 66010327Seric char **avp; 66110327Seric char *argv[MAXPV + 1]; 66210327Seric 66310327Seric /* take apart the words */ 66410327Seric i = 0; 66510327Seric while (*p != '\0' && i < MAXPV) 6664096Seric { 66710327Seric q = p; 66810327Seric while (*p != '\0' && !isspace(*p)) 66910327Seric p++; 67010327Seric while (isspace(*p)) 67110327Seric *p++ = '\0'; 67210327Seric argv[i++] = newstr(q); 6734096Seric } 67410327Seric argv[i++] = NULL; 6754096Seric 67610327Seric /* now make a copy of the argv */ 67710327Seric avp = (char **) xalloc(sizeof *avp * i); 67816893Seric bcopy((char *) argv, (char *) avp, sizeof *avp * i); 67910327Seric 68010327Seric return (avp); 6813308Seric } 6823308Seric /* 6833308Seric ** PRINTRULES -- print rewrite rules (for debugging) 6843308Seric ** 6853308Seric ** Parameters: 6863308Seric ** none. 6873308Seric ** 6883308Seric ** Returns: 6893308Seric ** none. 6903308Seric ** 6913308Seric ** Side Effects: 6923308Seric ** prints rewrite rules. 6933308Seric */ 6943308Seric 6953308Seric printrules() 6963308Seric { 6973308Seric register struct rewrite *rwp; 6984072Seric register int ruleset; 6993308Seric 7004072Seric for (ruleset = 0; ruleset < 10; ruleset++) 7013308Seric { 7024072Seric if (RewriteRules[ruleset] == NULL) 7034072Seric continue; 7048067Seric printf("\n----Rule Set %d:", ruleset); 7053308Seric 7064072Seric for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next) 7073308Seric { 7088067Seric printf("\nLHS:"); 7098067Seric printav(rwp->r_lhs); 7108067Seric printf("RHS:"); 7118067Seric printav(rwp->r_rhs); 7123308Seric } 7133308Seric } 7143308Seric } 7154319Seric 7164096Seric /* 7178256Seric ** SETOPTION -- set global processing option 7188256Seric ** 7198256Seric ** Parameters: 7208256Seric ** opt -- option name. 7218256Seric ** val -- option value (as a text string). 72221755Seric ** safe -- set if this came from a configuration file. 72321755Seric ** Some options (if set from the command line) will 72421755Seric ** reset the user id to avoid security problems. 7258269Seric ** sticky -- if set, don't let other setoptions override 7268269Seric ** this value. 7278256Seric ** 7288256Seric ** Returns: 7298256Seric ** none. 7308256Seric ** 7318256Seric ** Side Effects: 7328256Seric ** Sets options as implied by the arguments. 7338256Seric */ 7348256Seric 73510687Seric static BITMAP StickyOpt; /* set if option is stuck */ 7368269Seric 73721755Seric setoption(opt, val, safe, sticky) 7388256Seric char opt; 7398256Seric char *val; 74021755Seric bool safe; 7418269Seric bool sticky; 7428256Seric { 7438265Seric extern bool atobool(); 74412633Seric extern time_t convtime(); 74514879Seric extern int QueueLA; 74614879Seric extern int RefuseLA; 74717474Seric extern bool trusteduser(); 74817474Seric extern char *username(); 7498256Seric 7508256Seric if (tTd(37, 1)) 7519341Seric printf("setoption %c=%s", opt, val); 7528256Seric 7538256Seric /* 7548269Seric ** See if this option is preset for us. 7558256Seric */ 7568256Seric 75710687Seric if (bitnset(opt, StickyOpt)) 7588269Seric { 7599341Seric if (tTd(37, 1)) 7609341Seric printf(" (ignored)\n"); 7618269Seric return; 7628269Seric } 7638269Seric 76421755Seric /* 76521755Seric ** Check to see if this option can be specified by this user. 76621755Seric */ 76721755Seric 76836238Skarels if (!safe && getuid() == 0) 76921755Seric safe = TRUE; 77053400Seric if (!safe && index("deiLmorsvC", opt) == NULL) 77121755Seric { 77239111Srick if (opt != 'M' || (val[0] != 'r' && val[0] != 's')) 77321755Seric { 77436582Sbostic if (tTd(37, 1)) 77536582Sbostic printf(" (unsafe)"); 77636582Sbostic if (getuid() != geteuid()) 77736582Sbostic { 77851210Seric if (tTd(37, 1)) 77951210Seric printf("(Resetting uid)"); 78036582Sbostic (void) setgid(getgid()); 78136582Sbostic (void) setuid(getuid()); 78236582Sbostic } 78321755Seric } 78421755Seric } 78551210Seric if (tTd(37, 1)) 78617985Seric printf("\n"); 7878269Seric 7888256Seric switch (opt) 7898256Seric { 79051312Seric case '=': /* config file generation level */ 79151312Seric ConfigLevel = atoi(val); 79251312Seric break; 79351312Seric 79452106Seric case '8': /* allow eight-bit input */ 79552106Seric EightBit = atobool(val); 79652106Seric break; 79752106Seric 7988256Seric case 'A': /* set default alias file */ 7999381Seric if (val[0] == '\0') 8008269Seric AliasFile = "aliases"; 8019381Seric else 8029381Seric AliasFile = newstr(val); 8038256Seric break; 8048256Seric 80517474Seric case 'a': /* look N minutes for "@:@" in alias file */ 80617474Seric if (val[0] == '\0') 80717474Seric SafeAlias = 5; 80817474Seric else 80917474Seric SafeAlias = atoi(val); 81017474Seric break; 81117474Seric 81216843Seric case 'B': /* substitution for blank character */ 81316843Seric SpaceSub = val[0]; 81416843Seric if (SpaceSub == '\0') 81516843Seric SpaceSub = ' '; 81616843Seric break; 81716843Seric 8189284Seric case 'c': /* don't connect to "expensive" mailers */ 8199381Seric NoConnect = atobool(val); 8209284Seric break; 8219284Seric 82251305Seric case 'C': /* checkpoint every N addresses */ 82351305Seric CheckpointInterval = atoi(val); 82424944Seric break; 82524944Seric 8269284Seric case 'd': /* delivery mode */ 8279284Seric switch (*val) 8288269Seric { 8299284Seric case '\0': 8309284Seric SendMode = SM_DELIVER; 8318269Seric break; 8328269Seric 83310755Seric case SM_QUEUE: /* queue only */ 83410755Seric #ifndef QUEUE 83510755Seric syserr("need QUEUE to set -odqueue"); 83610755Seric #endif QUEUE 83710755Seric /* fall through..... */ 83810755Seric 8399284Seric case SM_DELIVER: /* do everything */ 8409284Seric case SM_FORK: /* fork after verification */ 8419284Seric SendMode = *val; 8428269Seric break; 8438269Seric 8448269Seric default: 8459284Seric syserr("Unknown delivery mode %c", *val); 8468269Seric exit(EX_USAGE); 8478269Seric } 8488269Seric break; 8498269Seric 8509146Seric case 'D': /* rebuild alias database as needed */ 8519381Seric AutoRebuild = atobool(val); 8529146Seric break; 8539146Seric 8548269Seric case 'e': /* set error processing mode */ 8558269Seric switch (*val) 8568269Seric { 8579381Seric case EM_QUIET: /* be silent about it */ 8589381Seric case EM_MAIL: /* mail back */ 8599381Seric case EM_BERKNET: /* do berknet error processing */ 8609381Seric case EM_WRITE: /* write back (or mail) */ 8618269Seric HoldErrs = TRUE; 8629381Seric /* fall through... */ 8638269Seric 8649381Seric case EM_PRINT: /* print errors normally (default) */ 8659381Seric ErrorMode = *val; 8668269Seric break; 8678269Seric } 8688269Seric break; 8698269Seric 8709049Seric case 'F': /* file mode */ 87117975Seric FileMode = atooct(val) & 0777; 8729049Seric break; 8739049Seric 8748269Seric case 'f': /* save Unix-style From lines on front */ 8759381Seric SaveFrom = atobool(val); 8768269Seric break; 8778269Seric 8788256Seric case 'g': /* default gid */ 87917474Seric DefGid = atoi(val); 8808256Seric break; 8818256Seric 8828256Seric case 'H': /* help file */ 8839381Seric if (val[0] == '\0') 8848269Seric HelpFile = "sendmail.hf"; 8859381Seric else 8869381Seric HelpFile = newstr(val); 8878256Seric break; 8888256Seric 88951305Seric case 'h': /* maximum hop count */ 89051305Seric MaxHopCount = atoi(val); 89151305Seric break; 89251305Seric 89335651Seric case 'I': /* use internet domain name server */ 89435651Seric UseNameServer = atobool(val); 89535651Seric break; 89635651Seric 8978269Seric case 'i': /* ignore dot lines in message */ 8989381Seric IgnrDot = atobool(val); 8998269Seric break; 9008269Seric 9018256Seric case 'L': /* log level */ 9029381Seric LogLevel = atoi(val); 9038256Seric break; 9048256Seric 9058269Seric case 'M': /* define macro */ 9069381Seric define(val[0], newstr(&val[1]), CurEnv); 90716878Seric sticky = FALSE; 9088269Seric break; 9098269Seric 9108269Seric case 'm': /* send to me too */ 9119381Seric MeToo = atobool(val); 9128269Seric break; 9138269Seric 91425820Seric case 'n': /* validate RHS in newaliases */ 91525820Seric CheckAliases = atobool(val); 91625820Seric break; 91725820Seric 9188269Seric case 'o': /* assume old style headers */ 9199381Seric if (atobool(val)) 9209341Seric CurEnv->e_flags |= EF_OLDSTYLE; 9219341Seric else 9229341Seric CurEnv->e_flags &= ~EF_OLDSTYLE; 9238269Seric break; 9248269Seric 92524944Seric case 'P': /* postmaster copy address for returned mail */ 92624944Seric PostMasterCopy = newstr(val); 92724944Seric break; 92824944Seric 92924944Seric case 'q': /* slope of queue only function */ 93024944Seric QueueFactor = atoi(val); 93124944Seric break; 93224944Seric 9338256Seric case 'Q': /* queue directory */ 9349381Seric if (val[0] == '\0') 9358269Seric QueueDir = "mqueue"; 9369381Seric else 9379381Seric QueueDir = newstr(val); 9388256Seric break; 9398256Seric 9408256Seric case 'r': /* read timeout */ 9419381Seric ReadTimeout = convtime(val); 9428256Seric break; 9438256Seric 9448256Seric case 'S': /* status file */ 9459381Seric if (val[0] == '\0') 9468269Seric StatFile = "sendmail.st"; 9479381Seric else 9489381Seric StatFile = newstr(val); 9498256Seric break; 9508256Seric 9518265Seric case 's': /* be super safe, even if expensive */ 9529381Seric SuperSafe = atobool(val); 9538256Seric break; 9548256Seric 9558256Seric case 'T': /* queue timeout */ 9569381Seric TimeOut = convtime(val); 95733936Sbostic /*FALLTHROUGH*/ 9588256Seric 9598265Seric case 't': /* time zone name */ 96052106Seric TimeZoneSpec = newstr(val); 9618265Seric break; 9628265Seric 96350556Seric case 'U': /* location of user database */ 96451360Seric UdbSpec = newstr(val); 96550556Seric break; 96650556Seric 9678256Seric case 'u': /* set default uid */ 96817474Seric DefUid = atoi(val); 96940973Sbostic setdefuser(); 9708256Seric break; 9718256Seric 9728269Seric case 'v': /* run in verbose mode */ 9739381Seric Verbose = atobool(val); 9748256Seric break; 9758256Seric 97651216Seric case 'w': /* we don't have wildcard MX records */ 97751216Seric NoWildcardMX = atobool(val); 97850537Seric break; 97950537Seric 98014879Seric case 'x': /* load avg at which to auto-queue msgs */ 98114879Seric QueueLA = atoi(val); 98214879Seric break; 98314879Seric 98414879Seric case 'X': /* load avg at which to auto-reject connections */ 98514879Seric RefuseLA = atoi(val); 98614879Seric break; 98714879Seric 98824981Seric case 'y': /* work recipient factor */ 98924981Seric WkRecipFact = atoi(val); 99024981Seric break; 99124981Seric 99224981Seric case 'Y': /* fork jobs during queue runs */ 99324952Seric ForkQueueRuns = atobool(val); 99424952Seric break; 99524952Seric 99624981Seric case 'z': /* work message class factor */ 99724981Seric WkClassFact = atoi(val); 99824981Seric break; 99924981Seric 100024981Seric case 'Z': /* work time factor */ 100124981Seric WkTimeFact = atoi(val); 100224981Seric break; 100324981Seric 10048256Seric default: 10058256Seric break; 10068256Seric } 100716878Seric if (sticky) 100816878Seric setbitn(opt, StickyOpt); 10099188Seric return; 10108256Seric } 101110687Seric /* 101210687Seric ** SETCLASS -- set a word into a class 101310687Seric ** 101410687Seric ** Parameters: 101510687Seric ** class -- the class to put the word in. 101610687Seric ** word -- the word to enter 101710687Seric ** 101810687Seric ** Returns: 101910687Seric ** none. 102010687Seric ** 102110687Seric ** Side Effects: 102210687Seric ** puts the word into the symbol table. 102310687Seric */ 102410687Seric 102510687Seric setclass(class, word) 102610687Seric int class; 102710687Seric char *word; 102810687Seric { 102910687Seric register STAB *s; 103010687Seric 103110687Seric s = stab(word, ST_CLASS, ST_ENTER); 103210687Seric setbitn(class, s->s_class); 103310687Seric } 1034*53654Seric /* 1035*53654Seric ** MAKEMAPENTRY -- create a map entry 1036*53654Seric ** 1037*53654Seric ** Parameters: 1038*53654Seric ** line -- the config file line 1039*53654Seric ** 1040*53654Seric ** Returns: 1041*53654Seric ** TRUE if it successfully entered the map entry. 1042*53654Seric ** FALSE otherwise (usually syntax error). 1043*53654Seric ** 1044*53654Seric ** Side Effects: 1045*53654Seric ** Enters the map into the dictionary. 1046*53654Seric */ 1047*53654Seric 1048*53654Seric void 1049*53654Seric makemapentry(line) 1050*53654Seric char *line; 1051*53654Seric { 1052*53654Seric register char *p; 1053*53654Seric char *mapname; 1054*53654Seric char *classname; 1055*53654Seric register STAB *map; 1056*53654Seric STAB *class; 1057*53654Seric 1058*53654Seric for (p = line; isspace(*p); p++) 1059*53654Seric continue; 1060*53654Seric if (!isalnum(*p)) 1061*53654Seric { 1062*53654Seric syserr("readcf: config K line: no map name"); 1063*53654Seric return; 1064*53654Seric } 1065*53654Seric 1066*53654Seric mapname = p; 1067*53654Seric while (isalnum(*++p)) 1068*53654Seric continue; 1069*53654Seric if (*p != '\0') 1070*53654Seric *p++ = '\0'; 1071*53654Seric while (isspace(*p)) 1072*53654Seric p++; 1073*53654Seric if (!isalnum(*p)) 1074*53654Seric { 1075*53654Seric syserr("readcf: config K line, map %s: no map class", mapname); 1076*53654Seric return; 1077*53654Seric } 1078*53654Seric classname = p; 1079*53654Seric while (isalnum(*++p)) 1080*53654Seric continue; 1081*53654Seric if (*p != '\0') 1082*53654Seric *p++ = '\0'; 1083*53654Seric while (isspace(*p)) 1084*53654Seric p++; 1085*53654Seric 1086*53654Seric /* look up the class */ 1087*53654Seric class = stab(classname, ST_MAPCLASS, ST_FIND); 1088*53654Seric if (class == NULL) 1089*53654Seric { 1090*53654Seric syserr("readcf: map %s: class %s not available", mapname, classname); 1091*53654Seric return; 1092*53654Seric } 1093*53654Seric 1094*53654Seric /* enter the map */ 1095*53654Seric map = stab(mapname, ST_MAP, ST_ENTER); 1096*53654Seric map->s_map.map_class = &class->s_mapclass; 1097*53654Seric 1098*53654Seric if ((*class->s_mapclass.map_init)(&map->s_map, p)) 1099*53654Seric map->s_map.map_flags |= MF_VALID; 1100*53654Seric } 1101