/* -*-c-*- * * $Id$ * * Parser for `become.conf' files * * (c) 1998 EBI */ /*----- Licensing notice --------------------------------------------------* * * This file is part of `become' * * `Become' is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * `Become' is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with `become'; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /*----- Header files ------------------------------------------------------*/ %{ /* --- ANSI headers --- */ #include #include #include /* --- Unix headers --- */ #include #include #include #include #include #include #include /* --- mLib headers --- */ #include #include /* --- Local headers --- */ #include "class.h" #include "daemon.h" #include "lexer.h" #include "name.h" #include "rule.h" #include "userdb.h" #define YYDEBUG 1 #define YYERROR_VERBOSE %} /*----- Stack type --------------------------------------------------------*/ %union { long i; char *s; name *n; class_node *c; } /*----- Token and rule declarations ---------------------------------------*/ /* --- Tokens --- */ %token BADTOKEN %token USER %token COMMAND %token HOST %token ALLOW %token PORT %token KEYFILE %token INT %token WORD %token STRING %token ARROW %left ',' %left '-' %left '|' %left '&' /* --- Rules --- */ %type user_class command_class host_class %type user_class_opt command_class_opt host_class_opt %type name /*----- Error reporting ---------------------------------------------------*/ %{ /* --- @yyprint@ --- * * * Arguments: @FILE *fp@ = pointer to stream to write on * @int type@ = pointer to token type * @YYSTYPE v@ = token value * * Returns: --- * * Use: Displays the semantic value of a token. */ #define YYPRINT(fp, type, value) yyprint(fp, type, value) static void yyprint(FILE *fp, int type, YYSTYPE v) { switch (type) { case INT: fprintf(fp, " %li", v.i); break; case WORD: case STRING: fprintf(fp, " `%s'", v.s); break; } } /* --- @yyerror@ --- * * * Arguments: @const char *msg@ = pointer to error message * * Returns: --- * * Use: Reports parse errors. */ static void yyerror(const char *msg) { moan("%s at line %i", msg, lex_line); } %} %% /*----- The actual grammar ------------------------------------------------*/ /* --- Simple driver things --- */ file : /* empty */ | file statement ; statement : user_spec | command_spec | host_spec | allow_spec | port_spec | key_spec | error ';' ; /* --- Main statement types --- */ user_spec : USER name '=' user_class ';' { if ($2->c) class_dec($2->c); $2->c = $4; } ; command_spec : COMMAND name '=' command_class ';' { if ($2->c) class_dec($2->c); $2->c = $4; } ; host_spec : HOST name '=' host_class ';' { if ($2->c) class_dec($2->c); $2->c = $4; } ; port_spec : PORT STRING ';' { #ifndef NONETWORK struct servent *s = getservbyname($2, "udp"); if (!s) { moan("unknown service `%s' at line %i", $2, lex_line); yynerrs++; YYERROR; } daemon_usePort(s->s_port); #else yyerror("`port' command unsupported"); yynerrs++; YYERROR; #endif } | PORT INT ';' { #ifndef NONETWORK daemon_usePort(htons($2)); #else yyerror("`port' command unsupported"); yynerrs++; YYERROR; #endif } ; key_spec : KEYFILE STRING ';' { #ifndef NONETWORK daemon_readKey($2); #else yyerror("`keyfile' command unsupported"); yynerrs++; YYERROR; #endif } ; /* --- Parsing allow specifications --- */ allow_spec : ALLOW host_class_opt user_class ARROW user_class_opt command_class_opt ';' { rule_add($2, $3, $5, $6); } ; host_class_opt : /* empty */ { $$ = class_all; } | '[' host_class ']' { $$ = $2; } ; user_class_opt : /* empty */ { $$ = class_all; } | user_class { $$ = $1; } ; command_class_opt : /* empty */ { $$ = class_all; } | ':' command_class { $$ = $2; } ; /* --- Names get translated into symbols quickly --- */ name : WORD { unsigned f; name *n = name_find($1, 1, &f); if (!f) n->c = 0; $$ = n; } ; /*----- Various class expression types ------------------------------------* * * Unfortunately, all these need to handle token types slightly differently * and I can't be bothered to remember the current state. */ /* --- User class expressions --- */ user_class : user_class ',' user_class { if (($$ = class_union($1, $3)) == 0) { yyerror("type mismatch"); yynerrs++; YYERROR; } } | user_class '-' user_class { if (($$ = class_diff($1, $3)) == 0) { yyerror("type mismatch"); yynerrs++; YYERROR; } } | user_class '&' user_class { if (($$ = class_isect($1, $3)) == 0) { yyerror("type mismatch"); yynerrs++; YYERROR; } } | user_class '|' user_class { if (($$ = class_union($1, $3)) == 0) { yyerror("type mismatch"); yynerrs++; YYERROR; } } | INT { $$ = class_fromUser(clType_user, $1); } | STRING { struct passwd *pw; if ((pw = userdb_userByName($1)) == 0) { moan("user `%s' not known at line %i", $1, lex_line); yynerrs++; YYERROR; } else $$ = class_fromUser(clType_user, pw->pw_uid); } | WORD { name *n = name_find($1, 0, 0); if (!n || !n->c) { moan("class `%s' not found at line %i", $1, lex_line); yynerrs++; YYERROR; } else if (~n->c->type & clType_user) { yynerrs++; yyerror("type mismatch"); yynerrs++; YYERROR; } else { $$ = n->c; class_inc(n->c); } } | '(' user_class ')' { $$ = $2; } ; /* --- Command class expressions --- */ command_class : command_class ',' command_class { if (($$ = class_union($1, $3)) == 0) { yyerror("type mismatch"); yynerrs++; YYERROR; } } | command_class '-' command_class { if (($$ = class_diff($1, $3)) == 0) { yyerror("type mismatch"); yynerrs++; YYERROR; } } | command_class '&' command_class { if (($$ = class_isect($1, $3)) == 0) { yyerror("type mismatch"); yynerrs++; YYERROR; } } | command_class '|' command_class { if (($$ = class_union($1, $3)) == 0) { yyerror("type mismatch"); yynerrs++; YYERROR; } } | STRING { $$ = class_fromString(clType_command, $1); } | WORD { name *n = name_find($1, 0, 0); if (!n || !n->c) { moan("class `%s' not found at line %i", $1, lex_line); yynerrs++; YYERROR; } else if (~n->c->type & clType_command) { yyerror("type mismatch"); yynerrs++; YYERROR; } else { $$ = n->c; class_inc(n->c); } } | '(' command_class ')' { $$ = $2; } ; /* --- Host class expressions --- */ host_class : host_class ',' host_class { if (($$ = class_union($1, $3)) == 0) { yyerror("type mismatch"); yynerrs++; YYERROR; } } | host_class '-' host_class { if (($$ = class_diff($1, $3)) == 0) { yyerror("type mismatch"); yynerrs++; YYERROR; } } | host_class '&' host_class { if (($$ = class_isect($1, $3)) == 0) { yyerror("type mismatch"); yynerrs++; YYERROR; } } | host_class '|' host_class { if (($$ = class_union($1, $3)) == 0) { yyerror("type mismatch"); yynerrs++; YYERROR; } } | STRING { $$ = class_fromString(clType_host, $1); } | WORD { name *n = name_find($1, 0, 0); if (!n || !n->c) { moan("class `%s' not found at line %i", $1, lex_line); yynerrs++; YYERROR; } else if (~n->c->type & clType_host) { yyerror("type mismatch"); yynerrs++; YYERROR; } else { $$ = n->c; class_inc(n->c); } } | '(' host_class ')' { $$ = $2; } ; /*----- Helper functions --------------------------------------------------*/ %% /* --- @parse@ --- * * * Arguments: --- * * Returns: Zero if it worked, nonzero if it didn't. * * Use: Parses configuration files. */ int parse(void) { yynerrs = 0; return (yyparse() || yynerrs); } /*----- That's all, folks -------------------------------------------------*/