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, see
17 <http://www.gnu.org/licenses/>. */
27 #include <sys/param.h>
29 #include "nss_hesiod.h"
31 /* Get the declaration of the parser function. */
33 #define STRUCTURE group
35 #include <nss/nss_files/files-parse.c>
38 _nss_hesiod_setgrent (int stayopen
)
40 return NSS_STATUS_SUCCESS
;
44 _nss_hesiod_endgrent (void)
46 return NSS_STATUS_SUCCESS
;
49 static enum nss_status
50 lookup (const char *name
, const char *type
, struct group
*grp
,
51 char *buffer
, size_t buflen
, int *errnop
)
53 struct parser_data
*data
= (void *) buffer
;
61 context
= _nss_hesiod_init ();
63 return NSS_STATUS_UNAVAIL
;
65 list
= hesiod_resolve (context
, name
, type
);
71 return err
== ENOENT
? NSS_STATUS_NOTFOUND
: NSS_STATUS_UNAVAIL
;
74 linebuflen
= buffer
+ buflen
- data
->linebuffer
;
75 len
= strlen (*list
) + 1;
78 hesiod_free_list (context
, list
);
81 return NSS_STATUS_TRYAGAIN
;
84 memcpy (data
->linebuffer
, *list
, len
);
85 hesiod_free_list (context
, list
);
88 parse_res
= _nss_files_parse_grent (buffer
, grp
, data
, buflen
, errnop
);
92 return parse_res
== -1 ? NSS_STATUS_TRYAGAIN
: NSS_STATUS_NOTFOUND
;
95 return NSS_STATUS_SUCCESS
;
99 _nss_hesiod_getgrnam_r (const char *name
, struct group
*grp
,
100 char *buffer
, size_t buflen
, int *errnop
)
102 return lookup (name
, "group", grp
, buffer
, buflen
, errnop
);
106 _nss_hesiod_getgrgid_r (gid_t gid
, struct group
*grp
,
107 char *buffer
, size_t buflen
, int *errnop
)
109 char gidstr
[21]; /* We will probably never have a gid_t with more
112 snprintf (gidstr
, sizeof gidstr
, "%d", gid
);
114 return lookup (gidstr
, "gid", grp
, buffer
, buflen
, errnop
);
118 internal_gid_in_list (const gid_t
*list
, const gid_t g
, long int len
)
130 static enum nss_status
131 internal_gid_from_group (void *context
, const char *groupname
, gid_t
*group
)
134 enum nss_status status
= NSS_STATUS_NOTFOUND
;
136 grp_res
= hesiod_resolve (context
, groupname
, "group");
137 if (grp_res
!= NULL
&& *grp_res
!= NULL
)
141 /* Skip to third field. */
142 while (*p
!= '\0' && *p
!= ':')
146 while (*p
!= '\0' && *p
!= ':')
154 while (*q
!= '\0' && *q
!= ':')
157 val
= strtol (p
, &endp
, 10);
158 if (sizeof (gid_t
) == sizeof (long int) || (gid_t
) val
== val
)
161 if (endp
== q
&& endp
!= p
)
162 status
= NSS_STATUS_SUCCESS
;
165 hesiod_free_list (context
, grp_res
);
171 _nss_hesiod_initgroups_dyn (const char *user
, gid_t group
, long int *start
,
172 long int *size
, gid_t
**groupsp
, long int limit
,
175 enum nss_status status
= NSS_STATUS_SUCCESS
;
179 gid_t
*groups
= *groupsp
;
182 context
= _nss_hesiod_init ();
184 return NSS_STATUS_UNAVAIL
;
186 list
= hesiod_resolve (context
, user
, "grplist");
190 hesiod_end (context
);
191 return errno
== ENOENT
? NSS_STATUS_NOTFOUND
: NSS_STATUS_UNAVAIL
;
194 if (!internal_gid_in_list (groups
, group
, *start
))
196 if (__builtin_expect (*start
== *size
, 0))
198 /* Need a bigger buffer. */
202 if (limit
> 0 && *size
== limit
)
203 /* We reached the maximum. */
209 newsize
= MIN (limit
, 2 * *size
);
211 newgroups
= realloc (groups
, newsize
* sizeof (*groups
));
212 if (newgroups
== NULL
)
214 *groupsp
= groups
= newgroups
;
218 groups
[(*start
)++] = group
;
230 status
= NSS_STATUS_NOTFOUND
;
233 while (*q
!= '\0' && *q
!= ':' && *q
!= ',')
240 val
= strtol (p
, &endp
, 10);
241 /* Test whether the number is representable in a variable of
242 type `gid_t'. If not ignore the number. */
243 if ((sizeof (gid_t
) == sizeof (long int) || (gid_t
) val
== val
)
246 if (*endp
== '\0' && endp
!= p
)
249 status
= NSS_STATUS_SUCCESS
;
252 status
= internal_gid_from_group (context
, p
, &group
);
254 if (status
== NSS_STATUS_SUCCESS
255 && !internal_gid_in_list (groups
, group
, *start
))
257 if (__builtin_expect (*start
== *size
, 0))
259 /* Need a bigger buffer. */
263 if (limit
> 0 && *size
== limit
)
264 /* We reached the maximum. */
270 newsize
= MIN (limit
, 2 * *size
);
272 newgroups
= realloc (groups
, newsize
* sizeof (*groups
));
273 if (newgroups
== NULL
)
275 *groupsp
= groups
= newgroups
;
279 groups
[(*start
)++] = group
;
286 __set_errno (save_errno
);
289 hesiod_free_list (context
, list
);
290 hesiod_end (context
);
292 return NSS_STATUS_SUCCESS
;