[SSSD] [PATCH] Split becoming user into a separate module (with tests!)

Jakub Hrozek jhrozek at redhat.com
Thu Oct 9 18:12:33 UTC 2014


On Thu, Oct 09, 2014 at 05:07:27PM +0200, Lukas Slebodnik wrote:
> On (08/10/14 17:46), Lukas Slebodnik wrote:
> >On (06/10/14 15:46), Jakub Hrozek wrote:
> >>Hi,
> >>
> >>attached are more patches that are related to running sssd as a
> >>non-root. I split the functions to become a different user into a
> >>separate module and provided unit tests using nss_wrapper and
> >>uid_wrapper.
> >>
> >>There is a separate Makefile.am for the cwrap-based tests, because it's
> >>not easy (read: possible without a wrapper script) to run tests with a
> >>custom environment. Some build systems, like cmake, allow that, but
> >>autotools does not.
> >
> >>From 565aca30c666cb696ccb5c6a9426144276c7dce9 Mon Sep 17 00:00:00 2001
> >>From: Jakub Hrozek <jhrozek at redhat.com>
> >>Date: Sat, 26 Jul 2014 12:46:26 +0200
> >>Subject: [PATCH 1/3] UTIL: Move become_user outside krb5 tree
> >>
> >>In order for several other SSSD processes to run as a non-root user, we
> >>need to move the functions to become another user to a shared space in
> >>our source tree.
> >>---
> >> Makefile.am                                          | 20 ++++++++++++--------
> >> src/providers/krb5/krb5_utils.h                      |  8 --------
> >> .../krb5/krb5_become_user.c => util/become_user.c}   |  1 -
> >> src/util/util.h                                      |  9 +++++++++
> >> 4 files changed, 21 insertions(+), 17 deletions(-)
> >> rename src/{providers/krb5/krb5_become_user.c => util/become_user.c} (99%)
> >>
> >>diff --git a/Makefile.am b/Makefile.am
> >>index eb0e649437f465d3354a0c063a29c65203824506..ac3f26cad6e1d0a5ac8979ce13286bc896c62b62 100644
> >>--- a/Makefile.am
> >>+++ b/Makefile.am
> >>@@ -1286,13 +1286,14 @@ strtonum_tests_LDADD = \
> >> krb5_utils_tests_SOURCES = \
> >>     src/tests/krb5_utils-tests.c \
> >>     src/providers/krb5/krb5_utils.c \
> >>-    src/providers/krb5/krb5_become_user.c \
> >>     src/providers/krb5/krb5_common.c \
> >>     src/util/sss_krb5.c \
> >>     src/providers/data_provider_fo.c \
> >>     src/providers/data_provider_opts.c \
> >>     src/providers/data_provider_callbacks.c \
> >>-    $(SSSD_FAILOVER_OBJ)
> >>+    src/util/become_user.c \
> >>+    $(SSSD_FAILOVER_OBJ) \
> >>+    $(NULL)
> >Does it make sense to add NULL at the end?
> >You put new file before SSSD_FAILOVER_OBJ.
> >If you think that it make sense I will not insist on change.
> >

I'm not sure what you propose as a better way. I'm not opposed to a
change, but I'm not sure what shall I change.

I added the conditional into specfile. I also squashed in your changes
(thanks!) and new patches are attached.
-------------- next part --------------
>From 3525f453ad243cb385433a1b4689d93954758fc4 Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek at redhat.com>
Date: Sat, 26 Jul 2014 12:46:26 +0200
Subject: [PATCH 1/3] UTIL: Move become_user outside krb5 tree

In order for several other SSSD processes to run as a non-root user, we
need to move the functions to become another user to a shared space in
our source tree.
---
 Makefile.am                                          | 20 ++++++++++++--------
 src/providers/krb5/krb5_utils.h                      |  8 --------
 .../krb5/krb5_become_user.c => util/become_user.c}   |  1 -
 src/util/util.h                                      |  9 +++++++++
 4 files changed, 21 insertions(+), 17 deletions(-)
 rename src/{providers/krb5/krb5_become_user.c => util/become_user.c} (99%)

diff --git a/Makefile.am b/Makefile.am
index eb0e649437f465d3354a0c063a29c65203824506..ac3f26cad6e1d0a5ac8979ce13286bc896c62b62 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1286,13 +1286,14 @@ strtonum_tests_LDADD = \
 krb5_utils_tests_SOURCES = \
     src/tests/krb5_utils-tests.c \
     src/providers/krb5/krb5_utils.c \
