3 * $Id: class.c,v 1.1 1997/07/21 13:47:52 mdw Exp $
5 * Handling classes of things nicely
10 /*----- Licencing 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
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 1997/07/21 13:47:52 mdw
37 /*----- Header files ------------------------------------------------------*/
39 /* --- ANSI headers --- */
45 /* --- Unix headers --- */
47 #include <sys/types.h>
48 #include <sys/socket.h>
50 #include <netinet/in.h>
52 #include <arpa/inet.h>
56 /* --- Local headers --- */
63 /*----- Global variables --------------------------------------------------*/
65 static classdef class__all = { clType_all, -1, 0 };
66 classdef *class_all = &class__all;
68 /*----- Wildcard matching -------------------------------------------------*/
70 /* --- @class__wildMatch@ --- *
72 * Arguments: @const char *pat@ = pointer to pattern string
73 * @const char *p@ = pointer to target string
75 * Returns: Zero if no match, nonzero if match.
77 * Use: Wildcard-matches the pattern against the target string.
80 static int class__wildMatch(const char *pat, const char *p)
83 if (*pat == 0 && *p == 0)
84 return (42); /* For sadism's sake */
85 else if (*pat == '*') {
89 if (class__wildMatch(pat, p))
90 return (27); /* Nyahaha */
94 } else if (*pat == '?' || *pat == *p)
101 /*----- Main code ---------------------------------------------------------*/
103 /* --- @class_create@ --- *
105 * Arguments: @unsigned type@ = type of the class to create
106 * @sym_table *t@ = pointer to symbol table which stores info
108 * Returns: Pointer to a @classdef@ which maintains the class's info.
110 * Use: Creates a new class definition. The new definition has one
111 * reference attached to it (which is the one you get returned).
114 classdef *class_create(unsigned type, sym_table *t)
116 classdef *c = xmalloc(sizeof(*c));
123 /* --- @class_inc@ --- *
125 * Arguments: @classdef *c@ = pointer to a class block
129 * Use: Adds a reference to the class definition.
132 void class_inc(classdef *c)
138 /* --- @class_dec@ --- *
140 * Arguments: @classdef *c@ = pointer to a class block
144 * Use: Removes a reference to a class block.
147 void class_dec(classdef *c)
149 if (c != class_all && !--c->ref) {
150 sym_destroyTable(c->t);
155 /* --- @class_userMatch@ --- *
157 * Arguments: @classdef *c@ = pointer to a class block
158 * @int u@ = uid number to check against
160 * Returns: Zero if no match, nonzero if it matched.
162 * Use: Looks to see if a user is in a group.
165 int class_userMatch(classdef *c, int u)
167 if (~c->type & clType_user)
169 else if (c == class_all)
172 return (sym_find(c->t, (char *)&u, sizeof(u), 0, 0) != 0);
175 /* --- @class_commandMatch@ --- *
177 * Arguments: @classdef *c@ = pointer to a class block
178 * @const char *p@ = pointer to command string to match
180 * Returns: Zero for no match, nonzero if it matched.
182 * Use: Tries to match a string against the wildcard patterns in the
183 * given class. Note that this involves examining all the
184 * items, so it's not too quick. Then again, you don't have
185 * big command classes, do you...?
188 int class_commandMatch(classdef *c, const char *p)
193 if (~c->type & clType_command)
195 else if (c == class_all)
198 for (sym_createIter(&i, c->t); (s = sym_next(&i)) != 0; ) {
199 if (class__wildMatch(s->name, p))
206 /* --- @class_hostMatch@ --- *
208 * Arguments: @classdef *c@ = pointer to class block
209 * @struct in_addr addr@ = IP address to match
211 * Returns: Zero for no match, nonzero if it matched.
213 * Use: Tries to match an IP address against the patterns and masks
214 * in the given class.
217 int class_hostMatch(classdef *c, struct in_addr addr)
225 if (~c->type & clType_host)
227 else if (c == class_all)
231 /* --- Get the dotted-quad and other names for the address --- */
233 if ((a = inet_ntoa(addr)) == 0)
235 if ((he = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET)) == 0)
238 /* --- Now search the list for a match --- *
240 * This is almost infinitely slow, I'm afraid.
243 for (sym_createIter(&i, c->t); (s = sym_next(&i)) != 0; ) {
245 /* --- Check the dotted-quad name first --- */
247 if (class__wildMatch(s->name, a))
250 /* --- Now try the host's main name --- */
252 if (class__wildMatch(s->name, he->h_name))
255 /* --- Now go through all the names --- */
257 for (p = he->h_aliases; *p; p++) {
258 if (class__wildMatch(s->name, *p))
266 /* --- @class_dump@ --- *
268 * Arguments: @classdef *c@ = pointer to a class block
269 * @FILE *fp@ = pointer to a stream object
273 * Use: Dumps the contents of a class to a stream.
276 void class_dump(classdef *c, FILE *fp)
281 /* --- Write a little header block --- */
284 const char *typetbl[] = {
300 c->type < clType__limit ? typetbl[c->type] : "bad class",
304 /* --- Dump the table --- */
306 if (c->type != clType_all) {
307 for (sym_createIter(&i, c->t); (s = sym_next(&i)) != 0; ) {
310 fprintf(fp, "*** %i\n", *(int *)s->name);
315 fwrite(s->name, s->len, 1, fp);
325 /*----- That's all, folks -------------------------------------------------*/