2011-10-07 14:06:31 +00:00
|
|
|
/* Cache handling for netgroup lookup.
|
2024-01-01 18:12:26 +00:00
|
|
|
Copyright (C) 2011-2024 Free Software Foundation, Inc.
|
2011-10-07 14:06:31 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published
|
|
|
|
by the Free Software Foundation; version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
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
|
|
|
along with this program; if not, see <https://www.gnu.org/licenses/>. */
|
2011-10-07 14:06:31 +00:00
|
|
|
|
|
|
|
#include <alloca.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <libintl.h>
|
|
|
|
#include <stdbool.h>
|
2014-10-22 20:17:20 +00:00
|
|
|
#include <stdlib.h>
|
2011-10-07 14:06:31 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
2024-04-25 13:01:07 +00:00
|
|
|
#include <scratch_buffer.h>
|
2011-10-07 14:06:31 +00:00
|
|
|
|
2023-10-02 12:55:22 +00:00
|
|
|
#include "../nss/netgroup.h"
|
2011-10-07 14:06:31 +00:00
|
|
|
#include "nscd.h"
|
|
|
|
#include "dbg_log.h"
|
2012-05-10 21:39:53 +00:00
|
|
|
|
|
|
|
#include <kernel-features.h>
|
2011-10-07 14:06:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* This is the standard reply in case the service is disabled. */
|
|
|
|
static const netgroup_response_header disabled =
|
|
|
|
{
|
|
|
|
.version = NSCD_VERSION,
|
|
|
|
.found = -1,
|
|
|
|
.nresults = 0,
|
|
|
|
.result_len = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This is the struct describing how to write this record. */
|
|
|
|
const struct iovec netgroup_iov_disabled =
|
|
|
|
{
|
|
|
|
.iov_base = (void *) &disabled,
|
|
|
|
.iov_len = sizeof (disabled)
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* This is the standard reply in case we haven't found the dataset. */
|
|
|
|
static const netgroup_response_header notfound =
|
|
|
|
{
|
|
|
|
.version = NSCD_VERSION,
|
|
|
|
.found = 0,
|
|
|
|
.nresults = 0,
|
|
|
|
.result_len = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct dataset
|
|
|
|
{
|
|
|
|
struct datahead head;
|
|
|
|
netgroup_response_header resp;
|
|
|
|
char strdata[0];
|
|
|
|
};
|
|
|
|
|
2024-04-25 13:01:07 +00:00
|
|
|
/* Send a notfound response to FD. Always returns -1 to indicate an
|
|
|
|
ephemeral error. */
|
|
|
|
static time_t
|
|
|
|
send_notfound (int fd)
|
|
|
|
{
|
|
|
|
if (fd != -1)
|
|
|
|
TEMP_FAILURE_RETRY (send (fd, ¬found, sizeof (notfound), MSG_NOSIGNAL));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-01-02 04:35:27 +00:00
|
|
|
/* Sends a notfound message and prepares a notfound dataset to write to the
|
|
|
|
cache. Returns true if there was enough memory to allocate the dataset and
|
|
|
|
returns the dataset in DATASETP, total bytes to write in TOTALP and the
|
|
|
|
timeout in TIMEOUTP. KEY_COPY is set to point to the copy of the key in the
|
|
|
|
dataset. */
|
|
|
|
static bool
|
|
|
|
do_notfound (struct database_dyn *db, int fd, request_header *req,
|
|
|
|
const char *key, struct dataset **datasetp, ssize_t *totalp,
|
|
|
|
time_t *timeoutp, char **key_copy)
|
|
|
|
{
|
|
|
|
struct dataset *dataset;
|
|
|
|
ssize_t total;
|
|
|
|
time_t timeout;
|
|
|
|
bool cacheable = false;
|
|
|
|
|
|
|
|
total = sizeof (notfound);
|
|
|
|
timeout = time (NULL) + db->negtimeout;
|
|
|
|
|
2024-04-25 13:01:07 +00:00
|
|
|
send_notfound (fd);
|
2014-01-02 04:35:27 +00:00
|
|
|
|
|
|
|
dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len, 1);
|
|
|
|
/* If we cannot permanently store the result, so be it. */
|
|
|
|
if (dataset != NULL)
|
|
|
|
{
|
2014-04-30 06:27:09 +00:00
|
|
|
timeout = datahead_init_neg (&dataset->head,
|
|
|
|
sizeof (struct dataset) + req->key_len,
|
|
|
|
total, db->negtimeout);
|
2014-01-02 04:35:27 +00:00
|
|
|
|
|
|
|
/* This is the reply. */
|
|
|
|
memcpy (&dataset->resp, ¬found, total);
|
|
|
|
|
|
|
|
/* Copy the key data. */
|
|
|
|
memcpy (dataset->strdata, key, req->key_len);
|
|
|
|
*key_copy = dataset->strdata;
|
|
|
|
|
|
|
|
cacheable = true;
|
|
|
|
}
|
|
|
|
*timeoutp = timeout;
|
|
|
|
*totalp = total;
|
|
|
|
*datasetp = dataset;
|
|
|
|
return cacheable;
|
|
|
|
}
|
2011-10-07 14:06:31 +00:00
|
|
|
|
2024-04-25 13:01:07 +00:00
|
|
|
struct addgetnetgrentX_scratch
|
|
|
|
{
|
|
|
|
/* This is the result that the caller should use. It can be NULL,
|
|
|
|
point into buffer, or it can be in the cache. */
|
|
|
|
struct dataset *dataset;
|
|
|
|
|
|
|
|
struct scratch_buffer buffer;
|
|
|
|
|
|
|
|
/* Used internally in addgetnetgrentX as a staging area. */
|
|
|
|
struct scratch_buffer tmp;
|
|
|
|
|
|
|
|
/* Number of bytes in buffer that are actually used. */
|
|
|
|
size_t buffer_used;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
addgetnetgrentX_scratch_init (struct addgetnetgrentX_scratch *scratch)
|
|
|
|
{
|
|
|
|
scratch->dataset = NULL;
|
|
|
|
scratch_buffer_init (&scratch->buffer);
|
|
|
|
scratch_buffer_init (&scratch->tmp);
|
|
|
|
|
|
|
|
/* Reserve space for the header. */
|
|
|
|
scratch->buffer_used = sizeof (struct dataset);
|
|
|
|
static_assert (sizeof (struct dataset) < sizeof (scratch->tmp.__space),
|
|
|
|
"initial buffer space");
|
|
|
|
memset (scratch->tmp.data, 0, sizeof (struct dataset));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
addgetnetgrentX_scratch_free (struct addgetnetgrentX_scratch *scratch)
|
|
|
|
{
|
|
|
|
scratch_buffer_free (&scratch->buffer);
|
|
|
|
scratch_buffer_free (&scratch->tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy LENGTH bytes from S into SCRATCH. Returns NULL if SCRATCH
|
|
|
|
could not be resized, otherwise a pointer to the copy. */
|
|
|
|
static char *
|
|
|
|
addgetnetgrentX_append_n (struct addgetnetgrentX_scratch *scratch,
|
|
|
|
const char *s, size_t length)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
size_t remaining = scratch->buffer.length - scratch->buffer_used;
|
|
|
|
if (remaining >= length)
|
|
|
|
break;
|
|
|
|
if (!scratch_buffer_grow_preserve (&scratch->buffer))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
char *copy = scratch->buffer.data + scratch->buffer_used;
|
|
|
|
memcpy (copy, s, length);
|
|
|
|
scratch->buffer_used += length;
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy S into SCRATCH, including its null terminator. Returns false
|
|
|
|
if SCRATCH could not be resized. */
|
|
|
|
static bool
|
|
|
|
addgetnetgrentX_append (struct addgetnetgrentX_scratch *scratch, const char *s)
|
|
|
|
{
|
|
|
|
if (s == NULL)
|
|
|
|
s = "";
|
|
|
|
return addgetnetgrentX_append_n (scratch, s, strlen (s) + 1) != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Caller must initialize and free *SCRATCH. If the return value is
|
|
|
|
negative, this function has sent a notfound response. */
|
2011-10-07 14:06:31 +00:00
|
|
|
static time_t
|
|
|
|
addgetnetgrentX (struct database_dyn *db, int fd, request_header *req,
|
|
|
|
const char *key, uid_t uid, struct hashentry *he,
|
2024-04-25 13:01:07 +00:00
|
|
|
struct datahead *dh, struct addgetnetgrentX_scratch *scratch)
|
2011-10-07 14:06:31 +00:00
|
|
|
{
|
2014-02-10 13:45:42 +00:00
|
|
|
if (__glibc_unlikely (debug_level > 0))
|
2011-10-07 14:06:31 +00:00
|
|
|
{
|
|
|
|
if (he == NULL)
|
|
|
|
dbg_log (_("Haven't found \"%s\" in netgroup cache!"), key);
|
|
|
|
else
|
|
|
|
dbg_log (_("Reloading \"%s\" in netgroup cache!"), key);
|
|
|
|
}
|
|
|
|
|
2020-11-10 03:09:34 +00:00
|
|
|
static nss_action_list netgroup_database;
|
2011-10-07 14:06:31 +00:00
|
|
|
time_t timeout;
|
|
|
|
struct dataset *dataset;
|
|
|
|
bool cacheable = false;
|
|
|
|
ssize_t total;
|
2014-01-02 04:35:27 +00:00
|
|
|
bool found = false;
|
2011-10-07 14:06:31 +00:00
|
|
|
|
|
|
|
char *key_copy = NULL;
|
|
|
|
struct __netgrent data;
|
|
|
|
size_t nentries = 0;
|
|
|
|
size_t group_len = strlen (key) + 1;
|
2014-10-22 20:17:20 +00:00
|
|
|
struct name_list *first_needed
|
|
|
|
= alloca (sizeof (struct name_list) + group_len);
|
2011-10-07 14:06:31 +00:00
|
|
|
|
|
|
|
if (netgroup_database == NULL
|
2021-02-16 02:34:23 +00:00
|
|
|
&& !__nss_database_get (nss_database_netgroup, &netgroup_database))
|
2011-10-07 14:06:31 +00:00
|
|
|
{
|
|
|
|
/* No such service. */
|
2014-01-02 04:35:27 +00:00
|
|
|
cacheable = do_notfound (db, fd, req, key, &dataset, &total, &timeout,
|
|
|
|
&key_copy);
|
2024-04-25 13:01:07 +00:00
|
|
|
goto maybe_cache_add;
|
2011-10-07 14:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memset (&data, '\0', sizeof (data));
|
2014-10-22 20:17:20 +00:00
|
|
|
first_needed->next = first_needed;
|
|
|
|
memcpy (first_needed->name, key, group_len);
|
|
|
|
data.needed_groups = first_needed;
|
2011-10-07 14:06:31 +00:00
|
|
|
|
|
|
|
while (data.needed_groups != NULL)
|
|
|
|
{
|
|
|
|
/* Add the next group to the list of those which are known. */
|
|
|
|
struct name_list *this_group = data.needed_groups->next;
|
|
|
|
if (this_group == data.needed_groups)
|
|
|
|
data.needed_groups = NULL;
|
|
|
|
else
|
|
|
|
data.needed_groups->next = this_group->next;
|
|
|
|
this_group->next = data.known_groups;
|
|
|
|
data.known_groups = this_group;
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
enum nss_status (*f) (const char *, struct __netgrent *);
|
|
|
|
void *ptr;
|
|
|
|
} setfct;
|
|
|
|
|
2020-11-10 03:09:34 +00:00
|
|
|
nss_action_list nip = netgroup_database;
|
2011-10-07 14:06:31 +00:00
|
|
|
int no_more = __nss_lookup (&nip, "setnetgrent", NULL, &setfct.ptr);
|
|
|
|
while (!no_more)
|
|
|
|
{
|
|
|
|
enum nss_status status
|
|
|
|
= DL_CALL_FCT (*setfct.f, (data.known_groups->name, &data));
|
|
|
|
|
|
|
|
if (status == NSS_STATUS_SUCCESS)
|
|
|
|
{
|
2014-01-02 04:35:27 +00:00
|
|
|
found = true;
|
2011-10-07 14:06:31 +00:00
|
|
|
union
|
|
|
|
{
|
|
|
|
enum nss_status (*f) (struct __netgrent *, char *, size_t,
|
|
|
|
int *);
|
|
|
|
void *ptr;
|
|
|
|
} getfct;
|
|
|
|
getfct.ptr = __nss_lookup_function (nip, "getnetgrent_r");
|
|
|
|
if (getfct.f != NULL)
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int e;
|
2024-04-25 13:01:07 +00:00
|
|
|
status = getfct.f (&data, scratch->tmp.data,
|
|
|
|
scratch->tmp.length, &e);
|
2011-10-07 14:06:31 +00:00
|
|
|
if (status == NSS_STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
if (data.type == triple_val)
|
|
|
|
{
|
|
|
|
const char *nhost = data.val.triple.host;
|
|
|
|
const char *nuser = data.val.triple.user;
|
|
|
|
const char *ndomain = data.val.triple.domain;
|
2024-04-25 13:01:07 +00:00
|
|
|
if (!(addgetnetgrentX_append (scratch, nhost)
|
|
|
|
&& addgetnetgrentX_append (scratch, nuser)
|
|
|
|
&& addgetnetgrentX_append (scratch, ndomain)))
|
|
|
|
return send_notfound (fd);
|
2011-10-07 14:06:31 +00:00
|
|
|
++nentries;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Check that the group has not been
|
|
|
|
requested before. */
|
|
|
|
struct name_list *runp = data.needed_groups;
|
|
|
|
if (runp != NULL)
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (strcmp (runp->name, data.val.group) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
runp = runp->next;
|
|
|
|
if (runp == data.needed_groups)
|
|
|
|
{
|
|
|
|
runp = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (runp == NULL)
|
|
|
|
{
|
|
|
|
runp = data.known_groups;
|
|
|
|
while (runp != NULL)
|
|
|
|
if (strcmp (runp->name, data.val.group) == 0)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
runp = runp->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (runp == NULL)
|
|
|
|
{
|
|
|
|
/* A new group is requested. */
|
|
|
|
size_t namelen = strlen (data.val.group) + 1;
|
|
|
|
struct name_list *newg = alloca (sizeof (*newg)
|
|
|
|
+ namelen);
|
|
|
|
memcpy (newg->name, data.val.group, namelen);
|
|
|
|
if (data.needed_groups == NULL)
|
|
|
|
data.needed_groups = newg->next = newg;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newg->next = data.needed_groups->next;
|
|
|
|
data.needed_groups->next = newg;
|
|
|
|
data.needed_groups = newg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-26 06:10:08 +00:00
|
|
|
else if (status == NSS_STATUS_TRYAGAIN && e == ERANGE)
|
2011-10-07 14:06:31 +00:00
|
|
|
{
|
2024-04-25 13:01:07 +00:00
|
|
|
if (!scratch_buffer_grow (&scratch->tmp))
|
|
|
|
return send_notfound (fd);
|
2011-10-07 14:06:31 +00:00
|
|
|
}
|
2014-05-26 06:10:08 +00:00
|
|
|
else if (status == NSS_STATUS_RETURN
|
|
|
|
|| status == NSS_STATUS_NOTFOUND
|
|
|
|
|| status == NSS_STATUS_UNAVAIL)
|
|
|
|
/* This was either the last one for this group or the
|
|
|
|
group was empty or the NSS module had an internal
|
|
|
|
failure. Look at next group if available. */
|
|
|
|
break;
|
2011-10-07 14:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum nss_status (*endfct) (struct __netgrent *);
|
|
|
|
endfct = __nss_lookup_function (nip, "endnetgrent");
|
|
|
|
if (endfct != NULL)
|
|
|
|
(void) DL_CALL_FCT (*endfct, (&data));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
no_more = __nss_next2 (&nip, "setnetgrent", NULL, &setfct.ptr,
|
|
|
|
status, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-02 04:35:27 +00:00
|
|
|
/* No results. Return a failure and write out a notfound record in the
|
|
|
|
cache. */
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
cacheable = do_notfound (db, fd, req, key, &dataset, &total, &timeout,
|
|
|
|
&key_copy);
|
2024-04-25 13:01:07 +00:00
|
|
|
goto maybe_cache_add;
|
2014-01-02 04:35:27 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 13:01:07 +00:00
|
|
|
/* Capture the result size without the key appended. */
|
|
|
|
total = scratch->buffer_used;
|
|
|
|
|
|
|
|
/* Make a copy of the key. The scratch buffer must not move after
|
|
|
|
this point. */
|
|
|
|
key_copy = addgetnetgrentX_append_n (scratch, key, req->key_len);
|
|
|
|
if (key_copy == NULL)
|
|
|
|
return send_notfound (fd);
|
2011-10-07 14:06:31 +00:00
|
|
|
|
|
|
|
/* Fill in the dataset. */
|
2024-04-25 13:01:07 +00:00
|
|
|
dataset = scratch->buffer.data;
|
2014-04-30 06:27:09 +00:00
|
|
|
timeout = datahead_init_pos (&dataset->head, total + req->key_len,
|
|
|
|
total - offsetof (struct dataset, resp),
|
|
|
|
he == NULL ? 0 : dh->nreloads + 1,
|
|
|
|
db->postimeout);
|
2011-10-07 14:06:31 +00:00
|
|
|
|
|
|
|
dataset->resp.version = NSCD_VERSION;
|
|
|
|
dataset->resp.found = 1;
|
|
|
|
dataset->resp.nresults = nentries;
|
2024-04-25 13:01:07 +00:00
|
|
|
dataset->resp.result_len = total - sizeof (*dataset);
|
2011-10-07 14:06:31 +00:00
|
|
|
|
|
|
|
/* Now we can determine whether on refill we have to create a new
|
|
|
|
record or not. */
|
|
|
|
if (he != NULL)
|
|
|
|
{
|
|
|
|
assert (fd == -1);
|
|
|
|
|
|
|
|
if (dataset->head.allocsize == dh->allocsize
|
|
|
|
&& dataset->head.recsize == dh->recsize
|
|
|
|
&& memcmp (&dataset->resp, dh->data,
|
|
|
|
dh->allocsize - offsetof (struct dataset, resp)) == 0)
|
|
|
|
{
|
|
|
|
/* The data has not changed. We will just bump the timeout
|
|
|
|
value. Note that the new record has been allocated on
|
|
|
|
the stack and need not be freed. */
|
|
|
|
dh->timeout = dataset->head.timeout;
|
|
|
|
dh->ttl = dataset->head.ttl;
|
|
|
|
++dh->nreloads;
|
|
|
|
dataset = (struct dataset *) dh;
|
|
|
|
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
struct dataset *newp
|
|
|
|
= (struct dataset *) mempool_alloc (db, total + req->key_len, 1);
|
2014-02-10 13:45:42 +00:00
|
|
|
if (__glibc_likely (newp != NULL))
|
2011-10-07 14:06:31 +00:00
|
|
|
{
|
|
|
|
/* Adjust pointer into the memory block. */
|
2024-04-25 13:01:07 +00:00
|
|
|
key_copy = (char *) newp + (key_copy - (char *) dataset);
|
2011-10-07 14:06:31 +00:00
|
|
|
|
|
|
|
dataset = memcpy (newp, dataset, total + req->key_len);
|
|
|
|
cacheable = true;
|
|
|
|
|
|
|
|
if (he != NULL)
|
|
|
|
/* Mark the old record as obsolete. */
|
|
|
|
dh->usable = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (he == NULL && fd != -1)
|
2024-04-25 13:01:07 +00:00
|
|
|
/* We write the dataset before inserting it to the database since
|
|
|
|
while inserting this thread might block and so would
|
|
|
|
unnecessarily let the receiver wait. */
|
2018-05-16 13:51:15 +00:00
|
|
|
writeall (fd, &dataset->resp, dataset->head.recsize);
|
2011-10-07 14:06:31 +00:00
|
|
|
|
2024-04-25 13:01:07 +00:00
|
|
|
maybe_cache_add:
|
2011-10-07 14:06:31 +00:00
|
|
|
if (cacheable)
|
|
|
|
{
|
|
|
|
/* If necessary, we also propagate the data to disk. */
|
|
|
|
if (db->persistent)
|
|
|
|
{
|
|
|
|
// XXX async OK?
|
|
|
|
uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
|
|
|
|
msync ((void *) pval,
|
|
|
|
((uintptr_t) dataset & pagesize_m1) + total + req->key_len,
|
|
|
|
MS_ASYNC);
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) cache_add (req->type, key_copy, req->key_len, &dataset->head,
|
|
|
|
true, db, uid, he == NULL);
|
|
|
|
|
|
|
|
pthread_rwlock_unlock (&db->lock);
|
|
|
|
|
|
|
|
/* Mark the old entry as obsolete. */
|
|
|
|
if (dh != NULL)
|
|
|
|
dh->usable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2024-04-25 13:01:07 +00:00
|
|
|
scratch->dataset = dataset;
|
2011-10-07 14:06:31 +00:00
|
|
|
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static time_t
|
|
|
|
addinnetgrX (struct database_dyn *db, int fd, request_header *req,
|
|
|
|
char *key, uid_t uid, struct hashentry *he,
|
|
|
|
struct datahead *dh)
|
|
|
|
{
|
|
|
|
const char *group = key;
|
2023-02-03 12:01:33 +00:00
|
|
|
key = strchr (key, '\0') + 1;
|
[BZ #22342] Fix netgroup cache keys.
Unlike other nscd caches, the netgroup cache contains two types of
records - those for "iterate through a netgroup" (i.e. setnetgrent())
and those for "is this user in this netgroup" (i.e. innetgr()),
i.e. full and partial records. The timeout code assumes these records
have the same key for the group name, so that the collection of records
that is "this netgroup" can be expired as a unit.
However, the keys are not the same, as the in-netgroup key is generated
by nscd rather than being passed to it from elsewhere, and is generated
without the trailing NUL. All other keys have the trailing NUL, and as
noted in the linked BZ, debug statements confirm that two keys for the
same netgroup are added to the cache with two different lengths.
The result of this is that as records in the cache expire, the purge
code only cleans out one of the two types of entries, resulting in
stale, possibly incorrect, and possibly inconsistent cache data.
The patch simply includes the existing NUL in the computation for the
key length ('key' points to the char after the NUL, and 'group' to the
first char of the group, so 'key-group' includes the first char to the
NUL, inclusive).
[BZ #22342]
* nscd/netgroupcache.c (addinnetgrX): Include trailing NUL in
key value.
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
2018-03-02 04:20:45 +00:00
|
|
|
size_t group_len = key - group;
|
2011-10-07 14:06:31 +00:00
|
|
|
const char *host = *key++ ? key : NULL;
|
|
|
|
if (host != NULL)
|
2023-02-03 12:01:33 +00:00
|
|
|
key = strchr (key, '\0') + 1;
|
2011-10-07 14:06:31 +00:00
|
|
|
const char *user = *key++ ? key : NULL;
|
|
|
|
if (user != NULL)
|
2023-02-03 12:01:33 +00:00
|
|
|
key = strchr (key, '\0') + 1;
|
2011-10-07 14:06:31 +00:00
|
|
|
const char *domain = *key++ ? key : NULL;
|
2024-04-25 13:01:07 +00:00
|
|
|
struct addgetnetgrentX_scratch scratch;
|
|
|
|
|
|
|
|
addgetnetgrentX_scratch_init (&scratch);
|
2011-10-07 14:06:31 +00:00
|
|
|
|
2014-02-10 13:45:42 +00:00
|
|
|
if (__glibc_unlikely (debug_level > 0))
|
2011-10-07 14:06:31 +00:00
|
|
|
{
|
|
|
|
if (he == NULL)
|
|
|
|
dbg_log (_("Haven't found \"%s (%s,%s,%s)\" in netgroup cache!"),
|
|
|
|
group, host ?: "", user ?: "", domain ?: "");
|
|
|
|
else
|
|
|
|
dbg_log (_("Reloading \"%s (%s,%s,%s)\" in netgroup cache!"),
|
|
|
|
group, host ?: "", user ?: "", domain ?: "");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dataset *result = (struct dataset *) cache_search (GETNETGRENT,
|
|
|
|
group, group_len,
|
|
|
|
db, uid);
|
|
|
|
time_t timeout;
|
|
|
|
if (result != NULL)
|
2024-04-25 13:01:07 +00:00
|
|
|
timeout = result->head.timeout;
|
2011-10-07 14:06:31 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
request_header req_get =
|
|
|
|
{
|
|
|
|
.type = GETNETGRENT,
|
|
|
|
.key_len = group_len
|
|
|
|
};
|
|
|
|
timeout = addgetnetgrentX (db, -1, &req_get, group, uid, NULL, NULL,
|
2024-04-25 13:01:07 +00:00
|
|
|
&scratch);
|
|
|
|
result = scratch.dataset;
|
|
|
|
if (timeout < 0)
|
|
|
|
goto out;
|
2011-10-07 14:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct indataset
|
|
|
|
{
|
|
|
|
struct datahead head;
|
|
|
|
innetgroup_response_header resp;
|
|
|
|
} *dataset
|
|
|
|
= (struct indataset *) mempool_alloc (db,
|
|
|
|
sizeof (*dataset) + req->key_len,
|
|
|
|
1);
|
|
|
|
bool cacheable = true;
|
2014-02-10 13:45:42 +00:00
|
|
|
if (__glibc_unlikely (dataset == NULL))
|
2011-10-07 14:06:31 +00:00
|
|
|
{
|
|
|
|
cacheable = false;
|
2024-04-30 10:31:37 +00:00
|
|
|
/* The alloca is safe because nscd_run_worker verifies that
|
2024-04-25 13:00:45 +00:00
|
|
|
key_len is not larger than MAXKEYLEN. */
|
|
|
|
dataset = alloca (sizeof (*dataset) + req->key_len);
|
2011-10-07 14:06:31 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 06:27:09 +00:00
|
|
|
datahead_init_pos (&dataset->head, sizeof (*dataset) + req->key_len,
|
|
|
|
sizeof (innetgroup_response_header),
|
2024-04-25 13:01:07 +00:00
|
|
|
he == NULL ? 0 : dh->nreloads + 1,
|
|
|
|
result == NULL ? db->negtimeout : result->head.ttl);
|
2014-04-30 06:27:09 +00:00
|
|
|
/* Set the notfound status and timeout based on the result from
|
|
|
|
getnetgrent. */
|
2024-04-25 13:01:07 +00:00
|
|
|
dataset->head.notfound = result == NULL || result->head.notfound;
|
2011-10-07 14:06:31 +00:00
|
|
|
dataset->head.timeout = timeout;
|
|
|
|
|
|
|
|
dataset->resp.version = NSCD_VERSION;
|
2024-04-25 13:01:07 +00:00
|
|
|
dataset->resp.found = result != NULL && result->resp.found;
|
2011-10-07 14:06:31 +00:00
|
|
|
/* Until we find a matching entry the result is 0. */
|
|
|
|
dataset->resp.result = 0;
|
|
|
|
|
|
|
|
char *key_copy = memcpy ((char *) (dataset + 1), group, req->key_len);
|
|
|
|
|
|
|
|
if (dataset->resp.found)
|
|
|
|
{
|
|
|
|
const char *triplets = (const char *) (&result->resp + 1);
|
|
|
|
|
|
|
|
for (nscd_ssize_t i = result->resp.nresults; i > 0; --i)
|
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
|
Fix nscd lookup for innetgr when netgroup has wildcards (BZ #16758)
nscd works correctly when the request in innetgr is a wildcard,
i.e. when one or more of host, user or domain parameters is NULL.
However, it does not work when the the triplet in the netgroup
definition has a wildcard. This is easy to reproduce for a triplet
defined as follows:
foonet (,foo,)
Here, an innetgr call that looks like this:
innetgr ("foonet", "foohost", "foo", NULL);
should succeed and so should:
innetgr ("foonet", NULL, "foo", "foodomain");
It does succeed with nscd disabled, but not with nscd enabled. This
fix adds this additional check for all three parts of the triplet so
that it gives the correct result.
[BZ #16758]
* nscd/netgroupcache.c (addinnetgrX): Succeed if triplet has
blank values.
2014-03-27 01:45:22 +00:00
|
|
|
/* For the host, user and domain in each triplet, we assume success
|
|
|
|
if the value is blank because that is how the wildcard entry to
|
|
|
|
match anything is stored in the netgroup cache. */
|
|
|
|
if (host != NULL && *triplets != '\0')
|
2011-10-07 14:06:31 +00:00
|
|
|
success = strcmp (host, triplets) == 0;
|
2023-02-03 12:01:33 +00:00
|
|
|
triplets = strchr (triplets, '\0') + 1;
|
2011-10-07 14:06:31 +00:00
|
|
|
|
Fix nscd lookup for innetgr when netgroup has wildcards (BZ #16758)
nscd works correctly when the request in innetgr is a wildcard,
i.e. when one or more of host, user or domain parameters is NULL.
However, it does not work when the the triplet in the netgroup
definition has a wildcard. This is easy to reproduce for a triplet
defined as follows:
foonet (,foo,)
Here, an innetgr call that looks like this:
innetgr ("foonet", "foohost", "foo", NULL);
should succeed and so should:
innetgr ("foonet", NULL, "foo", "foodomain");
It does succeed with nscd disabled, but not with nscd enabled. This
fix adds this additional check for all three parts of the triplet so
that it gives the correct result.
[BZ #16758]
* nscd/netgroupcache.c (addinnetgrX): Succeed if triplet has
blank values.
2014-03-27 01:45:22 +00:00
|
|
|
if (success && user != NULL && *triplets != '\0')
|
2011-10-07 14:06:31 +00:00
|
|
|
success = strcmp (user, triplets) == 0;
|
2023-02-03 12:01:33 +00:00
|
|
|
triplets = strchr (triplets, '\0') + 1;
|
2011-10-07 14:06:31 +00:00
|
|
|
|
Fix nscd lookup for innetgr when netgroup has wildcards (BZ #16758)
nscd works correctly when the request in innetgr is a wildcard,
i.e. when one or more of host, user or domain parameters is NULL.
However, it does not work when the the triplet in the netgroup
definition has a wildcard. This is easy to reproduce for a triplet
defined as follows:
foonet (,foo,)
Here, an innetgr call that looks like this:
innetgr ("foonet", "foohost", "foo", NULL);
should succeed and so should:
innetgr ("foonet", NULL, "foo", "foodomain");
It does succeed with nscd disabled, but not with nscd enabled. This
fix adds this additional check for all three parts of the triplet so
that it gives the correct result.
[BZ #16758]
* nscd/netgroupcache.c (addinnetgrX): Succeed if triplet has
blank values.
2014-03-27 01:45:22 +00:00
|
|
|
if (success && (domain == NULL || *triplets == '\0'
|
|
|
|
|| strcmp (domain, triplets) == 0))
|
2011-10-07 14:06:31 +00:00
|
|
|
{
|
|
|
|
dataset->resp.result = 1;
|
|
|
|
break;
|
|
|
|
}
|
2023-02-03 12:01:33 +00:00
|
|
|
triplets = strchr (triplets, '\0') + 1;
|
2011-10-07 14:06:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (he != NULL && dh->data[0].innetgroupdata.result == dataset->resp.result)
|
|
|
|
{
|
|
|
|
/* The data has not changed. We will just bump the timeout
|
|
|
|
value. Note that the new record has been allocated on
|
|
|
|
the stack and need not be freed. */
|
|
|
|
dh->timeout = timeout;
|
|
|
|
dh->ttl = dataset->head.ttl;
|
|
|
|
++dh->nreloads;
|
2017-09-25 23:43:09 +00:00
|
|
|
if (cacheable)
|
|
|
|
pthread_rwlock_unlock (&db->lock);
|
2018-08-28 11:19:27 +00:00
|
|
|
goto out;
|
2011-10-07 14:06:31 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 13:01:07 +00:00
|
|
|
/* addgetnetgrentX may have already sent a notfound response. Do
|
|
|
|
not send another one. */
|
|
|
|
if (he == NULL && dataset->resp.found)
|
2011-10-07 14:06:31 +00:00
|
|
|
{
|
|
|
|
/* We write the dataset before inserting it to the database
|
|
|
|
since while inserting this thread might block and so would
|
|
|
|
unnecessarily let the receiver wait. */
|
2018-05-16 13:51:15 +00:00
|
|
|
assert (fd != -1);
|
2011-10-07 14:06:31 +00:00
|
|
|
|
2018-05-16 13:51:15 +00:00
|
|
|
writeall (fd, &dataset->resp, sizeof (innetgroup_response_header));
|
2011-10-07 14:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cacheable)
|
|
|
|
{
|
|
|
|
/* If necessary, we also propagate the data to disk. */
|
|
|
|
if (db->persistent)
|
|
|
|
{
|
|
|
|
// XXX async OK?
|
|
|
|
uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1;
|
|
|
|
msync ((void *) pval,
|
|
|
|
((uintptr_t) dataset & pagesize_m1) + sizeof (*dataset)
|
|
|
|
+ req->key_len,
|
|
|
|
MS_ASYNC);
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) cache_add (req->type, key_copy, req->key_len, &dataset->head,
|
|
|
|
true, db, uid, he == NULL);
|
|
|
|
|
|
|
|
pthread_rwlock_unlock (&db->lock);
|
|
|
|
|
|
|
|
/* Mark the old entry as obsolete. */
|
|
|
|
if (dh != NULL)
|
|
|
|
dh->usable = false;
|
|
|
|
}
|
|
|
|
|
2018-08-28 11:19:27 +00:00
|
|
|
out:
|
2024-04-25 13:01:07 +00:00
|
|
|
addgetnetgrentX_scratch_free (&scratch);
|
2011-10-07 14:06:31 +00:00
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-28 11:19:27 +00:00
|
|
|
static time_t
|
|
|
|
addgetnetgrentX_ignore (struct database_dyn *db, int fd, request_header *req,
|
|
|
|
const char *key, uid_t uid, struct hashentry *he,
|
|
|
|
struct datahead *dh)
|
|
|
|
{
|
2024-04-25 13:01:07 +00:00
|
|
|
struct addgetnetgrentX_scratch scratch;
|
|
|
|
addgetnetgrentX_scratch_init (&scratch);
|
|
|
|
time_t timeout = addgetnetgrentX (db, fd, req, key, uid, he, dh, &scratch);
|
|
|
|
addgetnetgrentX_scratch_free (&scratch);
|
|
|
|
if (timeout < 0)
|
|
|
|
timeout = 0;
|
2018-08-28 11:19:27 +00:00
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
2011-10-07 14:06:31 +00:00
|
|
|
void
|
|
|
|
addgetnetgrent (struct database_dyn *db, int fd, request_header *req,
|
|
|
|
void *key, uid_t uid)
|
|
|
|
{
|
2018-08-28 11:19:27 +00:00
|
|
|
addgetnetgrentX_ignore (db, fd, req, key, uid, NULL, NULL);
|
2011-10-07 14:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
time_t
|
|
|
|
readdgetnetgrent (struct database_dyn *db, struct hashentry *he,
|
|
|
|
struct datahead *dh)
|
|
|
|
{
|
|
|
|
request_header req =
|
|
|
|
{
|
|
|
|
.type = GETNETGRENT,
|
|
|
|
.key_len = he->len
|
|
|
|
};
|
2018-08-28 11:19:27 +00:00
|
|
|
return addgetnetgrentX_ignore
|
|
|
|
(db, -1, &req, db->data + he->key, he->owner, he, dh);
|
2011-10-07 14:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
addinnetgr (struct database_dyn *db, int fd, request_header *req,
|
|
|
|
void *key, uid_t uid)
|
|
|
|
{
|
|
|
|
addinnetgrX (db, fd, req, key, uid, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
time_t
|
|
|
|
readdinnetgr (struct database_dyn *db, struct hashentry *he,
|
|
|
|
struct datahead *dh)
|
|
|
|
{
|
|
|
|
request_header req =
|
|
|
|
{
|
|
|
|
.type = INNETGR,
|
|
|
|
.key_len = he->len
|
|
|
|
};
|
|
|
|
|
2024-05-02 15:06:19 +00:00
|
|
|
time_t timeout = addinnetgrX (db, -1, &req, db->data + he->key, he->owner,
|
|
|
|
he, dh);
|
2024-04-25 13:01:07 +00:00
|
|
|
if (timeout < 0)
|
|
|
|
timeout = 0;
|
|
|
|
return timeout;
|
2011-10-07 14:06:31 +00:00
|
|
|
}
|