3 * $Id: parser.y,v 1.6 1998/04/23 13:26:49 mdw Exp $
5 * Parser for `become.conf' files
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of `become'
14 * `Become' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `Become' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `become'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.6 1998/04/23 13:26:49 mdw
33 * New `parse' interface to configuration file parser; informs caller
34 * whether parsing encountered any errors. Also support no-network
37 * Revision 1.5 1998/01/12 16:46:22 mdw
40 * Revision 1.4 1997/09/17 10:26:52 mdw
41 * Use rewritten class handler. Makes the expression parsers considerably
44 * Revision 1.3 1997/09/09 18:17:06 mdw
45 * Allow default port to be given as a service name or port number.
47 * Revision 1.2 1997/08/04 10:24:24 mdw
48 * Sources placed under CVS control.
50 * Revision 1.1 1997/07/21 13:47:45 mdw
55 /*----- Header files ------------------------------------------------------*/
58 /* --- ANSI headers --- */
64 /* --- Unix headers --- */
66 #include <sys/types.h>
67 #include <sys/socket.h>
69 #include <netinet/in.h>
71 #include <arpa/inet.h>
77 /* --- Local headers --- */
89 /*----- Stack type --------------------------------------------------------*/
98 /*----- Token and rule declarations ---------------------------------------*/
121 %type <c> user_class command_class host_class
122 %type <c> user_class_opt command_class_opt host_class_opt
125 /*----- Error reporting ---------------------------------------------------*/
129 #define YYERROR_VERBOSE
131 /* --- @yyprint@ --- *
133 * Arguments: @FILE *fp@ = pointer to stream to write on
134 * @int type@ = pointer to token type
135 * @YYSTYPE v@ = token value
139 * Use: Displays the semantic value of a token.
142 #define YYPRINT(fp, type, value) yyprint(fp, type, value)
144 static void yyprint(FILE *fp, int type, YYSTYPE v)
148 fprintf(fp, " %li", v.i);
152 fprintf(fp, " `%s'", v.s);
157 /* --- @yyerror@ --- *
159 * Arguments: @const char *msg@ = pointer to error message
163 * Use: Reports parse errors.
166 static void yyerror(const char *msg)
168 moan("%s at line %i", msg, lex_line);
173 /*----- The actual grammar ------------------------------------------------*/
175 /* --- Simple driver things --- */
181 statement : user_spec
190 /* --- Main statement types --- */
192 user_spec : USER name '=' user_class ';' {
199 command_spec : COMMAND name '=' command_class ';' {
206 host_spec : HOST name '=' host_class ';' {
213 port_spec : PORT STRING ';' {
215 struct servent *s = getservbyname($2, "udp");
217 moan("unknown service `%s' at line %i",
221 daemon_usePort(s->s_port);
223 yyerror("`port' command unsupported");
229 daemon_usePort(htons($2));
231 yyerror("`port' command unsupported");
237 key_spec : KEYFILE STRING ';' {
241 yyerror("`keyfile' command unsupported");
246 /* --- Parsing allow specifications --- */
248 allow_spec : ALLOW host_class_opt user_class ARROW
249 user_class_opt command_class_opt ';' {
250 rule_add($2, $3, $5, $6);
253 host_class_opt : /* empty */ { $$ = class_all; }
254 | '[' host_class ']' { $$ = $2; }
257 user_class_opt : /* empty */ { $$ = class_all; }
258 | user_class { $$ = $1; }
261 command_class_opt : /* empty */ { $$ = class_all; }
262 | ':' command_class { $$ = $2; }
265 /* --- Names get translated into symbols quickly --- */
269 name *n = name_find($1, 1, &f);
275 /*----- Various class expression types ------------------------------------*
277 * Unfortunately, all these need to handle token types slightly differently
278 * and I can't be bothered to remember the current state.
281 /* --- User class expressions --- */
283 user_class : user_class ',' user_class {
284 if (($$ = class_union($1, $3)) == 0) {
285 yyerror("type mismatch");
289 | user_class '-' user_class {
290 if (($$ = class_diff($1, $3)) == 0) {
291 yyerror("type mismatch");
295 | user_class '&' user_class {
296 if (($$ = class_isect($1, $3)) == 0) {
297 yyerror("type mismatch");
301 | user_class '|' user_class {
302 if (($$ = class_union($1, $3)) == 0) {
303 yyerror("type mismatch");
307 | INT { $$ = class_fromUser(clType_user, $1); }
310 if ((pw = userdb_userByName($1)) == 0) {
311 moan("user `%s' not known at line %i",
315 $$ = class_fromUser(clType_user, pw->pw_uid);
318 name *n = name_find($1, 0, 0);
320 moan("class `%s' not found at line %i",
323 } else if (~n->c->type & clType_user) {
324 yynerrs++; yyerror("type mismatch");
331 | '(' user_class ')' { $$ = $2; }
334 /* --- Command class expressions --- */
336 command_class : command_class ',' command_class {
337 if (($$ = class_union($1, $3)) == 0) {
338 yyerror("type mismatch");
342 | command_class '-' command_class {
343 if (($$ = class_diff($1, $3)) == 0) {
344 yyerror("type mismatch");
348 | command_class '&' command_class {
349 if (($$ = class_isect($1, $3)) == 0) {
350 yyerror("type mismatch");
354 | command_class '|' command_class {
355 if (($$ = class_union($1, $3)) == 0) {
356 yyerror("type mismatch");
360 | STRING { $$ = class_fromString(clType_command, $1); }
362 name *n = name_find($1, 0, 0);
364 moan("class `%s' not found at line %i",
367 } else if (~n->c->type & clType_command) {
368 yyerror("type mismatch");
375 | '(' command_class ')' { $$ = $2; }
378 /* --- Host class expressions --- */
380 host_class : host_class ',' host_class {
381 if (($$ = class_union($1, $3)) == 0) {
382 yyerror("type mismatch");
386 | host_class '-' host_class {
387 if (($$ = class_diff($1, $3)) == 0) {
388 yyerror("type mismatch");
392 | host_class '&' host_class {
393 if (($$ = class_isect($1, $3)) == 0) {
394 yyerror("type mismatch");
398 | host_class '|' host_class {
399 if (($$ = class_union($1, $3)) == 0) {
400 yyerror("type mismatch");
404 | STRING { $$ = class_fromString(clType_host, $1); }
406 name *n = name_find($1, 0, 0);
408 moan("class `%s' not found at line %i",
411 } else if (~n->c->type & clType_host) {
412 yyerror("type mismatch");
419 | '(' host_class ')' { $$ = $2; }
422 /*----- Helper functions --------------------------------------------------*/
428 * Returns: Zero if it worked, nonzero if it didn't.
430 * Use: Parses configuration files.
436 return (yyparse() || yynerrs);
439 /*----- That's all, folks -------------------------------------------------*/