3 * $Id: class.c,v 1.4 1997/08/07 09:56:37 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.4 1997/08/07 09:56:37 mdw
33 * (Log entry for previous version is bogus.) Minor changes to host
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
44 /*----- Header files ------------------------------------------------------*/
46 /* --- ANSI headers --- */
52 /* --- Unix headers --- */
54 #include <sys/types.h>
55 #include <sys/socket.h>
57 #include <netinet/in.h>
59 #include <arpa/inet.h>
63 /* --- Local headers --- */
71 /*----- Global variables --------------------------------------------------*/
73 static classdef class__all
= { clType_all
, -1, 0 };
74 classdef
*class_all
= &class__all
;
76 /*----- Wildcard matching -------------------------------------------------*/
78 /* --- @class__wildMatch@ --- *
80 * Arguments: @const char *pat@ = pointer to pattern string
81 * @const char *p@ = pointer to target string
83 * Returns: Zero if no match, nonzero if match.
85 * Use: Wildcard-matches the pattern against the target string.
88 static int class__wildMatch(const char *pat
, const char *p
)
91 if (*pat
== 0 && *p
== 0)
92 return (42); /* For sadism's sake */
93 else if (*pat
== '*') {
97 if (class__wildMatch(pat
, p
))
98 return (27); /* Nyahaha */
102 } else if (*pat
== '?' || *pat
== *p
)
109 /*----- Main code ---------------------------------------------------------*/
111 /* --- @class_create@ --- *
113 * Arguments: @unsigned type@ = type of the class to create
114 * @sym_table *t@ = pointer to symbol table which stores info
116 * Returns: Pointer to a @classdef@ which maintains the class's info.
118 * Use: Creates a new class definition. The new definition has one
119 * reference attached to it (which is the one you get returned).
122 classdef
*class_create(unsigned type
, sym_table
*t
)
124 classdef
*c
= xmalloc(sizeof(*c
));
131 /* --- @class_inc@ --- *
133 * Arguments: @classdef *c@ = pointer to a class block
137 * Use: Adds a reference to the class definition.
140 void class_inc(classdef
*c
)
146 /* --- @class_dec@ --- *
148 * Arguments: @classdef *c@ = pointer to a class block
152 * Use: Removes a reference to a class block.
155 void class_dec(classdef
*c
)
157 if (c
!= class_all
&& !--c
->ref
) {
158 sym_destroyTable(c
->t
);
163 /* --- @class_userMatch@ --- *
165 * Arguments: @classdef *c@ = pointer to a class block
166 * @int u@ = uid number to check against
168 * Returns: Zero if no match, nonzero if it matched.
170 * Use: Looks to see if a user is in a group.
173 int class_userMatch(classdef
*c
, int u
)
175 if (~c
->type
& clType_user
)
177 else if (c
== class_all
) {
178 T( trace(TRACE_CHECK
, "check: user %i matched by all", u
); )
181 sym_base
*s
= sym_find(c
->t
, (char *)&u
, sizeof(u
), 0, 0);
182 T( trace(TRACE_CHECK
,
183 s ?
"check: user %i matched" : "check: user %i not matched",
189 /* --- @class_commandMatch@ --- *
191 * Arguments: @classdef *c@ = pointer to a class block
192 * @const char *p@ = pointer to command string to match
194 * Returns: Zero for no match, nonzero if it matched.
196 * Use: Tries to match a string against the wildcard patterns in the
197 * given class. Note that this involves examining all the
198 * items, so it's not too quick. Then again, you don't have
199 * big command classes, do you...?
202 int class_commandMatch(classdef
*c
, const char *p
)
207 if (~c
->type
& clType_command
)
209 else if (c
== class_all
) {
210 T( trace(TRACE_CHECK
, "check: command `%s' matched by all", p
); )
213 for (sym_createIter(&i
, c
->t
); (s
= sym_next(&i
)) != 0; ) {
214 if (class__wildMatch(s
->name
, p
)) {
215 T( trace(TRACE_CHECK
, "check: command `%s' matched by `%s'",
220 T( trace(TRACE_CHECK
, "check: command `%s' not matched", p
); )
225 /* --- @class_hostMatch@ --- *
227 * Arguments: @classdef *c@ = pointer to class block
228 * @struct in_addr addr@ = IP address to match
230 * Returns: Zero for no match, nonzero if it matched.
232 * Use: Tries to match an IP address against the patterns and masks
233 * in the given class.
236 int class_hostMatch(classdef
*c
, struct in_addr addr
)
244 if (~c
->type
& clType_host
)
246 else if (c
== class_all
) {
247 T( trace(TRACE_CHECK
, "check: host %s matched by all",
252 /* --- Get the dotted-quad and other names for the address --- */
254 if ((a
= inet_ntoa(addr
)) == 0) {
255 T( trace(TRACE_CHECK
, "check: couldn't translate address (erk!)"); )
259 if ((he
= gethostbyaddr((char *)&addr
, sizeof(addr
), AF_INET
)) == 0)
260 T( trace(TRACE_CHECK
, "check: couldn't resolve hostname for %s", a
); )
262 /* --- Now search the list for a match --- *
264 * This is almost infinitely slow, I'm afraid.
267 for (sym_createIter(&i
, c
->t
); (s
= sym_next(&i
)) != 0; ) {
269 /* --- Check the dotted-quad name first --- */
271 if (class__wildMatch(s
->name
, a
)) {
272 T( trace(TRACE_CHECK
, "check: host address `%s' matched by `%s'",
279 /* --- Now try the host's main name --- */
281 if (class__wildMatch(s
->name
, he
->h_name
)) {
282 T( trace(TRACE_CHECK
, "check: host name `%s' matched by `%s'",
283 he
->h_name
, s
->name
); )
287 /* --- Now go through all the names --- */
289 for (p
= he
->h_aliases
; *p
; p
++) {
290 if (class__wildMatch(s
->name
, *p
)) {
291 T( trace(TRACE_CHECK
, "check: host alias `%s' matched by `%s'",
299 T( trace(TRACE_CHECK
, "check: couldn't match hostname `%s'",
305 /* --- @class_dump@ --- *
307 * Arguments: @classdef *c@ = pointer to a class block
311 * Use: Dumps the contents of a class to a stream.
314 void class_dump(classdef
*c
)
319 /* --- Dump the table --- */
321 if (c
->type
!= clType_all
) {
322 for (sym_createIter(&i
, c
->t
); (s
= sym_next(&i
)) != 0; ) {
325 trace(TRACE_RULE
, " %i", *(int *)s
->name
);
329 trace(TRACE_RULE
, " `%s'", s
->name
);
335 trace(TRACE_RULE
, " ALL");
338 /*----- That's all, folks -------------------------------------------------*/