2011-04-19 17:43:03 +00:00
|
|
|
/* Initgroups handling in nss_files module.
|
2021-01-02 19:32:25 +00:00
|
|
|
Copyright (C) 2011-2021 Free Software Foundation, Inc.
|
2011-04-19 17:43:03 +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 Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 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
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
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
|
|
|
<https://www.gnu.org/licenses/>. */
|
2011-04-19 17:43:03 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <nss.h>
|
|
|
|
#include <stdio_ext.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/param.h>
|
2012-07-30 23:09:11 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
2018-06-25 17:29:11 +00:00
|
|
|
#include <scratch_buffer.h>
|
2020-02-12 10:47:40 +00:00
|
|
|
#include <nss.h>
|
2020-07-16 14:12:46 +00:00
|
|
|
#include <nss_files.h>
|
2020-02-12 10:47:40 +00:00
|
|
|
|
|
|
|
NSS_DECLARE_MODULE_FUNCTIONS (files)
|
2011-04-19 17:43:03 +00:00
|
|
|
|
|
|
|
enum nss_status
|
|
|
|
_nss_files_initgroups_dyn (const char *user, gid_t group, long int *start,
|
|
|
|
long int *size, gid_t **groupsp, long int limit,
|
|
|
|
int *errnop)
|
|
|
|
{
|
2020-07-16 14:12:46 +00:00
|
|
|
FILE *stream = __nss_files_fopen ("/etc/group");
|
2011-04-19 17:43:03 +00:00
|
|
|
if (stream == NULL)
|
|
|
|
{
|
|
|
|
*errnop = errno;
|
|
|
|
return *errnop == ENOMEM ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *line = NULL;
|
|
|
|
size_t linelen = 0;
|
|
|
|
enum nss_status status = NSS_STATUS_SUCCESS;
|
2011-05-06 16:54:12 +00:00
|
|
|
bool any = false;
|
2011-04-19 17:43:03 +00:00
|
|
|
|
2018-06-25 17:29:11 +00:00
|
|
|
struct scratch_buffer tmpbuf;
|
|
|
|
scratch_buffer_init (&tmpbuf);
|
2011-04-19 17:43:03 +00:00
|
|
|
|
|
|
|
gid_t *groups = *groupsp;
|
|
|
|
|
|
|
|
/* We have to iterate over the entire file. */
|
2011-09-26 15:49:14 +00:00
|
|
|
while (1)
|
2011-04-19 17:43:03 +00:00
|
|
|
{
|
2011-09-26 15:49:14 +00:00
|
|
|
fpos_t pos;
|
|
|
|
fgetpos (stream, &pos);
|
2011-04-19 17:43:03 +00:00
|
|
|
ssize_t n = getline (&line, &linelen, stream);
|
|
|
|
if (n < 0)
|
|
|
|
{
|
|
|
|
if (! feof_unlocked (stream))
|
|
|
|
status = ((*errnop = errno) == ENOMEM
|
|
|
|
? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct group grp;
|
2018-06-25 17:29:11 +00:00
|
|
|
int res = _nss_files_parse_grent (line, &grp,
|
|
|
|
tmpbuf.data, tmpbuf.length, errnop);
|
2011-09-26 15:49:14 +00:00
|
|
|
if (res == -1)
|
2011-04-19 17:43:03 +00:00
|
|
|
{
|
2018-06-25 17:29:11 +00:00
|
|
|
if (!scratch_buffer_grow (&tmpbuf))
|
2011-04-19 17:43:03 +00:00
|
|
|
{
|
2018-06-25 17:29:11 +00:00
|
|
|
*errnop = ENOMEM;
|
|
|
|
status = NSS_STATUS_TRYAGAIN;
|
|
|
|
goto out;
|
2011-04-19 17:43:03 +00:00
|
|
|
}
|
2011-09-26 15:49:14 +00:00
|
|
|
/* Reread current line, the parser has clobbered it. */
|
|
|
|
fsetpos (stream, &pos);
|
|
|
|
continue;
|
2011-04-19 17:43:03 +00:00
|
|
|
}
|
|
|
|
|
2011-04-19 21:16:11 +00:00
|
|
|
if (res > 0 && grp.gr_gid != group)
|
2011-04-19 17:43:03 +00:00
|
|
|
for (char **m = grp.gr_mem; *m != NULL; ++m)
|
|
|
|
if (strcmp (*m, user) == 0)
|
|
|
|
{
|
|
|
|
/* Matches user. Insert this group. */
|
|
|
|
if (*start == *size)
|
|
|
|
{
|
|
|
|
/* Need a bigger buffer. */
|
|
|
|
if (limit > 0 && *size == limit)
|
|
|
|
/* We reached the maximum. */
|
|
|
|
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] = grp.gr_gid;
|
|
|
|
*start += 1;
|
2011-05-06 16:54:12 +00:00
|
|
|
any = true;
|
2011-04-19 17:43:03 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
/* Free memory. */
|
2018-06-25 17:29:11 +00:00
|
|
|
scratch_buffer_free (&tmpbuf);
|
2011-04-19 17:43:03 +00:00
|
|
|
free (line);
|
|
|
|
|
|
|
|
fclose (stream);
|
|
|
|
|
2011-05-06 16:54:12 +00:00
|
|
|
return status == NSS_STATUS_SUCCESS && !any ? NSS_STATUS_NOTFOUND : status;
|
2011-04-19 17:43:03 +00:00
|
|
|
}
|