glibc/nss/nss_db/db-initgroups.c

141 lines
3.5 KiB
C
Raw Normal View History

2011-06-16 01:00:21 +00:00
/* Initgroups handling in nss_db module.
Copyright (C) 2011-2024 Free Software Foundation, Inc.
2011-06-16 01:00:21 +00:00
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
not, see <https://www.gnu.org/licenses/>. */
2011-06-16 01:00:21 +00:00
#include <ctype.h>
#include <errno.h>
#include <grp.h>
2012-05-10 22:13:54 +00:00
#include <limits.h>
2011-06-16 01:00:21 +00:00
#include <paths.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
2012-05-10 22:13:54 +00:00
#include <sys/param.h>
2011-06-16 01:00:21 +00:00
#include "nss_db.h"
/* The hashing function we use. */
#include "../intl/hash-string.h"
enum nss_status
_nss_db_initgroups_dyn (const char *user, gid_t group, long int *start,
long int *size, gid_t **groupsp, long int limit,
int *errnop)
{
struct nss_db_map state = { NULL, 0 };
enum nss_status status = internal_setent (_PATH_VARDB "group.db", &state);
if (status != NSS_STATUS_SUCCESS)
{
*errnop = errno;
return status;
}
const struct nss_db_header *header = state.header;
int i;
for (i = 0; i < header->ndbs; ++i)
if (header->dbs[i].id == ':')
break;
if (i == header->ndbs)
{
status = NSS_STATUS_UNAVAIL;
goto out;
}
const stridx_t *hashtable
= (const stridx_t *) ((const char *) header
+ header->dbs[i].hashoffset);
const char *valstrtab = (const char *) header + header->valstroffset;
size_t userlen = strlen (user);
uint32_t hashval = __hash_string (user);
size_t hidx = hashval % header->dbs[i].hashsize;
size_t hval2 = 1 + hashval % (header->dbs[i].hashsize - 2);
gid_t *groups = *groupsp;
status = NSS_STATUS_NOTFOUND;
while (hashtable[hidx] != ~((stridx_t) 0))
{
const char *valstr = valstrtab + hashtable[hidx];
while (isblank (*valstr))
++valstr;
if (strncmp (valstr, user, userlen) == 0 && isblank (valstr[userlen]))
{
valstr += userlen + 1;
while (isblank (*valstr))
++valstr;
while (*valstr != '\0')
{
errno = 0;
char *endp;
unsigned long int n = strtoul (valstr, &endp, 10);
if (*endp != ',' && *endp != '\0')
break;
valstr = *endp == '\0' ? endp : endp + 1;
if (n != ULONG_MAX || errno != ERANGE)
{
/* Insert the group. */
if (*start == *size)
{
/* Need a bigger buffer. */
if (limit > 0 && *size == limit)
{
/* We reached the maximum. */
status = NSS_STATUS_SUCCESS;
goto out;
}
long int newsize;
if (limit <= 0)
newsize = 2 * *size;
else
newsize = MIN (limit, 2 * *size);
gid_t *newgroups = realloc (groups,
newsize * sizeof (*groups));
if (newgroups == NULL)
{
*errnop = ENOMEM;
status = NSS_STATUS_TRYAGAIN;
goto out;
}
*groupsp = groups = newgroups;
*size = newsize;
}
groups[*start] = n;
*start += 1;
}
}
status = NSS_STATUS_SUCCESS;
break;
}
if ((hidx += hval2) >= header->dbs[i].hashsize)
hidx -= header->dbs[i].hashsize;
}
out:
internal_endent (&state);
return status;
}