summaryrefslogtreecommitdiff
path: root/openbsd-compat/pwcache.c
diff options
context:
space:
mode:
authorDarren Tucker <dtucker@zip.com.au>2010-01-15 12:38:30 +1100
committerDarren Tucker <dtucker@zip.com.au>2010-01-15 12:38:30 +1100
commit909a390bb812b49f283a4b02e0cc8d582f020fb2 (patch)
tree0d4783272f979ef6d289cd585421f33a2483060c /openbsd-compat/pwcache.c
parent9d1fd5bc1017c09bc9ff8b28511b0851fd3472a4 (diff)
- (dtucker) [configure.ac openbsd-compat/{Makefile.in,pwcache.c} Portability
for pwcache. Also, added caching of negative hits.
Diffstat (limited to 'openbsd-compat/pwcache.c')
-rw-r--r--openbsd-compat/pwcache.c29
1 files changed, 21 insertions, 8 deletions
diff --git a/openbsd-compat/pwcache.c b/openbsd-compat/pwcache.c
index 6f8e6447e..472505d02 100644
--- a/openbsd-compat/pwcache.c
+++ b/openbsd-compat/pwcache.c
@@ -28,22 +28,26 @@
28 * SUCH DAMAGE. 28 * SUCH DAMAGE.
29 */ 29 */
30 30
31/* OPENBSD ORIGINAL: lib/libc/gen/pwcache.c */
32
31#include <sys/types.h> 33#include <sys/types.h>
32 34
33#include <grp.h> 35#include <grp.h>
34#include <pwd.h> 36#include <pwd.h>
35#include <stdio.h> 37#include <stdio.h>
38#include <stdlib.h>
36#include <string.h> 39#include <string.h>
37 40
38#define NCACHE 64 /* power of 2 */ 41#define NCACHE 64 /* power of 2 */
39#define MASK (NCACHE - 1) /* bits to store with */ 42#define MASK (NCACHE - 1) /* bits to store with */
40 43
44#ifndef HAVE_USER_FROM_UID
41char * 45char *
42user_from_uid(uid_t uid, int nouser) 46user_from_uid(uid_t uid, int nouser)
43{ 47{
44 static struct ncache { 48 static struct ncache {
45 uid_t uid; 49 uid_t uid;
46 char name[_PW_NAME_LEN + 1]; 50 char *name;
47 } c_uid[NCACHE]; 51 } c_uid[NCACHE];
48 static int pwopen; 52 static int pwopen;
49 static char nbuf[15]; /* 32 bits == 10 digits */ 53 static char nbuf[15]; /* 32 bits == 10 digits */
@@ -51,29 +55,34 @@ user_from_uid(uid_t uid, int nouser)
51 struct ncache *cp; 55 struct ncache *cp;
52 56
53 cp = c_uid + (uid & MASK); 57 cp = c_uid + (uid & MASK);
54 if (cp->uid != uid || !*cp->name) { 58 if (cp->uid != uid || cp->name == NULL) {
59#ifdef HAVE_SETPASSENT
55 if (pwopen == 0) { 60 if (pwopen == 0) {
56 setpassent(1); 61 setpassent(1);
57 pwopen = 1; 62 pwopen = 1;
58 } 63 }
64#endif
59 if ((pw = getpwuid(uid)) == NULL) { 65 if ((pw = getpwuid(uid)) == NULL) {
60 if (nouser) 66 if (nouser)
61 return (NULL); 67 return (NULL);
62 (void)snprintf(nbuf, sizeof(nbuf), "%u", uid); 68 (void)snprintf(nbuf, sizeof(nbuf), "%u", uid);
63 return (nbuf);
64 } 69 }
65 cp->uid = uid; 70 cp->uid = uid;
66 strlcpy(cp->name, pw->pw_name, sizeof(cp->name)); 71 if (cp->name != NULL)
72 free(cp->name);
73 cp->name = strdup(pw ? pw->pw_name : nbuf);
67 } 74 }
68 return (cp->name); 75 return (cp->name);
69} 76}
77#endif
70 78
79#ifndef HAVE_GROUP_FROM_GID
71char * 80char *
72group_from_gid(gid_t gid, int nogroup) 81group_from_gid(gid_t gid, int nogroup)
73{ 82{
74 static struct ncache { 83 static struct ncache {
75 gid_t gid; 84 gid_t gid;
76 char name[_PW_NAME_LEN + 1]; 85 char *name;
77 } c_gid[NCACHE]; 86 } c_gid[NCACHE];
78 static int gropen; 87 static int gropen;
79 static char nbuf[15]; /* 32 bits == 10 digits */ 88 static char nbuf[15]; /* 32 bits == 10 digits */
@@ -81,19 +90,23 @@ group_from_gid(gid_t gid, int nogroup)
81 struct ncache *cp; 90 struct ncache *cp;
82 91
83 cp = c_gid + (gid & MASK); 92 cp = c_gid + (gid & MASK);
84 if (cp->gid != gid || !*cp->name) { 93 if (cp->gid != gid || cp->name == NULL) {
94#ifdef HAVE_SETGROUPENT
85 if (gropen == 0) { 95 if (gropen == 0) {
86 setgroupent(1); 96 setgroupent(1);
87 gropen = 1; 97 gropen = 1;
88 } 98 }
99#endif
89 if ((gr = getgrgid(gid)) == NULL) { 100 if ((gr = getgrgid(gid)) == NULL) {
90 if (nogroup) 101 if (nogroup)
91 return (NULL); 102 return (NULL);
92 (void)snprintf(nbuf, sizeof(nbuf), "%u", gid); 103 (void)snprintf(nbuf, sizeof(nbuf), "%u", gid);
93 return (nbuf);
94 } 104 }
95 cp->gid = gid; 105 cp->gid = gid;
96 strlcpy(cp->name, gr->gr_name, sizeof(cp->name)); 106 if (cp->name != NULL)
107 free(cp->name);
108 cp->name = strdup(gr ? gr->gr_name : nbuf);
97 } 109 }
98 return (cp->name); 110 return (cp->name);
99} 111}
112#endif