1 /* Copyright (C) 1997-2019 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 <https://www.gnu.org/licenses/>. */
27 #include <sys/param.h>
29 /* Get the declaration of the parser function. */
31 #define STRUCTURE group
33 #include <nss/nss_files/files-parse.c>
36 _nss_hesiod_setgrent (int stayopen
)
38 return NSS_STATUS_SUCCESS
;
42 _nss_hesiod_endgrent (void)
44 return NSS_STATUS_SUCCESS
;
47 static enum nss_status
48 lookup (const char *name
, const char *type
, struct group
*grp
,
49 char *buffer
, size_t buflen
, int *errnop
)
51 struct parser_data
*data
= (void *) buffer
;
59 if (hesiod_init (&context
) < 0)
60 return NSS_STATUS_UNAVAIL
;
62 list
= hesiod_resolve (context
, name
, type
);
68 return err
== ENOENT
? NSS_STATUS_NOTFOUND
: NSS_STATUS_UNAVAIL
;
71 linebuflen
= buffer
+ buflen
- data
->linebuffer
;
72 len
= strlen (*list
) + 1;
75 hesiod_free_list (context
, list
);
78 return NSS_STATUS_TRYAGAIN
;
81 memcpy (data
->linebuffer
, *list
, len
);
82 hesiod_free_list (context
, list
);
85 parse_res
= _nss_files_parse_grent (buffer
, grp
, data
, buflen
, errnop
);
89 return parse_res
== -1 ? NSS_STATUS_TRYAGAIN
: NSS_STATUS_NOTFOUND
;
92 return NSS_STATUS_SUCCESS
;
96 _nss_hesiod_getgrnam_r (const char *name
, struct group
*grp
,
97 char *buffer
, size_t buflen
, int *errnop
)
99 return lookup (name
, "group", grp
, buffer
, buflen
, errnop
);
103 _nss_hesiod_getgrgid_r (gid_t gid
, struct group
*grp
,
104 char *buffer
, size_t buflen
, int *errnop
)
106 char gidstr
[21]; /* We will probably never have a gid_t with more
109 snprintf (gidstr
, sizeof gidstr
, "%d", gid
);
111 return lookup (gidstr
, "gid", grp
, buffer
, buflen
, errnop
);
115 internal_gid_in_list (const gid_t
*list
, const gid_t g
, long int len
)
127 static enum nss_status
128 internal_gid_from_group (void *context
, const char *groupname
, gid_t
*group
)
131 enum nss_status status
= NSS_STATUS_NOTFOUND
;
133 grp_res
= hesiod_resolve (context
, groupname
, "group");
134 if (grp_res
!= NULL
&& *grp_res
!= NULL
)
138 /* Skip to third field. */
139 while (*p
!= '\0' && *p
!= ':')
143 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 if (hesiod_init (&context
) < 0)
180 return NSS_STATUS_UNAVAIL
;
182 list
= hesiod_resolve (context
, user
, "grplist");
186 hesiod_end (context
);
187 return errno
== ENOENT
? NSS_STATUS_NOTFOUND
: NSS_STATUS_UNAVAIL
;
199 status
= NSS_STATUS_NOTFOUND
;
202 while (*q
!= '\0' && *q
!= ':' && *q
!= ',')
209 val
= strtol (p
, &endp
, 10);
210 /* Test whether the number is representable in a variable of
211 type `gid_t'. If not ignore the number. */
212 if ((sizeof (gid_t
) == sizeof (long int) || (gid_t
) val
== val
)
215 if (*endp
== '\0' && endp
!= p
)
218 status
= NSS_STATUS_SUCCESS
;
221 status
= internal_gid_from_group (context
, p
, &group
);
223 if (status
== NSS_STATUS_SUCCESS
224 && !internal_gid_in_list (groups
, group
, *start
))
226 if (__glibc_unlikely (*start
== *size
))
228 /* Need a bigger buffer. */
232 if (limit
> 0 && *size
== limit
)
233 /* We reached the maximum. */
239 newsize
= MIN (limit
, 2 * *size
);
241 newgroups
= realloc (groups
, newsize
* sizeof (*groups
));
242 if (newgroups
== NULL
)
244 *groupsp
= groups
= newgroups
;
248 groups
[(*start
)++] = group
;
255 __set_errno (save_errno
);
258 hesiod_free_list (context
, list
);
259 hesiod_end (context
);
261 return NSS_STATUS_SUCCESS
;