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>
35 static char digits[] = "0123456789";
49 static struct userid *user_alist;
51 /* The members of this list have names not in the local passwd file. */
52 static struct userid *nouser_alist;
54 /* Translate UID to a login name, with cache, or NULL if unresolved. */
59 register struct userid *tail;
62 for (tail = user_alist; tail; tail = tail->next)
63 if (tail->id.u == uid)
66 pwent = getpwuid (uid);
67 tail = xmalloc (sizeof *tail);
69 tail->name = pwent ? xstrdup (pwent->pw_name) : NULL;
71 /* Add to the head of the list, so most recently used is first. */
72 tail->next = user_alist;
77 /* Translate USER to a UID, with cache.
78 Return NULL if there is no such user.
79 (We also cache which user names have no passwd entry,
80 so we don't keep looking them up.) */
83 getuidbyname (const char *user)
85 register struct userid *tail;
88 for (tail = user_alist; tail; tail = tail->next)
89 /* Avoid a function call for the most common case. */
90 if (*tail->name == *user && !strcmp (tail->name, user))
93 for (tail = nouser_alist; tail; tail = tail->next)
94 /* Avoid a function call for the most common case. */
95 if (*tail->name == *user && !strcmp (tail->name, user))
98 pwent = getpwnam (user);
100 /* We need to pretend to be the user USER, to make
101 pwd functions know about an arbitrary user name. */
102 if (!pwent && strspn (user, digits) < strlen (user))
104 setenv ("USER", user, 1);
105 pwent = getpwnam (user); /* now it will succeed */
109 tail = xmalloc (sizeof *tail);
110 tail->name = xstrdup (user);
112 /* Add to the head of the list, so most recently used is first. */
115 tail->id.u = pwent->pw_uid;
116 tail->next = user_alist;
121 tail->next = nouser_alist;
126 /* Use the same struct as for userids. */
127 static struct userid *group_alist;
128 static struct userid *nogroup_alist;
130 /* Translate GID to a group name, with cache, or NULL if unresolved. */
135 register struct userid *tail;
138 for (tail = group_alist; tail; tail = tail->next)
139 if (tail->id.g == gid)
142 grent = getgrgid (gid);
143 tail = xmalloc (sizeof *tail);
145 tail->name = grent ? xstrdup (grent->gr_name) : NULL;
147 /* Add to the head of the list, so most recently used is first. */
148 tail->next = group_alist;
153 /* Translate GROUP to a GID, with cache.
154 Return NULL if there is no such group.
155 (We also cache which group names have no group entry,
156 so we don't keep looking them up.) */
159 getgidbyname (const char *group)
161 register struct userid *tail;
164 for (tail = group_alist; tail; tail = tail->next)
165 /* Avoid a function call for the most common case. */
166 if (*tail->name == *group && !strcmp (tail->name, group))
169 for (tail = nogroup_alist; tail; tail = tail->next)
170 /* Avoid a function call for the most common case. */
171 if (*tail->name == *group && !strcmp (tail->name, group))
174 grent = getgrnam (group);
176 /* We need to pretend to belong to group GROUP, to make
177 grp functions know about any arbitrary group name. */
178 if (!grent && strspn (group, digits) < strlen (group))
180 setenv ("GROUP", group, 1);
181 grent = getgrnam (group); /* now it will succeed */
185 tail = xmalloc (sizeof *tail);
186 tail->name = xstrdup (group);
188 /* Add to the head of the list, so most recently used is first. */
191 tail->id.g = grent->gr_gid;
192 tail->next = group_alist;
197 tail->next = nogroup_alist;
198 nogroup_alist = tail;