Skip to content

Commit

Permalink
nsswitch: use new internal API (core)
Browse files Browse the repository at this point in the history
Core changes to switch the NSS internals to use the new API.

Reviewed-by: Siddhesh Poyarekar <[email protected]>
  • Loading branch information
djdelorierh committed Dec 4, 2020
1 parent fa78fec commit f8847d8
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 845 deletions.
4 changes: 3 additions & 1 deletion malloc/set-freeres.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <set-hooks.h>
#include <libc-internal.h>

#include "../nss/nss_module.h"
#include "../nss/nsswitch.h"
#include "../libio/libioP.h"

DEFINE_HOOK (__libc_subfreeres, (void));
Expand All @@ -43,6 +43,8 @@ __libc_freeres (void)
void *const *p;

call_function_static_weak (__nss_module_freeres);
call_function_static_weak (__nss_action_freeres);
call_function_static_weak (__nss_database_freeres);

_IO_cleanup ();

Expand Down
3 changes: 2 additions & 1 deletion nss/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ tests-container = \
tst-nss-test3 \
tst-nss-files-hosts-long \
tst-nss-db-endpwent \
tst-nss-db-endgrent
tst-nss-db-endgrent \
tst-reload1

# Tests which need libdl
ifeq (yes,$(build-shared))
Expand Down
2 changes: 1 addition & 1 deletion nss/nss_action.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */

#include <nss_action.h>
#include <nsswitch.h>

#include <string.h>
#include <libc-lock.h>
Expand Down
22 changes: 8 additions & 14 deletions nss/nss_action_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */

#include "nss_action.h"
#include "nss_module.h"
#include <nsswitch.h>

#include <ctype.h>
#include <string.h>
Expand Down Expand Up @@ -169,18 +168,13 @@ nss_action_list
action_list_init (&list);
if (nss_action_parse (line, &list))
{
size_t size = action_list_size (&list);
nss_action_list result
= malloc (sizeof (*result) * (size + 1));
if (result == NULL)
{
action_list_free (&list);
return NULL;
}
memcpy (result, action_list_begin (&list), sizeof (*result) * size);
/* Sentinel. */
result[size].module = NULL;
return result;
size_t size;
struct nss_action null_service
= { .module = NULL, };

action_list_add (&list, null_service);
size = action_list_size (&list);
return __nss_action_allocate (action_list_begin (&list), size);
}
else if (action_list_has_failed (&list))
{
Expand Down
40 changes: 38 additions & 2 deletions nss/nss_database.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,39 @@ process_line (struct nss_database_data *data, char *line)
return true;
}

int
__nss_configure_lookup (const char *dbname, const char *service_line)
{
int db;
nss_action_list result;
struct nss_database_state *local;

/* Convert named database to index. */
db = name_to_database_index (dbname);
if (db < 0)
/* Not our database (e.g., sudoers). */
return -1;

/* Force any load/cache/read whatever to happen, so we can override
it. */
__nss_database_get (db, &result);

local = nss_database_state_get ();

result = __nss_action_parse (service_line);
if (result == NULL)
return -1;

atomic_store_release (&local->data.reload_disabled, 1);
local->data.services[db] = result;

#ifdef USE_NSCD
__nss_database_custom[db] = true;
#endif

return 0;
}

/* Iterate over the lines in FP, parse them, and store them in DATA.
Return false on memory allocation failure, true on success. */
static bool
Expand Down Expand Up @@ -326,8 +359,11 @@ nss_database_check_reload_and_get (struct nss_database_state *local,
may have loaded the configuration first, so synchronize with the
Release MO store there. */
if (atomic_load_acquire (&local->data.reload_disabled))
/* No reload, so there is no error. */
return true;
{
*result = local->data.services[database_index];
/* No reload, so there is no error. */
return true;
}

struct file_change_detection initial;
if (!__file_change_detection_for_path (&initial, _PATH_NSSWITCH_CONF))
Expand Down
64 changes: 63 additions & 1 deletion nss/nss_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */

#include <nss_module.h>
#include <nsswitch.h>
#include <nscd/nscd.h>
#include <nscd/nscd_proto.h>

#include <array_length.h>
#include <assert.h>
Expand Down Expand Up @@ -287,6 +289,66 @@ __nss_module_get_function (struct nss_module *module, const char *name)
return fptr;
}

#if defined SHARED && defined USE_NSCD
/* Load all libraries for the service. */
static void
nss_load_all_libraries (const char *service, const char *def)
{
nss_action_list ni = NULL;

if (__nss_database_lookup2 (service, NULL, def, &ni) == 0)
while (ni->module != NULL)
{
__nss_module_load (ni->module);
++ni;
}
}

define_traced_file (pwd, _PATH_NSSWITCH_CONF);
define_traced_file (grp, _PATH_NSSWITCH_CONF);
define_traced_file (hst, _PATH_NSSWITCH_CONF);
define_traced_file (serv, _PATH_NSSWITCH_CONF);
define_traced_file (netgr, _PATH_NSSWITCH_CONF);

/* Called by nscd and nscd alone. */
void
__nss_disable_nscd (void (*cb) (size_t, struct traced_file *))
{
void (*cb1) (size_t, struct traced_file *);
cb1 = cb;
# ifdef PTR_MANGLE
PTR_MANGLE (cb);
# endif
nscd_init_cb = cb;
is_nscd = true;

/* Find all the relevant modules so that the init functions are called. */
nss_load_all_libraries ("passwd", DEFAULT_CONFIG);
nss_load_all_libraries ("group", DEFAULT_CONFIG);
nss_load_all_libraries ("hosts", "dns [!UNAVAIL=return] files");
nss_load_all_libraries ("services", NULL);

/* Make sure NSCD purges its cache if nsswitch.conf changes. */
init_traced_file (&pwd_traced_file.file, _PATH_NSSWITCH_CONF, 0);
cb1 (pwddb, &pwd_traced_file.file);
init_traced_file (&grp_traced_file.file, _PATH_NSSWITCH_CONF, 0);
cb1 (grpdb, &grp_traced_file.file);
init_traced_file (&hst_traced_file.file, _PATH_NSSWITCH_CONF, 0);
cb1 (hstdb, &hst_traced_file.file);
init_traced_file (&serv_traced_file.file, _PATH_NSSWITCH_CONF, 0);
cb1 (servdb, &serv_traced_file.file);
init_traced_file (&netgr_traced_file.file, _PATH_NSSWITCH_CONF, 0);
cb1 (netgrdb, &netgr_traced_file.file);

/* Disable all uses of NSCD. */
__nss_not_use_nscd_passwd = -1;
__nss_not_use_nscd_group = -1;
__nss_not_use_nscd_hosts = -1;
__nss_not_use_nscd_services = -1;
__nss_not_use_nscd_netgroup = -1;
}
#endif

void __libc_freeres_fn_section
__nss_module_freeres (void)
{
Expand Down
Loading

0 comments on commit f8847d8

Please sign in to comment.