3 * $Id: name.c,v 1.2 1997/08/04 10:24:24 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.2 1997/08/04 10:24:24 mdw
33 * Sources placed under CVS control.
35 * Revision 1.1 1997/07/21 13:47:46 mdw
40 /*----- Header files ------------------------------------------------------*/
42 /* --- ANSI headers --- */
48 /* --- Unix headers --- */
53 # include <rpcsvc/ypclnt.h>
54 # include <rpcsvc/yp_prot.h>
60 /* --- Local headers --- */
69 /*----- Static variables --------------------------------------------------*/
71 static sym_table name__table
; /* Symbol table for everything */
73 /*----- Main code ---------------------------------------------------------*/
75 /* --- @name__users@ --- *
81 * Use: Adds all of the users registered with the user database to
82 * the name table. Also adds the users' primary groups.
85 static void name__users(void)
90 userdb_iterateUsers();
91 while ((pw
= userdb_nextUser()) != 0) {
96 /* --- First, add the user to the table --- */
98 n
= sym_find(&name__table
, pw
->pw_name
, -1, sizeof(name
), &f
);
100 sym_table
*t
= xmalloc(sizeof(*t
));
102 n
->c
= class_create(clType_user
, t
);
105 sym_find(n
->c
->t
, (char *)&u
, sizeof(u
), sizeof(sym_base
), 0);
107 /* --- Now handle the user's default group --- */
109 if ((gr
= userdb_groupById(pw
->pw_gid
)) != 0) {
110 n
= sym_find(&name__table
, gr
->gr_name
, -1, sizeof(name
), &f
);
112 sym_table
*t
= xmalloc(sizeof(*t
));
114 n
->c
= class_create(clType_user
, t
);
116 sym_find(n
->c
->t
, (char *)&u
, sizeof(u
), sizeof(sym_base
), 0);
121 /* --- @name__groups@ --- *
127 * Use: Adds users into all of their supplementary groups.
130 static void name__groups(void)
136 userdb_iterateGroups();
137 while ((gr
= userdb_nextGroup()) != 0) {
142 /* --- Add the group name to the table --- */
144 n
= sym_find(&name__table
, gr
->gr_name
, -1, sizeof(name
), &f
);
146 sym_table
*t
= xmalloc(sizeof(*t
));
148 n
->c
= class_create(clType_user
, t
);
151 /* --- Now add all of the members --- */
153 for (p
= gr
->gr_mem
; *p
; p
++) {
154 if ((pw
= userdb_userByName(*p
)) != 0) {
156 sym_find(n
->c
->t
, (char *)&u
, sizeof(u
), sizeof(sym_base
), 0);
162 /* --- @name_init@ --- *
168 * Use: Initialises the name table. Requires the user database to
169 * be populated (see @userdb_local@ and @userdb_yp@).
174 /* --- Initialise the name table --- */
176 sym_createTable(&name__table
);
178 /* --- Add everyone into the table --- */
183 /* --- Finally add in the `all' class --- *
185 * Do that now, to prevent it being overwritten by the above.
192 n
= sym_find(&name__table
, "all", -1, sizeof(name
), &f
);
199 /* --- @name_reinit@ --- *
205 * Use: Reinitialises the names table. It's cleared and then
206 * initialised with the current user and group ids as for
210 void name_reinit(void)
212 /* --- Empty the symbol table --- */
218 for (sym_createIter(&i
, &name__table
); (n
= sym_next(&i
)) != 0; ) {
224 /* --- Destroy and recreate the table --- */
226 sym_destroyTable(&name__table
);
230 /* --- @name_find@ --- *
232 * Arguments: @const char *p@ = pointer to name to look up
233 * @unsigned create@ = whether to create the item
234 * @unsigned *f@ = whether the item was created
236 * Returns: Pointer to a @name@ block containing the symbol, or
237 * zero if it wasn't found and we didn't want to create a
240 * Use: Looks up a name in the symbol table and returns the
244 name
*name_find(const char *p
, unsigned create
, unsigned *f
)
246 /* --- This is a trivial veneer onto @sym_find@ --- */
248 return (sym_find(&name__table
, p
, -1, create ?
sizeof(name
) : 0, f
));
251 /* --- @name_dump@ --- *
257 * Use: Dumps a complete listing of the symbol table.
265 trace(TRACE_DEBUG
, "name: dumping names");
266 for (sym_createIter(&i
, &name__table
); (n
= sym_next(&i
)) != 0; ) {
267 trace(TRACE_DEBUG
, "name: dumping `%s'", n
->base
.name
);
272 /*----- Test driver -------------------------------------------------------*/
282 traceon(stdout
, TRACE_DEBUG
);