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