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