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-1999 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;
153 if (!((buf[0]= *p++) && (buf[1]= *p++))) {
154 parseerrprint("quoted string ends inside \\x<hex> sequence");
158 v= strtoul(buf,&bep,16);
160 parseerrprint("invalid \\<hex> sequence \\x%s in quoted string",buf);
163 assert(!(v & ~0xff));
168 parseerrprint("unknown \\<letter> sequence \\%c in quoted string",*p);
170 } else if (isdigit(*p)) {
171 if (!((buf[0]= *p++) && (buf[1]= *p++) && (buf[2]= *p++))) abort();
172 buf[3]= 0; v= strtoul(buf,&bep,8);
173 if (bep != buf+3 || (v & ~0xff)) {
174 parseerrprint("invalid \\<octal> sequence \\%s in quoted string",buf);
178 } else if (ispunct(*p)) {
179 *q++= *p++; continue;
181 while (*p==' ' || *p=='\t') p++;
182 v= *p++; assert(v=='\n');
186 assert(*p); assert(!*++p);
188 return tokv_quotedstring;
191 const char *printtoken(int token) {
192 /* Returns pointer to static buffer, overwritten by next call. */
193 static const char keywordfmt[]= "keyword `%s'";
194 static const char operatorfmt[]= "operator `%s'";
196 static char buf[250];
202 if ((token & tokm_repres) == tokr_word) {
203 assert(strlen(yytext)+sizeof(keywordfmt)<sizeof(buf));
204 snyprintf(buf,sizeof(buf),keywordfmt,yytext);
206 } else if (token & tokt_number) {
207 snyprintf(buf,sizeof(buf),"number %d",lr_min); return buf;
208 } else if (token & tokt_fdrange && lr_max==-1) {
209 snyprintf(buf,sizeof(buf),"fdrange %d-",lr_min); return buf;
210 } else if (token & tokt_fdrange) {
211 snyprintf(buf,sizeof(buf),"fdrange %d-%d",lr_min,lr_max); return buf;
212 } else if ((token & tokm_repres) == tokr_punct) {
213 assert(strlen(yytext)+sizeof(operatorfmt)<sizeof(buf));
214 snyprintf(buf,sizeof(buf),operatorfmt,yytext);
216 } else if (token & tokt_string) {
218 case tokv_barestring: strcpy(buf,"unquoted string (bare word)"); break;
219 case tokv_quotedstring: strcpy(buf,"quoted string"); break;
223 l= strlen(buf); i= sizeof(buf)-l-2; p= yytext; q= buf+l;
225 if (i-- <= 0) { q--; strcpy(q-3,"..."); break; }
226 if (isspace(c)) c= ' ';
227 else if (!isprint(c) || iscntrl(c)) c= '?';
234 case tokv_lwsp: return "linear whitespace";
235 case tokv_newline: return "newline (or comment followed by newline)";
236 case tokv_eof: return "end of input file";
237 case tokv_error: return "syntax error token";
239 sprintf(buf,"token#0%o",token); return buf;
244 static const char *string2path(const char *in) {
245 /* Returned pointers become invalid on next call.
246 * May return 0, having printed an error message.
251 if (strncmp(in,"~/",2)) return in;
252 if (makeroom(&p,&pl,strlen(serviceuser_dir)+1+strlen(in+2)+1)) {
253 stringoverflow("pathname");
256 snyprintf(p,pl,"%s/%s",serviceuser_dir,in+2);
261 * Parser component functions for parsing parameters to directives
263 * Functions pa_... parse just one parameter,
264 * and return tokv_error or 0, having scanned exactly what they were expecting
265 * Functions paa_... parse complete parameter lists, including the leading lwsp,
266 * and return tokv_error or 0, having scanned up to and including the newline
268 * For any function which takes `const char **rv' the
269 * value returned in *rv is overwritten by repeated calls to
270 * any of those functions (whether the same or another).
273 static int pa_mnl(void) {
274 return unexpected(yylex(),tokv_newline,"newline");
276 static int pa_mwsp(void) {
277 return unexpected(yylex(),tokv_lwsp,"linear whitespace");
280 static int pa_string(const char **rv) {
286 r= pa_mwsp(); if (r) return r;
287 r= yylex(); if (r == tokv_error) return r;
288 if ((r & tokm_repres) == tokr_nonstring) return unexpected(r,-1,"string");
290 if (makeroom(&p,&pl,l)) return stringoverflow("string argument");
291 strcpy(p,yytext); *rv= p;
296 static int pa_numberdollar(int *value_r) {
297 /* Also parses the whitespace beforehand. */
300 r= pa_mwsp(); if (r) return r;
301 r= yylex(); if (r == tokv_error || r == tokv_dollar) return r;
302 if (unexpected(r,tokv_ordinal,"expected number or dollar")) return tokv_error;
307 static int paa_1string(const char **rv) {
310 r= pa_string(rv); if (r) return r;
314 static int paa_1path(const char **rv) {
318 r= paa_1string(&cp); if (r) return r;
319 *rv= string2path(cp); if (!*rv) return tokv_error;
323 static int paa_pathargs(const char **path_r, char ***newargs_r) {
324 /* Repeated calls do _not_ overwrite newargs_r; caller must free.
325 * Repeated calls _do_ overwrite string returned in path_r.
326 * Any call to this function invalidates previous returns in *rv.
332 r= pa_string(&string); if (r == tokv_error) return r;
333 *path_r= string2path(string); if (!*path_r) return tokv_error;
336 newargs= xmalloc(sizeof(char*)*(size+1));
339 r= yylex(); if (r == tokv_error) goto error;
340 if (r==tokv_newline) break;
341 if (unexpected(r,tokv_lwsp,"newline after or whitespace between arguments")) {
342 r= tokv_error; goto error;
344 r= yylex(); if (r==tokv_error) goto error;
345 if ((r & tokm_repres) == tokr_nonstring) {
346 r= unexpected(r,-1,"string for command argument");
350 if (used >= MAX_ARGSDEFVAR) {
351 parseerrprint("far too many arguments to service program");
352 r= tokv_error; goto error;
355 newargs= xrealloc(newargs,sizeof(char*)*(size+1));
357 newargs[used++]= xstrsave(yytext);
365 freecharparray(newargs);
369 static int paa_message(const char **message_r) {
370 /* Returned value is invalidated by repeated calls. */
371 static char *buildbuf;
372 static int buildbuflen;
377 r= pa_mwsp(); if (r) return r;
379 if (makeroom(&buildbuf,&buildbuflen,50)) return stringoverflow("start of message");
382 r= yylex(); if (r == tokv_error) return r;
384 parseerrprint("unexpected end of file in message text");
387 if (r == tokv_newline) break;
388 usetext= r == tokv_lwsp ? " " : yytext;
389 tl+= strlen(usetext);
390 if (makeroom(&buildbuf,&buildbuflen,tl)) return stringoverflow("message");
391 strcat(buildbuf,usetext);
393 *message_r= buildbuf;
398 * Skipping routines (used by e.g. the `if' code).
401 static int skiptoeol(void) {
402 /* Returns 0 if OK, having just parsed the newline
403 * or tokv_error if an error occurs, leaving the position at the error
404 * (which may be EOF or a syntax error token).
408 do { token= yylex(); }
409 while (token != tokv_newline && !(token & tokt_exception));
410 if (token == tokv_newline) return 0;
411 if (token == tokv_error) return token;
412 assert(token == tokv_eof);
413 parseerrprint("unexpected end of file while looking for end of line");
417 static int skip(int allowce) {
418 /* Scans a piece of config without executing it. Returns
419 * tokv_error (leaving the parsing state at the error),
420 * or the tokt_controlend token with type allowce if one
421 * was found (in which case rest of line, including any whitespace
422 * after tokt_controlend, has not been parsed), or tokv_eof.
426 for (;;) { /* loop over lines */
427 cstate->reportlineno= cstate->lineno;
428 do { token= yylex(); } while (token == tokv_lwsp);
429 if (token & tokt_exception) {
431 } else if (token & tokt_controlend) {
432 if (allowce == lr_controlend) return token;
433 else return unexpected(token,-1,"control structure end of a different kind");
434 } else if (token & tokt_controlstart) {
436 while (r & tokt_controlstart) {
437 r= skiptoeol(); if (r) return r;
438 cstate->reportlineno= cstate->lineno;
439 r= skip(token); if (r & tokt_exception) return r;
441 } else if (!(token & tokt_directive) && !(token & tokt_condop)) {
442 parseerrprint("not a directive (or conditional operator) "
443 "while looking for control structure end");
446 r= skiptoeol(); if (r) return r;
451 * Routines for parsing and getting the values of parameters
454 static void parm_1string(char ***rvalues, const char *tocopy) {
456 a= xmalloc(sizeof(char*)*2);
457 a[0]= xstrsave(tocopy);
462 static char *parm_ulong(unsigned long ul) {
466 l= CHAR_BIT*sizeof(unsigned long)/3+4;
468 snyprintf(p,l,"%lu",ul);
472 static int parm_usernameuid(char ***rvalues, const char *name, uid_t id) {
475 a= xmalloc(sizeof(char*)*3);
476 a[0]= xstrsave(name);
477 a[1]= parm_ulong(id);
483 static int parm_groups(char ***rvalues, int size,
484 gid_t *gids, const char *const *groups) {
488 if (size >= 2 && gids[0] == gids[1]) { size--; gids++; groups++; }
489 a= xmalloc(sizeof(char*)*(size+1)*2);
490 for (i=0; i<size; i++) {
491 a[i]= xstrsave(groups[i]);
492 a[size+i]= parm_ulong(gids[i]);
499 static int pf_service(int ptoken, char ***rvalues) {
500 parm_1string(rvalues,service); return 0;
503 static int pf_callinguser(int ptoken, char ***rvalues) {
504 return parm_usernameuid(rvalues,loginname,request_mbuf.callinguid);
507 static int pf_serviceuser(int ptoken, char ***rvalues) {
508 return parm_usernameuid(rvalues,serviceuser,serviceuser_uid);
511 static int pf_callinggroup(int ptoken, char ***rvalues) {
512 return parm_groups(rvalues,request_mbuf.ngids,calling_gids,calling_groups);
515 static int pf_servicegroup(int ptoken, char ***rvalues) {
516 return parm_groups(rvalues,service_ngids,service_gids,service_groups);
519 static int pf_callingusershell(int ptoken, char ***rvalues) {
520 parm_1string(rvalues,callinguser_shell); return 0;
523 static int pf_serviceusershell(int ptoken, char ***rvalues) {
524 parm_1string(rvalues,serviceuser_shell); return 0;
527 static int pa_parameter(char ***rvalues, char **rname) {
528 /* Scans a single parameter token and calls the appropriate parameter
529 * function, returning tokv_error (leaving the parser state wherever),
530 * or 0 on success (having just parsed the parameter name).
531 * If rname is non-null then the name of the parameter (malloc'd) will
534 * Caller must free *rvalues using freecharparray.
539 token= yylex(); if (token == tokv_error) return token;
540 name= xstrsave(yytext);
541 if ((token & tokm_repres) != tokr_nonstring &&
542 !memcmp(yytext,"u-",2) && strlen(yytext)>=3) {
544 i<request_mbuf.nvars && strcmp(yytext+2,defvararray[i].key);
546 if (i>=request_mbuf.nvars) {
547 *rvalues= xmalloc(sizeof(char*));
550 parm_1string(rvalues,defvararray[i].value);
552 } else if (token & tokt_parameter) {
553 r= (lr_parameter)(token,rvalues);
554 if (r) { free(name); return r; }
557 return unexpected(token,-1,"parameter name");
559 debug_dumpparameter(name,*rvalues);
560 if (rname) *rname= name;
566 * Routines for parsing conditions, including
567 * parameter-based conditional functions (parmcondition's).
570 int pcf_glob(int ctoken, char *const *pv, int *rtrue) {
571 int token, actrue, r;
575 r= pa_mwsp(); if (r) return r;
578 if ((token & tokm_repres) == tokr_nonstring)
579 return unexpected(token,-1,"glob pattern");
580 for (pp= pv; !actrue && *pp; pp++) if (!fnmatch(yytext,*pp,0)) actrue= 1;
582 if (token == tokv_newline) break;
583 if (unexpected(token,tokv_lwsp,"newline after or whitespace between glob patterns"))
590 int pcf_range(int ctoken, char *const *pv, int *rtrue) {
591 int mintoken, min, maxtoken, max, r;
596 r= pa_mwsp(); if (r) return r;
597 mintoken= pa_numberdollar(&min); if (mintoken == tokv_error) return mintoken;
598 r= pa_mwsp(); if (r) return r;
599 maxtoken= pa_numberdollar(&max); if (maxtoken == tokv_error) return maxtoken;
600 r= pa_mnl(); if (r) return r;
601 for (pp= pv; *pp; pp++) {
602 v= strtoul(*pp,&ep,10); if (*ep) continue;
603 if (mintoken != tokv_dollar && v < min) continue;
604 if (maxtoken != tokv_dollar && v > max) continue;
610 int pcf_grep(int ctoken, char *const *pv, int *rtrue) {
615 int r, maxlen, l, c, actrue, posstrue;
617 r= paa_1path(&cp); if (r) return r;
620 parseerrprint("unable to open file `%s' for grep: %s",cp,strerror(errno));
624 for (pp= pv; *pp; pp++) { l= strlen(*pp); if (l > maxlen) maxlen= l; }
625 buf= xmalloc(maxlen+2); actrue= 0; c= 0;
626 while (!actrue && c!=EOF) {
627 c= getc(file); if (c==EOF) break;
628 if (isspace(c)) continue;
630 while (l>0 && c!='\n' && c!=EOF) { *p++= c; l--; c= getc(file); }
631 if (c=='\n' || c==EOF || isspace(c)) {
632 while (p>buf && isspace(p[-1])) --p;
634 for (pp= pv; !posstrue && *pp; pp++)
635 if (!strcmp(*pp,buf)) posstrue= 1;
639 if (c!='\n' && c!=EOF) {
642 if (c==EOF || c=='\n') break;
643 if (!isspace(c)) posstrue= 0;
646 if (posstrue) actrue= 1;
649 parseerrprint("error while reading `%s' for grep: %s",cp,strerror(errno));
650 fclose(file); free(buf); return tokv_error;
652 assert(actrue || feof(file));
653 fclose(file); free(buf); *rtrue= actrue;
657 static int pa_condition(int *rtrue) {
658 /* Scans up to and including the newline following the condition;
659 * may scan more than one line if the condition is a multi-line
660 * one. Returns 0 (setting *rtrue, and parsing up to and including
661 * the last newline) or tokv_error.
662 * Expects to scan the whitespace before its condition.
664 int r, token, andor, ctrue, actrue;
667 r= pa_mwsp(); if (r) return r;
669 if (token == tokv_error) {
671 } else if (token == tokv_not) {
672 r= pa_condition(&ctrue); if (r) return r;
673 *rtrue= !ctrue; return 0;
674 } else if (token == tokv_openparen) {
675 andor= 0; actrue= -1;
677 cstate->reportlineno= cstate->lineno;
678 r= pa_condition(&ctrue); if (r) return r;
680 case 0: assert(actrue==-1); actrue= ctrue; break;
681 case tokv_and: assert(actrue>=0); actrue= actrue && ctrue; break;
682 case tokv_or: assert(actrue>=0); actrue= actrue || ctrue; break;
685 do { token= yylex(); } while (token == tokv_lwsp);
686 if (token == tokv_error) return token;
687 if (token == tokv_closeparen) break;
689 r= unexpected(token,andor,"same conjunction as before"); if (r) return r;
691 if (token != tokv_and && token != tokv_or)
692 return unexpected(token,-1,"first conjunction inside connective");
696 r= pa_mnl(); if (r) return r;
697 *rtrue= actrue; return 0;
698 } else if (token & tokt_parmcondition) {
699 r= pa_mwsp(); if (r) return r;
700 r= pa_parameter(&parmvalues,0); if (r) return r;
701 r= (lr_parmcond)(token,parmvalues,rtrue); freecharparray(parmvalues);
704 return unexpected(token,-1,"condition");
709 * Directive functions and associated `common code' functions
712 /* Directives specifying the service program (execute-... and reset) */
714 static void execreset(void) {
717 free(execpath); execpath= 0;
718 freecharparray(execargs); execargs= 0;
721 int df_reject(int dtoken) {
724 r= pa_mnl(); if (r) return r;
726 execute= tokv_word_reject;
730 int df_executefrompath(int dtoken) {
733 r= pa_mnl(); if (r) return r;
735 execute= tokv_word_executefrompath;
739 int df_execute(int dtoken) {
744 r= paa_pathargs(&rv,&newargs); if (r) return r;
746 execute= tokv_word_execute;
748 execpath= xstrsave(rv);
752 int df_executefromdirectory(int dtoken) {
753 const char *p, *q, *rv;
758 r= paa_pathargs(&rv,&newargs); if (r) return r;
759 p= strrchr(service,'/'); if (p) p++; else p= service;
760 if (!*p || !isalnum(*p)) {
761 parseerrprint("execute-from-directory requires initial char of service "
762 "portion to be alphanumeric (service portion was `%s')",
764 freecharparray(newargs);
767 for (q=p+1; *q; q++) {
768 if (!isalnum(*q) && *q != '-') {
769 parseerrprint("execute-from-directory requires service portion to "
770 "contain only alphanumerics and hyphens (was `%s')",
772 freecharparray(newargs);
776 l= strlen(rv)+1+strlen(p)+1;
778 snyprintf(fn,l,"%s/%s",rv,p);
779 if (stat(fn,&stab)) {
780 if (errno == ENOENT) { free(fn); freecharparray(newargs); return 0; }
781 parseerrprint("failed to stat `%s' for execute-from-directory: %s",
783 free(fn); freecharparray(newargs); return tokv_error;
785 if (!S_ISREG(stab.st_mode)) {
786 parseerrprint("file `%s' in execute-from-directory is not an ordinary file"
787 " or link to one (mode=0%lo)",fn,(unsigned long)stab.st_mode);
788 free(fn); freecharparray(newargs); return tokv_error;
791 execute= tokv_word_executefromdirectory;
797 /* Parsing builtin service requests (execute-builtin) */
799 static int bispa_none(char ***rnewargs) {
803 static int bispa_parameter(char ***rnewargs) {
805 char **parmvalues, *name, **newargs;
807 r= pa_mwsp(); if (r) return r;
808 r= pa_parameter(&parmvalues,&name); if (r) return r;
809 for (i=0; parmvalues[i]; i++);
810 newargs= xmalloc(sizeof(char*)*(i+2));
812 memcpy(newargs+1,parmvalues,sizeof(char*)*(i+1));
814 r= pa_mnl(); if (r) { free(newargs); return r; }
819 int df_executebuiltin(int dtoken) {
821 builtinserviceexec_fnt *bisexec;
822 char *newpath, **newargs;
824 r= pa_mwsp(); if (r) return r;
825 r= yylex(); if (r == tokv_error) return r;
826 if (!(r & tokt_builtinservice)) return unexpected(r,-1,"builtin service name");
828 newpath= xstrsave(yytext);
830 r= lr_bispa(&newargs); if (r) { free(newpath); return r; }
833 execute= tokv_word_executebuiltin;
834 execbuiltin= bisexec;
840 /* Directives for changing other execution parameters */
842 int dfg_setflag(int dtoken) {
845 r= pa_mnl(); if (r) return r;
846 *lr_flag= lr_flagval;
850 int df_reset(int dtoken) {
853 r= pa_mnl(); if (r) return r;
854 r= parse_string(RESET_CONFIGURATION,"<builtin reset configuration>",1);
858 int dfg_fdwant(int dtoken) {
859 int fdmin, fdmax, r, needreadwrite, havereadwrite, fd;
861 needreadwrite= lr_fdwant_readwrite;
862 r= pa_mwsp(); if (r) return r;
863 r= yylex(); if (r == tokv_error) return r;
864 if (!(r & tokt_fdrange)) return unexpected(r,-1,"file descriptor range");
865 fdmin= lr_min; fdmax= lr_max;
866 if (fdmin<0 || fdmin>MAX_ALLOW_FD ||
867 (fdmax != -1 && fdmax<0) || fdmax>MAX_ALLOW_FD) {
868 parseerrprint("file descriptor in range is negative or far too large");
871 r= yylex(); if (r == tokv_error) return r;
872 if (r == tokv_newline) {
873 if (needreadwrite > 0) {
874 parseerrprint("read or write is required");
878 } else if (r == tokv_lwsp) {
879 if (needreadwrite < 0) {
880 parseerrprint("read or write not allowed"); return tokv_error;
882 r= yylex(); if (r == tokv_error) return r;
883 if (!(r & tokt_readwrite))
884 return unexpected(r,-1,"read or write (or perhaps newline)");
886 r= pa_mnl(); if (r) return r;
888 return unexpected(r,-1,"whitespace before read or write or newline");
890 ensurefdarray(fdmin);
892 if (!(dtoken == tokv_word_rejectfd || dtoken == tokv_word_ignorefd))
893 parseerrprint("unspecified maximum only allowed with reject-fd and ignore-fd");
894 fdmax= fdarrayused-1;
895 restfdwantstate= dtoken;
896 restfdwantrw= havereadwrite;
898 ensurefdarray(fdmax);
899 for (fd=fdmin; fd<=fdmax; fd++) {
900 fdarray[fd].wantstate= dtoken;
901 fdarray[fd].wantrw= havereadwrite;
906 /* Directives for changing error handling */
908 int df_errorstostderr(int dtoken) {
911 r= pa_mnl(); if (r) return r;
912 closeerrorfile(); eh.handling= dtoken;
916 int df_errorstosyslog(int dtoken) {
917 int token, level, facility;
919 facility= DEFUSERLOGFACILITY;
920 level= DEFUSERLOGLEVEL;
922 if (token == tokv_lwsp) {
923 token= yylex(); if (token == tokv_error) return token;
924 if (!(token & tokt_logfacility))
925 return unexpected(token,-1,"syslog facility (or end of line)");
926 facility= lr_logfacility;
929 if (token == tokv_lwsp) {
930 token= yylex(); if (token == tokv_error) return token;
931 if (!(token & tokt_loglevel))
932 return unexpected(token,-1,"syslog level (or end of line) after facility");
936 if (unexpected(token,tokv_newline,"end of line after errors-to-syslog"))
938 closeerrorfile(); eh.handling= tokv_word_errorstosyslog;
939 eh.logfacility= facility; eh.loglevel= level;
943 int df_errorstofile(int dtoken) {
948 r= paa_1path(&cp); if (r) return r;
951 parseerrprint("unable to open error log file `%s': %s",cp,strerror(errno));
954 if (setvbuf(file,0,_IOLBF,MAX_ERRMSG_LEN)) {
955 parseerrprint("unable to set line buffering on errors file: %s",strerror(errno));
956 fclose(file); return tokv_error;
958 closeerrorfile(); eh.handling= tokv_word_errorstofile;
959 eh.file= file; eh.filename= xstrsave(cp);
963 /* Directives for including other files or configuration data */
965 int dfi_includeuserrcfile(int dtoken) {
968 r= pa_mnl(); if (r) return r;
970 return parse_file(userrcfile,0);
973 int dfi_includeclientconfig(int dtoken) {
976 r= pa_mnl(); if (r) return r;
977 assert(overridedata);
978 return parse_string(overridedata,"<configuration override data>",0);
981 int df_include(int dtoken) {
985 r= paa_1path(&cp); if (r) return r;
986 r= parse_file(cp,&found); if (r) return r;
987 if (found || dtoken == tokv_word_includeifexist) return 0;
988 parseerrprint(dtoken == tokv_word_includesysconfig ?
989 "system configuration file `%s' does not exist" :
990 "included file `%s' does not exist",
995 int df_includedirectory(int dtoken) {
996 static char *buildbuf=0;
997 static int buildbuflen=0;
999 int r, cpl, tel, c, found;
1002 const char *p, *cpget;
1005 r= paa_1path(&cpget); if (r) return r;
1008 parseerrprint("unable to open directory `%s': %s",cpget,strerror(errno));
1011 cp= xstrsave(cpget);
1013 while ((de= readdir(d))) {
1014 tel= strlen(de->d_name);
1017 if (!*p || !isalnum(*p)) continue;
1018 while ((c= *++p)) if (!(isalnum(c) || c=='-')) break;
1020 if (makeroom(&buildbuf,&buildbuflen,cpl+1+tel+1)) {
1021 stringoverflow("pathname in directory");
1022 r= tokv_error; goto x_err;
1024 snyprintf(buildbuf,buildbuflen,"%s/%s",cp,de->d_name);
1025 r= parse_file(buildbuf,&found); if (r) goto x_err;
1027 parseerrprint("unable to open file `%s' in included directory `%s': %s",
1028 de->d_name,cp,strerror(errno));
1029 r= tokv_error; goto x_err;
1033 parseerrprint("error closing directory `%s': %s",cp,strerror(errno));
1046 int df_includelookup(int dtoken) {
1047 static char *buildbuf=0;
1050 char **parmvalues, **pp, *p, *q, *cp;
1053 int r, done, thisdone, cpl, c;
1055 r= pa_mwsp(); if (r) return r;
1056 r= pa_parameter(&parmvalues,0); if (r) return r;
1057 r= paa_1path(&cpget); if (r) { freecharparray(parmvalues); return r; }
1058 if (stat(cpget,&stab)) {
1059 parseerrprint("unable to access directory `%s': %s",cpget,strerror(errno));
1060 freecharparray(parmvalues); return tokv_error;
1062 if (!S_ISDIR(stab.st_mode)) {
1063 parseerrprint("object `%s' is not a directory or link to one",cpget);
1064 freecharparray(parmvalues); return tokv_error;
1067 cp= xstrsave(cpget);
1069 if (!parmvalues[0]) {
1070 if (makeroom(&buildbuf,&buildbuflen,cpl+1+sizeof(NONEINCLUDELOOKUP))) {
1071 stringoverflow("pathname in directory for lookup of undefined parameter");
1072 r= tokv_error; goto x_err;
1074 snyprintf(buildbuf,buildbuflen,"%s/" NONEINCLUDELOOKUP,cp);
1075 r= parse_file(buildbuf,&thisdone); if (r) goto x_err;
1076 if (thisdone) done= 1;
1079 *pp && (!done || dtoken == tokv_word_includelookupall);
1081 if (makeroom(&buildbuf,&buildbuflen,
1082 cpl+1+strlen(*pp)*2+3+sizeof(EMPTYINCLUDELOOKUP)+1)) {
1083 stringoverflow("pathname in directory for lookup");
1084 r= tokv_error; goto x_err;
1086 strcpy(buildbuf,cp);
1087 p= *pp; q= buildbuf+cpl;
1090 strcpy(q,EMPTYINCLUDELOOKUP);
1092 if (*p=='.') *q++= ':';
1097 } else if (!((c >= '0' && c <= '9') ||
1098 (c >= 'a' && c <= 'z') ||
1099 c == '-' || c == '_')) {
1106 r= parse_file(buildbuf,&thisdone);
1108 if (thisdone) done= 1;
1112 if (makeroom(&buildbuf,&buildbuflen,
1113 cpl+1+sizeof(DEFAULTINCLUDELOOKUP))) {
1114 stringoverflow("pathname in directory for lookup of default");
1115 r= tokv_error; goto x_err;
1117 snyprintf(buildbuf,buildbuflen,"%s/" DEFAULTINCLUDELOOKUP,cp);
1118 r= parse_file(buildbuf,0); if (r) goto x_err;
1123 freecharparray(parmvalues);
1128 /* Control constructs */
1130 int df_catchquit(int dtoken) {
1133 r= pa_mnl(); if (r) return r;
1134 r= parser(tokv_word_catchquit);
1135 if (r == tokv_quit || r == tokv_error) {
1136 if (r == tokv_error) {
1137 r= parse_string(RESET_CONFIGURATION,
1138 "<builtin reset configuration (caught error)>",1);
1141 r= skip(tokv_word_catchquit);
1143 if (r & tokt_controlend) {
1144 assert(r == tokv_word_hctac);
1150 int df_if(int dtoken) {
1155 r= pa_condition(&true); if (r) return r;
1156 if (!done && true) { r= parser(tokv_word_if); done= 1; }
1157 else { r= skip(tokv_word_if); }
1158 if (!(r & tokt_controlend)) return r;
1159 } while (r == tokv_word_elif);
1160 if (r == tokv_word_else) {
1161 r= pa_mnl(); if (r) return r;
1162 cstate->reportlineno= cstate->lineno;
1163 if (done) r= skip(tokv_word_if);
1164 else r= parser(tokv_word_if);
1165 if (!(r & tokt_controlend)) return r;
1167 if (unexpected(r,tokv_word_fi,"`fi' to end `if'")) return tokv_error;
1171 int df_errorspush(int dt) {
1172 struct error_handling save;
1175 r= pa_mnl(); if (r) return r;
1180 r= parser(tokv_word_errorspush);
1185 if (r & tokt_controlend) {
1186 assert(r == tokv_word_srorre);
1192 /* Miscelleanous directives */
1194 int df_cd(int dtoken) {
1198 r= paa_1path(&cp); if (r) return r;
1199 if (!chdir(cp)) return 0;
1200 parseerrprint("unable to change directory to `%s': %s",cp,strerror(errno));
1204 int df_userrcfile(int dtoken) {
1208 r= paa_1path(&cp); if (r) return r;
1209 free(userrcfile); userrcfile= xstrsave(cp);
1213 int df_message(int dtoken) {
1217 r= paa_message(&mp); if (r) return r;
1218 parseerrprint("`message' directive: %s",mp);
1222 int df_error(int dtoken) {
1226 r= paa_message(&mp); if (r) return r;
1227 parseerrprint("`error' directive: %s",mp);
1231 int df_eof(int dtoken) {
1234 r= pa_mnl(); if (r) return r;
1238 int df_quit(int dtoken) {
1241 r= pa_mnl(); if (r) return r;
1246 * Main parser routines
1249 static void parser_push(struct parser_state *usestate,
1250 const char *newfile,
1251 const struct stat *newfilestab,
1252 YY_BUFFER_STATE ybuf,
1254 usestate->lineno= 1;
1255 usestate->reportlineno= 1;
1256 usestate->filename= newfile;
1257 usestate->filestab= *newfilestab;
1258 usestate->notedreferer= 0;
1259 usestate->isinternal= isinternal;
1260 usestate->ybuf= ybuf;
1261 usestate->upstate= cstate;
1264 yy_switch_to_buffer(ybuf);
1267 static void parser_pop(void) {
1268 struct parser_state *oldstate;
1271 cstate= cstate->upstate;
1272 if (cstate) yy_switch_to_buffer(cstate->ybuf);
1273 yy_delete_buffer(oldstate->ybuf);
1276 int parse_string(const char *string, const char *descrip, int isinternal) {
1277 /* Returns the same things as parser, except that tokv_eof is turned
1278 * into 0. *string must be statically allocated or copied, so that
1279 * it is not overwritten while the parsing takes place (unlike with
1282 static const struct stat blankstab;
1284 struct parser_state usestate;
1285 YY_BUFFER_STATE ybuf;
1288 ybuf= yy_scan_string(string);
1289 if (!ybuf) syscallerror("unable to create flex buffer for internal string");
1290 parser_push(&usestate,descrip,&blankstab,ybuf,isinternal);
1295 if (r == tokv_eof) r= 0;
1299 static int parse_file(const char *string, int *didexist) {
1300 /* Returns the same things as parser, except that tokv_eof is turned
1301 * into 0. If *didexist is 0 then errno will have been set.
1302 * *string will be copied by parse_file so it may be be overwritten
1303 * during the parsing (so, for example, yytext need not be copied).
1305 static int fileparselevel= 0;
1307 struct parser_state usestate, *checkrecurse;
1308 YY_BUFFER_STATE ybuf;
1312 struct stat newstab;
1314 if (fileparselevel >= MAX_INCLUDE_NEST) {
1315 parseerrprint("too many nested levels of included files");
1318 file= fopen(string,"r");
1320 if (errno == ENOENT) {
1321 if (didexist) *didexist= 0;
1324 parseerrprint("unable to open config file `%s': %s",string,strerror(errno));
1327 r= fstat(fileno(file),&newstab); if (r) syscallerror("unable to fstat new file");
1328 for (checkrecurse= cstate; checkrecurse; checkrecurse= checkrecurse->upstate) {
1329 if (!checkrecurse->filestab.st_mode) continue;
1330 if (newstab.st_dev==checkrecurse->filestab.st_dev &&
1331 newstab.st_ino==checkrecurse->filestab.st_ino) {
1332 parseerrprint("recursion detected - config file `%s' calls itself",string);
1338 if (didexist) *didexist= 1;
1340 ybuf= yy_create_buffer(file,YY_BUF_SIZE);
1341 if (!ybuf) syscallerror("unable to create flex buffer for file");
1342 filename= xstrsave(string);
1343 parser_push(&usestate,filename,&newstab,ybuf,0);
1348 parseerrprint("error reading configuration file `%s'",string);
1356 if (r == tokv_eof) r= 0;
1360 static int parser(int allowce) {
1362 * an exception (error, eof or quit)
1363 * then rest of `file' is uninteresting
1365 * token if allowce was !0 and equal to token's controlend
1366 * then rest of `file' (including rest of line with the
1367 * controlend - even the whitespace) not scanned yet
1371 for (;;) { /* loop over lines */
1372 cstate->reportlineno= cstate->lineno;
1373 do { token= yylex(); } while (token == tokv_lwsp);
1374 if (token & tokt_exception) {
1376 } else if (token & tokt_controlend) {
1377 if (lr_controlend == allowce) return token;
1378 else return unexpected(token,-1,"directive (not this kind of"
1379 " control structure end)");
1380 } else if (token & tokt_directive) {
1381 if ((token & tokt_internal) && !cstate->isinternal)
1382 return unexpected(token,-1,"published directive, not internal-use-only one");
1383 r= (lr_dir)(token); if (r) { assert(r & tokt_exception); return r; }
1384 } else if (token == tokv_newline) {
1385 /* ignore blank lines (and comment-only lines) */
1387 return unexpected(token,-1,"directive");