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++;
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;
376 r= pa_mwsp(); if (r) return r;
378 if (makeroom(&buildbuf,&buildbuflen,50)) return stringoverflow("start of message");
381 r= yylex(); if (r == tokv_error) return r;
383 parseerrprint("unexpected end of file in message text");
386 if (r == tokv_newline) break;
388 if (makeroom(&buildbuf,&buildbuflen,tl)) return stringoverflow("message");
389 strcat(buildbuf,yytext);
391 *message_r= buildbuf;
396 * Skipping routines (used by e.g. the `if' code).
399 static int skiptoeol(void) {
400 /* Returns 0 if OK, having just parsed the newline
401 * or tokv_error if an error occurs, leaving the position at the error
402 * (which may be EOF or a syntax error token).
406 do { token= yylex(); }
407 while (token != tokv_newline && !(token & tokt_exception));
408 if (token == tokv_newline) return 0;
409 if (token == tokv_error) return token;
410 assert(token == tokv_eof);
411 parseerrprint("unexpected end of file while looking for end of line");
415 static int skip(int allowce) {
416 /* Scans a piece of config without executing it. Returns
417 * tokv_error (leaving the parsing state at the error),
418 * or the tokt_controlend token with type allowce if one
419 * was found (in which case rest of line, including any whitespace
420 * after tokt_controlend, has not been parsed), or tokv_eof.
424 for (;;) { /* loop over lines */
425 cstate->reportlineno= cstate->lineno;
426 do { token= yylex(); } while (token == tokv_lwsp);
427 if (token & tokt_exception) {
429 } else if (token & tokt_controlend) {
430 if (allowce == lr_controlend) return token;
431 else return unexpected(token,-1,"control structure end of a different kind");
432 } else if (token & tokt_controlstart) {
434 while (r & tokt_controlstart) {
435 r= skiptoeol(); if (r) return r;
436 cstate->reportlineno= cstate->lineno;
437 r= skip(token); if (r & tokt_exception) return r;
439 } else if (!(token & tokt_directive) && !(token & tokt_condop)) {
440 parseerrprint("not a directive (or conditional operator) "
441 "while looking for control structure end");
444 r= skiptoeol(); if (r) return r;
449 * Routines for parsing and getting the values of parameters
452 static void parm_1string(char ***rvalues, const char *tocopy) {
454 a= xmalloc(sizeof(char*)*2);
455 a[0]= xstrsave(tocopy);
460 static char *parm_ulong(unsigned long ul) {
464 l= CHAR_BIT*sizeof(unsigned long)/3+4;
466 snyprintf(p,l,"%lu",ul);
470 static int parm_usernameuid(char ***rvalues, const char *name, uid_t id) {
473 a= xmalloc(sizeof(char*)*3);
474 a[0]= xstrsave(name);
475 a[1]= parm_ulong(id);
481 static int parm_groups(char ***rvalues, int size,
482 gid_t *gids, const char *const *groups) {
486 if (size >= 2 && gids[0] == gids[1]) { size--; gids++; groups++; }
487 a= xmalloc(sizeof(char*)*(size+1)*2);
488 for (i=0; i<size; i++) {
489 a[i]= xstrsave(groups[i]);
490 a[size+i]= parm_ulong(gids[i]);
497 static int pf_service(int ptoken, char ***rvalues) {
498 parm_1string(rvalues,service); return 0;
501 static int pf_callinguser(int ptoken, char ***rvalues) {
502 return parm_usernameuid(rvalues,logname,request_mbuf.callinguid);
505 static int pf_serviceuser(int ptoken, char ***rvalues) {
506 return parm_usernameuid(rvalues,serviceuser,serviceuser_uid);
509 static int pf_callinggroup(int ptoken, char ***rvalues) {
510 return parm_groups(rvalues,request_mbuf.ngids,calling_gids,calling_groups);
513 static int pf_servicegroup(int ptoken, char ***rvalues) {
514 return parm_groups(rvalues,service_ngids,service_gids,service_groups);
517 static int pf_callingusershell(int ptoken, char ***rvalues) {
518 parm_1string(rvalues,callinguser_shell); return 0;
521 static int pf_serviceusershell(int ptoken, char ***rvalues) {
522 parm_1string(rvalues,serviceuser_shell); return 0;
525 static int pa_parameter(char ***rvalues, char **rname) {
526 /* Scans a single parameter token and calls the appropriate parameter
527 * function, returning tokv_error (leaving the parser state wherever),
528 * or 0 on success (having just parsed the parameter name).
529 * If rname is non-null then the name of the parameter (malloc'd) will
532 * Caller must free *rvalues using freecharparray.
537 token= yylex(); if (token == tokv_error) return token;
538 name= xstrsave(yytext);
539 if ((token & tokm_repres) != tokr_nonstring &&
540 !memcmp(yytext,"u-",2) && strlen(yytext)>=3) {
542 i<request_mbuf.nvars && strcmp(yytext+2,defvararray[i].key);
544 if (i>=request_mbuf.nvars) {
545 *rvalues= xmalloc(sizeof(char*));
548 parm_1string(rvalues,defvararray[i].value);
550 } else if (token & tokt_parameter) {
551 r= (lr_parameter)(token,rvalues);
552 if (r) { free(name); return r; }
555 return unexpected(token,-1,"parameter name");
557 debug_dumpparameter(name,*rvalues);
558 if (rname) *rname= name;
564 * Routines for parsing conditions, including
565 * parameter-based conditional functions (parmcondition's).
568 int pcf_glob(int ctoken, char *const *pv, int *rtrue) {
569 int token, actrue, r;
573 r= pa_mwsp(); if (r) return r;
576 if ((token & tokm_repres) == tokr_nonstring)
577 return unexpected(token,-1,"glob pattern");
578 for (pp= pv; !actrue && *pp; pp++) if (!fnmatch(yytext,*pp,0)) actrue= 1;
580 if (token == tokv_newline) break;
581 if (unexpected(token,tokv_lwsp,"newline after or whitespace between glob patterns"))
588 int pcf_range(int ctoken, char *const *pv, int *rtrue) {
589 int mintoken, min, maxtoken, max, r;
594 r= pa_mwsp(); if (r) return r;
595 mintoken= pa_numberdollar(&min); if (mintoken == tokv_error) return mintoken;
596 r= pa_mwsp(); if (r) return r;
597 maxtoken= pa_numberdollar(&max); if (maxtoken == tokv_error) return maxtoken;
598 r= pa_mnl(); if (r) return r;
599 for (pp= pv; *pp; pp++) {
600 v= strtoul(*pp,&ep,10); if (*ep) continue;
601 if (mintoken != tokv_dollar && v < min) continue;
602 if (maxtoken != tokv_dollar && v > max) continue;
608 int pcf_grep(int ctoken, char *const *pv, int *rtrue) {
613 int r, maxlen, l, c, actrue, posstrue;
615 r= paa_1path(&cp); if (r) return r;
618 parseerrprint("unable to open file `%s' for grep: %s",cp,strerror(errno));
622 for (pp= pv; *pp; pp++) { l= strlen(*pp); if (l > maxlen) maxlen= l; }
623 buf= xmalloc(maxlen+2); actrue= 0; c= 0;
624 while (!actrue && c!=EOF) {
625 c= getc(file); if (c==EOF) break;
626 if (isspace(c)) continue;
628 while (l>0 && c!='\n' && c!=EOF) { *p++= c; l--; c= getc(file); }
629 if (c=='\n' || c==EOF || isspace(c)) {
630 while (p>buf && isspace(p[-1])) --p;
632 for (pp= pv; !posstrue && *pp; pp++)
633 if (!strcmp(*pp,buf)) posstrue= 1;
637 if (c!='\n' && c!=EOF) {
640 if (c==EOF || c=='\n') break;
641 if (!isspace(c)) posstrue= 0;
644 if (posstrue) actrue= 1;
647 parseerrprint("error while reading `%s' for grep: %s",cp,strerror(errno));
648 fclose(file); free(buf); return tokv_error;
650 assert(actrue || feof(file));
651 fclose(file); free(buf); *rtrue= actrue;
655 static int pa_condition(int *rtrue) {
656 /* Scans up to and including the newline following the condition;
657 * may scan more than one line if the condition is a multi-line
658 * one. Returns 0 (setting *rtrue, and parsing up to and including
659 * the last newline) or tokv_error.
660 * Expects to scan the whitespace before its condition.
662 int r, token, andor, ctrue, actrue;
665 r= pa_mwsp(); if (r) return r;
667 if (token == tokv_error) {
669 } else if (token == tokv_not) {
670 r= pa_condition(&ctrue); if (r) return r;
671 *rtrue= !ctrue; return 0;
672 } else if (token == tokv_openparen) {
673 andor= 0; actrue= -1;
675 cstate->reportlineno= cstate->lineno;
676 r= pa_condition(&ctrue); if (r) return r;
678 case 0: assert(actrue==-1); actrue= ctrue; break;
679 case tokv_and: assert(actrue>=0); actrue= actrue && ctrue; break;
680 case tokv_or: assert(actrue>=0); actrue= actrue || ctrue; break;
683 do { token= yylex(); } while (token == tokv_lwsp);
684 if (token == tokv_error) return token;
685 if (token == tokv_closeparen) break;
687 r= unexpected(token,andor,"same conjunction as before"); if (r) return r;
689 if (token != tokv_and && token != tokv_or)
690 return unexpected(token,-1,"first conjunction inside connective");
694 r= pa_mnl(); if (r) return r;
695 *rtrue= actrue; return 0;
696 } else if (token & tokt_parmcondition) {
697 r= pa_mwsp(); if (r) return r;
698 r= pa_parameter(&parmvalues,0); if (r) return r;
699 r= (lr_parmcond)(token,parmvalues,rtrue); freecharparray(parmvalues);
702 return unexpected(token,-1,"condition");
707 * Directive functions and associated `common code' functions
710 /* Directives specifying the service program (execute-... and reset) */
712 static void execreset(void) {
715 free(execpath); execpath= 0;
716 freecharparray(execargs); execargs= 0;
719 int df_reject(int dtoken) {
722 r= pa_mnl(); if (r) return r;
724 execute= tokv_word_reject;
728 int df_executefrompath(int dtoken) {
731 r= pa_mnl(); if (r) return r;
733 execute= tokv_word_executefrompath;
737 int df_execute(int dtoken) {
742 r= paa_pathargs(&rv,&newargs); if (r) return r;
744 execute= tokv_word_execute;
746 execpath= xstrsave(rv);
750 int df_executefromdirectory(int dtoken) {
751 const char *p, *q, *rv;
756 r= paa_pathargs(&rv,&newargs); if (r) return r;
757 p= strrchr(service,'/'); if (p) p++; else p= service;
758 if (!*p || !isalnum(*p)) {
759 parseerrprint("execute-from-directory requires initial char of service "
760 "portion to be alphanumeric (service portion was `%s')",
762 freecharparray(newargs);
765 for (q=p+1; *q; q++) {
766 if (!isalnum(*q) && *q != '-') {
767 parseerrprint("execute-from-directory requires service portion to "
768 "contain only alphanumerics and hyphens (was `%s')",
770 freecharparray(newargs);
774 l= strlen(rv)+1+strlen(p)+1;
776 snyprintf(fn,l,"%s/%s",rv,p);
777 if (stat(fn,&stab)) {
778 if (errno == ENOENT) { free(fn); freecharparray(newargs); return 0; }
779 parseerrprint("failed to stat `%s' for execute-from-directory: %s",
781 free(fn); freecharparray(newargs); return tokv_error;
783 if (!S_ISREG(stab.st_mode)) {
784 parseerrprint("file `%s' in execute-from-directory is not an ordinary file"
785 " or link to one (mode=0%lo)",fn,(unsigned long)stab.st_mode);
786 free(fn); freecharparray(newargs); return tokv_error;
789 execute= tokv_word_executefromdirectory;
795 /* Parsing builtin service requests (execute-builtin) */
797 static int bispa_none(char ***rnewargs) {
801 static int bispa_parameter(char ***rnewargs) {
803 char **parmvalues, *name, **newargs;
805 r= pa_mwsp(); if (r) return r;
806 r= pa_parameter(&parmvalues,&name); if (r) return r;
807 for (i=0; parmvalues[i]; i++);
808 newargs= xmalloc(sizeof(char*)*(i+2));
810 memcpy(newargs+1,parmvalues,sizeof(char*)*(i+1));
812 r= pa_mnl(); if (r) { free(newargs); return r; }
817 int df_executebuiltin(int dtoken) {
819 builtinserviceexec_fnt *bisexec;
820 char *newpath, **newargs;
822 r= pa_mwsp(); if (r) return r;
823 r= yylex(); if (r == tokv_error) return r;
824 if (!(r & tokt_builtinservice)) return unexpected(r,-1,"builtin service name");
826 newpath= xstrsave(yytext);
828 r= lr_bispa(&newargs); if (r) { free(newpath); return r; }
831 execute= tokv_word_executebuiltin;
832 execbuiltin= bisexec;
838 /* Directives for changing other execution parameters */
840 int dfg_setflag(int dtoken) {
843 r= pa_mnl(); if (r) return r;
844 *lr_flag= lr_flagval;
848 int df_reset(int dtoken) {
851 r= pa_mnl(); if (r) return r;
852 r= parse_string(RESET_CONFIGURATION,"<builtin reset configuration>",1);
856 int dfg_fdwant(int dtoken) {
857 int fdmin, fdmax, r, needreadwrite, havereadwrite, fd;
859 needreadwrite= lr_fdwant_readwrite;
860 r= pa_mwsp(); if (r) return r;
861 r= yylex(); if (r == tokv_error) return r;
862 if (!(r & tokt_fdrange)) return unexpected(r,-1,"file descriptor range");
863 fdmin= lr_min; fdmax= lr_max;
864 if (fdmin<0 || fdmin>MAX_ALLOW_FD ||
865 (fdmax != -1 && fdmax<0) || fdmax>MAX_ALLOW_FD) {
866 parseerrprint("file descriptor in range is negative or far too large");
869 r= yylex(); if (r == tokv_error) return r;
870 if (r == tokv_newline) {
871 if (needreadwrite > 0) {
872 parseerrprint("read or write is required");
876 } else if (r == tokv_lwsp) {
877 if (needreadwrite < 0) {
878 parseerrprint("read or write not allowed"); return tokv_error;
880 r= yylex(); if (r == tokv_error) return r;
881 if (!(r & tokt_readwrite))
882 return unexpected(r,-1,"read or write (or perhaps newline)");
884 r= pa_mnl(); if (r) return r;
886 return unexpected(r,-1,"whitespace before read or write or newline");
888 ensurefdarray(fdmin);
890 if (!(dtoken == tokv_word_rejectfd || dtoken == tokv_word_ignorefd))
891 parseerrprint("unspecified maximum only allowed with reject-fd and ignore-fd");
892 fdmax= fdarrayused-1;
893 restfdwantstate= dtoken;
894 restfdwantrw= havereadwrite;
896 ensurefdarray(fdmax);
897 for (fd=fdmin; fd<=fdmax; fd++) {
898 fdarray[fd].wantstate= dtoken;
899 fdarray[fd].wantrw= havereadwrite;
904 /* Directives for changing error handling */
906 int df_errorstostderr(int dtoken) {
909 r= pa_mnl(); if (r) return r;
910 closeerrorfile(); eh.handling= dtoken;
914 int df_errorstosyslog(int dtoken) {
915 int token, level, facility;
917 facility= DEFUSERLOGFACILITY;
918 level= DEFUSERLOGLEVEL;
920 if (token == tokv_lwsp) {
921 token= yylex(); if (token == tokv_error) return token;
922 if (!(token & tokt_logfacility))
923 return unexpected(token,-1,"syslog facility (or end of line)");
924 facility= lr_logfacility;
927 if (token == tokv_lwsp) {
928 token= yylex(); if (token == tokv_error) return token;
929 if (!(token & tokt_loglevel))
930 return unexpected(token,-1,"syslog level (or end of line) after facility");
934 if (unexpected(token,tokv_newline,"end of line after errors-to-syslog"))
936 closeerrorfile(); eh.handling= tokv_word_errorstosyslog;
937 eh.logfacility= facility; eh.loglevel= level;
941 int df_errorstofile(int dtoken) {
946 r= paa_1path(&cp); if (r) return r;
949 parseerrprint("unable to open error log file `%s': %s",cp,strerror(errno));
952 if (setvbuf(file,0,_IOLBF,MAX_ERRMSG_LEN)) {
953 parseerrprint("unable to set line buffering on errors file: %s",strerror(errno));
954 fclose(file); return tokv_error;
956 closeerrorfile(); eh.handling= tokv_word_errorstofile;
957 eh.file= file; eh.filename= xstrsave(cp);
961 /* Directives for including other files or configuration data */
963 int dfi_includeuserrcfile(int dtoken) {
966 r= pa_mnl(); if (r) return r;
968 return parse_file(userrcfile,0);
971 int dfi_includeclientconfig(int dtoken) {
974 r= pa_mnl(); if (r) return r;
975 assert(overridedata);
976 return parse_string(overridedata,"<configuration override data>",0);
979 int df_include(int dtoken) {
983 r= paa_1path(&cp); if (r) return r;
984 r= parse_file(cp,&found); if (r) return r;
985 if (found || dtoken == tokv_word_includeifexist) return 0;
986 parseerrprint(dtoken == tokv_word_includesysconfig ?
987 "system configuration file `%s' does not exist" :
988 "included file `%s' does not exist",
993 int df_includedirectory(int dtoken) {
994 static char *buildbuf=0;
995 static int buildbuflen=0;
997 int r, cpl, tel, c, found;
1000 const char *p, *cpget;
1003 r= paa_1path(&cpget); if (r) return r;
1006 parseerrprint("unable to open directory `%s': %s",cpget,strerror(errno));
1009 cp= xstrsave(cpget);
1011 while ((de= readdir(d))) {
1012 tel= strlen(de->d_name);
1015 if (!*p || !isalnum(*p)) continue;
1016 while ((c= *++p)) if (!(isalnum(c) || c=='-')) break;
1018 if (makeroom(&buildbuf,&buildbuflen,cpl+1+tel+1)) {
1019 stringoverflow("pathname in directory");
1020 r= tokv_error; goto x_err;
1022 snyprintf(buildbuf,buildbuflen,"%s/%s",cp,de->d_name);
1023 r= parse_file(buildbuf,&found); if (r) goto x_err;
1025 parseerrprint("unable to open file `%s' in included directory `%s': %s",
1026 de->d_name,cp,strerror(errno));
1027 r= tokv_error; goto x_err;
1031 parseerrprint("error closing directory `%s': %s",cp,strerror(errno));
1044 int df_includelookup(int dtoken) {
1045 static char *buildbuf=0;
1048 char **parmvalues, **pp, *p, *q, *cp;
1051 int r, done, thisdone, cpl, c;
1053 r= pa_mwsp(); if (r) return r;
1054 r= pa_parameter(&parmvalues,0); if (r) return r;
1055 r= paa_1path(&cpget); if (r) { freecharparray(parmvalues); return r; }
1056 if (stat(cpget,&stab)) {
1057 parseerrprint("unable to access directory `%s': %s",cpget,strerror(errno));
1058 freecharparray(parmvalues); return tokv_error;
1060 if (!S_ISDIR(stab.st_mode)) {
1061 parseerrprint("object `%s' is not a directory or link to one",cpget);
1062 freecharparray(parmvalues); return tokv_error;
1065 cp= xstrsave(cpget);
1067 if (!parmvalues[0]) {
1068 if (makeroom(&buildbuf,&buildbuflen,cpl+1+sizeof(NONEINCLUDELOOKUP))) {
1069 stringoverflow("pathname in directory for lookup of undefined parameter");
1070 r= tokv_error; goto x_err;
1072 snyprintf(buildbuf,buildbuflen,"%s/" NONEINCLUDELOOKUP,cp);
1073 r= parse_file(buildbuf,&thisdone); if (r) goto x_err;
1074 if (thisdone) done= 1;
1077 *pp && (!done || dtoken == tokv_word_includelookupall);
1079 if (makeroom(&buildbuf,&buildbuflen,
1080 cpl+1+strlen(*pp)*2+3+sizeof(EMPTYINCLUDELOOKUP)+1)) {
1081 stringoverflow("pathname in directory for lookup");
1082 r= tokv_error; goto x_err;
1084 strcpy(buildbuf,cp);
1085 p= *pp; q= buildbuf+cpl;
1088 strcpy(q,EMPTYINCLUDELOOKUP);
1090 if (*p=='.') *q++= ':';
1092 if (c=='/') { *q++= ':'; c='-'; }
1093 else if (c==':') { *q++= ':'; }
1098 r= parse_file(buildbuf,&thisdone);
1100 if (thisdone) done= 1;
1104 if (makeroom(&buildbuf,&buildbuflen,
1105 cpl+1+sizeof(DEFAULTINCLUDELOOKUP))) {
1106 stringoverflow("pathname in directory for lookup of default");
1107 r= tokv_error; goto x_err;
1109 snyprintf(buildbuf,buildbuflen,"%s/" DEFAULTINCLUDELOOKUP,cp);
1110 r= parse_file(buildbuf,0); if (r) goto x_err;
1115 freecharparray(parmvalues);
1120 /* Control constructs */
1122 int df_catchquit(int dtoken) {
1125 r= pa_mnl(); if (r) return r;
1126 r= parser(tokv_word_catchquit);
1127 if (r == tokv_quit || r == tokv_error) {
1128 if (r == tokv_error) {
1129 r= parse_string(RESET_CONFIGURATION,
1130 "<builtin reset configuration (caught error)>",1);
1133 r= skip(tokv_word_catchquit);
1135 if (r & tokt_controlend) {
1136 assert(r == tokv_word_hctac);
1142 int df_if(int dtoken) {
1147 r= pa_condition(&true); if (r) return r;
1148 if (!done && true) { r= parser(tokv_word_if); done= 1; }
1149 else { r= skip(tokv_word_if); }
1150 if (!(r & tokt_controlend)) return r;
1151 } while (r == tokv_word_elif);
1152 if (r == tokv_word_else) {
1153 r= pa_mnl(); if (r) return r;
1154 cstate->reportlineno= cstate->lineno;
1155 if (done) r= skip(tokv_word_if);
1156 else r= parser(tokv_word_if);
1157 if (!(r & tokt_controlend)) return r;
1159 if (unexpected(r,tokv_word_fi,"`fi' to end `if'")) return tokv_error;
1163 int df_errorspush(int dt) {
1164 struct error_handling save;
1167 r= pa_mnl(); if (r) return r;
1172 r= parser(tokv_word_errorspush);
1177 if (r & tokt_controlend) {
1178 assert(r == tokv_word_srorre);
1184 /* Miscelleanous directives */
1186 int df_cd(int dtoken) {
1190 r= paa_1path(&cp); if (r) return r;
1191 if (!chdir(cp)) return 0;
1192 parseerrprint("unable to change directory to `%s': %s",cp,strerror(errno));
1196 int df_userrcfile(int dtoken) {
1200 r= paa_1path(&cp); if (r) return r;
1201 free(userrcfile); userrcfile= xstrsave(cp);
1205 int df_message(int dtoken) {
1209 r= paa_message(&mp); if (r) return r;
1210 parseerrprint("`message' directive: %s",mp);
1214 int df_error(int dtoken) {
1218 r= paa_message(&mp); if (r) return r;
1219 parseerrprint("`error' directive: %s",mp);
1223 int df_eof(int dtoken) {
1226 r= pa_mnl(); if (r) return r;
1230 int df_quit(int dtoken) {
1233 r= pa_mnl(); if (r) return r;
1238 * Main parser routines
1241 static void parser_push(struct parser_state *usestate,
1242 const char *newfile,
1243 const struct stat *newfilestab,
1244 YY_BUFFER_STATE ybuf,
1246 usestate->lineno= 1;
1247 usestate->reportlineno= 1;
1248 usestate->filename= newfile;
1249 usestate->filestab= *newfilestab;
1250 usestate->notedreferer= 0;
1251 usestate->isinternal= isinternal;
1252 usestate->ybuf= ybuf;
1253 usestate->upstate= cstate;
1256 yy_switch_to_buffer(ybuf);
1259 static void parser_pop(void) {
1260 struct parser_state *oldstate;
1263 cstate= cstate->upstate;
1264 if (cstate) yy_switch_to_buffer(cstate->ybuf);
1265 yy_delete_buffer(oldstate->ybuf);
1268 int parse_string(const char *string, const char *descrip, int isinternal) {
1269 /* Returns the same things as parser, except that tokv_eof is turned
1270 * into 0. *string must be statically allocated or copied, so that
1271 * it is not overwritten while the parsing takes place (unlike with
1274 static const struct stat blankstab;
1276 struct parser_state usestate;
1277 YY_BUFFER_STATE ybuf;
1280 ybuf= yy_scan_string(string);
1281 if (!ybuf) syscallerror("unable to create flex buffer for internal string");
1282 parser_push(&usestate,descrip,&blankstab,ybuf,isinternal);
1287 if (r == tokv_eof) r= 0;
1291 static int parse_file(const char *string, int *didexist) {
1292 /* Returns the same things as parser, except that tokv_eof is turned
1293 * into 0. If *didexist is 0 then errno will have been set.
1294 * *string will be copied by parse_file so it may be be overwritten
1295 * during the parsing (so, for example, yytext need not be copied).
1297 static int fileparselevel= 0;
1299 struct parser_state usestate, *checkrecurse;
1300 YY_BUFFER_STATE ybuf;
1304 struct stat newstab;
1306 if (fileparselevel >= MAX_INCLUDE_NEST) {
1307 parseerrprint("too many nested levels of included files");
1310 file= fopen(string,"r");
1312 if (errno == ENOENT) {
1313 if (didexist) *didexist= 0;
1316 parseerrprint("unable to open config file `%s': %s",string,strerror(errno));
1319 r= fstat(fileno(file),&newstab); if (r) syscallerror("unable to fstat new file");
1320 for (checkrecurse= cstate; checkrecurse; checkrecurse= checkrecurse->upstate) {
1321 if (!checkrecurse->filestab.st_mode) continue;
1322 if (newstab.st_dev==checkrecurse->filestab.st_dev &&
1323 newstab.st_ino==checkrecurse->filestab.st_ino) {
1324 parseerrprint("recursion detected - config file `%s' calls itself",string);
1330 if (didexist) *didexist= 1;
1332 ybuf= yy_create_buffer(file,YY_BUF_SIZE);
1333 if (!ybuf) syscallerror("unable to create flex buffer for file");
1334 filename= xstrsave(string);
1335 parser_push(&usestate,filename,&newstab,ybuf,0);
1340 parseerrprint("error reading configuration file `%s'",string);
1348 if (r == tokv_eof) r= 0;
1352 static int parser(int allowce) {
1354 * an exception (error, eof or quit)
1355 * then rest of `file' is uninteresting
1357 * token if allowce was !0 and equal to token's controlend
1358 * then rest of `file' (including rest of line with the
1359 * controlend - even the whitespace) not scanned yet
1363 for (;;) { /* loop over lines */
1364 cstate->reportlineno= cstate->lineno;
1365 do { token= yylex(); } while (token == tokv_lwsp);
1366 if (token & tokt_exception) {
1368 } else if (token & tokt_controlend) {
1369 if (lr_controlend == allowce) return token;
1370 else return unexpected(token,-1,"directive (not this kind of"
1371 " control structure end)");
1372 } else if (token & tokt_directive) {
1373 if ((token & tokt_internal) && !cstate->isinternal)
1374 return unexpected(token,-1,"published directive, not internal-use-only one");
1375 r= (lr_dir)(token); if (r) { assert(r & tokt_exception); return r; }
1376 } else if (token == tokv_newline) {
1377 /* ignore blank lines (and comment-only lines) */
1379 return unexpected(token,-1,"directive");