[SCM] Samba Shared Repository - branch v3-2-test updated - initial-v3-2-unstable-266-g8ad4287

Stefan Metzmacher metze at samba.org
Fri Nov 9 09:55:40 GMT 2007


The branch, v3-2-test has been updated
       via  8ad4287cca5393a7fef148365aef2e2377c29280 (commit)
       via  bb125f3e084a99125e92cc6c3aceb7f2b66db678 (commit)
       via  fc98c1904865608509a01911afa46de74873ef41 (commit)
       via  02cfd283627bd8f10d7beb8d43cead35dd867346 (commit)
       via  e0c8c01650b2aa0a1ab491c9800b8aa575bf4185 (commit)
       via  350d6c19e3e933804ce7234f21ef008c74894e2e (commit)
       via  17b449212a2f68262a95b0ebaabb377cc9a8dcc9 (commit)
       via  95dfbd3a6c22ff6381c8b220b06ec3a5cf59c06b (commit)
       via  70ea441fe46d75162adc6c4b055a9fd5fab00342 (commit)
       via  77900d2ba03455412ad645e757ba468ca90453fb (commit)
       via  2451399acb9dbba6b02935f6a1d02e2b56b499ef (commit)
       via  8e81dd7356eb8422284a93bfc9b55ed1523c8208 (commit)
       via  96827929626a528bb3e1ec8366aef58c174d67e1 (commit)
       via  28789e60cc59a3e7a42500ebb78c472093be6b12 (commit)
       via  6abc84a564682f2b3dda314b5a08816ef99ac517 (commit)
       via  36af3e864c2698b2f4ffd1bc3c873f1e4f8b718b (commit)
       via  0924b0c8de70c0b84785cf6885f6aab325fc9b8e (commit)
      from  4642af8026f086488672a51a0ecce2603e65f4ce (commit)

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=v3-2-test


- Log -----------------------------------------------------------------
commit 8ad4287cca5393a7fef148365aef2e2377c29280
Author: Stefan Metzmacher <metze at samba.org>
Date:   Wed Oct 24 13:18:42 2007 +0200

    make test: start winbindd
    
    metze

commit bb125f3e084a99125e92cc6c3aceb7f2b66db678
Author: Stefan Metzmacher <metze at samba.org>
Date:   Thu Oct 18 17:14:37 2007 +0200

    we don't have build-farm specific tests anymore
    
    metze

commit fc98c1904865608509a01911afa46de74873ef41
Author: Stefan Metzmacher <metze at samba.org>
Date:   Thu Oct 18 17:13:01 2007 +0200

    remove faked_create_user() BUILD_FARM hack as we have nss_wrapper now
    
    metze

commit 02cfd283627bd8f10d7beb8d43cead35dd867346
Author: Stefan Metzmacher <metze at samba.org>
Date:   Mon Nov 5 09:49:08 2007 +0100

    make test: activate nss_wrapper for make test
    
    metze

commit e0c8c01650b2aa0a1ab491c9800b8aa575bf4185
Author: metze <metze at 0c0555d6-39d7-0310-84fc-f1cc0bd64818>
Date:   Tue Nov 6 12:34:50 2007 +0000

    nss_wrapper: hopefully a better fix for the solaris get*ent_r functions
    
    metze
    
    git-svn-id: svn+ssh://svn.samba.org/data/svn/samba/branches/SAMBA_4_0@25873 0c0555d6-39d7-0310-84fc-f1cc0bd64818

commit 350d6c19e3e933804ce7234f21ef008c74894e2e
Author: metze <metze at 0c0555d6-39d7-0310-84fc-f1cc0bd64818>
Date:   Tue Nov 6 12:15:19 2007 +0000

    nss_wrapper: revert solaris get*ent_r() functions
    
    This was the wrong approach, as in future lib/replace
    should hide this special solaris stuff.
    
    metze
    
    git-svn-id: svn+ssh://svn.samba.org/data/svn/samba/branches/SAMBA_4_0@25872 0c0555d6-39d7-0310-84fc-f1cc0bd64818

