From patchwork Tue Feb 16 02:34:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: DJ Delorie X-Patchwork-Id: 42134 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E9D8E3894C2E; Tue, 23 Feb 2021 23:56:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E9D8E3894C2E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1614124602; bh=orXBCvsBxAzl1aeK8ur8lik1rIwwdjDqwbrVd6oiHfo=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=r3FhZCoYebF12DNmps2tm1T/hh/ygkQgaToEkd2RtzGLJWNrZ0dIxYasRWqS38zEo vMEHryxbaGj1SoxaFi7XjuyTvej/GBK90qlsDmF9yflAE2WOfmjJWNFcBcpwpl3P30 kQdXySORTJk1RVzjwNFgQMsnAt6jNx6TG/kYgdzk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 780EC3848012 for ; Tue, 23 Feb 2021 23:56:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 780EC3848012 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-10-gDpqkBw1PuW3Ar2q0lsWVQ-1; Tue, 23 Feb 2021 18:56:36 -0500 X-MC-Unique: gDpqkBw1PuW3Ar2q0lsWVQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BFA081868404 for ; Tue, 23 Feb 2021 23:56:35 +0000 (UTC) Received: from greed.delorie.com (ovpn-116-4.rdu2.redhat.com [10.10.116.4]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 915935D9D0 for ; Tue, 23 Feb 2021 23:56:35 +0000 (UTC) Received: from greed.delorie.com (localhost [127.0.0.1]) by greed.delorie.com (8.14.7/8.14.7) with ESMTP id 11NNuYPX032420 for ; Tue, 23 Feb 2021 18:56:34 -0500 Received: (from dj@localhost) by greed.delorie.com (8.14.7/8.14.7/Submit) id 11NNuYvW032419; Tue, 23 Feb 2021 18:56:34 -0500 Message-Id: <202102232356.11NNuYvW032419@greed.delorie.com> Date: Mon, 15 Feb 2021 21:34:23 -0500 Subject: [PATCH v2] nss: fix nss_database_lookup2's alternate handling [BZ #27416] To: libc-alpha@sourceware.org X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DATE_IN_PAST_96_XX, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: DJ Delorie via Libc-alpha From: DJ Delorie Reply-To: DJ Delorie Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" __nss_database_lookup2's extra arguments were left unused in the nsswitch reloading patch set; this broke compat (default config ignored) and shadow files (secondary name ignored). This patch adds in the previous behavior. The test case verifies that compat targets work (passwd) and that the default configuration works (group). Tested on x86-64. --- nss/Makefile | 1 + nss/databases.def | 3 + nss/nss_database.c | 2 +- nss/nsswitch.c | 38 +++++------ nss/tst-nss-compat1.c | 64 +++++++++++++++++++ nss/tst-nss-compat1.root/etc/group | 1 + nss/tst-nss-compat1.root/etc/nsswitch.conf | 3 + nss/tst-nss-compat1.root/etc/passwd | 3 + .../tst-nss-compat1.script | 1 + 9 files changed, 97 insertions(+), 19 deletions(-) create mode 100644 nss/tst-nss-compat1.c create mode 100644 nss/tst-nss-compat1.root/etc/group create mode 100644 nss/tst-nss-compat1.root/etc/nsswitch.conf create mode 100644 nss/tst-nss-compat1.root/etc/passwd create mode 100644 nss/tst-nss-compat1.root/tst-nss-compat1.script diff --git a/nss/Makefile b/nss/Makefile index 0906202db9..71fbe583bf 100644 --- a/nss/Makefile +++ b/nss/Makefile @@ -63,6 +63,7 @@ tests = test-netdb test-digits-dots tst-nss-getpwent bug17079 \ xtests = bug-erange tests-container = \ + tst-nss-compat1 \ tst-nss-test3 \ tst-nss-files-hosts-long \ tst-nss-db-endpwent \ diff --git a/nss/databases.def b/nss/databases.def index df5fab4168..3dc95648a8 100644 --- a/nss/databases.def +++ b/nss/databases.def @@ -23,17 +23,20 @@ DEFINE_DATABASE (aliases) DEFINE_DATABASE (ethers) DEFINE_DATABASE (group) +DEFINE_DATABASE (group_compat) DEFINE_DATABASE (gshadow) DEFINE_DATABASE (hosts) DEFINE_DATABASE (initgroups) DEFINE_DATABASE (netgroup) DEFINE_DATABASE (networks) DEFINE_DATABASE (passwd) +DEFINE_DATABASE (passwd_compat) DEFINE_DATABASE (protocols) DEFINE_DATABASE (publickey) DEFINE_DATABASE (rpc) DEFINE_DATABASE (services) DEFINE_DATABASE (shadow) +DEFINE_DATABASE (shadow_compat) /* Local Variables: diff --git a/nss/nss_database.c b/nss/nss_database.c index fb72d0cc03..9ff3bb6ffb 100644 --- a/nss/nss_database.c +++ b/nss/nss_database.c @@ -172,7 +172,7 @@ nss_database_select_default (struct nss_database_default_cache *cache, /* database_name must be large enough for each individual name plus a null terminator. */ -typedef char database_name[11]; +typedef char database_name[14]; #define DEFINE_DATABASE(name) \ _Static_assert (sizeof (#name) <= sizeof (database_name), #name); #include "databases.def" diff --git a/nss/nsswitch.c b/nss/nsswitch.c index 46f232d720..6d8673b507 100644 --- a/nss/nsswitch.c +++ b/nss/nsswitch.c @@ -76,24 +76,26 @@ __nss_database_lookup2 (const char *database, const char *alternate_name, for (database_id = 0; database_names[database_id]; database_id++) if (strcmp (database_names[database_id], database) == 0) - break; - - if (database_names[database_id] == NULL) - return -1; - - /* If *NI is NULL, the database was not mentioned in nsswitch.conf. - If *NI is not NULL, but *NI->module is NULL, the database was in - nsswitch.conf but listed no actions. We test for the former. */ - if (__nss_database_get (database_id, ni) && *ni != NULL) - { - /* Success. */ - return 0; - } - else - { - /* Failure. */ - return -1; - } + /* If *NI is NULL, the database was not mentioned in nsswitch.conf. + If *NI is not NULL, but *NI->module is NULL, the database was in + nsswitch.conf but listed no actions. We test for the former. */ + if (__nss_database_get (database_id, ni) && *ni != NULL) + return 0; + + /* Primary name not found, try alternate. */ + if (alternate_name) + for (database_id = 0; database_names[database_id]; database_id++) + if (strcmp (database_names[database_id], alternate_name) == 0) + if (__nss_database_get (database_id, ni) && *ni != NULL) + return 0; + + /* Neither found, use default config. */ + *ni = __nss_action_parse (defconfig); + if (*ni != NULL) + return 0; + + /* Failure. */ + return -1; } libc_hidden_def (__nss_database_lookup2) diff --git a/nss/tst-nss-compat1.c b/nss/tst-nss-compat1.c new file mode 100644 index 0000000000..45355ef225 --- /dev/null +++ b/nss/tst-nss-compat1.c @@ -0,0 +1,64 @@ +/* Test error checking for group entries. + Copyright (C) 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 + . */ + +#include +#include +#include +#include + +#include +#include + +#include "nss_test.h" + +static struct passwd pwd_table[] = { + PWD (100), + PWD (30), + PWD_LAST () + }; + +void +_nss_test1_init_hook(test_tables *t) +{ + t->pwd_table = pwd_table; +} + +static int +do_test (void) +{ + struct passwd *p = NULL; + struct group *g = NULL; + + /* Test that compat-to-test works. */ + p = getpwuid (100); + if (p == NULL) + FAIL_EXIT1("getpwuid-compat-test1 p"); + else if (strcmp (p->pw_name, "name100") != 0) + FAIL_EXIT1("getpwuid-compat-test1 name100"); + + /* Test that internal defconfig works. */ + g = getgrgid (100); + if (g == NULL) + FAIL_EXIT1("getgrgid-compat-null"); + if (strcmp (g->gr_name, "wilma") != 0) + FAIL_EXIT1("getgrgid-compat-name"); + + return 0; +} + +#include diff --git a/nss/tst-nss-compat1.root/etc/group b/nss/tst-nss-compat1.root/etc/group new file mode 100644 index 0000000000..ee467c7950 --- /dev/null +++ b/nss/tst-nss-compat1.root/etc/group @@ -0,0 +1 @@ +wilma:x:100: diff --git a/nss/tst-nss-compat1.root/etc/nsswitch.conf b/nss/tst-nss-compat1.root/etc/nsswitch.conf new file mode 100644 index 0000000000..7fe69d5ffa --- /dev/null +++ b/nss/tst-nss-compat1.root/etc/nsswitch.conf @@ -0,0 +1,3 @@ +passwd : compat +passwd_compat : test1 + diff --git a/nss/tst-nss-compat1.root/etc/passwd b/nss/tst-nss-compat1.root/etc/passwd new file mode 100644 index 0000000000..84635587bd --- /dev/null +++ b/nss/tst-nss-compat1.root/etc/passwd @@ -0,0 +1,3 @@ +name5:x:5:555:name5 for testing:/home/name5:/bin/nologin ++name100 ++name30 diff --git a/nss/tst-nss-compat1.root/tst-nss-compat1.script b/nss/tst-nss-compat1.root/tst-nss-compat1.script new file mode 100644 index 0000000000..fe6e863f01 --- /dev/null +++ b/nss/tst-nss-compat1.root/tst-nss-compat1.script @@ -0,0 +1 @@ +cp $B/nss/libnss_test1.so $L/libnss_test1.so.2