pwcache.c revision 1.6 1 /* $NetBSD: pwcache.c,v 1.6 1996/12/20 20:16:07 sommerfe 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 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)pwcache.c 8.1 (Berkeley) 6/4/93";
39 #else
40 static char rcsid[] = "$NetBSD: pwcache.c,v 1.6 1996/12/20 20:16:07 sommerfe Exp $";
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43
44 #include <sys/types.h>
45 #include <sys/param.h>
46
47 #include <grp.h>
48 #include <pwd.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <utmp.h>
52
53 #define NCACHE 64 /* power of 2 */
54 #define MASK NCACHE - 1 /* bits to store with */
55
56 char *
57 user_from_uid(uid, nouser)
58 uid_t uid;
59 int nouser;
60 {
61 static struct ncache {
62 uid_t uid;
63 char name[MAXLOGNAME + 1];
64 } c_uid[NCACHE];
65 static int pwopen;
66 static char nbuf[15]; /* 32 bits == 10 digits */
67 register struct passwd *pw;
68 register struct ncache *cp;
69
70 cp = c_uid + (uid & MASK);
71 if (cp->uid != uid || !*cp->name) {
72 if (pwopen == 0) {
73 setpassent(1);
74 pwopen = 1;
75 }
76 if ((pw = getpwuid(uid)) == NULL) {
77 if (nouser)
78 return (NULL);
79 (void)snprintf(nbuf, sizeof(nbuf), "%u", uid);
80 return (nbuf);
81 }
82 cp->uid = uid;
83 (void)strncpy(cp->name, pw->pw_name, MAXLOGNAME);
84 cp->name[MAXLOGNAME] = '\0';
85 }
86 return (cp->name);
87 }
88
89 char *
90 group_from_gid(gid, nogroup)
91 gid_t gid;
92 int nogroup;
93 {
94 static struct ncache {
95 gid_t gid;
96 char name[MAXLOGNAME + 1];
97 } c_gid[NCACHE];
98 static int gropen;
99 static char nbuf[15]; /* 32 bits == 10 digits */
100 struct group *gr;
101 struct ncache *cp;
102
103 cp = c_gid + (gid & MASK);
104 if (cp->gid != gid || !*cp->name) {
105 if (gropen == 0) {
106 setgroupent(1);
107 gropen = 1;
108 }
109 if ((gr = getgrgid(gid)) == NULL) {
110 if (nogroup)
111 return (NULL);
112 (void)snprintf(nbuf, sizeof(nbuf), "%u", gid);
113 return (nbuf);
114 }
115 cp->gid = gid;
116 (void)strncpy(cp->name, gr->gr_name, MAXLOGNAME);
117 cp->name[MAXLOGNAME] = '\0';
118 }
119 return (cp->name);
120 }
121