pwcache.c revision 1.9 1 /* $NetBSD: pwcache.c,v 1.9 1998/02/03 18:23:51 perry Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)pwcache.c 8.1 (Berkeley) 6/4/93";
40 #else
41 __RCSID("$NetBSD: pwcache.c,v 1.9 1998/02/03 18:23:51 perry Exp $");
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44
45 #include "namespace.h"
46 #include <sys/types.h>
47 #include <sys/param.h>
48
49 #include <grp.h>
50 #include <pwd.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <utmp.h>
54
55 #ifdef __weak_alias
56 __weak_alias(user_from_uid,_user_from_uid);
57 __weak_alias(group_from_gid,_group_from_gid);
58 #endif
59
60 #define NCACHE 64 /* power of 2 */
61 #define MASK (NCACHE - 1) /* bits to store with */
62
63 char *
64 user_from_uid(uid, nouser)
65 uid_t uid;
66 int nouser;
67 {
68 static struct ncache {
69 uid_t uid;
70 char name[MAXLOGNAME + 1];
71 } c_uid[NCACHE];
72 static int pwopen;
73 static char nbuf[15]; /* 32 bits == 10 digits */
74 struct passwd *pw;
75 struct ncache *cp;
76
77 cp = c_uid + (uid & MASK);
78 if (cp->uid != uid || !*cp->name) {
79 if (pwopen == 0) {
80 setpassent(1);
81 pwopen = 1;
82 }
83 if ((pw = getpwuid(uid)) == NULL) {
84 if (nouser)
85 return (NULL);
86 (void)snprintf(nbuf, sizeof(nbuf), "%u", uid);
87 return (nbuf);
88 }
89 cp->uid = uid;
90 (void)strncpy(cp->name, pw->pw_name, MAXLOGNAME);
91 cp->name[MAXLOGNAME] = '\0';
92 }
93 return (cp->name);
94 }
95
96 char *
97 group_from_gid(gid, nogroup)
98 gid_t gid;
99 int nogroup;
100 {
101 static struct ncache {
102 gid_t gid;
103 char name[MAXLOGNAME + 1];
104 } c_gid[NCACHE];
105 static int gropen;
106 static char nbuf[15]; /* 32 bits == 10 digits */
107 struct group *gr;
108 struct ncache *cp;
109
110 cp = c_gid + (gid & MASK);
111 if (cp->gid != gid || !*cp->name) {
112 if (gropen == 0) {
113 setgroupent(1);
114 gropen = 1;
115 }
116 if ((gr = getgrgid(gid)) == NULL) {
117 if (nogroup)
118 return (NULL);
119 (void)snprintf(nbuf, sizeof(nbuf), "%u", gid);
120 return (nbuf);
121 }
122 cp->gid = gid;
123 (void)strncpy(cp->name, gr->gr_name, MAXLOGNAME);
124 cp->name[MAXLOGNAME] = '\0';
125 }
126 return (cp->name);
127 }
128