3 * $Id: class.h,v 1.7 2004/04/08 01:36:20 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.
36 /*----- Required headers --------------------------------------------------*/
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
45 /*----- Data structures ---------------------------------------------------*/
47 /* --- Class types --- */
51 /* --- Data types (user visible) --- *
53 * These bits encode what the user can think of as `types'. They get used
54 * for typechecking and things.
57 clType_user
= 0x01, /* Class of users */
58 clType_command
= 0x02, /* Class of command strings */
59 clType_host
= 0x04, /* Class of host names */
60 clType_all
= 0x0F, /* All of the above (maintain me) */
61 clType_mask
= 0x0F, /* Mask for type flags */
63 /* --- Node types --- *
65 * A class actually looks suspiciously like a tree once things start
66 * getting complicated. These bits encode the type of node we've got here.
69 clNode_any
= 0x10, /* Magic type for the `all' class */
70 clNode_immed
= 0x20, /* Immediate data item */
71 clNode_hash
= 0x30, /* Hashtable of values */
72 clNode_binop
= 0x40, /* Binary operations start here */
73 clNode_union
= 0x40, /* Union of two classes */
74 clNode_diff
= 0x50, /* Difference of two classes */
75 clNode_isect
= 0x60, /* Intersection of two classes */
76 clNode_mask
= 0xF0, /* Mask for picking these out */
78 /* --- Other useful flags --- */
80 clFlag_friendly
= 0x100 /* Item can be hashed with others */
83 /* --- Class block --- */
85 typedef struct class_node
{
86 unsigned type
; /* Class and node type, and flags */
87 unsigned ref
; /* Reference count */
89 uid_t u
; /* Immediate user id number */
90 char *s
; /* Immediate string value */
91 sym_table t
; /* Hash table for this class */
93 struct class_node
*l
, *r
; /* Left and right operands */
94 } c
; /* Class operands for binops */
95 } v
; /* Value of this class node */
98 /*----- Global variables --------------------------------------------------*/
100 extern class_node
*class_all
; /* The match-everything class */
101 extern class_node
*class_none
; /* The match-nothing class */
103 /*----- Functions provided ------------------------------------------------*/
105 /* --- @class_fromString@ --- *
107 * Arguments: @unsigned type@ = a type field
108 * @const char *s@ = pointer to string to copy
110 * Returns: A pointer to a class node containing that string, typed to
111 * be the right thing.
113 * Use: Given a string, wrap a class node around it. The node has
114 * one reference (the one you get returned). The string is
115 * copied, so you can get rid of your original one if you like.
118 extern class_node
*class_fromString(unsigned /*type*/, const char */
*s*/
);
120 /* --- @class_fromUser@ --- *
122 * Arguments: @unsigned type@ = a type field
123 * @uid_t u@ = a user-id number
125 * Returns: A pointer to a class node containing the uid, typed to be
126 * the thing you asked for. Hopefully this will be
129 * Use: Given a uid, wrap a class node around it.
132 extern class_node
*class_fromUser(unsigned /*type*/, uid_t
/*u*/);
134 /* --- @class_inc@ --- *
136 * Arguments: @class_node *c@ = pointer to a class block
140 * Use: Adds a reference to the class definition.
143 extern void class_inc(class_node */
*c*/
);
145 /* --- @class_dec@ --- *
147 * Arguments: @class_node *c@ = pointer to a class block
151 * Use: Removes a reference to a class block.
154 extern void class_dec(class_node */
*c*/
);
156 /* --- @class_mod@ --- *
158 * Arguments: @class_node *c@ = pointer to a class node
160 * Returns: A pointer to a class node, maybe the same one, maybe not,
161 * with a reference count of 1, containing the same data.
163 * Use: Gives you a node which you can modify. Don't call this
164 * for @class_all@ or @class_none@.
167 extern class_node
*class_mod(class_node */
*c*/
);
169 /* --- @class_union@ --- *
171 * Arguments: @class_node *l@ = left argument
172 * @class_node *r@ = right argument
174 * Returns: A class node representing the union of the two classes.
176 * Use: Performs the union operation on classes. If the types don't
177 * match, then a null pointer is returned. Both @l@ and @r@
178 * may be modified, and will be decremented before they get
179 * returned to you. If you don't want that to happen, ensure
180 * that you've claimed a reference to the original versions.
183 extern class_node
*class_union(class_node */
*l*/
, class_node */
*r*/
);
185 /* --- @class_diff@ --- *
187 * Arguments: @class_node *l@ = left argument
188 * @class_node *r@ = right argument
190 * Returns: A class node representing the difference of the two classes.
192 * Use: Performs the set difference operation on classes. If the
193 * types don't match, then a null pointer is returned. Both
194 * @l@ and @r@ may be modified, and will be decremented before
195 * they get returned to you. If you don't want that to happen,
196 * ensure that you've claimed a reference to the original
200 extern class_node
*class_diff(class_node */
*l*/
, class_node */
*r*/
);
202 /* --- @class_isect@ --- *
204 * Arguments: @class_node *l@ = left argument
205 * @class_node *r@ = right argument
207 * Returns: A class node representing the intersection of the two
210 * Use: Performs the intersecion operation on classes. If the types
211 * don't match, then a null pointer is returned. Both @l@ and
212 * @r@ may be modified, and will be decremented before they get
213 * returned to you. If you don't want that to happen, ensure
214 * that you've claimed a reference to the original versions.
217 extern class_node
*class_isect(class_node */
*l*/
, class_node */
*r*/
);
219 /* --- @class_addUser@ --- *
221 * Arguments: @class_node *c@ = pointer to a class node (may be null)
222 * @uid_t u@ = user id number
224 * Returns: Pointer to the combined node.
226 * Use: Adds a user to a node, maybe hashifying it.
229 extern class_node
*class_addUser(class_node */
*c*/
, uid_t
/*u*/);
231 /* --- @class_addString@ --- *
233 * Arguments: @class_node *c@ = pointer to a class node (may be null)
234 * @const char *s@ = pointer to a string
236 * Returns: Pointer to the combined node.
238 * Use: Adds a user to a node, maybe hashifying it.
241 extern class_node
*class_addString(class_node */
*c*/
, const char */
*s*/
);
243 /* --- @class_matchUser@ --- *
245 * Arguments: @class_node *c@ = pointer to root class node
246 * @uid_t u@ = user id number
248 * Returns: Nonzero if it matches, zero if it doesn't.
250 * Use: Determines whether a user is matched by a class. Assumes
251 * that the types are correct.
254 extern int class_matchUser(class_node */
*c*/
, uid_t
/*u*/);
256 /* --- @class_matchCommand@ --- *
258 * Arguments: @class_node *c@ = pointer to root class node
259 * @const char *s@ = pointer to a string
261 * Returns: Nonzero if it matches, zero if it doesn't.
263 * Use: Determines whether a string is matched by a class. Assumes
264 * that the types are correct.
267 extern int class_matchCommand(class_node */
*c*/
, const char */
*s*/
);
269 /* --- @class_matchHost@ --- *
271 * Arguments: @class_node *c@ = pointer to root class node
272 * @struct in_addr a@ = IP address to match
274 * Returns: Nonzero if it matches, zero if it doesn't.
276 * Use: Determines whether a host matches a host class. Assumes
277 * that the types are correct. The actual mechanism is a bit
278 * odd here, but I think this is the Right Thing. At each stage
279 * I try to match %%@/all/%% of the possible names for the host.
280 * Thus host `splat' with address 1.2.3.4 would fail to match
281 * the class "1.2.*" - "splat". This seems to be what the
282 * author intuitively expects. It's just a bit weird.
285 extern int class_matchHost(class_node */
*c*/
, struct in_addr
/*a*/);
287 /* --- @class_dump@ --- *
289 * Argumemnts: @class_node *c@ = pointer to root node
290 * @int indent@ = indent depth
294 * Use: Dumps a class to the trace output.
297 extern void class_dump(class_node */
*c*/
, int /*indent*/);
299 /*----- That's all, folks -------------------------------------------------*/