1 /* idcache.c -- map user and group IDs, cached for speed
3 Copyright (C) 1985, 1988, 1989, 1990, 1997, 1998, 2003, 2005 Free
4 Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
26 #include <sys/types.h>
37 static char digits[] = "0123456789";
51 static struct userid *user_alist;
53 /* The members of this list have names not in the local passwd file. */
54 static struct userid *nouser_alist;
56 /* Translate UID to a login name, with cache, or NULL if unresolved. */
61 register struct userid *tail;
64 for (tail = user_alist; tail; tail = tail->next)
65 if (tail->id.u == uid)
68 pwent = getpwuid (uid);
69 tail = xmalloc (sizeof *tail);
71 tail->name = pwent ? xstrdup (pwent->pw_name) : NULL;
73 /* Add to the head of the list, so most recently used is first. */
74 tail->next = user_alist;
79 /* Translate USER to a UID, with cache.
80 Return NULL if there is no such user.
81 (We also cache which user names have no passwd entry,
82 so we don't keep looking them up.) */
85 getuidbyname (const char *user)
87 register struct userid *tail;
90 for (tail = user_alist; tail; tail = tail->next)
91 /* Avoid a function call for the most common case. */
92 if (*tail->name == *user && !strcmp (tail->name, user))
95 for (tail = nouser_alist; tail; tail = tail->next)
96 /* Avoid a function call for the most common case. */
97 if (*tail->name == *user && !strcmp (tail->name, user))
100 pwent = getpwnam (user);
102 /* We need to pretend to be the user USER, to make
103 pwd functions know about an arbitrary user name. */
104 if (!pwent && strspn (user, digits) < strlen (user))
106 setenv ("USER", user, 1);
107 pwent = getpwnam (user); /* now it will succeed */
111 tail = xmalloc (sizeof *tail);
112 tail->name = xstrdup (user);
114 /* Add to the head of the list, so most recently used is first. */
117 tail->id.u = pwent->pw_uid;
118 tail->next = user_alist;
123 tail->next = nouser_alist;
128 /* Use the same struct as for userids. */
129 static struct userid *group_alist;
130 static struct userid *nogroup_alist;
132 /* Translate GID to a group name, with cache, or NULL if unresolved. */
137 register struct userid *tail;
140 for (tail = group_alist; tail; tail = tail->next)
141 if (tail->id.g == gid)
144 grent = getgrgid (gid);
145 tail = xmalloc (sizeof *tail);
147 tail->name = grent ? xstrdup (grent->gr_name) : NULL;
149 /* Add to the head of the list, so most recently used is first. */
150 tail->next = group_alist;
155 /* Translate GROUP to a GID, with cache.
156 Return NULL if there is no such group.
157 (We also cache which group names have no group entry,
158 so we don't keep looking them up.) */
161 getgidbyname (const char *group)
163 register struct userid *tail;
166 for (tail = group_alist; tail; tail = tail->next)
167 /* Avoid a function call for the most common case. */
168 if (*tail->name == *group && !strcmp (tail->name, group))
171 for (tail = nogroup_alist; tail; tail = tail->next)
172 /* Avoid a function call for the most common case. */
173 if (*tail->name == *group && !strcmp (tail->name, group))
176 grent = getgrnam (group);
178 /* We need to pretend to belong to group GROUP, to make
179 grp functions know about any arbitrary group name. */
180 if (!grent && strspn (group, digits) < strlen (group))
182 setenv ("GROUP", group, 1);
183 grent = getgrnam (group); /* now it will succeed */
187 tail = xmalloc (sizeof *tail);
188 tail->name = xstrdup (group);
190 /* Add to the head of the list, so most recently used is first. */
193 tail->id.g = grent->gr_gid;
194 tail->next = group_alist;
199 tail->next = nogroup_alist;
200 nogroup_alist = tail;