ddad501010c5f5cdf086e06453283c49d52c8bec
3 * $Id: bcquery.c,v 1.6 2004/04/08 01:36: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 /*----- Header files ------------------------------------------------------*/
31 /* --- ANSI headers --- */
41 /* --- Unix headers --- */
43 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/utsname.h>
48 #include <netinet/in.h>
50 #include <arpa/inet.h>
58 /* --- mLib headers --- */
60 #include <mLib/alloc.h>
61 #include <mLib/mdwopt.h>
62 #include <mLib/quis.h>
63 #include <mLib/report.h>
66 /* --- Local headers --- */
79 /*----- Type definitions --------------------------------------------------*/
91 typedef struct qnode
{
105 #define q_right q.q.r
109 /*----- Static variables --------------------------------------------------*/
126 static unsigned flags
;
127 static const char *cf
= file_RULES
;
128 static unsigned outmask
= cat_where
| cat_from
| cat_to
| cat_what
;
130 /*----- Low-level options handling ----------------------------------------*/
132 /* --- @optname@ --- *
136 * Returns: Pointer to a string describing the current option.
138 * Use: Creates a textual description of an option for use in
142 static const char *optname(void)
146 case 'H': return ("-host");
147 case 'F': return ("-from");
148 case 'T': return ("-to");
149 case 'C': return ("-command");
150 case 0: return (optarg
);
151 case '(': case ')': case '&': case '|': case '!':
155 case EOF
: return ("<eof>");
156 default: return ("<unknown>");
160 /* --- @nextopt@ --- *
164 * Returns: Next option id, or @EOF@.
166 * Use: Reads the next option. Does a lot of the messy work of
170 static int nextopt(void)
172 const static struct option opts
[] = {
173 { "help", 0, 0, 'h' },
175 { "file", gFlag_argReq
, 0, 'f' },
176 { "dump", 0, 0, 'd' },
177 { "check", 0, 0, 'k' },
179 { "output", gFlag_argReq
, 0, 'o' },
180 { "columns", 0, 0, '|' },
181 { "rows", 0, 0, '-' },
182 { "nohead", 0, 0, 'n' },
184 { "host", gFlag_argReq
, 0, 'H' },
185 { "from", gFlag_argReq
, 0, 'F' },
186 { "to", gFlag_argReq
, 0, 'T' },
187 { "command", gFlag_argReq
, 0, 'C' },
189 { "and", 0, 0, '&' },
191 { "not", 0, 0, '!' },
197 opt
= mdwopt(ac
, av
, "-", opts
, 0, 0, gFlag_noShorts
);
202 "Usage: %s [-help]\n"
203 " %s [-output COLS] [-dump] [-file FILE] [EXPR | -check]\n"
205 "Reads the `become' configuration file FILE (or " file_RULES
" by\n"
206 "default) and writes the rules which match the EXPR.\n"
208 "EXPR may make use of the following operators: `-host HOST', `-from USER',\n"
209 "`-to USER', and `-command CMD'. You may join them together with `-and',\n"
210 "`-or' and `-not' operators (which may be spelled `&', `|' and `!' if you\n"
211 "prefer), and group subexpressions with parentheses `(' and `)'.\n",
234 enum { m_replace
, m_add
, m_remove
} mode
= m_replace
;
258 die(1, "unknown column specifier `%c'", *p
);
261 if (mode
== m_replace
) {
267 else if (mode
== m_remove
)
270 die(1, "bad mode while setting output mask: %u", mode
);
278 die(1, "type `%s --help' for usage information", quis());
280 if (optarg
[0] && optarg
[1] == 0) switch (optarg
[0]) {
282 case '&': case '|': case '!':
287 die(1, "unexpected text `%s' found", optarg
);
294 /*----- Recursive descent query parser ------------------------------------*/
296 /* --- @qparse@ --- *
300 * Returns: A pointer to the finished tree.
302 * Use: Scans the command line arguments and makes them into a tree.
305 static qnode
*qparse_expr(void);
307 static qnode
*qparse_atom(void)
315 die(1, "syntax error: expected `)', found `%s'", optname());
321 qnode
*q
= xmalloc(sizeof(*q
));
322 h
= gethostbyname(optarg
);
324 die(1, "unknown host `%s'", optarg
);
325 q
->q_cat
= cat_where
;
326 memcpy(&q
->q_in
, h
->h_addr
, sizeof(q
->q_in
));
330 case 'F': case 'T': {
331 qnode
*q
= xmalloc(sizeof(*q
));
332 q
->q_cat
= (opt
== 'F' ? cat_from
: cat_to
);
333 if (isdigit((unsigned char)optarg
[0]))
334 q
->q_uid
= atoi(optarg
);
337 if (!(flags
& f_userdb
)) {
343 pw
= userdb_userByName(optarg
);
345 die(1, "unknown user `%s'", optarg
);
346 q
->q_uid
= pw
->pw_uid
;
352 qnode
*q
= xmalloc(sizeof(*q
));
359 die(1, "unexpected token: `%s'", optname());
364 static qnode
*qparse_factor(void)
367 qnode
*q
= xmalloc(sizeof(*q
));
370 q
->q_arg
= qparse_atom();
373 return (qparse_atom());
376 static qnode
*qparse_term(void)
378 qnode
*top
, *q
, **qq
;
386 case 'H': case 'F': case 'T': case 'C': case '!': case '(':
387 *qq
= xmalloc(sizeof(*q
));
388 (*qq
)->q_cat
= cat_and
;
390 qq
= &(*qq
)->q_right
;
399 static qnode
*qparse_expr(void)
401 qnode
*top
, *q
, **qq
;
409 *qq
= xmalloc(sizeof(*q
));
410 (*qq
)->q_cat
= cat_or
;
412 qq
= &(*qq
)->q_right
;
421 static qnode
*qparse(void)
429 die(1, "syntax error: `%s' unexpected", optname());
433 /* --- @dumptree@ --- *
435 * Arguments: @qnode *q@ = pointer to tree to dump
436 * @int indent@ = indentation for this subtree
440 * Use: Dumps a tree to stdout for debugging purposes.
443 static void dumptree(qnode
*q
, int indent
)
446 printf("<empty> -- magic query which matches everything\n");
451 printf("%*s", indent
* 2, "");
455 printf("host = %s\n", inet_ntoa(q
->q_in
));
458 printf("from = %u\n", (unsigned)q
->q_uid
);
461 printf("to = %u\n", (unsigned)q
->q_uid
);
464 printf("command = `%s'\n", q
->q_cmd
);
472 unsigned cat
= q
->q_cat
;
473 printf(cat
== cat_and ?
"and\n" : "or\n");
474 while (q
->q_cat
== cat
) {
475 dumptree(q
->q_left
, indent
);
481 printf("unknown type %u\n", q
->q_cat
);
485 /*----- Recursive query matching ------------------------------------------*/
487 /* --- @checkrule@ --- *
489 * Arguments: @rule *r@ = pointer to a rule
490 * @qnode *q@ = pointer to a query tree
492 * Returns: Nonzero if the query matches the rule.
494 * Use: Matches rules and queries.
497 static int checkrule(rule
*r
, qnode
*q
)
502 /* --- Handle the compound query types --- */
505 return (!checkrule(r
, q
->q_arg
));
508 if (!checkrule(r
, q
->q_left
))
514 if (checkrule(r
, q
->q_left
))
519 /* --- And now the simple query types --- */
522 return (class_matchHost(r
->host
, q
->q_in
));
524 return (class_matchUser(r
->from
, q
->q_uid
));
526 return (class_matchUser(r
->to
, q
->q_uid
));
528 return (class_matchCommand(r
->cmd
, q
->q_cmd
));
531 /* --- Anything else is bogus (and a bug) --- */
533 die(1, "unexpected cat code %u in checkrule", q
->q_cat
);
537 /*----- Rule output -------------------------------------------------------*/
539 /* --- @showrule@ --- *
541 * Arguments: @rule *r@ = pointer to a rule block
545 * Use: Writes a rule block to the output in a pleasant way.
548 static const char *xltuser(uid_t u
)
551 struct passwd
*pw
= userdb_userById(u
);
553 return (pw
->pw_name
);
554 sprintf(buf
, "%u", (unsigned)u
);
558 static void classfirstrow(class_node
*c
, const char *fmt
, sym_iter
*i
,
559 unsigned bit
, unsigned *imask
)
561 switch (c
->type
& clNode_mask
) {
563 printf(fmt
, (c
== class_all ?
"ALL" :
564 c
== class_none ?
"NONE" :
568 printf(fmt
, (c
->type
& clType_user
) ?
xltuser(c
->v
.u
) : c
->v
.s
);
572 sym_mkiter(i
, &c
->v
.t
);
577 } else if (c
->type
& clType_user
)
578 printf(fmt
, xltuser(*(uid_t
*)b
->name
));
580 printf(fmt
, b
->name
);
584 printf(fmt
, "<complex>");
589 static void showclass(class_node
*c
,
590 void (*sc
)(class_node
*c
),
591 void (*sh
)(sym_base
*b
))
596 switch (c
->type
& clNode_mask
) {
598 fputs(c
== class_all ?
"ALL" :
599 c
== class_none ?
"NONE" : "<buggy>",
608 sym_mkiter(&i
, &c
->v
.t
);
610 if ((b
= sym_next(&i
)) != 0) {
612 while ((b
= sym_next(&i
)) != 0) {
629 fputs("<unknown node type>", stdout
);
632 type
= c
->type
& clNode_mask
;
635 showclass(c
->v
.c
.l
, sc
, sh
);
638 } while ((c
->type
& clNode_mask
) == type
);
639 showclass(c
, sc
, sh
);
645 static void showuseri(class_node
*c
) { fputs(xltuser(c
->v
.u
), stdout
); }
647 static void showuserh(sym_base
*b
)
649 fputs(xltuser(*(uid_t
*)b
->name
), stdout
);
652 static void showstringi(class_node
*c
) { fputs(c
->v
.s
, stdout
); }
654 static void showstringh(sym_base
*b
) { fputs(b
->name
, stdout
); }
656 static void showrule(rule
*r
)
658 /* --- First up: display of simple classes in columns --- */
660 if (flags
& f_simple
) {
662 sym_base
*w
= 0, *x
= 0, *y
= 0, *z
= 0;
665 /* --- Print the header line if necessary --- */
667 if (!(flags
& f_header
)) {
668 if (!(flags
& f_nohead
)) {
669 if (outmask
& cat_from
) printf("%-15s ", "FROM");
670 if (outmask
& cat_to
) printf("%-15s ", "TO");
671 if (outmask
& cat_where
) printf("%-24s ", "HOST");
672 if (outmask
& cat_what
) printf("%s", "COMMAND");
680 /* --- Print out the first row --- */
682 if (outmask
& cat_from
)
683 classfirstrow(r
->from
, "%-15.15s ", &a
, cat_from
, &imask
);
684 if (outmask
& cat_to
)
685 classfirstrow(r
->to
, "%-15.15s ", &b
, cat_to
, &imask
);
686 if (outmask
& cat_where
)
687 classfirstrow(r
->host
, "%-24.24s ", &c
, cat_where
, &imask
);
688 if (outmask
& cat_what
)
689 classfirstrow(r
->cmd
, "%s", &d
, cat_what
, &imask
);
692 /* --- And now for the rest --- */
695 if ((imask
& cat_from
) && (w
= sym_next(&a
)) == 0)
697 if ((imask
& cat_to
) && (x
= sym_next(&b
)) == 0)
699 if ((imask
& cat_where
) && (y
= sym_next(&c
)) == 0)
701 if ((imask
& cat_what
) && (z
= sym_next(&d
)) == 0)
707 if (outmask
& cat_from
) {
709 !(imask
& cat_from
) ?
"" : xltuser(*(uid_t
*)w
->name
));
712 if (outmask
& cat_to
) {
714 !(imask
& cat_to
) ?
"" : xltuser(*(uid_t
*)x
->name
));
717 if (outmask
& cat_where
)
718 printf("%-24.24s ", !(imask
& cat_where
) ?
"" : y
->name
);
720 if (outmask
& cat_what
)
721 printf("%s", !(imask
& cat_what
) ?
"" : z
->name
);
727 /* --- Otherwise deal with complex cases --- */
730 if (flags
& f_header
)
734 if (outmask
& cat_from
) {
735 fputs(" From: ", stdout
);
736 showclass(r
->from
, showuseri
, showuserh
);
739 if (outmask
& cat_to
) {
740 fputs(" To: ", stdout
);
741 showclass(r
->to
, showuseri
, showuserh
);
744 if (outmask
& cat_where
) {
745 fputs(" Hosts: ", stdout
);
746 showclass(r
->host
, showstringi
, showstringh
);
749 if (outmask
& cat_what
) {
750 fputs("Commands: ", stdout
);
751 showclass(r
->cmd
, showstringi
, showstringh
);
757 /*----- Dummy functions ---------------------------------------------------*/
759 void daemon_usePort(int p
) { ; }
760 void daemon_readKey(const char *f
) { ; }
762 /*----- Main code ---------------------------------------------------------*/
766 * Arguments: @int argc@ = number of command line arguments
767 * @char *argv[]@ = pointer to command line arguments
769 * Returns: Zero if all went OK.
771 * Use: Verifies and queries the `become' configuration file.
774 int main(int argc
, char *argv
[])
778 /* --- Initialise things --- */
781 ac
= argc
; av
= argv
;
783 /* --- Read the query tree --- */
787 /* --- Dump the tree if so requested --- */
789 if (flags
& f_dump
) {
794 /* --- Check columns requested --- */
796 if (outmask
== (outmask
& (outmask
- 1)))
799 /* --- Read the ruleset --- */
801 if (!(flags
& f_userdb
)) {
812 FILE *fp
= fopen(cf
, "r");
816 die(1, "couldn't open configuration file `%s': %s", cf
, strerror(errno
));
823 /* --- Now scan the query --- */
826 rule
*rl
= rule_list(), *r
;
828 /* --- Decide on output format if not already chosen --- */
830 if (!(flags
& f_force
)) {
834 if ((!qtree
|| checkrule(r
, qtree
)) &&
835 ((r
->host
->type
& clNode_mask
) >= clNode_binop
||
836 (r
->from
->type
& clNode_mask
) >= clNode_binop
||
837 (r
->to
->type
& clNode_mask
) >= clNode_binop
||
838 (r
->cmd
->type
& clNode_mask
) >= clNode_binop
)) {
846 /* --- Now just dump the matching items --- */
850 if (!qtree
|| checkrule(r
, qtree
)) {
860 if (!(flags
& f_match
))
865 /*----- That's all, folks -------------------------------------------------*/