-    src/providers/krb5/krb5_become_user.c \
     src/providers/krb5/krb5_common.c \
     src/util/sss_krb5.c \
     src/providers/data_provider_fo.c \
     src/providers/data_provider_opts.c \
     src/providers/data_provider_callbacks.c \
-    $(SSSD_FAILOVER_OBJ)
+    src/util/become_user.c \
+    $(SSSD_FAILOVER_OBJ) \
+    $(NULL)
 krb5_utils_tests_CFLAGS = \
     $(AM_CFLAGS) \
     $(KRB5_CFLAGS) \
@@ -1567,13 +1568,14 @@ krb5_child_test_SOURCES = \
     src/tests/krb5_child-test.c \
     src/providers/krb5/krb5_utils.c \
     src/providers/krb5/krb5_child_handler.c \
-    src/providers/krb5/krb5_become_user.c \
     src/providers/krb5/krb5_common.c \
     src/util/sss_krb5.c \
     src/providers/data_provider_fo.c \
     src/providers/data_provider_opts.c \
     src/providers/data_provider_callbacks.c \
-    $(SSSD_FAILOVER_OBJ)
+    src/util/become_user.c \
+    $(SSSD_FAILOVER_OBJ) \
+    $(NULL)
 krb5_child_test_CFLAGS = \
     $(AM_CFLAGS) \
     -DKRB5_CHILD_DIR=\"$(builddir)\" \
@@ -2243,7 +2245,6 @@ libsss_ad_common_la_LIBADD = \
 
 libsss_krb5_common_la_SOURCES = \
     src/providers/krb5/krb5_utils.c \
-    src/providers/krb5/krb5_become_user.c \
     src/providers/krb5/krb5_delayed_online_authentication.c \
     src/providers/krb5/krb5_renew_tgt.c \
     src/providers/krb5/krb5_wait_queue.c \
@@ -2252,7 +2253,9 @@ libsss_krb5_common_la_SOURCES = \
     src/providers/krb5/krb5_access.c \
     src/providers/krb5/krb5_child_handler.c \
     src/providers/krb5/krb5_init_shared.c \
-    src/util/sss_krb5.c
+    src/util/sss_krb5.c \
+    src/util/become_user.c \
+    $(NULL)
 libsss_krb5_common_la_CFLAGS = \
     $(KRB5_CFLAGS)
 libsss_krb5_common_la_LIBADD = \
@@ -2432,7 +2435,6 @@ libsss_ad_la_LDFLAGS = \
     -module
 
 krb5_child_SOURCES = \
-    src/providers/krb5/krb5_become_user.c \
     src/providers/krb5/krb5_child.c \
     src/providers/dp_pam_data_util.c \
     src/util/user_info_msg.c \
@@ -2441,7 +2443,9 @@ krb5_child_SOURCES = \
     src/util/authtok.c \
     src/util/util.c \
     src/util/signal.c \
-    src/sss_client/common.c
+    src/util/become_user.c \
+    src/sss_client/common.c \
+    $(NULL)
 krb5_child_CFLAGS = \
     $(AM_CFLAGS) \
     $(POPT_CFLAGS) \
diff --git a/src/providers/krb5/krb5_utils.h b/src/providers/krb5/krb5_utils.h
index 4b1ebb0bb7a9e13d68ee62820f6408d029a2f072..f54a07f7936a361c21ca933026ee753a89fe5808 100644
--- a/src/providers/krb5/krb5_utils.h
+++ b/src/providers/krb5/krb5_utils.h
@@ -49,14 +49,6 @@ char *expand_ccname_template(TALLOC_CTX *mem_ctx, struct krb5child_req *kr,
                              const char *template, bool file_mode,
                              bool case_sensitive);
 
-errno_t become_user(uid_t uid, gid_t gid);
-struct sss_creds;
-errno_t switch_creds(TALLOC_CTX *mem_ctx,
-                     uid_t uid, gid_t gid,
-                     int num_gids, gid_t *gids,
-                     struct sss_creds **saved_creds);
-errno_t restore_creds(struct sss_creds *saved_creds);
-
 errno_t sss_krb5_precreate_ccache(const char *ccname, pcre *illegal_re,
                                   uid_t uid, gid_t gid);
 errno_t sss_krb5_cc_destroy(const char *ccname, uid_t uid, gid_t gid);
