c00ee869ded1696f511d764136f40df1511dbf71
[gnulib.git] / lib / idcache.c
1 /* idcache.c -- map user and group IDs, cached for speed
2    Copyright (C) 1985, 1988, 1989, 1990, 1997, 1998 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <pwd.h>
25 #include <grp.h>
26
27 #if STDC_HEADERS || HAVE_STRING_H
28 # include <string.h>
29 #else
30 # include <strings.h>
31 #endif
32
33 #if HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36
37 #ifndef _POSIX_VERSION
38 struct passwd *getpwuid ();
39 struct passwd *getpwnam ();
40 struct group *getgrgid ();
41 struct group *getgrnam ();
42 #endif
43
44 char *xmalloc ();
45 char *xstrdup ();
46
47 struct userid
48 {
49   union
50     {
51       uid_t u;
52       gid_t g;
53     } id;
54   char *name;
55   struct userid *next;
56 };
57
58 static struct userid *user_alist;
59
60 /* The members of this list have names not in the local passwd file.  */
61 static struct userid *nouser_alist;
62
63 /* Translate UID to a login name, with cache, or NULL if unresolved.  */
64
65 char *
66 getuser (uid_t uid)
67 {
68   register struct userid *tail;
69   struct passwd *pwent;
70
71   for (tail = user_alist; tail; tail = tail->next)
72     if (tail->id.u == uid)
73       return tail->name;
74
75   pwent = getpwuid (uid);
76   tail = (struct userid *) xmalloc (sizeof (struct userid));
77   tail->id.u = uid;
78   tail->name = pwent ? xstrdup (pwent->pw_name) : NULL;
79
80   /* Add to the head of the list, so most recently used is first.  */
81   tail->next = user_alist;
82   user_alist = tail;
83   return tail->name;
84 }
85
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.)  */
90
91 uid_t *
92 getuidbyname (const char *user)
93 {
94   register struct userid *tail;
95   struct passwd *pwent;
96
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))
100       return &tail->id.u;
101
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))
105       return 0;
106
107   pwent = getpwnam (user);
108
109   tail = (struct userid *) xmalloc (sizeof (struct userid));
110   tail->name = xstrdup (user);
111
112   /* Add to the head of the list, so most recently used is first.  */
113   if (pwent)
114     {
115       tail->id.u = pwent->pw_uid;
116       tail->next = user_alist;
117       user_alist = tail;
118       return &tail->id.u;
119     }
120
121   tail->next = nouser_alist;
122   nouser_alist = tail;
123   return 0;
124 }
125
126 /* Use the same struct as for userids.  */
127 static struct userid *group_alist;
128 static struct userid *nogroup_alist;
129
130 /* Translate GID to a group name, with cache, or NULL if unresolved.  */
131
132 char *
133 getgroup (gid_t gid)
134 {
135   register struct userid *tail;
136   struct group *grent;
137
138   for (tail = group_alist; tail; tail = tail->next)
139     if (tail->id.g == gid)
140       return tail->name;
141
142   grent = getgrgid (gid);
143   tail = (struct userid *) xmalloc (sizeof (struct userid));
144   tail->id.g = gid;
145   tail->name = grent ? xstrdup (grent->gr_name) : NULL;
146
147   /* Add to the head of the list, so most recently used is first.  */
148   tail->next = group_alist;
149   group_alist = tail;
150   return tail->name;
151 }
152
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.)  */
157
158 gid_t *
159 getgidbyname (const char *group)
160 {
161   register struct userid *tail;
162   struct group *grent;
163
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))
167       return &tail->id.g;
168
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))
172       return 0;
173
174   grent = getgrnam (group);
175
176   tail = (struct userid *) xmalloc (sizeof (struct userid));
177   tail->name = xstrdup (group);
178
179   /* Add to the head of the list, so most recently used is first.  */
180   if (grent)
181     {
182       tail->id.g = grent->gr_gid;
183       tail->next = group_alist;
184       group_alist = tail;
185       return &tail->id.g;
186     }
187
188   tail->next = nogroup_alist;
189   nogroup_alist = tail;
190   return 0;
191 }