2 * Copyright (C)1996-1997,1999 Ian Jackson
4 * This is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with userv; if not, write to the Free Software
16 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 #include <sys/types.h>
50 typedef int directive_fnt(int dtoken);
51 static directive_fnt df_reject, df_execute, df_executefrompath;
52 static directive_fnt df_executefromdirectory, df_executebuiltin;
53 static directive_fnt df_errorstostderr, df_errorstosyslog, df_errorstofile;
54 static directive_fnt dfg_fdwant, dfg_setflag;
55 static directive_fnt df_reset, df_cd, df_userrcfile, df_include;
56 static directive_fnt df_includelookup, df_includedirectory;
57 static directive_fnt df_message, df_error, df_quit, df_eof;
58 static directive_fnt df_if, df_catchquit, df_errorspush;
59 static directive_fnt dfi_includeuserrcfile, dfi_includeclientconfig;
60 /* directive functions return:
61 * 0 for success having scanned up to and including end of line but not beyond,
62 * or tokv_error or tokv_quit.
63 * They expect to parse the whitespace before their parameters (if any).
66 typedef int parmcondition_fnt(int ctoken, char *const *parmvalues, int *rtrue);
67 static parmcondition_fnt pcf_glob, pcf_range, pcf_grep;
68 /* all conditional functions return tokv_error for failure or 0 for success
69 * at parsing and testing, in which case *rtrue is set to 0 or 1.
70 * On success they have scanned up to and including the condition's
71 * terminating newline; the pcf_... functions expect to parse the whitespace
72 * between the parameter name and the condition's arguments.
73 * Otherwise they return tokv_error.
74 * The parameter-based conditionals take a list of parameter values
75 * as obtained from the parameter functions and pa_parameter,
76 * and do _not_ free it.
79 typedef int parameter_fnt(int ptoken, char ***rvalues);
80 static parameter_fnt pf_service;
81 static parameter_fnt pf_callinguser, pf_serviceuser;
82 static parameter_fnt pf_callinggroup, pf_servicegroup;
83 static parameter_fnt pf_callingusershell, pf_serviceusershell;
84 /* Parameter functions return tokv_error or 0 for success at parsing
85 * and determining the value, in which case *rvalues is made to be
86 * a mallocd null-terminated array of pointers to mallocd strings.
87 * freeparm can be used to free such an array.
90 typedef int builtinserviceparse_fnt(char ***rnewargs);
91 static builtinserviceparse_fnt bispa_none, bispa_parameter;
92 /* These parse the arguments to a builtin service, including the
93 * newline at the end of the line. *rnewargs will initially be
94 * null, indicating that no arguments are to be set; the function
95 * may store a mallocd array of mallocd strings in it,
96 * containing the arguments it wishes to have set (null-pointer
100 static int yylex(void);
101 /* Returns a token (which may be an eof or error exception) */
103 static directive_fnt *lr_dir;
104 static parmcondition_fnt *lr_parmcond;
105 static builtinserviceparse_fnt *lr_bispa;
106 static builtinserviceexec_fnt *lr_bisexec;
107 static parameter_fnt *lr_parameter;
108 static int lr_loglevel, lr_logfacility, lr_min, lr_max, *lr_flag;
109 static int lr_flagval, lr_controlend;
110 static int lr_fdwant_readwrite; /* -1=never, 0=opt, 1=always */
112 /* Forward declarations of things used in lexer and parser */
114 struct parser_state {
115 int lineno, reportlineno, notedreferer, isinternal;
116 const char *filename;
117 struct stat filestab;
118 YY_BUFFER_STATE ybuf;
119 struct parser_state *upstate;
122 static struct parser_state *cstate;
124 struct error_handling {
125 int handling; /* One of the error handling modes tokt_ehandlemode */
126 int logfacility, loglevel;
127 int filekeep; /* File is in use by higher-level errors-push, leave it open */
132 static struct error_handling eh = { tokv_word_errorstostderr, 0,0,0,0,0 };
134 static int dequote(char *inplace);
144 reject { lr_dir= df_reject; return tokv_word_reject; }
145 execute-from-directory { lr_dir= df_executefromdirectory; return tokv_word_executefromdirectory; }
146 execute-from-path { lr_dir= df_executefrompath; return tokv_word_executefrompath; }
147 execute-builtin { lr_dir= df_executebuiltin; return tokv_word_executebuiltin; }
148 errors-to-stderr { lr_dir= df_errorstostderr; return tokv_word_errorstostderr; }
149 errors-to-syslog { lr_dir= df_errorstosyslog; return tokv_word_errorstosyslog; }
150 errors-to-file { lr_dir= df_errorstofile; return tokv_word_errorstofile; }
151 require-fd { lr_dir= dfg_fdwant; lr_fdwant_readwrite=1; return tokv_word_requirefd; }
152 allow-fd { lr_dir= dfg_fdwant; lr_fdwant_readwrite=0; return tokv_word_allowfd; }
153 null-fd { lr_dir= dfg_fdwant; lr_fdwant_readwrite=0; return tokv_word_nullfd; }
154 reject-fd { lr_dir= dfg_fdwant; lr_fdwant_readwrite=-1; return tokv_word_rejectfd; }
155 ignore-fd { lr_dir= dfg_fdwant; lr_fdwant_readwrite=-1; return tokv_word_ignorefd; }
156 set-environment { lr_dir= dfg_setflag; lr_flag= &setenvironment; lr_flagval= 1; return tokv_word_setenvironment; }
157 no-set-environment { lr_dir= dfg_setflag; lr_flag= &setenvironment; lr_flagval= 0; return tokv_word_nosetenvironment; }
158 suppress-args { lr_dir= dfg_setflag; lr_flag= &suppressargs; lr_flagval= 1; return tokv_word_suppressargs; }
159 no-suppress-args { lr_dir= dfg_setflag; lr_flag= &suppressargs; lr_flagval= 0; return tokv_word_nosuppressargs; }
160 disconnect-hup { lr_dir= dfg_setflag; lr_flag= &disconnecthup; lr_flagval= 1; return tokv_word_disconnecthup; }
161 no-disconnect-hup { lr_dir= dfg_setflag; lr_flag= &disconnecthup; lr_flagval= 0; return tokv_word_nodisconnecthup; }
162 cd { lr_dir= df_cd; return tokv_word_cd; }
163 user-rcfile { lr_dir= df_userrcfile; return tokv_word_userrcfile; }
164 include { lr_dir= df_include; return tokv_word_include; }
165 include-ifexist { lr_dir= df_include; return tokv_word_includeifexist; }
166 include-lookup { lr_dir= df_includelookup; return tokv_word_includelookup; }
167 include-lookup-all { lr_dir= df_includelookup; return tokv_word_includelookupall; }
168 include-directory { lr_dir= df_includedirectory; return tokv_word_includedirectory; }
169 message { lr_dir= df_message; return tokv_word_message; }
170 _include-sysconfig { lr_dir= df_include; return tokv_word_includesysconfig; }
171 _include-user-rcfile { lr_dir= dfi_includeuserrcfile; return tokv_word_includeuserrcfile; }
172 _include-client-config { lr_dir= dfi_includeclientconfig; return tokv_word_includeclientconfig; }
173 quit { lr_dir= df_quit; return tokv_word_quit; }
174 eof { lr_dir= df_eof; return tokv_word_eof; }
175 if { lr_dir= df_if; return tokv_word_if; }
176 catch-quit { lr_dir= df_catchquit; return tokv_word_catchquit; }
177 errors-push { lr_dir= df_errorspush; return tokv_word_errorspush; }
178 elif { lr_controlend= tokv_word_if; return tokv_word_elif; }
179 else { lr_controlend= tokv_word_if; return tokv_word_else; }
180 fi { lr_controlend= tokv_word_if; return tokv_word_fi; }
181 hctac { lr_controlend= tokv_word_catchquit; return tokv_word_hctac; }
182 srorre { lr_controlend= tokv_word_errorspush; return tokv_word_srorre; }
183 glob { lr_parmcond= pcf_glob; return tokv_word_glob; }
184 range { lr_parmcond= pcf_range; return tokv_word_range; }
185 grep { lr_parmcond= pcf_grep; return tokv_word_grep; }
186 environment { lr_bispa= bispa_none; lr_bisexec= bisexec_environment; return tokv_word_environment; }
187 parameter { lr_bispa= bispa_parameter; lr_bisexec= bisexec_parameter; return tokv_word_parameter; }
188 version { lr_bispa= bispa_none; lr_bisexec= bisexec_version; return tokv_word_version; }
189 toplevel { lr_bispa= bispa_none; lr_bisexec= bisexec_toplevel; return tokv_word_toplevel; }
190 override { lr_bispa= bispa_none; lr_bisexec= bisexec_override; return tokv_word_override; }
191 reset { lr_bispa= bispa_none; lr_bisexec= bisexec_reset; lr_dir= df_reset; return tokv_word_reset; }
192 execute { lr_bispa= bispa_none; lr_bisexec= bisexec_execute; lr_dir= df_execute; return tokv_word_execute; }
193 help { lr_bispa= bispa_none; lr_bisexec= bisexec_help; return tokv_word_help; }
194 service { lr_parameter= pf_service; return tokv_word_service; }
195 calling-user { lr_parameter= pf_callinguser; return tokv_word_callinguser; }
196 calling-group { lr_parameter= pf_callinggroup; return tokv_word_callinggroup; }
197 calling-user-shell { lr_parameter= pf_callingusershell; return tokv_word_callingusershell; }
198 service-user { lr_parameter= pf_serviceuser; return tokv_word_serviceuser; }
199 service-group { lr_parameter= pf_servicegroup; return tokv_word_servicegroup; }
200 service-user-shell { lr_parameter= pf_serviceusershell; return tokv_word_serviceusershell; }
201 debug { lr_loglevel= LOG_DEBUG; return tokv_syslog_debug; }
202 info { lr_loglevel= LOG_INFO; return tokv_syslog_info; }
203 notice { lr_loglevel= LOG_NOTICE; return tokv_syslog_notice; }
204 warn(ing)? { lr_loglevel= LOG_WARNING; return tokv_syslog_warning; }
205 err { lr_loglevel= LOG_ERR; return tokv_syslog_err; }
206 crit { lr_loglevel= LOG_CRIT; return tokv_syslog_crit; }
207 alert { lr_loglevel= LOG_ALERT; return tokv_syslog_alert; }
208 emerg|panic { lr_loglevel= LOG_EMERG; return tokv_syslog_emerg; }
209 auth(priv)?|security { lr_logfacility= LOG_AUTHPRIV; return tokv_syslog_authpriv; }
210 cron { lr_logfacility= LOG_CRON; return tokv_syslog_cron; }
211 daemon { lr_logfacility= LOG_DAEMON; return tokv_syslog_daemon; }
212 kern(el)? { lr_logfacility= LOG_KERN; return tokv_syslog_kern; }
213 lpr { lr_logfacility= LOG_LPR; return tokv_syslog_lpr; }
214 mail { lr_logfacility= LOG_MAIL; return tokv_syslog_mail; }
215 news { lr_logfacility= LOG_NEWS; return tokv_syslog_news; }
216 syslog { lr_logfacility= LOG_SYSLOG; return tokv_syslog_syslog; }
217 user { lr_logfacility= LOG_USER; return tokv_syslog_user; }
218 uucp { lr_logfacility= LOG_UUCP; return tokv_syslog_uucp; }
219 local0 { lr_logfacility= LOG_LOCAL0; return tokv_syslog_local0; }
220 local1 { lr_logfacility= LOG_LOCAL1; return tokv_syslog_local1; }
221 local2 { lr_logfacility= LOG_LOCAL2; return tokv_syslog_local2; }
222 local3 { lr_logfacility= LOG_LOCAL3; return tokv_syslog_local3; }
223 local4 { lr_logfacility= LOG_LOCAL4; return tokv_syslog_local4; }
224 local5 { lr_logfacility= LOG_LOCAL5; return tokv_syslog_local5; }
225 local6 { lr_logfacility= LOG_LOCAL6; return tokv_syslog_local6; }
226 local7 { lr_logfacility= LOG_LOCAL7; return tokv_syslog_local7; }
227 read { return tokv_word_read; }
228 write { return tokv_word_write; }
229 \$ { return tokv_dollar; }
230 \( { return tokv_openparen; }
231 \) { return tokv_closeparen; }
232 \! { return tokv_not; }
233 \& { return tokv_and; }
234 \| { return tokv_or; }
235 error { lr_dir= df_error; lr_loglevel= LOG_ERR; return tokv_word_error; }
241 lr_min=lr_max= (int)strtoul(yytext,&ep,10);
245 [0-9]{1,8}-[0-9]{1,8} {
247 lr_min= (int)strtoul(yytext,&ep,10);
248 assert(*ep == HYPHEN);
250 lr_max= (int)strtoul(ep,&ep,10);
252 if (lr_max < lr_min) {
253 parseerrprint("fd range has min > max");
260 lr_min= (int)strtoul(yytext,&ep,10);
261 assert(*ep == HYPHEN);
264 return tokv_fdstoend;
266 [\ \t]+ return tokv_lwsp;
267 [\ \t]*\n cstate->lineno++; return tokv_newline;
268 [\ \t]*\#[^\n]*\n cstate->lineno++; return tokv_newline;
270 parseerrprint("missing newline at eof after comment");
273 \"([^\\\"\n]|\\[a-z]|\\[0-9]{3}|\\x[0-9A-Fa-f]{2}|\\[[:punct:]]|\\[ \t]*\n)*\" {
274 return dequote(yytext);
277 parseerrprint("misquoted or unterminated string");
280 [^\ \t\n]+ return tokv_barestring;
281 <<EOF>> return tokv_eof;
286 const char *const builtinservicehelpstrings[]= {
288 "parameter <parameter>",