diff --git a/src/providers/krb5/krb5_become_user.c b/src/util/become_user.c
similarity index 99%
rename from src/providers/krb5/krb5_become_user.c
rename to src/util/become_user.c
index 6ddb35a56393e124354cada1111830ebdb7c9a18..b5f94f993cd2c23bd3340fc502d36a530aa729fa 100644
--- a/src/providers/krb5/krb5_become_user.c
+++ b/src/util/become_user.c
@@ -23,7 +23,6 @@
 */
 
 #include "util/util.h"
-#include "providers/krb5/krb5_utils.h"
 #include <grp.h>
 
 errno_t become_user(uid_t uid, gid_t gid)
diff --git a/src/util/util.h b/src/util/util.h
index 482a660e75b6a94bc0df2289ebb2c44e6157658f..c497f605bb58a6acba45c64399a05cc3539b4263 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -580,4 +580,13 @@ char * sss_reverse_replace_space(TALLOC_CTX *mem_ctx,
                                  const char *orig_name,
                                  const char replace_char);
 
+/* from become_user.c */
+errno_t become_user(uid_t uid, gid_t gid);
+struct sss_creds;
+errno_t switch_creds(TALLOC_CTX *mem_ctx,
+                     uid_t uid, gid_t gid,
+                     int num_gids, gid_t *gids,
+                     struct sss_creds **saved_creds);
+errno_t restore_creds(struct sss_creds *saved_creds);
+
 #endif /* __SSSD_UTIL_H__ */
-- 
1.9.3

-------------- next part --------------
>From a4cfacc34986bd7f4ff6dc2da3e544417130b422 Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek at redhat.com>
Date: Sat, 26 Jul 2014 14:58:58 +0200
Subject: [PATCH 2/3] BUILD: Detect nss_wrapper and uid_wrapper during
 configure

Unit testing the utilities to become another user requires the use of
the cwrap libraries. This patch augments our build system with macros to
detect the nss_wrapper and and uid_wrapper libraries.
---
 configure.ac          |  3 +++
 src/external/cwrap.m4 | 31 +++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)
 create mode 100644 src/external/cwrap.m4

diff --git a/configure.ac b/configure.ac
index 2852c2f8e35efe5e7da76d4f25487f968e73bfde..1edf4cb19e390ced2043353341d321425652da71 100644
--- a/configure.ac
+++ b/configure.ac
@@ -171,6 +171,7 @@ m4_include([src/external/samba.m4])
 m4_include([src/external/sasl.m4])
 m4_include([src/external/configlib.m4])
 m4_include([src/external/libnfsidmap.m4])
+m4_include([src/external/cwrap.m4])
 
 if test x$build_config_lib = xyes; then
     m4_include([src/external/libaugeas.m4])
@@ -323,6 +324,8 @@ AM_CONDITIONAL([HAVE_DOXYGEN], [test x$DOXYGEN != xfalse ])
 
 AM_CONDITIONAL([HAVE_CHECK], [test x$have_check != x])
 AM_CHECK_CMOCKA
+AM_CHECK_UID_WRAPPER
+AM_CHECK_NSS_WRAPPER
 
 AM_CONDITIONAL([HAVE_DEVSHM], [test -d /dev/shm])
 
diff --git a/src/external/cwrap.m4 b/src/external/cwrap.m4
new file mode 100644
index 0000000000000000000000000000000000000000..0bd0bc9c98814340c210b5929f2fc38985de86a9
--- /dev/null
+++ b/src/external/cwrap.m4
@@ -0,0 +1,31 @@
+dnl A macro to check presence of a cwrap wrapper on the system
+dnl Usage:
+dnl     AM_CHECK_WRAPPER(name, conditional)
+dnl If the cwrap library is found, sets the HAVE_$name conditional
+AC_DEFUN([AM_CHECK_WRAPPER],
+[
+    FOUND_WRAPPER=0
+
+    AC_MSG_CHECKING([for $1])
+    PKG_CHECK_EXISTS([$1],
+                     [
+                        AC_MSG_RESULT([yes])
+                        FOUND_WRAPPER=1
+                     ],
+                     [
+                        AC_MSG_RESULT([no])
+                        AC_MSG_WARN([cwrap library $1 not found, some tests will not run])
+                     ])
+
+    AM_CONDITIONAL($2, [ test x$FOUND_WRAPPER = x1])
+])
+
+AC_DEFUN([AM_CHECK_UID_WRAPPER],
+[
+    AM_CHECK_WRAPPER(uid_wrapper, HAVE_UID_WRAPPER)
+])
+
+AC_DEFUN([AM_CHECK_NSS_WRAPPER],
+[
+    AM_CHECK_WRAPPER(nss_wrapper, HAVE_NSS_WRAPPER)
+])
-- 
1.9.3

