1 /* idcache.c -- map user and group IDs, cached for speed
3 Copyright (C) 1985, 1988, 1989, 1990, 1997, 1998, 2003, 2005, 2006
4 Free 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. */
24 #include <sys/types.h>
33 static char digits[] = "0123456789";
47 static struct userid *user_alist;
49 /* The members of this list have names not in the local passwd file. */
50 static struct userid *nouser_alist;
52 /* Translate UID to a login name, with cache, or NULL if unresolved. */
57 register struct userid *tail;
60 for (tail = user_alist; tail; tail = tail->next)
61 if (tail->id.u == uid)
64 pwent = getpwuid (uid);
65 tail = xmalloc (sizeof *tail);
67 tail->name = pwent ? xstrdup (pwent->pw_name) : NULL;
69 /* Add to the head of the list, so most recently used is first. */
70 tail->next = user_alist;
75 /* Translate USER to a UID, with cache.
76 Return NULL if there is no such user.
77 (We also cache which user names have no passwd entry,
78 so we don't keep looking them up.) */
81 getuidbyname (const char *user)
83 register struct userid *tail;
86 for (tail = user_alist; tail; tail = tail->next)
87 /* Avoid a function call for the most common case. */
88 if (*tail->name == *user && !strcmp (tail->name, user))
91 for (tail = nouser_alist; tail; tail = tail->next)
92 /* Avoid a function call for the most common case. */
93 if (*tail->name == *user && !strcmp (tail->name, user))
96 pwent = getpwnam (user);
98 /* We need to pretend to be the user USER, to make
99 pwd functions know about an arbitrary user name. */
100 if (!pwent && strspn (user, digits) < strlen (user))
102 setenv ("USER", user, 1);
103 pwent = getpwnam (user); /* now it will succeed */
107 tail = xmalloc (sizeof *tail);
108 tail->name = xstrdup (user);
110 /* Add to the head of the list, so most recently used is first. */
113 tail->id.u = pwent->pw_uid;
114 tail->next = user_alist;
119 tail->next = nouser_alist;
124 /* Use the same struct as for userids. */
125 static struct userid *group_alist;
126 static struct userid *nogroup_alist;
128 /* Translate GID to a group name, with cache, or NULL if unresolved. */
133 register struct userid *tail;
136 for (tail = group_alist; tail; tail = tail->next)
137 if (tail->id.g == gid)
140 grent = getgrgid (gid);
141 tail = xmalloc (sizeof *tail);
143 tail->name = grent ? xstrdup (grent->gr_name) : NULL;
145 /* Add to the head of the list, so most recently used is first. */
146 tail->next = group_alist;
151 /* Translate GROUP to a GID, with cache.
152 Return NULL if there is no such group.
153 (We also cache which group names have no group entry,
154 so we don't keep looking them up.) */
157 getgidbyname (const char *group)
159 register struct userid *tail;
162 for (tail = group_alist; tail; tail = tail->next)
163 /* Avoid a function call for the most common case. */
164 if (*tail->name == *group && !strcmp (tail->name, group))
167 for (tail = nogroup_alist; tail; tail = tail->next)
168 /* Avoid a function call for the most common case. */
169 if (*tail->name == *group && !strcmp (tail->name, group))
172 grent = getgrnam (group);
174 /* We need to pretend to belong to group GROUP, to make
175 grp functions know about any arbitrary group name. */
176 if (!grent && strspn (group, digits) < strlen (group))
178 setenv ("GROUP", group, 1);
179 grent = getgrnam (group); /* now it will succeed */
183 tail = xmalloc (sizeof *tail);
184 tail->name = xstrdup (group);
186 /* Add to the head of the list, so most recently used is first. */
189 tail->id.g = grent->gr_gid;
190 tail->next = group_alist;
195 tail->next = nogroup_alist;
196 nogroup_alist = tail;