/* Cache handling for netgroup lookup.
Copyright (C) 2011-2025 Free Software Foundation, Inc.
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
along with this program; if not, see <https://www.gnu.org/licenses/>. */
#include <alloca.h>
#include <assert.h>
#include <errno.h>
#include <libintl.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <scratch_buffer.h>
#include "../nss/netgroup.h"
#include "nscd.h"
#include "dbg_log.h"
#include <kernel-features.h>
/* 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];
};
/* 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;
}
/* 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;
send_notfound (fd);
dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len, 1);
/* If we cannot permanently store the result, so be it. */
if (dataset != NULL)
{
timeout = datahead_init_neg (&dataset->head,
sizeof (struct dataset) + req->key_len,
total, db->negtimeout);
/* This is the reply. */
memcpy (&dataset->resp, ¬found, total);
/* Copy the key data. */
memcpy (dataset->strdata, key,