3 * $Id: class.h,v 1.3 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.3 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.2 1997/08/04 10:24:21 mdw
37 * Sources placed under CVS control.
39 * Revision 1.1 1997/07/21 13:47:52 mdw
51 /*----- Required headers --------------------------------------------------*/
53 #include <sys/types.h>
54 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
62 /*----- Data structures ---------------------------------------------------*/
64 /* --- Class types --- */
68 /* --- Data types (user visible) --- *
70 * These bits encode what the user can think of as `types'. They get used
71 * for typechecking and things.
74 clType_user
= 0x01, /* Class of users */
75 clType_command
= 0x02, /* Class of command strings */
76 clType_host
= 0x04, /* Class of host names */
77 clType_all
= 0x0F, /* All of the above (maintain me) */
78 clType_mask
= 0x0F, /* Mask for type flags */
80 /* --- Node types --- *
82 * A class actually looks suspiciously like a tree once things start
83 * getting complicated. These bits encode the type of node we've got here.
86 clNode_any
= 0x10, /* Magic type for the `all' class */
87 clNode_immed
= 0x20, /* Immediate data item */
88 clNode_hash
= 0x30, /* Hashtable of values */
89 clNode_union
= 0x40, /* Union of two classes */
90 clNode_binop
= 0x50, /* Binary operations start here */
91 clNode_diff
= 0x50, /* Difference of two classes */
92 clNode_isect
= 0x60, /* Intersection of two classes */
93 clNode_mask
= 0xF0, /* Mask for picking these out */
95 /* --- Other useful flags --- */
97 clFlag_friendly
= 0x100 /* Item can be hashed with others */
100 /* --- Class block --- */
102 typedef struct class_node
{
103 unsigned type
; /* Class and node type, and flags */
104 unsigned ref
; /* Reference count */
106 uid_t u
; /* Immediate user id number */
107 char *s
; /* Immediate string value */
108 sym_table t
; /* Hash table for this class */
110 struct class_node
*l
, *r
; /* Left and right operands */
111 } c
; /* Class operands for binops */
112 } v
; /* Value of this class node */
115 /*----- Global variables --------------------------------------------------*/
117 extern class_node
*class_all
; /* The match-everything class */
118 extern class_node
*class_none
; /* The match-nothing class */
120 /*----- Functions provided ------------------------------------------------*/
122 /* --- @class_fromString@ --- *
124 * Arguments: @unsigned type@ = a type field
125 * @const char *s@ = pointer to string to copy
127 * Returns: A pointer to a class node containing that string, typed to
128 * be the right thing.
130 * Use: Given a string, wrap a class node around it. The node has
131 * one reference (the one you get returned). The string is
132 * copied, so you can get rid of your original one if you like.
135 extern class_node
*class_fromString(unsigned /*type*/, const char */
*s*/
);
137 /* --- @class_fromUser@ --- *
139 * Arguments: @unsigned type@ = a type field
140 * @uid_t u@ = a user-id number
142 * Returns: A pointer to a class node containing the uid, typed to be
143 * the thing you asked for. Hopefully this will be
146 * Use: Given a uid, wrap a class node around it.
149 extern class_node
*class_fromUser(unsigned /*type*/, uid_t
/*u*/);
151 /* --- @class_inc@ --- *
153 * Arguments: @class_node *c@ = pointer to a class block
157 * Use: Adds a reference to the class definition.
160 extern void class_inc(class_node */
*c*/
);
162 /* --- @class_dec@ --- *
164 * Arguments: @class_node *c@ = pointer to a class block
168 * Use: Removes a reference to a class block.
171 extern void class_dec(class_node */
*c*/
);
173 /* --- @class_mod@ --- *
175 * Arguments: @class_node *c@ = pointer to a class node
177 * Returns: A pointer to a class node, maybe the same one, maybe not,
178 * with a reference count of 1, containing the same data.
180 * Use: Gives you a node which you can modify. Don't call this
181 * for @class_all@ or @class_none@.
184 extern class_node
*class_mod(class_node */
*c*/
);
186 /* --- @class_union@ --- *
188 * Arguments: @class_node *l@ = left argument
189 * @class_node *r@ = right argument
191 * Returns: A class node representing the union of the two classes.
193 * Use: Performs the union operation on classes. If the types don't
194 * match, then a null pointer is returned. Both @l@ and @r@
195 * may be modified, and will be decremented before they get
196 * returned to you. If you don't want that to happen, ensure
197 * that you've claimed a reference to the original versions.
200 extern class_node
*class_union(class_node */
*l*/
, class_node */
*r*/
);
202 /* --- @class_diff@ --- *
204 * Arguments: @class_node *l@ = left argument
205 * @class_node *r@ = right argument
207 * Returns: A class node representing the difference of the two classes.
209 * Use: Performs the set difference operation on classes. If the
210 * types don't match, then a null pointer is returned. Both
211 * @l@ and @r@ may be modified, and will be decremented before
212 * they get returned to you. If you don't want that to happen,
213 * ensure that you've claimed a reference to the original
217 extern class_node
*class_diff(class_node */
*l*/
, class_node */
*r*/
);
219 /* --- @class_isect@ --- *
221 * Arguments: @class_node *l@ = left argument
222 * @class_node *r@ = right argument
224 * Returns: A class node representing the intersection of the two
227 * Use: Performs the intersecion operation on classes. If the types
228 * don't match, then a null pointer is returned. Both @l@ and
229 * @r@ may be modified, and will be decremented before they get
230 * returned to you. If you don't want that to happen, ensure
231 * that you've claimed a reference to the original versions.
234 extern class_node
*class_isect(class_node */
*l*/
, class_node */
*r*/
);
236 /* --- @class_addUser@ --- *
238 * Arguments: @class_node *c@ = pointer to a class node (may be null)
239 * @uid_t u@ = user id number
241 * Returns: Pointer to the combined node.
243 * Use: Adds a user to a node, maybe hashifying it.
246 extern class_node
*class_addUser(class_node */
*c*/
, uid_t
/*u*/);
248 /* --- @class_addString@ --- *
250 * Arguments: @class_node *c@ = pointer to a class node (may be null)
251 * @const char *s@ = pointer to a string
253 * Returns: Pointer to the combined node.
255 * Use: Adds a user to a node, maybe hashifying it.
258 extern class_node
*class_addString(class_node */
*c*/
, const char */
*s*/
);
260 /* --- @class_matchUser@ --- *
262 * Arguments: @class_node *c@ = pointer to root class node
263 * @uid_t u@ = user id number
265 * Returns: Nonzero if it matches, zero if it doesn't.
267 * Use: Determines whether a user is matched by a class. Assumes
268 * that the types are correct.
271 extern int class_matchUser(class_node */
*c*/
, uid_t
/*u*/);
273 /* --- @class_matchCommand@ --- *
275 * Arguments: @class_node *c@ = pointer to root class node
276 * @const char *s@ = pointer to a string
278 * Returns: Nonzero if it matches, zero if it doesn't.
280 * Use: Determines whether a string is matched by a class. Assumes
281 * that the types are correct.
284 extern int class_matchCommand(class_node */
*c*/
, const char */
*s*/
);
286 /* --- @class_matchHost@ --- *
288 * Arguments: @class_node *c@ = pointer to root class node
289 * @struct in_addr a@ = IP address to match
291 * Returns: Nonzero if it matches, zero if it doesn't.
293 * Use: Determines whether a host matches a host class. Assumes
294 * that the types are correct. The actual mechanism is a bit
295 * odd here, but I think this is the Right Thing. At each stage
296 * I try to match %%@/all/%% of the possible names for the host.
297 * Thus host `splat' with address 1.2.3.4 would fail to match
298 * the class "1.2.*" - "splat". This seems to be what the
299 * author intuitively expects. It's just a bit weird.
302 extern int class_matchHost(class_node */
*c*/
, struct in_addr
/*a*/);
304 /* --- @class_dump@ --- *
306 * Argumemnts: @class_node *c@ = pointer to root node
307 * @int indent@ = indent depth
311 * Use: Dumps a class to the trace output.
314 extern void class_dump(class_node */
*c*/
, int /*indent*/);
316 /*----- That's all, folks -------------------------------------------------*/