summaryrefslogtreecommitdiff
path: root/openbsd-compat/pwcache.c
diff options
context:
space:
mode:
authorDarren Tucker <dtucker@zip.com.au>2010-01-15 12:14:45 +1100
committerDarren Tucker <dtucker@zip.com.au>2010-01-15 12:14:45 +1100
commit9d1fd5bc1017c09bc9ff8b28511b0851fd3472a4 (patch)
tree5ff2018d37e9dccd8e012bc0dce8a972eeca9fa9 /openbsd-compat/pwcache.c
parentb8b17e984acab330b13c66460e6570e72c55ded9 (diff)
- (dtucker) [openbsd-compat.c/pwcache.c] Pull in pwcache.c from OpenBSD (no
changes yet but there will be some to come).
Diffstat (limited to 'openbsd-compat/pwcache.c')
-rw-r--r--openbsd-compat/pwcache.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/openbsd-compat/pwcache.c b/openbsd-compat/pwcache.c
new file mode 100644
index 000000000..6f8e6447e
--- /dev/null
+++ b/openbsd-compat/pwcache.c
@@ -0,0 +1,99 @@
1/* $OpenBSD: pwcache.c,v 1.9 2005/08/08 08:05:34 espie Exp $ */
2/*
3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/types.h>
32
33#include <grp.h>
34#include <pwd.h>
35#include <stdio.h>
36#include <string.h>
37
38#define NCACHE 64 /* power of 2 */
39#define MASK (NCACHE - 1) /* bits to store with */
40
41char *
42user_from_uid(uid_t uid, int nouser)
43{
44 static struct ncache {
45 uid_t uid;
46 char name[_PW_NAME_LEN + 1];
47 } c_uid[NCACHE];
48 static int pwopen;
49 static char nbuf[15]; /* 32 bits == 10 digits */
50 struct passwd *pw;
51 struct ncache *cp;
52
53 cp = c_uid + (uid & MASK);
54 if (cp->uid != uid || !*cp->name) {
55 if (pwopen == 0) {
56 setpassent(1);
57 pwopen = 1;
58 }
59 if ((pw = getpwuid(uid)) == NULL) {
60 if (nouser)
61 return (NULL);
62 (void)snprintf(nbuf, sizeof(nbuf), "%u", uid);
63 return (nbuf);
64 }
65 cp->uid = uid;
66 strlcpy(cp->name, pw->pw_name, sizeof(cp->name));
67 }
68 return (cp->name);
69}
70
71char *
72group_from_gid(gid_t gid, int nogroup)
73{
74 static struct ncache {
75 gid_t gid;
76 char name[_PW_NAME_LEN + 1];
77 } c_gid[NCACHE];
78 static int gropen;
79 static char nbuf[15]; /* 32 bits == 10 digits */
80 struct group *gr;
81 struct ncache *cp;
82
83 cp = c_gid + (gid & MASK);
84 if (cp->gid != gid || !*cp->name) {
85 if (gropen == 0) {
86 setgroupent(1);
87 gropen = 1;
88 }
89 if ((gr = getgrgid(gid)) == NULL) {
90 if (nogroup)
91 return (NULL);
92 (void)snprintf(nbuf, sizeof(nbuf), "%u", gid);
93 return (nbuf);
94 }
95 cp->gid = gid;
96 strlcpy(cp->name, gr->gr_name, sizeof(cp->name));
97 }
98 return (cp->name);
99}