commit 17b449212a2f68262a95b0ebaabb377cc9a8dcc9
Author: metze <metze at 0c0555d6-39d7-0310-84fc-f1cc0bd64818>
Date:   Tue Nov 6 12:11:35 2007 +0000

    nss_wrapper: set the destination pointer in the get*_r functions
    
    metze
    
    git-svn-id: svn+ssh://svn.samba.org/data/svn/samba/branches/SAMBA_4_0@25871 0c0555d6-39d7-0310-84fc-f1cc0bd64818

commit 95dfbd3a6c22ff6381c8b220b06ec3a5cf59c06b
Author: metze <metze at 0c0555d6-39d7-0310-84fc-f1cc0bd64818>
Date:   Tue Nov 6 10:40:07 2007 +0000

    libreplace: AC_VERIFY_C_PROTOTYPE() needs AC_LANG_SOURCE() to bring in confdefs.h
    
    metze
    
    git-svn-id: svn+ssh://svn.samba.org/data/svn/samba/branches/SAMBA_4_0@25870 0c0555d6-39d7-0310-84fc-f1cc0bd64818

commit 70ea441fe46d75162adc6c4b055a9fd5fab00342
Author: metze <metze at 0c0555d6-39d7-0310-84fc-f1cc0bd64818>
Date:   Tue Nov 6 09:26:42 2007 +0000

    nss_wrapper: add solaris versions of getpwent_r and getgrent_r
    
    metze
    
    git-svn-id: svn+ssh://svn.samba.org/data/svn/samba/branches/SAMBA_4_0@25868 0c0555d6-39d7-0310-84fc-f1cc0bd64818

commit 77900d2ba03455412ad645e757ba468ca90453fb
Author: metze <metze at 0c0555d6-39d7-0310-84fc-f1cc0bd64818>
Date:   Tue Nov 6 09:15:11 2007 +0000

    libreplace: solaris has different prototypes for getpwent_r and getgrent_r
    
    metze
    
    git-svn-id: svn+ssh://svn.samba.org/data/svn/samba/branches/SAMBA_4_0@25867 0c0555d6-39d7-0310-84fc-f1cc0bd64818

commit 2451399acb9dbba6b02935f6a1d02e2b56b499ef
Author: metze <metze at 0c0555d6-39d7-0310-84fc-f1cc0bd64818>
Date:   Tue Nov 6 08:06:50 2007 +0000

    nss_wrapper: not all systems have get*_r calls
    
    metze
    
    git-svn-id: svn+ssh://svn.samba.org/data/svn/samba/branches/SAMBA_4_0@25866 0c0555d6-39d7-0310-84fc-f1cc0bd64818

commit 8e81dd7356eb8422284a93bfc9b55ed1523c8208
Author: metze <metze at 0c0555d6-39d7-0310-84fc-f1cc0bd64818>
Date:   Tue Nov 6 07:26:12 2007 +0000

    libreplace: check for nss passwd|group get*_r functions
    
    metze
    
    git-svn-id: svn+ssh://svn.samba.org/data/svn/samba/branches/SAMBA_4_0@25865 0c0555d6-39d7-0310-84fc-f1cc0bd64818

commit 96827929626a528bb3e1ec8366aef58c174d67e1
Author: Stefan Metzmacher <metze at samba.org>
Date:   Thu Oct 18 16:46:16 2007 +0200

    use nss_wrapper code in samba3 when --enable-nss-wrapper is given
    
    metze

commit 28789e60cc59a3e7a42500ebb78c472093be6b12
Author: metze <metze at 0c0555d6-39d7-0310-84fc-f1cc0bd64818>
Date:   Mon Nov 5 15:42:38 2007 +0000

    nss_wrapper: add nss_wrapper.pl
    
    This script will be used to add|delete passwd|group entries
    
    metze
    
    git-svn-id: svn+ssh://svn.samba.org/data/svn/samba/branches/SAMBA_4_0@25837 0c0555d6-39d7-0310-84fc-f1cc0bd64818

