|
Packit |
6c4009 |
/* Initgroups handling in nss_files module.
|
|
Packit |
6c4009 |
Copyright (C) 2011-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <grp.h>
|
|
Packit |
6c4009 |
#include <nss.h>
|
|
Packit |
6c4009 |
#include <stdio_ext.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <sys/param.h>
|
|
Packit |
6c4009 |
#include <stdbool.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <scratch_buffer.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
enum nss_status
|
|
Packit |
6c4009 |
_nss_files_initgroups_dyn (const char *user, gid_t group, long int *start,
|
|
Packit |
6c4009 |
long int *size, gid_t **groupsp, long int limit,
|
|
Packit |
6c4009 |
int *errnop)
|
|
Packit |
6c4009 |
{
|
|
Packit Bot |
0c2104 |
FILE *stream = fopen ("/etc/group", "rce");
|
|
Packit |
6c4009 |
if (stream == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
*errnop = errno;
|
|
Packit |
6c4009 |
return *errnop == ENOMEM ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit Bot |
0c2104 |
/* No other thread using this stream. */
|
|
Packit Bot |
0c2104 |
__fsetlocking (stream, FSETLOCKING_BYCALLER);
|
|
Packit Bot |
0c2104 |
|
|
Packit |
6c4009 |
char *line = NULL;
|
|
Packit |
6c4009 |
size_t linelen = 0;
|
|
Packit |
6c4009 |
enum nss_status status = NSS_STATUS_SUCCESS;
|
|
Packit |
6c4009 |
bool any = false;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct scratch_buffer tmpbuf;
|
|
Packit |
6c4009 |
scratch_buffer_init (&tmpbuf);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
gid_t *groups = *groupsp;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* We have to iterate over the entire file. */
|
|
Packit |
6c4009 |
while (1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
fpos_t pos;
|
|
Packit |
6c4009 |
fgetpos (stream, &pos;;
|
|
Packit |
6c4009 |
ssize_t n = getline (&line, &linelen, stream);
|
|
Packit |
6c4009 |
if (n < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (! feof_unlocked (stream))
|
|
Packit |
6c4009 |
status = ((*errnop = errno) == ENOMEM
|
|
Packit |
6c4009 |
? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL);
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct group grp;
|
|
Packit |
6c4009 |
int res = _nss_files_parse_grent (line, &grp,
|
|
Packit |
6c4009 |
tmpbuf.data, tmpbuf.length, errnop);
|
|
Packit |
6c4009 |
if (res == -1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (!scratch_buffer_grow (&tmpbuf))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
*errnop = ENOMEM;
|
|
Packit |
6c4009 |
status = NSS_STATUS_TRYAGAIN;
|
|
Packit |
6c4009 |
goto out;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
/* Reread current line, the parser has clobbered it. */
|
|
Packit |
6c4009 |
fsetpos (stream, &pos;;
|
|
Packit |
6c4009 |
continue;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (res > 0 && grp.gr_gid != group)
|
|
Packit |
6c4009 |
for (char **m = grp.gr_mem; *m != NULL; ++m)
|
|
Packit |
6c4009 |
if (strcmp (*m, user) == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Matches user. Insert this group. */
|
|
Packit |
6c4009 |
if (*start == *size)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Need a bigger buffer. */
|
|
Packit |
6c4009 |
if (limit > 0 && *size == limit)
|
|
Packit |
6c4009 |
/* We reached the maximum. */
|
|
Packit |
6c4009 |
goto out;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
long int newsize;
|
|
Packit |
6c4009 |
if (limit <= 0)
|
|
Packit |
6c4009 |
newsize = 2 * *size;
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
newsize = MIN (limit, 2 * *size);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
gid_t *newgroups = realloc (groups,
|
|
Packit |
6c4009 |
newsize * sizeof (*groups));
|
|
Packit |
6c4009 |
if (newgroups == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
*errnop = ENOMEM;
|
|
Packit |
6c4009 |
status = NSS_STATUS_TRYAGAIN;
|
|
Packit |
6c4009 |
goto out;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
*groupsp = groups = newgroups;
|
|
Packit |
6c4009 |
*size = newsize;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
groups[*start] = grp.gr_gid;
|
|
Packit |
6c4009 |
*start += 1;
|
|
Packit |
6c4009 |
any = true;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
out:
|
|
Packit |
6c4009 |
/* Free memory. */
|
|
Packit |
6c4009 |
scratch_buffer_free (&tmpbuf);
|
|
Packit |
6c4009 |
free (line);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
fclose (stream);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return status == NSS_STATUS_SUCCESS && !any ? NSS_STATUS_NOTFOUND : status;
|
|
Packit |
6c4009 |
}
|