3 * $Id: class.c,v 1.6 1997/09/17 10:14:56 mdw Exp $
5 * Handling classes of things nicely
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.6 1997/09/17 10:14:56 mdw
33 * Complete rewrite to support class trees. Makes the behaviour of the set
34 * operators much more logical.
36 * Revision 1.5 1997/08/20 16:16:13 mdw
37 * Patch memory leak. Don't try to trace when tracing's turned off.
39 * Revision 1.4 1997/08/07 09:56:37 mdw
40 * (Log entry for previous version is bogus.) Minor changes to host
43 * Revision 1.2 1997/08/04 10:24:21 mdw
44 * Sources placed under CVS control.
46 * Revision 1.1 1997/07/21 13:47:52 mdw
51 /*----- 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>
70 /* --- Local headers --- */
77 /*----- Global variables --------------------------------------------------*/
79 static class_node class__all
= { clType_all
| clNode_any
, -1, { 0 }};
80 class_node
*class_all
= &class__all
;
82 static class_node class__none
= { clType_all
| clNode_any
, -1, { 0 }};
83 class_node
*class_none
= &class__none
;
85 /*----- Wildcard matching -------------------------------------------------*/
87 /* --- @class__wildMatch@ --- *
89 * Arguments: @const char *pat@ = pointer to pattern string
90 * @const char *p@ = pointer to target string
92 * Returns: Zero if no match, nonzero if match.
94 * Use: Wildcard-matches the pattern against the target string.
97 static int class__wildMatch(const char *pat
, const char *p
)
100 if (*pat
== 0 && *p
== 0)
101 return (42); /* For sadism's sake */
102 else if (*pat
== '*') {
106 if (class__wildMatch(pat
, p
))
107 return (27); /* Nyahaha */
110 return (pat
[1] == 0);
111 } else if (*pat
== '?' || *pat
== *p
)
118 /*----- Creating new class nodes ------------------------------------------*/
120 /* --- @class_fromString@ --- *
122 * Arguments: @unsigned type@ = a type field
123 * @const char *s@ = pointer to string to copy
125 * Returns: A pointer to a class node containing that string, typed to
126 * be the right thing.
128 * Use: Given a string, wrap a class node around it. The node has
129 * one reference (the one you get returned). The string is
130 * copied, so you can get rid of your original one if you like.
133 class_node
*class_fromString(unsigned type
, const char *s
)
135 class_node
*c
= xmalloc(sizeof(*c
));
136 c
->type
= type
| clNode_immed
;
137 if (s
[strcspn(s
, "*?")] == 0)
138 c
->type
|= clFlag_friendly
;
144 /* --- @class_fromUser@ --- *
146 * Arguments: @unsigned type@ = a type field
147 * @uid_t u@ = a user-id number
149 * Returns: A pointer to a class node containing the uid, typed to be
150 * the thing you asked for. Hopefully this will be
153 * Use: Given a uid, wrap a class node around it.
156 class_node
*class_fromUser(unsigned type
, uid_t u
)
158 class_node
*c
= xmalloc(sizeof(*c
));
159 c
->type
= type
| clNode_immed
| clFlag_friendly
;
165 /*----- Reference counter tricks ------------------------------------------*/
167 /* --- @class_inc@ --- *
169 * Arguments: @class_node *c@ = pointer to a class block
173 * Use: Adds a reference to the class definition.
176 void class_inc(class_node
*c
)
178 if (c
!= class_all
&& c
!= class_none
)
182 /* --- @class_dec@ --- *
184 * Arguments: @class_node *c@ = pointer to a class block
188 * Use: Removes a reference to a class block.
191 void class_dec(class_node
*c
)
195 for (cc
= 0; c
; c
= cc
, cc
= 0) {
196 if (c
== class_all
|| c
== class_none
|| --c
->ref
)
198 switch (c
->type
& clNode_mask
) {
203 if (c
->type
& (clType_host
| clType_command
))
207 sym_destroyTable(&c
->v
.t
);
220 /* --- @class_mod@ --- *
222 * Arguments: @class_node *c@ = pointer to a class node
224 * Returns: A pointer to a class node, maybe the same one, maybe not,
225 * with a reference count of 1, containing the same data.
227 * Use: Gives you a node which you can modify. Don't call this
228 * for @class_all@ or @class_none@.
231 class_node
*class_mod(class_node
*c
)
238 cc
= xmalloc(sizeof(*cc
));
241 switch (c
->type
& clNode_mask
) {
243 die("internal error: class_mod called on non-modifiable class node");
247 if (c
->type
& clType_user
)
250 cc
->v
.s
= xstrdup(c
->v
.s
);
257 sym_createTable(&cc
->v
.t
);
258 for (sym_createIter(&i
, &c
->v
.t
); (b
= sym_next(&i
)) != 0; )
259 sym_find(&cc
->v
.t
, b
->name
, b
->len
, sizeof(sym_base
), 0);
265 cc
->v
.c
.l
= c
->v
.c
.l
;
266 cc
->v
.c
.r
= c
->v
.c
.r
;
267 class_inc(cc
->v
.c
.l
);
268 class_inc(cc
->v
.c
.r
);
276 /*----- Some weirder operations on classes --------------------------------*/
278 /* --- @class__hashify@ --- *
280 * Arguments: @class_node *c@ = pointer to a node
282 * Returns: A pointer to a hash node containing the node's value.
284 * Use: The original node must have type `immediate', and it must
285 * be friendly. The old reference is discarded -- you get this
289 static class_node
*class__hashify(class_node
*c
)
291 /* --- Some sanity checking --- */
293 if (~c
->type
& clFlag_friendly
)
294 die("internal error: class__hashify can't hashify unfriendly nodes");
295 if ((c
->type
& clNode_mask
) != clNode_immed
)
296 die("internal error: class__hashify can't hashify non-immediate nodes");
298 /* --- Split off a private copy of the node --- */
302 c
->type
= (c
->type
& clType_mask
) | clNode_hash
| clFlag_friendly
;
304 if (c
->type
& clType_user
) {
306 sym_createTable(&c
->v
.t
);
307 sym_find(&c
->v
.t
, (char *)&u
, sizeof(u
), sizeof(sym_base
), 0);
310 sym_createTable(&c
->v
.t
);
311 sym_find(&c
->v
.t
, s
, -1, sizeof(sym_base
), 0);
318 /*----- Arithmetic on classes ---------------------------------------------*/
320 /* --- @class__binop@ --- *
322 * Arguments: @class_node *l@ = left argument
323 * @class_node *r@ = right argument
324 * @unsigned op@ = the binop code
326 * Returns: A class node representing the result of a binary operation
329 * Use: Performs a binary operation on classes. If the types don't
330 * match, then a null pointer is returned. Both @l@ and @r@
331 * may be modified, and will be decremented before they get
332 * returned to you. If you don't want that to happen, ensure
333 * that you've claimed a reference to the original versions.
335 * If both nodes are `friendly' (see below), then an attempt is
336 * made to combine them sensibly using a hashtable.
338 * If one or both nodes is/are unfriendly, a binop node is
339 * created with type @op@ to allow the matcher to decide on
340 * membership appropriately at match time.
342 * A friendly node is one which can be placed in a hash table to
343 * speed up searching. It's friendly, because it doesn't mind
344 * living with other nodes. Uid numbers are friendly.
345 * Wildcarded strings aren't. Hashtables are trivially
349 class_node
*class__binop(class_node
*l
, class_node
*r
, int op
)
351 unsigned type
= l
->type
& r
->type
& clType_mask
;
352 unsigned lnode
= l
->type
& clNode_mask
, rnode
= r
->type
& clNode_mask
;
354 /* --- Check for compatible types --- */
362 /* --- Handle `friendly' nodes --- */
364 if ((l
->type
& r
->type
& clFlag_friendly
)) {
366 /* --- Consider promoting an item to a hash --- *
368 * If both are immediate nodes, and they're both `friendly', we can
369 * profitably hash them together.
371 * Life gets complicated when we do subtraction, because it's not
372 * commutative. In this case, I have to promote the left operand anyway.
375 if (lnode
== clNode_immed
&&
376 (rnode
== clNode_immed
|| op
== clNode_diff
)) {
378 /* --- Quick check for triviality --- *
380 * There are some more short-cuts I can employ if the values are
384 if (rnode
== clNode_immed
) {
386 /* --- See whether the two items are equal --- */
388 int eq
= (type
& clType_user ?
389 l
->v
.u
== r
->v
.u
: strcmp(l
->v
.s
, r
->v
.s
) == 0);
391 /* --- Now do something appropriate --- */
420 /* --- Turn @l@ into a hash containing itself --- */
422 l
= class__hashify(l
);
426 /* --- Otherwise, make @l@ the hash --- *
428 * Both @l@ and @r@ are friendly. Since they're not both immediates,
429 * one must be a hash.
432 else if ((l
->type
& clNode_mask
) == clNode_immed
) {
436 tn
= l
, l
= r
, r
= tn
;
437 tt
= lnode
, lnode
= rnode
, rnode
= tt
;
440 /* --- Now merge @r@ with @l@ --- */
446 /* --- The union operation isn't hard --- */
449 if (rnode
== clNode_immed
) {
450 if (type
& clType_user
) {
451 sym_find(&l
->v
.t
, (char *)&r
->v
.u
,
452 sizeof(r
->v
.u
), sizeof(sym_base
), 0);
454 sym_find(&l
->v
.t
, r
->v
.s
, -1, sizeof(sym_base
), 0);
459 for (sym_createIter(&i
, &r
->v
.t
); (b
= sym_next(&i
)) != 0; )
460 sym_find(&l
->v
.t
, b
->name
, b
->len
, sizeof(sym_base
), 0);
464 /* --- Set difference is similar in spirit --- */
467 if (rnode
== clNode_immed
) {
470 if (type
& clType_user
)
471 f
= sym_find(&l
->v
.t
, (char *)&r
->v
.u
, sizeof(r
->v
.u
), 0, 0);
473 f
= sym_find(&l
->v
.t
, r
->v
.s
, -1, 0, 0);
475 sym_remove(&l
->v
.t
, f
);
480 for (sym_createIter(&i
, &r
->v
.t
); (b
= sym_next(&i
)) != 0; ) {
481 if ((f
= sym_find(&l
->v
.t
, b
->name
, b
->len
, 0, 0)) != 0)
482 sym_remove(&l
->v
.t
, f
);
487 /* --- Intersection is wild and wacky --- */
490 if (rnode
== clNode_immed
) {
493 if (type
& clType_user
)
494 f
= sym_find(&l
->v
.t
, (char *)&r
->v
.u
, sizeof(r
->v
.u
), 0, 0);
496 f
= sym_find(&l
->v
.t
, r
->v
.s
, -1, 0, 0);
509 for (sym_createIter(&i
, &l
->v
.t
); (b
= sym_next(&i
)) != 0; ) {
510 if (!sym_find(&r
->v
.t
, b
->name
, b
->len
, 0, 0))
511 sym_remove(&l
->v
.t
, b
);
517 /* --- Now trim the @l@ table to size --- *
519 * It may have lost a load of elements. Maybe it can be represented
520 * better as an immediate or even as @class_none@.
529 sym_createIter(&i
, &l
->v
.t
);
530 if ((b
= sym_next(&i
)) == 0) {
535 if (type
& clType_user
) {
536 uid_t u
= *(uid_t
*)b
->name
;
537 sym_destroyTable(&l
->v
.t
);
538 l
->type
= (l
->type
& ~clNode_mask
) | clNode_immed
;
541 char *s
= xstrdup(b
->name
);
542 sym_destroyTable(&l
->v
.t
);
543 l
->type
= (l
->type
& ~clNode_mask
) | clNode_immed
;
554 /* --- Unfriendly nodes --- *
556 * Create a binop node and return that. If @l@ is a binop node, and @r@
557 * isn't, and the operation isn't set difference (i.e., it's commutative)
558 * then swap the two over to lessen the depth of recursion later.
562 class_node
*c
= xmalloc(sizeof(*c
));
566 if (lnode
>= clNode_binop
&& rnode
< clNode_binop
&& op
!= clNode_diff
) {
577 /* --- @class_union@ --- *
579 * Arguments: @class_node *l@ = left argument
580 * @class_node *r@ = right argument
582 * Returns: A class node representing the union of the two classes.
584 * Use: Performs the union operation on classes. If the types don't
585 * match, then a null pointer is returned. Both @l@ and @r@
586 * may be modified, and will be decremented before they get
587 * returned to you. If you don't want that to happen, ensure
588 * that you've claimed a reference to the original versions.
591 class_node
*class_union(class_node
*l
, class_node
*r
)
593 /* --- Check for the really simple cases --- */
595 if (l
== class_all
|| r
== class_all
) {
606 /* --- Do the job --- */
608 return (class__binop(l
, r
, clNode_union
));
611 /* --- @class_diff@ --- *
613 * Arguments: @class_node *l@ = left argument
614 * @class_node *r@ = right argument
616 * Returns: A class node representing the difference of the two classes.
618 * Use: Performs the set difference operation on classes. If the
619 * types don't match, then a null pointer is returned. Both
620 * @l@ and @r@ may be modified, and will be decremented before
621 * they get returned to you. If you don't want that to happen,
622 * ensure that you've claimed a reference to the original
626 class_node
*class_diff(class_node
*l
, class_node
*r
)
628 /* --- Check for the really simple cases --- */
630 if (l
== class_none
|| r
== class_all
) {
639 /* --- Do the job --- */
641 return (class__binop(l
, r
, clNode_diff
));
644 /* --- @class_isect@ --- *
646 * Arguments: @class_node *l@ = left argument
647 * @class_node *r@ = right argument
649 * Returns: A class node representing the intersection of the two
652 * Use: Performs the intersecion operation on classes. If the types
653 * don't match, then a null pointer is returned. Both @l@ and
654 * @r@ may be modified, and will be decremented before they get
655 * returned to you. If you don't want that to happen, ensure
656 * that you've claimed a reference to the original versions.
659 class_node
*class_isect(class_node
*l
, class_node
*r
)
661 /* --- Check for the really simple cases --- */
663 if (l
== class_none
|| r
== class_none
) {
674 /* --- Do the job --- */
676 return (class__binop(l
, r
, clNode_isect
));
679 /*----- Building the predefined classes -----------------------------------*/
681 /* --- @class_addUser@ --- *
683 * Arguments: @class_node *c@ = pointer to a class node (may be null)
684 * @uid_t u@ = user id number
686 * Returns: Pointer to the combined node.
688 * Use: Adds a user to a node, maybe hashifying it.
691 class_node
*class_addUser(class_node
*c
, uid_t u
)
694 return (class_fromUser(clType_user
, u
));
695 if ((c
->type
& clNode_mask
) == clNode_immed
)
696 c
= class__hashify(c
);
697 sym_find(&c
->v
.t
, (char *)&u
, sizeof(u
), sizeof(sym_base
), 0);
701 /* --- @class_addString@ --- *
703 * Arguments: @class_node *c@ = pointer to a class node (may be null)
704 * @const char *s@ = pointer to a string
706 * Returns: Pointer to the combined node.
708 * Use: Adds a user to a node, maybe hashifying it.
711 class_node
*class_addString(class_node
*c
, const char *s
)
713 class_node
*n
= class_fromString(clType_host
, s
); /* hack */
715 return (class_union(c
, n
));
720 /*----- Matching functions ------------------------------------------------*/
722 /* --- @class_matchUser@ --- *
724 * Arguments: @class_node *c@ = pointer to root class node
725 * @uid_t u@ = user id number
727 * Returns: Nonzero if it matches, zero if it doesn't.
729 * Use: Determines whether a user is matched by a class. Assumes
730 * that the types are correct.
733 int class_matchUser(class_node
*c
, uid_t u
)
737 for (cc
= 0; c
; c
= cc
, cc
= 0) {
742 switch (c
->type
& clNode_mask
) {
744 return (u
== c
->v
.u
);
747 return (sym_find(&c
->v
.t
, (char *)&u
, sizeof(u
), 0, 0) != 0);
750 if (class_matchUser(c
->v
.c
.l
, u
))
755 if (!class_matchUser(c
->v
.c
.l
, u
))
760 return (class_matchUser(c
->v
.c
.l
, u
) &&
761 !class_matchUser(c
->v
.c
.r
, u
));
766 die("internal error: can't get here in class_matchUser");
770 /* --- @class_matchCommand@ --- *
772 * Arguments: @class_node *c@ = pointer to root class node
773 * @const char *s@ = pointer to a string
775 * Returns: Nonzero if it matches, zero if it doesn't.
777 * Use: Determines whether a string is matched by a class. Assumes
778 * that the types are correct.
781 int class_matchCommand(class_node
*c
, const char *s
)
785 for (cc
= 0; c
; c
= cc
, cc
= 0) {
790 switch (c
->type
& clNode_mask
) {
792 return (class__wildMatch(c
->v
.s
, s
));
795 return (sym_find(&c
->v
.t
, s
, -1, 0, 0) != 0);
798 if (class_matchCommand(c
->v
.c
.l
, s
))
803 if (!class_matchCommand(c
->v
.c
.l
, s
))
808 return (class_matchCommand(c
->v
.c
.l
, s
) &&
809 !class_matchCommand(c
->v
.c
.r
, s
));
814 die("internal error: can't get here in class_matchCommand");
818 /* --- @class_matchHost@ --- *
820 * Arguments: @class_node *c@ = pointer to root class node
821 * @struct in_addr a@ = IP address to match
823 * Returns: Nonzero if it matches, zero if it doesn't.
825 * Use: Determines whether a host matches a host class. Assumes
826 * that the types are correct. The actual mechanism is a bit
827 * odd here, but I think this is the Right Thing. At each stage
828 * I try to match %%@/all/%% of the possible names for the host.
829 * Thus host `splat' with address 1.2.3.4 would fail to match
830 * the class "1.2.*" - "splat". This seems to be what the
831 * author intuitively expects. It's just a bit weird.
834 static int class__doMatchHost(class_node
*c
, const char *ip
,
835 const char *name
, char **aliases
)
839 for (cc
= 0; c
; c
= cc
, cc
= 0) {
844 switch (c
->type
& clNode_mask
) {
846 if ((ip
&& class__wildMatch(c
->v
.s
, ip
)) ||
847 (name
&& class__wildMatch(c
->v
.s
, name
)))
849 if (aliases
) for (; *aliases
; aliases
++) {
850 if (class__wildMatch(c
->v
.s
, *aliases
))
856 if ((ip
&& sym_find(&c
->v
.t
, ip
, -1, 0, 0)) ||
857 (name
&& sym_find(&c
->v
.t
, name
, -1, 0, 0)))
859 if (aliases
) for (; *aliases
; aliases
++) {
860 if (sym_find(&c
->v
.t
, *aliases
, -1, 0, 0))
866 if (class__doMatchHost(c
->v
.c
.l
, ip
, name
, aliases
))
871 if (!class__doMatchHost(c
->v
.c
.l
, ip
, name
, aliases
))
876 return (class__doMatchHost(c
->v
.c
.l
, ip
, name
, aliases
) &&
877 !class__doMatchHost(c
->v
.c
.r
, ip
, name
, aliases
));
882 die("internal error: can't get here in class_matchUser");
886 int class_matchHost(class_node
*c
, struct in_addr a
)
888 char *ip
, *name
, **aliases
;
892 if ((h
= gethostbyaddr((char *)&a
, sizeof(a
), AF_INET
)) != 0) {
894 aliases
= h
->h_aliases
;
900 return (class__doMatchHost(c
, ip
, name
, aliases
));
903 /*----- Debugging code ----------------------------------------------------*/
905 /* --- @class_dump@ --- *
907 * Argumemnts: @class_node *c@ = pointer to root node
908 * @int indent@ = indent depth
912 * Use: Dumps a class to the trace output.
915 void class_dump(class_node
*c
, int indent
)
919 static char *types
[] = {
927 static char *nodes
[] = {
934 "binop: intersection"
937 /* --- Handle some magical cases --- */
939 if (c
== class_all
) {
940 trace(TRACE_RULE
, "rule:%*s class ALL", indent
* 2, "");
943 if (c
== class_none
) {
944 trace(TRACE_RULE
, "rule:%*s class NONE", indent
* 2, "");
948 /* --- Dump basic type information --- */
950 trace(TRACE_RULE
, "rule:%*s type == [%s], node type == [%s]%s",
952 types
[c
->type
& clType_mask
],
953 nodes
[(c
->type
& clNode_mask
) >> 4],
954 (c
->type
& clFlag_friendly
) ?
" Friendly" : "");
956 /* --- Now trace the contents --- */
958 switch (c
->type
& clNode_mask
) {
960 if (c
->type
& clType_user
) {
961 trace(TRACE_RULE
, "rule:%*s user %lu",
962 indent
* 2, "", (unsigned long)c
->v
.u
);
964 trace(TRACE_RULE
, "rule:%*s `%s'", indent
* 2, "", c
->v
.s
);
970 for (sym_createIter(&i
, &c
->v
.t
); (b
= sym_next(&i
)) != 0; ) {
971 if (c
->type
& clType_user
) {
972 trace(TRACE_RULE
, "rule:%*s user %lu",
973 indent
* 2, "", (unsigned long)*(uid_t
*)b
->name
);
975 trace(TRACE_RULE
, "rule:%*s `%s'", indent
* 2, "", b
->name
);
981 class_dump(c
->v
.c
.l
, indent
+ 1);
982 class_dump(c
->v
.c
.r
, indent
+ 1);
989 /*----- That's all, folks -------------------------------------------------*/