commit 6abc84a564682f2b3dda314b5a08816ef99ac517
Author: metze <metze at 0c0555d6-39d7-0310-84fc-f1cc0bd64818>
Date:   Mon Nov 5 15:41:23 2007 +0000

    nss_wrapper: add support for groups and return EPERM in initgroups()
    
    The NSS_WRAPPER_GROUP envvar should point to
    the /etc/group style file.
    
    Note: NSS_WRAPPER_PASSWD and NSS_WRAPPER_GROUP both
          need non-empty values to enable NSS Wrapper
          support.
    
    metze
    
    git-svn-id: svn+ssh://svn.samba.org/data/svn/samba/branches/SAMBA_4_0@25836 0c0555d6-39d7-0310-84fc-f1cc0bd64818

commit 36af3e864c2698b2f4ffd1bc3c873f1e4f8b718b
Author: metze <metze at 0c0555d6-39d7-0310-84fc-f1cc0bd64818>
Date:   Mon Nov 5 15:39:46 2007 +0000

    nss_wrapper: add support for passwd accounts
    
    The NSS_WRAPPER_PASSWD envvar should point to
    the /etc/passwd style file.
    
    metze
    
    git-svn-id: svn+ssh://svn.samba.org/data/svn/samba/branches/SAMBA_4_0@25835 0c0555d6-39d7-0310-84fc-f1cc0bd64818

commit 0924b0c8de70c0b84785cf6885f6aab325fc9b8e
Author: metze <metze at 0c0555d6-39d7-0310-84fc-f1cc0bd64818>
Date:   Mon Nov 5 15:38:36 2007 +0000

    nss_wrapper: add lib/nss_wrapper skeleton
    
    metze
    
    git-svn-id: svn+ssh://svn.samba.org/data/svn/samba/branches/SAMBA_4_0@25834 0c0555d6-39d7-0310-84fc-f1cc0bd64818

-----------------------------------------------------------------------

Summary of changes:
 source/Makefile.in                      |    8 +-
 source/configure.developer              |    1 +
 source/configure.in                     |    1 +
 source/lib/nss_wrapper/config.m4        |   19 +
 source/lib/nss_wrapper/config.mk        |   10 +
 source/lib/nss_wrapper/nss_wrapper.c    | 1130 +++++++++++++++++++++++++++++++
 source/lib/nss_wrapper/nss_wrapper.h    |  165 +++++
 source/lib/nss_wrapper/nss_wrapper.pl   |  265 ++++++++
 source/lib/replace/libreplace_macros.m4 |    4 +-
 source/lib/replace/system/config.m4     |   34 +
 source/lib/system.c                     |   77 ---
 source/passdb/pdb_interface.c           |    8 -
 source/script/tests/selftest.sh         |   45 ++-
 source/script/tests/test_functions.sh   |   57 ++
 source/script/tests/test_posix_s3.sh    |    5 +-
 15 files changed, 1731 insertions(+), 98 deletions(-)
 create mode 100644 source/lib/nss_wrapper/config.m4
 create mode 100644 source/lib/nss_wrapper/config.mk
 create mode 100644 source/lib/nss_wrapper/nss_wrapper.c
 create mode 100644 source/lib/nss_wrapper/nss_wrapper.h
 create mode 100644 source/lib/nss_wrapper/nss_wrapper.pl


Changeset truncated at 500 lines:

diff --git a/source/Makefile.in b/source/Makefile.in
index ef3a0c9..884e5a4 100644
--- a/source/Makefile.in
+++ b/source/Makefile.in
@@ -273,11 +273,12 @@ RPC_PARSE_OBJ2 = rpc_parse/parse_rpc.o rpc_parse/parse_net.o rpc_parse/parse_srv
 LIBREPLACE_OBJ = @LIBREPLACE_OBJS@
 
 SOCKET_WRAPPER_OBJ = @SOCKET_WRAPPER_OBJS@
+NSS_WRAPPER_OBJ = @NSS_WRAPPER_OBJS@
 
 TALLOC_OBJ = lib/talloc/talloc.o
 
 
-LIB_WITHOUT_PROTO_OBJ = $(LIBREPLACE_OBJ) $(SOCKET_WRAPPER_OBJ) $(TALLOC_OBJ) \
+LIB_WITHOUT_PROTO_OBJ = $(LIBREPLACE_OBJ) $(SOCKET_WRAPPER_OBJ) $(NSS_WRAPPER_OBJ) $(TALLOC_OBJ) \
 	lib/messages.o librpc/gen_ndr/ndr_messaging.o lib/messages_local.o \
 	lib/messages_ctdbd.o lib/packet.o lib/ctdbd_conn.o lib/talloc_stack.o \
 	lib/interfaces.o
