3 * configuration file parser; this file is actually #included from
4 * lexer.c, which is generated using flex from lexer.l, in turn from
5 * lexer.l.m4. It's in a separate file so that we don't have to worry
6 * about m4 quoting &c., but we have to #include it so that the C
7 * objects from the lexer are available.
9 * Copyright (C)1996-1997 Ian Jackson
11 * This is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with userv; if not, write to the Free Software
23 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 static int parse_file(const char *string, int *didexist);
27 static int parser(int allowce);
28 int parse_string(const char *string, const char *descrip, int isinternal);
31 * Error-handling routines
34 static void closeerrorfile(void) {
35 if (eh.file && !eh.filekeep) {
36 if (fclose(eh.file)) {
37 eh.handling= tokv_word_errorstostderr;
38 parseerrprint("error writing to error log file `%s': %s",
39 eh.filename,strerror(errno));
44 eh.file= 0; eh.filename= 0; eh.filekeep= 0;
47 static void senderrmsg(const char *errmsg, int useehandling) {
48 char suberrmsg[MAX_ERRMSG_LEN];
53 switch (useehandling) {
54 case tokv_word_errorstostderr:
55 senderrmsgstderr(errmsg);
57 case tokv_word_errorstosyslog:
58 ensurelogopen(eh.logfacility);
59 syslog(eh.loglevel,"%s",errmsg);
61 case tokv_word_errorstofile:
62 if (time(&now)==-1) syscallerror("get current time");
63 lt= localtime(&now); if (!lt) syscallerror("convert current time");
64 if (fprintf(eh.file,"%d-%02d-%02d %02d:%02d:%02d: uservd: %s\n",
65 lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday,
66 lt->tm_hour, lt->tm_min, lt->tm_sec,
69 closeerrorfile(); eh.handling= tokv_word_errorstofile;
70 snyprintf(suberrmsg,sizeof(suberrmsg),
71 "error writing to error log file `%.*s': %s;"
72 " reverting to errors-to-stderr",
73 (int)(sizeof(suberrmsg)>>1),eh.filename,strerror(e));
74 senderrmsg(suberrmsg,eh.handling);
75 senderrmsg(errmsg,eh.handling);
83 static void errwhere(struct parser_state *tstate, char *bufput, int bufputlen) {
84 static const char suffix[]= "references ...";
85 char errmsg[MAX_ERRMSG_LEN];
88 strnycpy(bufput,"<initialisation>: ",bufputlen);
91 if (!tstate->notedreferer && tstate->upstate && !tstate->upstate->isinternal) {
92 errwhere(tstate->upstate,errmsg,sizeof(errmsg)-sizeof(suffix));
93 strcat(errmsg,suffix);
94 senderrmsg(errmsg,eh.handling);
95 tstate->notedreferer= 1;
97 snyprintf(bufput,bufputlen,"%.*s:%d: ",bufputlen-10,
98 tstate->filename,tstate->reportlineno);
101 void parseerrprint(const char *fmt, ...) {
103 char errmsg[MAX_ERRMSG_LEN];
106 errwhere(cstate,errmsg,sizeof(errmsg)>>1);
107 vsnytprintfcat(errmsg,sizeof(errmsg),fmt,al);
108 senderrmsg(errmsg,eh.handling);
112 static int unexpected(int found, int wanted, const char *wantedstr) {
113 /* pass wanted==-1 if you know it's not what you wanted;
114 * otherwise this function will check it for you.
116 if (found == wanted) return 0;
117 if (found == tokv_error) return found;
118 parseerrprint("found %s, expected %s",printtoken(found),wantedstr);
122 static int stringoverflow(const char *where) {
123 parseerrprint("string buffer became far too large building %s",where);
128 * General assistance functions
131 static void freecharparray(char **array) {
135 for (pp=array; *pp; pp++) free(*pp);
139 static int dequote(char *inplace) {
140 char *p, *q, buf[4], *bep;
145 while (*p && *p != '"') {
146 if (*p != '\\') { *q++= *p++; continue; }
148 case 'n': *q++= '\n'; continue;
149 case 'r': *q++= '\r'; continue;
150 case 't': *q++= '\t'; continue;
152 assert(buf[0]= *++p); assert(buf[1]= *++p); buf[2]= 0;
153 v= strtoul(buf,&bep,16); assert(bep == buf+2);
154 assert(!(v & ~0xff)); *q++= v; p++; continue;
157 parseerrprint("unknown \\<letter> sequence \\%c in quoted string",*p);
159 } else if (isdigit(*p)) {
160 assert(buf[0]= *++p); assert(buf[1]= *++p); assert(buf[2]= *++p);
161 buf[3]= 0; v= strtoul(buf,&bep,8);
162 if (bep != buf+3 || (v & ~0xff)); {
163 parseerrprint("invalid \\<octal> sequence \\%s in quoted string",buf);
166 *q++= v; p++; continue;
167 } else if (ispunct(*p)) {
168 *q++= *p++; continue;
170 while (*p==' ' || *p=='\t') p++;
175 assert(*p); assert(!*++p);
177 return tokv_quotedstring;
180 const char *printtoken(int token) {
181 /* Returns pointer to static buffer, overwritten by next call. */
182 static const char keywordfmt[]= "keyword `%s'";
183 static const char operatorfmt[]= "operator `%s'";
185 static char buf[250];
191 if ((token & tokm_repres) == tokr_word) {
192 assert(strlen(yytext)+sizeof(keywordfmt)<sizeof(buf));
193 snyprintf(buf,sizeof(buf),keywordfmt,yytext);
195 } else if (token & tokt_number) {
196 snyprintf(buf,sizeof(buf),"number %d",lr_min); return buf;
197 } else if (token & tokt_fdrange && lr_max==-1) {
198 snyprintf(buf,sizeof(buf),"fdrange %d-",lr_min); return buf;
199 } else if (token & tokt_fdrange) {
200 snyprintf(buf,sizeof(buf),"fdrange %d-%d",lr_min,lr_max); return buf;
201 } else if ((token & tokm_repres) == tokr_punct) {
202 assert(strlen(yytext)+sizeof(operatorfmt)<sizeof(buf));
203 snyprintf(buf,sizeof(buf),operatorfmt,yytext);
205 } else if (token & tokt_string) {
207 case tokv_barestring: strcpy(buf,"unquoted string (bare word)"); break;
208 case tokv_quotedstring: strcpy(buf,"quoted string"); break;
212 l= strlen(buf); i= sizeof(buf)-l-2; p= yytext; q= buf+l;
214 if (i-- <= 0) { q--; strcpy(q-3,"..."); break; }
215 if (isspace(c)) c= ' ';
216 else if (!isprint(c) || iscntrl(c)) c= '?';
223 case tokv_lwsp: return "linear whitespace";
224 case tokv_newline: return "newline (or comment followed by newline)";
225 case tokv_eof: return "end of input file";
226 case tokv_error: return "syntax error token";
228 sprintf(buf,"token#0%o",token); return buf;
233 static const char *string2path(const char *in) {
234 /* Returned pointers become invalid on next call.
235 * May return 0, having printed an error message.
240 if (strncmp(in,"~/",2)) return in;
241 if (makeroom(&p,&pl,strlen(serviceuser_dir)+1+strlen(in+2)+1)) {
242 stringoverflow("pathname");
245 snyprintf(p,pl,"%s/%s",serviceuser_dir,in+2);
250 * Parser component functions for parsing parameters to directives
252 * Functions pa_... parse just one parameter,
253 * and return tokv_error or 0, having scanned exactly what they were expecting
254 * Functions paa_... parse complete parameter lists, including the leading lwsp,
255 * and return tokv_error or 0, having scanned up to and including the newline
257 * For any function which takes `const char **rv' the
258 * value returned in *rv is overwritten by repeated calls to
259 * any of those functions (whether the same or another).
262 static int pa_mnl(void) {
263 return unexpected(yylex(),tokv_newline,"newline");
265 static int pa_mwsp(void) {
266 return unexpected(yylex(),tokv_lwsp,"linear whitespace");
269 static int pa_string(const char **rv) {
275 r= pa_mwsp(); if (r) return r;
276 r= yylex(); if (r == tokv_error) return r;
277 if ((r & tokm_repres) == tokr_nonstring) return unexpected(r,-1,"string");
279 if (makeroom(&p,&pl,l)) return stringoverflow("string argument");
280 strcpy(p,yytext); *rv= p;
285 static int pa_numberdollar(int *value_r) {
286 /* Also parses the whitespace beforehand. */
289 r= pa_mwsp(); if (r) return r;
290 r= yylex(); if (r == tokv_error || r == tokv_dollar) return r;
291 if (unexpected(r,tokv_ordinal,"expected number or dollar")) return tokv_error;
296 static int paa_1string(const char **rv) {
299 r= pa_string(rv); if (r) return r;
303 static int paa_1path(const char **rv) {
307 r= paa_1string(&cp); if (r) return r;
308 *rv= string2path(cp); if (!*rv) return tokv_error;
312 static int paa_pathargs(const char **path_r, char ***newargs_r) {
313 /* Repeated calls do _not_ overwrite newargs_r; caller must free.
314 * Repeated calls _do_ overwrite string returned in path_r.
315 * Any call to this function invalidates previous returns in *rv.
321 r= pa_string(&string); if (r == tokv_error) return r;
322 *path_r= string2path(string); if (!*path_r) return tokv_error;
325 newargs= xmalloc(sizeof(char*)*(size+1));
328 r= yylex(); if (r == tokv_error) goto error;
329 if (r==tokv_newline) break;
330 if (unexpected(r,tokv_lwsp,"newline after or whitespace between arguments")) {
331 r= tokv_error; goto error;
333 r= yylex(); if (r==tokv_error) goto error;
334 if ((r & tokm_repres) == tokr_nonstring) {
335 r= unexpected(r,-1,"string for command argument");
339 if (used >= MAX_ARGSDEFVAR) {
340 parseerrprint("far too many arguments to service program");
341 r= tokv_error; goto error;
344 newargs= xrealloc(newargs,sizeof(char*)*(size+1));
346 newargs[used++]= xstrsave(yytext);
354 freecharparray(newargs);
358 static int paa_message(const char **message_r) {
359 /* Returned value is invalidated by repeated calls. */
360 static char *buildbuf;
361 static int buildbuflen;
365 r= pa_mwsp(); if (r) return r;
367 if (makeroom(&buildbuf,&buildbuflen,50)) return stringoverflow("start of message");
370 r= yylex(); if (r == tokv_error) return r;
372 parseerrprint("unexpected end of file in message text");
375 if (r == tokv_newline) break;
377 if (makeroom(&buildbuf,&buildbuflen,tl)) return stringoverflow("message");
378 strcat(buildbuf,yytext);
380 *message_r= buildbuf;
385 * Skipping routines (used by e.g. the `if' code).
388 static int skiptoeol(void) {
389 /* Returns 0 if OK, having just parsed the newline
390 * or tokv_error if an error occurs, leaving the position at the error
391 * (which may be EOF or a syntax error token).
395 do { token= yylex(); }
396 while (token != tokv_newline && !(token & tokt_exception));
397 if (token == tokv_newline) return 0;
398 if (token == tokv_error) return token;
399 assert(token == tokv_eof);
400 parseerrprint("unexpected end of file while looking for end of line");
404 static int skip(int allowce) {
405 /* Scans a piece of config without executing it. Returns
406 * tokv_error (leaving the parsing state at the error),
407 * or the tokt_controlend token with type allowce if one
408 * was found (in which case rest of line, including any whitespace
409 * after tokt_controlend, has not been parsed), or tokv_eof.
413 for (;;) { /* loop over lines */
414 cstate->reportlineno= cstate->lineno;
415 do { token= yylex(); } while (token == tokv_lwsp);
416 if (token & tokt_exception) {
418 } else if (token & tokt_controlend) {
419 if (allowce == lr_controlend) return token;
420 else return unexpected(token,-1,"control structure end of a different kind");
421 } else if (token & tokt_controlstart) {
423 while (r & tokt_controlstart) {
424 r= skiptoeol(); if (r) return r;
425 cstate->reportlineno= cstate->lineno;
426 r= skip(token); if (r & tokt_exception) return r;
428 } else if (!(token & tokt_directive) && !(token & tokt_condop)) {
429 parseerrprint("not a directive (or conditional operator) "
430 "while looking for control structure end");
433 r= skiptoeol(); if (r) return r;
438 * Routines for parsing and getting the values of parameters
441 static void parm_1string(char ***rvalues, const char *tocopy) {
443 a= xmalloc(sizeof(char*)*2);
444 a[0]= xstrsave(tocopy);
449 static char *parm_ulong(unsigned long ul) {
453 l= CHAR_BIT*sizeof(unsigned long)/3+4;
455 snyprintf(p,l,"%lu",ul);
459 static int parm_usernameuid(char ***rvalues, const char *name, uid_t id) {
462 a= xmalloc(sizeof(char*)*3);
463 a[0]= xstrsave(name);
464 a[1]= parm_ulong(id);
470 static int parm_groups(char ***rvalues, int size,
471 gid_t *gids, const char *const *groups) {
475 if (size >= 2 && gids[0] == gids[1]) { size--; gids++; groups++; }
476 a= xmalloc(sizeof(char*)*(size+1)*2);
477 for (i=0; i<size; i++) {
478 a[i]= xstrsave(groups[i]);
479 a[size+i]= parm_ulong(gids[i]);
486 static int pf_service(int ptoken, char ***rvalues) {
487 parm_1string(rvalues,service); return 0;
490 static int pf_callinguser(int ptoken, char ***rvalues) {
491 return parm_usernameuid(rvalues,logname,request_mbuf.callinguid);
494 static int pf_serviceuser(int ptoken, char ***rvalues) {
495 return parm_usernameuid(rvalues,serviceuser,serviceuser_uid);
498 static int pf_callinggroup(int ptoken, char ***rvalues) {
499 return parm_groups(rvalues,request_mbuf.ngids,calling_gids,calling_groups);
502 static int pf_servicegroup(int ptoken, char ***rvalues) {
503 return parm_groups(rvalues,service_ngids,service_gids,service_groups);
506 static int pf_callingusershell(int ptoken, char ***rvalues) {
507 parm_1string(rvalues,callinguser_shell); return 0;
510 static int pf_serviceusershell(int ptoken, char ***rvalues) {
511 parm_1string(rvalues,serviceuser_shell); return 0;
514 static int pa_parameter(char ***rvalues, char **rname) {
515 /* Scans a single parameter token and calls the appropriate parameter
516 * function, returning tokv_error (leaving the parser state wherever),
517 * or 0 on success (having just parsed the parameter name).
518 * If rname is non-null then the name of the parameter (malloc'd) will
521 * Caller must free *rvalues using freecharparray.
526 token= yylex(); if (token == tokv_error) return token;
527 name= xstrsave(yytext);
528 if ((token & tokm_repres) != tokr_nonstring &&
529 !memcmp(yytext,"u-",2) && strlen(yytext)>=3) {
531 i<request_mbuf.nvars && strcmp(yytext+2,defvararray[i].key);
533 if (i>=request_mbuf.nvars) {
534 *rvalues= xmalloc(sizeof(char*));
537 parm_1string(rvalues,defvararray[i].value);
539 } else if (token & tokt_parameter) {
540 r= (lr_parameter)(token,rvalues);
541 if (r) { free(name); return r; }
544 return unexpected(token,-1,"parameter name");
546 debug_dumpparameter(name,*rvalues);
547 if (rname) *rname= name;
553 * Routines for parsing conditions, including
554 * parameter-based conditional functions (parmcondition's).
557 int pcf_glob(int ctoken, char *const *pv, int *rtrue) {
558 int token, actrue, r;
562 r= pa_mwsp(); if (r) return r;
565 if ((token & tokm_repres) == tokr_nonstring)
566 return unexpected(token,-1,"glob pattern");
567 for (pp= pv; !actrue && *pp; pp++) if (!fnmatch(yytext,*pp,0)) actrue= 1;
569 if (token == tokv_newline) break;
570 if (unexpected(token,tokv_lwsp,"newline after or whitespace between glob patterns"))
577 int pcf_range(int ctoken, char *const *pv, int *rtrue) {
578 int mintoken, min, maxtoken, max, r;
583 r= pa_mwsp(); if (r) return r;
584 mintoken= pa_numberdollar(&min); if (mintoken == tokv_error) return mintoken;
585 r= pa_mwsp(); if (r) return r;
586 maxtoken= pa_numberdollar(&max); if (maxtoken == tokv_error) return maxtoken;
587 r= pa_mnl(); if (r) return r;
588 for (pp= pv; *pp; pp++) {
589 v= strtoul(*pp,&ep,10); if (*ep) continue;
590 if (mintoken != tokv_dollar && v < min) continue;
591 if (maxtoken != tokv_dollar && v > max) continue;
597 int pcf_grep(int ctoken, char *const *pv, int *rtrue) {
602 int r, maxlen, l, c, actrue, posstrue;
604 r= paa_1path(&cp); if (r) return r;
607 parseerrprint("unable to open file `%s' for grep: %s",cp,strerror(errno));
611 for (pp= pv; *pp; pp++) { l= strlen(*pp); if (l > maxlen) maxlen= l; }
612 buf= xmalloc(maxlen+2); actrue= 0; c= 0;
613 while (!actrue && c!=EOF) {
614 c= getc(file); if (c==EOF) break;
615 if (isspace(c)) continue;
617 while (l>0 && c!='\n' && c!=EOF) { *p++= c; l--; c= getc(file); }
618 if (c=='\n' || c==EOF || isspace(c)) {
619 while (p>buf && isspace(p[-1])) --p;
621 for (pp= pv; !posstrue && *pp; pp++)
622 if (!strcmp(*pp,buf)) posstrue= 1;
626 if (c!='\n' && c!=EOF) {
629 if (c==EOF || c=='\n') break;
630 if (!isspace(c)) posstrue= 0;
633 if (posstrue) actrue= 1;
636 parseerrprint("error while reading `%s' for grep: %s",cp,strerror(errno));
637 fclose(file); free(buf); return tokv_error;
639 assert(actrue || feof(file));
640 fclose(file); free(buf); *rtrue= actrue;
644 static int pa_condition(int *rtrue) {
645 /* Scans up to and including the newline following the condition;
646 * may scan more than one line if the condition is a multi-line
647 * one. Returns 0 (setting *rtrue, and parsing up to and including
648 * the last newline) or tokv_error.
649 * Expects to scan the whitespace before its condition.
651 int r, token, andor, ctrue, actrue;
654 r= pa_mwsp(); if (r) return r;
656 if (token == tokv_error) {
658 } else if (token == tokv_not) {
659 r= pa_condition(&ctrue); if (r) return r;
660 *rtrue= !ctrue; return 0;
661 } else if (token == tokv_openparen) {
662 andor= 0; actrue= -1;
664 cstate->reportlineno= cstate->lineno;
665 r= pa_condition(&ctrue); if (r) return r;
667 case 0: assert(actrue==-1); actrue= ctrue; break;
668 case tokv_and: assert(actrue>=0); actrue= actrue && ctrue; break;
669 case tokv_or: assert(actrue>=0); actrue= actrue || ctrue; break;
672 do { token= yylex(); } while (token == tokv_lwsp);
673 if (token == tokv_error) return token;
674 if (token == tokv_closeparen) break;
676 r= unexpected(token,andor,"same conjunction as before"); if (r) return r;
678 if (token != tokv_and && token != tokv_or)
679 return unexpected(token,-1,"first conjunction inside connective");
683 r= pa_mnl(); if (r) return r;
684 *rtrue= actrue; return 0;
685 } else if (token & tokt_parmcondition) {
686 r= pa_mwsp(); if (r) return r;
687 r= pa_parameter(&parmvalues,0); if (r) return r;
688 r= (lr_parmcond)(token,parmvalues,rtrue); freecharparray(parmvalues);
691 return unexpected(token,-1,"condition");
696 * Directive functions and associated `common code' functions
699 /* Directives specifying the service program (execute-... and reset) */
701 static void execreset(void) {
704 free(execpath); execpath= 0;
705 freecharparray(execargs); execargs= 0;
708 int df_reject(int dtoken) {
711 r= pa_mnl(); if (r) return r;
713 execute= tokv_word_reject;
717 int df_executefrompath(int dtoken) {
720 r= pa_mnl(); if (r) return r;
722 execute= tokv_word_executefrompath;
726 int df_execute(int dtoken) {
731 r= paa_pathargs(&rv,&newargs); if (r) return r;
733 execute= tokv_word_execute;
735 execpath= xstrsave(rv);
739 int df_executefromdirectory(int dtoken) {
740 const char *p, *q, *rv;
745 r= paa_pathargs(&rv,&newargs); if (r) return r;
746 p= strrchr(service,'/'); if (p) p++; else p= service;
747 if (!*p || !isalnum(*p)) {
748 parseerrprint("execute-from-directory requires initial char of service "
749 "portion to be alphanumeric (service portion was `%s')",
751 freecharparray(newargs);
754 for (q=p+1; *q; q++) {
755 if (!isalnum(*q) && *q != '-') {
756 parseerrprint("execute-from-directory requires service portion to "
757 "contain only alphanumerics and hyphens (was `%s')",
759 freecharparray(newargs);
763 l= strlen(rv)+1+strlen(p)+1;
765 snyprintf(fn,l,"%s/%s",rv,p);
766 if (stat(fn,&stab)) {
767 if (errno == ENOENT) { free(fn); freecharparray(newargs); return 0; }
768 parseerrprint("failed to stat `%s' for execute-from-directory: %s",
770 free(fn); freecharparray(newargs); return tokv_error;
772 if (!S_ISREG(stab.st_mode)) {
773 parseerrprint("file `%s' in execute-from-directory is not an ordinary file"
774 " or link to one (mode=0%lo)",fn,(unsigned long)stab.st_mode);
775 free(fn); freecharparray(newargs); return tokv_error;
778 execute= tokv_word_executefromdirectory;
784 /* Parsing builtin service requests (execute-builtin) */
786 static int bispa_none(char ***rnewargs) {
790 static int bispa_parameter(char ***rnewargs) {
792 char **parmvalues, *name, **newargs;
794 r= pa_mwsp(); if (r) return r;
795 r= pa_parameter(&parmvalues,&name); if (r) return r;
796 for (i=0; parmvalues[i]; i++);
797 newargs= xmalloc(sizeof(char*)*(i+2));
799 memcpy(newargs+1,parmvalues,sizeof(char*)*(i+1));
801 r= pa_mnl(); if (r) { free(newargs); return r; }
806 int df_executebuiltin(int dtoken) {
808 builtinserviceexec_fnt *bisexec;
809 char *newpath, **newargs;
811 r= pa_mwsp(); if (r) return r;
812 r= yylex(); if (r == tokv_error) return r;
813 if (!(r & tokt_builtinservice)) return unexpected(r,-1,"builtin service name");
815 newpath= xstrsave(yytext);
817 r= lr_bispa(&newargs); if (r) { free(newpath); return r; }
820 execute= tokv_word_executebuiltin;
821 execbuiltin= bisexec;
827 /* Directives for changing other execution parameters */
829 int dfg_setflag(int dtoken) {
832 r= pa_mnl(); if (r) return r;
833 *lr_flag= lr_flagval;
837 int df_reset(int dtoken) {
840 r= pa_mnl(); if (r) return r;
841 r= parse_string(RESET_CONFIGURATION,"<builtin reset configuration>",1);
845 int dfg_fdwant(int dtoken) {
846 int fdmin, fdmax, r, needreadwrite, havereadwrite, fd;
848 needreadwrite= lr_fdwant_readwrite;
849 r= pa_mwsp(); if (r) return r;
850 r= yylex(); if (r == tokv_error) return r;
851 if (!(r & tokt_fdrange)) return unexpected(r,-1,"file descriptor range");
852 fdmin= lr_min; fdmax= lr_max;
853 if (fdmin<0 || fdmin>MAX_ALLOW_FD ||
854 (fdmax != -1 && fdmax<0) || fdmax>MAX_ALLOW_FD) {
855 parseerrprint("file descriptor in range is negative or far too large");
858 r= yylex(); if (r == tokv_error) return r;
859 if (r == tokv_newline) {
860 if (needreadwrite > 0) {
861 parseerrprint("read or write is required");
865 } else if (r == tokv_lwsp) {
866 if (needreadwrite < 0) {
867 parseerrprint("read or write not allowed"); return tokv_error;
869 r= yylex(); if (r == tokv_error) return r;
870 if (!(r & tokt_readwrite))
871 return unexpected(r,-1,"read or write (or perhaps newline)");
873 r= pa_mnl(); if (r) return r;
875 return unexpected(r,-1,"whitespace before read or write or newline");
877 ensurefdarray(fdmin);
879 if (!(dtoken == tokv_word_rejectfd || dtoken == tokv_word_ignorefd))
880 parseerrprint("unspecified maximum only allowed with reject-fd and ignore-fd");
881 fdmax= fdarrayused-1;
882 restfdwantstate= dtoken;
883 restfdwantrw= havereadwrite;
885 ensurefdarray(fdmax);
886 for (fd=fdmin; fd<=fdmax; fd++) {
887 fdarray[fd].wantstate= dtoken;
888 fdarray[fd].wantrw= havereadwrite;
893 /* Directives for changing error handling */
895 int df_errorstostderr(int dtoken) {
898 r= pa_mnl(); if (r) return r;
899 closeerrorfile(); eh.handling= dtoken;
903 int df_errorstosyslog(int dtoken) {
904 int token, level, facility;
906 facility= DEFUSERLOGFACILITY;
907 level= DEFUSERLOGLEVEL;
909 if (token == tokv_lwsp) {
910 token= yylex(); if (token == tokv_error) return token;
911 if (!(token & tokt_logfacility))
912 return unexpected(token,-1,"syslog facility (or end of line)");
913 facility= lr_logfacility;
916 if (token == tokv_lwsp) {
917 token= yylex(); if (token == tokv_error) return token;
918 if (!(token & tokt_loglevel))
919 return unexpected(token,-1,"syslog level (or end of line) after facility");
923 if (unexpected(token,tokv_newline,"end of line after errors-to-syslog"))
925 closeerrorfile(); eh.handling= tokv_word_errorstosyslog;
926 eh.logfacility= facility; eh.loglevel= level;
930 int df_errorstofile(int dtoken) {
935 r= paa_1path(&cp); if (r) return r;
938 parseerrprint("unable to open error log file `%s': %s",cp,strerror(errno));
941 if (setvbuf(file,0,_IOLBF,MAX_ERRMSG_LEN)) {
942 parseerrprint("unable to set line buffering on errors file: %s",strerror(errno));
943 fclose(file); return tokv_error;
945 closeerrorfile(); eh.handling= tokv_word_errorstofile;
946 eh.file= file; eh.filename= xstrsave(cp);
950 /* Directives for including other files or configuration data */
952 int dfi_includeuserrcfile(int dtoken) {
955 r= pa_mnl(); if (r) return r;
957 return parse_file(userrcfile,0);
960 int dfi_includeclientconfig(int dtoken) {
963 r= pa_mnl(); if (r) return r;
964 assert(overridedata);
965 return parse_string(overridedata,"<configuration override data>",0);
968 int df_include(int dtoken) {
972 r= paa_1path(&cp); if (r) return r;
973 r= parse_file(cp,&found); if (r) return r;
974 if (found || dtoken == tokv_word_includeifexist) return 0;
975 parseerrprint(dtoken == tokv_word_includesysconfig ?
976 "system configuration file `%s' does not exist" :
977 "included file `%s' does not exist",
982 int df_includedirectory(int dtoken) {
983 static char *buildbuf=0;
984 static int buildbuflen=0;
986 int r, cpl, tel, c, found;
989 const char *p, *cpget;
992 r= paa_1path(&cpget); if (r) return r;
995 parseerrprint("unable to open directory `%s': %s",cpget,strerror(errno));
1000 while ((de= readdir(d))) {
1001 tel= strlen(de->d_name);
1004 if (!*p || !isalnum(*p)) continue;
1005 while ((c= *++p)) if (!(isalnum(c) || c=='-')) break;
1007 if (makeroom(&buildbuf,&buildbuflen,cpl+1+tel+1)) {
1008 stringoverflow("pathname in directory");
1009 r= tokv_error; goto x_err;
1011 snyprintf(buildbuf,buildbuflen,"%s/%s",cp,de->d_name);
1012 r= parse_file(buildbuf,&found); if (r) goto x_err;
1014 parseerrprint("unable to open file `%s' in included directory `%s': %s",
1015 de->d_name,cp,strerror(errno));
1016 r= tokv_error; goto x_err;
1020 parseerrprint("error closing directory `%s': %s",cp,strerror(errno));
1033 int df_includelookup(int dtoken) {
1034 static char *buildbuf=0;
1037 char **parmvalues, **pp, *p, *q, *cp;
1040 int r, done, thisdone, cpl, c;
1042 r= pa_mwsp(); if (r) return r;
1043 r= pa_parameter(&parmvalues,0); if (r) return r;
1044 r= paa_1path(&cpget); if (r) { freecharparray(parmvalues); return r; }
1045 if (stat(cpget,&stab)) {
1046 parseerrprint("unable to access directory `%s': %s",cpget,strerror(errno));
1047 freecharparray(parmvalues); return tokv_error;
1049 if (!S_ISDIR(stab.st_mode)) {
1050 parseerrprint("object `%s' is not a directory or link to one",cpget);
1051 freecharparray(parmvalues); return tokv_error;
1054 cp= xstrsave(cpget);
1056 if (!parmvalues[0]) {
1057 if (makeroom(&buildbuf,&buildbuflen,cpl+1+sizeof(NONEINCLUDELOOKUP))) {
1058 stringoverflow("pathname in directory for lookup of undefined parameter");
1059 r= tokv_error; goto x_err;
1061 snyprintf(buildbuf,buildbuflen,"%s/" NONEINCLUDELOOKUP,cp);
1062 r= parse_file(buildbuf,&thisdone); if (r) goto x_err;
1063 if (thisdone) done= 1;
1066 *pp && (!done || dtoken == tokv_word_includelookupall);
1068 if (makeroom(&buildbuf,&buildbuflen,
1069 cpl+1+strlen(*pp)*2+3+sizeof(EMPTYINCLUDELOOKUP)+1)) {
1070 stringoverflow("pathname in directory for lookup");
1071 r= tokv_error; goto x_err;
1073 strcpy(buildbuf,cp);
1074 p= *pp; q= buildbuf+cpl;
1077 strcpy(q,EMPTYINCLUDELOOKUP);
1079 if (*p=='.') *q++= ':';
1081 if (c=='/') { *q++= ':'; c='-'; }
1082 else if (c==':') { *q++= ':'; }
1087 r= parse_file(buildbuf,&thisdone);
1089 if (thisdone) done= 1;
1093 if (makeroom(&buildbuf,&buildbuflen,
1094 cpl+1+sizeof(DEFAULTINCLUDELOOKUP))) {
1095 stringoverflow("pathname in directory for lookup of default");
1096 r= tokv_error; goto x_err;
1098 snyprintf(buildbuf,buildbuflen,"%s/" DEFAULTINCLUDELOOKUP,cp);
1099 r= parse_file(buildbuf,0); if (r) goto x_err;
1104 freecharparray(parmvalues);
1109 /* Control constructs */
1111 int df_catchquit(int dtoken) {
1114 r= pa_mnl(); if (r) return r;
1115 r= parser(tokv_word_catchquit);
1116 if (r == tokv_quit || r == tokv_error) {
1117 if (r == tokv_error) {
1118 r= parse_string(RESET_CONFIGURATION,
1119 "<builtin reset configuration (caught error)>",1);
1122 r= skip(tokv_word_catchquit);
1124 if (r & tokt_controlend) {
1125 assert(r == tokv_word_hctac);
1131 int df_if(int dtoken) {
1136 r= pa_condition(&true); if (r) return r;
1137 if (!done && true) { r= parser(tokv_word_if); done= 1; }
1138 else { r= skip(tokv_word_if); }
1139 if (!(r & tokt_controlend)) return r;
1140 } while (r == tokv_word_elif);
1141 if (r == tokv_word_else) {
1142 r= pa_mnl(); if (r) return r;
1143 cstate->reportlineno= cstate->lineno;
1144 if (done) r= skip(tokv_word_if);
1145 else r= parser(tokv_word_if);
1146 if (!(r & tokt_controlend)) return r;
1148 if (unexpected(r,tokv_word_fi,"`fi' to end `if'")) return tokv_error;
1152 int df_errorspush(int dt) {
1153 struct error_handling save;
1156 r= pa_mnl(); if (r) return r;
1161 r= parser(tokv_word_errorspush);
1166 if (r & tokt_controlend) {
1167 assert(r == tokv_word_srorre);
1173 /* Miscelleanous directives */
1175 int df_cd(int dtoken) {
1179 r= paa_1path(&cp); if (r) return r;
1180 if (!chdir(cp)) return 0;
1181 parseerrprint("unable to change directory to `%s': %s",cp,strerror(errno));
1185 int df_userrcfile(int dtoken) {
1189 r= paa_1path(&cp); if (r) return r;
1190 free(userrcfile); userrcfile= xstrsave(cp);
1194 int df_message(int dtoken) {
1198 r= paa_message(&mp); if (r) return r;
1199 parseerrprint("`message' directive: %s",mp);
1203 int df_error(int dtoken) {
1207 r= paa_message(&mp); if (r) return r;
1208 parseerrprint("`error' directive: %s",mp);
1212 int df_eof(int dtoken) {
1215 r= pa_mnl(); if (r) return r;
1219 int df_quit(int dtoken) {
1222 r= pa_mnl(); if (r) return r;
1227 * Main parser routines
1230 static void parser_push(struct parser_state *usestate,
1231 const char *newfile,
1232 const struct stat *newfilestab,
1233 YY_BUFFER_STATE ybuf,
1235 usestate->lineno= 1;
1236 usestate->reportlineno= 1;
1237 usestate->filename= newfile;
1238 usestate->filestab= *newfilestab;
1239 usestate->notedreferer= 0;
1240 usestate->isinternal= isinternal;
1241 usestate->ybuf= ybuf;
1242 usestate->upstate= cstate;
1245 yy_switch_to_buffer(ybuf);
1248 static void parser_pop(void) {
1249 struct parser_state *oldstate;
1252 cstate= cstate->upstate;
1253 if (cstate) yy_switch_to_buffer(cstate->ybuf);
1254 yy_delete_buffer(oldstate->ybuf);
1257 int parse_string(const char *string, const char *descrip, int isinternal) {
1258 /* Returns the same things as parser, except that tokv_eof is turned
1259 * into 0. *string must be statically allocated or copied, so that
1260 * it is not overwritten while the parsing takes place (unlike with
1263 static const struct stat blankstab;
1265 struct parser_state usestate;
1266 YY_BUFFER_STATE ybuf;
1269 ybuf= yy_scan_string(string);
1270 if (!ybuf) syscallerror("unable to create flex buffer for internal string");
1271 parser_push(&usestate,descrip,&blankstab,ybuf,isinternal);
1276 if (r == tokv_eof) r= 0;
1280 static int parse_file(const char *string, int *didexist) {
1281 /* Returns the same things as parser, except that tokv_eof is turned
1282 * into 0. If *didexist is 0 then errno will have been set.
1283 * *string will be copied by parse_file so it may be be overwritten
1284 * during the parsing (so, for example, yytext need not be copied).
1286 static int fileparselevel= 0;
1288 struct parser_state usestate, *checkrecurse;
1289 YY_BUFFER_STATE ybuf;
1293 struct stat newstab;
1295 if (fileparselevel >= MAX_INCLUDE_NEST) {
1296 parseerrprint("too many nested levels of included files");
1299 file= fopen(string,"r");
1301 if (errno == ENOENT) {
1302 if (didexist) *didexist= 0;
1305 parseerrprint("unable to open config file `%s': %s",string,strerror(errno));
1308 r= fstat(fileno(file),&newstab); if (r) syscallerror("unable to fstat new file");
1309 for (checkrecurse= cstate; checkrecurse; checkrecurse= checkrecurse->upstate) {
1310 if (!checkrecurse->filestab.st_mode) continue;
1311 if (newstab.st_dev==checkrecurse->filestab.st_dev &&
1312 newstab.st_ino==checkrecurse->filestab.st_ino) {
1313 parseerrprint("recursion detected - config file `%s' calls itself",string);
1319 if (didexist) *didexist= 1;
1321 ybuf= yy_create_buffer(file,YY_BUF_SIZE);
1322 if (!ybuf) syscallerror("unable to create flex buffer for file");
1323 filename= xstrsave(string);
1324 parser_push(&usestate,filename,&newstab,ybuf,0);
1329 parseerrprint("error reading configuration file `%s'",string);
1337 if (r == tokv_eof) r= 0;
1341 static int parser(int allowce) {
1343 * an exception (error, eof or quit)
1344 * then rest of `file' is uninteresting
1346 * token if allowce was !0 and equal to token's controlend
1347 * then rest of `file' (including rest of line with the
1348 * controlend - even the whitespace) not scanned yet
1352 for (;;) { /* loop over lines */
1353 cstate->reportlineno= cstate->lineno;
1354 do { token= yylex(); } while (token == tokv_lwsp);
1355 if (token & tokt_exception) {
1357 } else if (token & tokt_controlend) {
1358 if (lr_controlend == allowce) return token;
1359 else return unexpected(token,-1,"directive (not this kind of"
1360 " control structure end)");
1361 } else if (token & tokt_directive) {
1362 if ((token & tokt_internal) && !cstate->isinternal)
1363 return unexpected(token,-1,"published directive, not internal-use-only one");
1364 r= (lr_dir)(token); if (r) { assert(r & tokt_exception); return r; }
1365 } else if (token == tokv_newline) {
1366 /* ignore blank lines (and comment-only lines) */
1368 return unexpected(token,-1,"directive");