3 * $Id: parser.y,v 1.8 2003/10/12 00:14:55 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.8 2003/10/12 00:14:55 mdw
33 * Major overhaul. Now uses DSA signatures rather than the bogus symmetric
34 * encrypt-and-hope thing. Integrated with mLib and Catacomb.
36 * Revision 1.7 1999/03/26 15:25:22 mdw
37 * Insert some missing semicolons. Bison didn't seem to care, but other
38 * programs like `yyextract' do, so it's worth fixing.
40 * Revision 1.6 1998/04/23 13:26:49 mdw
41 * New `parse' interface to configuration file parser; informs caller
42 * whether parsing encountered any errors. Also support no-network
45 * Revision 1.5 1998/01/12 16:46:22 mdw
48 * Revision 1.4 1997/09/17 10:26:52 mdw
49 * Use rewritten class handler. Makes the expression parsers considerably
52 * Revision 1.3 1997/09/09 18:17:06 mdw
53 * Allow default port to be given as a service name or port number.
55 * Revision 1.2 1997/08/04 10:24:24 mdw
56 * Sources placed under CVS control.
58 * Revision 1.1 1997/07/21 13:47:45 mdw
63 /*----- Header files ------------------------------------------------------*/
66 /* --- ANSI headers --- */
72 /* --- Unix headers --- */
74 #include <sys/types.h>
75 #include <sys/socket.h>
77 #include <netinet/in.h>
79 #include <arpa/inet.h>
85 /* --- mLib headers --- */
87 #include <mLib/report.h>
90 /* --- Local headers --- */
100 /*----- Stack type --------------------------------------------------------*/
109 /*----- Token and rule declarations ---------------------------------------*/
132 %type <c> user_class command_class host_class
133 %type <c> user_class_opt command_class_opt host_class_opt
136 /*----- Error reporting ---------------------------------------------------*/
140 #define YYERROR_VERBOSE
142 /* --- @yyprint@ --- *
144 * Arguments: @FILE *fp@ = pointer to stream to write on
145 * @int type@ = pointer to token type
146 * @YYSTYPE v@ = token value
150 * Use: Displays the semantic value of a token.
153 #define YYPRINT(fp, type, value) yyprint(fp, type, value)
155 static void yyprint(FILE *fp, int type, YYSTYPE v)
159 fprintf(fp, " %li", v.i);
163 fprintf(fp, " `%s'", v.s);
168 /* --- @yyerror@ --- *
170 * Arguments: @const char *msg@ = pointer to error message
174 * Use: Reports parse errors.
177 static void yyerror(const char *msg)
179 moan("%s at line %i", msg, lex_line);
184 /*----- The actual grammar ------------------------------------------------*/
186 /* --- Simple driver things --- */
192 statement : user_spec
201 /* --- Main statement types --- */
203 user_spec : USER name '=' user_class ';' {
210 command_spec : COMMAND name '=' command_class ';' {
217 host_spec : HOST name '=' host_class ';' {
224 port_spec : PORT STRING ';' {
226 struct servent *s = getservbyname($2, "udp");
228 moan("unknown service `%s' at line %i",
232 daemon_usePort(s->s_port);
234 yyerror("`port' command unsupported");
240 daemon_usePort(htons($2));
242 yyerror("`port' command unsupported");
248 key_spec : KEYFILE STRING ';' {
252 yyerror("`keyfile' command unsupported");
258 /* --- Parsing allow specifications --- */
260 allow_spec : ALLOW host_class_opt user_class ARROW
261 user_class_opt command_class_opt ';' {
262 rule_add($2, $3, $5, $6);
266 host_class_opt : /* empty */ { $$ = class_all; }
267 | '[' host_class ']' { $$ = $2; }
270 user_class_opt : /* empty */ { $$ = class_all; }
271 | user_class { $$ = $1; }
274 command_class_opt : /* empty */ { $$ = class_all; }
275 | ':' command_class { $$ = $2; }
278 /* --- Names get translated into symbols quickly --- */
282 name *n = name_find($1, 1, &f);
289 /*----- Various class expression types ------------------------------------*
291 * Unfortunately, all these need to handle token types slightly differently
292 * and I can't be bothered to remember the current state.
295 /* --- User class expressions --- */
297 user_class : user_class ',' user_class {
298 if (($$ = class_union($1, $3)) == 0) {
299 yyerror("type mismatch");
303 | user_class '-' user_class {
304 if (($$ = class_diff($1, $3)) == 0) {
305 yyerror("type mismatch");
309 | user_class '&' user_class {
310 if (($$ = class_isect($1, $3)) == 0) {
311 yyerror("type mismatch");
315 | user_class '|' user_class {
316 if (($$ = class_union($1, $3)) == 0) {
317 yyerror("type mismatch");
321 | INT { $$ = class_fromUser(clType_user, $1); }
324 if ((pw = userdb_userByName($1)) == 0) {
325 moan("user `%s' not known at line %i",
329 $$ = class_fromUser(clType_user, pw->pw_uid);
332 name *n = name_find($1, 0, 0);
334 moan("class `%s' not found at line %i",
337 } else if (~n->c->type & clType_user) {
338 yynerrs++; yyerror("type mismatch");
345 | '(' user_class ')' { $$ = $2; }
348 /* --- Command class expressions --- */
350 command_class : command_class ',' command_class {
351 if (($$ = class_union($1, $3)) == 0) {
352 yyerror("type mismatch");
356 | command_class '-' command_class {
357 if (($$ = class_diff($1, $3)) == 0) {
358 yyerror("type mismatch");
362 | command_class '&' command_class {
363 if (($$ = class_isect($1, $3)) == 0) {
364 yyerror("type mismatch");
368 | command_class '|' command_class {
369 if (($$ = class_union($1, $3)) == 0) {
370 yyerror("type mismatch");
374 | STRING { $$ = class_fromString(clType_command, $1); }
376 name *n = name_find($1, 0, 0);
378 moan("class `%s' not found at line %i",
381 } else if (~n->c->type & clType_command) {
382 yyerror("type mismatch");
389 | '(' command_class ')' { $$ = $2; }
392 /* --- Host class expressions --- */
394 host_class : host_class ',' host_class {
395 if (($$ = class_union($1, $3)) == 0) {
396 yyerror("type mismatch");
400 | host_class '-' host_class {
401 if (($$ = class_diff($1, $3)) == 0) {
402 yyerror("type mismatch");
406 | host_class '&' host_class {
407 if (($$ = class_isect($1, $3)) == 0) {
408 yyerror("type mismatch");
412 | host_class '|' host_class {
413 if (($$ = class_union($1, $3)) == 0) {
414 yyerror("type mismatch");
418 | STRING { $$ = class_fromString(clType_host, $1); }
420 name *n = name_find($1, 0, 0);
422 moan("class `%s' not found at line %i",
425 } else if (~n->c->type & clType_host) {
426 yyerror("type mismatch");
433 | '(' host_class ')' { $$ = $2; }
436 /*----- Helper functions --------------------------------------------------*/
442 * Returns: Zero if it worked, nonzero if it didn't.
444 * Use: Parses configuration files.
450 return (yyparse() || yynerrs);
453 /*----- That's all, folks -------------------------------------------------*/