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 /* Skip to third field. */
143 while (*p
!= '\0' && *p
!= ':')
147 while (*p
!= '\0' && *p
!= ':')
155 while (*q
!= '\0' && *q
!= ':')
158 val
= strtol (p
, &endp
, 10);
159 if (sizeof (gid_t
) == sizeof (long int) || (gid_t
) val
== val
)
162 if (endp
== q
&& endp
!= p
)
163 status
= NSS_STATUS_SUCCESS
;
166 hesiod_free_list (context
, grp_res
);
172 _nss_hesiod_initgroups_dyn (const char *user
, gid_t group
, long int *start
,
173 long int *size
, gid_t
**groupsp
, long int limit
,
176 enum nss_status status
= NSS_STATUS_SUCCESS
;
180 gid_t
*groups
= *groupsp
;
183 context
= _nss_hesiod_init ();
185 return NSS_STATUS_UNAVAIL
;
187 list
= hesiod_resolve (context
, user
, "grplist");
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. */
203 if (limit
> 0 && *size
== limit
)
204 /* We reached the maximum. */
210 newsize
= MIN (limit
, 2 * *size
);
212 newgroups
= realloc (groups
, newsize
* sizeof (*groups
));
213 if (newgroups
== NULL
)
215 *groupsp
= groups
= newgroups
;
219 groups
[(*start
)++] = group
;
231 status
= NSS_STATUS_NOTFOUND
;
234 while (*q
!= '\0' && *q
!= ':' && *q
!= ',')
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
)
247 if (*endp
== '\0' && endp
!= p
)
250 status
= NSS_STATUS_SUCCESS
;
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. */
264 if (limit
> 0 && *size
== limit
)
265 /* We reached the maximum. */
271 newsize
= MIN (limit
, 2 * *size
);
273 newgroups
= realloc (groups
, newsize
* sizeof (*groups
));
274 if (newgroups
== NULL
)
276 *groupsp
= groups
= newgroups
;
280 groups
[(*start
)++] = group
;
287 __set_errno (save_errno
);
290 hesiod_free_list (context
, list
);
291 hesiod_end (context
);
293 return NSS_STATUS_SUCCESS
;