3 * $Id: bcquery.c,v 1.1 1998/04/23 13:20:20 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.1 1998/04/23 13:20:20 mdw
33 * Added new program to verify and query Become configuration files.
37 /*----- Header files ------------------------------------------------------*/
39 /* --- ANSI headers --- */
49 /* --- Unix headers --- */
51 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <sys/utsname.h>
56 #include <netinet/in.h>
58 #include <arpa/inet.h>
66 /* --- Local headers --- */
82 /*----- Type definitions --------------------------------------------------*/
94 typedef struct qnode
{
108 #define q_right q.q.r
112 /*----- Static variables --------------------------------------------------*/
129 static unsigned flags
;
130 static const char *cf
= file_RULES
;
131 static unsigned outmask
= cat_where
| cat_from
| cat_to
| cat_what
;
133 /*----- Low-level options handling ----------------------------------------*/
135 /* --- @optname@ --- *
139 * Returns: Pointer to a string describing the current option.
141 * Use: Creates a textual description of an option for use in
145 static const char *optname(void)
149 case 'H': return ("-host");
150 case 'F': return ("-from");
151 case 'T': return ("-to");
152 case 'C': return ("-command");
153 case 0: return (optarg
);
154 case '(': case ')': case '&': case '|': case '!':
158 case EOF
: return ("<eof>");
159 default: return ("<unknown>");
163 /* --- @nextopt@ --- *
167 * Returns: Next option id, or @EOF@.
169 * Use: Reads the next option. Does a lot of the messy work of
173 static int nextopt(void)
175 const static struct option opts
[] = {
176 { "help", 0, 0, 'h' },
178 { "file", gFlag_argReq
, 0, 'f' },
179 { "dump", 0, 0, 'd' },
180 { "check", 0, 0, 'k' },
182 { "output", gFlag_argReq
, 0, 'o' },
183 { "columns", 0, 0, '|' },
184 { "rows", 0, 0, '-' },
185 { "nohead", 0, 0, 'n' },
187 { "host", gFlag_argReq
, 0, 'H' },
188 { "from", gFlag_argReq
, 0, 'F' },
189 { "to", gFlag_argReq
, 0, 'T' },
190 { "command", gFlag_argReq
, 0, 'C' },
192 { "and", 0, 0, '&' },
194 { "not", 0, 0, '!' },
200 opt
= mdwopt(ac
, av
, "-", opts
, 0, 0, gFlag_noShorts
);
205 "Usage: %s [-help]\n"
206 " %s [-output COLS] [-dump] [-file FILE] [EXPR | -check]\n"
208 "Reads the `become' configuration file FILE (or " file_RULES
" by\n"
209 "default) and writes the rules which match the EXPR.\n"
211 "EXPR may make use of the following operators: `-host HOST', `-from USER',\n"
212 "`-to USER', and `-command CMD'. You may join them together with `-and',\n"
213 "`-or' and `-not' operators (which may be spelled `&', `|' and `!' if you\n"
214 "prefer), and group subexpressions with parentheses `(' and `)'.\n",
237 enum { m_replace
, m_add
, m_remove
} mode
= m_replace
;
261 die("unknown column specifier `%c'", *p
);
264 if (mode
== m_replace
) {
270 else if (mode
== m_remove
)
273 die("bad mode while setting output mask: %u", mode
);
281 die("type `%s --help' for usage information", quis());
283 if (optarg
[0] && optarg
[1] == 0) switch (optarg
[0]) {
285 case '&': case '|': case '!':
290 die("unexpected text `%s' found", optarg
);
297 /*----- Recursive descent query parser ------------------------------------*/
299 /* --- @qparse@ --- *
303 * Returns: A pointer to the finished tree.
305 * Use: Scans the command line arguments and makes them into a tree.
308 static qnode
*qparse_expr(void);
310 static qnode
*qparse_atom(void)
318 die("syntax error: expected `)', found `%s'", optname());
324 qnode
*q
= xmalloc(sizeof(*q
));
325 h
= gethostbyname(optarg
);
327 die("unknown host `%s'", optarg
);
328 q
->q_cat
= cat_where
;
329 memcpy(&q
->q_in
, h
->h_addr
, sizeof(struct in_addr
));
333 case 'F': case 'T': {
334 qnode
*q
= xmalloc(sizeof(*q
));
335 q
->q_cat
= (opt
== 'F' ? cat_from
: cat_to
);
336 if (isdigit((unsigned char)optarg
[0]))
337 q
->q_uid
= atoi(optarg
);
340 if (!(flags
& f_userdb
)) {
346 pw
= userdb_userByName(optarg
);
348 die("unknown user `%s'", optarg
);
349 q
->q_uid
= pw
->pw_uid
;
355 qnode
*q
= xmalloc(sizeof(*q
));
362 die("unexpected token: `%s'", optname());
367 static qnode
*qparse_factor(void)
370 qnode
*q
= xmalloc(sizeof(*q
));
373 q
->q_arg
= qparse_atom();
376 return (qparse_atom());
379 static qnode
*qparse_term(void)
381 qnode
*top
, *q
, **qq
;
389 case 'H': case 'F': case 'T': case 'C': case '!': case '(':
390 *qq
= xmalloc(sizeof(*q
));
391 (*qq
)->q_cat
= cat_and
;
393 qq
= &(*qq
)->q_right
;
402 static qnode
*qparse_expr(void)
404 qnode
*top
, *q
, **qq
;
412 *qq
= xmalloc(sizeof(*q
));
413 (*qq
)->q_cat
= cat_or
;
415 qq
= &(*qq
)->q_right
;
424 static qnode
*qparse(void)
432 die("syntax error: `%s' unexpected", optname());
436 /* --- @dumptree@ --- *
438 * Arguments: @qnode *q@ = pointer to tree to dump
439 * @int indent@ = indentation for this subtree
443 * Use: Dumps a tree to stdout for debugging purposes.
446 static void dumptree(qnode
*q
, int indent
)
449 printf("<empty> -- magic query which matches everything\n");
452 printf("%*s", indent
* 2, "");
456 printf("host = %s\n", inet_ntoa(q
->q_in
));
459 printf("from = %u\n", (unsigned)q
->q_uid
);
462 printf("to = %u\n", (unsigned)q
->q_uid
);
465 printf("command = `%s'\n", q
->q_cmd
);
473 unsigned cat
= q
->q_cat
;
474 printf(cat
== cat_and ?
"and\n" : "or\n");
475 while (q
->q_cat
== cat
) {
476 dumptree(q
->q_left
, indent
);
482 printf("unknown type %u\n", q
->q_cat
);
486 /*----- Recursive query matching ------------------------------------------*/
488 /* --- @checkrule@ --- *
490 * Arguments: @rule *r@ = pointer to a rule
491 * @qnode *q@ = pointer to a query tree
493 * Returns: Nonzero if the query matches the rule.
495 * Use: Matches rules and queries.
498 static int checkrule(rule
*r
, qnode
*q
)
503 /* --- Handle the compound query types --- */
506 return (!checkrule(r
, q
->q_arg
));
509 if (!checkrule(r
, q
->q_left
))
515 if (checkrule(r
, q
->q_left
))
520 /* --- And now the simple query types --- */
523 return (class_matchHost(r
->host
, q
->q_in
));
525 return (class_matchUser(r
->from
, q
->q_uid
));
527 return (class_matchUser(r
->to
, q
->q_uid
));
529 return (class_matchCommand(r
->cmd
, q
->q_cmd
));
532 /* --- Anything else is bogus (and a bug) --- */
534 die("unexpected cat code %u in checkrule", q
->q_cat
);
538 /*----- Rule output -------------------------------------------------------*/
540 /* --- @showrule@ --- *
542 * Arguments: @rule *r@ = pointer to a rule block
546 * Use: Writes a rule block to the output in a pleasant way.
549 static const char *xltuser(uid_t u
)
552 struct passwd
*pw
= userdb_userById(u
);
554 return (pw
->pw_name
);
555 sprintf(buf
, "%u", (unsigned)u
);
559 static void classfirstrow(class_node
*c
, const char *fmt
, sym_iter
*i
,
560 unsigned bit
, unsigned *imask
)
562 switch (c
->type
& clNode_mask
) {
564 printf(fmt
, (c
== class_all ?
"ALL" :
565 c
== class_none ?
"NONE" :
569 printf(fmt
, (c
->type
& clType_user
) ?
xltuser(c
->v
.u
) : c
->v
.s
);
573 sym_createIter(i
, &c
->v
.t
);
578 } else if (c
->type
& clType_user
)
579 printf(fmt
, xltuser(*(uid_t
*)b
->name
));
581 printf(fmt
, b
->name
);
585 printf(fmt
, "<complex>");
590 static void showclass(class_node
*c
,
591 void (*sc
)(class_node
*c
),
592 void (*sh
)(sym_base
*b
))
597 switch (c
->type
& clNode_mask
) {
599 fputs(c
== class_all ?
"ALL" :
600 c
== class_none ?
"NONE" : "<buggy>",
609 sym_createIter(&i
, &c
->v
.t
);
611 if ((b
= sym_next(&i
)) != 0) {
613 while ((b
= sym_next(&i
)) != 0) {
630 fputs("<unknown node type>", stdout
);
633 type
= c
->type
& clNode_mask
;
636 showclass(c
->v
.c
.l
, sc
, sh
);
639 } while ((c
->type
& clNode_mask
) == type
);
640 showclass(c
, sc
, sh
);
646 static void showuseri(class_node
*c
) { fputs(xltuser(c
->v
.u
), stdout
); }
648 static void showuserh(sym_base
*b
)
650 fputs(xltuser(*(uid_t
*)b
->name
), stdout
);
653 static void showstringi(class_node
*c
) { fputs(c
->v
.s
, stdout
); }
655 static void showstringh(sym_base
*b
) { fputs(b
->name
, stdout
); }
657 static void showrule(rule
*r
)
659 /* --- First up: display of simple classes in columns --- */
661 if (flags
& f_simple
) {
663 sym_base
*w
= 0, *x
= 0, *y
= 0, *z
= 0;
666 /* --- Print the header line if necessary --- */
668 if (!(flags
& f_header
)) {
669 if (!(flags
& f_nohead
)) {
670 if (outmask
& cat_from
) printf("%-15s ", "FROM");
671 if (outmask
& cat_to
) printf("%-15s ", "TO");
672 if (outmask
& cat_where
) printf("%-24s ", "HOST");
673 if (outmask
& cat_what
) printf("%s", "COMMAND");
681 /* --- Print out the first row --- */
683 if (outmask
& cat_from
)
684 classfirstrow(r
->from
, "%-15.15s ", &a
, cat_from
, &imask
);
685 if (outmask
& cat_to
)
686 classfirstrow(r
->to
, "%-15.15s ", &b
, cat_to
, &imask
);
687 if (outmask
& cat_where
)
688 classfirstrow(r
->host
, "%-24.24s ", &c
, cat_where
, &imask
);
689 if (outmask
& cat_what
)
690 classfirstrow(r
->cmd
, "%s", &d
, cat_what
, &imask
);
693 /* --- And now for the rest --- */
696 if ((imask
& cat_from
) && (w
= sym_next(&a
)) == 0)
698 if ((imask
& cat_to
) && (x
= sym_next(&b
)) == 0)
700 if ((imask
& cat_where
) && (y
= sym_next(&c
)) == 0)
702 if ((imask
& cat_what
) && (z
= sym_next(&d
)) == 0)
708 if (outmask
& cat_from
) {
710 !(imask
& cat_from
) ?
"" : xltuser(*(uid_t
*)w
->name
));
713 if (outmask
& cat_to
) {
715 !(imask
& cat_to
) ?
"" : xltuser(*(uid_t
*)x
->name
));
718 if (outmask
& cat_where
)
719 printf("%-24.24s ", !(imask
& cat_where
) ?
"" : y
->name
);
721 if (outmask
& cat_what
)
722 printf("%s", !(imask
& cat_what
) ?
"" : z
->name
);
728 /* --- Otherwise deal with complex cases --- */
731 if (flags
& f_header
)
735 if (outmask
& cat_from
) {
736 fputs(" From: ", stdout
);
737 showclass(r
->from
, showuseri
, showuserh
);
740 if (outmask
& cat_to
) {
741 fputs(" To: ", stdout
);
742 showclass(r
->to
, showuseri
, showuserh
);
745 if (outmask
& cat_where
) {
746 fputs(" Hosts: ", stdout
);
747 showclass(r
->host
, showstringi
, showstringh
);
750 if (outmask
& cat_what
) {
751 fputs("Commands: ", stdout
);
752 showclass(r
->cmd
, showstringi
, showstringh
);
758 /*----- Dummy functions ---------------------------------------------------*/
760 void daemon_usePort(int p
) { ; }
761 void daemon_readKey(const char *f
) { ; }
763 /*----- Main code ---------------------------------------------------------*/
767 * Arguments: @int argc@ = number of command line arguments
768 * @char *argv[]@ = pointer to command line arguments
770 * Returns: Zero if all went OK.
772 * Use: Verifies and queries the `become' configuration file.
775 int main(int argc
, char *argv
[])
779 /* --- Initialise things --- */
782 ac
= argc
; av
= argv
;
784 /* --- Read the query tree --- */
788 /* --- Dump the tree if so requested --- */
790 if (flags
& f_dump
) {
795 /* --- Check columns requested --- */
797 if (outmask
== (outmask
& (outmask
- 1)))
800 /* --- Read the ruleset --- */
802 if (!(flags
& f_userdb
)) {
813 FILE *fp
= fopen(cf
, "r");
817 die("couldn't open configuration file `%s': %s", cf
, strerror(errno
));
824 /* --- Now scan the query --- */
827 rule
*rl
= rule_list(), *r
;
829 /* --- Decide on output format if not already chosen --- */
831 if (!(flags
& f_force
)) {
835 if ((!qtree
|| checkrule(r
, qtree
)) &&
836 ((r
->host
->type
& clNode_mask
) >= clNode_binop
||
837 (r
->from
->type
& clNode_mask
) >= clNode_binop
||
838 (r
->to
->type
& clNode_mask
) >= clNode_binop
||
839 (r
->cmd
->type
& clNode_mask
) >= clNode_binop
)) {
847 /* --- Now just dump the matching items --- */
851 if (!qtree
|| checkrule(r
, qtree
)) {
861 if (!(flags
& f_match
))
866 /*----- That's all, folks -------------------------------------------------*/