pwcache.c revision 1.27 1 1.27 sjg /* $NetBSD: pwcache.c,v 1.27 2004/05/18 22:14:35 sjg Exp $ */
2 1.4 cgd
3 1.10 mycroft /*-
4 1.25 agc * Copyright (c) 1992 Keith Muller.
5 1.10 mycroft * Copyright (c) 1992, 1993
6 1.4 cgd * The Regents of the University of California. All rights reserved.
7 1.1 cgd *
8 1.10 mycroft * This code is derived from software contributed to Berkeley by
9 1.10 mycroft * Keith Muller of the University of California, San Diego.
10 1.10 mycroft *
11 1.1 cgd * Redistribution and use in source and binary forms, with or without
12 1.1 cgd * modification, are permitted provided that the following conditions
13 1.1 cgd * are met:
14 1.1 cgd * 1. Redistributions of source code must retain the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer.
16 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 cgd * notice, this list of conditions and the following disclaimer in the
18 1.1 cgd * documentation and/or other materials provided with the distribution.
19 1.24 agc * 3. Neither the name of the University nor the names of its contributors
20 1.24 agc * may be used to endorse or promote products derived from this software
21 1.24 agc * without specific prior written permission.
22 1.24 agc *
23 1.24 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.24 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.24 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.24 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.24 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.24 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.24 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.24 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.24 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.24 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.24 agc * SUCH DAMAGE.
34 1.24 agc */
35 1.24 agc
36 1.24 agc /*-
37 1.17 lukem * Copyright (c) 2002 The NetBSD Foundation, Inc.
38 1.17 lukem * All rights reserved.
39 1.17 lukem *
40 1.17 lukem * Redistribution and use in source and binary forms, with or without
41 1.17 lukem * modification, are permitted provided that the following conditions
42 1.17 lukem * are met:
43 1.17 lukem * 1. Redistributions of source code must retain the above copyright
44 1.17 lukem * notice, this list of conditions and the following disclaimer.
45 1.17 lukem * 2. Redistributions in binary form must reproduce the above copyright
46 1.17 lukem * notice, this list of conditions and the following disclaimer in the
47 1.17 lukem * documentation and/or other materials provided with the distribution.
48 1.17 lukem * 3. All advertising materials mentioning features or use of this software
49 1.17 lukem * must display the following acknowledgement:
50 1.17 lukem * This product includes software developed by the NetBSD
51 1.17 lukem * Foundation, Inc. and its contributors.
52 1.17 lukem * 4. Neither the name of The NetBSD Foundation nor the names of its
53 1.17 lukem * contributors may be used to endorse or promote products derived
54 1.17 lukem * from this software without specific prior written permission.
55 1.17 lukem *
56 1.17 lukem * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 1.17 lukem * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 1.17 lukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 1.17 lukem * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 1.17 lukem * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 1.17 lukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 1.17 lukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 1.17 lukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 1.17 lukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 1.17 lukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 1.17 lukem * POSSIBILITY OF SUCH DAMAGE.
67 1.17 lukem */
68 1.17 lukem
69 1.27 sjg #if HAVE_NBTOOL_CONFIG_H
70 1.27 sjg #include "nbtool_config.h"
71 1.27 sjg #endif
72 1.27 sjg
73 1.7 christos #include <sys/cdefs.h>
74 1.15 msaitoh #if defined(LIBC_SCCS) && !defined(lint)
75 1.4 cgd #if 0
76 1.10 mycroft static char sccsid[] = "@(#)cache.c 8.1 (Berkeley) 5/31/93";
77 1.4 cgd #else
78 1.27 sjg __RCSID("$NetBSD: pwcache.c,v 1.27 2004/05/18 22:14:35 sjg Exp $");
79 1.4 cgd #endif
80 1.15 msaitoh #endif /* LIBC_SCCS and not lint */
81 1.1 cgd
82 1.8 jtc #include "namespace.h"
83 1.10 mycroft
84 1.1 cgd #include <sys/types.h>
85 1.6 sommerfe #include <sys/param.h>
86 1.4 cgd
87 1.13 lukem #include <assert.h>
88 1.4 cgd #include <grp.h>
89 1.1 cgd #include <pwd.h>
90 1.1 cgd #include <stdio.h>
91 1.10 mycroft #include <stdlib.h>
92 1.5 jtc #include <string.h>
93 1.10 mycroft #include <unistd.h>
94 1.10 mycroft
95 1.8 jtc #ifdef __weak_alias
96 1.14 mycroft __weak_alias(user_from_uid,_user_from_uid)
97 1.14 mycroft __weak_alias(group_from_gid,_group_from_gid)
98 1.17 lukem __weak_alias(pwcache_userdb,_pwcache_userdb)
99 1.17 lukem __weak_alias(pwcache_groupdb,_pwcache_groupdb)
100 1.8 jtc #endif
101 1.19 lukem
102 1.19 lukem #if !HAVE_PWCACHE_USERDB
103 1.19 lukem #include "pwcache.h"
104 1.1 cgd
105 1.10 mycroft /*
106 1.10 mycroft * routines that control user, group, uid and gid caches (for the archive
107 1.10 mycroft * member print routine).
108 1.10 mycroft * IMPORTANT:
109 1.10 mycroft * these routines cache BOTH hits and misses, a major performance improvement
110 1.10 mycroft */
111 1.10 mycroft
112 1.17 lukem /*
113 1.17 lukem * function pointers to various name lookup routines.
114 1.17 lukem * these may be changed as necessary.
115 1.17 lukem */
116 1.17 lukem static int (*_pwcache_setgroupent)(int) = setgroupent;
117 1.17 lukem static void (*_pwcache_endgrent)(void) = endgrent;
118 1.17 lukem static struct group * (*_pwcache_getgrnam)(const char *) = getgrnam;
119 1.17 lukem static struct group * (*_pwcache_getgrgid)(gid_t) = getgrgid;
120 1.17 lukem static int (*_pwcache_setpassent)(int) = setpassent;
121 1.17 lukem static void (*_pwcache_endpwent)(void) = endpwent;
122 1.17 lukem static struct passwd * (*_pwcache_getpwnam)(const char *) = getpwnam;
123 1.17 lukem static struct passwd * (*_pwcache_getpwuid)(uid_t) = getpwuid;
124 1.17 lukem
125 1.17 lukem /*
126 1.17 lukem * internal state
127 1.17 lukem */
128 1.17 lukem static int pwopn; /* is password file open */
129 1.17 lukem static int gropn; /* is group file open */
130 1.17 lukem static UIDC **uidtb; /* uid to name cache */
131 1.17 lukem static GIDC **gidtb; /* gid to name cache */
132 1.17 lukem static UIDC **usrtb; /* user name to uid cache */
133 1.17 lukem static GIDC **grptb; /* group name to gid cache */
134 1.17 lukem
135 1.17 lukem static int uidtb_fail; /* uidtb_start() failed ? */
136 1.17 lukem static int gidtb_fail; /* gidtb_start() failed ? */
137 1.17 lukem static int usrtb_fail; /* usrtb_start() failed ? */
138 1.17 lukem static int grptb_fail; /* grptb_start() failed ? */
139 1.17 lukem
140 1.17 lukem
141 1.17 lukem static u_int st_hash(const char *, size_t, int);
142 1.17 lukem static int uidtb_start(void);
143 1.17 lukem static int gidtb_start(void);
144 1.17 lukem static int usrtb_start(void);
145 1.17 lukem static int grptb_start(void);
146 1.17 lukem
147 1.10 mycroft
148 1.10 mycroft static u_int
149 1.16 lukem st_hash(const char *name, size_t len, int tabsz)
150 1.10 mycroft {
151 1.10 mycroft u_int key = 0;
152 1.10 mycroft
153 1.13 lukem _DIAGASSERT(name != NULL);
154 1.13 lukem
155 1.10 mycroft while (len--) {
156 1.10 mycroft key += *name++;
157 1.10 mycroft key = (key << 8) | (key >> 24);
158 1.10 mycroft }
159 1.10 mycroft
160 1.10 mycroft return (key % tabsz);
161 1.10 mycroft }
162 1.10 mycroft
163 1.10 mycroft /*
164 1.10 mycroft * uidtb_start
165 1.10 mycroft * creates an an empty uidtb
166 1.10 mycroft * Return:
167 1.10 mycroft * 0 if ok, -1 otherwise
168 1.10 mycroft */
169 1.10 mycroft static int
170 1.10 mycroft uidtb_start(void)
171 1.10 mycroft {
172 1.10 mycroft
173 1.10 mycroft if (uidtb != NULL)
174 1.10 mycroft return (0);
175 1.17 lukem if (uidtb_fail)
176 1.10 mycroft return (-1);
177 1.10 mycroft if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) {
178 1.17 lukem ++uidtb_fail;
179 1.10 mycroft return (-1);
180 1.10 mycroft }
181 1.10 mycroft return (0);
182 1.10 mycroft }
183 1.10 mycroft
184 1.10 mycroft /*
185 1.10 mycroft * gidtb_start
186 1.10 mycroft * creates an an empty gidtb
187 1.10 mycroft * Return:
188 1.10 mycroft * 0 if ok, -1 otherwise
189 1.10 mycroft */
190 1.17 lukem static int
191 1.10 mycroft gidtb_start(void)
192 1.10 mycroft {
193 1.10 mycroft
194 1.10 mycroft if (gidtb != NULL)
195 1.10 mycroft return (0);
196 1.17 lukem if (gidtb_fail)
197 1.10 mycroft return (-1);
198 1.10 mycroft if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) {
199 1.17 lukem ++gidtb_fail;
200 1.10 mycroft return (-1);
201 1.10 mycroft }
202 1.10 mycroft return (0);
203 1.10 mycroft }
204 1.10 mycroft
205 1.10 mycroft /*
206 1.10 mycroft * usrtb_start
207 1.10 mycroft * creates an an empty usrtb
208 1.10 mycroft * Return:
209 1.10 mycroft * 0 if ok, -1 otherwise
210 1.10 mycroft */
211 1.17 lukem static int
212 1.10 mycroft usrtb_start(void)
213 1.10 mycroft {
214 1.10 mycroft
215 1.10 mycroft if (usrtb != NULL)
216 1.10 mycroft return (0);
217 1.17 lukem if (usrtb_fail)
218 1.10 mycroft return (-1);
219 1.10 mycroft if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {
220 1.17 lukem ++usrtb_fail;
221 1.10 mycroft return (-1);
222 1.10 mycroft }
223 1.10 mycroft return (0);
224 1.10 mycroft }
225 1.10 mycroft
226 1.10 mycroft /*
227 1.10 mycroft * grptb_start
228 1.10 mycroft * creates an an empty grptb
229 1.10 mycroft * Return:
230 1.10 mycroft * 0 if ok, -1 otherwise
231 1.10 mycroft */
232 1.17 lukem static int
233 1.10 mycroft grptb_start(void)
234 1.10 mycroft {
235 1.10 mycroft
236 1.10 mycroft if (grptb != NULL)
237 1.10 mycroft return (0);
238 1.17 lukem if (grptb_fail)
239 1.10 mycroft return (-1);
240 1.10 mycroft if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {
241 1.17 lukem ++grptb_fail;
242 1.10 mycroft return (-1);
243 1.10 mycroft }
244 1.10 mycroft return (0);
245 1.10 mycroft }
246 1.10 mycroft
247 1.10 mycroft /*
248 1.11 mycroft * user_from_uid()
249 1.16 lukem * caches the name (if any) for the uid. If noname clear, we always
250 1.26 simonb * return the stored name (if valid or invalid match).
251 1.16 lukem * We use a simple hash table.
252 1.10 mycroft * Return
253 1.10 mycroft * Pointer to stored name (or a empty string)
254 1.10 mycroft */
255 1.10 mycroft const char *
256 1.10 mycroft user_from_uid(uid_t uid, int noname)
257 1.1 cgd {
258 1.9 perry struct passwd *pw;
259 1.10 mycroft UIDC *ptr, **pptr;
260 1.1 cgd
261 1.10 mycroft if ((uidtb == NULL) && (uidtb_start() < 0))
262 1.10 mycroft return (NULL);
263 1.10 mycroft
264 1.10 mycroft /*
265 1.10 mycroft * see if we have this uid cached
266 1.10 mycroft */
267 1.10 mycroft pptr = uidtb + (uid % UID_SZ);
268 1.10 mycroft ptr = *pptr;
269 1.10 mycroft
270 1.10 mycroft if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
271 1.10 mycroft /*
272 1.10 mycroft * have an entry for this uid
273 1.10 mycroft */
274 1.10 mycroft if (!noname || (ptr->valid == VALID))
275 1.10 mycroft return (ptr->name);
276 1.10 mycroft return (NULL);
277 1.10 mycroft }
278 1.10 mycroft
279 1.10 mycroft /*
280 1.10 mycroft * No entry for this uid, we will add it
281 1.10 mycroft */
282 1.10 mycroft if (!pwopn) {
283 1.17 lukem if (_pwcache_setpassent != NULL)
284 1.17 lukem (*_pwcache_setpassent)(1);
285 1.10 mycroft ++pwopn;
286 1.1 cgd }
287 1.10 mycroft
288 1.10 mycroft if (ptr == NULL)
289 1.12 mycroft *pptr = ptr = (UIDC *)malloc(sizeof(UIDC));
290 1.10 mycroft
291 1.17 lukem if ((pw = (*_pwcache_getpwuid)(uid)) == NULL) {
292 1.10 mycroft /*
293 1.10 mycroft * no match for this uid in the local password file
294 1.23 grant * a string that is the uid in numeric format
295 1.10 mycroft */
296 1.10 mycroft if (ptr == NULL)
297 1.10 mycroft return (NULL);
298 1.10 mycroft ptr->uid = uid;
299 1.10 mycroft (void)snprintf(ptr->name, UNMLEN, "%lu", (long) uid);
300 1.10 mycroft ptr->valid = INVALID;
301 1.10 mycroft if (noname)
302 1.10 mycroft return (NULL);
303 1.10 mycroft } else {
304 1.10 mycroft /*
305 1.10 mycroft * there is an entry for this uid in the password file
306 1.10 mycroft */
307 1.10 mycroft if (ptr == NULL)
308 1.10 mycroft return (pw->pw_name);
309 1.10 mycroft ptr->uid = uid;
310 1.17 lukem (void)strlcpy(ptr->name, pw->pw_name, UNMLEN);
311 1.10 mycroft ptr->valid = VALID;
312 1.10 mycroft }
313 1.10 mycroft return (ptr->name);
314 1.1 cgd }
315 1.1 cgd
316 1.10 mycroft /*
317 1.10 mycroft * group_from_gid()
318 1.16 lukem * caches the name (if any) for the gid. If noname clear, we always
319 1.26 simonb * return the stored name (if valid or invalid match).
320 1.16 lukem * We use a simple hash table.
321 1.10 mycroft * Return
322 1.10 mycroft * Pointer to stored name (or a empty string)
323 1.10 mycroft */
324 1.10 mycroft const char *
325 1.10 mycroft group_from_gid(gid_t gid, int noname)
326 1.10 mycroft {
327 1.10 mycroft struct group *gr;
328 1.10 mycroft GIDC *ptr, **pptr;
329 1.10 mycroft
330 1.10 mycroft if ((gidtb == NULL) && (gidtb_start() < 0))
331 1.10 mycroft return (NULL);
332 1.10 mycroft
333 1.10 mycroft /*
334 1.10 mycroft * see if we have this gid cached
335 1.10 mycroft */
336 1.10 mycroft pptr = gidtb + (gid % GID_SZ);
337 1.10 mycroft ptr = *pptr;
338 1.10 mycroft
339 1.10 mycroft if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {
340 1.10 mycroft /*
341 1.10 mycroft * have an entry for this gid
342 1.10 mycroft */
343 1.10 mycroft if (!noname || (ptr->valid == VALID))
344 1.10 mycroft return (ptr->name);
345 1.10 mycroft return (NULL);
346 1.10 mycroft }
347 1.10 mycroft
348 1.10 mycroft /*
349 1.10 mycroft * No entry for this gid, we will add it
350 1.10 mycroft */
351 1.10 mycroft if (!gropn) {
352 1.17 lukem if (_pwcache_setgroupent != NULL)
353 1.17 lukem (*_pwcache_setgroupent)(1);
354 1.10 mycroft ++gropn;
355 1.10 mycroft }
356 1.10 mycroft
357 1.10 mycroft if (ptr == NULL)
358 1.12 mycroft *pptr = ptr = (GIDC *)malloc(sizeof(GIDC));
359 1.10 mycroft
360 1.17 lukem if ((gr = (*_pwcache_getgrgid)(gid)) == NULL) {
361 1.10 mycroft /*
362 1.10 mycroft * no match for this gid in the local group file, put in
363 1.10 mycroft * a string that is the gid in numberic format
364 1.10 mycroft */
365 1.10 mycroft if (ptr == NULL)
366 1.10 mycroft return (NULL);
367 1.10 mycroft ptr->gid = gid;
368 1.10 mycroft (void)snprintf(ptr->name, GNMLEN, "%lu", (long) gid);
369 1.10 mycroft ptr->valid = INVALID;
370 1.10 mycroft if (noname)
371 1.10 mycroft return (NULL);
372 1.10 mycroft } else {
373 1.10 mycroft /*
374 1.10 mycroft * there is an entry for this group in the group file
375 1.10 mycroft */
376 1.10 mycroft if (ptr == NULL)
377 1.10 mycroft return (gr->gr_name);
378 1.10 mycroft ptr->gid = gid;
379 1.17 lukem (void)strlcpy(ptr->name, gr->gr_name, GNMLEN);
380 1.10 mycroft ptr->valid = VALID;
381 1.10 mycroft }
382 1.10 mycroft return (ptr->name);
383 1.10 mycroft }
384 1.10 mycroft
385 1.10 mycroft /*
386 1.10 mycroft * uid_from_user()
387 1.10 mycroft * caches the uid for a given user name. We use a simple hash table.
388 1.10 mycroft * Return
389 1.10 mycroft * the uid (if any) for a user name, or a -1 if no match can be found
390 1.10 mycroft */
391 1.10 mycroft int
392 1.10 mycroft uid_from_user(const char *name, uid_t *uid)
393 1.10 mycroft {
394 1.10 mycroft struct passwd *pw;
395 1.10 mycroft UIDC *ptr, **pptr;
396 1.10 mycroft size_t namelen;
397 1.10 mycroft
398 1.10 mycroft /*
399 1.10 mycroft * return -1 for mangled names
400 1.10 mycroft */
401 1.13 lukem if (name == NULL || ((namelen = strlen(name)) == 0))
402 1.10 mycroft return (-1);
403 1.10 mycroft if ((usrtb == NULL) && (usrtb_start() < 0))
404 1.10 mycroft return (-1);
405 1.10 mycroft
406 1.10 mycroft /*
407 1.10 mycroft * look up in hash table, if found and valid return the uid,
408 1.10 mycroft * if found and invalid, return a -1
409 1.10 mycroft */
410 1.10 mycroft pptr = usrtb + st_hash(name, namelen, UNM_SZ);
411 1.10 mycroft ptr = *pptr;
412 1.10 mycroft
413 1.10 mycroft if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
414 1.10 mycroft if (ptr->valid == INVALID)
415 1.10 mycroft return (-1);
416 1.10 mycroft *uid = ptr->uid;
417 1.10 mycroft return (0);
418 1.10 mycroft }
419 1.10 mycroft
420 1.10 mycroft if (!pwopn) {
421 1.17 lukem if (_pwcache_setpassent != NULL)
422 1.17 lukem (*_pwcache_setpassent)(1);
423 1.10 mycroft ++pwopn;
424 1.10 mycroft }
425 1.10 mycroft
426 1.10 mycroft if (ptr == NULL)
427 1.12 mycroft *pptr = ptr = (UIDC *)malloc(sizeof(UIDC));
428 1.10 mycroft
429 1.10 mycroft /*
430 1.10 mycroft * no match, look it up, if no match store it as an invalid entry,
431 1.10 mycroft * or store the matching uid
432 1.10 mycroft */
433 1.10 mycroft if (ptr == NULL) {
434 1.17 lukem if ((pw = (*_pwcache_getpwnam)(name)) == NULL)
435 1.10 mycroft return (-1);
436 1.10 mycroft *uid = pw->pw_uid;
437 1.10 mycroft return (0);
438 1.10 mycroft }
439 1.17 lukem (void)strlcpy(ptr->name, name, UNMLEN);
440 1.17 lukem if ((pw = (*_pwcache_getpwnam)(name)) == NULL) {
441 1.10 mycroft ptr->valid = INVALID;
442 1.10 mycroft return (-1);
443 1.10 mycroft }
444 1.10 mycroft ptr->valid = VALID;
445 1.10 mycroft *uid = ptr->uid = pw->pw_uid;
446 1.10 mycroft return (0);
447 1.10 mycroft }
448 1.10 mycroft
449 1.10 mycroft /*
450 1.10 mycroft * gid_from_group()
451 1.10 mycroft * caches the gid for a given group name. We use a simple hash table.
452 1.10 mycroft * Return
453 1.10 mycroft * the gid (if any) for a group name, or a -1 if no match can be found
454 1.10 mycroft */
455 1.10 mycroft int
456 1.10 mycroft gid_from_group(const char *name, gid_t *gid)
457 1.1 cgd {
458 1.4 cgd struct group *gr;
459 1.10 mycroft GIDC *ptr, **pptr;
460 1.10 mycroft size_t namelen;
461 1.10 mycroft
462 1.10 mycroft /*
463 1.10 mycroft * return -1 for mangled names
464 1.10 mycroft */
465 1.13 lukem if (name == NULL || ((namelen = strlen(name)) == 0))
466 1.10 mycroft return (-1);
467 1.10 mycroft if ((grptb == NULL) && (grptb_start() < 0))
468 1.10 mycroft return (-1);
469 1.10 mycroft
470 1.10 mycroft /*
471 1.10 mycroft * look up in hash table, if found and valid return the uid,
472 1.10 mycroft * if found and invalid, return a -1
473 1.10 mycroft */
474 1.10 mycroft pptr = grptb + st_hash(name, namelen, GID_SZ);
475 1.10 mycroft ptr = *pptr;
476 1.10 mycroft
477 1.10 mycroft if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
478 1.10 mycroft if (ptr->valid == INVALID)
479 1.10 mycroft return (-1);
480 1.10 mycroft *gid = ptr->gid;
481 1.10 mycroft return (0);
482 1.10 mycroft }
483 1.10 mycroft
484 1.10 mycroft if (!gropn) {
485 1.17 lukem if (_pwcache_setgroupent != NULL)
486 1.17 lukem (*_pwcache_setgroupent)(1);
487 1.10 mycroft ++gropn;
488 1.10 mycroft }
489 1.10 mycroft
490 1.10 mycroft if (ptr == NULL)
491 1.12 mycroft *pptr = ptr = (GIDC *)malloc(sizeof(GIDC));
492 1.10 mycroft
493 1.10 mycroft /*
494 1.10 mycroft * no match, look it up, if no match store it as an invalid entry,
495 1.10 mycroft * or store the matching gid
496 1.10 mycroft */
497 1.10 mycroft if (ptr == NULL) {
498 1.17 lukem if ((gr = (*_pwcache_getgrnam)(name)) == NULL)
499 1.10 mycroft return (-1);
500 1.10 mycroft *gid = gr->gr_gid;
501 1.10 mycroft return (0);
502 1.10 mycroft }
503 1.1 cgd
504 1.17 lukem (void)strlcpy(ptr->name, name, GNMLEN);
505 1.17 lukem if ((gr = (*_pwcache_getgrnam)(name)) == NULL) {
506 1.10 mycroft ptr->valid = INVALID;
507 1.10 mycroft return (-1);
508 1.1 cgd }
509 1.10 mycroft ptr->valid = VALID;
510 1.10 mycroft *gid = ptr->gid = gr->gr_gid;
511 1.10 mycroft return (0);
512 1.1 cgd }
513 1.17 lukem
514 1.17 lukem #define FLUSHTB(arr, len, fail) \
515 1.17 lukem do { \
516 1.17 lukem if (arr != NULL) { \
517 1.17 lukem for (i = 0; i < len; i++) \
518 1.17 lukem if (arr[i] != NULL) \
519 1.17 lukem free(arr[i]); \
520 1.17 lukem arr = NULL; \
521 1.17 lukem } \
522 1.17 lukem fail = 0; \
523 1.17 lukem } while (/* CONSTCOND */0);
524 1.17 lukem
525 1.17 lukem int
526 1.17 lukem pwcache_userdb(
527 1.17 lukem int (*a_setpassent)(int),
528 1.17 lukem void (*a_endpwent)(void),
529 1.17 lukem struct passwd * (*a_getpwnam)(const char *),
530 1.17 lukem struct passwd * (*a_getpwuid)(uid_t))
531 1.17 lukem {
532 1.17 lukem int i;
533 1.17 lukem
534 1.17 lukem /* a_setpassent and a_endpwent may be NULL */
535 1.17 lukem if (a_getpwnam == NULL || a_getpwuid == NULL)
536 1.17 lukem return (-1);
537 1.17 lukem
538 1.17 lukem if (_pwcache_endpwent != NULL)
539 1.17 lukem (*_pwcache_endpwent)();
540 1.17 lukem FLUSHTB(uidtb, UID_SZ, uidtb_fail);
541 1.17 lukem FLUSHTB(usrtb, UNM_SZ, usrtb_fail);
542 1.17 lukem pwopn = 0;
543 1.17 lukem _pwcache_setpassent = a_setpassent;
544 1.17 lukem _pwcache_endpwent = a_endpwent;
545 1.17 lukem _pwcache_getpwnam = a_getpwnam;
546 1.17 lukem _pwcache_getpwuid = a_getpwuid;
547 1.17 lukem
548 1.17 lukem return (0);
549 1.17 lukem }
550 1.17 lukem
551 1.17 lukem int
552 1.17 lukem pwcache_groupdb(
553 1.17 lukem int (*a_setgroupent)(int),
554 1.17 lukem void (*a_endgrent)(void),
555 1.17 lukem struct group * (*a_getgrnam)(const char *),
556 1.17 lukem struct group * (*a_getgrgid)(gid_t))
557 1.17 lukem {
558 1.17 lukem int i;
559 1.17 lukem
560 1.17 lukem /* a_setgroupent and a_endgrent may be NULL */
561 1.17 lukem if (a_getgrnam == NULL || a_getgrgid == NULL)
562 1.17 lukem return (-1);
563 1.17 lukem
564 1.17 lukem if (_pwcache_endgrent != NULL)
565 1.17 lukem (*_pwcache_endgrent)();
566 1.17 lukem FLUSHTB(gidtb, GID_SZ, gidtb_fail);
567 1.17 lukem FLUSHTB(grptb, GNM_SZ, grptb_fail);
568 1.17 lukem gropn = 0;
569 1.17 lukem _pwcache_setgroupent = a_setgroupent;
570 1.17 lukem _pwcache_endgrent = a_endgrent;
571 1.17 lukem _pwcache_getgrnam = a_getgrnam;
572 1.17 lukem _pwcache_getgrgid = a_getgrgid;
573 1.17 lukem
574 1.17 lukem return (0);
575 1.17 lukem }
576 1.17 lukem
577 1.17 lukem
578 1.17 lukem #ifdef TEST_PWCACHE
579 1.17 lukem
580 1.17 lukem struct passwd *
581 1.17 lukem test_getpwnam(const char *name)
582 1.17 lukem {
583 1.17 lukem static struct passwd foo;
584 1.17 lukem
585 1.17 lukem memset(&foo, 0, sizeof(foo));
586 1.17 lukem if (strcmp(name, "toor") == 0) {
587 1.17 lukem foo.pw_uid = 666;
588 1.17 lukem return &foo;
589 1.17 lukem }
590 1.17 lukem return (getpwnam(name));
591 1.17 lukem }
592 1.17 lukem
593 1.17 lukem int
594 1.17 lukem main(int argc, char *argv[])
595 1.17 lukem {
596 1.17 lukem uid_t u;
597 1.17 lukem int r, i;
598 1.17 lukem
599 1.17 lukem printf("pass 1 (default userdb)\n");
600 1.17 lukem for (i = 1; i < argc; i++) {
601 1.17 lukem printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",
602 1.17 lukem i, pwopn, usrtb_fail, usrtb);
603 1.17 lukem r = uid_from_user(argv[i], &u);
604 1.17 lukem if (r == -1)
605 1.17 lukem printf(" uid_from_user %s: failed\n", argv[i]);
606 1.17 lukem else
607 1.17 lukem printf(" uid_from_user %s: %d\n", argv[i], u);
608 1.17 lukem }
609 1.17 lukem printf("pass 1 finish: pwopn %d usrtb_fail %d usrtb %p\n",
610 1.17 lukem pwopn, usrtb_fail, usrtb);
611 1.17 lukem
612 1.17 lukem puts("");
613 1.17 lukem printf("pass 2 (replacement userdb)\n");
614 1.17 lukem printf("pwcache_userdb returned %d\n",
615 1.17 lukem pwcache_userdb(setpassent, test_getpwnam, getpwuid));
616 1.17 lukem printf("pwopn %d usrtb_fail %d usrtb %p\n", pwopn, usrtb_fail, usrtb);
617 1.17 lukem
618 1.17 lukem for (i = 1; i < argc; i++) {
619 1.17 lukem printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",
620 1.17 lukem i, pwopn, usrtb_fail, usrtb);
621 1.17 lukem u = -1;
622 1.17 lukem r = uid_from_user(argv[i], &u);
623 1.17 lukem if (r == -1)
624 1.17 lukem printf(" uid_from_user %s: failed\n", argv[i]);
625 1.17 lukem else
626 1.17 lukem printf(" uid_from_user %s: %d\n", argv[i], u);
627 1.17 lukem }
628 1.17 lukem printf("pass 2 finish: pwopn %d usrtb_fail %d usrtb %p\n",
629 1.17 lukem pwopn, usrtb_fail, usrtb);
630 1.17 lukem
631 1.17 lukem puts("");
632 1.17 lukem printf("pass 3 (null pointers)\n");
633 1.17 lukem printf("pwcache_userdb returned %d\n",
634 1.17 lukem pwcache_userdb(NULL, NULL, NULL));
635 1.17 lukem
636 1.17 lukem return (0);
637 1.17 lukem }
638 1.17 lukem #endif /* TEST_PWCACHE */
639 1.18 lukem #endif /* !HAVE_PWCACHE_USERDB */
640