@@ -2043,11 +2044,12 @@ test_pam_modules: pam_modules
 ##
 test: all torture timelimit
 	@echo Running Test suite
-	@sh $(srcdir)/script/tests/selftest.sh ${selftest_prefix}/st all "${smbtorture4_path}"
+	@PERL="$(PERL)" $(srcdir)/script/tests/selftest.sh ${selftest_prefix}/st all "${smbtorture4_path}"
 
 valgrindtest: all torture timelimit
 	@echo Running Test suite with valgrind
 	@NMBD_VALGRIND="xterm -n nmbd -e valgrind -q --db-attach=yes --num-callers=30" \
+	 WINBINDD_VALGRIND="xterm -n winbindd -e valgrind -q --db-attach=yes --num-callers=30" \
 	 SMBD_VALGRIND="xterm -n smbd -e valgrind -q --db-attach=yes --num-callers=30" \
 	 VALGRIND="valgrind -q --num-callers=30 --log-file=${selftest_prefix}/st/valgrind.log" \
-	 $(srcdir)/script/tests/selftest.sh ${selftest_prefix}/st all "${smbtorture4_path}"
+	 PERL="$(PERL)" $(srcdir)/script/tests/selftest.sh ${selftest_prefix}/st all "${smbtorture4_path}"
diff --git a/source/configure.developer b/source/configure.developer
index b8cf6d6..5033670 100755
--- a/source/configure.developer
+++ b/source/configure.developer
@@ -2,4 +2,5 @@
 `dirname $0`/configure -C \
 	--enable-developer \
 	--enable-socket-wrapper \
+	--enable-nss-wrapper \
 	"$@"
diff --git a/source/configure.in b/source/configure.in
index 02ca8ac..be2b262 100644
--- a/source/configure.in
+++ b/source/configure.in
@@ -370,6 +370,7 @@ CFLAGS="${CFLAGS} -D_SAMBA_BUILD_=3"
 AC_LIBREPLACE_CC_CHECKS
 
 m4_include(lib/socket_wrapper/config.m4)
+m4_include(lib/nss_wrapper/config.m4)
 
 SWAT_SBIN_TARGETS='bin/swat$(EXEEXT)'
 SWAT_INSTALL_TARGETS=installswat
