chiark / gitweb /
@@ -7,7 +7,7 @@
[userv.git] / lexer.l
1 /*
2  *   Copyright (C)1996-1997,1999 Ian Jackson
3  *  
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.
8  *  
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.
13  *  
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.
17  */
18
19 %{
20
21
22
23
24
25 #include <syslog.h>
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <pwd.h>
33 #include <grp.h>
34 #include <fnmatch.h>
35 #include <limits.h>
36 #include <dirent.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <time.h>
40 #include <errno.h>
41
42 #include "config.h"
43 #include "common.h"
44 #include "daemon.h"
45 #include "lib.h"
46 #include "both.h"
47 #include "tokens.h"
48
49 #define HYPHEN '-'
50
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).
65  */
66
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.
78  */
79
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.
89  */
90
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
98  * terminated).
99  */
100
101 static int yylex(void);
102 /* Returns a token (which may be an eof or error exception) */
103
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 */
112
113 /* Forward declarations of things used in lexer and parser */
114
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;
121 };
122
123 static struct parser_state *cstate;
124
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 */
129   FILE *file;
130   char *filename;
131 };
132
133 static struct error_handling eh = { tokv_word_errorstostderr, 0,0,0,0,0 };
134
135 static int dequote(char *inplace);
136
137 #define YY_NO_UNPUT
138
139 %}
140
141 %option noyywrap
142
143 %%
144
145 reject { lr_dir= df_reject; return tokv_word_reject; }
146 execute-from-directory { lr_dir= df_executefromdirectory; return tokv_word_executefromdirectory; }
147 execute-from-path { lr_dir= df_executefrompath; return tokv_word_executefrompath; }
148 execute-builtin { lr_dir= df_executebuiltin; return tokv_word_executebuiltin; }
149 errors-to-stderr { lr_dir= df_errorstostderr; return tokv_word_errorstostderr; }
150 errors-to-syslog { lr_dir= df_errorstosyslog; return tokv_word_errorstosyslog; }
151 errors-to-file { lr_dir= df_errorstofile; return tokv_word_errorstofile; }
152 require-fd { lr_dir= dfg_fdwant; lr_fdwant_readwrite=1; return tokv_word_requirefd; }
153 allow-fd { lr_dir= dfg_fdwant; lr_fdwant_readwrite=0; return tokv_word_allowfd; }
154 null-fd { lr_dir= dfg_fdwant; lr_fdwant_readwrite=0; return tokv_word_nullfd; }
155 reject-fd { lr_dir= dfg_fdwant; lr_fdwant_readwrite=-1; return tokv_word_rejectfd; }
156 ignore-fd { lr_dir= dfg_fdwant; lr_fdwant_readwrite=-1; return tokv_word_ignorefd; }
157 set-environment { lr_dir= dfg_setflag; lr_flag= &setenvironment; lr_flagval= 1; return tokv_word_setenvironment; }
158 no-set-environment { lr_dir= dfg_setflag; lr_flag= &setenvironment; lr_flagval= 0; return tokv_word_nosetenvironment; }
159 suppress-args { lr_dir= dfg_setflag; lr_flag= &suppressargs; lr_flagval= 1; return tokv_word_suppressargs; }
160 no-suppress-args { lr_dir= dfg_setflag; lr_flag= &suppressargs; lr_flagval= 0; return tokv_word_nosuppressargs; }
161 disconnect-hup { lr_dir= dfg_setflag; lr_flag= &disconnecthup; lr_flagval= 1; return tokv_word_disconnecthup; }
162 no-disconnect-hup { lr_dir= dfg_setflag; lr_flag= &disconnecthup; lr_flagval= 0; return tokv_word_nodisconnecthup; }
163 cd { lr_dir= df_cd; return tokv_word_cd; }
164 user-rcfile { lr_dir= df_userrcfile; return tokv_word_userrcfile; }
165 include { lr_dir= df_include; return tokv_word_include; }
166 include-ifexist { lr_dir= df_include; return tokv_word_includeifexist; }
167 include-lookup { lr_dir= df_includelookup; return tokv_word_includelookup; }
168 include-lookup-all { lr_dir= df_includelookup; return tokv_word_includelookupall; }
169 include-directory { lr_dir= df_includedirectory; return tokv_word_includedirectory; }
170 message { lr_dir= df_message; return tokv_word_message; }
171 _include-sysconfig { lr_dir= df_include; return tokv_word_includesysconfig; }
172 _include-user-rcfile { lr_dir= dfi_includeuserrcfile; return tokv_word_includeuserrcfile; }
173 _include-client-config { lr_dir= dfi_includeclientconfig; return tokv_word_includeclientconfig; }
174 quit { lr_dir= df_quit; return tokv_word_quit; }
175 eof { lr_dir= df_eof; return tokv_word_eof; }
176 if { lr_dir= df_if; return tokv_word_if; }
177 catch-quit { lr_dir= df_catchquit; return tokv_word_catchquit; }
178 errors-push { lr_dir= df_errorspush; return tokv_word_errorspush; }
179 elif { lr_controlend= tokv_word_if; return tokv_word_elif; }
180 else { lr_controlend= tokv_word_if; return tokv_word_else; }
181 fi { lr_controlend= tokv_word_if; return tokv_word_fi; }
182 hctac { lr_controlend= tokv_word_catchquit; return tokv_word_hctac; }
183 srorre { lr_controlend= tokv_word_errorspush; return tokv_word_srorre; }
184 glob { lr_parmcond= pcf_glob; return tokv_word_glob; }
185 range { lr_parmcond= pcf_range; return tokv_word_range; }
186 grep { lr_parmcond= pcf_grep; return tokv_word_grep; }
187 environment { lr_bispa= bispa_none; lr_bisexec= bisexec_environment; return tokv_word_environment; }
188 parameter { lr_bispa= bispa_parameter; lr_bisexec= bisexec_parameter; return tokv_word_parameter; }
189 version { lr_bispa= bispa_none; lr_bisexec= bisexec_version; return tokv_word_version; }
190 toplevel { lr_bispa= bispa_none; lr_bisexec= bisexec_toplevel; return tokv_word_toplevel; }
191 override { lr_bispa= bispa_none; lr_bisexec= bisexec_override; return tokv_word_override; }
192 shutdown { lr_bispa= bispa_none; lr_bisexec= bisexec_shutdown; return tokv_word_shutdown; }
193 reset { lr_bispa= bispa_none; lr_bisexec= bisexec_reset; lr_dir= df_reset; return tokv_word_reset; }
194 execute { lr_bispa= bispa_none; lr_bisexec= bisexec_execute; lr_dir= df_execute; return tokv_word_execute; }
195 help { lr_bispa= bispa_none; lr_bisexec= bisexec_help; return tokv_word_help; }
196 service { lr_parameter= pf_service; return tokv_word_service; }
197 calling-user { lr_parameter= pf_callinguser; return tokv_word_callinguser; }
198 calling-group { lr_parameter= pf_callinggroup; return tokv_word_callinggroup; }
199 calling-user-shell { lr_parameter= pf_callingusershell; return tokv_word_callingusershell; }
200 service-user { lr_parameter= pf_serviceuser; return tokv_word_serviceuser; }
201 service-group { lr_parameter= pf_servicegroup; return tokv_word_servicegroup; }
202 service-user-shell { lr_parameter= pf_serviceusershell; return tokv_word_serviceusershell; }
203 debug { lr_loglevel= LOG_DEBUG; return tokv_syslog_debug; }
204 info { lr_loglevel= LOG_INFO; return tokv_syslog_info; }
205 notice { lr_loglevel= LOG_NOTICE; return tokv_syslog_notice; }
206 warn(ing)? { lr_loglevel= LOG_WARNING; return tokv_syslog_warning; }
207 err { lr_loglevel= LOG_ERR; return tokv_syslog_err; }
208 crit { lr_loglevel= LOG_CRIT; return tokv_syslog_crit; }
209 alert { lr_loglevel= LOG_ALERT; return tokv_syslog_alert; }
210 emerg|panic { lr_loglevel= LOG_EMERG; return tokv_syslog_emerg; }
211 auth(priv)?|security { lr_logfacility= LOG_AUTHPRIV; return tokv_syslog_authpriv; }
212 cron { lr_logfacility= LOG_CRON; return tokv_syslog_cron; }
213 daemon { lr_logfacility= LOG_DAEMON; return tokv_syslog_daemon; }
214 kern(el)? { lr_logfacility= LOG_KERN; return tokv_syslog_kern; }
215 lpr { lr_logfacility= LOG_LPR; return tokv_syslog_lpr; }
216 mail { lr_logfacility= LOG_MAIL; return tokv_syslog_mail; }
217 news { lr_logfacility= LOG_NEWS; return tokv_syslog_news; }
218 syslog { lr_logfacility= LOG_SYSLOG; return tokv_syslog_syslog; }
219 user { lr_logfacility= LOG_USER; return tokv_syslog_user; }
220 uucp { lr_logfacility= LOG_UUCP; return tokv_syslog_uucp; }
221 local0 { lr_logfacility= LOG_LOCAL0; return tokv_syslog_local0; }
222 local1 { lr_logfacility= LOG_LOCAL1; return tokv_syslog_local1; }
223 local2 { lr_logfacility= LOG_LOCAL2; return tokv_syslog_local2; }
224 local3 { lr_logfacility= LOG_LOCAL3; return tokv_syslog_local3; }
225 local4 { lr_logfacility= LOG_LOCAL4; return tokv_syslog_local4; }
226 local5 { lr_logfacility= LOG_LOCAL5; return tokv_syslog_local5; }
227 local6 { lr_logfacility= LOG_LOCAL6; return tokv_syslog_local6; }
228 local7 { lr_logfacility= LOG_LOCAL7; return tokv_syslog_local7; }
229 read { return tokv_word_read; }
230 write { return tokv_word_write; }
231 \$ { return tokv_dollar; }
232 \( { return tokv_openparen; }
233 \) { return tokv_closeparen; }
234 \! { return tokv_not; }
235 \& { return tokv_and; }
236 \| { return tokv_or; }
237 error { lr_dir= df_error; lr_loglevel= LOG_ERR; return tokv_word_error; }
238
239
240
241 [0-9]{1,8}              {
242                           char *ep;
243                           lr_min=lr_max= (int)strtoul(yytext,&ep,10);
244                           assert(!*ep);
245                           return tokv_ordinal;
246                         }
247 [0-9]{1,8}-[0-9]{1,8}   {
248                           char *ep;
249                           lr_min= (int)strtoul(yytext,&ep,10);
250                           assert(*ep == HYPHEN);
251                           assert(*++ep);
252                           lr_max= (int)strtoul(ep,&ep,10);
253                           assert(!*ep);
254                           if (lr_max < lr_min) {
255                             parseerrprint("fd range has min > max");
256                             return tokv_error;
257                           }
258                           return tokv_fdrange;
259                         }
260 [0-9]{1,8}-             {
261                           char *ep;
262                           lr_min= (int)strtoul(yytext,&ep,10);
263                           assert(*ep == HYPHEN);
264                           assert(!*++ep);
265                           lr_max=-1;
266                           return tokv_fdstoend;
267                         }
268 ([\ \t]*\\[\ \t]*\n[\ \t]*)+    return tokv_lwsp;
269 [\ \t]+                         return tokv_lwsp;
270 [\ \t]*\n               cstate->lineno++; return tokv_newline;
271 [\ \t]*\#[^\n]*\n       cstate->lineno++; return tokv_newline;
272 [\ \t]*\#[^\n]*         {
273                           parseerrprint("missing newline at eof after comment");
274                           return tokv_error;
275                         }
276 \"([^\\\"\n]|\\[a-z]|\\[0-9]{3}|\\x[0-9A-Fa-f]{2}|\\[[:punct:]]|\\[ \t]*\n)*\" {
277                           return dequote(yytext);
278                         }
279 \".*                    {
280                           parseerrprint("misquoted or unterminated string");
281                           return tokv_error;
282                         }
283 [^\ \t\n\\]+            return tokv_barestring;
284 <<EOF>>                 return tokv_eof;
285
286
287 %%
288
289 const char *const builtinservicehelpstrings[]= {
290   "environment",
291   "parameter <parameter>",
292   "version",
293   "toplevel",
294   "override",
295   "shutdown",
296   "reset",
297   "execute",
298   "help",
299    0
300 };
301
302 #include "parser.c"
303