3 * $Id: name.c,v 1.10 2004/04/08 01:36:20 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 /*----- Header files ------------------------------------------------------*/
31 /* --- ANSI headers --- */
38 /* --- Unix headers --- */
40 #include <sys/types.h>
41 #include <sys/socket.h>
43 #include <netinet/in.h>
45 #include <arpa/inet.h>
51 /* --- mLib headers --- */
53 #include <mLib/alloc.h>
56 /* --- Local headers --- */
66 /*----- Static variables --------------------------------------------------*/
68 static sym_table name__table
; /* Symbol table for everything */
70 /*----- Main code ---------------------------------------------------------*/
72 /* --- @name__get@ --- *
74 * Arguments: @const char *p@ = pointer to the name we want
75 * @unsigned type@ = type of class it should have
77 * Returns: A pointer to a name ready to use, or zero if there's a type
80 * Use: Creates a name of the appropriate type all ready to use.
83 static name
*name__get(const char *p
, unsigned type
)
86 name
*n
= sym_find(&name__table
, p
, -1, sizeof(*n
), &f
);
89 return ((n
->c
&& !(n
->c
->type
& type
)) ?
0 : n
);
92 /* --- @name__sanitise@ --- *
94 * Arguments: @const char *n@ = pointer to a name
95 * @char *buf@ = pointer to a buffer of space
96 * @size_t sz@ = size of the buffer
98 * Returns: A pointer to the transformed name in the buffer, or null
99 * if there wasn't enough space.
101 * Use: Transforms a name so that it only contains nice characters.
104 static char *name__sanitise(const char *n
, char *buf
, size_t sz
)
108 if (strlen(n
) + 1 > sz
)
112 if (isalnum((unsigned char)*n
))
122 /* --- @name__users@ --- *
128 * Use: Adds all of the users registered with the user database to
129 * the name table. Also adds the users' primary groups.
132 static void name__users(void)
138 userdb_iterateUsers();
139 while ((pw
= userdb_nextUser()) != 0) {
141 uid_t u
= pw
->pw_uid
;
143 /* --- First, add the user to the table --- */
145 if (name__sanitise(pw
->pw_name
, buf
, sizeof(buf
)) &&
146 (n
= name__get(buf
, clType_user
)) != 0)
147 n
->c
= class_addUser(n
->c
, u
);
149 /* --- Now handle the user's default group --- */
151 if ((gr
= userdb_groupById(pw
->pw_gid
)) != 0 &&
152 name__sanitise(gr
->gr_name
, buf
, sizeof(buf
)) &&
153 (n
= name__get(buf
, clType_user
)) != 0)
154 n
->c
= class_addUser(n
->c
, u
);
158 /* --- @name__groups@ --- *
164 * Use: Adds users into all of their supplementary groups.
167 static void name__groups(void)
174 userdb_iterateGroups();
175 while ((gr
= userdb_nextGroup()) != 0) {
178 if (name__sanitise(gr
->gr_name
, buf
, sizeof(buf
)) &&
179 (n
= name__get(buf
, clType_user
)) != 0) {
181 /* --- Now add all of the members --- */
183 for (p
= gr
->gr_mem
; *p
; p
++) {
184 if ((pw
= userdb_userByName(*p
)) != 0)
185 n
->c
= class_addUser(n
->c
, pw
->pw_uid
);
193 /* --- @name__scan@ --- *
195 * Arguments: @netg *n@ = the netgroup handle we're scanning
196 * @const char *host@ = the host name
197 * @const char *user@ = the user name
198 * @const char *domain@ = the (NIS?) domain name
199 * @void *ctx@ = some context pointer
201 * Returns: Zero to continue scanning.
203 * Use: Scans a netgroup, adding items to the name table.
206 /* --- A data type --- */
208 typedef struct name__scanctx
{
209 char *name
; /* Netgroup name prefixed with `?_'*/
210 unsigned f
; /* Various interesting flags */
211 name
*h
; /* Name entry for hosts */
212 name
*u
; /* Name entry for users */
215 enum { f_host
= 1, f_user
= 2 };
217 /* --- And now for the real code --- */
219 static int name__scan(netg
*n
, const char *host
, const char *user
,
220 const char *domain
, void *ctx
)
222 name__scanctx
*sc
= ctx
;
224 /* --- Add the host to the hosts class --- */
226 if (sc
->f
& f_host
&& host
) {
231 /* --- First ensure that I have a host class --- */
235 sc
->h
= name__get(sc
->name
, clType_host
);
242 /* --- Now that I've done that, try to add the host --- *
244 * I'll turn it into an IP address. There's less chance of confusion
248 if ((h
= gethostbyname(host
)) == 0)
250 memcpy(&in
, h
->h_addr
, sizeof(in
));
251 if ((a
= inet_ntoa(in
)) == 0)
253 sc
->h
->c
= class_addString(sc
->h
->c
, a
);
257 /* --- Add the user to the users class --- */
259 if (sc
->f
& f_user
&& user
) {
262 /* --- First ensure that I have a user class --- */
266 sc
->u
= name__get(sc
->name
, clType_user
);
273 /* --- Add the user to the list --- */
275 if ((pw
= userdb_userByName(user
)) == 0)
277 sc
->u
->c
= class_addUser(sc
->u
->c
, pw
->pw_uid
);
284 /* --- @name__netgroups@ --- *
290 * Use: Populates the name table with netgroup information.
293 void name__netgroups(void)
302 while ((n
= netg_next()) != 0) {
304 if (name__sanitise(p
, buf
+ 2, sizeof(buf
) - 2) == 0)
308 sc
.f
= f_host
| f_user
;
309 netg_scan(n
, name__scan
, &sc
);
313 /* --- @name_init@ --- *
319 * Use: Initialises the name table. Requires the user database to
320 * be populated (see @userdb_local@ and @userdb_yp@, and
326 /* --- Initialise the name table --- */
328 sym_create(&name__table
);
330 /* --- Add everyone into the table --- */
336 /* --- Finally add in the `all' and `none' classes --- *
338 * Do that now, to prevent them being overwritten by the above.
345 n
= sym_find(&name__table
, "all", -1, sizeof(name
), &f
);
350 n
= sym_find(&name__table
, "none", -1, sizeof(name
), &f
);
357 /* --- @name_end@ --- *
363 * Use: Closes down the name database, so that it can be
369 /* --- Empty the symbol table --- */
375 for (sym_mkiter(&i
, &name__table
); (n
= sym_next(&i
)) != 0; ) {
381 /* --- Destroy and recreate the table --- */
383 sym_destroy(&name__table
);
386 /* --- @name_find@ --- *
388 * Arguments: @const char *p@ = pointer to name to look up
389 * @unsigned create@ = whether to create the item
390 * @unsigned *f@ = whether the item was created
392 * Returns: Pointer to a @name@ block containing the symbol, or
393 * zero if it wasn't found and we didn't want to create a
396 * Use: Looks up a name in the symbol table and returns the
400 name
*name_find(const char *p
, unsigned create
, unsigned *f
)
402 /* --- This is a trivial veneer onto @sym_find@ --- */
404 return (sym_find(&name__table
, p
, -1, create ?
sizeof(name
) : 0, f
));
407 /* --- @name_dump@ --- *
413 * Use: Dumps a complete listing of the symbol table.
422 trace(TRACE_DEBUG
, "name: dumping names");
423 for (sym_mkiter(&i
, &name__table
); (n
= sym_next(&i
)) != 0; ) {
424 trace(TRACE_DEBUG
, "name: dumping `%s'", n
->base
.name
);
426 trace(TRACE_DEBUG
, "name: <empty>");
433 /*----- Test driver -------------------------------------------------------*/
440 trace_on(stdout
, TRACE_ALL
);
446 /* printf("loaded (%lu)\n", track_memused()); */
453 /* printf("cleared (%lu)\n", track_memused()); */
454 /* track_memlist(); */
460 /* printf("reloaded (%lu)\n", track_memused()); */