From patchwork Tue Nov 10 21:46:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: DJ Delorie X-Patchwork-Id: 1397813 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=sourceware.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=YEWTexzj; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CW1fg2QYrz9s1l for ; Wed, 11 Nov 2020 08:47:07 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4C59A3987413; Tue, 10 Nov 2020 21:47:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4C59A3987413 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1605044825; bh=vj2bMJraGjUa91IqwTYXLOBSoE0C2WyZi+qcifZYC44=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=YEWTexzj1cWu8SFXzffxrCrwnzxE5ulK5k3eFRl1SI938w72OmwRtfvYL04H2UE1Z TDdjDgkWRt49dLk6q1F44qw7KFY+RaOcloKVIr3yxg5BNmwralPBNWOF0jmydmqM6d 5KNueASv+PZLZzCI/sfhE5hcfVuZ9yJcWS7cbvRY= 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 [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id 368C83986826 for ; Tue, 10 Nov 2020 21:47:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 368C83986826 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-172-hGczKGWMOLqxOma42ooh1A-1; Tue, 10 Nov 2020 16:46:59 -0500 X-MC-Unique: hGczKGWMOLqxOma42ooh1A-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DFC04809DE2 for ; Tue, 10 Nov 2020 21:46:57 +0000 (UTC) Received: from greed.delorie.com (ovpn-112-91.rdu2.redhat.com [10.10.112.91]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9A7695C1C4 for ; Tue, 10 Nov 2020 21:46:57 +0000 (UTC) Received: from greed.delorie.com.redhat.com (localhost [127.0.0.1]) by greed.delorie.com (8.14.7/8.14.7) with ESMTP id 0AALku7N028839 for ; Tue, 10 Nov 2020 16:46:56 -0500 Date: Tue, 10 Nov 2020 16:46:56 -0500 Message-Id: To: libc-alpha@sourceware.org Subject: v4 [PATCH 5/6] nsswitch: user new internal API (tests) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_ASCII_DIVIDERS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, 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" From 910b6c8eeb52a00f03a9365329a18e94d1c28044 Mon Sep 17 00:00:00 2001 From: DJ Delorie Date: Mon, 9 Nov 2020 22:08:04 -0500 Subject: [PATCH 5/6] nsswitch: user new internal API (tests) Testsuite support and new test for new API. --- nss/nss_test.h | 9 + nss/nss_test1.c | 166 +++++++++++- nss/tst-reload1.c | 341 ++++++++++++++++++++++++ nss/tst-reload1.root/etc/nsswitch.conf | 3 + nss/tst-reload1.root/etc/nsswitch.conf2 | 3 + nss/tst-reload1.root/etc/services | 1 + nss/tst-reload1.root/tst-reload1.script | 2 + 7 files changed, 524 insertions(+), 1 deletion(-) create mode 100644 nss/tst-reload1.c create mode 100644 nss/tst-reload1.root/etc/nsswitch.conf create mode 100644 nss/tst-reload1.root/etc/nsswitch.conf2 create mode 100644 nss/tst-reload1.root/etc/services create mode 100644 nss/tst-reload1.root/tst-reload1.script diff --git a/nss/nss_test.h b/nss/nss_test.h index 49a9aff2fd..b6c6ad2333 100644 --- a/nss/nss_test.h +++ b/nss/nss_test.h @@ -33,10 +33,12 @@ #include #include +#include typedef struct test_tables { struct passwd *pwd_table; struct group *grp_table; + struct hostent *host_table; } test_tables; extern void _nss_test1_init_hook (test_tables *) __attribute__((weak)); @@ -44,9 +46,11 @@ extern void _nss_test2_init_hook (test_tables *) __attribute__((weak)); #define PWD_LAST() { .pw_name = NULL, .pw_uid = 0 } #define GRP_LAST() { .gr_name = NULL, .gr_gid = 0 } +#define HOST_LAST() { .h_name = NULL, .h_aliases = NULL, .h_length = 0, .h_addr_list = NULL } #define PWD_ISLAST(p) ((p)->pw_name == NULL && (p)->pw_uid == 0) #define GRP_ISLAST(g) ((g)->gr_name == NULL && (g)->gr_gid == 0) +#define HOST_ISLAST(h) ((h)->h_name == NULL && (h)->h_length == 0) /* Macros to fill in the tables easily. */ @@ -72,6 +76,11 @@ extern void _nss_test2_init_hook (test_tables *) __attribute__((weak)); { .gr_name = (char *) n, .gr_passwd = (char *) "*", .gr_gid = u, \ .gr_mem = (char **) m } +#define HOST(u) \ + { .h_name = (char *) "name" #u, .h_aliases = NULL, .h_addrtype = u, \ + .h_length = 4, \ + .h_addr_list = (char **) hostaddr_##u } + /*------------------------------------------------------------*/ /* Helper functions for testing passwd entries. Call diff --git a/nss/nss_test1.c b/nss/nss_test1.c index 13532cd7ab..612c427c57 100644 --- a/nss/nss_test1.c +++ b/nss/nss_test1.c @@ -66,6 +66,9 @@ static int npwd_data = default_npwd_data; static struct group *grp_data = NULL; static int ngrp_data = 0; +static struct hostent *host_data = NULL; +static int nhost_data = 0; + /* This function will get called, and once per session, look back into the test case's executable for an init hook function, and call it. */ @@ -99,6 +102,13 @@ init(void) ; ngrp_data = i; } + if (t.host_table) + { + host_data = t.host_table; + for (i=0; ! HOST_ISLAST(& host_data[i]); i++) + ; + nhost_data = i; + } } initted = 1; } @@ -280,7 +290,7 @@ NAME(getgrent_r) (struct group *result, char *buffer, size_t buflen, ++grp_iter; } - pthread_mutex_unlock (&pwd_lock); + pthread_mutex_unlock (&grp_lock); return res; } @@ -312,3 +322,157 @@ NAME(getgrnam_r) (const char *name, struct group *result, char *buffer, return NSS_STATUS_NOTFOUND; } + +/* -------------------------------------------------- */ +/* Host handling. */ + +static size_t host_iter; +#define CURHOST host_data[host_iter] + +static pthread_mutex_t host_lock = PTHREAD_MUTEX_INITIALIZER; + +enum nss_status +NAME(sethostent) (int stayopen) +{ + init(); + host_iter = 0; + return NSS_STATUS_SUCCESS; +} + + +enum nss_status +NAME(endhostent) (void) +{ + init(); + return NSS_STATUS_SUCCESS; +} + +static enum nss_status +copy_host (struct hostent *result, struct hostent *local, + char *buffer, size_t buflen, int *errnop) +{ + struct alloc_buffer buf = alloc_buffer_create (buffer, buflen); + char **memlist; + int i, j; + + if (local->h_addr_list) + { + i = 0; + while (local->h_addr_list[i]) + ++i; + + memlist = alloc_buffer_alloc_array (&buf, char *, i + 1); + + if (memlist) { + for (j = 0; j < i; ++j) + memlist[j] = alloc_buffer_maybe_copy_string (&buf, local->h_addr_list[j]); + memlist[j] = NULL; + } + + result->h_addr_list = memlist; + } + else + { + result->h_addr_list = NULL; + } + + result->h_aliases = NULL; + result->h_addrtype = AF_INET; + result->h_length = 4; + result->h_name = alloc_buffer_maybe_copy_string (&buf, local->h_name); + + if (alloc_buffer_has_failed (&buf)) + { + *errnop = ERANGE; + return NSS_STATUS_TRYAGAIN; + } + + return NSS_STATUS_SUCCESS; +} + + +enum nss_status +NAME(gethostent_r) (struct hostent *ret, char *buffer, size_t buflen, + struct hostent **result, int *errnop) +{ + int res = NSS_STATUS_SUCCESS; + + init(); + pthread_mutex_lock (&host_lock); + + if (host_iter >= nhost_data) + { + res = NSS_STATUS_NOTFOUND; + *result = NULL; + } + else + { + res = copy_host (ret, &CURHOST, buffer, buflen, errnop); + *result = ret; + ++host_iter; + } + + pthread_mutex_unlock (&host_lock); + + return res; +} + +enum nss_status +NAME(gethostbyname3_r) (const char *name, int af, struct hostent *ret, + char *buffer, size_t buflen, int *errnop, + int *h_errnop, int32_t *ttlp, char **canonp) +{ + init(); + + for (size_t idx = 0; idx < nhost_data; ++idx) + if (strcmp (host_data[idx].h_name, name) == 0) + return copy_host (ret, & host_data[idx], buffer, buflen, h_errnop); + + return NSS_STATUS_NOTFOUND; +} + +enum nss_status +NAME(gethostbyname_r) (const char *name, struct hostent *result, + char *buffer, size_t buflen, + int *errnop, int *h_errnop) +{ + return NAME(gethostbyname3_r) (name, AF_INET, result, buffer, buflen, + errnop, h_errnop, NULL, NULL); +} + +enum nss_status +NAME(gethostbyname2_r) (const char *name, int af, struct hostent *result, + char *buffer, size_t buflen, + int *errnop, int *h_errnop) +{ + return NAME(gethostbyname3_r) (name, af, result, buffer, buflen, + errnop, h_errnop, NULL, NULL); +} + +enum nss_status +NAME(gethostbyaddr2_r) (const void *addr, socklen_t len, int af, + struct hostent *result, char *buffer, size_t buflen, + int *errnop, int *h_errnop, int32_t *ttlp) +{ + init(); + + /* Support this later. */ + if (len != 4) + return NSS_STATUS_NOTFOUND; + + for (size_t idx = 0; idx < nhost_data; ++idx) + if (memcmp (host_data[idx].h_addr, addr, len) == 0) + return copy_host (result, & host_data[idx], buffer, buflen, h_errnop); + + return NSS_STATUS_NOTFOUND; +} + +/* Note: only the first address is supported, intentionally. */ +enum nss_status +NAME(gethostbyaddr_r) (const void *addr, socklen_t len, int af, + struct hostent *result, char *buffer, size_t buflen, + int *errnop, int *h_errnop) +{ + return NAME(gethostbyaddr2_r) (addr, len, af, result, buffer, buflen, + errnop, h_errnop, NULL); +} diff --git a/nss/tst-reload1.c b/nss/tst-reload1.c new file mode 100644 index 0000000000..b17b11d9da --- /dev/null +++ b/nss/tst-reload1.c @@ -0,0 +1,341 @@ +/* Test that nsswitch.conf reloading actually works. + Copyright (C) 2020 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 + +#include +#include + +#include "nss_test.h" + +/* Size of buffers used by *_r functions. */ +#define TESTBUFLEN 4096 + +static struct passwd pwd_table_1[] = { + PWD (100), + PWD (30), + PWD (200), + PWD (60), + PWD (20000), + PWD_LAST () + }; + +static const char *hostaddr_5[] = + { + "ABCD", "abcd", "1234", NULL + }; + +static const char *hostaddr_15[] = + { + "4321", "ghij", NULL + }; + +static const char *hostaddr_25[] = + { + "WXYZ", NULL + }; + + +static struct hostent host_table_1[] = { + HOST (5), + HOST (15), + HOST (25), + HOST_LAST () +}; + +void +_nss_test1_init_hook(test_tables *t) +{ + t->pwd_table = pwd_table_1; + t->host_table = host_table_1; +} + +/* The first of these must not appear in pwd_table_1. */ +static struct passwd pwd_table_2[] = { + PWD (5), + PWD_N(200, "name30"), + PWD (16), + PWD_LAST () + }; + +static const char *hostaddr_6[] = + { + "mnop", NULL + }; + +static const char *hostaddr_16[] = + { + "7890", "a1b2", NULL + }; + +static const char *hostaddr_26[] = + { + "qwer", "tyui", NULL + }; + +static struct hostent host_table_2[] = { + HOST (6), + HOST (16), + HOST (26), + HOST_LAST () +}; + +void +_nss_test2_init_hook(test_tables *t) +{ + t->pwd_table = pwd_table_2; + t->host_table = host_table_2; +} + +static void +must_be_tests (struct passwd *pt, struct hostent *ht) +{ + int i; + struct hostent *h; + + struct passwd *p; + for (i = 0; !PWD_ISLAST (&pt[i]); ++ i) + { + p = getpwuid (pt[i].pw_uid); + TEST_VERIFY (p != NULL); + if (p != NULL) + { + TEST_VERIFY (strcmp (p->pw_name, pt[i].pw_name) == 0); + } + } + + setpwent (); + for (i = 0; !PWD_ISLAST (&pt[i]); ++ i) + { + p = getpwent (); + TEST_VERIFY (p != NULL); + if (p != NULL) + { + TEST_VERIFY (strcmp (p->pw_name, pt[i].pw_name) == 0); + TEST_VERIFY (p->pw_uid == pt[i].pw_uid); + } + } + endpwent (); + + for (i = 0; !HOST_ISLAST (&ht[i]); ++ i) + { + h = gethostbyname (ht[i].h_name); + TEST_VERIFY (h != NULL); + if (h != NULL) + { + TEST_VERIFY (strcmp (h->h_name, ht[i].h_name) == 0); + TEST_VERIFY (h->h_addr_list[0] != NULL); + if (h->h_addr_list[0]) + TEST_VERIFY (strcmp (h->h_addr_list[0], ht[i].h_addr_list[0]) == 0); + } + } + + for (i = 0; !HOST_ISLAST (&ht[i]); ++ i) + { + struct hostent r, *rp; + char buf[TESTBUFLEN]; + int herrno, res; + + res = gethostbyname2_r (ht[i].h_name, AF_INET, + &r, buf, TESTBUFLEN, &rp, &herrno); + TEST_VERIFY (res == 0); + if (res == 0) + { + TEST_VERIFY (strcmp (r.h_name, ht[i].h_name) == 0); + TEST_VERIFY (r.h_addr_list[0] != NULL); + if (r.h_addr_list[0]) + TEST_VERIFY (strcmp (r.h_addr_list[0], ht[i].h_addr_list[0]) == 0); + } + } + + for (i = 0; !HOST_ISLAST (&ht[i]); ++ i) + { + h = gethostbyaddr (ht[i].h_addr, 4, AF_INET); + TEST_VERIFY (h != NULL); + if (h != NULL) + { + TEST_VERIFY (strcmp (h->h_name, ht[i].h_name) == 0); + TEST_VERIFY (h->h_addr_list[0] != NULL); + if (h->h_addr_list[0]) + TEST_VERIFY (strcmp (h->h_addr_list[0], ht[i].h_addr_list[0]) == 0); + } + } + + /* getaddrinfo */ + + for (i = 0; !HOST_ISLAST (&ht[i]); ++ i) + { + struct addrinfo *ap; + struct addrinfo hint; + int res, j; + + memset (&hint, 0, sizeof (hint)); + hint.ai_family = AF_INET; + hint.ai_socktype = SOCK_STREAM; + hint.ai_protocol = 0; + hint.ai_flags = 0; + + ap = NULL; + res = getaddrinfo (ht[i].h_name, NULL, &hint, &ap); + TEST_VERIFY (res == 0); + TEST_VERIFY (ap != NULL); + if (res == 0 && ap != NULL) + { + j = 0; /* which address in the list */ + while (ap) + { + struct sockaddr_in *in = (struct sockaddr_in *)ap->ai_addr; + unsigned char *up = (unsigned char *)&in->sin_addr; + + TEST_VERIFY (memcmp (up, ht[i].h_addr_list[j], 4) == 0); + + ap = ap->ai_next; + ++ j; + } + } + } + + /* getnameinfo */ + + for (i = 0; !HOST_ISLAST (&ht[i]); ++ i) + { + struct sockaddr_in addr; + int res; + char host_buf[NI_MAXHOST]; + + memset (&addr, 0, sizeof (addr)); + addr.sin_family = AF_INET; + addr.sin_port = 80; + memcpy (& addr.sin_addr, ht[i].h_addr_list[0], 4); + + res = getnameinfo ((struct sockaddr *) &addr, sizeof(addr), + host_buf, sizeof(host_buf), + NULL, 0, NI_NOFQDN); + + TEST_VERIFY (res == 0); + if (res == 0) + TEST_VERIFY (strcmp (ht[i].h_name, host_buf) == 0); + else + printf ("error %s\n", gai_strerror (res)); + } +} + +static void +must_be_1 (void) +{ + struct passwd *p; + + must_be_tests (pwd_table_1, host_table_1); + p = getpwnam("name5"); + TEST_VERIFY (p == NULL); +} + +static void +must_be_2 (void) +{ + struct passwd *p; + + must_be_tests (pwd_table_2, host_table_2); + p = getpwnam("name100"); + TEST_VERIFY (p == NULL); +} + +static void +xrename (const char *a, const char *b) +{ + int i = rename (a, b); + if (i != 0) + FAIL_EXIT1 ("rename(%s,%s) failed: %s\n", a, b, strerror(errno)); +} + +/* If the actions change while in the midst of doing a series of + lookups, make sure they're consistent. */ +static void +test_cross_switch_consistency (void) +{ + int i; + struct passwd *p; + + /* We start by initiating a set/get/end loop on conf1. */ + setpwent (); + for (i = 0; !PWD_ISLAST (&pwd_table_1[i]); ++ i) + { + p = getpwent (); + TEST_VERIFY (p != NULL); + if (p != NULL) + { + TEST_VERIFY (strcmp (p->pw_name, pwd_table_1[i].pw_name) == 0); + TEST_VERIFY (p->pw_uid == pwd_table_1[i].pw_uid); + } + + /* After the first lookup, switch to conf2 and verify */ + if (i == 0) + { + xrename ("/etc/nsswitch.conf", "/etc/nsswitch.conf1"); + xrename ("/etc/nsswitch.conf2", "/etc/nsswitch.conf"); + + p = getpwnam (pwd_table_2[0].pw_name); + TEST_VERIFY (p->pw_uid == pwd_table_2[0].pw_uid); + } + + /* But the original loop should still be on conf1. */ + } + endpwent (); + + /* Make sure the set/get/end loop sees conf2 now. */ + setpwent (); + for (i = 0; !PWD_ISLAST (&pwd_table_2[i]); ++ i) + { + p = getpwent (); + TEST_VERIFY (p != NULL); + if (p != NULL) + { + TEST_VERIFY (strcmp (p->pw_name, pwd_table_2[i].pw_name) == 0); + TEST_VERIFY (p->pw_uid == pwd_table_2[i].pw_uid); + } + } + endpwent (); + +} + +static int +do_test (void) +{ + /* The test1 module was configured at program start. */ + must_be_1 (); + + xrename ("/etc/nsswitch.conf", "/etc/nsswitch.conf1"); + xrename ("/etc/nsswitch.conf2", "/etc/nsswitch.conf"); + must_be_2 (); + + xrename ("/etc/nsswitch.conf", "/etc/nsswitch.conf2"); + xrename ("/etc/nsswitch.conf1", "/etc/nsswitch.conf"); + must_be_1 (); + + test_cross_switch_consistency (); + + return 0; +} + +#include diff --git a/nss/tst-reload1.root/etc/nsswitch.conf b/nss/tst-reload1.root/etc/nsswitch.conf new file mode 100644 index 0000000000..606d8f51d6 --- /dev/null +++ b/nss/tst-reload1.root/etc/nsswitch.conf @@ -0,0 +1,3 @@ +passwd: test1 +group: test1 +hosts: test1 diff --git a/nss/tst-reload1.root/etc/nsswitch.conf2 b/nss/tst-reload1.root/etc/nsswitch.conf2 new file mode 100644 index 0000000000..2e93977efb --- /dev/null +++ b/nss/tst-reload1.root/etc/nsswitch.conf2 @@ -0,0 +1,3 @@ +passwd: test2 +group: test2 +hosts: test2 diff --git a/nss/tst-reload1.root/etc/services b/nss/tst-reload1.root/etc/services new file mode 100644 index 0000000000..94d4f07612 --- /dev/null +++ b/nss/tst-reload1.root/etc/services @@ -0,0 +1 @@ +http 80/tcp diff --git a/nss/tst-reload1.root/tst-reload1.script b/nss/tst-reload1.root/tst-reload1.script new file mode 100644 index 0000000000..a10beb1e6c --- /dev/null +++ b/nss/tst-reload1.root/tst-reload1.script @@ -0,0 +1,2 @@ +cp $B/nss/libnss_test1.so $L/libnss_test1.so.2 +cp $B/nss/libnss_test2.so $L/libnss_test2.so.2