diff --git a/source/lib/nss_wrapper/config.m4 b/source/lib/nss_wrapper/config.m4
new file mode 100644
index 0000000..58e94f9
--- /dev/null
+++ b/source/lib/nss_wrapper/config.m4
@@ -0,0 +1,19 @@
+AC_ARG_ENABLE(nss-wrapper,
+[  --enable-nss-wrapper         Turn on nss wrapper library (default=no)])
+
+HAVE_NSS_WRAPPER=no
+
+if eval "test x$developer = xyes"; then
+	enable_nss_wrapper=yes
+fi
+
+if eval "test x$enable_nss_wrapper = xyes"; then
+        AC_DEFINE(NSS_WRAPPER,1,[Use nss wrapper library])
+	HAVE_NSS_WRAPPER=yes
+
+	# this is only used for samba3
+	NSS_WRAPPER_OBJS="lib/nss_wrapper/nss_wrapper.o"
+fi
+
+AC_SUBST(HAVE_NSS_WRAPPER)
+AC_SUBST(NSS_WRAPPER_OBJS)
diff --git a/source/lib/nss_wrapper/config.mk b/source/lib/nss_wrapper/config.mk
new file mode 100644
index 0000000..9751d2b
--- /dev/null
+++ b/source/lib/nss_wrapper/config.mk
@@ -0,0 +1,10 @@
+##############################
+# Start SUBSYSTEM NSS_WRAPPER
+[LIBRARY::NSS_WRAPPER]
+VERSION = 0.0.1
+SO_VERSION = 0
+DESCRIPTION = Wrapper library for testing nss calls without being root
+PUBLIC_HEADERS = nss_wrapper.h
+OBJ_FILES = nss_wrapper.o
+# End SUBSYSTEM NSS_WRAPPER
+##############################
diff --git a/source/lib/nss_wrapper/nss_wrapper.c b/source/lib/nss_wrapper/nss_wrapper.c
new file mode 100644
index 0000000..5bf7ebd
--- /dev/null
+++ b/source/lib/nss_wrapper/nss_wrapper.c
@@ -0,0 +1,1130 @@
+/*
+ * Copyright (C) Stefan Metzmacher 2007 <metze at samba.org>
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of the author nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifdef _SAMBA_BUILD_
+
+#define NSS_WRAPPER_NOT_REPLACE
+#include "lib/replace/replace.h"
+#include "system/passwd.h"
+#include "system/filesys.h"
+
+#else /* _SAMBA_BUILD_ */
+
+#error nss_wrapper_only_supported_in_samba_yet
+
+#endif
+
+#ifndef _PUBLIC_
+#define _PUBLIC_
+#endif
+
+/* not all systems have _r functions... */
+#ifndef HAVE_GETPWNAM_R
+#define getpwnam_r(name, pwdst, buf, buflen, pwdstp)	ENOSYS
+#endif
+#ifndef HAVE_GETPWUID_R
+#define getpwuid_r(uid, pwdst, buf, buflen, pwdstp)	ENOSYS
+#endif
+#ifndef HAVE_GETPWENT_R
+#define getpwent_r(pwdst, buf, buflen, pwdstp)		ENOSYS
+#endif
+#ifndef HAVE_GETGRNAM_R
+#define getgrnam_r(name, grdst, buf, buflen, grdstp)	ENOSYS
+#endif
+#ifndef HAVE_GETGRUID_R
+#define getgrgid_r(uid, grdst, buf, buflen, grdstp)	ENOSYS
+#endif
+#ifndef HAVE_GETGRENT_R
+#define getgrent_r(grdst, buf, buflen, grdstp)		ENOSYS
+#endif
+
+/* LD_PRELOAD doesn't work yet, so REWRITE_CALLS is all we support
+ * for now */
+#define REWRITE_CALLS
+
+#ifdef REWRITE_CALLS
+
+#define real_getpwnam		getpwnam
+#define real_getpwnam_r		getpwnam_r
+#define real_getpwuid		getpwuid
+#define real_getpwuid_r		getpwuid_r
+
+#define real_setpwent		setpwent
+#define real_getpwent		getpwent
+#define real_getpwent_r		getpwent_r
+#define real_endpwent		endpwent
+
+/*
+#define real_getgrlst		getgrlst
+#define real_getgrlst_r		getgrlst_r
+#define real_initgroups_dyn	initgroups_dyn
+*/
+#define real_initgroups		initgroups
+
+#define real_getgrnam		getgrnam
+#define real_getgrnam_r		getgrnam_r
+#define real_getgrgid		getgrgid
+#define real_getgrgid_r		getgrgid_r
+
+#define real_setgrent		setgrent
+#define real_getgrent		getgrent
+#define real_getgrent_r		getgrent_r
+#define real_endgrent		endgrent
+
+#endif
+
+#if 0
+# ifdef DEBUG
+# define NWRAP_ERROR(args)	DEBUG(0, args)
+# else
+# define NWRAP_ERROR(args)	printf args
+# endif
+#else
+#define NWRAP_ERROR(args)
+#endif
+
+#if 0
+# ifdef DEBUG
+# define NWRAP_DEBUG(args)	DEBUG(0, args)
+# else
+# define NWRAP_DEBUG(args)	printf args
+# endif
+#else
+#define NWRAP_DEBUG(args)
+#endif
+
+#if 0
+# ifdef DEBUG
+# define NWRAP_VERBOSE(args)	DEBUG(0, args)
+# else
+# define NWRAP_VERBOSE(args)	printf args
+# endif
+#else
+#define NWRAP_VERBOSE(args)
+#endif
+
+struct nwrap_cache {
+	const char *path;
+	int fd;
+	struct stat st;
+	uint8_t *buf;
+	void *private_data;
+	bool (*parse_line)(struct nwrap_cache *, char *line);
+	void (*unload)(struct nwrap_cache *);
+};
+
+struct nwrap_pw {
+	struct nwrap_cache *cache;
+
+	struct passwd *list;
+	int num;
+	int idx;
+};
+
+struct nwrap_cache __nwrap_cache_pw;
+struct nwrap_pw nwrap_pw_global;
+
+static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line);
+static void nwrap_pw_unload(struct nwrap_cache *nwrap);
+
+struct nwrap_gr {
+	struct nwrap_cache *cache;
+
+	struct group *list;
+	int num;
+	int idx;
+};
+
+struct nwrap_cache __nwrap_cache_gr;
+struct nwrap_gr nwrap_gr_global;
+
+static bool nwrap_gr_parse_line(struct nwrap_cache *nwrap, char *line);
+static void nwrap_gr_unload(struct nwrap_cache *nwrap);
+
+static void nwrap_init(void)
+{
+	static bool initialized;
+
+	if (initialized) return;
+	initialized = true;
+
+	nwrap_pw_global.cache = &__nwrap_cache_pw;
+
+	nwrap_pw_global.cache->path = getenv("NSS_WRAPPER_PASSWD");
+	nwrap_pw_global.cache->fd = -1;
+	nwrap_pw_global.cache->private_data = &nwrap_pw_global;
+	nwrap_pw_global.cache->parse_line = nwrap_pw_parse_line;
+	nwrap_pw_global.cache->unload = nwrap_pw_unload;
+
+	nwrap_gr_global.cache = &__nwrap_cache_gr;
+
+	nwrap_gr_global.cache->path = getenv("NSS_WRAPPER_GROUP");
+	nwrap_gr_global.cache->fd = -1;
+	nwrap_gr_global.cache->private_data = &nwrap_gr_global;
+	nwrap_gr_global.cache->parse_line = nwrap_gr_parse_line;
+	nwrap_gr_global.cache->unload = nwrap_gr_unload;
+}
+
+static bool nwrap_enabled(void)
+{
+	nwrap_init();
+
+	if (!nwrap_pw_global.cache->path) {
+		return false;
+	}
+	if (nwrap_pw_global.cache->path[0] == '\0') {
+		return false;
+	}
+	if (!nwrap_gr_global.cache->path) {
+		return false;
+	}
+	if (nwrap_gr_global.cache->path[0] == '\0') {
+		return false;
+	}
+
+	return true;
+}
+
+static bool nwrap_parse_file(struct nwrap_cache *nwrap)
+{
+	int ret;
+	uint8_t *buf = NULL;
+	char *nline;
+
+	if (nwrap->st.st_size == 0) {
+		NWRAP_DEBUG(("%s: size == 0\n",
+			     __location__));
+		goto done;
+	}
+
+	if (nwrap->st.st_size > INT32_MAX) {
+		NWRAP_ERROR(("%s: size[%u] larger than INT32_MAX\n",
+			     __location__, (unsigned)nwrap->st.st_size));
+		goto failed;
+	}
+
+	ret = lseek(nwrap->fd, 0, SEEK_SET);
+	if (ret != 0) {
+		NWRAP_ERROR(("%s: lseek - %d\n",__location__,ret));
+		goto failed;
+	}
+
+	buf = malloc(nwrap->st.st_size + 1);
+	if (!buf) {
+		NWRAP_ERROR(("%s: malloc failed\n",__location__));
+		goto failed;
+	}
+
+	ret = read(nwrap->fd, buf, nwrap->st.st_size);
+	if (ret != nwrap->st.st_size) {
+		NWRAP_ERROR(("%s: read(%u) gave %d\n",
+			     __location__, (unsigned)nwrap->st.st_size, ret));
+		goto failed;
+	}
+
+	buf[nwrap->st.st_size] = '\0';
+
+	nline = (char *)buf;
+	while (nline && nline[0]) {
+		char *line;
+		char *e;
+		bool ok;
+
+		line = nline;
+		nline = NULL;
+
+		e = strchr(line, '\n');
+		if (e) {
+			e[0] = '\0';
+			e++;
+			if (e[0] == '\r') {
+				e[0] = '\0';
+				e++;
+			}
+			nline = e;
+		}
+
+		NWRAP_VERBOSE(("%s:'%s'\n",__location__, line));
+
+		if (strlen(line) == 0) {
+			continue;
+		}
+
+		ok = nwrap->parse_line(nwrap, line);
+		if (!ok) {
+			goto failed;
+		}
+	}
+
+done:
+	nwrap->buf = buf;
+	return true;
+
+failed:
+	if (buf) free(buf);
+	return false;
+}
+
+static void nwrap_cache_unload(struct nwrap_cache *nwrap)
+{
+	nwrap->unload(nwrap);
+
+	if (nwrap->buf) free(nwrap->buf);
+
+	nwrap->buf = NULL;
+}
+
+static void nwrap_cache_reload(struct nwrap_cache *nwrap)
+{
+	struct stat st;
+	int ret;
+	bool ok;
+	bool retried = false;
+
+reopen:
+	if (nwrap->fd < 0) {
+		nwrap->fd = open(nwrap->path, O_RDONLY);
+		if (nwrap->fd < 0) {
+			NWRAP_ERROR(("%s: unable to open '%s' readonly %d:%s\n",
+				     __location__,
+				     nwrap->path, nwrap->fd,
+				     strerror(errno)));
+			return;
+		}
+		NWRAP_VERBOSE(("%s: open '%s'\n", __location__, nwrap->path));
+	}
+
+	ret = fstat(nwrap->fd, &st);
+	if (ret != 0) {
+		NWRAP_ERROR(("%s: fstat(%s) - %d:%s\n",
+			     __location__,
+			     nwrap->path,
+			     ret, strerror(errno)));
+		return;
+	}
+
+	if (retried == false && st.st_nlink == 0) {
+		/* maybe someone has replaced the file... */
+		NWRAP_DEBUG(("%s: st_nlink == 0, reopen %s\n",
+			     __location__, nwrap->path));
+		retried = true;
+		memset(&nwrap->st, 0, sizeof(nwrap->st));
+		close(nwrap->fd);
+		nwrap->fd = -1;
+		goto reopen;
+	}
+
+	if (st.st_mtime == nwrap->st.st_mtime) {
+		NWRAP_VERBOSE(("%s: st_mtime[%u] hasn't changed, skip reload\n",
+			       __location__, (unsigned)st.st_mtime));
+		return;
+	}
+	NWRAP_DEBUG(("%s: st_mtime has changed [%u] => [%u], start reload\n",
+		     __location__, (unsigned)st.st_mtime,
+		     (unsigned)nwrap->st.st_mtime));
+
+	nwrap->st = st;
+
+	nwrap_cache_unload(nwrap);
+
+	ok = nwrap_parse_file(nwrap);
+	if (!ok) {
+		NWRAP_ERROR(("%s: failed to reload %s\n",
+			     __location__, nwrap->path));
+		nwrap_cache_unload(nwrap);
+	}
+	NWRAP_DEBUG(("%s: reloaded %s\n",
+		     __location__, nwrap->path));
+}
+
+/*
+ * the caller has to call nwrap_unload() on failure
+ */
+static bool nwrap_pw_parse_line(struct nwrap_cache *nwrap, char *line)
+{
+	struct nwrap_pw *nwrap_pw;
+	char *c;
+	char *p;
+	char *e;
+	struct passwd *pw;
+	size_t list_size;
+
+	nwrap_pw = (struct nwrap_pw *)nwrap->private_data;
+
+	list_size = sizeof(*nwrap_pw->list) * (nwrap_pw->num+1);
+	pw = (struct passwd *)realloc(nwrap_pw->list, list_size);
+	if (!pw) {
+		NWRAP_ERROR(("%s:realloc(%u) failed\n",
+			     __location__, list_size));
+		return false;
+	}
+	nwrap_pw->list = pw;
+
+	pw = &nwrap_pw->list[nwrap_pw->num];
+
+	c = line;
+
+	/* name */
+	p = strchr(c, ':');


-- 
Samba Shared Repository


More information about the samba-cvs mailing list