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*57736Seric static char sccsid[] = "@(#)readcf.c 6.5 (Berkeley) 01/28/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 21*57736Seric /* System 5 compatibility */ 22*57736Seric #ifndef S_ISREG 23*57736Seric #define S_ISREG(foo) ((foo & S_IFREG) == S_IFREG) 24*57736Seric #endif 25*57736Seric #ifndef S_IWGRP 26*57736Seric #define S_IWGRP 020 27*57736Seric #endif 28*57736Seric #ifndef S_IWOTH 29*57736Seric #define S_IWOTH 002 30*57736Seric #endif 31*57736Seric 323308Seric /* 333308Seric ** READCF -- read control file. 343308Seric ** 353308Seric ** This routine reads the control file and builds the internal 363308Seric ** form. 373308Seric ** 384432Seric ** The file is formatted as a sequence of lines, each taken 394432Seric ** atomically. The first character of each line describes how 404432Seric ** the line is to be interpreted. The lines are: 414432Seric ** Dxval Define macro x to have value val. 424432Seric ** Cxword Put word into class x. 434432Seric ** Fxfile [fmt] Read file for lines to put into 444432Seric ** class x. Use scanf string 'fmt' 454432Seric ** or "%s" if not present. Fmt should 464432Seric ** only produce one string-valued result. 474432Seric ** Hname: value Define header with field-name 'name' 484432Seric ** and value as specified; this will be 494432Seric ** macro expanded immediately before 504432Seric ** use. 514432Seric ** Sn Use rewriting set n. 524432Seric ** Rlhs rhs Rewrite addresses that match lhs to 534432Seric ** be rhs. 5424944Seric ** Mn arg=val... Define mailer. n is the internal name. 5524944Seric ** Args specify mailer parameters. 568252Seric ** Oxvalue Set option x to value. 578252Seric ** Pname=value Set precedence name to value. 5852645Seric ** Vversioncode Version level of configuration syntax. 5953654Seric ** Kmapname mapclass arguments.... 6053654Seric ** Define keyed lookup of a given class. 6153654Seric ** Arguments are class dependent. 624432Seric ** 633308Seric ** Parameters: 643308Seric ** cfname -- control file name. 6554973Seric ** safe -- TRUE if this is the system config file; 6654973Seric ** FALSE otherwise. 6755012Seric ** e -- the main envelope. 683308Seric ** 693308Seric ** Returns: 703308Seric ** none. 713308Seric ** 723308Seric ** Side Effects: 733308Seric ** Builds several internal tables. 743308Seric */ 753308Seric 7655012Seric readcf(cfname, safe, e) 773308Seric char *cfname; 7854973Seric bool safe; 7955012Seric register ENVELOPE *e; 803308Seric { 813308Seric FILE *cf; 828547Seric int ruleset = 0; 838547Seric char *q; 848547Seric char **pv; 859350Seric struct rewrite *rwp = NULL; 8657135Seric char *bp; 8757589Seric int nfuzzy; 883308Seric char buf[MAXLINE]; 893308Seric register char *p; 903308Seric extern char **prescan(); 913308Seric extern char **copyplist(); 9252647Seric struct stat statb; 935909Seric char exbuf[MAXLINE]; 9416915Seric char pvpbuf[PSBUFSIZE]; 959350Seric extern char *fgetfolded(); 9610709Seric extern char *munchstring(); 9753654Seric extern void makemapentry(); 983308Seric 9952647Seric FileName = cfname; 10052647Seric LineNumber = 0; 10152647Seric 1023308Seric cf = fopen(cfname, "r"); 1033308Seric if (cf == NULL) 1043308Seric { 10552647Seric syserr("cannot open"); 1063308Seric exit(EX_OSFILE); 1073308Seric } 1083308Seric 10952647Seric if (fstat(fileno(cf), &statb) < 0) 11052647Seric { 11152647Seric syserr("cannot fstat"); 11252647Seric exit(EX_OSFILE); 11352647Seric } 11452647Seric 11552647Seric if (!S_ISREG(statb.st_mode)) 11652647Seric { 11752647Seric syserr("not a plain file"); 11852647Seric exit(EX_OSFILE); 11952647Seric } 12052647Seric 12152647Seric if (OpMode != MD_TEST && bitset(S_IWGRP|S_IWOTH, statb.st_mode)) 12252647Seric { 12353037Seric if (OpMode == MD_DAEMON || OpMode == MD_FREEZE) 12453037Seric fprintf(stderr, "%s: WARNING: dangerous write permissions\n", 12553037Seric FileName); 12653037Seric #ifdef LOG 12753037Seric if (LogLevel > 0) 12853037Seric syslog(LOG_CRIT, "%s: WARNING: dangerous write permissions", 12953037Seric FileName); 13053037Seric #endif 13152647Seric } 13252647Seric 13357135Seric while ((bp = fgetfolded(buf, sizeof buf, cf)) != NULL) 1343308Seric { 13557135Seric if (bp[0] == '#') 13657135Seric { 13757135Seric if (bp != buf) 13857135Seric free(bp); 13952637Seric continue; 14057135Seric } 14152637Seric 14216157Seric /* map $ into \001 (ASCII SOH) for macro expansion */ 14357135Seric for (p = bp; *p != '\0'; p++) 14416157Seric { 14557135Seric if (*p == '#' && p > bp && ConfigLevel >= 3) 14652647Seric { 14752647Seric /* this is an on-line comment */ 14852647Seric register char *e; 14952647Seric 15052647Seric switch (*--p) 15152647Seric { 15252647Seric case '\001': 15352647Seric /* it's from $# -- let it go through */ 15452647Seric p++; 15552647Seric break; 15652647Seric 15752647Seric case '\\': 15852647Seric /* it's backslash escaped */ 15952647Seric (void) strcpy(p, p + 1); 16052647Seric break; 16152647Seric 16252647Seric default: 16352647Seric /* delete preceeding white space */ 16457135Seric while (isspace(*p) && p > bp) 16552647Seric p--; 16656795Seric if ((e = strchr(++p, '\n')) != NULL) 16752647Seric (void) strcpy(p, e); 16852647Seric else 16952647Seric p[0] = p[1] = '\0'; 17052647Seric break; 17152647Seric } 17252647Seric continue; 17352647Seric } 17452647Seric 17516157Seric if (*p != '$') 17616157Seric continue; 17716157Seric 17816157Seric if (p[1] == '$') 17916157Seric { 18016157Seric /* actual dollar sign.... */ 18123111Seric (void) strcpy(p, p + 1); 18216157Seric continue; 18316157Seric } 18416157Seric 18516157Seric /* convert to macro expansion character */ 18616157Seric *p = '\001'; 18716157Seric } 18816157Seric 18916157Seric /* interpret this line */ 19057135Seric switch (bp[0]) 1913308Seric { 1923308Seric case '\0': 1933308Seric case '#': /* comment */ 1943308Seric break; 1953308Seric 1963308Seric case 'R': /* rewriting rule */ 19757135Seric for (p = &bp[1]; *p != '\0' && *p != '\t'; p++) 1983308Seric continue; 1993308Seric 2003308Seric if (*p == '\0') 2015909Seric { 20257135Seric syserr("invalid rewrite line \"%s\"", bp); 2035909Seric break; 2045909Seric } 2055909Seric 2065909Seric /* allocate space for the rule header */ 2075909Seric if (rwp == NULL) 2085909Seric { 2095909Seric RewriteRules[ruleset] = rwp = 2105909Seric (struct rewrite *) xalloc(sizeof *rwp); 2115909Seric } 2123308Seric else 2133308Seric { 2145909Seric rwp->r_next = (struct rewrite *) xalloc(sizeof *rwp); 2155909Seric rwp = rwp->r_next; 2165909Seric } 2175909Seric rwp->r_next = NULL; 2183308Seric 2195909Seric /* expand and save the LHS */ 2205909Seric *p = '\0'; 22157135Seric expand(&bp[1], exbuf, &exbuf[sizeof exbuf], e); 22216915Seric rwp->r_lhs = prescan(exbuf, '\t', pvpbuf); 22357589Seric nfuzzy = 0; 2245909Seric if (rwp->r_lhs != NULL) 22557589Seric { 22657589Seric register char **ap; 22757589Seric 2285909Seric rwp->r_lhs = copyplist(rwp->r_lhs, TRUE); 22957589Seric 23057589Seric /* count the number of fuzzy matches in LHS */ 23157589Seric for (ap = rwp->r_lhs; *ap != NULL; ap++) 23257589Seric { 23357589Seric switch (**ap) 23457589Seric { 23557589Seric case MATCHZANY: 23657589Seric case MATCHANY: 23757589Seric case MATCHONE: 23857589Seric case MATCHCLASS: 23957589Seric case MATCHNCLASS: 24057589Seric nfuzzy++; 24157589Seric } 24257589Seric } 24357589Seric } 24456678Seric else 24556678Seric syserr("R line: null LHS"); 2465909Seric 2475909Seric /* expand and save the RHS */ 2485909Seric while (*++p == '\t') 2495909Seric continue; 2507231Seric q = p; 2517231Seric while (*p != '\0' && *p != '\t') 2527231Seric p++; 2537231Seric *p = '\0'; 25455012Seric expand(q, exbuf, &exbuf[sizeof exbuf], e); 25516915Seric rwp->r_rhs = prescan(exbuf, '\t', pvpbuf); 2565909Seric if (rwp->r_rhs != NULL) 25757589Seric { 25857589Seric register char **ap; 25957589Seric 2605909Seric rwp->r_rhs = copyplist(rwp->r_rhs, TRUE); 26157589Seric 26257589Seric /* check no out-of-bounds replacements */ 26357589Seric nfuzzy += '0'; 26457589Seric for (ap = rwp->r_rhs; *ap != NULL; ap++) 26557589Seric { 26657589Seric if (**ap != MATCHREPL) 26757589Seric continue; 26857589Seric if ((*ap)[1] <= '0' || (*ap)[1] > nfuzzy) 26957589Seric { 27057589Seric syserr("replacement $%c out of bounds", 27157589Seric (*ap)[1]); 27257589Seric } 27357589Seric } 27457589Seric } 27556678Seric else 27656678Seric syserr("R line: null RHS"); 2773308Seric break; 2783308Seric 2794072Seric case 'S': /* select rewriting set */ 28057135Seric ruleset = atoi(&bp[1]); 2818056Seric if (ruleset >= MAXRWSETS || ruleset < 0) 2828056Seric { 2839381Seric syserr("bad ruleset %d (%d max)", ruleset, MAXRWSETS); 2848056Seric ruleset = 0; 2858056Seric } 2864072Seric rwp = NULL; 2874072Seric break; 2884072Seric 2893308Seric case 'D': /* macro definition */ 29057135Seric define(bp[1], newstr(munchstring(&bp[2])), e); 2913308Seric break; 2923308Seric 2933387Seric case 'H': /* required header line */ 29457135Seric (void) chompheader(&bp[1], TRUE, e); 2953387Seric break; 2963387Seric 2974061Seric case 'C': /* word class */ 2984432Seric case 'F': /* word class from file */ 2994432Seric /* read list of words from argument or file */ 30057135Seric if (bp[0] == 'F') 3014432Seric { 3024432Seric /* read from file */ 30357135Seric for (p = &bp[2]; *p != '\0' && !isspace(*p); p++) 3044432Seric continue; 3054432Seric if (*p == '\0') 3064432Seric p = "%s"; 3074432Seric else 3084432Seric { 3094432Seric *p = '\0'; 3104432Seric while (isspace(*++p)) 3114432Seric continue; 3124432Seric } 31357135Seric fileclass(bp[1], &bp[2], p, safe); 3144432Seric break; 3154432Seric } 3164061Seric 3174432Seric /* scan the list of words and set class for all */ 31857135Seric for (p = &bp[2]; *p != '\0'; ) 3194061Seric { 3204061Seric register char *wd; 3214061Seric char delim; 3224061Seric 3234061Seric while (*p != '\0' && isspace(*p)) 3244061Seric p++; 3254061Seric wd = p; 3264061Seric while (*p != '\0' && !isspace(*p)) 3274061Seric p++; 3284061Seric delim = *p; 3294061Seric *p = '\0'; 3304061Seric if (wd[0] != '\0') 33157135Seric setclass(bp[1], wd); 3324061Seric *p = delim; 3334061Seric } 3344061Seric break; 3354061Seric 3364096Seric case 'M': /* define mailer */ 33757135Seric makemailer(&bp[1]); 3384096Seric break; 3394096Seric 3408252Seric case 'O': /* set option */ 34157135Seric setoption(bp[1], &bp[2], safe, FALSE); 3428252Seric break; 3438252Seric 3448252Seric case 'P': /* set precedence */ 3458252Seric if (NumPriorities >= MAXPRIORITIES) 3468252Seric { 3478547Seric toomany('P', MAXPRIORITIES); 3488252Seric break; 3498252Seric } 35057135Seric for (p = &bp[1]; *p != '\0' && *p != '=' && *p != '\t'; p++) 3518252Seric continue; 3528252Seric if (*p == '\0') 3538252Seric goto badline; 3548252Seric *p = '\0'; 35557135Seric Priorities[NumPriorities].pri_name = newstr(&bp[1]); 3568252Seric Priorities[NumPriorities].pri_val = atoi(++p); 3578252Seric NumPriorities++; 3588252Seric break; 3598252Seric 3608547Seric case 'T': /* trusted user(s) */ 36157135Seric p = &bp[1]; 3628547Seric while (*p != '\0') 3638547Seric { 3648547Seric while (isspace(*p)) 3658547Seric p++; 3668547Seric q = p; 3678547Seric while (*p != '\0' && !isspace(*p)) 3688547Seric p++; 3698547Seric if (*p != '\0') 3708547Seric *p++ = '\0'; 3718547Seric if (*q == '\0') 3728547Seric continue; 3738547Seric for (pv = TrustedUsers; *pv != NULL; pv++) 3748547Seric continue; 3758547Seric if (pv >= &TrustedUsers[MAXTRUST]) 3768547Seric { 3778547Seric toomany('T', MAXTRUST); 3788547Seric break; 3798547Seric } 3808547Seric *pv = newstr(q); 3818547Seric } 3828547Seric break; 3838547Seric 38452645Seric case 'V': /* configuration syntax version */ 38557135Seric ConfigLevel = atoi(&bp[1]); 38652645Seric break; 38752645Seric 38853654Seric case 'K': 38957135Seric makemapentry(&bp[1]); 39053654Seric break; 39153654Seric 3923308Seric default: 3934061Seric badline: 39457135Seric syserr("unknown control line \"%s\"", bp); 3953308Seric } 39657135Seric if (bp != buf) 39757135Seric free(bp); 3983308Seric } 39952637Seric if (ferror(cf)) 40052637Seric { 40152647Seric syserr("I/O read error", cfname); 40252637Seric exit(EX_OSFILE); 40352637Seric } 40452637Seric fclose(cf); 4059381Seric FileName = NULL; 40656836Seric 40757076Seric if (stab("host", ST_MAP, ST_FIND) == NULL) 40857076Seric { 40957076Seric /* user didn't initialize: set up host map */ 41057076Seric strcpy(buf, "host host"); 41157076Seric if (ConfigLevel >= 2) 41257076Seric strcat(buf, " -a."); 41357076Seric makemapentry(buf); 41457076Seric } 4154096Seric } 4164096Seric /* 4178547Seric ** TOOMANY -- signal too many of some option 4188547Seric ** 4198547Seric ** Parameters: 4208547Seric ** id -- the id of the error line 4218547Seric ** maxcnt -- the maximum possible values 4228547Seric ** 4238547Seric ** Returns: 4248547Seric ** none. 4258547Seric ** 4268547Seric ** Side Effects: 4278547Seric ** gives a syserr. 4288547Seric */ 4298547Seric 4308547Seric toomany(id, maxcnt) 4318547Seric char id; 4328547Seric int maxcnt; 4338547Seric { 4349381Seric syserr("too many %c lines, %d max", id, maxcnt); 4358547Seric } 4368547Seric /* 4374432Seric ** FILECLASS -- read members of a class from a file 4384432Seric ** 4394432Seric ** Parameters: 4404432Seric ** class -- class to define. 4414432Seric ** filename -- name of file to read. 4424432Seric ** fmt -- scanf string to use for match. 4434432Seric ** 4444432Seric ** Returns: 4454432Seric ** none 4464432Seric ** 4474432Seric ** Side Effects: 4484432Seric ** 4494432Seric ** puts all lines in filename that match a scanf into 4504432Seric ** the named class. 4514432Seric */ 4524432Seric 45354973Seric fileclass(class, filename, fmt, safe) 4544432Seric int class; 4554432Seric char *filename; 4564432Seric char *fmt; 45754973Seric bool safe; 4584432Seric { 45925808Seric FILE *f; 46054973Seric struct stat stbuf; 4614432Seric char buf[MAXLINE]; 4624432Seric 46354973Seric if (stat(filename, &stbuf) < 0) 46454973Seric { 46554973Seric syserr("fileclass: cannot stat %s", filename); 46654973Seric return; 46754973Seric } 46854973Seric if (!S_ISREG(stbuf.st_mode)) 46954973Seric { 47054973Seric syserr("fileclass: %s not a regular file", filename); 47154973Seric return; 47254973Seric } 47354973Seric if (!safe && access(filename, R_OK) < 0) 47454973Seric { 47554973Seric syserr("fileclass: access denied on %s", filename); 47654973Seric return; 47754973Seric } 47854973Seric f = fopen(filename, "r"); 4794432Seric if (f == NULL) 4804432Seric { 48154973Seric syserr("fileclass: cannot open %s", filename); 4824432Seric return; 4834432Seric } 4844432Seric 4854432Seric while (fgets(buf, sizeof buf, f) != NULL) 4864432Seric { 4874432Seric register STAB *s; 48825808Seric register char *p; 48925808Seric # ifdef SCANF 4904432Seric char wordbuf[MAXNAME+1]; 4914432Seric 4924432Seric if (sscanf(buf, fmt, wordbuf) != 1) 4934432Seric continue; 49425808Seric p = wordbuf; 49556795Seric # else /* SCANF */ 49625808Seric p = buf; 49756795Seric # endif /* SCANF */ 49825808Seric 49925808Seric /* 50025808Seric ** Break up the match into words. 50125808Seric */ 50225808Seric 50325808Seric while (*p != '\0') 50425808Seric { 50525808Seric register char *q; 50625808Seric 50725808Seric /* strip leading spaces */ 50825808Seric while (isspace(*p)) 50925808Seric p++; 51025808Seric if (*p == '\0') 51125808Seric break; 51225808Seric 51325808Seric /* find the end of the word */ 51425808Seric q = p; 51525808Seric while (*p != '\0' && !isspace(*p)) 51625808Seric p++; 51725808Seric if (*p != '\0') 51825808Seric *p++ = '\0'; 51925808Seric 52025808Seric /* enter the word in the symbol table */ 52125808Seric s = stab(q, ST_CLASS, ST_ENTER); 52225808Seric setbitn(class, s->s_class); 52325808Seric } 5244432Seric } 5254432Seric 52654973Seric (void) fclose(f); 5274432Seric } 5284432Seric /* 5294096Seric ** MAKEMAILER -- define a new mailer. 5304096Seric ** 5314096Seric ** Parameters: 53210327Seric ** line -- description of mailer. This is in labeled 53310327Seric ** fields. The fields are: 53410327Seric ** P -- the path to the mailer 53510327Seric ** F -- the flags associated with the mailer 53610327Seric ** A -- the argv for this mailer 53710327Seric ** S -- the sender rewriting set 53810327Seric ** R -- the recipient rewriting set 53910327Seric ** E -- the eol string 54010327Seric ** The first word is the canonical name of the mailer. 5414096Seric ** 5424096Seric ** Returns: 5434096Seric ** none. 5444096Seric ** 5454096Seric ** Side Effects: 5464096Seric ** enters the mailer into the mailer table. 5474096Seric */ 5483308Seric 54921066Seric makemailer(line) 5504096Seric char *line; 5514096Seric { 5524096Seric register char *p; 5538067Seric register struct mailer *m; 5548067Seric register STAB *s; 5558067Seric int i; 55610327Seric char fcode; 5574096Seric extern int NextMailer; 55810327Seric extern char **makeargv(); 55910327Seric extern char *munchstring(); 56010327Seric extern char *DelimChar; 56110701Seric extern long atol(); 5624096Seric 56310327Seric /* allocate a mailer and set up defaults */ 56410327Seric m = (struct mailer *) xalloc(sizeof *m); 56510327Seric bzero((char *) m, sizeof *m); 56610327Seric m->m_eol = "\n"; 56710327Seric 56810327Seric /* collect the mailer name */ 56910327Seric for (p = line; *p != '\0' && *p != ',' && !isspace(*p); p++) 57010327Seric continue; 57110327Seric if (*p != '\0') 57210327Seric *p++ = '\0'; 57310327Seric m->m_name = newstr(line); 57410327Seric 57510327Seric /* now scan through and assign info from the fields */ 57610327Seric while (*p != '\0') 57710327Seric { 57810327Seric while (*p != '\0' && (*p == ',' || isspace(*p))) 57910327Seric p++; 58010327Seric 58110327Seric /* p now points to field code */ 58210327Seric fcode = *p; 58310327Seric while (*p != '\0' && *p != '=' && *p != ',') 58410327Seric p++; 58510327Seric if (*p++ != '=') 58610327Seric { 58752637Seric syserr("mailer %s: `=' expected", m->m_name); 58810327Seric return; 58910327Seric } 59010327Seric while (isspace(*p)) 59110327Seric p++; 59210327Seric 59310327Seric /* p now points to the field body */ 59410327Seric p = munchstring(p); 59510327Seric 59610327Seric /* install the field into the mailer struct */ 59710327Seric switch (fcode) 59810327Seric { 59910327Seric case 'P': /* pathname */ 60010327Seric m->m_mailer = newstr(p); 60110327Seric break; 60210327Seric 60310327Seric case 'F': /* flags */ 60410687Seric for (; *p != '\0'; p++) 60552637Seric if (!isspace(*p)) 60652637Seric setbitn(*p, m->m_flags); 60710327Seric break; 60810327Seric 60910327Seric case 'S': /* sender rewriting ruleset */ 61010327Seric case 'R': /* recipient rewriting ruleset */ 61110327Seric i = atoi(p); 61210327Seric if (i < 0 || i >= MAXRWSETS) 61310327Seric { 61410327Seric syserr("invalid rewrite set, %d max", MAXRWSETS); 61510327Seric return; 61610327Seric } 61710327Seric if (fcode == 'S') 61810327Seric m->m_s_rwset = i; 61910327Seric else 62010327Seric m->m_r_rwset = i; 62110327Seric break; 62210327Seric 62310327Seric case 'E': /* end of line string */ 62410327Seric m->m_eol = newstr(p); 62510327Seric break; 62610327Seric 62710327Seric case 'A': /* argument vector */ 62810327Seric m->m_argv = makeargv(p); 62910327Seric break; 63010701Seric 63110701Seric case 'M': /* maximum message size */ 63210701Seric m->m_maxsize = atol(p); 63310701Seric break; 63452106Seric 63552106Seric case 'L': /* maximum line length */ 63652106Seric m->m_linelimit = atoi(p); 63752106Seric break; 63810327Seric } 63910327Seric 64010327Seric p = DelimChar; 64110327Seric } 64210327Seric 64352106Seric /* do some heuristic cleanup for back compatibility */ 64452106Seric if (bitnset(M_LIMITS, m->m_flags)) 64552106Seric { 64652106Seric if (m->m_linelimit == 0) 64752106Seric m->m_linelimit = SMTPLINELIM; 64855418Seric if (ConfigLevel < 2) 64952106Seric setbitn(M_7BITS, m->m_flags); 65052106Seric } 65152106Seric 6524096Seric if (NextMailer >= MAXMAILERS) 6534096Seric { 6549381Seric syserr("too many mailers defined (%d max)", MAXMAILERS); 6554096Seric return; 6564096Seric } 65757402Seric 65810327Seric s = stab(m->m_name, ST_MAILER, ST_ENTER); 65957402Seric if (s->s_mailer != NULL) 66057402Seric { 66157402Seric i = s->s_mailer->m_mno; 66257402Seric free(s->s_mailer); 66357402Seric } 66457402Seric else 66557402Seric { 66657402Seric i = NextMailer++; 66757402Seric } 66857402Seric Mailer[i] = s->s_mailer = m; 66957454Seric m->m_mno = i; 67010327Seric } 67110327Seric /* 67210327Seric ** MUNCHSTRING -- translate a string into internal form. 67310327Seric ** 67410327Seric ** Parameters: 67510327Seric ** p -- the string to munch. 67610327Seric ** 67710327Seric ** Returns: 67810327Seric ** the munched string. 67910327Seric ** 68010327Seric ** Side Effects: 68110327Seric ** Sets "DelimChar" to point to the string that caused us 68210327Seric ** to stop. 68310327Seric */ 6844096Seric 68510327Seric char * 68610327Seric munchstring(p) 68710327Seric register char *p; 68810327Seric { 68910327Seric register char *q; 69010327Seric bool backslash = FALSE; 69110327Seric bool quotemode = FALSE; 69210327Seric static char buf[MAXLINE]; 69310327Seric extern char *DelimChar; 6944096Seric 69510327Seric for (q = buf; *p != '\0'; p++) 6964096Seric { 69710327Seric if (backslash) 69810327Seric { 69910327Seric /* everything is roughly literal */ 70010357Seric backslash = FALSE; 70110327Seric switch (*p) 70210327Seric { 70310327Seric case 'r': /* carriage return */ 70410327Seric *q++ = '\r'; 70510327Seric continue; 70610327Seric 70710327Seric case 'n': /* newline */ 70810327Seric *q++ = '\n'; 70910327Seric continue; 71010327Seric 71110327Seric case 'f': /* form feed */ 71210327Seric *q++ = '\f'; 71310327Seric continue; 71410327Seric 71510327Seric case 'b': /* backspace */ 71610327Seric *q++ = '\b'; 71710327Seric continue; 71810327Seric } 71910327Seric *q++ = *p; 72010327Seric } 72110327Seric else 72210327Seric { 72310327Seric if (*p == '\\') 72410327Seric backslash = TRUE; 72510327Seric else if (*p == '"') 72610327Seric quotemode = !quotemode; 72710327Seric else if (quotemode || *p != ',') 72810327Seric *q++ = *p; 72910327Seric else 73010327Seric break; 73110327Seric } 7324096Seric } 7334096Seric 73410327Seric DelimChar = p; 73510327Seric *q++ = '\0'; 73610327Seric return (buf); 73710327Seric } 73810327Seric /* 73910327Seric ** MAKEARGV -- break up a string into words 74010327Seric ** 74110327Seric ** Parameters: 74210327Seric ** p -- the string to break up. 74310327Seric ** 74410327Seric ** Returns: 74510327Seric ** a char **argv (dynamically allocated) 74610327Seric ** 74710327Seric ** Side Effects: 74810327Seric ** munges p. 74910327Seric */ 7504096Seric 75110327Seric char ** 75210327Seric makeargv(p) 75310327Seric register char *p; 75410327Seric { 75510327Seric char *q; 75610327Seric int i; 75710327Seric char **avp; 75810327Seric char *argv[MAXPV + 1]; 75910327Seric 76010327Seric /* take apart the words */ 76110327Seric i = 0; 76210327Seric while (*p != '\0' && i < MAXPV) 7634096Seric { 76410327Seric q = p; 76510327Seric while (*p != '\0' && !isspace(*p)) 76610327Seric p++; 76710327Seric while (isspace(*p)) 76810327Seric *p++ = '\0'; 76910327Seric argv[i++] = newstr(q); 7704096Seric } 77110327Seric argv[i++] = NULL; 7724096Seric 77310327Seric /* now make a copy of the argv */ 77410327Seric avp = (char **) xalloc(sizeof *avp * i); 77516893Seric bcopy((char *) argv, (char *) avp, sizeof *avp * i); 77610327Seric 77710327Seric return (avp); 7783308Seric } 7793308Seric /* 7803308Seric ** PRINTRULES -- print rewrite rules (for debugging) 7813308Seric ** 7823308Seric ** Parameters: 7833308Seric ** none. 7843308Seric ** 7853308Seric ** Returns: 7863308Seric ** none. 7873308Seric ** 7883308Seric ** Side Effects: 7893308Seric ** prints rewrite rules. 7903308Seric */ 7913308Seric 7923308Seric printrules() 7933308Seric { 7943308Seric register struct rewrite *rwp; 7954072Seric register int ruleset; 7963308Seric 7974072Seric for (ruleset = 0; ruleset < 10; ruleset++) 7983308Seric { 7994072Seric if (RewriteRules[ruleset] == NULL) 8004072Seric continue; 8018067Seric printf("\n----Rule Set %d:", ruleset); 8023308Seric 8034072Seric for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next) 8043308Seric { 8058067Seric printf("\nLHS:"); 8068067Seric printav(rwp->r_lhs); 8078067Seric printf("RHS:"); 8088067Seric printav(rwp->r_rhs); 8093308Seric } 8103308Seric } 8113308Seric } 8124319Seric 8134096Seric /* 8148256Seric ** SETOPTION -- set global processing option 8158256Seric ** 8168256Seric ** Parameters: 8178256Seric ** opt -- option name. 8188256Seric ** val -- option value (as a text string). 81921755Seric ** safe -- set if this came from a configuration file. 82021755Seric ** Some options (if set from the command line) will 82121755Seric ** reset the user id to avoid security problems. 8228269Seric ** sticky -- if set, don't let other setoptions override 8238269Seric ** this value. 8248256Seric ** 8258256Seric ** Returns: 8268256Seric ** none. 8278256Seric ** 8288256Seric ** Side Effects: 8298256Seric ** Sets options as implied by the arguments. 8308256Seric */ 8318256Seric 83210687Seric static BITMAP StickyOpt; /* set if option is stuck */ 8338269Seric 83457207Seric 83557207Seric #ifdef NAMED_BIND 83657207Seric 83757207Seric struct resolverflags 83857207Seric { 83957207Seric char *rf_name; /* name of the flag */ 84057207Seric long rf_bits; /* bits to set/clear */ 84157207Seric } ResolverFlags[] = 84257207Seric { 84357207Seric "debug", RES_DEBUG, 84457207Seric "aaonly", RES_AAONLY, 84557207Seric "usevc", RES_USEVC, 84657207Seric "primary", RES_PRIMARY, 84757207Seric "igntc", RES_IGNTC, 84857207Seric "recurse", RES_RECURSE, 84957207Seric "defnames", RES_DEFNAMES, 85057207Seric "stayopen", RES_STAYOPEN, 85157207Seric "dnsrch", RES_DNSRCH, 85257207Seric NULL, 0 85357207Seric }; 85457207Seric 85557207Seric #endif 85657207Seric 85721755Seric setoption(opt, val, safe, sticky) 8588256Seric char opt; 8598256Seric char *val; 86021755Seric bool safe; 8618269Seric bool sticky; 8628256Seric { 86357207Seric register char *p; 8648265Seric extern bool atobool(); 86512633Seric extern time_t convtime(); 86614879Seric extern int QueueLA; 86714879Seric extern int RefuseLA; 86817474Seric extern bool trusteduser(); 86917474Seric extern char *username(); 8708256Seric 8718256Seric if (tTd(37, 1)) 8729341Seric printf("setoption %c=%s", opt, val); 8738256Seric 8748256Seric /* 8758269Seric ** See if this option is preset for us. 8768256Seric */ 8778256Seric 87810687Seric if (bitnset(opt, StickyOpt)) 8798269Seric { 8809341Seric if (tTd(37, 1)) 8819341Seric printf(" (ignored)\n"); 8828269Seric return; 8838269Seric } 8848269Seric 88521755Seric /* 88621755Seric ** Check to see if this option can be specified by this user. 88721755Seric */ 88821755Seric 88936238Skarels if (!safe && getuid() == 0) 89021755Seric safe = TRUE; 89157136Seric if (!safe && strchr("deEiLmorsvC8", opt) == NULL) 89221755Seric { 89339111Srick if (opt != 'M' || (val[0] != 'r' && val[0] != 's')) 89421755Seric { 89536582Sbostic if (tTd(37, 1)) 89636582Sbostic printf(" (unsafe)"); 89736582Sbostic if (getuid() != geteuid()) 89836582Sbostic { 89951210Seric if (tTd(37, 1)) 90051210Seric printf("(Resetting uid)"); 90136582Sbostic (void) setgid(getgid()); 90236582Sbostic (void) setuid(getuid()); 90336582Sbostic } 90421755Seric } 90521755Seric } 90651210Seric if (tTd(37, 1)) 90717985Seric printf("\n"); 9088269Seric 9098256Seric switch (opt) 9108256Seric { 91152106Seric case '8': /* allow eight-bit input */ 91252106Seric EightBit = atobool(val); 91352106Seric break; 91452106Seric 9158256Seric case 'A': /* set default alias file */ 9169381Seric if (val[0] == '\0') 9178269Seric AliasFile = "aliases"; 9189381Seric else 9199381Seric AliasFile = newstr(val); 9208256Seric break; 9218256Seric 92217474Seric case 'a': /* look N minutes for "@:@" in alias file */ 92317474Seric if (val[0] == '\0') 92417474Seric SafeAlias = 5; 92517474Seric else 92617474Seric SafeAlias = atoi(val); 92717474Seric break; 92817474Seric 92916843Seric case 'B': /* substitution for blank character */ 93016843Seric SpaceSub = val[0]; 93116843Seric if (SpaceSub == '\0') 93216843Seric SpaceSub = ' '; 93316843Seric break; 93416843Seric 9359284Seric case 'c': /* don't connect to "expensive" mailers */ 9369381Seric NoConnect = atobool(val); 9379284Seric break; 9389284Seric 93951305Seric case 'C': /* checkpoint every N addresses */ 94051305Seric CheckpointInterval = atoi(val); 94124944Seric break; 94224944Seric 9439284Seric case 'd': /* delivery mode */ 9449284Seric switch (*val) 9458269Seric { 9469284Seric case '\0': 9479284Seric SendMode = SM_DELIVER; 9488269Seric break; 9498269Seric 95010755Seric case SM_QUEUE: /* queue only */ 95110755Seric #ifndef QUEUE 95210755Seric syserr("need QUEUE to set -odqueue"); 95356795Seric #endif /* QUEUE */ 95410755Seric /* fall through..... */ 95510755Seric 9569284Seric case SM_DELIVER: /* do everything */ 9579284Seric case SM_FORK: /* fork after verification */ 9589284Seric SendMode = *val; 9598269Seric break; 9608269Seric 9618269Seric default: 9629284Seric syserr("Unknown delivery mode %c", *val); 9638269Seric exit(EX_USAGE); 9648269Seric } 9658269Seric break; 9668269Seric 9679146Seric case 'D': /* rebuild alias database as needed */ 9689381Seric AutoRebuild = atobool(val); 9699146Seric break; 9709146Seric 97155372Seric case 'E': /* error message header/header file */ 97255379Seric if (*val != '\0') 97355379Seric ErrMsgFile = newstr(val); 97455372Seric break; 97555372Seric 9768269Seric case 'e': /* set error processing mode */ 9778269Seric switch (*val) 9788269Seric { 9799381Seric case EM_QUIET: /* be silent about it */ 9809381Seric case EM_MAIL: /* mail back */ 9819381Seric case EM_BERKNET: /* do berknet error processing */ 9829381Seric case EM_WRITE: /* write back (or mail) */ 9838269Seric HoldErrs = TRUE; 9849381Seric /* fall through... */ 9858269Seric 9869381Seric case EM_PRINT: /* print errors normally (default) */ 9879381Seric ErrorMode = *val; 9888269Seric break; 9898269Seric } 9908269Seric break; 9918269Seric 9929049Seric case 'F': /* file mode */ 99317975Seric FileMode = atooct(val) & 0777; 9949049Seric break; 9959049Seric 9968269Seric case 'f': /* save Unix-style From lines on front */ 9979381Seric SaveFrom = atobool(val); 9988269Seric break; 9998269Seric 100053735Seric case 'G': /* match recipients against GECOS field */ 100153735Seric MatchGecos = atobool(val); 100253735Seric break; 100353735Seric 10048256Seric case 'g': /* default gid */ 100517474Seric DefGid = atoi(val); 10068256Seric break; 10078256Seric 10088256Seric case 'H': /* help file */ 10099381Seric if (val[0] == '\0') 10108269Seric HelpFile = "sendmail.hf"; 10119381Seric else 10129381Seric HelpFile = newstr(val); 10138256Seric break; 10148256Seric 101551305Seric case 'h': /* maximum hop count */ 101651305Seric MaxHopCount = atoi(val); 101751305Seric break; 101851305Seric 101935651Seric case 'I': /* use internet domain name server */ 102057207Seric #ifdef NAMED_BIND 102157207Seric UseNameServer = TRUE; 102257207Seric for (p = val; *p != 0; ) 102357207Seric { 102457207Seric bool clearmode; 102557207Seric char *q; 102657207Seric struct resolverflags *rfp; 102757207Seric 102857207Seric while (*p == ' ') 102957207Seric p++; 103057207Seric if (*p == '\0') 103157207Seric break; 103257207Seric clearmode = FALSE; 103357207Seric if (*p == '-') 103457207Seric clearmode = TRUE; 103557207Seric else if (*p != '+') 103657207Seric p--; 103757207Seric p++; 103857207Seric q = p; 103957207Seric while (*p != '\0' && !isspace(*p)) 104057207Seric p++; 104157207Seric if (*p != '\0') 104257207Seric *p++ = '\0'; 104357207Seric for (rfp = ResolverFlags; rfp->rf_name != NULL; rfp++) 104457207Seric { 104557207Seric if (strcasecmp(q, rfp->rf_name) == 0) 104657207Seric break; 104757207Seric } 104857207Seric if (clearmode) 104957207Seric _res.options &= ~rfp->rf_bits; 105057207Seric else 105157207Seric _res.options |= rfp->rf_bits; 105257207Seric } 105357207Seric if (tTd(8, 2)) 105457207Seric printf("_res.options = %x\n", _res.options); 105557207Seric #else 105657207Seric usrerr("name server (I option) specified but BIND not compiled in"); 105757207Seric #endif 105835651Seric break; 105935651Seric 10608269Seric case 'i': /* ignore dot lines in message */ 10619381Seric IgnrDot = atobool(val); 10628269Seric break; 10638269Seric 106457136Seric case 'J': /* .forward search path */ 106557136Seric ForwardPath = newstr(val); 106657136Seric break; 106757136Seric 106854967Seric case 'k': /* connection cache size */ 106954967Seric MaxMciCache = atoi(val); 107056215Seric if (MaxMciCache < 0) 107156215Seric MaxMciCache = 0; 107254967Seric break; 107354967Seric 107454967Seric case 'K': /* connection cache timeout */ 107554967Seric MciCacheTimeout = convtime(val); 107654967Seric break; 107754967Seric 10788256Seric case 'L': /* log level */ 10799381Seric LogLevel = atoi(val); 10808256Seric break; 10818256Seric 10828269Seric case 'M': /* define macro */ 10839381Seric define(val[0], newstr(&val[1]), CurEnv); 108416878Seric sticky = FALSE; 10858269Seric break; 10868269Seric 10878269Seric case 'm': /* send to me too */ 10889381Seric MeToo = atobool(val); 10898269Seric break; 10908269Seric 109125820Seric case 'n': /* validate RHS in newaliases */ 109225820Seric CheckAliases = atobool(val); 109325820Seric break; 109425820Seric 10958269Seric case 'o': /* assume old style headers */ 10969381Seric if (atobool(val)) 10979341Seric CurEnv->e_flags |= EF_OLDSTYLE; 10989341Seric else 10999341Seric CurEnv->e_flags &= ~EF_OLDSTYLE; 11008269Seric break; 11018269Seric 110224944Seric case 'P': /* postmaster copy address for returned mail */ 110324944Seric PostMasterCopy = newstr(val); 110424944Seric break; 110524944Seric 110624944Seric case 'q': /* slope of queue only function */ 110724944Seric QueueFactor = atoi(val); 110824944Seric break; 110924944Seric 11108256Seric case 'Q': /* queue directory */ 11119381Seric if (val[0] == '\0') 11128269Seric QueueDir = "mqueue"; 11139381Seric else 11149381Seric QueueDir = newstr(val); 11158256Seric break; 11168256Seric 11178256Seric case 'r': /* read timeout */ 11189381Seric ReadTimeout = convtime(val); 11198256Seric break; 11208256Seric 11218256Seric case 'S': /* status file */ 11229381Seric if (val[0] == '\0') 11238269Seric StatFile = "sendmail.st"; 11249381Seric else 11259381Seric StatFile = newstr(val); 11268256Seric break; 11278256Seric 11288265Seric case 's': /* be super safe, even if expensive */ 11299381Seric SuperSafe = atobool(val); 11308256Seric break; 11318256Seric 11328256Seric case 'T': /* queue timeout */ 11339381Seric TimeOut = convtime(val); 113454967Seric break; 11358256Seric 11368265Seric case 't': /* time zone name */ 113752106Seric TimeZoneSpec = newstr(val); 11388265Seric break; 11398265Seric 114050556Seric case 'U': /* location of user database */ 114151360Seric UdbSpec = newstr(val); 114250556Seric break; 114350556Seric 11448256Seric case 'u': /* set default uid */ 114517474Seric DefUid = atoi(val); 114640973Sbostic setdefuser(); 11478256Seric break; 11488256Seric 11498269Seric case 'v': /* run in verbose mode */ 11509381Seric Verbose = atobool(val); 11518256Seric break; 11528256Seric 115351216Seric case 'w': /* we don't have wildcard MX records */ 115451216Seric NoWildcardMX = atobool(val); 115550537Seric break; 115650537Seric 115714879Seric case 'x': /* load avg at which to auto-queue msgs */ 115814879Seric QueueLA = atoi(val); 115914879Seric break; 116014879Seric 116114879Seric case 'X': /* load avg at which to auto-reject connections */ 116214879Seric RefuseLA = atoi(val); 116314879Seric break; 116414879Seric 116524981Seric case 'y': /* work recipient factor */ 116624981Seric WkRecipFact = atoi(val); 116724981Seric break; 116824981Seric 116924981Seric case 'Y': /* fork jobs during queue runs */ 117024952Seric ForkQueueRuns = atobool(val); 117124952Seric break; 117224952Seric 117324981Seric case 'z': /* work message class factor */ 117424981Seric WkClassFact = atoi(val); 117524981Seric break; 117624981Seric 117724981Seric case 'Z': /* work time factor */ 117824981Seric WkTimeFact = atoi(val); 117924981Seric break; 118024981Seric 11818256Seric default: 11828256Seric break; 11838256Seric } 118416878Seric if (sticky) 118516878Seric setbitn(opt, StickyOpt); 11869188Seric return; 11878256Seric } 118810687Seric /* 118910687Seric ** SETCLASS -- set a word into a class 119010687Seric ** 119110687Seric ** Parameters: 119210687Seric ** class -- the class to put the word in. 119310687Seric ** word -- the word to enter 119410687Seric ** 119510687Seric ** Returns: 119610687Seric ** none. 119710687Seric ** 119810687Seric ** Side Effects: 119910687Seric ** puts the word into the symbol table. 120010687Seric */ 120110687Seric 120210687Seric setclass(class, word) 120310687Seric int class; 120410687Seric char *word; 120510687Seric { 120610687Seric register STAB *s; 120710687Seric 120810687Seric s = stab(word, ST_CLASS, ST_ENTER); 120910687Seric setbitn(class, s->s_class); 121010687Seric } 121153654Seric /* 121253654Seric ** MAKEMAPENTRY -- create a map entry 121353654Seric ** 121453654Seric ** Parameters: 121553654Seric ** line -- the config file line 121653654Seric ** 121753654Seric ** Returns: 121853654Seric ** TRUE if it successfully entered the map entry. 121953654Seric ** FALSE otherwise (usually syntax error). 122053654Seric ** 122153654Seric ** Side Effects: 122253654Seric ** Enters the map into the dictionary. 122353654Seric */ 122453654Seric 122553654Seric void 122653654Seric makemapentry(line) 122753654Seric char *line; 122853654Seric { 122953654Seric register char *p; 123053654Seric char *mapname; 123153654Seric char *classname; 123253654Seric register STAB *map; 123353654Seric STAB *class; 123453654Seric 123553654Seric for (p = line; isspace(*p); p++) 123653654Seric continue; 123753654Seric if (!isalnum(*p)) 123853654Seric { 123953654Seric syserr("readcf: config K line: no map name"); 124053654Seric return; 124153654Seric } 124253654Seric 124353654Seric mapname = p; 124453654Seric while (isalnum(*++p)) 124553654Seric continue; 124653654Seric if (*p != '\0') 124753654Seric *p++ = '\0'; 124853654Seric while (isspace(*p)) 124953654Seric p++; 125053654Seric if (!isalnum(*p)) 125153654Seric { 125253654Seric syserr("readcf: config K line, map %s: no map class", mapname); 125353654Seric return; 125453654Seric } 125553654Seric classname = p; 125653654Seric while (isalnum(*++p)) 125753654Seric continue; 125853654Seric if (*p != '\0') 125953654Seric *p++ = '\0'; 126053654Seric while (isspace(*p)) 126153654Seric p++; 126253654Seric 126353654Seric /* look up the class */ 126453654Seric class = stab(classname, ST_MAPCLASS, ST_FIND); 126553654Seric if (class == NULL) 126653654Seric { 126753654Seric syserr("readcf: map %s: class %s not available", mapname, classname); 126853654Seric return; 126953654Seric } 127053654Seric 127153654Seric /* enter the map */ 127253654Seric map = stab(mapname, ST_MAP, ST_ENTER); 127353654Seric map->s_map.map_class = &class->s_mapclass; 127453654Seric 127556823Seric if ((*class->s_mapclass.map_init)(&map->s_map, mapname, p)) 127653654Seric map->s_map.map_flags |= MF_VALID; 127753654Seric } 1278