-------------- next part --------------
>From 43a450c869f5593b1357c51d384c9ab7cb6d2b75 Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek at redhat.com>
Date: Sun, 27 Jul 2014 14:44:24 +0200
Subject: [PATCH 3/3] TESTS: Add a test to change user IDs

Adds a unit test using the nss_wrapper and uid_wrapper libraries that
exercises the ability to become another user.
---
 Makefile.am                         |   3 +
 configure.ac                        |   2 +-
 contrib/ci/deps.sh                  |   2 +
 contrib/sssd.spec.in                |   4 +
 src/tests/cwrap/Makefile.am         |  47 +++++++++++
 src/tests/cwrap/cwrap_test_setup.sh |  17 ++++
 src/tests/cwrap/group               |   1 +
 src/tests/cwrap/passwd              |   1 +
 src/tests/cwrap/test_become_user.c  | 156 ++++++++++++++++++++++++++++++++++++
 9 files changed, 232 insertions(+), 1 deletion(-)
 create mode 100644 src/tests/cwrap/Makefile.am
 create mode 100755 src/tests/cwrap/cwrap_test_setup.sh
 create mode 100644 src/tests/cwrap/group
 create mode 100644 src/tests/cwrap/passwd
 create mode 100644 src/tests/cwrap/test_become_user.c

diff --git a/Makefile.am b/Makefile.am
index ac3f26cad6e1d0a5ac8979ce13286bc896c62b62..ee6e39ba7f818524884ab9e4efc4e78791af1122 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,6 +19,8 @@ if HAVE_MANPAGES
 SUBDIRS += src/man
 endif
 
+SUBDIRS += . src/tests/cwrap
+
 # Some old versions of automake don't define builddir
 builddir ?= .
 
