1 /* idcache.c -- map user and group IDs, cached for speed
2 Copyright (C) 1985, 1988, 1989, 1990, 1997, 1998 Free Software
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include <sys/types.h>
28 #if STDC_HEADERS || HAVE_STRING_H
38 #ifndef _POSIX_VERSION
39 struct passwd
*getpwuid ();
40 struct passwd
*getpwnam ();
41 struct group
*getgrgid ();
42 struct group
*getgrnam ();
49 static char digits
[] = "0123456789";
63 static struct userid
*user_alist
;
65 /* The members of this list have names not in the local passwd file. */
66 static struct userid
*nouser_alist
;
68 /* Translate UID to a login name, with cache, or NULL if unresolved. */
73 register struct userid
*tail
;
76 for (tail
= user_alist
; tail
; tail
= tail
->next
)
77 if (tail
->id
.u
== uid
)
80 pwent
= getpwuid (uid
);
81 tail
= (struct userid
*) xmalloc (sizeof (struct userid
));
83 tail
->name
= pwent
? xstrdup (pwent
->pw_name
) : NULL
;
85 /* Add to the head of the list, so most recently used is first. */
86 tail
->next
= user_alist
;
91 /* Translate USER to a UID, with cache.
92 Return NULL if there is no such user.
93 (We also cache which user names have no passwd entry,
94 so we don't keep looking them up.) */
97 getuidbyname (const char *user
)
99 register struct userid
*tail
;
100 struct passwd
*pwent
;
102 for (tail
= user_alist
; tail
; tail
= tail
->next
)
103 /* Avoid a function call for the most common case. */
104 if (*tail
->name
== *user
&& !strcmp (tail
->name
, user
))
107 for (tail
= nouser_alist
; tail
; tail
= tail
->next
)
108 /* Avoid a function call for the most common case. */
109 if (*tail
->name
== *user
&& !strcmp (tail
->name
, user
))
112 pwent
= getpwnam (user
);
114 /* We need to pretend to be the user USER, to make
115 pwd functions know about an arbitrary user name. */
116 if (!pwent
&& strspn (user
, digits
) < strlen (user
))
118 setenv ("USER", user
, 1);
119 pwent
= getpwnam (user
); /* now it will succeed */
123 tail
= (struct userid
*) xmalloc (sizeof (struct userid
));
124 tail
->name
= xstrdup (user
);
126 /* Add to the head of the list, so most recently used is first. */
129 tail
->id
.u
= pwent
->pw_uid
;
130 tail
->next
= user_alist
;
135 tail
->next
= nouser_alist
;
140 /* Use the same struct as for userids. */
141 static struct userid
*group_alist
;
142 static struct userid
*nogroup_alist
;
144 /* Translate GID to a group name, with cache, or NULL if unresolved. */
149 register struct userid
*tail
;
152 for (tail
= group_alist
; tail
; tail
= tail
->next
)
153 if (tail
->id
.g
== gid
)
156 grent
= getgrgid (gid
);
157 tail
= (struct userid
*) xmalloc (sizeof (struct userid
));
159 tail
->name
= grent
? xstrdup (grent
->gr_name
) : NULL
;
161 /* Add to the head of the list, so most recently used is first. */
162 tail
->next
= group_alist
;
167 /* Translate GROUP to a GID, with cache.
168 Return NULL if there is no such group.
169 (We also cache which group names have no group entry,
170 so we don't keep looking them up.) */
173 getgidbyname (const char *group
)
175 register struct userid
*tail
;
178 for (tail
= group_alist
; tail
; tail
= tail
->next
)
179 /* Avoid a function call for the most common case. */
180 if (*tail
->name
== *group
&& !strcmp (tail
->name
, group
))
183 for (tail
= nogroup_alist
; tail
; tail
= tail
->next
)
184 /* Avoid a function call for the most common case. */
185 if (*tail
->name
== *group
&& !strcmp (tail
->name
, group
))
188 grent
= getgrnam (group
);
190 /* We need to pretend to belong to group GROUP, to make
191 grp functions know about any arbitrary group name. */
192 if (!grent
&& strspn (group
, digits
) < strlen (group
))
194 setenv ("GROUP", group
, 1);
195 grent
= getgrnam (group
); /* now it will succeed */
199 tail
= (struct userid
*) xmalloc (sizeof (struct userid
));
200 tail
->name
= xstrdup (group
);
202 /* Add to the head of the list, so most recently used is first. */
205 tail
->id
.g
= grent
->gr_gid
;
206 tail
->next
= group_alist
;
211 tail
->next
= nogroup_alist
;
212 nogroup_alist
= tail
;