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. */
25 #include <sys/types.h>
34 static char digits[] = "0123456789";
45 char name[FLEXIBLE_ARRAY_MEMBER];
48 static struct userid *user_alist;
50 /* Each entry on list is a user name for which the first lookup failed. */
51 static struct userid *nouser_alist;
53 /* Translate UID to a login name, with cache, or NULL if unresolved. */
59 struct userid *match = NULL;
61 for (tail = user_alist; tail; tail = tail->next)
63 if (tail->id.u == uid)
72 struct passwd *pwent = getpwuid (uid);
73 char const *name = pwent ? pwent->pw_name : "";
74 match = xmalloc (offsetof (struct userid, name) + strlen (name) + 1);
76 strcpy (match->name, name);
78 /* Add to the head of the list, so most recently used is first. */
79 match->next = user_alist;
83 return match->name[0] ? match->name : NULL;
86 /* Translate USER to a UID, with cache.
87 Return NULL if there is no such user.
88 (We also cache which user names have no passwd entry,
89 so we don't keep looking them up.) */
92 getuidbyname (const char *user)
97 for (tail = user_alist; tail; tail = tail->next)
98 /* Avoid a function call for the most common case. */
99 if (*tail->name == *user && !strcmp (tail->name, user))
102 for (tail = nouser_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 pwent = getpwnam (user);
109 /* We need to pretend to be the user USER, to make
110 pwd functions know about an arbitrary user name. */
111 if (!pwent && strspn (user, digits) < strlen (user))
113 setenv ("USER", user, 1);
114 pwent = getpwnam (user); /* now it will succeed */
118 tail = xmalloc (offsetof (struct userid, name) + strlen (user) + 1);
119 strcpy (tail->name, user);
121 /* Add to the head of the list, so most recently used is first. */
124 tail->id.u = pwent->pw_uid;
125 tail->next = user_alist;
130 tail->next = nouser_alist;
135 /* Use the same struct as for userids. */
136 static struct userid *group_alist;
138 /* Each entry on list is a group name for which the first lookup failed. */
139 static struct userid *nogroup_alist;
141 /* Translate GID to a group name, with cache, or NULL if unresolved. */
147 struct userid *match = NULL;
149 for (tail = group_alist; tail; tail = tail->next)
151 if (tail->id.g == gid)
160 struct group *grent = getgrgid (gid);
161 char const *name = grent ? grent->gr_name : "";
162 match = xmalloc (offsetof (struct userid, name) + strlen (name) + 1);
164 strcpy (match->name, name);
166 /* Add to the head of the list, so most recently used is first. */
167 match->next = group_alist;
171 return match->name[0] ? match->name : NULL;
174 /* Translate GROUP to a GID, with cache.
175 Return NULL if there is no such group.
176 (We also cache which group names have no group entry,
177 so we don't keep looking them up.) */
180 getgidbyname (const char *group)
185 for (tail = group_alist; tail; tail = tail->next)
186 /* Avoid a function call for the most common case. */
187 if (*tail->name == *group && !strcmp (tail->name, group))
190 for (tail = nogroup_alist; tail; tail = tail->next)
191 /* Avoid a function call for the most common case. */
192 if (*tail->name == *group && !strcmp (tail->name, group))
195 grent = getgrnam (group);
197 /* We need to pretend to belong to group GROUP, to make
198 grp functions know about an arbitrary group name. */
199 if (!grent && strspn (group, digits) < strlen (group))
201 setenv ("GROUP", group, 1);
202 grent = getgrnam (group); /* now it will succeed */
206 tail = xmalloc (offsetof (struct userid, name) + strlen (group) + 1);
207 strcpy (tail->name, group);
209 /* Add to the head of the list, so most recently used is first. */
212 tail->id.g = grent->gr_gid;
213 tail->next = group_alist;
218 tail->next = nogroup_alist;
219 nogroup_alist = tail;