Add missing bz number.
[glibc.git] / hesiod / nss_hesiod / hesiod-grp.c
blobf0c8c31e0605d5fca9f8785af7851cd50866f9ee
1 /* Copyright (C) 1997, 1999, 2000, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <ctype.h>
21 #include <errno.h>
22 #include <grp.h>
23 #include <hesiod.h>
24 #include <nss.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/param.h>
30 #include "nss_hesiod.h"
32 /* Get the declaration of the parser function. */
33 #define ENTNAME grent
34 #define STRUCTURE group
35 #define EXTERN_PARSER
36 #include <nss/nss_files/files-parse.c>
38 enum nss_status
39 _nss_hesiod_setgrent (int stayopen)
41 return NSS_STATUS_SUCCESS;
44 enum nss_status
45 _nss_hesiod_endgrent (void)
47 return NSS_STATUS_SUCCESS;
50 static enum nss_status
51 lookup (const char *name, const char *type, struct group *grp,
52 char *buffer, size_t buflen, int *errnop)
54 struct parser_data *data = (void *) buffer;
55 size_t linebuflen;
56 void *context;
57 char **list;
58 int parse_res;
59 size_t len;
60 int olderr = errno;
62 context = _nss_hesiod_init ();
63 if (context == NULL)
64 return NSS_STATUS_UNAVAIL;
66 list = hesiod_resolve (context, name, type);
67 if (list == NULL)
69 int err = errno;
70 hesiod_end (context);
71 __set_errno (olderr);
72 return err == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
75 linebuflen = buffer + buflen - data->linebuffer;
76 len = strlen (*list) + 1;
77 if (linebuflen < len)
79 hesiod_free_list (context, list);
80 hesiod_end (context);
81 *errnop = ERANGE;
82 return NSS_STATUS_TRYAGAIN;
85 memcpy (data->linebuffer, *list, len);
86 hesiod_free_list (context, list);
87 hesiod_end (context);
89 parse_res = _nss_files_parse_grent (buffer, grp, data, buflen, errnop);
90 if (parse_res < 1)
92 __set_errno (olderr);
93 return parse_res == -1 ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND;
96 return NSS_STATUS_SUCCESS;
99 enum nss_status
100 _nss_hesiod_getgrnam_r (const char *name, struct group *grp,
101 char *buffer, size_t buflen, int *errnop)
103 return lookup (name, "group", grp, buffer, buflen, errnop);
106 enum nss_status
107 _nss_hesiod_getgrgid_r (gid_t gid, struct group *grp,
108 char *buffer, size_t buflen, int *errnop)
110 char gidstr[21]; /* We will probably never have a gid_t with more
111 than 64 bits. */
113 snprintf (gidstr, sizeof gidstr, "%d", gid);
115 return lookup (gidstr, "gid", grp, buffer, buflen, errnop);
118 static int
119 internal_gid_in_list (const gid_t *list, const gid_t g, long int len)
121 while (len > 0)
123 if (*list == g)
124 return 1;
125 --len;
126 ++list;
128 return 0;
131 static enum nss_status
132 internal_gid_from_group (void *context, const char *groupname, gid_t *group)
134 char **grp_res;
135 enum nss_status status = NSS_STATUS_NOTFOUND;
137 grp_res = hesiod_resolve (context, groupname, "group");
138 if (grp_res != NULL && *grp_res != NULL)
140 char *p = *grp_res;
142 /* Skip to third field. */
143 while (*p != '\0' && *p != ':')
144 ++p;
145 if (*p != '\0')
146 ++p;
147 while (*p != '\0' && *p != ':')
148 ++p;
149 if (*p != '\0')
151 char *endp;
152 char *q = ++p;
153 long int val;
155 while (*q != '\0' && *q != ':')
156 ++q;
158 val = strtol (p, &endp, 10);
159 if (sizeof (gid_t) == sizeof (long int) || (gid_t) val == val)
161 *group = val;
162 if (endp == q && endp != p)
163 status = NSS_STATUS_SUCCESS;
166 hesiod_free_list (context, grp_res);
168 return status;
171 enum nss_status
172 _nss_hesiod_initgroups_dyn (const char *user, gid_t group, long int *start,
173 long int *size, gid_t **groupsp, long int limit,
174 int *errnop)
176 enum nss_status status = NSS_STATUS_SUCCESS;
177 char **list = NULL;
178 char *p;
179 void *context;
180 gid_t *groups = *groupsp;
181 int save_errno;
183 context = _nss_hesiod_init ();
184 if (context == NULL)
185 return NSS_STATUS_UNAVAIL;
187 list = hesiod_resolve (context, user, "grplist");
189 if (list == NULL)
191 hesiod_end (context);
192 return errno == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
195 if (!internal_gid_in_list (groups, group, *start))
197 if (__builtin_expect (*start == *size, 0))
199 /* Need a bigger buffer. */
200 gid_t *newgroups;
201 long int newsize;
203 if (limit > 0 && *size == limit)
204 /* We reached the maximum. */
205 goto done;
207 if (limit <= 0)
208 newsize = 2 * *size;
209 else
210 newsize = MIN (limit, 2 * *size);
212 newgroups = realloc (groups, newsize * sizeof (*groups));
213 if (newgroups == NULL)
214 goto done;
215 *groupsp = groups = newgroups;
216 *size = newsize;
219 groups[(*start)++] = group;
222 save_errno = errno;
224 p = *list;
225 while (*p != '\0')
227 char *endp;
228 char *q;
229 long int val;
231 status = NSS_STATUS_NOTFOUND;
233 q = p;
234 while (*q != '\0' && *q != ':' && *q != ',')
235 ++q;
237 if (*q != '\0')
238 *q++ = '\0';
240 __set_errno (0);
241 val = strtol (p, &endp, 10);
242 /* Test whether the number is representable in a variable of
243 type `gid_t'. If not ignore the number. */
244 if ((sizeof (gid_t) == sizeof (long int) || (gid_t) val == val)
245 && errno == 0)
247 if (*endp == '\0' && endp != p)
249 group = val;
250 status = NSS_STATUS_SUCCESS;
252 else
253 status = internal_gid_from_group (context, p, &group);
255 if (status == NSS_STATUS_SUCCESS
256 && !internal_gid_in_list (groups, group, *start))
258 if (__builtin_expect (*start == *size, 0))
260 /* Need a bigger buffer. */
261 gid_t *newgroups;
262 long int newsize;
264 if (limit > 0 && *size == limit)
265 /* We reached the maximum. */
266 goto done;
268 if (limit <= 0)
269 newsize = 2 * *size;
270 else
271 newsize = MIN (limit, 2 * *size);
273 newgroups = realloc (groups, newsize * sizeof (*groups));
274 if (newgroups == NULL)
275 goto done;
276 *groupsp = groups = newgroups;
277 *size = newsize;
280 groups[(*start)++] = group;
284 p = q;
287 __set_errno (save_errno);
289 done:
290 hesiod_free_list (context, list);
291 hesiod_end (context);
293 return NSS_STATUS_SUCCESS;