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*57454Seric static char sccsid[] = "@(#)readcf.c 6.3 (Berkeley) 01/10/93"; 1133731Sbostic #endif /* not lint */ 1222709Sdist 133313Seric # include "sendmail.h" 1452647Seric # include <sys/stat.h> 1554973Seric # include <unistd.h> 1657207Seric #ifdef NAMED_BIND 1757207Seric # include <arpa/nameser.h> 1857207Seric # include <resolv.h> 1957207Seric #endif 203308Seric 213308Seric /* 223308Seric ** READCF -- read control file. 233308Seric ** 243308Seric ** This routine reads the control file and builds the internal 253308Seric ** form. 263308Seric ** 274432Seric ** The file is formatted as a sequence of lines, each taken 284432Seric ** atomically. The first character of each line describes how 294432Seric ** the line is to be interpreted. The lines are: 304432Seric ** Dxval Define macro x to have value val. 314432Seric ** Cxword Put word into class x. 324432Seric ** Fxfile [fmt] Read file for lines to put into 334432Seric ** class x. Use scanf string 'fmt' 344432Seric ** or "%s" if not present. Fmt should 354432Seric ** only produce one string-valued result. 364432Seric ** Hname: value Define header with field-name 'name' 374432Seric ** and value as specified; this will be 384432Seric ** macro expanded immediately before 394432Seric ** use. 404432Seric ** Sn Use rewriting set n. 414432Seric ** Rlhs rhs Rewrite addresses that match lhs to 424432Seric ** be rhs. 4324944Seric ** Mn arg=val... Define mailer. n is the internal name. 4424944Seric ** Args specify mailer parameters. 458252Seric ** Oxvalue Set option x to value. 468252Seric ** Pname=value Set precedence name to value. 4752645Seric ** Vversioncode Version level of configuration syntax. 4853654Seric ** Kmapname mapclass arguments.... 4953654Seric ** Define keyed lookup of a given class. 5053654Seric ** Arguments are class dependent. 514432Seric ** 523308Seric ** Parameters: 533308Seric ** cfname -- control file name. 5454973Seric ** safe -- TRUE if this is the system config file; 5554973Seric ** FALSE otherwise. 5655012Seric ** e -- the main envelope. 573308Seric ** 583308Seric ** Returns: 593308Seric ** none. 603308Seric ** 613308Seric ** Side Effects: 623308Seric ** Builds several internal tables. 633308Seric */ 643308Seric 6555012Seric readcf(cfname, safe, e) 663308Seric char *cfname; 6754973Seric bool safe; 6855012Seric register ENVELOPE *e; 693308Seric { 703308Seric FILE *cf; 718547Seric int ruleset = 0; 728547Seric char *q; 738547Seric char **pv; 749350Seric struct rewrite *rwp = NULL; 7557135Seric char *bp; 763308Seric char buf[MAXLINE]; 773308Seric register char *p; 783308Seric extern char **prescan(); 793308Seric extern char **copyplist(); 8052647Seric struct stat statb; 815909Seric char exbuf[MAXLINE]; 8216915Seric char pvpbuf[PSBUFSIZE]; 839350Seric extern char *fgetfolded(); 8410709Seric extern char *munchstring(); 8553654Seric extern void makemapentry(); 863308Seric 8752647Seric FileName = cfname; 8852647Seric LineNumber = 0; 8952647Seric 903308Seric cf = fopen(cfname, "r"); 913308Seric if (cf == NULL) 923308Seric { 9352647Seric syserr("cannot open"); 943308Seric exit(EX_OSFILE); 953308Seric } 963308Seric 9752647Seric if (fstat(fileno(cf), &statb) < 0) 9852647Seric { 9952647Seric syserr("cannot fstat"); 10052647Seric exit(EX_OSFILE); 10152647Seric } 10252647Seric 10352647Seric if (!S_ISREG(statb.st_mode)) 10452647Seric { 10552647Seric syserr("not a plain file"); 10652647Seric exit(EX_OSFILE); 10752647Seric } 10852647Seric 10952647Seric if (OpMode != MD_TEST && bitset(S_IWGRP|S_IWOTH, statb.st_mode)) 11052647Seric { 11153037Seric if (OpMode == MD_DAEMON || OpMode == MD_FREEZE) 11253037Seric fprintf(stderr, "%s: WARNING: dangerous write permissions\n", 11353037Seric FileName); 11453037Seric #ifdef LOG 11553037Seric if (LogLevel > 0) 11653037Seric syslog(LOG_CRIT, "%s: WARNING: dangerous write permissions", 11753037Seric FileName); 11853037Seric #endif 11952647Seric } 12052647Seric 12157135Seric while ((bp = fgetfolded(buf, sizeof buf, cf)) != NULL) 1223308Seric { 12357135Seric if (bp[0] == '#') 12457135Seric { 12557135Seric if (bp != buf) 12657135Seric free(bp); 12752637Seric continue; 12857135Seric } 12952637Seric 13016157Seric /* map $ into \001 (ASCII SOH) for macro expansion */ 13157135Seric for (p = bp; *p != '\0'; p++) 13216157Seric { 13357135Seric if (*p == '#' && p > bp && ConfigLevel >= 3) 13452647Seric { 13552647Seric /* this is an on-line comment */ 13652647Seric register char *e; 13752647Seric 13852647Seric switch (*--p) 13952647Seric { 14052647Seric case '\001': 14152647Seric /* it's from $# -- let it go through */ 14252647Seric p++; 14352647Seric break; 14452647Seric 14552647Seric case '\\': 14652647Seric /* it's backslash escaped */ 14752647Seric (void) strcpy(p, p + 1); 14852647Seric break; 14952647Seric 15052647Seric default: 15152647Seric /* delete preceeding white space */ 15257135Seric while (isspace(*p) && p > bp) 15352647Seric p--; 15456795Seric if ((e = strchr(++p, '\n')) != NULL) 15552647Seric (void) strcpy(p, e); 15652647Seric else 15752647Seric p[0] = p[1] = '\0'; 15852647Seric break; 15952647Seric } 16052647Seric continue; 16152647Seric } 16252647Seric 16316157Seric if (*p != '$') 16416157Seric continue; 16516157Seric 16616157Seric if (p[1] == '$') 16716157Seric { 16816157Seric /* actual dollar sign.... */ 16923111Seric (void) strcpy(p, p + 1); 17016157Seric continue; 17116157Seric } 17216157Seric 17316157Seric /* convert to macro expansion character */ 17416157Seric *p = '\001'; 17516157Seric } 17616157Seric 17716157Seric /* interpret this line */ 17857135Seric switch (bp[0]) 1793308Seric { 1803308Seric case '\0': 1813308Seric case '#': /* comment */ 1823308Seric break; 1833308Seric 1843308Seric case 'R': /* rewriting rule */ 18557135Seric for (p = &bp[1]; *p != '\0' && *p != '\t'; p++) 1863308Seric continue; 1873308Seric 1883308Seric if (*p == '\0') 1895909Seric { 19057135Seric syserr("invalid rewrite line \"%s\"", bp); 1915909Seric break; 1925909Seric } 1935909Seric 1945909Seric /* allocate space for the rule header */ 1955909Seric if (rwp == NULL) 1965909Seric { 1975909Seric RewriteRules[ruleset] = rwp = 1985909Seric (struct rewrite *) xalloc(sizeof *rwp); 1995909Seric } 2003308Seric else 2013308Seric { 2025909Seric rwp->r_next = (struct rewrite *) xalloc(sizeof *rwp); 2035909Seric rwp = rwp->r_next; 2045909Seric } 2055909Seric rwp->r_next = NULL; 2063308Seric 2075909Seric /* expand and save the LHS */ 2085909Seric *p = '\0'; 20957135Seric expand(&bp[1], exbuf, &exbuf[sizeof exbuf], e); 21016915Seric rwp->r_lhs = prescan(exbuf, '\t', pvpbuf); 2115909Seric if (rwp->r_lhs != NULL) 2125909Seric rwp->r_lhs = copyplist(rwp->r_lhs, TRUE); 21356678Seric else 21456678Seric syserr("R line: null LHS"); 2155909Seric 2165909Seric /* expand and save the RHS */ 2175909Seric while (*++p == '\t') 2185909Seric continue; 2197231Seric q = p; 2207231Seric while (*p != '\0' && *p != '\t') 2217231Seric p++; 2227231Seric *p = '\0'; 22355012Seric expand(q, exbuf, &exbuf[sizeof exbuf], e); 22416915Seric rwp->r_rhs = prescan(exbuf, '\t', pvpbuf); 2255909Seric if (rwp->r_rhs != NULL) 2265909Seric rwp->r_rhs = copyplist(rwp->r_rhs, TRUE); 22756678Seric else 22856678Seric syserr("R line: null RHS"); 2293308Seric break; 2303308Seric 2314072Seric case 'S': /* select rewriting set */ 23257135Seric ruleset = atoi(&bp[1]); 2338056Seric if (ruleset >= MAXRWSETS || ruleset < 0) 2348056Seric { 2359381Seric syserr("bad ruleset %d (%d max)", ruleset, MAXRWSETS); 2368056Seric ruleset = 0; 2378056Seric } 2384072Seric rwp = NULL; 2394072Seric break; 2404072Seric 2413308Seric case 'D': /* macro definition */ 24257135Seric define(bp[1], newstr(munchstring(&bp[2])), e); 2433308Seric break; 2443308Seric 2453387Seric case 'H': /* required header line */ 24657135Seric (void) chompheader(&bp[1], TRUE, e); 2473387Seric break; 2483387Seric 2494061Seric case 'C': /* word class */ 2504432Seric case 'F': /* word class from file */ 2514432Seric /* read list of words from argument or file */ 25257135Seric if (bp[0] == 'F') 2534432Seric { 2544432Seric /* read from file */ 25557135Seric for (p = &bp[2]; *p != '\0' && !isspace(*p); p++) 2564432Seric continue; 2574432Seric if (*p == '\0') 2584432Seric p = "%s"; 2594432Seric else 2604432Seric { 2614432Seric *p = '\0'; 2624432Seric while (isspace(*++p)) 2634432Seric continue; 2644432Seric } 26557135Seric fileclass(bp[1], &bp[2], p, safe); 2664432Seric break; 2674432Seric } 2684061Seric 2694432Seric /* scan the list of words and set class for all */ 27057135Seric for (p = &bp[2]; *p != '\0'; ) 2714061Seric { 2724061Seric register char *wd; 2734061Seric char delim; 2744061Seric 2754061Seric while (*p != '\0' && isspace(*p)) 2764061Seric p++; 2774061Seric wd = p; 2784061Seric while (*p != '\0' && !isspace(*p)) 2794061Seric p++; 2804061Seric delim = *p; 2814061Seric *p = '\0'; 2824061Seric if (wd[0] != '\0') 28357135Seric setclass(bp[1], wd); 2844061Seric *p = delim; 2854061Seric } 2864061Seric break; 2874061Seric 2884096Seric case 'M': /* define mailer */ 28957135Seric makemailer(&bp[1]); 2904096Seric break; 2914096Seric 2928252Seric case 'O': /* set option */ 29357135Seric setoption(bp[1], &bp[2], safe, FALSE); 2948252Seric break; 2958252Seric 2968252Seric case 'P': /* set precedence */ 2978252Seric if (NumPriorities >= MAXPRIORITIES) 2988252Seric { 2998547Seric toomany('P', MAXPRIORITIES); 3008252Seric break; 3018252Seric } 30257135Seric for (p = &bp[1]; *p != '\0' && *p != '=' && *p != '\t'; p++) 3038252Seric continue; 3048252Seric if (*p == '\0') 3058252Seric goto badline; 3068252Seric *p = '\0'; 30757135Seric Priorities[NumPriorities].pri_name = newstr(&bp[1]); 3088252Seric Priorities[NumPriorities].pri_val = atoi(++p); 3098252Seric NumPriorities++; 3108252Seric break; 3118252Seric 3128547Seric case 'T': /* trusted user(s) */ 31357135Seric p = &bp[1]; 3148547Seric while (*p != '\0') 3158547Seric { 3168547Seric while (isspace(*p)) 3178547Seric p++; 3188547Seric q = p; 3198547Seric while (*p != '\0' && !isspace(*p)) 3208547Seric p++; 3218547Seric if (*p != '\0') 3228547Seric *p++ = '\0'; 3238547Seric if (*q == '\0') 3248547Seric continue; 3258547Seric for (pv = TrustedUsers; *pv != NULL; pv++) 3268547Seric continue; 3278547Seric if (pv >= &TrustedUsers[MAXTRUST]) 3288547Seric { 3298547Seric toomany('T', MAXTRUST); 3308547Seric break; 3318547Seric } 3328547Seric *pv = newstr(q); 3338547Seric } 3348547Seric break; 3358547Seric 33652645Seric case 'V': /* configuration syntax version */ 33757135Seric ConfigLevel = atoi(&bp[1]); 33852645Seric break; 33952645Seric 34053654Seric case 'K': 34157135Seric makemapentry(&bp[1]); 34253654Seric break; 34353654Seric 3443308Seric default: 3454061Seric badline: 34657135Seric syserr("unknown control line \"%s\"", bp); 3473308Seric } 34857135Seric if (bp != buf) 34957135Seric free(bp); 3503308Seric } 35152637Seric if (ferror(cf)) 35252637Seric { 35352647Seric syserr("I/O read error", cfname); 35452637Seric exit(EX_OSFILE); 35552637Seric } 35652637Seric fclose(cf); 3579381Seric FileName = NULL; 35856836Seric 35957076Seric if (stab("host", ST_MAP, ST_FIND) == NULL) 36057076Seric { 36157076Seric /* user didn't initialize: set up host map */ 36257076Seric strcpy(buf, "host host"); 36357076Seric if (ConfigLevel >= 2) 36457076Seric strcat(buf, " -a."); 36557076Seric makemapentry(buf); 36657076Seric } 3674096Seric } 3684096Seric /* 3698547Seric ** TOOMANY -- signal too many of some option 3708547Seric ** 3718547Seric ** Parameters: 3728547Seric ** id -- the id of the error line 3738547Seric ** maxcnt -- the maximum possible values 3748547Seric ** 3758547Seric ** Returns: 3768547Seric ** none. 3778547Seric ** 3788547Seric ** Side Effects: 3798547Seric ** gives a syserr. 3808547Seric */ 3818547Seric 3828547Seric toomany(id, maxcnt) 3838547Seric char id; 3848547Seric int maxcnt; 3858547Seric { 3869381Seric syserr("too many %c lines, %d max", id, maxcnt); 3878547Seric } 3888547Seric /* 3894432Seric ** FILECLASS -- read members of a class from a file 3904432Seric ** 3914432Seric ** Parameters: 3924432Seric ** class -- class to define. 3934432Seric ** filename -- name of file to read. 3944432Seric ** fmt -- scanf string to use for match. 3954432Seric ** 3964432Seric ** Returns: 3974432Seric ** none 3984432Seric ** 3994432Seric ** Side Effects: 4004432Seric ** 4014432Seric ** puts all lines in filename that match a scanf into 4024432Seric ** the named class. 4034432Seric */ 4044432Seric 40554973Seric fileclass(class, filename, fmt, safe) 4064432Seric int class; 4074432Seric char *filename; 4084432Seric char *fmt; 40954973Seric bool safe; 4104432Seric { 41125808Seric FILE *f; 41254973Seric struct stat stbuf; 4134432Seric char buf[MAXLINE]; 4144432Seric 41554973Seric if (stat(filename, &stbuf) < 0) 41654973Seric { 41754973Seric syserr("fileclass: cannot stat %s", filename); 41854973Seric return; 41954973Seric } 42054973Seric if (!S_ISREG(stbuf.st_mode)) 42154973Seric { 42254973Seric syserr("fileclass: %s not a regular file", filename); 42354973Seric return; 42454973Seric } 42554973Seric if (!safe && access(filename, R_OK) < 0) 42654973Seric { 42754973Seric syserr("fileclass: access denied on %s", filename); 42854973Seric return; 42954973Seric } 43054973Seric f = fopen(filename, "r"); 4314432Seric if (f == NULL) 4324432Seric { 43354973Seric syserr("fileclass: cannot open %s", filename); 4344432Seric return; 4354432Seric } 4364432Seric 4374432Seric while (fgets(buf, sizeof buf, f) != NULL) 4384432Seric { 4394432Seric register STAB *s; 44025808Seric register char *p; 44125808Seric # ifdef SCANF 4424432Seric char wordbuf[MAXNAME+1]; 4434432Seric 4444432Seric if (sscanf(buf, fmt, wordbuf) != 1) 4454432Seric continue; 44625808Seric p = wordbuf; 44756795Seric # else /* SCANF */ 44825808Seric p = buf; 44956795Seric # endif /* SCANF */ 45025808Seric 45125808Seric /* 45225808Seric ** Break up the match into words. 45325808Seric */ 45425808Seric 45525808Seric while (*p != '\0') 45625808Seric { 45725808Seric register char *q; 45825808Seric 45925808Seric /* strip leading spaces */ 46025808Seric while (isspace(*p)) 46125808Seric p++; 46225808Seric if (*p == '\0') 46325808Seric break; 46425808Seric 46525808Seric /* find the end of the word */ 46625808Seric q = p; 46725808Seric while (*p != '\0' && !isspace(*p)) 46825808Seric p++; 46925808Seric if (*p != '\0') 47025808Seric *p++ = '\0'; 47125808Seric 47225808Seric /* enter the word in the symbol table */ 47325808Seric s = stab(q, ST_CLASS, ST_ENTER); 47425808Seric setbitn(class, s->s_class); 47525808Seric } 4764432Seric } 4774432Seric 47854973Seric (void) fclose(f); 4794432Seric } 4804432Seric /* 4814096Seric ** MAKEMAILER -- define a new mailer. 4824096Seric ** 4834096Seric ** Parameters: 48410327Seric ** line -- description of mailer. This is in labeled 48510327Seric ** fields. The fields are: 48610327Seric ** P -- the path to the mailer 48710327Seric ** F -- the flags associated with the mailer 48810327Seric ** A -- the argv for this mailer 48910327Seric ** S -- the sender rewriting set 49010327Seric ** R -- the recipient rewriting set 49110327Seric ** E -- the eol string 49210327Seric ** The first word is the canonical name of the mailer. 4934096Seric ** 4944096Seric ** Returns: 4954096Seric ** none. 4964096Seric ** 4974096Seric ** Side Effects: 4984096Seric ** enters the mailer into the mailer table. 4994096Seric */ 5003308Seric 50121066Seric makemailer(line) 5024096Seric char *line; 5034096Seric { 5044096Seric register char *p; 5058067Seric register struct mailer *m; 5068067Seric register STAB *s; 5078067Seric int i; 50810327Seric char fcode; 5094096Seric extern int NextMailer; 51010327Seric extern char **makeargv(); 51110327Seric extern char *munchstring(); 51210327Seric extern char *DelimChar; 51310701Seric extern long atol(); 5144096Seric 51510327Seric /* allocate a mailer and set up defaults */ 51610327Seric m = (struct mailer *) xalloc(sizeof *m); 51710327Seric bzero((char *) m, sizeof *m); 51810327Seric m->m_eol = "\n"; 51910327Seric 52010327Seric /* collect the mailer name */ 52110327Seric for (p = line; *p != '\0' && *p != ',' && !isspace(*p); p++) 52210327Seric continue; 52310327Seric if (*p != '\0') 52410327Seric *p++ = '\0'; 52510327Seric m->m_name = newstr(line); 52610327Seric 52710327Seric /* now scan through and assign info from the fields */ 52810327Seric while (*p != '\0') 52910327Seric { 53010327Seric while (*p != '\0' && (*p == ',' || isspace(*p))) 53110327Seric p++; 53210327Seric 53310327Seric /* p now points to field code */ 53410327Seric fcode = *p; 53510327Seric while (*p != '\0' && *p != '=' && *p != ',') 53610327Seric p++; 53710327Seric if (*p++ != '=') 53810327Seric { 53952637Seric syserr("mailer %s: `=' expected", m->m_name); 54010327Seric return; 54110327Seric } 54210327Seric while (isspace(*p)) 54310327Seric p++; 54410327Seric 54510327Seric /* p now points to the field body */ 54610327Seric p = munchstring(p); 54710327Seric 54810327Seric /* install the field into the mailer struct */ 54910327Seric switch (fcode) 55010327Seric { 55110327Seric case 'P': /* pathname */ 55210327Seric m->m_mailer = newstr(p); 55310327Seric break; 55410327Seric 55510327Seric case 'F': /* flags */ 55610687Seric for (; *p != '\0'; p++) 55752637Seric if (!isspace(*p)) 55852637Seric setbitn(*p, m->m_flags); 55910327Seric break; 56010327Seric 56110327Seric case 'S': /* sender rewriting ruleset */ 56210327Seric case 'R': /* recipient rewriting ruleset */ 56310327Seric i = atoi(p); 56410327Seric if (i < 0 || i >= MAXRWSETS) 56510327Seric { 56610327Seric syserr("invalid rewrite set, %d max", MAXRWSETS); 56710327Seric return; 56810327Seric } 56910327Seric if (fcode == 'S') 57010327Seric m->m_s_rwset = i; 57110327Seric else 57210327Seric m->m_r_rwset = i; 57310327Seric break; 57410327Seric 57510327Seric case 'E': /* end of line string */ 57610327Seric m->m_eol = newstr(p); 57710327Seric break; 57810327Seric 57910327Seric case 'A': /* argument vector */ 58010327Seric m->m_argv = makeargv(p); 58110327Seric break; 58210701Seric 58310701Seric case 'M': /* maximum message size */ 58410701Seric m->m_maxsize = atol(p); 58510701Seric break; 58652106Seric 58752106Seric case 'L': /* maximum line length */ 58852106Seric m->m_linelimit = atoi(p); 58952106Seric break; 59010327Seric } 59110327Seric 59210327Seric p = DelimChar; 59310327Seric } 59410327Seric 59552106Seric /* do some heuristic cleanup for back compatibility */ 59652106Seric if (bitnset(M_LIMITS, m->m_flags)) 59752106Seric { 59852106Seric if (m->m_linelimit == 0) 59952106Seric m->m_linelimit = SMTPLINELIM; 60055418Seric if (ConfigLevel < 2) 60152106Seric setbitn(M_7BITS, m->m_flags); 60252106Seric } 60352106Seric 6044096Seric if (NextMailer >= MAXMAILERS) 6054096Seric { 6069381Seric syserr("too many mailers defined (%d max)", MAXMAILERS); 6074096Seric return; 6084096Seric } 60957402Seric 61010327Seric s = stab(m->m_name, ST_MAILER, ST_ENTER); 61157402Seric if (s->s_mailer != NULL) 61257402Seric { 61357402Seric i = s->s_mailer->m_mno; 61457402Seric free(s->s_mailer); 61557402Seric } 61657402Seric else 61757402Seric { 61857402Seric i = NextMailer++; 61957402Seric } 62057402Seric Mailer[i] = s->s_mailer = m; 621*57454Seric m->m_mno = i; 62210327Seric } 62310327Seric /* 62410327Seric ** MUNCHSTRING -- translate a string into internal form. 62510327Seric ** 62610327Seric ** Parameters: 62710327Seric ** p -- the string to munch. 62810327Seric ** 62910327Seric ** Returns: 63010327Seric ** the munched string. 63110327Seric ** 63210327Seric ** Side Effects: 63310327Seric ** Sets "DelimChar" to point to the string that caused us 63410327Seric ** to stop. 63510327Seric */ 6364096Seric 63710327Seric char * 63810327Seric munchstring(p) 63910327Seric register char *p; 64010327Seric { 64110327Seric register char *q; 64210327Seric bool backslash = FALSE; 64310327Seric bool quotemode = FALSE; 64410327Seric static char buf[MAXLINE]; 64510327Seric extern char *DelimChar; 6464096Seric 64710327Seric for (q = buf; *p != '\0'; p++) 6484096Seric { 64910327Seric if (backslash) 65010327Seric { 65110327Seric /* everything is roughly literal */ 65210357Seric backslash = FALSE; 65310327Seric switch (*p) 65410327Seric { 65510327Seric case 'r': /* carriage return */ 65610327Seric *q++ = '\r'; 65710327Seric continue; 65810327Seric 65910327Seric case 'n': /* newline */ 66010327Seric *q++ = '\n'; 66110327Seric continue; 66210327Seric 66310327Seric case 'f': /* form feed */ 66410327Seric *q++ = '\f'; 66510327Seric continue; 66610327Seric 66710327Seric case 'b': /* backspace */ 66810327Seric *q++ = '\b'; 66910327Seric continue; 67010327Seric } 67110327Seric *q++ = *p; 67210327Seric } 67310327Seric else 67410327Seric { 67510327Seric if (*p == '\\') 67610327Seric backslash = TRUE; 67710327Seric else if (*p == '"') 67810327Seric quotemode = !quotemode; 67910327Seric else if (quotemode || *p != ',') 68010327Seric *q++ = *p; 68110327Seric else 68210327Seric break; 68310327Seric } 6844096Seric } 6854096Seric 68610327Seric DelimChar = p; 68710327Seric *q++ = '\0'; 68810327Seric return (buf); 68910327Seric } 69010327Seric /* 69110327Seric ** MAKEARGV -- break up a string into words 69210327Seric ** 69310327Seric ** Parameters: 69410327Seric ** p -- the string to break up. 69510327Seric ** 69610327Seric ** Returns: 69710327Seric ** a char **argv (dynamically allocated) 69810327Seric ** 69910327Seric ** Side Effects: 70010327Seric ** munges p. 70110327Seric */ 7024096Seric 70310327Seric char ** 70410327Seric makeargv(p) 70510327Seric register char *p; 70610327Seric { 70710327Seric char *q; 70810327Seric int i; 70910327Seric char **avp; 71010327Seric char *argv[MAXPV + 1]; 71110327Seric 71210327Seric /* take apart the words */ 71310327Seric i = 0; 71410327Seric while (*p != '\0' && i < MAXPV) 7154096Seric { 71610327Seric q = p; 71710327Seric while (*p != '\0' && !isspace(*p)) 71810327Seric p++; 71910327Seric while (isspace(*p)) 72010327Seric *p++ = '\0'; 72110327Seric argv[i++] = newstr(q); 7224096Seric } 72310327Seric argv[i++] = NULL; 7244096Seric 72510327Seric /* now make a copy of the argv */ 72610327Seric avp = (char **) xalloc(sizeof *avp * i); 72716893Seric bcopy((char *) argv, (char *) avp, sizeof *avp * i); 72810327Seric 72910327Seric return (avp); 7303308Seric } 7313308Seric /* 7323308Seric ** PRINTRULES -- print rewrite rules (for debugging) 7333308Seric ** 7343308Seric ** Parameters: 7353308Seric ** none. 7363308Seric ** 7373308Seric ** Returns: 7383308Seric ** none. 7393308Seric ** 7403308Seric ** Side Effects: 7413308Seric ** prints rewrite rules. 7423308Seric */ 7433308Seric 7443308Seric printrules() 7453308Seric { 7463308Seric register struct rewrite *rwp; 7474072Seric register int ruleset; 7483308Seric 7494072Seric for (ruleset = 0; ruleset < 10; ruleset++) 7503308Seric { 7514072Seric if (RewriteRules[ruleset] == NULL) 7524072Seric continue; 7538067Seric printf("\n----Rule Set %d:", ruleset); 7543308Seric 7554072Seric for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next) 7563308Seric { 7578067Seric printf("\nLHS:"); 7588067Seric printav(rwp->r_lhs); 7598067Seric printf("RHS:"); 7608067Seric printav(rwp->r_rhs); 7613308Seric } 7623308Seric } 7633308Seric } 7644319Seric 7654096Seric /* 7668256Seric ** SETOPTION -- set global processing option 7678256Seric ** 7688256Seric ** Parameters: 7698256Seric ** opt -- option name. 7708256Seric ** val -- option value (as a text string). 77121755Seric ** safe -- set if this came from a configuration file. 77221755Seric ** Some options (if set from the command line) will 77321755Seric ** reset the user id to avoid security problems. 7748269Seric ** sticky -- if set, don't let other setoptions override 7758269Seric ** this value. 7768256Seric ** 7778256Seric ** Returns: 7788256Seric ** none. 7798256Seric ** 7808256Seric ** Side Effects: 7818256Seric ** Sets options as implied by the arguments. 7828256Seric */ 7838256Seric 78410687Seric static BITMAP StickyOpt; /* set if option is stuck */ 7858269Seric 78657207Seric 78757207Seric #ifdef NAMED_BIND 78857207Seric 78957207Seric struct resolverflags 79057207Seric { 79157207Seric char *rf_name; /* name of the flag */ 79257207Seric long rf_bits; /* bits to set/clear */ 79357207Seric } ResolverFlags[] = 79457207Seric { 79557207Seric "debug", RES_DEBUG, 79657207Seric "aaonly", RES_AAONLY, 79757207Seric "usevc", RES_USEVC, 79857207Seric "primary", RES_PRIMARY, 79957207Seric "igntc", RES_IGNTC, 80057207Seric "recurse", RES_RECURSE, 80157207Seric "defnames", RES_DEFNAMES, 80257207Seric "stayopen", RES_STAYOPEN, 80357207Seric "dnsrch", RES_DNSRCH, 80457207Seric NULL, 0 80557207Seric }; 80657207Seric 80757207Seric #endif 80857207Seric 80921755Seric setoption(opt, val, safe, sticky) 8108256Seric char opt; 8118256Seric char *val; 81221755Seric bool safe; 8138269Seric bool sticky; 8148256Seric { 81557207Seric register char *p; 8168265Seric extern bool atobool(); 81712633Seric extern time_t convtime(); 81814879Seric extern int QueueLA; 81914879Seric extern int RefuseLA; 82017474Seric extern bool trusteduser(); 82117474Seric extern char *username(); 8228256Seric 8238256Seric if (tTd(37, 1)) 8249341Seric printf("setoption %c=%s", opt, val); 8258256Seric 8268256Seric /* 8278269Seric ** See if this option is preset for us. 8288256Seric */ 8298256Seric 83010687Seric if (bitnset(opt, StickyOpt)) 8318269Seric { 8329341Seric if (tTd(37, 1)) 8339341Seric printf(" (ignored)\n"); 8348269Seric return; 8358269Seric } 8368269Seric 83721755Seric /* 83821755Seric ** Check to see if this option can be specified by this user. 83921755Seric */ 84021755Seric 84136238Skarels if (!safe && getuid() == 0) 84221755Seric safe = TRUE; 84357136Seric if (!safe && strchr("deEiLmorsvC8", opt) == NULL) 84421755Seric { 84539111Srick if (opt != 'M' || (val[0] != 'r' && val[0] != 's')) 84621755Seric { 84736582Sbostic if (tTd(37, 1)) 84836582Sbostic printf(" (unsafe)"); 84936582Sbostic if (getuid() != geteuid()) 85036582Sbostic { 85151210Seric if (tTd(37, 1)) 85251210Seric printf("(Resetting uid)"); 85336582Sbostic (void) setgid(getgid()); 85436582Sbostic (void) setuid(getuid()); 85536582Sbostic } 85621755Seric } 85721755Seric } 85851210Seric if (tTd(37, 1)) 85917985Seric printf("\n"); 8608269Seric 8618256Seric switch (opt) 8628256Seric { 86352106Seric case '8': /* allow eight-bit input */ 86452106Seric EightBit = atobool(val); 86552106Seric break; 86652106Seric 8678256Seric case 'A': /* set default alias file */ 8689381Seric if (val[0] == '\0') 8698269Seric AliasFile = "aliases"; 8709381Seric else 8719381Seric AliasFile = newstr(val); 8728256Seric break; 8738256Seric 87417474Seric case 'a': /* look N minutes for "@:@" in alias file */ 87517474Seric if (val[0] == '\0') 87617474Seric SafeAlias = 5; 87717474Seric else 87817474Seric SafeAlias = atoi(val); 87917474Seric break; 88017474Seric 88116843Seric case 'B': /* substitution for blank character */ 88216843Seric SpaceSub = val[0]; 88316843Seric if (SpaceSub == '\0') 88416843Seric SpaceSub = ' '; 88516843Seric break; 88616843Seric 8879284Seric case 'c': /* don't connect to "expensive" mailers */ 8889381Seric NoConnect = atobool(val); 8899284Seric break; 8909284Seric 89151305Seric case 'C': /* checkpoint every N addresses */ 89251305Seric CheckpointInterval = atoi(val); 89324944Seric break; 89424944Seric 8959284Seric case 'd': /* delivery mode */ 8969284Seric switch (*val) 8978269Seric { 8989284Seric case '\0': 8999284Seric SendMode = SM_DELIVER; 9008269Seric break; 9018269Seric 90210755Seric case SM_QUEUE: /* queue only */ 90310755Seric #ifndef QUEUE 90410755Seric syserr("need QUEUE to set -odqueue"); 90556795Seric #endif /* QUEUE */ 90610755Seric /* fall through..... */ 90710755Seric 9089284Seric case SM_DELIVER: /* do everything */ 9099284Seric case SM_FORK: /* fork after verification */ 9109284Seric SendMode = *val; 9118269Seric break; 9128269Seric 9138269Seric default: 9149284Seric syserr("Unknown delivery mode %c", *val); 9158269Seric exit(EX_USAGE); 9168269Seric } 9178269Seric break; 9188269Seric 9199146Seric case 'D': /* rebuild alias database as needed */ 9209381Seric AutoRebuild = atobool(val); 9219146Seric break; 9229146Seric 92355372Seric case 'E': /* error message header/header file */ 92455379Seric if (*val != '\0') 92555379Seric ErrMsgFile = newstr(val); 92655372Seric break; 92755372Seric 9288269Seric case 'e': /* set error processing mode */ 9298269Seric switch (*val) 9308269Seric { 9319381Seric case EM_QUIET: /* be silent about it */ 9329381Seric case EM_MAIL: /* mail back */ 9339381Seric case EM_BERKNET: /* do berknet error processing */ 9349381Seric case EM_WRITE: /* write back (or mail) */ 9358269Seric HoldErrs = TRUE; 9369381Seric /* fall through... */ 9378269Seric 9389381Seric case EM_PRINT: /* print errors normally (default) */ 9399381Seric ErrorMode = *val; 9408269Seric break; 9418269Seric } 9428269Seric break; 9438269Seric 9449049Seric case 'F': /* file mode */ 94517975Seric FileMode = atooct(val) & 0777; 9469049Seric break; 9479049Seric 9488269Seric case 'f': /* save Unix-style From lines on front */ 9499381Seric SaveFrom = atobool(val); 9508269Seric break; 9518269Seric 95253735Seric case 'G': /* match recipients against GECOS field */ 95353735Seric MatchGecos = atobool(val); 95453735Seric break; 95553735Seric 9568256Seric case 'g': /* default gid */ 95717474Seric DefGid = atoi(val); 9588256Seric break; 9598256Seric 9608256Seric case 'H': /* help file */ 9619381Seric if (val[0] == '\0') 9628269Seric HelpFile = "sendmail.hf"; 9639381Seric else 9649381Seric HelpFile = newstr(val); 9658256Seric break; 9668256Seric 96751305Seric case 'h': /* maximum hop count */ 96851305Seric MaxHopCount = atoi(val); 96951305Seric break; 97051305Seric 97135651Seric case 'I': /* use internet domain name server */ 97257207Seric #ifdef NAMED_BIND 97357207Seric UseNameServer = TRUE; 97457207Seric for (p = val; *p != 0; ) 97557207Seric { 97657207Seric bool clearmode; 97757207Seric char *q; 97857207Seric struct resolverflags *rfp; 97957207Seric 98057207Seric while (*p == ' ') 98157207Seric p++; 98257207Seric if (*p == '\0') 98357207Seric break; 98457207Seric clearmode = FALSE; 98557207Seric if (*p == '-') 98657207Seric clearmode = TRUE; 98757207Seric else if (*p != '+') 98857207Seric p--; 98957207Seric p++; 99057207Seric q = p; 99157207Seric while (*p != '\0' && !isspace(*p)) 99257207Seric p++; 99357207Seric if (*p != '\0') 99457207Seric *p++ = '\0'; 99557207Seric for (rfp = ResolverFlags; rfp->rf_name != NULL; rfp++) 99657207Seric { 99757207Seric if (strcasecmp(q, rfp->rf_name) == 0) 99857207Seric break; 99957207Seric } 100057207Seric if (clearmode) 100157207Seric _res.options &= ~rfp->rf_bits; 100257207Seric else 100357207Seric _res.options |= rfp->rf_bits; 100457207Seric } 100557207Seric if (tTd(8, 2)) 100657207Seric printf("_res.options = %x\n", _res.options); 100757207Seric #else 100857207Seric usrerr("name server (I option) specified but BIND not compiled in"); 100957207Seric #endif 101035651Seric break; 101135651Seric 10128269Seric case 'i': /* ignore dot lines in message */ 10139381Seric IgnrDot = atobool(val); 10148269Seric break; 10158269Seric 101657136Seric case 'J': /* .forward search path */ 101757136Seric ForwardPath = newstr(val); 101857136Seric break; 101957136Seric 102054967Seric case 'k': /* connection cache size */ 102154967Seric MaxMciCache = atoi(val); 102256215Seric if (MaxMciCache < 0) 102356215Seric MaxMciCache = 0; 102454967Seric break; 102554967Seric 102654967Seric case 'K': /* connection cache timeout */ 102754967Seric MciCacheTimeout = convtime(val); 102854967Seric break; 102954967Seric 10308256Seric case 'L': /* log level */ 10319381Seric LogLevel = atoi(val); 10328256Seric break; 10338256Seric 10348269Seric case 'M': /* define macro */ 10359381Seric define(val[0], newstr(&val[1]), CurEnv); 103616878Seric sticky = FALSE; 10378269Seric break; 10388269Seric 10398269Seric case 'm': /* send to me too */ 10409381Seric MeToo = atobool(val); 10418269Seric break; 10428269Seric 104325820Seric case 'n': /* validate RHS in newaliases */ 104425820Seric CheckAliases = atobool(val); 104525820Seric break; 104625820Seric 10478269Seric case 'o': /* assume old style headers */ 10489381Seric if (atobool(val)) 10499341Seric CurEnv->e_flags |= EF_OLDSTYLE; 10509341Seric else 10519341Seric CurEnv->e_flags &= ~EF_OLDSTYLE; 10528269Seric break; 10538269Seric 105424944Seric case 'P': /* postmaster copy address for returned mail */ 105524944Seric PostMasterCopy = newstr(val); 105624944Seric break; 105724944Seric 105824944Seric case 'q': /* slope of queue only function */ 105924944Seric QueueFactor = atoi(val); 106024944Seric break; 106124944Seric 10628256Seric case 'Q': /* queue directory */ 10639381Seric if (val[0] == '\0') 10648269Seric QueueDir = "mqueue"; 10659381Seric else 10669381Seric QueueDir = newstr(val); 10678256Seric break; 10688256Seric 10698256Seric case 'r': /* read timeout */ 10709381Seric ReadTimeout = convtime(val); 10718256Seric break; 10728256Seric 10738256Seric case 'S': /* status file */ 10749381Seric if (val[0] == '\0') 10758269Seric StatFile = "sendmail.st"; 10769381Seric else 10779381Seric StatFile = newstr(val); 10788256Seric break; 10798256Seric 10808265Seric case 's': /* be super safe, even if expensive */ 10819381Seric SuperSafe = atobool(val); 10828256Seric break; 10838256Seric 10848256Seric case 'T': /* queue timeout */ 10859381Seric TimeOut = convtime(val); 108654967Seric break; 10878256Seric 10888265Seric case 't': /* time zone name */ 108952106Seric TimeZoneSpec = newstr(val); 10908265Seric break; 10918265Seric 109250556Seric case 'U': /* location of user database */ 109351360Seric UdbSpec = newstr(val); 109450556Seric break; 109550556Seric 10968256Seric case 'u': /* set default uid */ 109717474Seric DefUid = atoi(val); 109840973Sbostic setdefuser(); 10998256Seric break; 11008256Seric 11018269Seric case 'v': /* run in verbose mode */ 11029381Seric Verbose = atobool(val); 11038256Seric break; 11048256Seric 110551216Seric case 'w': /* we don't have wildcard MX records */ 110651216Seric NoWildcardMX = atobool(val); 110750537Seric break; 110850537Seric 110914879Seric case 'x': /* load avg at which to auto-queue msgs */ 111014879Seric QueueLA = atoi(val); 111114879Seric break; 111214879Seric 111314879Seric case 'X': /* load avg at which to auto-reject connections */ 111414879Seric RefuseLA = atoi(val); 111514879Seric break; 111614879Seric 111724981Seric case 'y': /* work recipient factor */ 111824981Seric WkRecipFact = atoi(val); 111924981Seric break; 112024981Seric 112124981Seric case 'Y': /* fork jobs during queue runs */ 112224952Seric ForkQueueRuns = atobool(val); 112324952Seric break; 112424952Seric 112524981Seric case 'z': /* work message class factor */ 112624981Seric WkClassFact = atoi(val); 112724981Seric break; 112824981Seric 112924981Seric case 'Z': /* work time factor */ 113024981Seric WkTimeFact = atoi(val); 113124981Seric break; 113224981Seric 11338256Seric default: 11348256Seric break; 11358256Seric } 113616878Seric if (sticky) 113716878Seric setbitn(opt, StickyOpt); 11389188Seric return; 11398256Seric } 114010687Seric /* 114110687Seric ** SETCLASS -- set a word into a class 114210687Seric ** 114310687Seric ** Parameters: 114410687Seric ** class -- the class to put the word in. 114510687Seric ** word -- the word to enter 114610687Seric ** 114710687Seric ** Returns: 114810687Seric ** none. 114910687Seric ** 115010687Seric ** Side Effects: 115110687Seric ** puts the word into the symbol table. 115210687Seric */ 115310687Seric 115410687Seric setclass(class, word) 115510687Seric int class; 115610687Seric char *word; 115710687Seric { 115810687Seric register STAB *s; 115910687Seric 116010687Seric s = stab(word, ST_CLASS, ST_ENTER); 116110687Seric setbitn(class, s->s_class); 116210687Seric } 116353654Seric /* 116453654Seric ** MAKEMAPENTRY -- create a map entry 116553654Seric ** 116653654Seric ** Parameters: 116753654Seric ** line -- the config file line 116853654Seric ** 116953654Seric ** Returns: 117053654Seric ** TRUE if it successfully entered the map entry. 117153654Seric ** FALSE otherwise (usually syntax error). 117253654Seric ** 117353654Seric ** Side Effects: 117453654Seric ** Enters the map into the dictionary. 117553654Seric */ 117653654Seric 117753654Seric void 117853654Seric makemapentry(line) 117953654Seric char *line; 118053654Seric { 118153654Seric register char *p; 118253654Seric char *mapname; 118353654Seric char *classname; 118453654Seric register STAB *map; 118553654Seric STAB *class; 118653654Seric 118753654Seric for (p = line; isspace(*p); p++) 118853654Seric continue; 118953654Seric if (!isalnum(*p)) 119053654Seric { 119153654Seric syserr("readcf: config K line: no map name"); 119253654Seric return; 119353654Seric } 119453654Seric 119553654Seric mapname = p; 119653654Seric while (isalnum(*++p)) 119753654Seric continue; 119853654Seric if (*p != '\0') 119953654Seric *p++ = '\0'; 120053654Seric while (isspace(*p)) 120153654Seric p++; 120253654Seric if (!isalnum(*p)) 120353654Seric { 120453654Seric syserr("readcf: config K line, map %s: no map class", mapname); 120553654Seric return; 120653654Seric } 120753654Seric classname = p; 120853654Seric while (isalnum(*++p)) 120953654Seric continue; 121053654Seric if (*p != '\0') 121153654Seric *p++ = '\0'; 121253654Seric while (isspace(*p)) 121353654Seric p++; 121453654Seric 121553654Seric /* look up the class */ 121653654Seric class = stab(classname, ST_MAPCLASS, ST_FIND); 121753654Seric if (class == NULL) 121853654Seric { 121953654Seric syserr("readcf: map %s: class %s not available", mapname, classname); 122053654Seric return; 122153654Seric } 122253654Seric 122353654Seric /* enter the map */ 122453654Seric map = stab(mapname, ST_MAP, ST_ENTER); 122553654Seric map->s_map.map_class = &class->s_mapclass; 122653654Seric 122756823Seric if ((*class->s_mapclass.map_init)(&map->s_map, mapname, p)) 122853654Seric map->s_map.map_flags |= MF_VALID; 122953654Seric } 1230