nwrap,tests: Use nwrap_gr_copy_r() from lib nss_utils
authorPavel Filipenský <pfilipensky@samba.org>
Tue, 17 Jan 2023 10:50:08 +0000 (11:50 +0100)
committerAndreas Schneider <asn@samba.org>
Tue, 24 Jan 2023 08:56:35 +0000 (09:56 +0100)
nss_utils must be added to both nwrap,tests at the same time

Signed-off-by: Pavel Filipenský <pfilipensky@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
src/CMakeLists.txt
src/nss_wrapper.c
tests/CMakeLists.txt
tests/nss_nwrap.c

index 78f80fe1e77a59037e52d42cd695744f42addb0e..f56aad5bd204b3e860de2707efcee1fe839befaa 100644 (file)
@@ -13,7 +13,7 @@ endif()
 target_include_directories(nss_wrapper
                            PRIVATE
                                ${CMAKE_BINARY_DIR})
-target_link_libraries(nss_wrapper ${NWRAP_REQUIRED_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
+target_link_libraries(nss_wrapper nss_utils ${NWRAP_REQUIRED_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
 
 set_target_properties(
   nss_wrapper
index b629432b253af1ff993a2235d86be5b91d7e59cf..3399f06412a2e03b2e1c70f40d6ed2b588cf6e8e 100644 (file)
@@ -61,6 +61,7 @@
 #include <search.h>
 #include <assert.h>
 
+#include "nss_utils.h"
 /*
  * Defining _POSIX_PTHREAD_SEMANTICS before including pwd.h and grp.h  gives us
  * the posix getpwnam_r(), getpwuid_r(), getgrnam_r and getgrgid_r calls on
@@ -3013,93 +3014,6 @@ static void nwrap_gr_unload(struct nwrap_cache *nwrap)
        nwrap_gr->idx = 0;
 }
 
-static int nwrap_gr_copy_r(const struct group *src, struct group *dst,
-                          char *buf, size_t buflen, struct group **dstp)
-{
-       char *p = NULL;
-       uintptr_t align = 0;
-       unsigned int gr_mem_cnt = 0;
-       unsigned i;
-       size_t total_len;
-       size_t gr_name_len = strlen(src->gr_name) + 1;
-       size_t gr_passwd_len = strlen(src->gr_passwd) + 1;
-       union {
-               char *ptr;
-               char **data;
-       } g_mem;
-
-       for (i = 0; src->gr_mem[i] != NULL; i++) {
-               gr_mem_cnt++;
-       }
-
-       /* Align the memory for storing pointers */
-       align = __alignof__(char *) - ((p - (char *)0) % __alignof__(char *));
-       total_len = align +
-                   (1 + gr_mem_cnt) * sizeof(char *) +
-                   gr_name_len + gr_passwd_len;
-
-       if (total_len > buflen) {
-               errno = ERANGE;
-               return -1;
-       }
-       buflen -= total_len;
-
-       /* gr_mem */
-       p = buf + align;
-       g_mem.ptr = p;
-       dst->gr_mem = g_mem.data;
-
-       /* gr_name */
-       p += (1 + gr_mem_cnt) * sizeof(char *);
-       dst->gr_name = p;
-
-       /* gr_passwd */
-       p += gr_name_len;
-       dst->gr_passwd = p;
-
-       /* gr_mem[x] */
-       p += gr_passwd_len;
-
-       /* gr_gid */
-       dst->gr_gid = src->gr_gid;
-
-       memcpy(dst->gr_name, src->gr_name, gr_name_len);
-
-       memcpy(dst->gr_passwd, src->gr_passwd, gr_passwd_len);
-
-       /* Set the terminating entry */
-       dst->gr_mem[gr_mem_cnt] = NULL;
-
-       /* Now add the group members content */
-       total_len = 0;
-       for (i = 0; i < gr_mem_cnt; i++) {
-               size_t len = strlen(src->gr_mem[i]) + 1;
-
-               dst->gr_mem[i] = p;
-               total_len += len;
-               p += len;
-       }
-
-       if (total_len > buflen) {
-               errno = ERANGE;
-               return -1;
-       }
-
-       for (i = 0; i < gr_mem_cnt; i++) {
-               size_t len = strlen(src->gr_mem[i]) + 1;
-
-               memcpy(dst->gr_mem[i],
-                      src->gr_mem[i],
-                      len);
-       }
-
-       if (dstp != NULL) {
-               *dstp = dst;
-       }
-
-       return 0;
-}
-
 static struct nwrap_entlist *nwrap_entlist_init(struct nwrap_entdata *ed)
 {
        struct nwrap_entlist *el;
index 18c0ad2fe217d4aeb48a69cbe15fe6cc3a0f542c..24adbed80808096d9adb3848959c47bd298060b1 100644 (file)
@@ -1,6 +1,6 @@
 project(tests C)
 
-set(TESTSUITE_LIBRARIES ${NWRAP_REQUIRED_LIBRARIES} ${CMOCKA_LIBRARY})
+set(TESTSUITE_LIBRARIES nss_utils ${NWRAP_REQUIRED_LIBRARIES} ${CMOCKA_LIBRARY})
 string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
 
 add_library(nss_nwrap SHARED nss_nwrap.c)
index 62363a2e8665e6e7e22197c9c3237e93241e6f9d..df0f4ab8d81b31068fd7ba37384a57a80402b289 100644 (file)
@@ -8,6 +8,8 @@
 #include <pwd.h>
 #include <grp.h>
 
+#include "../src/nss_utils.h"
+
 #if defined(HAVE_NSS_H)
 /* Linux and BSD */
 #include <nss.h>