@@ -2829,6 +2831,7 @@ endif
 CLEANFILES = *.X */*.X */*/*.X
 
 tests: all $(check_PROGRAMS)
+	(cd src/tests/cwrap && $(MAKE) $(AM_MAKEFLAGS) $@) || exit 1;
 
 
 # RPM-related tasks
diff --git a/configure.ac b/configure.ac
index 1edf4cb19e390ced2043353341d321425652da71..660ea8d373272917d2d6ceeafdccf5dddbe493bc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -335,7 +335,7 @@ AC_SUBST([abs_builddir], $abs_build_dir)
 
 AC_CONFIG_FILES([Makefile contrib/sssd.spec src/examples/rwtab src/doxy.config
                  src/sysv/sssd src/sysv/gentoo/sssd src/sysv/SUSE/sssd
-                 po/Makefile.in src/man/Makefile
+                 po/Makefile.in src/man/Makefile src/tests/cwrap/Makefile
                  src/providers/ipa/ipa_hbac.pc src/providers/ipa/ipa_hbac.doxy
                  src/lib/idmap/sss_idmap.pc src/lib/idmap/sss_idmap.doxy
                  src/sss_client/sudo/sss_sudo.doxy
diff --git a/contrib/ci/deps.sh b/contrib/ci/deps.sh
index 998d2858b6aab6032782266c6e679a703ae28ffc..37a8e46c03d269281cbfc9f0b2137c0c25754af7 100644
--- a/contrib/ci/deps.sh
+++ b/contrib/ci/deps.sh
@@ -35,6 +35,8 @@ if [[ "$DISTRO_BRANCH" == -redhat-* ]]; then
         libcmocka-devel
         mock
         rpm-build
+        uid_wrapper
+        nss_wrapper
     )
     _DEPS_LIST_SPEC=`
         sed -e 's/@PACKAGE_VERSION@/0/g' \
diff --git a/contrib/sssd.spec.in b/contrib/sssd.spec.in
index 611730c1928b5bbfaa2aee50967226c5824bbca0..529304205274ad39e4281e3d298222420d5eb439 100644
--- a/contrib/sssd.spec.in
+++ b/contrib/sssd.spec.in
@@ -114,6 +114,10 @@ BuildRequires: selinux-policy-targeted
 %if 0%{?fedora}
 BuildRequires: libcmocka-devel
 %endif
+%if (0%{?fedora} >= 20)
+BuildRequires: uid_wrapper
+BuildRequires: nss_wrapper
+%endif
 %if (0%{?fedora} || 0%{?rhel} >= 7)
 BuildRequires: libnl3-devel
 %else
diff --git a/src/tests/cwrap/Makefile.am b/src/tests/cwrap/Makefile.am
new file mode 100644
index 0000000000000000000000000000000000000000..34aec92c197a60c47067c72df9c8f3644e1a3c45
--- /dev/null
+++ b/src/tests/cwrap/Makefile.am
@@ -0,0 +1,47 @@
+AM_CPPFLAGS = \
+    -Wall \
+    -I$(top_srcdir)/src \
+    -I. \
+    $(DBUS_CFLAGS) \
+    $(NULL)
+
+TESTS_ENVIRONMENT = \
+    CWRAP_TEST_SRCDIR=$(abs_srcdir) \
+    . $(srcdir)/cwrap_test_setup.sh; \
+    $(AUX_TESTS_ENVIRONMENT) \
+    $(NULL)
+
+dist_noinst_SCRIPTS = \
+    cwrap_test_setup.sh \
+    $(NULL)
+
+dist_noinst_DATA = \
+    group \
+    passwd \
+    $(NULL)
+
+check_PROGRAMS =
+if HAVE_CMOCKA
+if HAVE_NSS_WRAPPER
+if HAVE_UID_WRAPPER
+check_PROGRAMS += become_user-tests
+endif # HAVE_UID_WRAPPER
+endif # HAVE_NSS_WRAPPER
+endif # HAVE_CMOCKA
+
+TESTS = $(check_PROGRAMS)
+
+become_user_tests_SOURCES = \
+    test_become_user.c \
+    $(NULL)
+become_user_tests_CFLAGS = \
+    $(AM_CFLAGS) \
+    $(NULL)
+become_user_tests_LDADD = \
+    $(POPT_LIBS) \
+    $(CMOCKA_LIBS) \
+    $(abs_top_builddir)/libsss_debug.la \
+    $(abs_top_builddir)/libsss_test_common.la \
+    $(NULL)
+
+tests: $(check_PROGRAMS)
diff --git a/src/tests/cwrap/cwrap_test_setup.sh b/src/tests/cwrap/cwrap_test_setup.sh
new file mode 100755
index 0000000000000000000000000000000000000000..0d35cb7e5f94a4ee035bbc794640f1dd79b17649
--- /dev/null
+++ b/src/tests/cwrap/cwrap_test_setup.sh
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+pkg-config --exists nss_wrapper || exit 1
+pkg-config --exists uid_wrapper || exit 1
+
+nss_wrapper=$(pkg-config --libs nss_wrapper)
+uid_wrapper=$(pkg-config --libs uid_wrapper)
+if [ -z $nss_wrapper -o -z $uid_wrapper ]; then
+    echo "Cannot locate cwrap libraries"
+    exit 2
+fi
+
+export LD_PRELOAD="$nss_wrapper $uid_wrapper"
+export NSS_WRAPPER_PASSWD=$CWRAP_TEST_SRCDIR/passwd
+export NSS_WRAPPER_GROUP=$CWRAP_TEST_SRCDIR/group
+export UID_WRAPPER=1
+export UID_WRAPPER_ROOT=1
diff --git a/src/tests/cwrap/group b/src/tests/cwrap/group
new file mode 100644
index 0000000000000000000000000000000000000000..61e428c41a3df9d18cd216864d90c7f2603e8820
--- /dev/null
+++ b/src/tests/cwrap/group
@@ -0,0 +1 @@
+sssd:x:123:
diff --git a/src/tests/cwrap/passwd b/src/tests/cwrap/passwd
new file mode 100644
index 0000000000000000000000000000000000000000..aa0a97db5259172c0b4ab47c7c2346fa5c2aa88e
--- /dev/null
+++ b/src/tests/cwrap/passwd
@@ -0,0 +1 @@
+sssd:x:123:123:sssd unprivileged user:/:/sbin/nologin
diff --git a/src/tests/cwrap/test_become_user.c b/src/tests/cwrap/test_become_user.c
new file mode 100644
index 0000000000000000000000000000000000000000..06d3ad425c4928e9e9bff661fbb8f7b4536b8896
--- /dev/null
+++ b/src/tests/cwrap/test_become_user.c
@@ -0,0 +1,156 @@
+/*
+    Authors:
+        Jakub Hrozek <jhrozek at redhat.com>
+
+    Copyright (C) 2014 Red Hat
+
+    SSSD tests: User switching
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+
+    This program 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 General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/* Yes, a .c file. We need to call static functions during the test */
+#include "../../../src/util/become_user.c"
+
+#include <popt.h>
+#include "util/util.h"
+#include "tests/cmocka/common_mock.h"
+
+void test_become_user(void **state)
+{
+    struct passwd *sssd;
+    errno_t ret;
+    pid_t pid, wpid;
+    int status;
+
+    /* Must root as root, real or fake */
+    assert_int_equal(geteuid(), 0);
+
+    sssd = getpwnam("sssd");
+    assert_non_null(sssd);
+
+    pid = fork();
+    if (pid == 0) {
+        /* Change the UID in a child */
+        ret = become_user(sssd->pw_uid, sssd->pw_gid);
+        assert_int_equal(ret, EOK);
+
+        /* Make sure we have the requested UID and GID now and there
+         * are no supplementary groups
+         */
+        assert_int_equal(geteuid(), sssd->pw_uid);
+        assert_int_equal(getegid(), sssd->pw_gid);
+        assert_int_equal(getuid(), sssd->pw_uid);
+        assert_int_equal(getgid(), sssd->pw_gid);
+
+        /* Another become_user is a no-op */
+        ret = become_user(sssd->pw_uid, sssd->pw_gid);
+        assert_int_equal(ret, EOK);
+
+        assert_int_equal(getgroups(0, NULL), 0);
+        exit(0);
+    }
+
+    assert_int_not_equal(pid, -1);
+
+    wpid = waitpid(pid, &status, 0);
+    assert_int_equal(wpid, pid);
+    assert_true(WIFEXITED(status));
+    assert_int_equal(WEXITSTATUS(status), 0);
+}
+
+void test_switch_user(void **state)
+{
+    errno_t ret;
+    struct passwd *sssd;
+    TALLOC_CTX *tmp_ctx;
+    struct sss_creds *saved_creds;
+
+    check_leaks_push(global_talloc_context);
+    tmp_ctx = talloc_new(global_talloc_context);
+    assert_non_null(tmp_ctx);
+
+    /* Must root as root, real or fake */
+    assert_int_equal(geteuid(), 0);
+
+    sssd = getpwnam("sssd");
+    assert_non_null(sssd);
+
+    check_leaks_push(tmp_ctx);
+
+    ret = switch_creds(tmp_ctx, sssd->pw_uid, sssd->pw_gid,
+                       0, NULL, &saved_creds);
+    assert_int_equal(ret, EOK);
+    assert_int_equal(geteuid(), sssd->pw_uid);
+    assert_int_equal(getegid(), sssd->pw_gid);
+    /* Only effective UID is changed.. */
+    assert_int_equal(getuid(), 0);
+    assert_int_equal(getgid(), 0);
+
+    assert_non_null(saved_creds);
+    assert_int_equal(saved_creds->uid, 0);
+    assert_int_equal(saved_creds->gid, 0);
+
+    /* restore root */
+    ret = restore_creds(saved_creds);
+    assert_int_equal(ret, EOK);
+    assert_int_equal(geteuid(), 0);
+    assert_int_equal(getegid(), 0);
+    assert_int_equal(getuid(), 0);
+    assert_int_equal(getgid(), 0);
+
+    talloc_free(saved_creds);
+    check_leaks_pop(tmp_ctx);
+    talloc_free(tmp_ctx);
+    check_leaks_pop(global_talloc_context);
+}
+
+int main(int argc, const char *argv[])
+{
+    poptContext pc;
+    int opt;
+    struct poptOption long_options[] = {
+        POPT_AUTOHELP
+        SSSD_DEBUG_OPTS
+        POPT_TABLEEND
+    };
+
+    const UnitTest tests[] = {
+        unit_test(test_become_user),
+        unit_test(test_switch_user),
+    };
+
+    /* Set debug level to invalid value so we can deside if -d 0 was used. */
+    debug_level = SSSDBG_INVALID;
+
+    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
+    while((opt = poptGetNextOpt(pc)) != -1) {
+        switch(opt) {
+        default:
+            fprintf(stderr, "\nInvalid option %s: %s\n\n",
+                    poptBadOption(pc, 0), poptStrerror(opt));
+            poptPrintUsage(pc, stderr, 0);
+            return 1;
+        }
+    }
+    poptFreeContext(pc);
+
+    DEBUG_CLI_INIT(debug_level);
+
+    /* Even though normally the tests should clean up after themselves
+     * they might not after a failed run. Remove the old db to be sure */
+    tests_set_cwd();
+
+    return run_tests(tests);
+}
-- 
1.9.3



More information about the sssd-devel mailing list