3 * $Id: bcquery.c,v 1.3 1999/05/04 16:17:11 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.3 1999/05/04 16:17:11 mdw
33 * Change to header file name for parser. See log for `parse.h' for
36 * Revision 1.2 1998/06/26 10:32:31 mdw
37 * Cosmetic change: use sizeof(destination) in memcpy.
39 * Revision 1.1 1998/04/23 13:20:20 mdw
40 * Added new program to verify and query Become configuration files.
44 /*----- Header files ------------------------------------------------------*/
46 /* --- ANSI headers --- */
56 /* --- Unix headers --- */
58 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <sys/utsname.h>
63 #include <netinet/in.h>
65 #include <arpa/inet.h>
73 /* --- Local headers --- */
89 /*----- Type definitions --------------------------------------------------*/
101 typedef struct qnode {
115 #define q_right q.q.r
119 /*----- Static variables --------------------------------------------------*/
136 static unsigned flags;
137 static const char *cf = file_RULES;
138 static unsigned outmask = cat_where | cat_from | cat_to | cat_what;
140 /*----- Low-level options handling ----------------------------------------*/
142 /* --- @optname@ --- *
146 * Returns: Pointer to a string describing the current option.
148 * Use: Creates a textual description of an option for use in
152 static const char *optname(void)
156 case 'H': return ("-host");
157 case 'F': return ("-from");
158 case 'T': return ("-to");
159 case 'C': return ("-command");
160 case 0: return (optarg);
161 case '(': case ')': case '&': case '|': case '!':
165 case EOF: return ("<eof>");
166 default: return ("<unknown>");
170 /* --- @nextopt@ --- *
174 * Returns: Next option id, or @EOF@.
176 * Use: Reads the next option. Does a lot of the messy work of
180 static int nextopt(void)
182 const static struct option opts[] = {
183 { "help", 0, 0, 'h' },
185 { "file", gFlag_argReq, 0, 'f' },
186 { "dump", 0, 0, 'd' },
187 { "check", 0, 0, 'k' },
189 { "output", gFlag_argReq, 0, 'o' },
190 { "columns", 0, 0, '|' },
191 { "rows", 0, 0, '-' },
192 { "nohead", 0, 0, 'n' },
194 { "host", gFlag_argReq, 0, 'H' },
195 { "from", gFlag_argReq, 0, 'F' },
196 { "to", gFlag_argReq, 0, 'T' },
197 { "command", gFlag_argReq, 0, 'C' },
199 { "and", 0, 0, '&' },
201 { "not", 0, 0, '!' },
207 opt = mdwopt(ac, av, "-", opts, 0, 0, gFlag_noShorts);
212 "Usage: %s [-help]\n"
213 " %s [-output COLS] [-dump] [-file FILE] [EXPR | -check]\n"
215 "Reads the `become' configuration file FILE (or " file_RULES " by\n"
216 "default) and writes the rules which match the EXPR.\n"
218 "EXPR may make use of the following operators: `-host HOST', `-from USER',\n"
219 "`-to USER', and `-command CMD'. You may join them together with `-and',\n"
220 "`-or' and `-not' operators (which may be spelled `&', `|' and `!' if you\n"
221 "prefer), and group subexpressions with parentheses `(' and `)'.\n",
244 enum { m_replace, m_add, m_remove } mode = m_replace;
268 die("unknown column specifier `%c'", *p);
271 if (mode == m_replace) {
277 else if (mode == m_remove)
280 die("bad mode while setting output mask: %u", mode);
288 die("type `%s --help' for usage information", quis());
290 if (optarg[0] && optarg[1] == 0) switch (optarg[0]) {
292 case '&': case '|': case '!':
297 die("unexpected text `%s' found", optarg);
304 /*----- Recursive descent query parser ------------------------------------*/
306 /* --- @qparse@ --- *
310 * Returns: A pointer to the finished tree.
312 * Use: Scans the command line arguments and makes them into a tree.
315 static qnode *qparse_expr(void);
317 static qnode *qparse_atom(void)
325 die("syntax error: expected `)', found `%s'", optname());
331 qnode *q = xmalloc(sizeof(*q));
332 h = gethostbyname(optarg);
334 die("unknown host `%s'", optarg);
335 q->q_cat = cat_where;
336 memcpy(&q->q_in, h->h_addr, sizeof(q->q_in));
340 case 'F': case 'T': {
341 qnode *q = xmalloc(sizeof(*q));
342 q->q_cat = (opt == 'F' ? cat_from : cat_to);
343 if (isdigit((unsigned char)optarg[0]))
344 q->q_uid = atoi(optarg);
347 if (!(flags & f_userdb)) {
353 pw = userdb_userByName(optarg);
355 die("unknown user `%s'", optarg);
356 q->q_uid = pw->pw_uid;
362 qnode *q = xmalloc(sizeof(*q));
369 die("unexpected token: `%s'", optname());
374 static qnode *qparse_factor(void)
377 qnode *q = xmalloc(sizeof(*q));
380 q->q_arg = qparse_atom();
383 return (qparse_atom());
386 static qnode *qparse_term(void)
388 qnode *top, *q, **qq;
396 case 'H': case 'F': case 'T': case 'C': case '!': case '(':
397 *qq = xmalloc(sizeof(*q));
398 (*qq)->q_cat = cat_and;
400 qq = &(*qq)->q_right;
409 static qnode *qparse_expr(void)
411 qnode *top, *q, **qq;
419 *qq = xmalloc(sizeof(*q));
420 (*qq)->q_cat = cat_or;
422 qq = &(*qq)->q_right;
431 static qnode *qparse(void)
439 die("syntax error: `%s' unexpected", optname());
443 /* --- @dumptree@ --- *
445 * Arguments: @qnode *q@ = pointer to tree to dump
446 * @int indent@ = indentation for this subtree
450 * Use: Dumps a tree to stdout for debugging purposes.
453 static void dumptree(qnode *q, int indent)
456 printf("<empty> -- magic query which matches everything\n");
459 printf("%*s", indent * 2, "");
463 printf("host = %s\n", inet_ntoa(q->q_in));
466 printf("from = %u\n", (unsigned)q->q_uid);
469 printf("to = %u\n", (unsigned)q->q_uid);
472 printf("command = `%s'\n", q->q_cmd);
480 unsigned cat = q->q_cat;
481 printf(cat == cat_and ? "and\n" : "or\n");
482 while (q->q_cat == cat) {
483 dumptree(q->q_left, indent);
489 printf("unknown type %u\n", q->q_cat);
493 /*----- Recursive query matching ------------------------------------------*/
495 /* --- @checkrule@ --- *
497 * Arguments: @rule *r@ = pointer to a rule
498 * @qnode *q@ = pointer to a query tree
500 * Returns: Nonzero if the query matches the rule.
502 * Use: Matches rules and queries.
505 static int checkrule(rule *r, qnode *q)
510 /* --- Handle the compound query types --- */
513 return (!checkrule(r, q->q_arg));
516 if (!checkrule(r, q->q_left))
522 if (checkrule(r, q->q_left))
527 /* --- And now the simple query types --- */
530 return (class_matchHost(r->host, q->q_in));
532 return (class_matchUser(r->from, q->q_uid));
534 return (class_matchUser(r->to, q->q_uid));
536 return (class_matchCommand(r->cmd, q->q_cmd));
539 /* --- Anything else is bogus (and a bug) --- */
541 die("unexpected cat code %u in checkrule", q->q_cat);
545 /*----- Rule output -------------------------------------------------------*/
547 /* --- @showrule@ --- *
549 * Arguments: @rule *r@ = pointer to a rule block
553 * Use: Writes a rule block to the output in a pleasant way.
556 static const char *xltuser(uid_t u)
559 struct passwd *pw = userdb_userById(u);
561 return (pw->pw_name);
562 sprintf(buf, "%u", (unsigned)u);
566 static void classfirstrow(class_node *c, const char *fmt, sym_iter *i,
567 unsigned bit, unsigned *imask)
569 switch (c->type & clNode_mask) {
571 printf(fmt, (c == class_all ? "ALL" :
572 c == class_none ? "NONE" :
576 printf(fmt, (c->type & clType_user) ? xltuser(c->v.u) : c->v.s);
580 sym_createIter(i, &c->v.t);
585 } else if (c->type & clType_user)
586 printf(fmt, xltuser(*(uid_t *)b->name));
588 printf(fmt, b->name);
592 printf(fmt, "<complex>");
597 static void showclass(class_node *c,
598 void (*sc)(class_node *c),
599 void (*sh)(sym_base *b))
604 switch (c->type & clNode_mask) {
606 fputs(c == class_all ? "ALL" :
607 c == class_none ? "NONE" : "<buggy>",
616 sym_createIter(&i, &c->v.t);
618 if ((b = sym_next(&i)) != 0) {
620 while ((b = sym_next(&i)) != 0) {
637 fputs("<unknown node type>", stdout);
640 type = c->type & clNode_mask;
643 showclass(c->v.c.l, sc, sh);
646 } while ((c->type & clNode_mask) == type);
647 showclass(c, sc, sh);
653 static void showuseri(class_node *c) { fputs(xltuser(c->v.u), stdout); }
655 static void showuserh(sym_base *b)
657 fputs(xltuser(*(uid_t *)b->name), stdout);
660 static void showstringi(class_node *c) { fputs(c->v.s, stdout); }
662 static void showstringh(sym_base *b) { fputs(b->name, stdout); }
664 static void showrule(rule *r)
666 /* --- First up: display of simple classes in columns --- */
668 if (flags & f_simple) {
670 sym_base *w = 0, *x = 0, *y = 0, *z = 0;
673 /* --- Print the header line if necessary --- */
675 if (!(flags & f_header)) {
676 if (!(flags & f_nohead)) {
677 if (outmask & cat_from) printf("%-15s ", "FROM");
678 if (outmask & cat_to) printf("%-15s ", "TO");
679 if (outmask & cat_where) printf("%-24s ", "HOST");
680 if (outmask & cat_what) printf("%s", "COMMAND");
688 /* --- Print out the first row --- */
690 if (outmask & cat_from)
691 classfirstrow(r->from, "%-15.15s ", &a, cat_from, &imask);
692 if (outmask & cat_to)
693 classfirstrow(r->to, "%-15.15s ", &b, cat_to, &imask);
694 if (outmask & cat_where)
695 classfirstrow(r->host, "%-24.24s ", &c, cat_where, &imask);
696 if (outmask & cat_what)
697 classfirstrow(r->cmd, "%s", &d, cat_what, &imask);
700 /* --- And now for the rest --- */
703 if ((imask & cat_from) && (w = sym_next(&a)) == 0)
705 if ((imask & cat_to) && (x = sym_next(&b)) == 0)
707 if ((imask & cat_where) && (y = sym_next(&c)) == 0)
709 if ((imask & cat_what) && (z = sym_next(&d)) == 0)
715 if (outmask & cat_from) {
717 !(imask & cat_from) ? "" : xltuser(*(uid_t *)w->name));
720 if (outmask & cat_to) {
722 !(imask & cat_to) ? "" : xltuser(*(uid_t *)x->name));
725 if (outmask & cat_where)
726 printf("%-24.24s ", !(imask & cat_where) ? "" : y->name);
728 if (outmask & cat_what)
729 printf("%s", !(imask & cat_what) ? "" : z->name);
735 /* --- Otherwise deal with complex cases --- */
738 if (flags & f_header)
742 if (outmask & cat_from) {
743 fputs(" From: ", stdout);
744 showclass(r->from, showuseri, showuserh);
747 if (outmask & cat_to) {
748 fputs(" To: ", stdout);
749 showclass(r->to, showuseri, showuserh);
752 if (outmask & cat_where) {
753 fputs(" Hosts: ", stdout);
754 showclass(r->host, showstringi, showstringh);
757 if (outmask & cat_what) {
758 fputs("Commands: ", stdout);
759 showclass(r->cmd, showstringi, showstringh);
765 /*----- Dummy functions ---------------------------------------------------*/
767 void daemon_usePort(int p) { ; }
768 void daemon_readKey(const char *f) { ; }
770 /*----- Main code ---------------------------------------------------------*/
774 * Arguments: @int argc@ = number of command line arguments
775 * @char *argv[]@ = pointer to command line arguments
777 * Returns: Zero if all went OK.
779 * Use: Verifies and queries the `become' configuration file.
782 int main(int argc, char *argv[])
786 /* --- Initialise things --- */
789 ac = argc; av = argv;
791 /* --- Read the query tree --- */
795 /* --- Dump the tree if so requested --- */
797 if (flags & f_dump) {
802 /* --- Check columns requested --- */
804 if (outmask == (outmask & (outmask - 1)))
807 /* --- Read the ruleset --- */
809 if (!(flags & f_userdb)) {
820 FILE *fp = fopen(cf, "r");
824 die("couldn't open configuration file `%s': %s", cf, strerror(errno));
831 /* --- Now scan the query --- */
834 rule *rl = rule_list(), *r;
836 /* --- Decide on output format if not already chosen --- */
838 if (!(flags & f_force)) {
842 if ((!qtree || checkrule(r, qtree)) &&
843 ((r->host->type & clNode_mask) >= clNode_binop ||
844 (r->from->type & clNode_mask) >= clNode_binop ||
845 (r->to->type & clNode_mask) >= clNode_binop ||
846 (r->cmd->type & clNode_mask) >= clNode_binop)) {
854 /* --- Now just dump the matching items --- */
858 if (!qtree || checkrule(r, qtree)) {
868 if (!(flags & f_match))
873 /*----- That's all, folks -------------------------------------------------*/