]>
git.ipfire.org Git - thirdparty/glibc.git/blob - hesiod/nss_hesiod/hesiod-grp.c
fdaf1f26f7711b23ae333a3994e6935e70bfcc13
1 /* Copyright (C) 1997, 1999, 2000 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
;
61 context
= _nss_hesiod_init ();
63 return NSS_STATUS_UNAVAIL
;
65 list
= hesiod_resolve (context
, name
, type
);
69 return errno
== ENOENT
? NSS_STATUS_NOTFOUND
: NSS_STATUS_UNAVAIL
;
72 linebuflen
= buffer
+ buflen
- data
->linebuffer
;
73 len
= strlen (*list
) + 1;
76 hesiod_free_list (context
, list
);
79 return NSS_STATUS_TRYAGAIN
;
82 memcpy (data
->linebuffer
, *list
, len
);
83 hesiod_free_list (context
, list
);
86 parse_res
= _nss_files_parse_grent (buffer
, grp
, data
, buflen
, errnop
);
88 return parse_res
== -1 ? NSS_STATUS_TRYAGAIN
: NSS_STATUS_NOTFOUND
;
90 return NSS_STATUS_SUCCESS
;
94 _nss_hesiod_getgrnam_r (const char *name
, struct group
*grp
,
95 char *buffer
, size_t buflen
, int *errnop
)
97 return lookup (name
, "group", grp
, buffer
, buflen
, errnop
);
101 _nss_hesiod_getgrgid_r (gid_t gid
, struct group
*grp
,
102 char *buffer
, size_t buflen
, int *errnop
)
104 char gidstr
[21]; /* We will probably never have a gid_t with more
107 snprintf (gidstr
, sizeof gidstr
, "%d", gid
);
109 return lookup (gidstr
, "gid", grp
, buffer
, buflen
, errnop
);
113 internal_gid_in_list (const gid_t
*list
, const gid_t g
, long int len
)
125 static enum nss_status
126 internal_gid_from_group (void *context
, const char *groupname
, gid_t
*group
)
129 enum nss_status status
= NSS_STATUS_NOTFOUND
;
131 grp_res
= hesiod_resolve (context
, groupname
, "group");
132 if (grp_res
!= NULL
&& *grp_res
!= NULL
)
136 while (*p
!= '\0' && *p
!= ':')
138 while (*p
!= '\0' && *p
== ':')
140 while (*p
!= '\0' && *p
!= ':')
142 while (*p
!= '\0' && *p
== ':')
151 while (*q
!= '\0' && *q
!= ':')
154 val
= strtol (p
, &endp
, 10);
155 if (sizeof (gid_t
) == sizeof (long int) || (gid_t
) val
== val
)
158 if (endp
== q
&& endp
!= p
)
159 status
= NSS_STATUS_SUCCESS
;
162 hesiod_free_list (context
, grp_res
);
168 _nss_hesiod_initgroups_dyn (const char *user
, gid_t group
, long int *start
,
169 long int *size
, gid_t
**groupsp
, long int limit
,
172 enum nss_status status
= NSS_STATUS_SUCCESS
;
176 gid_t
*groups
= *groupsp
;
179 context
= _nss_hesiod_init ();
181 return NSS_STATUS_UNAVAIL
;
183 list
= hesiod_resolve (context
, user
, "grplist");
187 hesiod_end (context
);
188 return errno
== ENOENT
? NSS_STATUS_NOTFOUND
: NSS_STATUS_UNAVAIL
;
191 if (!internal_gid_in_list (groups
, group
, *start
))
193 if (__builtin_expect (*start
== *size
, 0))
195 /* Need a bigger buffer. */
199 if (limit
> 0 && *size
== limit
)
200 /* We reached the maximum. */
206 newsize
= MIN (limit
, 2 * *size
);
208 newgroups
= realloc (groups
, newsize
* sizeof (*groups
));
209 if (newgroups
== NULL
)
211 *groupsp
= groups
= newgroups
;
215 groups
[(*start
)++] = group
;
227 status
= NSS_STATUS_NOTFOUND
;
230 while (*q
!= '\0' && *q
!= ':' && *q
!= ',')
237 val
= strtol (p
, &endp
, 10);
238 /* Test whether the number is representable in a variable of
239 type `gid_t'. If not ignore the number. */
240 if ((sizeof (gid_t
) == sizeof (long int) || (gid_t
) val
== val
)
243 if (*endp
== '\0' && endp
!= p
)
246 status
= NSS_STATUS_SUCCESS
;
249 status
= internal_gid_from_group (context
, p
, &group
);
251 if (status
== NSS_STATUS_SUCCESS
252 && !internal_gid_in_list (groups
, group
, *start
))
254 if (__builtin_expect (*start
== *size
, 0))
256 /* Need a bigger buffer. */
260 if (limit
> 0 && *size
== limit
)
261 /* We reached the maximum. */
267 newsize
= MIN (limit
, 2 * *size
);
269 newgroups
= realloc (groups
, newsize
* sizeof (*groups
));
270 if (newgroups
== NULL
)
272 *groupsp
= groups
= newgroups
;
276 groups
[(*start
)++] = group
;
283 __set_errno (save_errno
);
286 hesiod_free_list (context
, list
);
287 hesiod_end (context
);
289 return NSS_STATUS_SUCCESS
;