3 * $Id: name.c,v 1.3 1997/08/07 09:49:39 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.3 1997/08/07 09:49:39 mdw
33 * Extensive modifications to handle netgroups. Also sanitise user and group
34 * names before adding them to the symbol table.
36 * Revision 1.2 1997/08/04 10:24:24 mdw
37 * Sources placed under CVS control.
39 * Revision 1.1 1997/07/21 13:47:46 mdw
44 /*----- Header files ------------------------------------------------------*/
46 /* --- ANSI headers --- */
53 /* --- Unix headers --- */
55 #include <sys/types.h>
56 #include <sys/socket.h>
58 #include <netinet/in.h>
60 #include <arpa/inet.h>
66 /* --- Local headers --- */
78 /*----- Static variables --------------------------------------------------*/
80 static sym_table name__table
; /* Symbol table for everything */
82 /*----- Main code ---------------------------------------------------------*/
84 /* --- @name__get@ --- *
86 * Arguments: @const char *p@ = pointer to the name we want
87 * @unsigned type@ = type of class it should have
89 * Returns: A pointer to a name ready to use, or zero if there's a type
92 * Use: Creates a name of the appropriate type all ready to use.
95 static name
*name__get(const char *p
, unsigned type
)
98 name
*n
= sym_find(&name__table
, p
, -1, sizeof(*n
), &f
);
100 sym_table
*t
= xmalloc(sizeof(*t
));
102 n
->c
= class_create(type
, t
);
104 return (n
->c
->type
== type ? n
: 0);
107 /* --- @name__sanitise@ --- *
109 * Arguments: @const char *n@ = pointer to a name
110 * @char *buf@ = pointer to a buffer of space
111 * @size_t sz@ = size of the buffer
113 * Returns: A pointer to the transformed name in the buffer, or null
114 * if there wasn't enough space.
116 * Use: Transforms a name so that it only contains nice characters.
119 static char *name__sanitise(const char *n
, char *buf
, size_t sz
)
123 if (strlen(n
) + 1 > sz
)
127 if (isalnum((unsigned char)*n
))
137 /* --- @name__users@ --- *
143 * Use: Adds all of the users registered with the user database to
144 * the name table. Also adds the users' primary groups.
147 static void name__users(void)
153 userdb_iterateUsers();
154 while ((pw
= userdb_nextUser()) != 0) {
158 /* --- Make the name into something nice --- */
160 /* --- First, add the user to the table --- */
162 if (name__sanitise(pw
->pw_name
, buf
, sizeof(buf
)) &&
163 (n
= name__get(buf
, clType_user
)) != 0)
164 sym_find(n
->c
->t
, (char *)&u
, sizeof(u
), sizeof(sym_base
), 0);
166 /* --- Now handle the user's default group --- */
168 if ((gr
= userdb_groupById(pw
->pw_gid
)) != 0 &&
169 name__sanitise(gr
->gr_name
, buf
, sizeof(buf
)) &&
170 (n
= name__get(buf
, clType_user
)) != 0)
171 sym_find(n
->c
->t
, (char *)&u
, sizeof(u
), sizeof(sym_base
), 0);
175 /* --- @name__groups@ --- *
181 * Use: Adds users into all of their supplementary groups.
184 static void name__groups(void)
191 userdb_iterateGroups();
192 while ((gr
= userdb_nextGroup()) != 0) {
196 if (name__sanitise(gr
->gr_name
, buf
, sizeof(buf
)) &&
197 (n
= name__get(buf
, clType_user
)) != 0) {
199 /* --- Now add all of the members --- */
201 for (p
= gr
->gr_mem
; *p
; p
++) {
202 if ((pw
= userdb_userByName(*p
)) != 0) {
204 sym_find(n
->c
->t
, (char *)&u
, sizeof(u
), sizeof(sym_base
), 0);
211 /* --- @name__scan@ --- *
213 * Arguments: @netg *n@ = the netgroup handle we're scanning
214 * @const char *host@ = the host name
215 * @const char *user@ = the user name
216 * @const char *domain@ = the (NIS?) domain name
217 * @void *ctx@ = some context pointer
219 * Returns: Zero to continue scanning.
221 * Use: Scans a netgroup, adding items to the name table.
224 /* --- A data type --- */
226 typedef struct name__scanctx
{
227 char *name
; /* Netgroup name prefixed with `?_'*/
228 unsigned f
; /* Various interesting flags */
229 name
*h
; /* Name entry for hosts */
230 name
*u
; /* Name entry for users */
233 enum { f_host
= 1, f_user
= 2 };
235 /* --- And now for the real code --- */
237 static int name__scan(netg
*n
, const char *host
, const char *user
,
238 const char *domain
, void *ctx
)
240 name__scanctx
*sc
= ctx
;
242 /* --- Add the host to the hosts class --- */
244 if (sc
->f
& f_host
&& host
) {
249 /* --- First ensure that I have a host class --- */
253 sc
->h
= name__get(sc
->name
, clType_host
);
260 /* --- Now that I've done that, try to add the host --- *
262 * I'll turn it into an IP address. There's less chance of confusion
266 if ((h
= gethostbyname(host
)) == 0)
268 memcpy(&in
, h
->h_addr
, sizeof(in
));
269 if ((a
= inet_ntoa(in
)) == 0)
271 sym_find(sc
->h
->c
->t
, a
, -1, sizeof(sym_base
), 0);
275 /* --- Add the user to the users class --- */
277 if (sc
->f
& f_user
&& user
) {
281 /* --- First ensure that I have a user class --- */
285 sc
->u
= name__get(sc
->name
, clType_user
);
292 /* --- Add the user to the list --- */
294 if ((pw
= userdb_userByName(user
)) == 0)
297 sym_find(sc
->u
->c
->t
, (char *)&u
, sizeof(u
), sizeof(sym_base
), 0);
304 /* --- @name__netgroups@ --- *
310 * Use: Populates the name table with netgroup information.
313 void name__netgroups(void)
322 while ((n
= netg_next()) != 0) {
324 if (name__sanitise(p
, buf
+ 2, sizeof(buf
) - 2) == 0)
328 sc
.f
= f_host
| f_user
;
329 netg_scan(n
, name__scan
, &sc
);
333 /* --- @name_init@ --- *
339 * Use: Initialises the name table. Requires the user database to
340 * be populated (see @userdb_local@ and @userdb_yp@, and
346 /* --- Initialise the name table --- */
348 sym_createTable(&name__table
);
350 /* --- Add everyone into the table --- */
356 /* --- Finally add in the `all' class --- *
358 * Do that now, to prevent it being overwritten by the above.
365 n
= sym_find(&name__table
, "all", -1, sizeof(name
), &f
);
372 /* --- @name_reinit@ --- *
378 * Use: Reinitialises the names table. It's cleared and then
379 * initialised with the current user and group ids as for
383 void name_reinit(void)
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
);
403 /* --- @name_find@ --- *
405 * Arguments: @const char *p@ = pointer to name to look up
406 * @unsigned create@ = whether to create the item
407 * @unsigned *f@ = whether the item was created
409 * Returns: Pointer to a @name@ block containing the symbol, or
410 * zero if it wasn't found and we didn't want to create a
413 * Use: Looks up a name in the symbol table and returns the
417 name
*name_find(const char *p
, unsigned create
, unsigned *f
)
419 /* --- This is a trivial veneer onto @sym_find@ --- */
421 return (sym_find(&name__table
, p
, -1, create ?
sizeof(name
) : 0, f
));
424 /* --- @name_dump@ --- *
430 * 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
);
445 /*----- Test driver -------------------------------------------------------*/
452 traceon(stdout
, TRACE_ALL
);