chiark / gitweb /
Declare fast forward from 1.1.1
[userv.git] / lexer.l
1 /* userv is
2  * Copyright 1996-2017 Ian Jackson <ian@davenant.greenend.org.uk>.
3  * Copyright 2000      Ben Harris <bjh21@cam.ac.uk>
4  * Copyright 2016-2017 Peter Benie <pjb1008@cam.ac.uk>
5  *
6  * This is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with userv; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 %{
21
22
23
24
25
26 #include <syslog.h>
27 #include <assert.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <ctype.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <pwd.h>
34 #include <grp.h>
35 #include <fnmatch.h>
36 #include <limits.h>
37 #include <dirent.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <time.h>
41 #include <errno.h>
42
43 #include "config.h"
44 #include "common.h"
45 #include "daemon.h"
46 #include "lib.h"
47 #include "both.h"
48 #include "tokens.h"
49
50 #define HYPHEN '-'
51
52 typedef int directive_fnt(int dtoken);
53 static directive_fnt df_reject, df_execute, df_executefrompath;
54 static directive_fnt df_executefromdirectory, df_executebuiltin;
55 static directive_fnt df_errorstostderr, df_errorstosyslog, df_errorstofile;
56 static directive_fnt dfg_fdwant, dfg_setflag, dfg_lookupquotemode;
57 static directive_fnt df_reset, df_cd, df_userrcfile, df_include;
58 static directive_fnt df_includelookup, df_includedirectory;
59 static directive_fnt df_message, df_error, df_quit, df_eof;
60 static directive_fnt df_if, df_catchquit, df_errorspush;
61 static directive_fnt dfi_includeuserrcfile, dfi_includeclientconfig;
62 /* directive functions return:
63  *  0 for success having scanned up to and including end of line but not beyond,
64  *  or tokv_error or tokv_quit.
65  * They expect to parse the whitespace before their parameters (if any).
66  */
67
68 typedef int parmcondition_fnt(int ctoken, char *const *parmvalues, int *rtrue);
69 static parmcondition_fnt pcf_glob, pcf_range, pcf_grep;
70 /* all conditional functions return tokv_error for failure or 0 for success
71  *  at parsing and testing, in which case *rtrue is set to 0 or 1.
72  *  On success they have scanned up to and including the condition's
73  *  terminating newline; the pcf_... functions expect to parse the whitespace
74  *  between the parameter name and the condition's arguments.
75  * Otherwise they return tokv_error.
76  * The parameter-based conditionals take a list of parameter values
77  * as obtained from the parameter functions and pa_parameter,
78  * and do _not_ free it.
79  */
80
81 typedef int parameter_fnt(int ptoken, char ***rvalues);
82 static parameter_fnt pf_service;
83 static parameter_fnt pf_callinguser, pf_serviceuser;
84 static parameter_fnt pf_callinggroup, pf_servicegroup;
85 static parameter_fnt pf_callingusershell, pf_serviceusershell;
86 /* Parameter functions return tokv_error or 0 for success at parsing
87  * and determining the value, in which case *rvalues is made to be
88  * a mallocd null-terminated array of pointers to mallocd strings.
89  * freeparm can be used to free such an array.
90  */
91
92 typedef int builtinserviceparse_fnt(char ***rnewargs);
93 static builtinserviceparse_fnt bispa_none, bispa_parameter;
94 /* These parse the arguments to a builtin service, including the
95  * newline at the end of the line.  *rnewargs will initially be
96  * null, indicating that no arguments are to be set; the function
97  * may store a mallocd array of mallocd strings in it,
98  * containing the arguments it wishes to have set (null-pointer
99  * terminated).
100  */
101
102 static int yylex(void);
103 /* Returns a token (which may be an eof or error exception) */
104
105 static directive_fnt *lr_dir;
106 static parmcondition_fnt *lr_parmcond;
107 static builtinserviceparse_fnt *lr_bispa;
108 static builtinserviceexec_fnt *lr_bisexec;
109 static parameter_fnt *lr_parameter;
110 static int lr_loglevel, lr_logfacility, lr_min, lr_max, *lr_flag;
111 static int lr_flagval, lr_controlend;
112 static int lr_fdwant_readwrite; /* -1=never, 0=opt, 1=always */
113
114 /* Forward declarations of things used in lexer and parser */
115
116 struct parser_state {
117   int lineno, reportlineno, notedreferer, isinternal;
118   const char *filename;
119   struct stat filestab;
120   YY_BUFFER_STATE ybuf;
121   struct parser_state *upstate;
122 };
123
124 static struct parser_state *cstate;
125
126 struct error_handling {
127   int handling; /* One of the error handling modes tokt_ehandlemode */
128   int logfacility, loglevel;
129   int filekeep; /* File is in use by higher-level errors-push, leave it open */
130   FILE *file;
131   char *filename;
132 };
133
134 static struct error_handling eh = { tokv_word_errorstostderr, 0,0,0,0,0 };
135
136 static int dequote(char *inplace);
137 static void countnewlines(void);
138
139 #define YY_NO_INPUT
140
141 %}
142
143 %option noyywrap
144 %option nounput
145
146 %%
147
148 reject { lr_dir= df_reject; return tokv_word_reject; }
149 execute-from-directory { lr_dir= df_executefromdirectory; return tokv_word_executefromdirectory; }
150 execute-from-path { lr_dir= df_executefrompath; return tokv_word_executefrompath; }
151 execute-builtin { lr_dir= df_executebuiltin; return tokv_word_executebuiltin; }
152 errors-to-stderr { lr_dir= df_errorstostderr; return tokv_word_errorstostderr; }
153 errors-to-syslog { lr_dir= df_errorstosyslog; return tokv_word_errorstosyslog; }
154 errors-to-file { lr_dir= df_errorstofile; return tokv_word_errorstofile; }
155 include-lookup-quote-old { lr_dir= dfg_lookupquotemode; return tokv_word_includelookupquoteold; }
156 include-lookup-quote-new { lr_dir= dfg_lookupquotemode; return tokv_word_includelookupquotenew; }
157 require-fd { lr_dir= dfg_fdwant; lr_fdwant_readwrite=1; return tokv_word_requirefd; }
158 allow-fd { lr_dir= dfg_fdwant; lr_fdwant_readwrite=0; return tokv_word_allowfd; }
159 null-fd { lr_dir= dfg_fdwant; lr_fdwant_readwrite=0; return tokv_word_nullfd; }
160 reject-fd { lr_dir= dfg_fdwant; lr_fdwant_readwrite=-1; return tokv_word_rejectfd; }
161 ignore-fd { lr_dir= dfg_fdwant; lr_fdwant_readwrite=-1; return tokv_word_ignorefd; }
162 set-environment { lr_dir= dfg_setflag; lr_flag= &setenvironment; lr_flagval= 1; return tokv_word_setenvironment; }
163 no-set-environment { lr_dir= dfg_setflag; lr_flag= &setenvironment; lr_flagval= 0; return tokv_word_nosetenvironment; }
164 suppress-args { lr_dir= dfg_setflag; lr_flag= &suppressargs; lr_flagval= 1; return tokv_word_suppressargs; }
165 no-suppress-args { lr_dir= dfg_setflag; lr_flag= &suppressargs; lr_flagval= 0; return tokv_word_nosuppressargs; }
166 disconnect-hup { lr_dir= dfg_setflag; lr_flag= &disconnecthup; lr_flagval= 1; return tokv_word_disconnecthup; }
167 no-disconnect-hup { lr_dir= dfg_setflag; lr_flag= &disconnecthup; lr_flagval= 0; return tokv_word_nodisconnecthup; }
168 cd { lr_dir= df_cd; return tokv_word_cd; }
169 user-rcfile { lr_dir= df_userrcfile; return tokv_word_userrcfile; }
170 include { lr_dir= df_include; return tokv_word_include; }
171 include-ifexist { lr_dir= df_include; return tokv_word_includeifexist; }
172 include-lookup { lr_dir= df_includelookup; return tokv_word_includelookup; }
173 include-lookup-all { lr_dir= df_includelookup; return tokv_word_includelookupall; }
174 include-directory { lr_dir= df_includedirectory; return tokv_word_includedirectory; }
175 message { lr_dir= df_message; return tokv_word_message; }
176 _include-sysconfig { lr_dir= df_include; return tokv_word_includesysconfig; }
177 _include-user-rcfile { lr_dir= dfi_includeuserrcfile; return tokv_word_includeuserrcfile; }
178 _include-client-config { lr_dir= dfi_includeclientconfig; return tokv_word_includeclientconfig; }
179 quit { lr_dir= df_quit; return tokv_word_quit; }
180 eof { lr_dir= df_eof; return tokv_word_eof; }
181 if { lr_dir= df_if; return tokv_word_if; }
182 catch-quit { lr_dir= df_catchquit; return tokv_word_catchquit; }
183 errors-push { lr_dir= df_errorspush; return tokv_word_errorspush; }
184 elif { lr_controlend= tokv_word_if; return tokv_word_elif; }
185 else { lr_controlend= tokv_word_if; return tokv_word_else; }
186 fi { lr_controlend= tokv_word_if; return tokv_word_fi; }
187 hctac { lr_controlend= tokv_word_catchquit; return tokv_word_hctac; }
188 srorre { lr_controlend= tokv_word_errorspush; return tokv_word_srorre; }
189 glob { lr_parmcond= pcf_glob; return tokv_word_glob; }
190 range { lr_parmcond= pcf_range; return tokv_word_range; }
191 grep { lr_parmcond= pcf_grep; return tokv_word_grep; }
192 environment { lr_bispa= bispa_none; lr_bisexec= bisexec_environment; return tokv_word_environment; }
193 parameter { lr_bispa= bispa_parameter; lr_bisexec= bisexec_parameter; return tokv_word_parameter; }
194 version { lr_bispa= bispa_none; lr_bisexec= bisexec_version; return tokv_word_version; }
195 toplevel { lr_bispa= bispa_none; lr_bisexec= bisexec_toplevel; return tokv_word_toplevel; }
196 override { lr_bispa= bispa_none; lr_bisexec= bisexec_override; return tokv_word_override; }
197 shutdown { lr_bispa= bispa_none; lr_bisexec= bisexec_shutdown; return tokv_word_shutdown; }
198 reset { lr_bispa= bispa_none; lr_bisexec= bisexec_reset; lr_dir= df_reset; return tokv_word_reset; }
199 execute { lr_bispa= bispa_none; lr_bisexec= bisexec_execute; lr_dir= df_execute; return tokv_word_execute; }
200 help { lr_bispa= bispa_none; lr_bisexec= bisexec_help; return tokv_word_help; }
201 service { lr_parameter= pf_service; return tokv_word_service; }
202 calling-user { lr_parameter= pf_callinguser; return tokv_word_callinguser; }
203 calling-group { lr_parameter= pf_callinggroup; return tokv_word_callinggroup; }
204 calling-user-shell { lr_parameter= pf_callingusershell; return tokv_word_callingusershell; }
205 service-user { lr_parameter= pf_serviceuser; return tokv_word_serviceuser; }
206 service-group { lr_parameter= pf_servicegroup; return tokv_word_servicegroup; }
207 service-user-shell { lr_parameter= pf_serviceusershell; return tokv_word_serviceusershell; }
208 debug { lr_loglevel= LOG_DEBUG; return tokv_syslog_debug; }
209 info { lr_loglevel= LOG_INFO; return tokv_syslog_info; }
210 notice { lr_loglevel= LOG_NOTICE; return tokv_syslog_notice; }
211 warn(ing)? { lr_loglevel= LOG_WARNING; return tokv_syslog_warning; }
212 err { lr_loglevel= LOG_ERR; return tokv_syslog_err; }
213 crit { lr_loglevel= LOG_CRIT; return tokv_syslog_crit; }
214 alert { lr_loglevel= LOG_ALERT; return tokv_syslog_alert; }
215 emerg|panic { lr_loglevel= LOG_EMERG; return tokv_syslog_emerg; }
216 auth(priv)?|security { lr_logfacility= LOG_AUTHPRIV; return tokv_syslog_authpriv; }
217 cron { lr_logfacility= LOG_CRON; return tokv_syslog_cron; }
218 daemon { lr_logfacility= LOG_DAEMON; return tokv_syslog_daemon; }
219 kern(el)? { lr_logfacility= LOG_KERN; return tokv_syslog_kern; }
220 lpr { lr_logfacility= LOG_LPR; return tokv_syslog_lpr; }
221 mail { lr_logfacility= LOG_MAIL; return tokv_syslog_mail; }
222 news { lr_logfacility= LOG_NEWS; return tokv_syslog_news; }
223 syslog { lr_logfacility= LOG_SYSLOG; return tokv_syslog_syslog; }
224 user { lr_logfacility= LOG_USER; return tokv_syslog_user; }
225 uucp { lr_logfacility= LOG_UUCP; return tokv_syslog_uucp; }
226 local0 { lr_logfacility= LOG_LOCAL0; return tokv_syslog_local0; }
227 local1 { lr_logfacility= LOG_LOCAL1; return tokv_syslog_local1; }
228 local2 { lr_logfacility= LOG_LOCAL2; return tokv_syslog_local2; }
229 local3 { lr_logfacility= LOG_LOCAL3; return tokv_syslog_local3; }
230 local4 { lr_logfacility= LOG_LOCAL4; return tokv_syslog_local4; }
231 local5 { lr_logfacility= LOG_LOCAL5; return tokv_syslog_local5; }
232 local6 { lr_logfacility= LOG_LOCAL6; return tokv_syslog_local6; }
233 local7 { lr_logfacility= LOG_LOCAL7; return tokv_syslog_local7; }
234 read { return tokv_word_read; }
235 write { return tokv_word_write; }
236 \$ { return tokv_dollar; }
237 stdin {  lr_max= lr_min= 0; return tokv_word_stdin; }
238 stdout {  lr_max= lr_min= 1; return tokv_word_stdout; }
239 stderr {  lr_max= lr_min= 2; return tokv_word_stderr; }
240 \( { return tokv_openparen; }
241 \) { return tokv_closeparen; }
242 \! { return tokv_not; }
243 \& { return tokv_and; }
244 \| { return tokv_or; }
245 error { lr_dir= df_error; lr_loglevel= LOG_ERR; return tokv_word_error; }
246
247
248
249 [0-9]{1,8}              {
250                           char *ep;
251                           lr_min=lr_max= (int)strtoul(yytext,&ep,10);
252                           assert(!*ep);
253                           return tokv_ordinal;
254                         }
255 [0-9]{1,8}-[0-9]{1,8}   {
256                           char *ep;
257                           lr_min= (int)strtoul(yytext,&ep,10);
258                           assert(*ep == HYPHEN);
259                           ep++;  assert(*ep);
260                           lr_max= (int)strtoul(ep,&ep,10);
261                           assert(!*ep);
262                           if (lr_max < lr_min)
263                             return parseerrprint("fd range has min > max");
264                           return tokv_fdrange;
265                         }
266 [0-9]{1,8}-             {
267                           char *ep;
268                           lr_min= (int)strtoul(yytext,&ep,10);
269                           assert(*ep == HYPHEN);
270                           ep++;  assert(!*ep);
271                           lr_max=-1;
272                           return tokv_fdstoend;
273                         }
274 ([\ \t]*\\[\ \t]*\n[\ \t]*)+    countnewlines(); return tokv_lwsp;
275 [\ \t]+                         return tokv_lwsp;
276 [\ \t]*\n               cstate->lineno++; return tokv_newline;
277 [\ \t]*\#[^\n]*\n       cstate->lineno++; return tokv_newline;
278 [\ \t]*\#[^\n]*         return parseerrprint("missing newline at eof after comment");
279 \"([^\\\"\n]|\\[a-z]|\\[0-9]{3}|\\x[0-9A-Fa-f]{2}|\\[[:punct:]]|\\[ \t]*\n)*\" {
280                           countnewlines();
281                           return dequote(yytext);
282                         }
283 [^\ \t\n\\\"]+          return tokv_barestring;
284 <<EOF>>                 return tokv_eof;
285 \"                      return parseerrprint("misquoted or unterminated string");
286 \\                      return parseerrprint("unexpected backslash");
287 .                       abort(); /* expect lex warning "rule cannot be matched" */
288
289
290 %%
291
292 const char *const builtinservicehelpstrings[]= {
293   "environment",
294   "parameter <parameter>",
295   "version",
296   "toplevel",
297   "override",
298   "shutdown",
299   "reset",
300   "execute",
301   "help",
302    0
303 };
304
305 #include "parser.c"
306