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