1 /* Copyright (C) 1997-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
26 #include <sys/param.h>
28 NSS_DECLARE_MODULE_FUNCTIONS (hesiod
)
30 /* Get the declaration of the parser function. */
32 #define STRUCTURE group
34 #include <nss/nss_files/files-parse.c>
37 _nss_hesiod_setgrent (int stayopen
)
39 return NSS_STATUS_SUCCESS
;
43 _nss_hesiod_endgrent (void)
45 return NSS_STATUS_SUCCESS
;
48 static enum nss_status
49 lookup (const char *name
, const char *type
, struct group
*grp
,
50 char *buffer
, size_t buflen
, int *errnop
)
52 struct parser_data
*data
= (void *) buffer
;
60 if (hesiod_init (&context
) < 0)
61 return NSS_STATUS_UNAVAIL
;
63 list
= hesiod_resolve (context
, name
, type
);
69 return err
== 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
);
90 return parse_res
== -1 ? NSS_STATUS_TRYAGAIN
: NSS_STATUS_NOTFOUND
;
93 return NSS_STATUS_SUCCESS
;
97 _nss_hesiod_getgrnam_r (const char *name
, struct group
*grp
,
98 char *buffer
, size_t buflen
, int *errnop
)
100 return lookup (name
, "group", grp
, buffer
, buflen
, errnop
);
104 _nss_hesiod_getgrgid_r (gid_t gid
, struct group
*grp
,
105 char *buffer
, size_t buflen
, int *errnop
)
107 char gidstr
[21]; /* We will probably never have a gid_t with more
110 snprintf (gidstr
, sizeof gidstr
, "%d", gid
);
112 return lookup (gidstr
, "gid", grp
, buffer
, buflen
, errnop
);
116 internal_gid_in_list (const gid_t
*list
, const gid_t g
, long int len
)
128 static enum nss_status
129 internal_gid_from_group (void *context
, const char *groupname
, gid_t
*group
)
132 enum nss_status status
= NSS_STATUS_NOTFOUND
;
134 grp_res
= hesiod_resolve (context
, groupname
, "group");
135 if (grp_res
!= NULL
&& *grp_res
!= NULL
)
139 /* Skip to third field. */
140 while (*p
!= '\0' && *p
!= ':')
144 while (*p
!= '\0' && *p
!= ':')
152 while (*q
!= '\0' && *q
!= ':')
155 val
= strtol (p
, &endp
, 10);
156 if (sizeof (gid_t
) == sizeof (long int) || (gid_t
) val
== val
)
159 if (endp
== q
&& endp
!= p
)
160 status
= NSS_STATUS_SUCCESS
;
163 hesiod_free_list (context
, grp_res
);
169 _nss_hesiod_initgroups_dyn (const char *user
, gid_t group
, long int *start
,
170 long int *size
, gid_t
**groupsp
, long int limit
,
173 enum nss_status status
= NSS_STATUS_SUCCESS
;
177 gid_t
*groups
= *groupsp
;
180 if (hesiod_init (&context
) < 0)
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
;
200 status
= NSS_STATUS_NOTFOUND
;
203 while (*q
!= '\0' && *q
!= ':' && *q
!= ',')
210 val
= strtol (p
, &endp
, 10);
211 /* Test whether the number is representable in a variable of
212 type `gid_t'. If not ignore the number. */
213 if ((sizeof (gid_t
) == sizeof (long int) || (gid_t
) val
== val
)
216 if (*endp
== '\0' && endp
!= p
)
219 status
= NSS_STATUS_SUCCESS
;
222 status
= internal_gid_from_group (context
, p
, &group
);
224 if (status
== NSS_STATUS_SUCCESS
225 && !internal_gid_in_list (groups
, group
, *start
))
227 if (__glibc_unlikely (*start
== *size
))
229 /* Need a bigger buffer. */
233 if (limit
> 0 && *size
== limit
)
234 /* We reached the maximum. */
240 newsize
= MIN (limit
, 2 * *size
);
242 newgroups
= realloc (groups
, newsize
* sizeof (*groups
));
243 if (newgroups
== NULL
)
245 *groupsp
= groups
= newgroups
;
249 groups
[(*start
)++] = group
;
256 __set_errno (save_errno
);
259 hesiod_free_list (context
, list
);
260 hesiod_end (context
);
262 return NSS_STATUS_SUCCESS
;