3 * $Id: parser.y,v 1.5 1998/01/12 16:46:22 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.5 1998/01/12 16:46:22 mdw
35 * Revision 1.4 1997/09/17 10:26:52 mdw
36 * Use rewritten class handler. Makes the expression parsers considerably
39 * Revision 1.3 1997/09/09 18:17:06 mdw
40 * Allow default port to be given as a service name or port number.
42 * Revision 1.2 1997/08/04 10:24:24 mdw
43 * Sources placed under CVS control.
45 * Revision 1.1 1997/07/21 13:47:45 mdw
50 /*----- Header files ------------------------------------------------------*/
53 /* --- ANSI headers --- */
59 /* --- Unix headers --- */
61 #include <sys/types.h>
62 #include <sys/socket.h>
64 #include <netinet/in.h>
66 #include <arpa/inet.h>
72 /* --- Local headers --- */
84 /*----- Stack type --------------------------------------------------------*/
93 /*----- Token and rule declarations ---------------------------------------*/
116 %type <c> user_class command_class host_class
117 %type <c> user_class_opt command_class_opt host_class_opt
120 /*----- Error reporting ---------------------------------------------------*/
124 #define YYERROR_VERBOSE
126 /* --- @yyprint@ --- *
128 * Arguments: @FILE *fp@ = pointer to stream to write on
129 * @int type@ = pointer to token type
130 * @YYSTYPE v@ = token value
134 * Use: Displays the semantic value of a token.
137 #define YYPRINT(fp, type, value) yyprint(fp, type, value)
139 static void yyprint(FILE *fp, int type, YYSTYPE v)
143 fprintf(fp, " %li", v.i);
147 fprintf(fp, " `%s'", v.s);
152 /* --- @yyerror@ --- *
154 * Arguments: @const char *msg@ = pointer to error message
158 * Use: Reports parse errors.
161 static void yyerror(const char *msg)
163 moan("%s at line %i", msg, lex_line);
168 /*----- The actual grammar ------------------------------------------------*/
170 /* --- Simple driver things --- */
176 statement : user_spec
185 /* --- Main statement types --- */
187 user_spec : USER name '=' user_class ';' {
194 command_spec : COMMAND name '=' command_class ';' {
201 host_spec : HOST name '=' host_class ';' {
208 port_spec : PORT STRING ';' {
209 struct servent *s = getservbyname($2, "udp");
211 moan("unknown service `%s' at line %i",
215 daemon_usePort(s->s_port);
217 | PORT INT ';' { daemon_usePort(htons($2)); }
220 key_spec : KEYFILE STRING ';' { daemon_readKey($2); }
222 /* --- Parsing allow specifications --- */
224 allow_spec : ALLOW host_class_opt user_class ARROW
225 user_class_opt command_class_opt ';' {
226 rule_add($2, $3, $5, $6);
229 host_class_opt : /* empty */ { $$ = class_all; }
230 | '[' host_class ']' { $$ = $2; }
233 user_class_opt : /* empty */ { $$ = class_all; }
234 | user_class { $$ = $1; }
237 command_class_opt : /* empty */ { $$ = class_all; }
238 | ':' command_class { $$ = $2; }
241 /* --- Names get translated into symbols quickly --- */
245 name *n = name_find($1, 1, &f);
251 /*----- Various class expression types ------------------------------------*
253 * Unfortunately, all these need to handle token types slightly differently
254 * and I can't be bothered to remember the current state.
257 /* --- User class expressions --- */
259 user_class : user_class ',' user_class {
260 if (($$ = class_union($1, $3)) == 0) {
261 yyerror("type mismatch");
265 | user_class '-' user_class {
266 if (($$ = class_diff($1, $3)) == 0) {
267 yyerror("type mismatch");
271 | user_class '&' user_class {
272 if (($$ = class_isect($1, $3)) == 0) {
273 yyerror("type mismatch");
277 | user_class '|' user_class {
278 if (($$ = class_union($1, $3)) == 0) {
279 yyerror("type mismatch");
283 | INT { $$ = class_fromUser(clType_user, $1); }
286 if ((pw = userdb_userByName($1)) == 0) {
287 moan("user `%s' not known at line %i",
291 $$ = class_fromUser(clType_user, pw->pw_uid);
294 name *n = name_find($1, 0, 0);
296 moan("class `%s' not found at line %i",
299 } else if (~n->c->type & clType_user) {
300 yyerror("type mismatch");
307 | '(' user_class ')' { $$ = $2; }
310 /* --- Command class expressions --- */
312 command_class : command_class ',' command_class {
313 if (($$ = class_union($1, $3)) == 0) {
314 yyerror("type mismatch");
318 | command_class '-' command_class {
319 if (($$ = class_diff($1, $3)) == 0) {
320 yyerror("type mismatch");
324 | command_class '&' command_class {
325 if (($$ = class_isect($1, $3)) == 0) {
326 yyerror("type mismatch");
330 | command_class '|' command_class {
331 if (($$ = class_union($1, $3)) == 0) {
332 yyerror("type mismatch");
336 | STRING { $$ = class_fromString(clType_command, $1); }
338 name *n = name_find($1, 0, 0);
340 moan("class `%s' not found at line %i",
343 } else if (~n->c->type & clType_command) {
344 yyerror("type mismatch");
351 | '(' command_class ')' { $$ = $2; }
354 /* --- Host class expressions --- */
356 host_class : host_class ',' host_class {
357 if (($$ = class_union($1, $3)) == 0) {
358 yyerror("type mismatch");
362 | host_class '-' host_class {
363 if (($$ = class_diff($1, $3)) == 0) {
364 yyerror("type mismatch");
368 | host_class '&' host_class {
369 if (($$ = class_isect($1, $3)) == 0) {
370 yyerror("type mismatch");
374 | host_class '|' host_class {
375 if (($$ = class_union($1, $3)) == 0) {
376 yyerror("type mismatch");
380 | STRING { $$ = class_fromString(clType_host, $1); }
382 name *n = name_find($1, 0, 0);
384 moan("class `%s' not found at line %i",
387 } else if (~n->c->type & clType_host) {
388 yyerror("type mismatch");
395 | '(' host_class ')' { $$ = $2; }
398 /*----- That's all, folks -------------------------------------------------*/