3 * $Id: class.c,v 1.7 1998/01/12 16:45:50 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.7 1998/01/12 16:45:50 mdw
35 * Revision 1.6 1997/09/17 10:14:56 mdw
36 * Complete rewrite to support class trees. Makes the behaviour of the set
37 * operators much more logical.
39 * Revision 1.5 1997/08/20 16:16:13 mdw
40 * Patch memory leak. Don't try to trace when tracing's turned off.
42 * Revision 1.4 1997/08/07 09:56:37 mdw
43 * (Log entry for previous version is bogus.) Minor changes to host
46 * Revision 1.2 1997/08/04 10:24:21 mdw
47 * Sources placed under CVS control.
49 * Revision 1.1 1997/07/21 13:47:52 mdw
54 /*----- Header files ------------------------------------------------------*/
56 /* --- ANSI headers --- */
62 /* --- Unix headers --- */
64 #include <sys/types.h>
65 #include <sys/socket.h>
67 #include <netinet/in.h>
69 #include <arpa/inet.h>
73 /* --- Local headers --- */
80 /*----- Global variables --------------------------------------------------*/
82 static class_node class__all
= { clType_all
| clNode_any
, -1, { 0 }};
83 class_node
*class_all
= &class__all
;
85 static class_node class__none
= { clType_all
| clNode_any
, -1, { 0 }};
86 class_node
*class_none
= &class__none
;
88 /*----- Wildcard matching -------------------------------------------------*/
90 /* --- @class__wildMatch@ --- *
92 * Arguments: @const char *pat@ = pointer to pattern string
93 * @const char *p@ = pointer to target string
95 * Returns: Zero if no match, nonzero if match.
97 * Use: Wildcard-matches the pattern against the target string.
100 static int class__wildMatch(const char *pat
, const char *p
)
103 if (*pat
== 0 && *p
== 0)
104 return (42); /* For sadism's sake */
105 else if (*pat
== '*') {
109 if (class__wildMatch(pat
, p
))
110 return (27); /* Nyahaha */
113 return (pat
[1] == 0);
114 } else if (*pat
== '?' || *pat
== *p
)
121 /*----- Creating new class nodes ------------------------------------------*/
123 /* --- @class_fromString@ --- *
125 * Arguments: @unsigned type@ = a type field
126 * @const char *s@ = pointer to string to copy
128 * Returns: A pointer to a class node containing that string, typed to
129 * be the right thing.
131 * Use: Given a string, wrap a class node around it. The node has
132 * one reference (the one you get returned). The string is
133 * copied, so you can get rid of your original one if you like.
136 class_node
*class_fromString(unsigned type
, const char *s
)
138 class_node
*c
= xmalloc(sizeof(*c
));
139 c
->type
= type
| clNode_immed
;
140 if (s
[strcspn(s
, "*?")] == 0)
141 c
->type
|= clFlag_friendly
;
147 /* --- @class_fromUser@ --- *
149 * Arguments: @unsigned type@ = a type field
150 * @uid_t u@ = a user-id number
152 * Returns: A pointer to a class node containing the uid, typed to be
153 * the thing you asked for. Hopefully this will be
156 * Use: Given a uid, wrap a class node around it.
159 class_node
*class_fromUser(unsigned type
, uid_t u
)
161 class_node
*c
= xmalloc(sizeof(*c
));
162 c
->type
= type
| clNode_immed
| clFlag_friendly
;
168 /*----- Reference counter tricks ------------------------------------------*/
170 /* --- @class_inc@ --- *
172 * Arguments: @class_node *c@ = pointer to a class block
176 * Use: Adds a reference to the class definition.
179 void class_inc(class_node
*c
)
181 if (c
!= class_all
&& c
!= class_none
)
185 /* --- @class_dec@ --- *
187 * Arguments: @class_node *c@ = pointer to a class block
191 * Use: Removes a reference to a class block.
194 void class_dec(class_node
*c
)
198 for (cc
= 0; c
; c
= cc
, cc
= 0) {
199 if (c
== class_all
|| c
== class_none
|| --c
->ref
)
201 switch (c
->type
& clNode_mask
) {
206 if (c
->type
& (clType_host
| clType_command
))
210 sym_destroyTable(&c
->v
.t
);
223 /* --- @class_mod@ --- *
225 * Arguments: @class_node *c@ = pointer to a class node
227 * Returns: A pointer to a class node, maybe the same one, maybe not,
228 * with a reference count of 1, containing the same data.
230 * Use: Gives you a node which you can modify. Don't call this
231 * for @class_all@ or @class_none@.
234 class_node
*class_mod(class_node
*c
)
241 cc
= xmalloc(sizeof(*cc
));
244 switch (c
->type
& clNode_mask
) {
246 die("internal error: class_mod called on non-modifiable class node");
250 if (c
->type
& clType_user
)
253 cc
->v
.s
= xstrdup(c
->v
.s
);
260 sym_createTable(&cc
->v
.t
);
261 for (sym_createIter(&i
, &c
->v
.t
); (b
= sym_next(&i
)) != 0; )
262 sym_find(&cc
->v
.t
, b
->name
, b
->len
, sizeof(sym_base
), 0);
268 cc
->v
.c
.l
= c
->v
.c
.l
;
269 cc
->v
.c
.r
= c
->v
.c
.r
;
270 class_inc(cc
->v
.c
.l
);
271 class_inc(cc
->v
.c
.r
);
279 /*----- Some weirder operations on classes --------------------------------*/
281 /* --- @class__hashify@ --- *
283 * Arguments: @class_node *c@ = pointer to a node
285 * Returns: A pointer to a hash node containing the node's value.
287 * Use: The original node must have type `immediate', and it must
288 * be friendly. The old reference is discarded -- you get this
292 static class_node
*class__hashify(class_node
*c
)
294 /* --- Some sanity checking --- */
296 if (~c
->type
& clFlag_friendly
)
297 die("internal error: class__hashify can't hashify unfriendly nodes");
298 if ((c
->type
& clNode_mask
) != clNode_immed
)
299 die("internal error: class__hashify can't hashify non-immediate nodes");
301 /* --- Split off a private copy of the node --- */
305 c
->type
= (c
->type
& clType_mask
) | clNode_hash
| clFlag_friendly
;
307 if (c
->type
& clType_user
) {
309 sym_createTable(&c
->v
.t
);
310 sym_find(&c
->v
.t
, (char *)&u
, sizeof(u
), sizeof(sym_base
), 0);
313 sym_createTable(&c
->v
.t
);
314 sym_find(&c
->v
.t
, s
, -1, sizeof(sym_base
), 0);
321 /*----- Arithmetic on classes ---------------------------------------------*/
323 /* --- @class__binop@ --- *
325 * Arguments: @class_node *l@ = left argument
326 * @class_node *r@ = right argument
327 * @unsigned op@ = the binop code
329 * Returns: A class node representing the result of a binary operation
332 * Use: Performs a binary operation on classes. If the types don't
333 * match, then a null pointer is returned. Both @l@ and @r@
334 * may be modified, and will be decremented before they get
335 * returned to you. If you don't want that to happen, ensure
336 * that you've claimed a reference to the original versions.
338 * If both nodes are `friendly' (see below), then an attempt is
339 * made to combine them sensibly using a hashtable.
341 * If one or both nodes is/are unfriendly, a binop node is
342 * created with type @op@ to allow the matcher to decide on
343 * membership appropriately at match time.
345 * A friendly node is one which can be placed in a hash table to
346 * speed up searching. It's friendly, because it doesn't mind
347 * living with other nodes. Uid numbers are friendly.
348 * Wildcarded strings aren't. Hashtables are trivially
352 class_node
*class__binop(class_node
*l
, class_node
*r
, int op
)
354 unsigned type
= l
->type
& r
->type
& clType_mask
;
355 unsigned lnode
= l
->type
& clNode_mask
, rnode
= r
->type
& clNode_mask
;
357 /* --- Check for compatible types --- */
365 /* --- Handle `friendly' nodes --- */
367 if ((l
->type
& r
->type
& clFlag_friendly
)) {
369 /* --- Consider promoting an item to a hash --- *
371 * If both are immediate nodes, and they're both `friendly', we can
372 * profitably hash them together.
374 * Life gets complicated when we do subtraction, because it's not
375 * commutative. In this case, I have to promote the left operand anyway.
378 if (lnode
== clNode_immed
&&
379 (rnode
== clNode_immed
|| op
== clNode_diff
)) {
381 /* --- Quick check for triviality --- *
383 * There are some more short-cuts I can employ if the values are
387 if (rnode
== clNode_immed
) {
389 /* --- See whether the two items are equal --- */
391 int eq
= (type
& clType_user ?
392 l
->v
.u
== r
->v
.u
: strcmp(l
->v
.s
, r
->v
.s
) == 0);
394 /* --- Now do something appropriate --- */
423 /* --- Turn @l@ into a hash containing itself --- */
425 l
= class__hashify(l
);
429 /* --- Otherwise, make @l@ the hash --- *
431 * Both @l@ and @r@ are friendly. Since they're not both immediates,
432 * one must be a hash.
435 else if ((l
->type
& clNode_mask
) == clNode_immed
) {
439 tn
= l
, l
= r
, r
= tn
;
440 tt
= lnode
, lnode
= rnode
, rnode
= tt
;
443 /* --- Now merge @r@ with @l@ --- */
449 /* --- The union operation isn't hard --- */
452 if (rnode
== clNode_immed
) {
453 if (type
& clType_user
) {
454 sym_find(&l
->v
.t
, (char *)&r
->v
.u
,
455 sizeof(r
->v
.u
), sizeof(sym_base
), 0);
457 sym_find(&l
->v
.t
, r
->v
.s
, -1, sizeof(sym_base
), 0);
462 for (sym_createIter(&i
, &r
->v
.t
); (b
= sym_next(&i
)) != 0; )
463 sym_find(&l
->v
.t
, b
->name
, b
->len
, sizeof(sym_base
), 0);
467 /* --- Set difference is similar in spirit --- */
470 if (rnode
== clNode_immed
) {
473 if (type
& clType_user
)
474 f
= sym_find(&l
->v
.t
, (char *)&r
->v
.u
, sizeof(r
->v
.u
), 0, 0);
476 f
= sym_find(&l
->v
.t
, r
->v
.s
, -1, 0, 0);
478 sym_remove(&l
->v
.t
, f
);
483 for (sym_createIter(&i
, &r
->v
.t
); (b
= sym_next(&i
)) != 0; ) {
484 if ((f
= sym_find(&l
->v
.t
, b
->name
, b
->len
, 0, 0)) != 0)
485 sym_remove(&l
->v
.t
, f
);
490 /* --- Intersection is wild and wacky --- */
493 if (rnode
== clNode_immed
) {
496 if (type
& clType_user
)
497 f
= sym_find(&l
->v
.t
, (char *)&r
->v
.u
, sizeof(r
->v
.u
), 0, 0);
499 f
= sym_find(&l
->v
.t
, r
->v
.s
, -1, 0, 0);
512 for (sym_createIter(&i
, &l
->v
.t
); (b
= sym_next(&i
)) != 0; ) {
513 if (!sym_find(&r
->v
.t
, b
->name
, b
->len
, 0, 0))
514 sym_remove(&l
->v
.t
, b
);
520 /* --- Now trim the @l@ table to size --- *
522 * It may have lost a load of elements. Maybe it can be represented
523 * better as an immediate or even as @class_none@.
532 sym_createIter(&i
, &l
->v
.t
);
533 if ((b
= sym_next(&i
)) == 0) {
538 if (type
& clType_user
) {
539 uid_t u
= *(uid_t
*)b
->name
;
540 sym_destroyTable(&l
->v
.t
);
541 l
->type
= (l
->type
& ~clNode_mask
) | clNode_immed
;
544 char *s
= xstrdup(b
->name
);
545 sym_destroyTable(&l
->v
.t
);
546 l
->type
= (l
->type
& ~clNode_mask
) | clNode_immed
;
557 /* --- Unfriendly nodes --- *
559 * Create a binop node and return that. If @l@ is a binop node, and @r@
560 * isn't, and the operation isn't set difference (i.e., it's commutative)
561 * then swap the two over to lessen the depth of recursion later.
565 class_node
*c
= xmalloc(sizeof(*c
));
569 if (lnode
>= clNode_binop
&& rnode
< clNode_binop
&& op
!= clNode_diff
) {
580 /* --- @class_union@ --- *
582 * Arguments: @class_node *l@ = left argument
583 * @class_node *r@ = right argument
585 * Returns: A class node representing the union of the two classes.
587 * Use: Performs the union operation on classes. If the types don't
588 * match, then a null pointer is returned. Both @l@ and @r@
589 * may be modified, and will be decremented before they get
590 * returned to you. If you don't want that to happen, ensure
591 * that you've claimed a reference to the original versions.
594 class_node
*class_union(class_node
*l
, class_node
*r
)
596 /* --- Check for the really simple cases --- */
598 if (l
== class_all
|| r
== class_all
) {
609 /* --- Do the job --- */
611 return (class__binop(l
, r
, clNode_union
));
614 /* --- @class_diff@ --- *
616 * Arguments: @class_node *l@ = left argument
617 * @class_node *r@ = right argument
619 * Returns: A class node representing the difference of the two classes.
621 * Use: Performs the set difference operation on classes. If the
622 * types don't match, then a null pointer is returned. Both
623 * @l@ and @r@ may be modified, and will be decremented before
624 * they get returned to you. If you don't want that to happen,
625 * ensure that you've claimed a reference to the original
629 class_node
*class_diff(class_node
*l
, class_node
*r
)
631 /* --- Check for the really simple cases --- */
633 if (l
== class_none
|| r
== class_all
) {
642 /* --- Do the job --- */
644 return (class__binop(l
, r
, clNode_diff
));
647 /* --- @class_isect@ --- *
649 * Arguments: @class_node *l@ = left argument
650 * @class_node *r@ = right argument
652 * Returns: A class node representing the intersection of the two
655 * Use: Performs the intersecion operation on classes. If the types
656 * don't match, then a null pointer is returned. Both @l@ and
657 * @r@ may be modified, and will be decremented before they get
658 * returned to you. If you don't want that to happen, ensure
659 * that you've claimed a reference to the original versions.
662 class_node
*class_isect(class_node
*l
, class_node
*r
)
664 /* --- Check for the really simple cases --- */
666 if (l
== class_none
|| r
== class_none
) {
677 /* --- Do the job --- */
679 return (class__binop(l
, r
, clNode_isect
));
682 /*----- Building the predefined classes -----------------------------------*/
684 /* --- @class_addUser@ --- *
686 * Arguments: @class_node *c@ = pointer to a class node (may be null)
687 * @uid_t u@ = user id number
689 * Returns: Pointer to the combined node.
691 * Use: Adds a user to a node, maybe hashifying it.
694 class_node
*class_addUser(class_node
*c
, uid_t u
)
697 return (class_fromUser(clType_user
, u
));
698 if ((c
->type
& clNode_mask
) == clNode_immed
)
699 c
= class__hashify(c
);
700 sym_find(&c
->v
.t
, (char *)&u
, sizeof(u
), sizeof(sym_base
), 0);
704 /* --- @class_addString@ --- *
706 * Arguments: @class_node *c@ = pointer to a class node (may be null)
707 * @const char *s@ = pointer to a string
709 * Returns: Pointer to the combined node.
711 * Use: Adds a user to a node, maybe hashifying it.
714 class_node
*class_addString(class_node
*c
, const char *s
)
716 class_node
*n
= class_fromString(clType_host
, s
); /* hack */
718 return (class_union(c
, n
));
723 /*----- Matching functions ------------------------------------------------*/
725 /* --- @class_matchUser@ --- *
727 * Arguments: @class_node *c@ = pointer to root class node
728 * @uid_t u@ = user id number
730 * Returns: Nonzero if it matches, zero if it doesn't.
732 * Use: Determines whether a user is matched by a class. Assumes
733 * that the types are correct.
736 int class_matchUser(class_node
*c
, uid_t u
)
740 for (cc
= 0; c
; c
= cc
, cc
= 0) {
745 switch (c
->type
& clNode_mask
) {
747 return (u
== c
->v
.u
);
750 return (sym_find(&c
->v
.t
, (char *)&u
, sizeof(u
), 0, 0) != 0);
753 if (class_matchUser(c
->v
.c
.l
, u
))
758 if (!class_matchUser(c
->v
.c
.l
, u
))
763 return (class_matchUser(c
->v
.c
.l
, u
) &&
764 !class_matchUser(c
->v
.c
.r
, u
));
769 die("internal error: can't get here in class_matchUser");
773 /* --- @class_matchCommand@ --- *
775 * Arguments: @class_node *c@ = pointer to root class node
776 * @const char *s@ = pointer to a string
778 * Returns: Nonzero if it matches, zero if it doesn't.
780 * Use: Determines whether a string is matched by a class. Assumes
781 * that the types are correct.
784 int class_matchCommand(class_node
*c
, const char *s
)
788 for (cc
= 0; c
; c
= cc
, cc
= 0) {
793 switch (c
->type
& clNode_mask
) {
795 return (class__wildMatch(c
->v
.s
, s
));
798 return (sym_find(&c
->v
.t
, s
, -1, 0, 0) != 0);
801 if (class_matchCommand(c
->v
.c
.l
, s
))
806 if (!class_matchCommand(c
->v
.c
.l
, s
))
811 return (class_matchCommand(c
->v
.c
.l
, s
) &&
812 !class_matchCommand(c
->v
.c
.r
, s
));
817 die("internal error: can't get here in class_matchCommand");
821 /* --- @class_matchHost@ --- *
823 * Arguments: @class_node *c@ = pointer to root class node
824 * @struct in_addr a@ = IP address to match
826 * Returns: Nonzero if it matches, zero if it doesn't.
828 * Use: Determines whether a host matches a host class. Assumes
829 * that the types are correct. The actual mechanism is a bit
830 * odd here, but I think this is the Right Thing. At each stage
831 * I try to match %%@/all/%% of the possible names for the host.
832 * Thus host `splat' with address 1.2.3.4 would fail to match
833 * the class "1.2.*" - "splat". This seems to be what the
834 * author intuitively expects. It's just a bit weird.
837 static int class__doMatchHost(class_node
*c
, const char *ip
,
838 const char *name
, char **aliases
)
842 for (cc
= 0; c
; c
= cc
, cc
= 0) {
847 switch (c
->type
& clNode_mask
) {
849 if ((ip
&& class__wildMatch(c
->v
.s
, ip
)) ||
850 (name
&& class__wildMatch(c
->v
.s
, name
)))
852 if (aliases
) for (; *aliases
; aliases
++) {
853 if (class__wildMatch(c
->v
.s
, *aliases
))
859 if ((ip
&& sym_find(&c
->v
.t
, ip
, -1, 0, 0)) ||
860 (name
&& sym_find(&c
->v
.t
, name
, -1, 0, 0)))
862 if (aliases
) for (; *aliases
; aliases
++) {
863 if (sym_find(&c
->v
.t
, *aliases
, -1, 0, 0))
869 if (class__doMatchHost(c
->v
.c
.l
, ip
, name
, aliases
))
874 if (!class__doMatchHost(c
->v
.c
.l
, ip
, name
, aliases
))
879 return (class__doMatchHost(c
->v
.c
.l
, ip
, name
, aliases
) &&
880 !class__doMatchHost(c
->v
.c
.r
, ip
, name
, aliases
));
885 die("internal error: can't get here in class_matchUser");
889 int class_matchHost(class_node
*c
, struct in_addr a
)
891 char *ip
, *name
, **aliases
;
895 if ((h
= gethostbyaddr((char *)&a
, sizeof(a
), AF_INET
)) != 0) {
897 aliases
= h
->h_aliases
;
903 return (class__doMatchHost(c
, ip
, name
, aliases
));
906 /*----- Debugging code ----------------------------------------------------*/
908 /* --- @class_dump@ --- *
910 * Argumemnts: @class_node *c@ = pointer to root node
911 * @int indent@ = indent depth
915 * Use: Dumps a class to the trace output.
918 void class_dump(class_node
*c
, int indent
)
922 static char *types
[] = {
930 static char *nodes
[] = {
937 "binop: intersection"
940 /* --- Handle some magical cases --- */
942 if (c
== class_all
) {
943 trace(TRACE_RULE
, "rule:%*s class ALL", indent
* 2, "");
946 if (c
== class_none
) {
947 trace(TRACE_RULE
, "rule:%*s class NONE", indent
* 2, "");
951 /* --- Dump basic type information --- */
953 trace(TRACE_RULE
, "rule:%*s type == [%s], node type == [%s]%s",
955 types
[c
->type
& clType_mask
],
956 nodes
[(c
->type
& clNode_mask
) >> 4],
957 (c
->type
& clFlag_friendly
) ?
" Friendly" : "");
959 /* --- Now trace the contents --- */
961 switch (c
->type
& clNode_mask
) {
963 if (c
->type
& clType_user
) {
964 trace(TRACE_RULE
, "rule:%*s user %lu",
965 indent
* 2, "", (unsigned long)c
->v
.u
);
967 trace(TRACE_RULE
, "rule:%*s `%s'", indent
* 2, "", c
->v
.s
);
973 for (sym_createIter(&i
, &c
->v
.t
); (b
= sym_next(&i
)) != 0; ) {
974 if (c
->type
& clType_user
) {
975 trace(TRACE_RULE
, "rule:%*s user %lu",
976 indent
* 2, "", (unsigned long)*(uid_t
*)b
->name
);
978 trace(TRACE_RULE
, "rule:%*s `%s'", indent
* 2, "", b
->name
);
984 class_dump(c
->v
.c
.l
, indent
+ 1);
985 class_dump(c
->v
.c
.r
, indent
+ 1);
992 /*----- That's all, folks -------------------------------------------------*/