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*58082Seric static char sccsid[] = "@(#)readcf.c 6.9 (Berkeley) 02/20/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 2157736Seric /* System 5 compatibility */ 2257736Seric #ifndef S_ISREG 2357736Seric #define S_ISREG(foo) ((foo & S_IFREG) == S_IFREG) 2457736Seric #endif 2557736Seric #ifndef S_IWGRP 2657736Seric #define S_IWGRP 020 2757736Seric #endif 2857736Seric #ifndef S_IWOTH 2957736Seric #define S_IWOTH 002 3057736Seric #endif 3157736Seric 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 14258050Seric /* map $ into \201 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 15058050Seric switch (*--p & 0377) 15152647Seric { 15258050Seric case MACROEXPAND: 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 */ 16458050Seric while (isascii(*p) && 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 */ 18658050Seric *p = MACROEXPAND; 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 { 23358050Seric switch (**ap & 0377) 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 { 26658050Seric if ((**ap & 0377) != 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 */ 30358050Seric for (p = &bp[2]; 30458050Seric *p != '\0' && !(isascii(*p) && isspace(*p)); 30558050Seric p++) 3064432Seric continue; 3074432Seric if (*p == '\0') 3084432Seric p = "%s"; 3094432Seric else 3104432Seric { 3114432Seric *p = '\0'; 31258050Seric while (isascii(*++p) && isspace(*p)) 3134432Seric continue; 3144432Seric } 31557135Seric fileclass(bp[1], &bp[2], p, safe); 3164432Seric break; 3174432Seric } 3184061Seric 3194432Seric /* scan the list of words and set class for all */ 32057135Seric for (p = &bp[2]; *p != '\0'; ) 3214061Seric { 3224061Seric register char *wd; 3234061Seric char delim; 3244061Seric 32558050Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 3264061Seric p++; 3274061Seric wd = p; 32858050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 3294061Seric p++; 3304061Seric delim = *p; 3314061Seric *p = '\0'; 3324061Seric if (wd[0] != '\0') 33357135Seric setclass(bp[1], wd); 3344061Seric *p = delim; 3354061Seric } 3364061Seric break; 3374061Seric 3384096Seric case 'M': /* define mailer */ 33957135Seric makemailer(&bp[1]); 3404096Seric break; 3414096Seric 3428252Seric case 'O': /* set option */ 34357135Seric setoption(bp[1], &bp[2], safe, FALSE); 3448252Seric break; 3458252Seric 3468252Seric case 'P': /* set precedence */ 3478252Seric if (NumPriorities >= MAXPRIORITIES) 3488252Seric { 3498547Seric toomany('P', MAXPRIORITIES); 3508252Seric break; 3518252Seric } 35257135Seric for (p = &bp[1]; *p != '\0' && *p != '=' && *p != '\t'; p++) 3538252Seric continue; 3548252Seric if (*p == '\0') 3558252Seric goto badline; 3568252Seric *p = '\0'; 35757135Seric Priorities[NumPriorities].pri_name = newstr(&bp[1]); 3588252Seric Priorities[NumPriorities].pri_val = atoi(++p); 3598252Seric NumPriorities++; 3608252Seric break; 3618252Seric 3628547Seric case 'T': /* trusted user(s) */ 36357135Seric p = &bp[1]; 3648547Seric while (*p != '\0') 3658547Seric { 36658050Seric while (isascii(*p) && isspace(*p)) 3678547Seric p++; 3688547Seric q = p; 36958050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 3708547Seric p++; 3718547Seric if (*p != '\0') 3728547Seric *p++ = '\0'; 3738547Seric if (*q == '\0') 3748547Seric continue; 3758547Seric for (pv = TrustedUsers; *pv != NULL; pv++) 3768547Seric continue; 3778547Seric if (pv >= &TrustedUsers[MAXTRUST]) 3788547Seric { 3798547Seric toomany('T', MAXTRUST); 3808547Seric break; 3818547Seric } 3828547Seric *pv = newstr(q); 3838547Seric } 3848547Seric break; 3858547Seric 38652645Seric case 'V': /* configuration syntax version */ 38757135Seric ConfigLevel = atoi(&bp[1]); 38852645Seric break; 38952645Seric 39053654Seric case 'K': 39157135Seric makemapentry(&bp[1]); 39253654Seric break; 39353654Seric 3943308Seric default: 3954061Seric badline: 39657135Seric syserr("unknown control line \"%s\"", bp); 3973308Seric } 39857135Seric if (bp != buf) 39957135Seric free(bp); 4003308Seric } 40152637Seric if (ferror(cf)) 40252637Seric { 40352647Seric syserr("I/O read error", cfname); 40452637Seric exit(EX_OSFILE); 40552637Seric } 40652637Seric fclose(cf); 4079381Seric FileName = NULL; 40856836Seric 40957076Seric if (stab("host", ST_MAP, ST_FIND) == NULL) 41057076Seric { 41157076Seric /* user didn't initialize: set up host map */ 41257076Seric strcpy(buf, "host host"); 41357076Seric if (ConfigLevel >= 2) 41457076Seric strcat(buf, " -a."); 41557076Seric makemapentry(buf); 41657076Seric } 4174096Seric } 4184096Seric /* 4198547Seric ** TOOMANY -- signal too many of some option 4208547Seric ** 4218547Seric ** Parameters: 4228547Seric ** id -- the id of the error line 4238547Seric ** maxcnt -- the maximum possible values 4248547Seric ** 4258547Seric ** Returns: 4268547Seric ** none. 4278547Seric ** 4288547Seric ** Side Effects: 4298547Seric ** gives a syserr. 4308547Seric */ 4318547Seric 4328547Seric toomany(id, maxcnt) 4338547Seric char id; 4348547Seric int maxcnt; 4358547Seric { 4369381Seric syserr("too many %c lines, %d max", id, maxcnt); 4378547Seric } 4388547Seric /* 4394432Seric ** FILECLASS -- read members of a class from a file 4404432Seric ** 4414432Seric ** Parameters: 4424432Seric ** class -- class to define. 4434432Seric ** filename -- name of file to read. 4444432Seric ** fmt -- scanf string to use for match. 4454432Seric ** 4464432Seric ** Returns: 4474432Seric ** none 4484432Seric ** 4494432Seric ** Side Effects: 4504432Seric ** 4514432Seric ** puts all lines in filename that match a scanf into 4524432Seric ** the named class. 4534432Seric */ 4544432Seric 45554973Seric fileclass(class, filename, fmt, safe) 4564432Seric int class; 4574432Seric char *filename; 4584432Seric char *fmt; 45954973Seric bool safe; 4604432Seric { 46125808Seric FILE *f; 46254973Seric struct stat stbuf; 4634432Seric char buf[MAXLINE]; 4644432Seric 46554973Seric if (stat(filename, &stbuf) < 0) 46654973Seric { 46754973Seric syserr("fileclass: cannot stat %s", filename); 46854973Seric return; 46954973Seric } 47054973Seric if (!S_ISREG(stbuf.st_mode)) 47154973Seric { 47254973Seric syserr("fileclass: %s not a regular file", filename); 47354973Seric return; 47454973Seric } 47554973Seric if (!safe && access(filename, R_OK) < 0) 47654973Seric { 47754973Seric syserr("fileclass: access denied on %s", filename); 47854973Seric return; 47954973Seric } 48054973Seric f = fopen(filename, "r"); 4814432Seric if (f == NULL) 4824432Seric { 48354973Seric syserr("fileclass: cannot open %s", filename); 4844432Seric return; 4854432Seric } 4864432Seric 4874432Seric while (fgets(buf, sizeof buf, f) != NULL) 4884432Seric { 4894432Seric register STAB *s; 49025808Seric register char *p; 49125808Seric # ifdef SCANF 4924432Seric char wordbuf[MAXNAME+1]; 4934432Seric 4944432Seric if (sscanf(buf, fmt, wordbuf) != 1) 4954432Seric continue; 49625808Seric p = wordbuf; 49756795Seric # else /* SCANF */ 49825808Seric p = buf; 49956795Seric # endif /* SCANF */ 50025808Seric 50125808Seric /* 50225808Seric ** Break up the match into words. 50325808Seric */ 50425808Seric 50525808Seric while (*p != '\0') 50625808Seric { 50725808Seric register char *q; 50825808Seric 50925808Seric /* strip leading spaces */ 51058050Seric while (isascii(*p) && isspace(*p)) 51125808Seric p++; 51225808Seric if (*p == '\0') 51325808Seric break; 51425808Seric 51525808Seric /* find the end of the word */ 51625808Seric q = p; 51758050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 51825808Seric p++; 51925808Seric if (*p != '\0') 52025808Seric *p++ = '\0'; 52125808Seric 52225808Seric /* enter the word in the symbol table */ 52325808Seric s = stab(q, ST_CLASS, ST_ENTER); 52425808Seric setbitn(class, s->s_class); 52525808Seric } 5264432Seric } 5274432Seric 52854973Seric (void) fclose(f); 5294432Seric } 5304432Seric /* 5314096Seric ** MAKEMAILER -- define a new mailer. 5324096Seric ** 5334096Seric ** Parameters: 53410327Seric ** line -- description of mailer. This is in labeled 53510327Seric ** fields. The fields are: 53610327Seric ** P -- the path to the mailer 53710327Seric ** F -- the flags associated with the mailer 53810327Seric ** A -- the argv for this mailer 53910327Seric ** S -- the sender rewriting set 54010327Seric ** R -- the recipient rewriting set 54110327Seric ** E -- the eol string 54210327Seric ** The first word is the canonical name of the mailer. 5434096Seric ** 5444096Seric ** Returns: 5454096Seric ** none. 5464096Seric ** 5474096Seric ** Side Effects: 5484096Seric ** enters the mailer into the mailer table. 5494096Seric */ 5503308Seric 55121066Seric makemailer(line) 5524096Seric char *line; 5534096Seric { 5544096Seric register char *p; 5558067Seric register struct mailer *m; 5568067Seric register STAB *s; 5578067Seric int i; 55810327Seric char fcode; 55958020Seric auto char *endp; 5604096Seric extern int NextMailer; 56110327Seric extern char **makeargv(); 56210327Seric extern char *munchstring(); 56310327Seric extern char *DelimChar; 56410701Seric extern long atol(); 5654096Seric 56610327Seric /* allocate a mailer and set up defaults */ 56710327Seric m = (struct mailer *) xalloc(sizeof *m); 56810327Seric bzero((char *) m, sizeof *m); 56910327Seric m->m_eol = "\n"; 57010327Seric 57110327Seric /* collect the mailer name */ 57258050Seric for (p = line; *p != '\0' && *p != ',' && !(isascii(*p) && isspace(*p)); p++) 57310327Seric continue; 57410327Seric if (*p != '\0') 57510327Seric *p++ = '\0'; 57610327Seric m->m_name = newstr(line); 57710327Seric 57810327Seric /* now scan through and assign info from the fields */ 57910327Seric while (*p != '\0') 58010327Seric { 58158050Seric while (*p != '\0' && (*p == ',' || (isascii(*p) && isspace(*p)))) 58210327Seric p++; 58310327Seric 58410327Seric /* p now points to field code */ 58510327Seric fcode = *p; 58610327Seric while (*p != '\0' && *p != '=' && *p != ',') 58710327Seric p++; 58810327Seric if (*p++ != '=') 58910327Seric { 59052637Seric syserr("mailer %s: `=' expected", m->m_name); 59110327Seric return; 59210327Seric } 59358050Seric while (isascii(*p) && isspace(*p)) 59410327Seric p++; 59510327Seric 59610327Seric /* p now points to the field body */ 59710327Seric p = munchstring(p); 59810327Seric 59910327Seric /* install the field into the mailer struct */ 60010327Seric switch (fcode) 60110327Seric { 60210327Seric case 'P': /* pathname */ 60310327Seric m->m_mailer = newstr(p); 60410327Seric break; 60510327Seric 60610327Seric case 'F': /* flags */ 60710687Seric for (; *p != '\0'; p++) 60858050Seric if (!(isascii(*p) && isspace(*p))) 60952637Seric setbitn(*p, m->m_flags); 61010327Seric break; 61110327Seric 61210327Seric case 'S': /* sender rewriting ruleset */ 61310327Seric case 'R': /* recipient rewriting ruleset */ 61458020Seric i = strtol(p, &endp, 10); 61510327Seric if (i < 0 || i >= MAXRWSETS) 61610327Seric { 61710327Seric syserr("invalid rewrite set, %d max", MAXRWSETS); 61810327Seric return; 61910327Seric } 62010327Seric if (fcode == 'S') 62158020Seric m->m_sh_rwset = m->m_se_rwset = i; 62210327Seric else 62358020Seric m->m_rh_rwset = m->m_re_rwset = i; 62458020Seric 62558020Seric p = endp; 62658020Seric if (*p == '/') 62758020Seric { 62858020Seric i = strtol(p, NULL, 10); 62958020Seric if (i < 0 || i >= MAXRWSETS) 63058020Seric { 63158020Seric syserr("invalid rewrite set, %d max", 63258020Seric MAXRWSETS); 63358020Seric return; 63458020Seric } 63558020Seric if (fcode == 'S') 63658020Seric m->m_sh_rwset = i; 63758020Seric else 63858020Seric m->m_rh_rwset = i; 63958020Seric } 64010327Seric break; 64110327Seric 64210327Seric case 'E': /* end of line string */ 64310327Seric m->m_eol = newstr(p); 64410327Seric break; 64510327Seric 64610327Seric case 'A': /* argument vector */ 64710327Seric m->m_argv = makeargv(p); 64810327Seric break; 64910701Seric 65010701Seric case 'M': /* maximum message size */ 65110701Seric m->m_maxsize = atol(p); 65210701Seric break; 65352106Seric 65452106Seric case 'L': /* maximum line length */ 65552106Seric m->m_linelimit = atoi(p); 65652106Seric break; 65710327Seric } 65810327Seric 65910327Seric p = DelimChar; 66010327Seric } 66110327Seric 66252106Seric /* do some heuristic cleanup for back compatibility */ 66352106Seric if (bitnset(M_LIMITS, m->m_flags)) 66452106Seric { 66552106Seric if (m->m_linelimit == 0) 66652106Seric m->m_linelimit = SMTPLINELIM; 66755418Seric if (ConfigLevel < 2) 66852106Seric setbitn(M_7BITS, m->m_flags); 66952106Seric } 67052106Seric 6714096Seric if (NextMailer >= MAXMAILERS) 6724096Seric { 6739381Seric syserr("too many mailers defined (%d max)", MAXMAILERS); 6744096Seric return; 6754096Seric } 67657402Seric 67710327Seric s = stab(m->m_name, ST_MAILER, ST_ENTER); 67857402Seric if (s->s_mailer != NULL) 67957402Seric { 68057402Seric i = s->s_mailer->m_mno; 68157402Seric free(s->s_mailer); 68257402Seric } 68357402Seric else 68457402Seric { 68557402Seric i = NextMailer++; 68657402Seric } 68757402Seric Mailer[i] = s->s_mailer = m; 68857454Seric m->m_mno = i; 68910327Seric } 69010327Seric /* 69110327Seric ** MUNCHSTRING -- translate a string into internal form. 69210327Seric ** 69310327Seric ** Parameters: 69410327Seric ** p -- the string to munch. 69510327Seric ** 69610327Seric ** Returns: 69710327Seric ** the munched string. 69810327Seric ** 69910327Seric ** Side Effects: 70010327Seric ** Sets "DelimChar" to point to the string that caused us 70110327Seric ** to stop. 70210327Seric */ 7034096Seric 70410327Seric char * 70510327Seric munchstring(p) 70610327Seric register char *p; 70710327Seric { 70810327Seric register char *q; 70910327Seric bool backslash = FALSE; 71010327Seric bool quotemode = FALSE; 71110327Seric static char buf[MAXLINE]; 71210327Seric extern char *DelimChar; 7134096Seric 71410327Seric for (q = buf; *p != '\0'; p++) 7154096Seric { 71610327Seric if (backslash) 71710327Seric { 71810327Seric /* everything is roughly literal */ 71910357Seric backslash = FALSE; 72010327Seric switch (*p) 72110327Seric { 72210327Seric case 'r': /* carriage return */ 72310327Seric *q++ = '\r'; 72410327Seric continue; 72510327Seric 72610327Seric case 'n': /* newline */ 72710327Seric *q++ = '\n'; 72810327Seric continue; 72910327Seric 73010327Seric case 'f': /* form feed */ 73110327Seric *q++ = '\f'; 73210327Seric continue; 73310327Seric 73410327Seric case 'b': /* backspace */ 73510327Seric *q++ = '\b'; 73610327Seric continue; 73710327Seric } 73810327Seric *q++ = *p; 73910327Seric } 74010327Seric else 74110327Seric { 74210327Seric if (*p == '\\') 74310327Seric backslash = TRUE; 74410327Seric else if (*p == '"') 74510327Seric quotemode = !quotemode; 74610327Seric else if (quotemode || *p != ',') 74710327Seric *q++ = *p; 74810327Seric else 74910327Seric break; 75010327Seric } 7514096Seric } 7524096Seric 75310327Seric DelimChar = p; 75410327Seric *q++ = '\0'; 75510327Seric return (buf); 75610327Seric } 75710327Seric /* 75810327Seric ** MAKEARGV -- break up a string into words 75910327Seric ** 76010327Seric ** Parameters: 76110327Seric ** p -- the string to break up. 76210327Seric ** 76310327Seric ** Returns: 76410327Seric ** a char **argv (dynamically allocated) 76510327Seric ** 76610327Seric ** Side Effects: 76710327Seric ** munges p. 76810327Seric */ 7694096Seric 77010327Seric char ** 77110327Seric makeargv(p) 77210327Seric register char *p; 77310327Seric { 77410327Seric char *q; 77510327Seric int i; 77610327Seric char **avp; 77710327Seric char *argv[MAXPV + 1]; 77810327Seric 77910327Seric /* take apart the words */ 78010327Seric i = 0; 78110327Seric while (*p != '\0' && i < MAXPV) 7824096Seric { 78310327Seric q = p; 78458050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 78510327Seric p++; 78658050Seric while (isascii(*p) && isspace(*p)) 78710327Seric *p++ = '\0'; 78810327Seric argv[i++] = newstr(q); 7894096Seric } 79010327Seric argv[i++] = NULL; 7914096Seric 79210327Seric /* now make a copy of the argv */ 79310327Seric avp = (char **) xalloc(sizeof *avp * i); 79416893Seric bcopy((char *) argv, (char *) avp, sizeof *avp * i); 79510327Seric 79610327Seric return (avp); 7973308Seric } 7983308Seric /* 7993308Seric ** PRINTRULES -- print rewrite rules (for debugging) 8003308Seric ** 8013308Seric ** Parameters: 8023308Seric ** none. 8033308Seric ** 8043308Seric ** Returns: 8053308Seric ** none. 8063308Seric ** 8073308Seric ** Side Effects: 8083308Seric ** prints rewrite rules. 8093308Seric */ 8103308Seric 8113308Seric printrules() 8123308Seric { 8133308Seric register struct rewrite *rwp; 8144072Seric register int ruleset; 8153308Seric 8164072Seric for (ruleset = 0; ruleset < 10; ruleset++) 8173308Seric { 8184072Seric if (RewriteRules[ruleset] == NULL) 8194072Seric continue; 8208067Seric printf("\n----Rule Set %d:", ruleset); 8213308Seric 8224072Seric for (rwp = RewriteRules[ruleset]; rwp != NULL; rwp = rwp->r_next) 8233308Seric { 8248067Seric printf("\nLHS:"); 8258067Seric printav(rwp->r_lhs); 8268067Seric printf("RHS:"); 8278067Seric printav(rwp->r_rhs); 8283308Seric } 8293308Seric } 8303308Seric } 8314319Seric 8324096Seric /* 8338256Seric ** SETOPTION -- set global processing option 8348256Seric ** 8358256Seric ** Parameters: 8368256Seric ** opt -- option name. 8378256Seric ** val -- option value (as a text string). 83821755Seric ** safe -- set if this came from a configuration file. 83921755Seric ** Some options (if set from the command line) will 84021755Seric ** reset the user id to avoid security problems. 8418269Seric ** sticky -- if set, don't let other setoptions override 8428269Seric ** this value. 8438256Seric ** 8448256Seric ** Returns: 8458256Seric ** none. 8468256Seric ** 8478256Seric ** Side Effects: 8488256Seric ** Sets options as implied by the arguments. 8498256Seric */ 8508256Seric 85110687Seric static BITMAP StickyOpt; /* set if option is stuck */ 8528269Seric 85357207Seric 85457207Seric #ifdef NAMED_BIND 85557207Seric 85657207Seric struct resolverflags 85757207Seric { 85857207Seric char *rf_name; /* name of the flag */ 85957207Seric long rf_bits; /* bits to set/clear */ 86057207Seric } ResolverFlags[] = 86157207Seric { 86257207Seric "debug", RES_DEBUG, 86357207Seric "aaonly", RES_AAONLY, 86457207Seric "usevc", RES_USEVC, 86557207Seric "primary", RES_PRIMARY, 86657207Seric "igntc", RES_IGNTC, 86757207Seric "recurse", RES_RECURSE, 86857207Seric "defnames", RES_DEFNAMES, 86957207Seric "stayopen", RES_STAYOPEN, 87057207Seric "dnsrch", RES_DNSRCH, 87157207Seric NULL, 0 87257207Seric }; 87357207Seric 87457207Seric #endif 87557207Seric 87621755Seric setoption(opt, val, safe, sticky) 8778256Seric char opt; 8788256Seric char *val; 87921755Seric bool safe; 8808269Seric bool sticky; 8818256Seric { 88257207Seric register char *p; 8838265Seric extern bool atobool(); 88412633Seric extern time_t convtime(); 88514879Seric extern int QueueLA; 88614879Seric extern int RefuseLA; 88717474Seric extern bool trusteduser(); 88817474Seric extern char *username(); 8898256Seric 8908256Seric if (tTd(37, 1)) 8919341Seric printf("setoption %c=%s", opt, val); 8928256Seric 8938256Seric /* 8948269Seric ** See if this option is preset for us. 8958256Seric */ 8968256Seric 89710687Seric if (bitnset(opt, StickyOpt)) 8988269Seric { 8999341Seric if (tTd(37, 1)) 9009341Seric printf(" (ignored)\n"); 9018269Seric return; 9028269Seric } 9038269Seric 90421755Seric /* 90521755Seric ** Check to see if this option can be specified by this user. 90621755Seric */ 90721755Seric 90836238Skarels if (!safe && getuid() == 0) 90921755Seric safe = TRUE; 910*58082Seric if (!safe && strchr("bdeEiLmoprsvC8", opt) == NULL) 91121755Seric { 91239111Srick if (opt != 'M' || (val[0] != 'r' && val[0] != 's')) 91321755Seric { 91436582Sbostic if (tTd(37, 1)) 91536582Sbostic printf(" (unsafe)"); 91636582Sbostic if (getuid() != geteuid()) 91736582Sbostic { 91851210Seric if (tTd(37, 1)) 91951210Seric printf("(Resetting uid)"); 92036582Sbostic (void) setgid(getgid()); 92136582Sbostic (void) setuid(getuid()); 92236582Sbostic } 92321755Seric } 92421755Seric } 92551210Seric if (tTd(37, 1)) 92617985Seric printf("\n"); 9278269Seric 9288256Seric switch (opt) 9298256Seric { 93052106Seric case '8': /* allow eight-bit input */ 93152106Seric EightBit = atobool(val); 93252106Seric break; 93352106Seric 9348256Seric case 'A': /* set default alias file */ 9359381Seric if (val[0] == '\0') 9368269Seric AliasFile = "aliases"; 9379381Seric else 9389381Seric AliasFile = newstr(val); 9398256Seric break; 9408256Seric 94117474Seric case 'a': /* look N minutes for "@:@" in alias file */ 94217474Seric if (val[0] == '\0') 94317474Seric SafeAlias = 5; 94417474Seric else 94517474Seric SafeAlias = atoi(val); 94617474Seric break; 94717474Seric 94816843Seric case 'B': /* substitution for blank character */ 94916843Seric SpaceSub = val[0]; 95016843Seric if (SpaceSub == '\0') 95116843Seric SpaceSub = ' '; 95216843Seric break; 95316843Seric 954*58082Seric case 'b': /* minimum number of blocks free on queue fs */ 955*58082Seric MinBlocksFree = atol(val); 956*58082Seric break; 957*58082Seric 9589284Seric case 'c': /* don't connect to "expensive" mailers */ 9599381Seric NoConnect = atobool(val); 9609284Seric break; 9619284Seric 96251305Seric case 'C': /* checkpoint every N addresses */ 96351305Seric CheckpointInterval = atoi(val); 96424944Seric break; 96524944Seric 9669284Seric case 'd': /* delivery mode */ 9679284Seric switch (*val) 9688269Seric { 9699284Seric case '\0': 9709284Seric SendMode = SM_DELIVER; 9718269Seric break; 9728269Seric 97310755Seric case SM_QUEUE: /* queue only */ 97410755Seric #ifndef QUEUE 97510755Seric syserr("need QUEUE to set -odqueue"); 97656795Seric #endif /* QUEUE */ 97710755Seric /* fall through..... */ 97810755Seric 9799284Seric case SM_DELIVER: /* do everything */ 9809284Seric case SM_FORK: /* fork after verification */ 9819284Seric SendMode = *val; 9828269Seric break; 9838269Seric 9848269Seric default: 9859284Seric syserr("Unknown delivery mode %c", *val); 9868269Seric exit(EX_USAGE); 9878269Seric } 9888269Seric break; 9898269Seric 9909146Seric case 'D': /* rebuild alias database as needed */ 9919381Seric AutoRebuild = atobool(val); 9929146Seric break; 9939146Seric 99455372Seric case 'E': /* error message header/header file */ 99555379Seric if (*val != '\0') 99655379Seric ErrMsgFile = newstr(val); 99755372Seric break; 99855372Seric 9998269Seric case 'e': /* set error processing mode */ 10008269Seric switch (*val) 10018269Seric { 10029381Seric case EM_QUIET: /* be silent about it */ 10039381Seric case EM_MAIL: /* mail back */ 10049381Seric case EM_BERKNET: /* do berknet error processing */ 10059381Seric case EM_WRITE: /* write back (or mail) */ 10068269Seric HoldErrs = TRUE; 10079381Seric /* fall through... */ 10088269Seric 10099381Seric case EM_PRINT: /* print errors normally (default) */ 10109381Seric ErrorMode = *val; 10118269Seric break; 10128269Seric } 10138269Seric break; 10148269Seric 10159049Seric case 'F': /* file mode */ 101617975Seric FileMode = atooct(val) & 0777; 10179049Seric break; 10189049Seric 10198269Seric case 'f': /* save Unix-style From lines on front */ 10209381Seric SaveFrom = atobool(val); 10218269Seric break; 10228269Seric 102353735Seric case 'G': /* match recipients against GECOS field */ 102453735Seric MatchGecos = atobool(val); 102553735Seric break; 102653735Seric 10278256Seric case 'g': /* default gid */ 102817474Seric DefGid = atoi(val); 10298256Seric break; 10308256Seric 10318256Seric case 'H': /* help file */ 10329381Seric if (val[0] == '\0') 10338269Seric HelpFile = "sendmail.hf"; 10349381Seric else 10359381Seric HelpFile = newstr(val); 10368256Seric break; 10378256Seric 103851305Seric case 'h': /* maximum hop count */ 103951305Seric MaxHopCount = atoi(val); 104051305Seric break; 104151305Seric 104235651Seric case 'I': /* use internet domain name server */ 104357207Seric #ifdef NAMED_BIND 104457207Seric UseNameServer = TRUE; 104557207Seric for (p = val; *p != 0; ) 104657207Seric { 104757207Seric bool clearmode; 104857207Seric char *q; 104957207Seric struct resolverflags *rfp; 105057207Seric 105157207Seric while (*p == ' ') 105257207Seric p++; 105357207Seric if (*p == '\0') 105457207Seric break; 105557207Seric clearmode = FALSE; 105657207Seric if (*p == '-') 105757207Seric clearmode = TRUE; 105857207Seric else if (*p != '+') 105957207Seric p--; 106057207Seric p++; 106157207Seric q = p; 106258050Seric while (*p != '\0' && !(isascii(*p) && isspace(*p))) 106357207Seric p++; 106457207Seric if (*p != '\0') 106557207Seric *p++ = '\0'; 106657207Seric for (rfp = ResolverFlags; rfp->rf_name != NULL; rfp++) 106757207Seric { 106857207Seric if (strcasecmp(q, rfp->rf_name) == 0) 106957207Seric break; 107057207Seric } 107157207Seric if (clearmode) 107257207Seric _res.options &= ~rfp->rf_bits; 107357207Seric else 107457207Seric _res.options |= rfp->rf_bits; 107557207Seric } 107657207Seric if (tTd(8, 2)) 107757207Seric printf("_res.options = %x\n", _res.options); 107857207Seric #else 107957207Seric usrerr("name server (I option) specified but BIND not compiled in"); 108057207Seric #endif 108135651Seric break; 108235651Seric 10838269Seric case 'i': /* ignore dot lines in message */ 10849381Seric IgnrDot = atobool(val); 10858269Seric break; 10868269Seric 108757136Seric case 'J': /* .forward search path */ 108857136Seric ForwardPath = newstr(val); 108957136Seric break; 109057136Seric 109154967Seric case 'k': /* connection cache size */ 109254967Seric MaxMciCache = atoi(val); 109356215Seric if (MaxMciCache < 0) 109456215Seric MaxMciCache = 0; 109554967Seric break; 109654967Seric 109754967Seric case 'K': /* connection cache timeout */ 109854967Seric MciCacheTimeout = convtime(val); 109954967Seric break; 110054967Seric 11018256Seric case 'L': /* log level */ 11029381Seric LogLevel = atoi(val); 11038256Seric break; 11048256Seric 11058269Seric case 'M': /* define macro */ 11069381Seric define(val[0], newstr(&val[1]), CurEnv); 110716878Seric sticky = FALSE; 11088269Seric break; 11098269Seric 11108269Seric case 'm': /* send to me too */ 11119381Seric MeToo = atobool(val); 11128269Seric break; 11138269Seric 111425820Seric case 'n': /* validate RHS in newaliases */ 111525820Seric CheckAliases = atobool(val); 111625820Seric break; 111725820Seric 11188269Seric case 'o': /* assume old style headers */ 11199381Seric if (atobool(val)) 11209341Seric CurEnv->e_flags |= EF_OLDSTYLE; 11219341Seric else 11229341Seric CurEnv->e_flags &= ~EF_OLDSTYLE; 11238269Seric break; 11248269Seric 1125*58082Seric case 'p': /* select privacy level */ 1126*58082Seric p = val; 1127*58082Seric for (;;) 1128*58082Seric { 1129*58082Seric register struct prival *pv; 1130*58082Seric extern struct prival PrivacyValues[]; 1131*58082Seric 1132*58082Seric while (isascii(*p) && (isspace(*p) || ispunct(*p))) 1133*58082Seric p++; 1134*58082Seric if (*p == '\0') 1135*58082Seric break; 1136*58082Seric val = p; 1137*58082Seric while (isascii(*p) && isalnum(*p)) 1138*58082Seric p++; 1139*58082Seric if (*p != '\0') 1140*58082Seric *p++ = '\0'; 1141*58082Seric 1142*58082Seric for (pv = PrivacyValues; pv->pv_name != NULL; pv++) 1143*58082Seric { 1144*58082Seric if (strcasecmp(val, pv->pv_name) == 0) 1145*58082Seric break; 1146*58082Seric } 1147*58082Seric PrivacyFlags |= pv->pv_flag; 1148*58082Seric } 1149*58082Seric break; 1150*58082Seric 115124944Seric case 'P': /* postmaster copy address for returned mail */ 115224944Seric PostMasterCopy = newstr(val); 115324944Seric break; 115424944Seric 115524944Seric case 'q': /* slope of queue only function */ 115624944Seric QueueFactor = atoi(val); 115724944Seric break; 115824944Seric 11598256Seric case 'Q': /* queue directory */ 11609381Seric if (val[0] == '\0') 11618269Seric QueueDir = "mqueue"; 11629381Seric else 11639381Seric QueueDir = newstr(val); 11648256Seric break; 11658256Seric 11668256Seric case 'r': /* read timeout */ 11679381Seric ReadTimeout = convtime(val); 11688256Seric break; 11698256Seric 11708256Seric case 'S': /* status file */ 11719381Seric if (val[0] == '\0') 11728269Seric StatFile = "sendmail.st"; 11739381Seric else 11749381Seric StatFile = newstr(val); 11758256Seric break; 11768256Seric 11778265Seric case 's': /* be super safe, even if expensive */ 11789381Seric SuperSafe = atobool(val); 11798256Seric break; 11808256Seric 11818256Seric case 'T': /* queue timeout */ 11829381Seric TimeOut = convtime(val); 118354967Seric break; 11848256Seric 11858265Seric case 't': /* time zone name */ 118652106Seric TimeZoneSpec = newstr(val); 11878265Seric break; 11888265Seric 118950556Seric case 'U': /* location of user database */ 119051360Seric UdbSpec = newstr(val); 119150556Seric break; 119250556Seric 11938256Seric case 'u': /* set default uid */ 119417474Seric DefUid = atoi(val); 119540973Sbostic setdefuser(); 11968256Seric break; 11978256Seric 11988269Seric case 'v': /* run in verbose mode */ 11999381Seric Verbose = atobool(val); 12008256Seric break; 12018256Seric 120214879Seric case 'x': /* load avg at which to auto-queue msgs */ 120314879Seric QueueLA = atoi(val); 120414879Seric break; 120514879Seric 120614879Seric case 'X': /* load avg at which to auto-reject connections */ 120714879Seric RefuseLA = atoi(val); 120814879Seric break; 120914879Seric 121024981Seric case 'y': /* work recipient factor */ 121124981Seric WkRecipFact = atoi(val); 121224981Seric break; 121324981Seric 121424981Seric case 'Y': /* fork jobs during queue runs */ 121524952Seric ForkQueueRuns = atobool(val); 121624952Seric break; 121724952Seric 121824981Seric case 'z': /* work message class factor */ 121924981Seric WkClassFact = atoi(val); 122024981Seric break; 122124981Seric 122224981Seric case 'Z': /* work time factor */ 122324981Seric WkTimeFact = atoi(val); 122424981Seric break; 122524981Seric 12268256Seric default: 12278256Seric break; 12288256Seric } 122916878Seric if (sticky) 123016878Seric setbitn(opt, StickyOpt); 12319188Seric return; 12328256Seric } 123310687Seric /* 123410687Seric ** SETCLASS -- set a word into a class 123510687Seric ** 123610687Seric ** Parameters: 123710687Seric ** class -- the class to put the word in. 123810687Seric ** word -- the word to enter 123910687Seric ** 124010687Seric ** Returns: 124110687Seric ** none. 124210687Seric ** 124310687Seric ** Side Effects: 124410687Seric ** puts the word into the symbol table. 124510687Seric */ 124610687Seric 124710687Seric setclass(class, word) 124810687Seric int class; 124910687Seric char *word; 125010687Seric { 125110687Seric register STAB *s; 125210687Seric 125357943Seric if (tTd(37, 8)) 125457943Seric printf("%s added to class %c\n", word, class); 125510687Seric s = stab(word, ST_CLASS, ST_ENTER); 125610687Seric setbitn(class, s->s_class); 125710687Seric } 125853654Seric /* 125953654Seric ** MAKEMAPENTRY -- create a map entry 126053654Seric ** 126153654Seric ** Parameters: 126253654Seric ** line -- the config file line 126353654Seric ** 126453654Seric ** Returns: 126553654Seric ** TRUE if it successfully entered the map entry. 126653654Seric ** FALSE otherwise (usually syntax error). 126753654Seric ** 126853654Seric ** Side Effects: 126953654Seric ** Enters the map into the dictionary. 127053654Seric */ 127153654Seric 127253654Seric void 127353654Seric makemapentry(line) 127453654Seric char *line; 127553654Seric { 127653654Seric register char *p; 127753654Seric char *mapname; 127853654Seric char *classname; 127953654Seric register STAB *map; 128053654Seric STAB *class; 128153654Seric 128258050Seric for (p = line; isascii(*p) && isspace(*p); p++) 128353654Seric continue; 128458050Seric if (!(isascii(*p) && isalnum(*p))) 128553654Seric { 128653654Seric syserr("readcf: config K line: no map name"); 128753654Seric return; 128853654Seric } 128953654Seric 129053654Seric mapname = p; 129158050Seric while (isascii(*++p) && isalnum(*p)) 129253654Seric continue; 129353654Seric if (*p != '\0') 129453654Seric *p++ = '\0'; 129558050Seric while (isascii(*p) && isspace(*p)) 129653654Seric p++; 129758050Seric if (!(isascii(*p) && isalnum(*p))) 129853654Seric { 129953654Seric syserr("readcf: config K line, map %s: no map class", mapname); 130053654Seric return; 130153654Seric } 130253654Seric classname = p; 130358050Seric while (isascii(*++p) && isalnum(*p)) 130453654Seric continue; 130553654Seric if (*p != '\0') 130653654Seric *p++ = '\0'; 130758050Seric while (isascii(*p) && isspace(*p)) 130853654Seric p++; 130953654Seric 131053654Seric /* look up the class */ 131153654Seric class = stab(classname, ST_MAPCLASS, ST_FIND); 131253654Seric if (class == NULL) 131353654Seric { 131453654Seric syserr("readcf: map %s: class %s not available", mapname, classname); 131553654Seric return; 131653654Seric } 131753654Seric 131853654Seric /* enter the map */ 131953654Seric map = stab(mapname, ST_MAP, ST_ENTER); 132053654Seric map->s_map.map_class = &class->s_mapclass; 132153654Seric 132256823Seric if ((*class->s_mapclass.map_init)(&map->s_map, mapname, p)) 132353654Seric map->s_map.map_flags |= MF_VALID; 132453654Seric } 1325