1 /* idcache.c -- map user and group IDs, cached for speed
2 Copyright (C) 1985, 1988, 1989, 1990, 1997, 1998, 2003 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. */
25 #include <sys/types.h>
35 #ifndef _POSIX_VERSION
36 struct passwd *getpwuid ();
37 struct passwd *getpwnam ();
38 struct group *getgrgid ();
39 struct group *getgrnam ();
43 static char digits[] = "0123456789";
57 static struct userid *user_alist;
59 /* The members of this list have names not in the local passwd file. */
60 static struct userid *nouser_alist;
62 /* Translate UID to a login name, with cache, or NULL if unresolved. */
67 register struct userid *tail;
70 for (tail = user_alist; tail; tail = tail->next)
71 if (tail->id.u == uid)
74 pwent = getpwuid (uid);
75 tail = xmalloc (sizeof (struct userid));
77 tail->name = pwent ? xstrdup (pwent->pw_name) : NULL;
79 /* Add to the head of the list, so most recently used is first. */
80 tail->next = user_alist;
85 /* Translate USER to a UID, with cache.
86 Return NULL if there is no such user.
87 (We also cache which user names have no passwd entry,
88 so we don't keep looking them up.) */
91 getuidbyname (const char *user)
93 register struct userid *tail;
96 for (tail = user_alist; tail; tail = tail->next)
97 /* Avoid a function call for the most common case. */
98 if (*tail->name == *user && !strcmp (tail->name, user))
101 for (tail = nouser_alist; tail; tail = tail->next)
102 /* Avoid a function call for the most common case. */
103 if (*tail->name == *user && !strcmp (tail->name, user))
106 pwent = getpwnam (user);
108 /* We need to pretend to be the user USER, to make
109 pwd functions know about an arbitrary user name. */
110 if (!pwent && strspn (user, digits) < strlen (user))
112 setenv ("USER", user, 1);
113 pwent = getpwnam (user); /* now it will succeed */
117 tail = xmalloc (sizeof (struct userid));
118 tail->name = xstrdup (user);
120 /* Add to the head of the list, so most recently used is first. */
123 tail->id.u = pwent->pw_uid;
124 tail->next = user_alist;
129 tail->next = nouser_alist;
134 /* Use the same struct as for userids. */
135 static struct userid *group_alist;
136 static struct userid *nogroup_alist;
138 /* Translate GID to a group name, with cache, or NULL if unresolved. */
143 register struct userid *tail;
146 for (tail = group_alist; tail; tail = tail->next)
147 if (tail->id.g == gid)
150 grent = getgrgid (gid);
151 tail = xmalloc (sizeof (struct userid));
153 tail->name = grent ? xstrdup (grent->gr_name) : NULL;
155 /* Add to the head of the list, so most recently used is first. */
156 tail->next = group_alist;
161 /* Translate GROUP to a GID, with cache.
162 Return NULL if there is no such group.
163 (We also cache which group names have no group entry,
164 so we don't keep looking them up.) */
167 getgidbyname (const char *group)
169 register struct userid *tail;
172 for (tail = group_alist; tail; tail = tail->next)
173 /* Avoid a function call for the most common case. */
174 if (*tail->name == *group && !strcmp (tail->name, group))
177 for (tail = nogroup_alist; tail; tail = tail->next)
178 /* Avoid a function call for the most common case. */
179 if (*tail->name == *group && !strcmp (tail->name, group))
182 grent = getgrnam (group);
184 /* We need to pretend to belong to group GROUP, to make
185 grp functions know about any arbitrary group name. */
186 if (!grent && strspn (group, digits) < strlen (group))
188 setenv ("GROUP", group, 1);
189 grent = getgrnam (group); /* now it will succeed */
193 tail = xmalloc (sizeof (struct userid));
194 tail->name = xstrdup (group);
196 /* Add to the head of the list, so most recently used is first. */
199 tail->id.g = grent->gr_gid;
200 tail->next = group_alist;
205 tail->next = nogroup_alist;
206 nogroup_alist = tail;