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
28 #include <sys/param.h>
30 #include "nss_hesiod.h"
32 /* Get the declaration of the parser function. */
34 #define STRUCTURE group
36 #include <nss/nss_files/files-parse.c>
39 _nss_hesiod_setgrent (int stayopen
)
41 return NSS_STATUS_SUCCESS
;
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
;
62 context
= _nss_hesiod_init ();
64 return NSS_STATUS_UNAVAIL
;
66 list
= hesiod_resolve (context
, name
, type
);
72 return err
== ENOENT
? NSS_STATUS_NOTFOUND
: NSS_STATUS_UNAVAIL
;
75 linebuflen
= buffer
+ buflen
- data
->linebuffer
;
76 len
= strlen (*list
) + 1;
79 hesiod_free_list (context
, list
);
82 return NSS_STATUS_TRYAGAIN
;
85 memcpy (data
->linebuffer
, *list
, len
);
86 hesiod_free_list (context
, list
);
89 parse_res
= _nss_files_parse_grent (buffer
, grp
, data
, buflen
, errnop
);
93 return parse_res
== -1 ? NSS_STATUS_TRYAGAIN
: NSS_STATUS_NOTFOUND
;
96 return NSS_STATUS_SUCCESS
;
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
);
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
113 snprintf (gidstr
, sizeof gidstr
, "%d", gid
);
115 return lookup (gidstr
, "gid", grp
, buffer
, buflen
, errnop
);
119 internal_gid_in_list (const gid_t
*list
, const gid_t g
, long int len
)
131 static enum nss_status
132 internal_gid_from_group (void *context
, const char *groupname
, gid_t
*group
)
135 enum nss_status status
= NSS_STATUS_NOTFOUND
;
137 grp_res
= hesiod_resolve (context
, groupname
, "group");
138 if (grp_res
!= NULL
&& *grp_res
!= NULL
)
142 while (*p
!= '\0' && *p
!= ':')
144 while (*p
!= '\0' && *p
== ':')
146 while (*p
!= '\0' && *p
!= ':')
148 while (*p
!= '\0' && *p
== ':')
157 while (*q
!= '\0' && *q
!= ':')
160 val
= strtol (p
, &endp
, 10);
161 if (sizeof (gid_t
) == sizeof (long int) || (gid_t
) val
== val
)
164 if (endp
== q
&& endp
!= p
)
165 status
= NSS_STATUS_SUCCESS
;
168 hesiod_free_list (context
, grp_res
);
174 _nss_hesiod_initgroups_dyn (const char *user
, gid_t group
, long int *start
,
175 long int *size
, gid_t
**groupsp
, long int limit
,
178 enum nss_status status
= NSS_STATUS_SUCCESS
;
182 gid_t
*groups
= *groupsp
;
185 context
= _nss_hesiod_init ();
187 return NSS_STATUS_UNAVAIL
;
189 list
= hesiod_resolve (context
, user
, "grplist");
193 hesiod_end (context
);
194 return errno
== ENOENT
? NSS_STATUS_NOTFOUND
: NSS_STATUS_UNAVAIL
;
197 if (!internal_gid_in_list (groups
, group
, *start
))
199 if (__builtin_expect (*start
== *size
, 0))
201 /* Need a bigger buffer. */
205 if (limit
> 0 && *size
== limit
)
206 /* We reached the maximum. */
212 newsize
= MIN (limit
, 2 * *size
);
214 newgroups
= realloc (groups
, newsize
* sizeof (*groups
));
215 if (newgroups
== NULL
)
217 *groupsp
= groups
= newgroups
;
221 groups
[(*start
)++] = group
;
233 status
= NSS_STATUS_NOTFOUND
;
236 while (*q
!= '\0' && *q
!= ':' && *q
!= ',')
243 val
= strtol (p
, &endp
, 10);
244 /* Test whether the number is representable in a variable of
245 type `gid_t'. If not ignore the number. */
246 if ((sizeof (gid_t
) == sizeof (long int) || (gid_t
) val
== val
)
249 if (*endp
== '\0' && endp
!= p
)
252 status
= NSS_STATUS_SUCCESS
;
255 status
= internal_gid_from_group (context
, p
, &group
);
257 if (status
== NSS_STATUS_SUCCESS
258 && !internal_gid_in_list (groups
, group
, *start
))
260 if (__builtin_expect (*start
== *size
, 0))
262 /* Need a bigger buffer. */
266 if (limit
> 0 && *size
== limit
)
267 /* We reached the maximum. */
273 newsize
= MIN (limit
, 2 * *size
);
275 newgroups
= realloc (groups
, newsize
* sizeof (*groups
));
276 if (newgroups
== NULL
)
278 *groupsp
= groups
= newgroups
;
282 groups
[(*start
)++] = group
;
289 __set_errno (save_errno
);
292 hesiod_free_list (context
, list
);
293 hesiod_end (context
);
295 return NSS_STATUS_SUCCESS
;