3 * $Id: parser.y,v 1.4 1997/09/17 10:26:52 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.4 1997/09/17 10:26:52 mdw
33 * Use rewritten class handler. Makes the expression parsers considerably
36 * Revision 1.3 1997/09/09 18:17:06 mdw
37 * Allow default port to be given as a service name or port number.
39 * Revision 1.2 1997/08/04 10:24:24 mdw
40 * Sources placed under CVS control.
42 * Revision 1.1 1997/07/21 13:47:45 mdw
47 /*----- Header files ------------------------------------------------------*/
50 /* --- ANSI headers --- */
56 /* --- Unix headers --- */
58 #include <sys/types.h>
59 #include <sys/socket.h>
61 #include <netinet/in.h>
63 #include <arpa/inet.h>
69 /* --- Local headers --- */
81 /*----- Stack type --------------------------------------------------------*/
90 /*----- Token and rule declarations ---------------------------------------*/
113 %type <c> user_class command_class host_class
114 %type <c> user_class_opt command_class_opt host_class_opt
117 /*----- Error reporting ---------------------------------------------------*/
121 #define YYERROR_VERBOSE
123 /* --- @yyprint@ --- *
125 * Arguments: @FILE *fp@ = pointer to stream to write on
126 * @int type@ = pointer to token type
127 * @YYSTYPE v@ = token value
131 * Use: Displays the semantic value of a token.
134 #define YYPRINT(fp, type, value) yyprint(fp, type, value)
136 static void yyprint(FILE *fp, int type, YYSTYPE v)
140 fprintf(fp, " %li", v.i);
144 fprintf(fp, " `%s'", v.s);
149 /* --- @yyerror@ --- *
151 * Arguments: @const char *msg@ = pointer to error message
155 * Use: Reports parse errors.
158 static void yyerror(const char *msg)
160 moan("%s at line %i", msg, lex_line);
165 /*----- The actual grammar ------------------------------------------------*/
167 /* --- Simple driver things --- */
173 statement : user_spec
182 /* --- Main statement types --- */
184 user_spec : USER name '=' user_class ';' {
191 command_spec : COMMAND name '=' command_class ';' {
198 host_spec : HOST name '=' host_class ';' {
205 port_spec : PORT STRING ';' {
206 struct servent *s = getservbyname($2, "udp");
208 moan("unknown service `%s' at line %i",
212 daemon_usePort(s->s_port);
214 | PORT INT ';' { daemon_usePort(htons($2)); }
217 key_spec : KEYFILE STRING ';' { daemon_readKey($2); }
219 /* --- Parsing allow specifications --- */
221 allow_spec : ALLOW host_class_opt user_class ARROW
222 user_class_opt command_class_opt ';' {
223 rule_add($2, $3, $5, $6);
226 host_class_opt : /* empty */ { $$ = class_all; }
227 | '[' host_class ']' { $$ = $2; }
230 user_class_opt : /* empty */ { $$ = class_all; }
231 | user_class { $$ = $1; }
234 command_class_opt : /* empty */ { $$ = class_all; }
235 | ':' command_class { $$ = $2; }
238 /* --- Names get translated into symbols quickly --- */
242 name *n = name_find($1, 1, &f);
248 /*----- Various class expression types ------------------------------------*
250 * Unfortunately, all these need to handle token types slightly differently
251 * and I can't be bothered to remember the current state.
254 /* --- User class expressions --- */
256 user_class : user_class ',' user_class {
257 if (($$ = class_union($1, $3)) == 0) {
258 yyerror("type mismatch");
262 | user_class '-' user_class {
263 if (($$ = class_diff($1, $3)) == 0) {
264 yyerror("type mismatch");
268 | user_class '&' user_class {
269 if (($$ = class_isect($1, $3)) == 0) {
270 yyerror("type mismatch");
274 | user_class '|' user_class {
275 if (($$ = class_union($1, $3)) == 0) {
276 yyerror("type mismatch");
280 | INT { $$ = class_fromUser(clType_user, $1); }
283 if ((pw = userdb_userByName($1)) == 0) {
284 moan("user `%s' not known at line %i",
288 $$ = class_fromUser(clType_user, pw->pw_uid);
291 name *n = name_find($1, 0, 0);
293 moan("class `%s' not found at line %i",
296 } else if (~n->c->type & clType_user) {
297 yyerror("type mismatch");
304 | '(' user_class ')' { $$ = $2; }
307 /* --- Command class expressions --- */
309 command_class : command_class ',' command_class {
310 if (($$ = class_union($1, $3)) == 0) {
311 yyerror("type mismatch");
315 | command_class '-' command_class {
316 if (($$ = class_diff($1, $3)) == 0) {
317 yyerror("type mismatch");
321 | command_class '&' command_class {
322 if (($$ = class_isect($1, $3)) == 0) {
323 yyerror("type mismatch");
327 | command_class '|' command_class {
328 if (($$ = class_union($1, $3)) == 0) {
329 yyerror("type mismatch");
333 | STRING { $$ = class_fromString(clType_command, $1); }
335 name *n = name_find($1, 0, 0);
337 moan("class `%s' not found at line %i",
340 } else if (~n->c->type & clType_command) {
341 yyerror("type mismatch");
348 | '(' command_class ')' { $$ = $2; }
351 /* --- Host class expressions --- */
353 host_class : host_class ',' host_class {
354 if (($$ = class_union($1, $3)) == 0) {
355 yyerror("type mismatch");
359 | host_class '-' host_class {
360 if (($$ = class_diff($1, $3)) == 0) {
361 yyerror("type mismatch");
365 | host_class '&' host_class {
366 if (($$ = class_isect($1, $3)) == 0) {
367 yyerror("type mismatch");
371 | host_class '|' host_class {
372 if (($$ = class_union($1, $3)) == 0) {
373 yyerror("type mismatch");
377 | STRING { $$ = class_fromString(clType_host, $1); }
379 name *n = name_find($1, 0, 0);
381 moan("class `%s' not found at line %i",
384 } else if (~n->c->type & clType_host) {
385 yyerror("type mismatch");
392 | '(' host_class ')' { $$ = $2; }
395 /*----- That's all, folks -------------------------------------------------*/