3 * $Id: bcquery.c,v 1.2 1998/06/26 10:32:31 mdw Exp $
5 * Query and dump Become's configuration file
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.2 1998/06/26 10:32:31 mdw
33 * Cosmetic change: use sizeof(destination) in memcpy.
35 * Revision 1.1 1998/04/23 13:20:20 mdw
36 * Added new program to verify and query Become configuration files.
40 /*----- Header files ------------------------------------------------------*/
42 /* --- ANSI headers --- */
52 /* --- Unix headers --- */
54 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <sys/utsname.h>
59 #include <netinet/in.h>
61 #include <arpa/inet.h>
69 /* --- Local headers --- */
85 /*----- Type definitions --------------------------------------------------*/
97 typedef struct qnode
{
111 #define q_right q.q.r
115 /*----- Static variables --------------------------------------------------*/
132 static unsigned flags
;
133 static const char *cf
= file_RULES
;
134 static unsigned outmask
= cat_where
| cat_from
| cat_to
| cat_what
;
136 /*----- Low-level options handling ----------------------------------------*/
138 /* --- @optname@ --- *
142 * Returns: Pointer to a string describing the current option.
144 * Use: Creates a textual description of an option for use in
148 static const char *optname(void)
152 case 'H': return ("-host");
153 case 'F': return ("-from");
154 case 'T': return ("-to");
155 case 'C': return ("-command");
156 case 0: return (optarg
);
157 case '(': case ')': case '&': case '|': case '!':
161 case EOF
: return ("<eof>");
162 default: return ("<unknown>");
166 /* --- @nextopt@ --- *
170 * Returns: Next option id, or @EOF@.
172 * Use: Reads the next option. Does a lot of the messy work of
176 static int nextopt(void)
178 const static struct option opts
[] = {
179 { "help", 0, 0, 'h' },
181 { "file", gFlag_argReq
, 0, 'f' },
182 { "dump", 0, 0, 'd' },
183 { "check", 0, 0, 'k' },
185 { "output", gFlag_argReq
, 0, 'o' },
186 { "columns", 0, 0, '|' },
187 { "rows", 0, 0, '-' },
188 { "nohead", 0, 0, 'n' },
190 { "host", gFlag_argReq
, 0, 'H' },
191 { "from", gFlag_argReq
, 0, 'F' },
192 { "to", gFlag_argReq
, 0, 'T' },
193 { "command", gFlag_argReq
, 0, 'C' },
195 { "and", 0, 0, '&' },
197 { "not", 0, 0, '!' },
203 opt
= mdwopt(ac
, av
, "-", opts
, 0, 0, gFlag_noShorts
);
208 "Usage: %s [-help]\n"
209 " %s [-output COLS] [-dump] [-file FILE] [EXPR | -check]\n"
211 "Reads the `become' configuration file FILE (or " file_RULES
" by\n"
212 "default) and writes the rules which match the EXPR.\n"
214 "EXPR may make use of the following operators: `-host HOST', `-from USER',\n"
215 "`-to USER', and `-command CMD'. You may join them together with `-and',\n"
216 "`-or' and `-not' operators (which may be spelled `&', `|' and `!' if you\n"
217 "prefer), and group subexpressions with parentheses `(' and `)'.\n",
240 enum { m_replace
, m_add
, m_remove
} mode
= m_replace
;
264 die("unknown column specifier `%c'", *p
);
267 if (mode
== m_replace
) {
273 else if (mode
== m_remove
)
276 die("bad mode while setting output mask: %u", mode
);
284 die("type `%s --help' for usage information", quis());
286 if (optarg
[0] && optarg
[1] == 0) switch (optarg
[0]) {
288 case '&': case '|': case '!':
293 die("unexpected text `%s' found", optarg
);
300 /*----- Recursive descent query parser ------------------------------------*/
302 /* --- @qparse@ --- *
306 * Returns: A pointer to the finished tree.
308 * Use: Scans the command line arguments and makes them into a tree.
311 static qnode
*qparse_expr(void);
313 static qnode
*qparse_atom(void)
321 die("syntax error: expected `)', found `%s'", optname());
327 qnode
*q
= xmalloc(sizeof(*q
));
328 h
= gethostbyname(optarg
);
330 die("unknown host `%s'", optarg
);
331 q
->q_cat
= cat_where
;
332 memcpy(&q
->q_in
, h
->h_addr
, sizeof(q
->q_in
));
336 case 'F': case 'T': {
337 qnode
*q
= xmalloc(sizeof(*q
));
338 q
->q_cat
= (opt
== 'F' ? cat_from
: cat_to
);
339 if (isdigit((unsigned char)optarg
[0]))
340 q
->q_uid
= atoi(optarg
);
343 if (!(flags
& f_userdb
)) {
349 pw
= userdb_userByName(optarg
);
351 die("unknown user `%s'", optarg
);
352 q
->q_uid
= pw
->pw_uid
;
358 qnode
*q
= xmalloc(sizeof(*q
));
365 die("unexpected token: `%s'", optname());
370 static qnode
*qparse_factor(void)
373 qnode
*q
= xmalloc(sizeof(*q
));
376 q
->q_arg
= qparse_atom();
379 return (qparse_atom());
382 static qnode
*qparse_term(void)
384 qnode
*top
, *q
, **qq
;
392 case 'H': case 'F': case 'T': case 'C': case '!': case '(':
393 *qq
= xmalloc(sizeof(*q
));
394 (*qq
)->q_cat
= cat_and
;
396 qq
= &(*qq
)->q_right
;
405 static qnode
*qparse_expr(void)
407 qnode
*top
, *q
, **qq
;
415 *qq
= xmalloc(sizeof(*q
));
416 (*qq
)->q_cat
= cat_or
;
418 qq
= &(*qq
)->q_right
;
427 static qnode
*qparse(void)
435 die("syntax error: `%s' unexpected", optname());
439 /* --- @dumptree@ --- *
441 * Arguments: @qnode *q@ = pointer to tree to dump
442 * @int indent@ = indentation for this subtree
446 * Use: Dumps a tree to stdout for debugging purposes.
449 static void dumptree(qnode
*q
, int indent
)
452 printf("<empty> -- magic query which matches everything\n");
455 printf("%*s", indent
* 2, "");
459 printf("host = %s\n", inet_ntoa(q
->q_in
));
462 printf("from = %u\n", (unsigned)q
->q_uid
);
465 printf("to = %u\n", (unsigned)q
->q_uid
);
468 printf("command = `%s'\n", q
->q_cmd
);
476 unsigned cat
= q
->q_cat
;
477 printf(cat
== cat_and ?
"and\n" : "or\n");
478 while (q
->q_cat
== cat
) {
479 dumptree(q
->q_left
, indent
);
485 printf("unknown type %u\n", q
->q_cat
);
489 /*----- Recursive query matching ------------------------------------------*/
491 /* --- @checkrule@ --- *
493 * Arguments: @rule *r@ = pointer to a rule
494 * @qnode *q@ = pointer to a query tree
496 * Returns: Nonzero if the query matches the rule.
498 * Use: Matches rules and queries.
501 static int checkrule(rule
*r
, qnode
*q
)
506 /* --- Handle the compound query types --- */
509 return (!checkrule(r
, q
->q_arg
));
512 if (!checkrule(r
, q
->q_left
))
518 if (checkrule(r
, q
->q_left
))
523 /* --- And now the simple query types --- */
526 return (class_matchHost(r
->host
, q
->q_in
));
528 return (class_matchUser(r
->from
, q
->q_uid
));
530 return (class_matchUser(r
->to
, q
->q_uid
));
532 return (class_matchCommand(r
->cmd
, q
->q_cmd
));
535 /* --- Anything else is bogus (and a bug) --- */
537 die("unexpected cat code %u in checkrule", q
->q_cat
);
541 /*----- Rule output -------------------------------------------------------*/
543 /* --- @showrule@ --- *
545 * Arguments: @rule *r@ = pointer to a rule block
549 * Use: Writes a rule block to the output in a pleasant way.
552 static const char *xltuser(uid_t u
)
555 struct passwd
*pw
= userdb_userById(u
);
557 return (pw
->pw_name
);
558 sprintf(buf
, "%u", (unsigned)u
);
562 static void classfirstrow(class_node
*c
, const char *fmt
, sym_iter
*i
,
563 unsigned bit
, unsigned *imask
)
565 switch (c
->type
& clNode_mask
) {
567 printf(fmt
, (c
== class_all ?
"ALL" :
568 c
== class_none ?
"NONE" :
572 printf(fmt
, (c
->type
& clType_user
) ?
xltuser(c
->v
.u
) : c
->v
.s
);
576 sym_createIter(i
, &c
->v
.t
);
581 } else if (c
->type
& clType_user
)
582 printf(fmt
, xltuser(*(uid_t
*)b
->name
));
584 printf(fmt
, b
->name
);
588 printf(fmt
, "<complex>");
593 static void showclass(class_node
*c
,
594 void (*sc
)(class_node
*c
),
595 void (*sh
)(sym_base
*b
))
600 switch (c
->type
& clNode_mask
) {
602 fputs(c
== class_all ?
"ALL" :
603 c
== class_none ?
"NONE" : "<buggy>",
612 sym_createIter(&i
, &c
->v
.t
);
614 if ((b
= sym_next(&i
)) != 0) {
616 while ((b
= sym_next(&i
)) != 0) {
633 fputs("<unknown node type>", stdout
);
636 type
= c
->type
& clNode_mask
;
639 showclass(c
->v
.c
.l
, sc
, sh
);
642 } while ((c
->type
& clNode_mask
) == type
);
643 showclass(c
, sc
, sh
);
649 static void showuseri(class_node
*c
) { fputs(xltuser(c
->v
.u
), stdout
); }
651 static void showuserh(sym_base
*b
)
653 fputs(xltuser(*(uid_t
*)b
->name
), stdout
);
656 static void showstringi(class_node
*c
) { fputs(c
->v
.s
, stdout
); }
658 static void showstringh(sym_base
*b
) { fputs(b
->name
, stdout
); }
660 static void showrule(rule
*r
)
662 /* --- First up: display of simple classes in columns --- */
664 if (flags
& f_simple
) {
666 sym_base
*w
= 0, *x
= 0, *y
= 0, *z
= 0;
669 /* --- Print the header line if necessary --- */
671 if (!(flags
& f_header
)) {
672 if (!(flags
& f_nohead
)) {
673 if (outmask
& cat_from
) printf("%-15s ", "FROM");
674 if (outmask
& cat_to
) printf("%-15s ", "TO");
675 if (outmask
& cat_where
) printf("%-24s ", "HOST");
676 if (outmask
& cat_what
) printf("%s", "COMMAND");
684 /* --- Print out the first row --- */
686 if (outmask
& cat_from
)
687 classfirstrow(r
->from
, "%-15.15s ", &a
, cat_from
, &imask
);
688 if (outmask
& cat_to
)
689 classfirstrow(r
->to
, "%-15.15s ", &b
, cat_to
, &imask
);
690 if (outmask
& cat_where
)
691 classfirstrow(r
->host
, "%-24.24s ", &c
, cat_where
, &imask
);
692 if (outmask
& cat_what
)
693 classfirstrow(r
->cmd
, "%s", &d
, cat_what
, &imask
);
696 /* --- And now for the rest --- */
699 if ((imask
& cat_from
) && (w
= sym_next(&a
)) == 0)
701 if ((imask
& cat_to
) && (x
= sym_next(&b
)) == 0)
703 if ((imask
& cat_where
) && (y
= sym_next(&c
)) == 0)
705 if ((imask
& cat_what
) && (z
= sym_next(&d
)) == 0)
711 if (outmask
& cat_from
) {
713 !(imask
& cat_from
) ?
"" : xltuser(*(uid_t
*)w
->name
));
716 if (outmask
& cat_to
) {
718 !(imask
& cat_to
) ?
"" : xltuser(*(uid_t
*)x
->name
));
721 if (outmask
& cat_where
)
722 printf("%-24.24s ", !(imask
& cat_where
) ?
"" : y
->name
);
724 if (outmask
& cat_what
)
725 printf("%s", !(imask
& cat_what
) ?
"" : z
->name
);
731 /* --- Otherwise deal with complex cases --- */
734 if (flags
& f_header
)
738 if (outmask
& cat_from
) {
739 fputs(" From: ", stdout
);
740 showclass(r
->from
, showuseri
, showuserh
);
743 if (outmask
& cat_to
) {
744 fputs(" To: ", stdout
);
745 showclass(r
->to
, showuseri
, showuserh
);
748 if (outmask
& cat_where
) {
749 fputs(" Hosts: ", stdout
);
750 showclass(r
->host
, showstringi
, showstringh
);
753 if (outmask
& cat_what
) {
754 fputs("Commands: ", stdout
);
755 showclass(r
->cmd
, showstringi
, showstringh
);
761 /*----- Dummy functions ---------------------------------------------------*/
763 void daemon_usePort(int p
) { ; }
764 void daemon_readKey(const char *f
) { ; }
766 /*----- Main code ---------------------------------------------------------*/
770 * Arguments: @int argc@ = number of command line arguments
771 * @char *argv[]@ = pointer to command line arguments
773 * Returns: Zero if all went OK.
775 * Use: Verifies and queries the `become' configuration file.
778 int main(int argc
, char *argv
[])
782 /* --- Initialise things --- */
785 ac
= argc
; av
= argv
;
787 /* --- Read the query tree --- */
791 /* --- Dump the tree if so requested --- */
793 if (flags
& f_dump
) {
798 /* --- Check columns requested --- */
800 if (outmask
== (outmask
& (outmask
- 1)))
803 /* --- Read the ruleset --- */
805 if (!(flags
& f_userdb
)) {
816 FILE *fp
= fopen(cf
, "r");
820 die("couldn't open configuration file `%s': %s", cf
, strerror(errno
));
827 /* --- Now scan the query --- */
830 rule
*rl
= rule_list(), *r
;
832 /* --- Decide on output format if not already chosen --- */
834 if (!(flags
& f_force
)) {
838 if ((!qtree
|| checkrule(r
, qtree
)) &&
839 ((r
->host
->type
& clNode_mask
) >= clNode_binop
||
840 (r
->from
->type
& clNode_mask
) >= clNode_binop
||
841 (r
->to
->type
& clNode_mask
) >= clNode_binop
||
842 (r
->cmd
->type
& clNode_mask
) >= clNode_binop
)) {
850 /* --- Now just dump the matching items --- */
854 if (!qtree
|| checkrule(r
, qtree
)) {
864 if (!(flags
& f_match
))
869 /*----- That's all, folks -------------------------------------------------*/