7a7d016281fd9126efc8791f01035fe547eb20b7
3 * $Id: class.c,v 1.2 1997/08/04 10:24:21 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.2 1997/08/04 10:24:21 mdw
33 * Sources placed under CVS control.
35 * Revision 1.1 1997/07/21 13:47:52 mdw
40 /*----- Header files ------------------------------------------------------*/
42 /* --- ANSI headers --- */
48 /* --- Unix headers --- */
50 #include <sys/types.h>
51 #include <sys/socket.h>
53 #include <netinet/in.h>
55 #include <arpa/inet.h>
59 /* --- Local headers --- */
67 /*----- Global variables --------------------------------------------------*/
69 static classdef class__all
= { clType_all
, -1, 0 };
70 classdef
*class_all
= &class__all
;
72 /*----- Wildcard matching -------------------------------------------------*/
74 /* --- @class__wildMatch@ --- *
76 * Arguments: @const char *pat@ = pointer to pattern string
77 * @const char *p@ = pointer to target string
79 * Returns: Zero if no match, nonzero if match.
81 * Use: Wildcard-matches the pattern against the target string.
84 static int class__wildMatch(const char *pat
, const char *p
)
87 if (*pat
== 0 && *p
== 0)
88 return (42); /* For sadism's sake */
89 else if (*pat
== '*') {
93 if (class__wildMatch(pat
, p
))
94 return (27); /* Nyahaha */
98 } else if (*pat
== '?' || *pat
== *p
)
105 /*----- Main code ---------------------------------------------------------*/
107 /* --- @class_create@ --- *
109 * Arguments: @unsigned type@ = type of the class to create
110 * @sym_table *t@ = pointer to symbol table which stores info
112 * Returns: Pointer to a @classdef@ which maintains the class's info.
114 * Use: Creates a new class definition. The new definition has one
115 * reference attached to it (which is the one you get returned).
118 classdef
*class_create(unsigned type
, sym_table
*t
)
120 classdef
*c
= xmalloc(sizeof(*c
));
127 /* --- @class_inc@ --- *
129 * Arguments: @classdef *c@ = pointer to a class block
133 * Use: Adds a reference to the class definition.
136 void class_inc(classdef
*c
)
142 /* --- @class_dec@ --- *
144 * Arguments: @classdef *c@ = pointer to a class block
148 * Use: Removes a reference to a class block.
151 void class_dec(classdef
*c
)
153 if (c
!= class_all
&& !--c
->ref
) {
154 sym_destroyTable(c
->t
);
159 /* --- @class_userMatch@ --- *
161 * Arguments: @classdef *c@ = pointer to a class block
162 * @int u@ = uid number to check against
164 * Returns: Zero if no match, nonzero if it matched.
166 * Use: Looks to see if a user is in a group.
169 int class_userMatch(classdef
*c
, int u
)
171 if (~c
->type
& clType_user
)
173 else if (c
== class_all
) {
174 T( trace(TRACE_CHECK
, "check: user %i matched by all", u
); )
177 sym_base
*s
= sym_find(c
->t
, (char *)&u
, sizeof(u
), 0, 0);
178 T( trace(TRACE_CHECK
,
179 s ?
"check: user %i matched" : "check: user %i not matched",
185 /* --- @class_commandMatch@ --- *
187 * Arguments: @classdef *c@ = pointer to a class block
188 * @const char *p@ = pointer to command string to match
190 * Returns: Zero for no match, nonzero if it matched.
192 * Use: Tries to match a string against the wildcard patterns in the
193 * given class. Note that this involves examining all the
194 * items, so it's not too quick. Then again, you don't have
195 * big command classes, do you...?
198 int class_commandMatch(classdef
*c
, const char *p
)
203 if (~c
->type
& clType_command
)
205 else if (c
== class_all
) {
206 T( trace(TRACE_CHECK
, "check: command `%s' matched by all", p
); )
209 for (sym_createIter(&i
, c
->t
); (s
= sym_next(&i
)) != 0; ) {
210 if (class__wildMatch(s
->name
, p
)) {
211 T( trace(TRACE_CHECK
, "check: command `%s' matched by `%s'",
216 T( trace(TRACE_CHECK
, "check: command `%s' not matched", p
); )
221 /* --- @class_hostMatch@ --- *
223 * Arguments: @classdef *c@ = pointer to class block
224 * @struct in_addr addr@ = IP address to match
226 * Returns: Zero for no match, nonzero if it matched.
228 * Use: Tries to match an IP address against the patterns and masks
229 * in the given class.
232 int class_hostMatch(classdef
*c
, struct in_addr addr
)
240 if (~c
->type
& clType_host
)
242 else if (c
== class_all
) {
243 T( trace(TRACE_CHECK
, "check: host %s matched by all",
248 /* --- Get the dotted-quad and other names for the address --- */
250 if ((a
= inet_ntoa(addr
)) == 0) {
251 T( trace(TRACE_CHECK
, "check: couldn't translate address (erk!)"); )
254 if ((he
= gethostbyaddr((char *)&addr
, sizeof(addr
), AF_INET
)) == 0) {
255 T( trace(TRACE_CHECK
, "check: couldn't resolve hostname for %s", a
); )
259 /* --- Now search the list for a match --- *
261 * This is almost infinitely slow, I'm afraid.
264 for (sym_createIter(&i
, c
->t
); (s
= sym_next(&i
)) != 0; ) {
266 /* --- Check the dotted-quad name first --- */
268 if (class__wildMatch(s
->name
, a
)) {
269 T( trace(TRACE_CHECK
, "check: host address `%s' matched by `%s'",
274 /* --- Now try the host's main name --- */
276 if (class__wildMatch(s
->name
, he
->h_name
)) {
277 T( trace(TRACE_CHECK
, "check: host name `%s' matched by `%s'",
278 he
->h_name
, s
->name
); )
282 /* --- Now go through all the names --- */
284 for (p
= he
->h_aliases
; *p
; p
++) {
285 if (class__wildMatch(s
->name
, *p
)) {
286 T( trace(TRACE_CHECK
, "check: host alias `%s' matched by `%s'",
293 T( trace(TRACE_CHECK
, "check: couldn't match hostname `%s'",
299 /* --- @class_dump@ --- *
301 * Arguments: @classdef *c@ = pointer to a class block
305 * Use: Dumps the contents of a class to a stream.
308 void class_dump(classdef
*c
)
313 /* --- Dump the table --- */
315 if (c
->type
!= clType_all
) {
316 for (sym_createIter(&i
, c
->t
); (s
= sym_next(&i
)) != 0; ) {
319 trace(TRACE_RULE
, " %i", *(int *)s
->name
);
323 trace(TRACE_RULE
, " `%s'", s
->name
);
329 trace(TRACE_RULE
, " ALL");
332 /*----- That's all, folks -------------------------------------------------*/