1 /* idcache.c -- map user and group IDs, cached for speed
3 Copyright (C) 1985, 1988-1990, 1997-1998, 2003, 2005-2007, 2009-2020 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 3 of the License, or
9 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
30 #include "flexmember.h"
34 static char digits
[] = "0123456789";
45 char name
[FLEXIBLE_ARRAY_MEMBER
];
48 /* FIXME: provide a function to free any malloc'd storage and reset lists,
49 so that an application can use code like this just before exiting:
50 #if defined GCC_LINT || defined lint
55 static struct userid
*user_alist
;
57 /* Each entry on list is a user name for which the first lookup failed. */
58 static struct userid
*nouser_alist
;
60 /* Use the same struct as for userids. */
61 static struct userid
*group_alist
;
63 /* Each entry on list is a group name for which the first lookup failed. */
64 static struct userid
*nogroup_alist
;
66 /* Translate UID to a login name, with cache, or NULL if unresolved. */
72 struct userid
*match
= NULL
;
74 for (tail
= user_alist
; tail
; tail
= tail
->next
)
76 if (tail
->id
.u
== uid
)
85 struct passwd
*pwent
= getpwuid (uid
);
86 char const *name
= pwent
? pwent
->pw_name
: "";
87 match
= xmalloc (FLEXSIZEOF (struct userid
, name
, strlen (name
) + 1));
89 strcpy (match
->name
, name
);
91 /* Add to the head of the list, so most recently used is first. */
92 match
->next
= user_alist
;
96 return match
->name
[0] ? match
->name
: NULL
;
99 /* Translate USER to a UID, with cache.
100 Return NULL if there is no such user.
101 (We also cache which user names have no passwd entry,
102 so we don't keep looking them up.) */
105 getuidbyname (const char *user
)
108 struct passwd
*pwent
;
110 for (tail
= user_alist
; tail
; tail
= tail
->next
)
111 /* Avoid a function call for the most common case. */
112 if (*tail
->name
== *user
&& !strcmp (tail
->name
, user
))
115 for (tail
= nouser_alist
; tail
; tail
= tail
->next
)
116 /* Avoid a function call for the most common case. */
117 if (*tail
->name
== *user
&& !strcmp (tail
->name
, user
))
120 pwent
= getpwnam (user
);
122 /* We need to pretend to be the user USER, to make
123 pwd functions know about an arbitrary user name. */
124 if (!pwent
&& strspn (user
, digits
) < strlen (user
))
126 setenv ("USER", user
, 1);
127 pwent
= getpwnam (user
); /* now it will succeed */
131 tail
= xmalloc (FLEXSIZEOF (struct userid
, name
, strlen (user
) + 1));
132 strcpy (tail
->name
, user
);
134 /* Add to the head of the list, so most recently used is first. */
137 tail
->id
.u
= pwent
->pw_uid
;
138 tail
->next
= user_alist
;
143 tail
->next
= nouser_alist
;
148 /* Translate GID to a group name, with cache, or NULL if unresolved. */
154 struct userid
*match
= NULL
;
156 for (tail
= group_alist
; tail
; tail
= tail
->next
)
158 if (tail
->id
.g
== gid
)
167 struct group
*grent
= getgrgid (gid
);
168 char const *name
= grent
? grent
->gr_name
: "";
169 match
= xmalloc (FLEXSIZEOF (struct userid
, name
, strlen (name
) + 1));
171 strcpy (match
->name
, name
);
173 /* Add to the head of the list, so most recently used is first. */
174 match
->next
= group_alist
;
178 return match
->name
[0] ? match
->name
: NULL
;
181 /* Translate GROUP to a GID, with cache.
182 Return NULL if there is no such group.
183 (We also cache which group names have no group entry,
184 so we don't keep looking them up.) */
187 getgidbyname (const char *group
)
192 for (tail
= group_alist
; tail
; tail
= tail
->next
)
193 /* Avoid a function call for the most common case. */
194 if (*tail
->name
== *group
&& !strcmp (tail
->name
, group
))
197 for (tail
= nogroup_alist
; tail
; tail
= tail
->next
)
198 /* Avoid a function call for the most common case. */
199 if (*tail
->name
== *group
&& !strcmp (tail
->name
, group
))
202 grent
= getgrnam (group
);
204 /* We need to pretend to belong to group GROUP, to make
205 grp functions know about an arbitrary group name. */
206 if (!grent
&& strspn (group
, digits
) < strlen (group
))
208 setenv ("GROUP", group
, 1);
209 grent
= getgrnam (group
); /* now it will succeed */
213 tail
= xmalloc (FLEXSIZEOF (struct userid
, name
, strlen (group
) + 1));
214 strcpy (tail
->name
, group
);
216 /* Add to the head of the list, so most recently used is first. */
219 tail
->id
.g
= grent
->gr_gid
;
220 tail
->next
= group_alist
;
225 tail
->next
= nogroup_alist
;
226 nogroup_alist
= tail
;