glibc/nss/nss_files/files-initgroups.c
Stefan Liebler 0e1f068108 Fix linknamespace errors and local-plt-usages in nss_files.
After commit f9c8b11ed7
"nss: Access nss_files through direct references",
when building with -Os, multiple conform/.../linknamespace tests
and elf/check-localplt are failing:
Extra PLT reference: libc.so: fgetc_unlocked
Extra PLT reference: libc.so: getline

Or e.g.:
[initial] glob -> [libc.a(glob.o)] __getpwnam_r -> [libc.a(getpwnam_r.o)] __nss_database_custom -> [libc.a(nsswitch.o)] __nss_module_get_function -> [libc.a(nss_module.o)] __nss_files_functions -> [libc.a(nss_files_functions.o)] _nss_files_endaliasent -> [libc.a(files-alias.o)] feof_unlocked
[initial] glob -> [libc.a(glob.o)] __getpwnam_r -> [libc.a(getpwnam_r.o)] __nss_database_custom -> [libc.a(nsswitch.o)] __nss_module_get_function -> [libc.a(nss_module.o)] __nss_files_functions -> [libc.a(nss_files_functions.o)] _nss_files_endaliasent -> [libc.a(files-alias.o)] fgetc_unlocked
[initial] glob -> [libc.a(glob.o)] __getpwnam_r -> [libc.a(getpwnam_r.o)] __nss_database_custom -> [libc.a(nsswitch.o)] __nss_module_get_function -> [libc.a(nss_module.o)] __nss_files_functions -> [libc.a(nss_files_functions.o)] _nss_files_endnetgrent -> [libc.a(files-netgrp.o)] getline

This patch is using the hidden symbols where possible.
Instead of fputc_unlocked, __putc_unlocked is used.
(Compare to commit eeaa19f75e
"mntent: Use __putc_unlocked instead of fputc_unlocked")
2021-07-14 16:59:12 +02:00

131 lines
3.3 KiB
C

/* Initgroups handling in nss_files module.
Copyright (C) 2011-2021 Free Software Foundation, Inc.
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
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <errno.h>
#include <grp.h>
#include <nss.h>
#include <stdio_ext.h>
#include <string.h>
#include <sys/param.h>
#include <stdbool.h>
#include <stdlib.h>
#include <scratch_buffer.h>
#include <nss.h>
#include <nss_files.h>
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)
{
FILE *stream = __nss_files_fopen ("/etc/group");
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;
bool any = false;
struct scratch_buffer tmpbuf;
scratch_buffer_init (&tmpbuf);
gid_t *groups = *groupsp;
/* We have to iterate over the entire file. */
while (1)
{
fpos_t pos;
fgetpos (stream, &pos);
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;
int res = _nss_files_parse_grent (line, &grp,
tmpbuf.data, tmpbuf.length, errnop);
if (res == -1)
{
if (!scratch_buffer_grow (&tmpbuf))
{
*errnop = ENOMEM;
status = NSS_STATUS_TRYAGAIN;
goto out;
}
/* Reread current line, the parser has clobbered it. */
fsetpos (stream, &pos);
continue;
}
if (res > 0 && grp.gr_gid != group)
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;
any = true;
break;
}
}
out:
/* Free memory. */
scratch_buffer_free (&tmpbuf);
free (line);
fclose (stream);
return status == NSS_STATUS_SUCCESS && !any ? NSS_STATUS_NOTFOUND : status;
}
libc_hidden_def (_nss_files_initgroups_dyn)