c4f2d992 |
1 | /* -*-c-*- |
2 | * |
aa5871cd |
3 | * $Id: class.h,v 1.5 1998/04/23 13:22:44 mdw Exp $ |
c4f2d992 |
4 | * |
5 | * Handling classes of things nicely |
6 | * |
c758e654 |
7 | * (c) 1998 EBI |
c4f2d992 |
8 | */ |
9 | |
03f996bd |
10 | /*----- Licensing notice --------------------------------------------------* |
c4f2d992 |
11 | * |
12 | * This file is part of `become' |
13 | * |
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. |
18 | * |
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. |
23 | * |
24 | * You should have received a copy of the GNU General Public License |
03f996bd |
25 | * along with `become'; if not, write to the Free Software Foundation, |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
c4f2d992 |
27 | */ |
28 | |
29 | /*----- Revision history --------------------------------------------------* |
30 | * |
31 | * $Log: class.h,v $ |
aa5871cd |
32 | * Revision 1.5 1998/04/23 13:22:44 mdw |
33 | * Fix value of clNode_binop, required for bcquery. |
34 | * |
c758e654 |
35 | * Revision 1.4 1998/01/12 16:45:53 mdw |
36 | * Fix copyright date. |
37 | * |
38 | * Revision 1.3 1997/09/17 10:14:56 mdw |
50a87c1f |
39 | * Complete rewrite to support class trees. Makes the behaviour of the set |
40 | * operators much more logical. |
41 | * |
03f996bd |
42 | * Revision 1.2 1997/08/04 10:24:21 mdw |
43 | * Sources placed under CVS control. |
44 | * |
45 | * Revision 1.1 1997/07/21 13:47:52 mdw |
c4f2d992 |
46 | * Initial revision |
47 | * |
48 | */ |
49 | |
50 | #ifndef CLASS_H |
51 | #define CLASS_H |
52 | |
53 | #ifdef __cplusplus |
54 | extern "C" { |
55 | #endif |
56 | |
57 | /*----- Required headers --------------------------------------------------*/ |
58 | |
59 | #include <sys/types.h> |
60 | #include <sys/socket.h> |
61 | #include <netinet/in.h> |
62 | #include <arpa/inet.h> |
63 | |
64 | #ifndef SYM_H |
65 | # include "sym.h" |
66 | #endif |
67 | |
68 | /*----- Data structures ---------------------------------------------------*/ |
69 | |
70 | /* --- Class types --- */ |
71 | |
72 | enum { |
50a87c1f |
73 | |
74 | /* --- Data types (user visible) --- * |
75 | * |
76 | * These bits encode what the user can think of as `types'. They get used |
77 | * for typechecking and things. |
78 | */ |
79 | |
80 | clType_user = 0x01, /* Class of users */ |
81 | clType_command = 0x02, /* Class of command strings */ |
82 | clType_host = 0x04, /* Class of host names */ |
83 | clType_all = 0x0F, /* All of the above (maintain me) */ |
84 | clType_mask = 0x0F, /* Mask for type flags */ |
85 | |
86 | /* --- Node types --- * |
87 | * |
88 | * A class actually looks suspiciously like a tree once things start |
89 | * getting complicated. These bits encode the type of node we've got here. |
90 | */ |
91 | |
92 | clNode_any = 0x10, /* Magic type for the `all' class */ |
93 | clNode_immed = 0x20, /* Immediate data item */ |
94 | clNode_hash = 0x30, /* Hashtable of values */ |
aa5871cd |
95 | clNode_binop = 0x40, /* Binary operations start here */ |
50a87c1f |
96 | clNode_union = 0x40, /* Union of two classes */ |
50a87c1f |
97 | clNode_diff = 0x50, /* Difference of two classes */ |
98 | clNode_isect = 0x60, /* Intersection of two classes */ |
99 | clNode_mask = 0xF0, /* Mask for picking these out */ |
100 | |
101 | /* --- Other useful flags --- */ |
102 | |
103 | clFlag_friendly = 0x100 /* Item can be hashed with others */ |
c4f2d992 |
104 | }; |
105 | |
106 | /* --- Class block --- */ |
107 | |
50a87c1f |
108 | typedef struct class_node { |
109 | unsigned type; /* Class and node type, and flags */ |
c4f2d992 |
110 | unsigned ref; /* Reference count */ |
50a87c1f |
111 | union { |
112 | uid_t u; /* Immediate user id number */ |
113 | char *s; /* Immediate string value */ |
114 | sym_table t; /* Hash table for this class */ |
115 | struct { |
116 | struct class_node *l, *r; /* Left and right operands */ |
117 | } c; /* Class operands for binops */ |
118 | } v; /* Value of this class node */ |
119 | } class_node; |
c4f2d992 |
120 | |
121 | /*----- Global variables --------------------------------------------------*/ |
122 | |
50a87c1f |
123 | extern class_node *class_all; /* The match-everything class */ |
124 | extern class_node *class_none; /* The match-nothing class */ |
c4f2d992 |
125 | |
126 | /*----- Functions provided ------------------------------------------------*/ |
127 | |
50a87c1f |
128 | /* --- @class_fromString@ --- * |
c4f2d992 |
129 | * |
50a87c1f |
130 | * Arguments: @unsigned type@ = a type field |
131 | * @const char *s@ = pointer to string to copy |
c4f2d992 |
132 | * |
50a87c1f |
133 | * Returns: A pointer to a class node containing that string, typed to |
134 | * be the right thing. |
c4f2d992 |
135 | * |
50a87c1f |
136 | * Use: Given a string, wrap a class node around it. The node has |
137 | * one reference (the one you get returned). The string is |
138 | * copied, so you can get rid of your original one if you like. |
c4f2d992 |
139 | */ |
140 | |
50a87c1f |
141 | extern class_node *class_fromString(unsigned /*type*/, const char */*s*/); |
142 | |
143 | /* --- @class_fromUser@ --- * |
144 | * |
145 | * Arguments: @unsigned type@ = a type field |
146 | * @uid_t u@ = a user-id number |
147 | * |
148 | * Returns: A pointer to a class node containing the uid, typed to be |
149 | * the thing you asked for. Hopefully this will be |
150 | * @clType_user@. |
151 | * |
152 | * Use: Given a uid, wrap a class node around it. |
153 | */ |
154 | |
155 | extern class_node *class_fromUser(unsigned /*type*/, uid_t /*u*/); |
c4f2d992 |
156 | |
157 | /* --- @class_inc@ --- * |
158 | * |
50a87c1f |
159 | * Arguments: @class_node *c@ = pointer to a class block |
c4f2d992 |
160 | * |
161 | * Returns: --- |
162 | * |
163 | * Use: Adds a reference to the class definition. |
164 | */ |
165 | |
50a87c1f |
166 | extern void class_inc(class_node */*c*/); |
c4f2d992 |
167 | |
168 | /* --- @class_dec@ --- * |
169 | * |
50a87c1f |
170 | * Arguments: @class_node *c@ = pointer to a class block |
c4f2d992 |
171 | * |
172 | * Returns: --- |
173 | * |
174 | * Use: Removes a reference to a class block. |
175 | */ |
176 | |
50a87c1f |
177 | extern void class_dec(class_node */*c*/); |
178 | |
179 | /* --- @class_mod@ --- * |
180 | * |
181 | * Arguments: @class_node *c@ = pointer to a class node |
182 | * |
183 | * Returns: A pointer to a class node, maybe the same one, maybe not, |
184 | * with a reference count of 1, containing the same data. |
185 | * |
186 | * Use: Gives you a node which you can modify. Don't call this |
187 | * for @class_all@ or @class_none@. |
188 | */ |
189 | |
190 | extern class_node *class_mod(class_node */*c*/); |
191 | |
192 | /* --- @class_union@ --- * |
193 | * |
194 | * Arguments: @class_node *l@ = left argument |
195 | * @class_node *r@ = right argument |
196 | * |
197 | * Returns: A class node representing the union of the two classes. |
198 | * |
199 | * Use: Performs the union operation on classes. If the types don't |
200 | * match, then a null pointer is returned. Both @l@ and @r@ |
201 | * may be modified, and will be decremented before they get |
202 | * returned to you. If you don't want that to happen, ensure |
203 | * that you've claimed a reference to the original versions. |
204 | */ |
205 | |
206 | extern class_node *class_union(class_node */*l*/, class_node */*r*/); |
207 | |
208 | /* --- @class_diff@ --- * |
209 | * |
210 | * Arguments: @class_node *l@ = left argument |
211 | * @class_node *r@ = right argument |
212 | * |
213 | * Returns: A class node representing the difference of the two classes. |
214 | * |
215 | * Use: Performs the set difference operation on classes. If the |
216 | * types don't match, then a null pointer is returned. Both |
217 | * @l@ and @r@ may be modified, and will be decremented before |
218 | * they get returned to you. If you don't want that to happen, |
219 | * ensure that you've claimed a reference to the original |
220 | * versions. |
221 | */ |
222 | |
223 | extern class_node *class_diff(class_node */*l*/, class_node */*r*/); |
224 | |
225 | /* --- @class_isect@ --- * |
226 | * |
227 | * Arguments: @class_node *l@ = left argument |
228 | * @class_node *r@ = right argument |
229 | * |
230 | * Returns: A class node representing the intersection of the two |
231 | * classes. |
232 | * |
233 | * Use: Performs the intersecion operation on classes. If the types |
234 | * don't match, then a null pointer is returned. Both @l@ and |
235 | * @r@ may be modified, and will be decremented before they get |
236 | * returned to you. If you don't want that to happen, ensure |
237 | * that you've claimed a reference to the original versions. |
238 | */ |
239 | |
240 | extern class_node *class_isect(class_node */*l*/, class_node */*r*/); |
241 | |
242 | /* --- @class_addUser@ --- * |
243 | * |
244 | * Arguments: @class_node *c@ = pointer to a class node (may be null) |
245 | * @uid_t u@ = user id number |
246 | * |
247 | * Returns: Pointer to the combined node. |
248 | * |
249 | * Use: Adds a user to a node, maybe hashifying it. |
250 | */ |
251 | |
252 | extern class_node *class_addUser(class_node */*c*/, uid_t /*u*/); |
253 | |
254 | /* --- @class_addString@ --- * |
255 | * |
256 | * Arguments: @class_node *c@ = pointer to a class node (may be null) |
257 | * @const char *s@ = pointer to a string |
258 | * |
259 | * Returns: Pointer to the combined node. |
260 | * |
261 | * Use: Adds a user to a node, maybe hashifying it. |
262 | */ |
263 | |
264 | extern class_node *class_addString(class_node */*c*/, const char */*s*/); |
c4f2d992 |
265 | |
50a87c1f |
266 | /* --- @class_matchUser@ --- * |
c4f2d992 |
267 | * |
50a87c1f |
268 | * Arguments: @class_node *c@ = pointer to root class node |
269 | * @uid_t u@ = user id number |
c4f2d992 |
270 | * |
50a87c1f |
271 | * Returns: Nonzero if it matches, zero if it doesn't. |
c4f2d992 |
272 | * |
50a87c1f |
273 | * Use: Determines whether a user is matched by a class. Assumes |
274 | * that the types are correct. |
c4f2d992 |
275 | */ |
276 | |
50a87c1f |
277 | extern int class_matchUser(class_node */*c*/, uid_t /*u*/); |
c4f2d992 |
278 | |
50a87c1f |
279 | /* --- @class_matchCommand@ --- * |
c4f2d992 |
280 | * |
50a87c1f |
281 | * Arguments: @class_node *c@ = pointer to root class node |
282 | * @const char *s@ = pointer to a string |
c4f2d992 |
283 | * |
50a87c1f |
284 | * Returns: Nonzero if it matches, zero if it doesn't. |
c4f2d992 |
285 | * |
50a87c1f |
286 | * Use: Determines whether a string is matched by a class. Assumes |
287 | * that the types are correct. |
c4f2d992 |
288 | */ |
289 | |
50a87c1f |
290 | extern int class_matchCommand(class_node */*c*/, const char */*s*/); |
c4f2d992 |
291 | |
50a87c1f |
292 | /* --- @class_matchHost@ --- * |
c4f2d992 |
293 | * |
50a87c1f |
294 | * Arguments: @class_node *c@ = pointer to root class node |
295 | * @struct in_addr a@ = IP address to match |
c4f2d992 |
296 | * |
50a87c1f |
297 | * Returns: Nonzero if it matches, zero if it doesn't. |
c4f2d992 |
298 | * |
50a87c1f |
299 | * Use: Determines whether a host matches a host class. Assumes |
300 | * that the types are correct. The actual mechanism is a bit |
301 | * odd here, but I think this is the Right Thing. At each stage |
302 | * I try to match %%@/all/%% of the possible names for the host. |
303 | * Thus host `splat' with address 1.2.3.4 would fail to match |
304 | * the class "1.2.*" - "splat". This seems to be what the |
305 | * author intuitively expects. It's just a bit weird. |
c4f2d992 |
306 | */ |
307 | |
50a87c1f |
308 | extern int class_matchHost(class_node */*c*/, struct in_addr /*a*/); |
c4f2d992 |
309 | |
310 | /* --- @class_dump@ --- * |
311 | * |
50a87c1f |
312 | * Argumemnts: @class_node *c@ = pointer to root node |
313 | * @int indent@ = indent depth |
c4f2d992 |
314 | * |
315 | * Returns: --- |
316 | * |
50a87c1f |
317 | * Use: Dumps a class to the trace output. |
c4f2d992 |
318 | */ |
319 | |
50a87c1f |
320 | extern void class_dump(class_node */*c*/, int /*indent*/); |
c4f2d992 |
321 | |
322 | /*----- That's all, folks -------------------------------------------------*/ |
323 | |
324 | #ifdef __cplusplus |
325 | } |
326 | #endif |
327 | |
328 | #endif |
329 | |