3 * $Id: name.c,v 1.4 1997/08/20 16:17:59 mdw Exp $
5 * Looking up of names in symbol tables
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/20 16:17:59 mdw
33 * Replace `name_reinit' by `name_end' for more sensible restart.
35 * Revision 1.3 1997/08/07 09:49:39 mdw
36 * Extensive modifications to handle netgroups. Also sanitise user and group
37 * names before adding them to the symbol table.
39 * Revision 1.2 1997/08/04 10:24:24 mdw
40 * Sources placed under CVS control.
42 * Revision 1.1 1997/07/21 13:47:46 mdw
47 /*----- Header files ------------------------------------------------------*/
49 /* --- ANSI headers --- */
56 /* --- Unix headers --- */
58 #include <sys/types.h>
59 #include <sys/socket.h>
61 #include <netinet/in.h>
63 #include <arpa/inet.h>
69 /* --- Local headers --- */
81 /*----- Static variables --------------------------------------------------*/
83 static sym_table name__table; /* Symbol table for everything */
85 /*----- Main code ---------------------------------------------------------*/
87 /* --- @name__get@ --- *
89 * Arguments: @const char *p@ = pointer to the name we want
90 * @unsigned type@ = type of class it should have
92 * Returns: A pointer to a name ready to use, or zero if there's a type
95 * Use: Creates a name of the appropriate type all ready to use.
98 static name *name__get(const char *p, unsigned type)
101 name *n = sym_find(&name__table, p, -1, sizeof(*n), &f);
103 sym_table *t = xmalloc(sizeof(*t));
105 n->c = class_create(type, t);
107 return (n->c->type == type ? n : 0);
110 /* --- @name__sanitise@ --- *
112 * Arguments: @const char *n@ = pointer to a name
113 * @char *buf@ = pointer to a buffer of space
114 * @size_t sz@ = size of the buffer
116 * Returns: A pointer to the transformed name in the buffer, or null
117 * if there wasn't enough space.
119 * Use: Transforms a name so that it only contains nice characters.
122 static char *name__sanitise(const char *n, char *buf, size_t sz)
126 if (strlen(n) + 1 > sz)
130 if (isalnum((unsigned char)*n))
140 /* --- @name__users@ --- *
146 * Use: Adds all of the users registered with the user database to
147 * the name table. Also adds the users' primary groups.
150 static void name__users(void)
156 userdb_iterateUsers();
157 while ((pw = userdb_nextUser()) != 0) {
161 /* --- First, add the user to the table --- */
163 if (name__sanitise(pw->pw_name, buf, sizeof(buf)) &&
164 (n = name__get(buf, clType_user)) != 0)
165 sym_find(n->c->t, (char *)&u, sizeof(u), sizeof(sym_base), 0);
167 /* --- Now handle the user's default group --- */
169 if ((gr = userdb_groupById(pw->pw_gid)) != 0 &&
170 name__sanitise(gr->gr_name, buf, sizeof(buf)) &&
171 (n = name__get(buf, clType_user)) != 0)
172 sym_find(n->c->t, (char *)&u, sizeof(u), sizeof(sym_base), 0);
176 /* --- @name__groups@ --- *
182 * Use: Adds users into all of their supplementary groups.
185 static void name__groups(void)
192 userdb_iterateGroups();
193 while ((gr = userdb_nextGroup()) != 0) {
197 if (name__sanitise(gr->gr_name, buf, sizeof(buf)) &&
198 (n = name__get(buf, clType_user)) != 0) {
200 /* --- Now add all of the members --- */
202 for (p = gr->gr_mem; *p; p++) {
203 if ((pw = userdb_userByName(*p)) != 0) {
205 sym_find(n->c->t, (char *)&u, sizeof(u), sizeof(sym_base), 0);
212 /* --- @name__scan@ --- *
214 * Arguments: @netg *n@ = the netgroup handle we're scanning
215 * @const char *host@ = the host name
216 * @const char *user@ = the user name
217 * @const char *domain@ = the (NIS?) domain name
218 * @void *ctx@ = some context pointer
220 * Returns: Zero to continue scanning.
222 * Use: Scans a netgroup, adding items to the name table.
225 /* --- A data type --- */
227 typedef struct name__scanctx {
228 char *name; /* Netgroup name prefixed with `?_'*/
229 unsigned f; /* Various interesting flags */
230 name *h; /* Name entry for hosts */
231 name *u; /* Name entry for users */
234 enum { f_host = 1, f_user = 2 };
236 /* --- And now for the real code --- */
238 static int name__scan(netg *n, const char *host, const char *user,
239 const char *domain, void *ctx)
241 name__scanctx *sc = ctx;
243 /* --- Add the host to the hosts class --- */
245 if (sc->f & f_host && host) {
250 /* --- First ensure that I have a host class --- */
254 sc->h = name__get(sc->name, clType_host);
261 /* --- Now that I've done that, try to add the host --- *
263 * I'll turn it into an IP address. There's less chance of confusion
267 if ((h = gethostbyname(host)) == 0)
269 memcpy(&in, h->h_addr, sizeof(in));
270 if ((a = inet_ntoa(in)) == 0)
272 sym_find(sc->h->c->t, a, -1, sizeof(sym_base), 0);
276 /* --- Add the user to the users class --- */
278 if (sc->f & f_user && user) {
282 /* --- First ensure that I have a user class --- */
286 sc->u = name__get(sc->name, clType_user);
293 /* --- Add the user to the list --- */
295 if ((pw = userdb_userByName(user)) == 0)
298 sym_find(sc->u->c->t, (char *)&u, sizeof(u), sizeof(sym_base), 0);
305 /* --- @name__netgroups@ --- *
311 * Use: Populates the name table with netgroup information.
314 void name__netgroups(void)
323 while ((n = netg_next()) != 0) {
325 if (name__sanitise(p, buf + 2, sizeof(buf) - 2) == 0)
329 sc.f = f_host | f_user;
330 netg_scan(n, name__scan, &sc);
334 /* --- @name_init@ --- *
340 * Use: Initialises the name table. Requires the user database to
341 * be populated (see @userdb_local@ and @userdb_yp@, and
347 /* --- Initialise the name table --- */
349 sym_createTable(&name__table);
351 /* --- Add everyone into the table --- */
357 /* --- Finally add in the `all' class --- *
359 * Do that now, to prevent it being overwritten by the above.
366 n = sym_find(&name__table, "all", -1, sizeof(name), &f);
373 /* --- @name_end@ --- *
379 * Use: Closes down the name database, so that it can be
385 /* --- Empty the symbol table --- */
391 for (sym_createIter(&i, &name__table); (n = sym_next(&i)) != 0; ) {
397 /* --- Destroy and recreate the table --- */
399 sym_destroyTable(&name__table);
402 /* --- @name_find@ --- *
404 * Arguments: @const char *p@ = pointer to name to look up
405 * @unsigned create@ = whether to create the item
406 * @unsigned *f@ = whether the item was created
408 * Returns: Pointer to a @name@ block containing the symbol, or
409 * zero if it wasn't found and we didn't want to create a
412 * Use: Looks up a name in the symbol table and returns the
416 name *name_find(const char *p, unsigned create, unsigned *f)
418 /* --- This is a trivial veneer onto @sym_find@ --- */
420 return (sym_find(&name__table, p, -1, create ? sizeof(name) : 0, f));
423 /* --- @name_dump@ --- *
429 * Use: Dumps a complete listing of the symbol table.
438 trace(TRACE_DEBUG, "name: dumping names");
439 for (sym_createIter(&i, &name__table); (n = sym_next(&i)) != 0; ) {
440 trace(TRACE_DEBUG, "name: dumping `%s'", n->base.name);
446 /*----- Test driver -------------------------------------------------------*/
453 /* traceon(stdout, TRACE_ALL); */
459 printf("loaded (%lu)\n", track_memused());
465 printf("cleared (%lu)\n", track_memused());
472 printf("reloaded (%lu)\n", track_memused());