chiark / gitweb /
Regenerated formatted documentation (spec.ps, spec.html).
[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 "tokens.h"
47
48 #define HYPHEN '-'
49
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).
64  */
65
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.
77  */
78
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.
88  */
89
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
97  * terminated).
98  */
99
100 static int yylex(void);
101 /* Returns a token (which may be an eof or error exception) */
102
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 */
111
112 /* Forward declarations of things used in lexer and parser */
113
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;
120 };
121
122 static struct parser_state *cstate;
123
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 */
128   FILE *file;
129   char *filename;
130 };
131
132 static struct error_handling eh = { tokv_word_errorstostderr, 0,0,0,0,0 };
133
134 static int dequote(char *inplace);
135
136 #define YY_NO_UNPUT
137
138 %}
139
140 %option noyywrap
141
142 %%
143
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 shutdown { lr_bispa= bispa_none; lr_bisexec= bisexec_shutdown; return tokv_word_shutdown; }
192 reset { lr_bispa= bispa_none; lr_bisexec= bisexec_reset; lr_dir= df_reset; return tokv_word_reset; }
193 execute { lr_bispa= bispa_none; lr_bisexec= bisexec_execute; lr_dir= df_execute; return tokv_word_execute; }
194 help { lr_bispa= bispa_none; lr_bisexec= bisexec_help; return tokv_word_help; }
195 service { lr_parameter= pf_service; return tokv_word_service; }
196 calling-user { lr_parameter= pf_callinguser; return tokv_word_callinguser; }
197 calling-group { lr_parameter= pf_callinggroup; return tokv_word_callinggroup; }
198 calling-user-shell { lr_parameter= pf_callingusershell; return tokv_word_callingusershell; }
199 service-user { lr_parameter= pf_serviceuser; return tokv_word_serviceuser; }
200 service-group { lr_parameter= pf_servicegroup; return tokv_word_servicegroup; }
201 service-user-shell { lr_parameter= pf_serviceusershell; return tokv_word_serviceusershell; }
202 debug { lr_loglevel= LOG_DEBUG; return tokv_syslog_debug; }
203 info { lr_loglevel= LOG_INFO; return tokv_syslog_info; }
204 notice { lr_loglevel= LOG_NOTICE; return tokv_syslog_notice; }
205 warn(ing)? { lr_loglevel= LOG_WARNING; return tokv_syslog_warning; }
206 err { lr_loglevel= LOG_ERR; return tokv_syslog_err; }
207 crit { lr_loglevel= LOG_CRIT; return tokv_syslog_crit; }
208 alert { lr_loglevel= LOG_ALERT; return tokv_syslog_alert; }
209 emerg|panic { lr_loglevel= LOG_EMERG; return tokv_syslog_emerg; }
210 auth(priv)?|security { lr_logfacility= LOG_AUTHPRIV; return tokv_syslog_authpriv; }
211 cron { lr_logfacility= LOG_CRON; return tokv_syslog_cron; }
212 daemon { lr_logfacility= LOG_DAEMON; return tokv_syslog_daemon; }
213 kern(el)? { lr_logfacility= LOG_KERN; return tokv_syslog_kern; }
214 lpr { lr_logfacility= LOG_LPR; return tokv_syslog_lpr; }
215 mail { lr_logfacility= LOG_MAIL; return tokv_syslog_mail; }
216 news { lr_logfacility= LOG_NEWS; return tokv_syslog_news; }
217 syslog { lr_logfacility= LOG_SYSLOG; return tokv_syslog_syslog; }
218 user { lr_logfacility= LOG_USER; return tokv_syslog_user; }
219 uucp { lr_logfacility= LOG_UUCP; return tokv_syslog_uucp; }
220 local0 { lr_logfacility= LOG_LOCAL0; return tokv_syslog_local0; }
221 local1 { lr_logfacility= LOG_LOCAL1; return tokv_syslog_local1; }
222 local2 { lr_logfacility= LOG_LOCAL2; return tokv_syslog_local2; }
223 local3 { lr_logfacility= LOG_LOCAL3; return tokv_syslog_local3; }
224 local4 { lr_logfacility= LOG_LOCAL4; return tokv_syslog_local4; }
225 local5 { lr_logfacility= LOG_LOCAL5; return tokv_syslog_local5; }
226 local6 { lr_logfacility= LOG_LOCAL6; return tokv_syslog_local6; }
227 local7 { lr_logfacility= LOG_LOCAL7; return tokv_syslog_local7; }
228 read { return tokv_word_read; }
229 write { return tokv_word_write; }
230 \$ { return tokv_dollar; }
231 \( { return tokv_openparen; }
232 \) { return tokv_closeparen; }
233 \! { return tokv_not; }
234 \& { return tokv_and; }
235 \| { return tokv_or; }
236 error { lr_dir= df_error; lr_loglevel= LOG_ERR; return tokv_word_error; }
237
238
239
240 [0-9]{1,8}              {
241                           char *ep;
242                           lr_min=lr_max= (int)strtoul(yytext,&ep,10);
243                           assert(!*ep);
244                           return tokv_ordinal;
245                         }
246 [0-9]{1,8}-[0-9]{1,8}   {
247                           char *ep;
248                           lr_min= (int)strtoul(yytext,&ep,10);
249                           assert(*ep == HYPHEN);
250                           assert(*++ep);
251                           lr_max= (int)strtoul(ep,&ep,10);
252                           assert(!*ep);
253                           if (lr_max < lr_min) {
254                             parseerrprint("fd range has min > max");
255                             return tokv_error;
256                           }
257                           return tokv_fdrange;
258                         }
259 [0-9]{1,8}-             {
260                           char *ep;
261                           lr_min= (int)strtoul(yytext,&ep,10);
262                           assert(*ep == HYPHEN);
263                           assert(!*++ep);
264                           lr_max=-1;
265                           return tokv_fdstoend;
266                         }
267 [\ \t]+                 return tokv_lwsp;
268 [\ \t]*\n               cstate->lineno++; return tokv_newline;
269 [\ \t]*\#[^\n]*\n       cstate->lineno++; return tokv_newline;
270 [\ \t]*\#[^\n]*         {
271                           parseerrprint("missing newline at eof after comment");
272                           return tokv_error;
273                         }
274 \"([^\\\"\n]|\\[a-z]|\\[0-9]{3}|\\x[0-9A-Fa-f]{2}|\\[[:punct:]]|\\[ \t]*\n)*\" {
275                           return dequote(yytext);
276                         }
277 \".*                    {
278                           parseerrprint("misquoted or unterminated string");
279                           return tokv_error;
280                         }
281 [^\ \t\n]+              return tokv_barestring;
282 <<EOF>>                 return tokv_eof;
283
284
285 %%
286
287 const char *const builtinservicehelpstrings[]= {
288   "environment",
289   "parameter <parameter>",
290   "version",
291   "toplevel",
292   "override",
293   "shutdown",
294   "reset",
295   "execute",
296   "help",
297    0
298 };
299
300 #include "parser.c"
301