|
|
905b4d |
From bb0970825fa8702d8b5dea94d9fc97c1041db338 Mon Sep 17 00:00:00 2001
|
|
|
905b4d |
From: Jakub Hrozek <jhrozek@redhat.com>
|
|
|
905b4d |
Date: Tue, 23 Sep 2014 16:27:23 +0200
|
|
|
905b4d |
Subject: [PATCH 23/46] UTIL: Add a function to convert id_t from a number or a
|
|
|
905b4d |
name
|
|
|
905b4d |
|
|
|
905b4d |
We need a custom function that would convert a numeric or string input
|
|
|
905b4d |
into uid_t. The function will be used to drop privileges in servers and
|
|
|
905b4d |
also in the PAC and IFP responders.
|
|
|
905b4d |
|
|
|
905b4d |
Includes a unit test to test all code that changed as well as a fix for
|
|
|
905b4d |
a misnamed attribute in the csv_to_uid_list function synopsis.
|
|
|
905b4d |
|
|
|
905b4d |
Reviewed-by: Pavel Reichl <preichl@redhat.com>
|
|
|
905b4d |
Reviewed-by: Simo Sorce <simo@redhat.com>
|
|
|
905b4d |
(cherry picked from commit 5eda23c28c582b43b2a0a165b1750f3875c0fa84)
|
|
|
905b4d |
---
|
|
|
905b4d |
src/responder/common/responder.h | 2 +-
|
|
|
905b4d |
src/responder/common/responder_common.c | 17 ++--
|
|
|
905b4d |
src/tests/cwrap/Makefile.am | 54 ++++++++++++
|
|
|
905b4d |
src/tests/cwrap/passwd | 3 +-
|
|
|
905b4d |
src/tests/cwrap/test_responder_common.c | 144 ++++++++++++++++++++++++++++++++
|
|
|
905b4d |
src/tests/cwrap/test_usertools.c | 106 +++++++++++++++++++++++
|
|
|
905b4d |
src/util/usertools.c | 44 ++++++++++
|
|
|
905b4d |
src/util/util.c | 1 +
|
|
|
905b4d |
src/util/util.h | 2 +
|
|
|
905b4d |
9 files changed, 360 insertions(+), 13 deletions(-)
|
|
|
905b4d |
create mode 100644 src/tests/cwrap/test_responder_common.c
|
|
|
905b4d |
create mode 100644 src/tests/cwrap/test_usertools.c
|
|
|
905b4d |
|
|
|
905b4d |
diff --git a/src/responder/common/responder.h b/src/responder/common/responder.h
|
|
|
905b4d |
index 3674d13f2303d0ce248f765a638aaa83d0c16cf3..97552ec472c5baa285b41cc48b51149f3ef6adb5 100644
|
|
|
905b4d |
--- a/src/responder/common/responder.h
|
|
|
905b4d |
+++ b/src/responder/common/responder.h
|
|
|
905b4d |
@@ -308,7 +308,7 @@ errno_t schedule_get_domains_task(TALLOC_CTX *mem_ctx,
|
|
|
905b4d |
struct tevent_context *ev,
|
|
|
905b4d |
struct resp_ctx *rctx);
|
|
|
905b4d |
|
|
|
905b4d |
-errno_t csv_string_to_uid_array(TALLOC_CTX *mem_ctx, const char *cvs_string,
|
|
|
905b4d |
+errno_t csv_string_to_uid_array(TALLOC_CTX *mem_ctx, const char *csv_string,
|
|
|
905b4d |
bool allow_sss_loop,
|
|
|
905b4d |
size_t *_uid_count, uid_t **_uids);
|
|
|
905b4d |
|
|
|
905b4d |
diff --git a/src/responder/common/responder_common.c b/src/responder/common/responder_common.c
|
|
|
905b4d |
index b7331ac8ab1de51839937d117968e92062af76d7..0ec2372e8d08f1002b303b5edc6897f17cee9699 100644
|
|
|
905b4d |
--- a/src/responder/common/responder_common.c
|
|
|
905b4d |
+++ b/src/responder/common/responder_common.c
|
|
|
905b4d |
@@ -159,7 +159,7 @@ errno_t check_allowed_uids(uid_t uid, size_t allowed_uids_count,
|
|
|
905b4d |
return EACCES;
|
|
|
905b4d |
}
|
|
|
905b4d |
|
|
|
905b4d |
-errno_t csv_string_to_uid_array(TALLOC_CTX *mem_ctx, const char *cvs_string,
|
|
|
905b4d |
+errno_t csv_string_to_uid_array(TALLOC_CTX *mem_ctx, const char *csv_string,
|
|
|
905b4d |
bool allow_sss_loop,
|
|
|
905b4d |
size_t *_uid_count, uid_t **_uids)
|
|
|
905b4d |
{
|
|
|
905b4d |
@@ -169,9 +169,8 @@ errno_t csv_string_to_uid_array(TALLOC_CTX *mem_ctx, const char *cvs_string,
|
|
|
905b4d |
int list_size;
|
|
|
905b4d |
uid_t *uids = NULL;
|
|
|
905b4d |
char *endptr;
|
|
|
905b4d |
- struct passwd *pwd;
|
|
|
905b4d |
|
|
|
905b4d |
- ret = split_on_separator(mem_ctx, cvs_string, ',', true, false,
|
|
|
905b4d |
+ ret = split_on_separator(mem_ctx, csv_string, ',', true, false,
|
|
|
905b4d |
&list, &list_size);
|
|
|
905b4d |
if (ret != EOK) {
|
|
|
905b4d |
DEBUG(SSSDBG_OP_FAILURE, "split_on_separator failed [%d][%s].\n",
|
|
|
905b4d |
@@ -211,17 +210,13 @@ errno_t csv_string_to_uid_array(TALLOC_CTX *mem_ctx, const char *cvs_string,
|
|
|
905b4d |
goto done;
|
|
|
905b4d |
}
|
|
|
905b4d |
|
|
|
905b4d |
- errno = 0;
|
|
|
905b4d |
- pwd = getpwnam(list[c]);
|
|
|
905b4d |
- if (pwd == NULL) {
|
|
|
905b4d |
+ ret = sss_user_by_name_or_uid(list[c], &uids[c], NULL);
|
|
|
905b4d |
+ if (ret != EOK) {
|
|
|
905b4d |
DEBUG(SSSDBG_OP_FAILURE, "List item [%s] is neither a valid "
|
|
|
905b4d |
- "UID nor a user name which cloud be "
|
|
|
905b4d |
- "resolved by getpwnam().\n", list[c]);
|
|
|
905b4d |
- ret = EINVAL;
|
|
|
905b4d |
+ "UID nor a user name which could be "
|
|
|
905b4d |
+ "resolved by getpwnam().\n", list[c]);
|
|
|
905b4d |
goto done;
|
|
|
905b4d |
}
|
|
|
905b4d |
-
|
|
|
905b4d |
- uids[c] = pwd->pw_uid;
|
|
|
905b4d |
}
|
|
|
905b4d |
}
|
|
|
905b4d |
|
|
|
905b4d |
diff --git a/src/tests/cwrap/Makefile.am b/src/tests/cwrap/Makefile.am
|
|
|
905b4d |
index d1f0e9e1b999814d8081af36f82e94f638452da4..02be67387110c0a440b647c35bba0c10e89e699d 100644
|
|
|
905b4d |
--- a/src/tests/cwrap/Makefile.am
|
|
|
905b4d |
+++ b/src/tests/cwrap/Makefile.am
|
|
|
905b4d |
@@ -45,6 +45,8 @@ if HAVE_UID_WRAPPER
|
|
|
905b4d |
check_PROGRAMS += \
|
|
|
905b4d |
become_user-tests \
|
|
|
905b4d |
server-tests \
|
|
|
905b4d |
+ usertools-tests \
|
|
|
905b4d |
+ responder_common-tests \
|
|
|
905b4d |
$(NULL)
|
|
|
905b4d |
endif # HAVE_UID_WRAPPER
|
|
|
905b4d |
endif # HAVE_NSS_WRAPPER
|
|
|
905b4d |
@@ -106,4 +108,56 @@ server_tests_LDADD = \
|
|
|
905b4d |
$(abs_top_builddir)/libsss_test_common.la \
|
|
|
905b4d |
$(NULL)
|
|
|
905b4d |
|
|
|
905b4d |
+usertools_tests_SOURCES = \
|
|
|
905b4d |
+ test_usertools.c \
|
|
|
905b4d |
+ ../../../src/util/domain_info_utils.c \
|
|
|
905b4d |
+ ../../../src/util/safe-format-string.c \
|
|
|
905b4d |
+ ../../../src/util/usertools.c \
|
|
|
905b4d |
+ ../../../src/util/strtonum.c \
|
|
|
905b4d |
+ ../../../src/util/backup_file.c \
|
|
|
905b4d |
+ ../../../src/util/atomic_io.c \
|
|
|
905b4d |
+ ../../../src/util/util.c \
|
|
|
905b4d |
+ ../../../src/util/util_errors.c \
|
|
|
905b4d |
+ ../../../src/util/sss_tc_utf8.c \
|
|
|
905b4d |
+ ../../../src/util/sss_utf8.c \
|
|
|
905b4d |
+ ../../../src/confdb/confdb.c \
|
|
|
905b4d |
+ ../../../src/db/sysdb.c \
|
|
|
905b4d |
+ ../../../src/db/sysdb_upgrade.c \
|
|
|
905b4d |
+ ../../../src/db/sysdb_autofs.c \
|
|
|
905b4d |
+ ../../../src/db/sysdb_search.c \
|
|
|
905b4d |
+ ../../../src/db/sysdb_services.c \
|
|
|
905b4d |
+ ../../../src/db/sysdb_ops.c \
|
|
|
905b4d |
+ ../../../src/db/sysdb_views.c \
|
|
|
905b4d |
+ $(NULL)
|
|
|
905b4d |
+usertools_tests_CFLAGS = \
|
|
|
905b4d |
+ $(AM_CFLAGS) \
|
|
|
905b4d |
+ $(NULL)
|
|
|
905b4d |
+usertools_tests_LDADD = \
|
|
|
905b4d |
+ $(CMOCKA_LIBS) \
|
|
|
905b4d |
+ $(UNICODE_LIBS) \
|
|
|
905b4d |
+ $(SSSD_LIBS) \
|
|
|
905b4d |
+ $(abs_top_builddir)/libsss_debug.la \
|
|
|
905b4d |
+ $(abs_top_builddir)/libsss_crypt.la \
|
|
|
905b4d |
+ $(abs_top_builddir)/libsss_test_common.la \
|
|
|
905b4d |
+ $(NULL)
|
|
|
905b4d |
+
|
|
|
905b4d |
+responder_common_tests_SOURCES =\
|
|
|
905b4d |
+ test_responder_common.c \
|
|
|
905b4d |
+ ../../../src/responder/common/responder_common.c \
|
|
|
905b4d |
+ ../../../src/responder/common/responder_packet.c \
|
|
|
905b4d |
+ ../../../src/responder/common/responder_cmd.c \
|
|
|
905b4d |
+ $(NULL)
|
|
|
905b4d |
+responder_common_tests_CFLAGS = \
|
|
|
905b4d |
+ $(AM_CFLAGS) \
|
|
|
905b4d |
+ $(NULL)
|
|
|
905b4d |
+responder_common_tests_LDADD = \
|
|
|
905b4d |
+ $(CMOCKA_LIBS) \
|
|
|
905b4d |
+ $(UNICODE_LIBS) \
|
|
|
905b4d |
+ $(SSSD_LIBS) \
|
|
|
905b4d |
+ $(abs_top_builddir)/libsss_debug.la \
|
|
|
905b4d |
+ $(abs_top_builddir)/libsss_crypt.la \
|
|
|
905b4d |
+ $(abs_top_builddir)/libsss_util.la \
|
|
|
905b4d |
+ $(abs_top_builddir)/libsss_test_common.la \
|
|
|
905b4d |
+ $(NULL)
|
|
|
905b4d |
+
|
|
|
905b4d |
tests: $(check_PROGRAMS)
|
|
|
905b4d |
diff --git a/src/tests/cwrap/passwd b/src/tests/cwrap/passwd
|
|
|
905b4d |
index aa0a97db5259172c0b4ab47c7c2346fa5c2aa88e..862ccfe03e40d43c60c56b0c50f328f494d7e6b9 100644
|
|
|
905b4d |
--- a/src/tests/cwrap/passwd
|
|
|
905b4d |
+++ b/src/tests/cwrap/passwd
|
|
|
905b4d |
@@ -1 +1,2 @@
|
|
|
905b4d |
-sssd:x:123:123:sssd unprivileged user:/:/sbin/nologin
|
|
|
905b4d |
+sssd:x:123:456:sssd unprivileged user:/:/sbin/nologin
|
|
|
905b4d |
+foobar:x:10001:10001:User for SSSD testing:/home/foobar:/bin/bash
|
|
|
905b4d |
diff --git a/src/tests/cwrap/test_responder_common.c b/src/tests/cwrap/test_responder_common.c
|
|
|
905b4d |
new file mode 100644
|
|
|
905b4d |
index 0000000000000000000000000000000000000000..23dcf753f184cdecaf39c73c6e9be0e23e6df968
|
|
|
905b4d |
--- /dev/null
|
|
|
905b4d |
+++ b/src/tests/cwrap/test_responder_common.c
|
|
|
905b4d |
@@ -0,0 +1,144 @@
|
|
|
905b4d |
+/*
|
|
|
905b4d |
+ Authors:
|
|
|
905b4d |
+ Jakub Hrozek <jhrozek@redhat.com>
|
|
|
905b4d |
+
|
|
|
905b4d |
+ Copyright (C) 2014 Red Hat
|
|
|
905b4d |
+
|
|
|
905b4d |
+ SSSD tests: User utilities
|
|
|
905b4d |
+
|
|
|
905b4d |
+ This program is free software; you can redistribute it and/or modify
|
|
|
905b4d |
+ it under the terms of the GNU General Public License as published by
|
|
|
905b4d |
+ the Free Software Foundation; either version 3 of the License, or
|
|
|
905b4d |
+ (at your option) any later version.
|
|
|
905b4d |
+
|
|
|
905b4d |
+ This program is distributed in the hope that it will be useful,
|
|
|
905b4d |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
905b4d |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
905b4d |
+ GNU General Public License for more details.
|
|
|
905b4d |
+
|
|
|
905b4d |
+ You should have received a copy of the GNU General Public License
|
|
|
905b4d |
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
905b4d |
+*/
|
|
|
905b4d |
+
|
|
|
905b4d |
+#include <sys/types.h>
|
|
|
905b4d |
+#include <sys/stat.h>
|
|
|
905b4d |
+#include <fcntl.h>
|
|
|
905b4d |
+
|
|
|
905b4d |
+#include <popt.h>
|
|
|
905b4d |
+#include "util/util.h"
|
|
|
905b4d |
+#include "responder/common/responder.h"
|
|
|
905b4d |
+#include "tests/cmocka/common_mock.h"
|
|
|
905b4d |
+
|
|
|
905b4d |
+/* Just to satisfy dependencies */
|
|
|
905b4d |
+struct cli_protocol_version *register_cli_protocol_version(void)
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ static struct cli_protocol_version responder_test_cli_protocol_version[] = {
|
|
|
905b4d |
+ {0, NULL, NULL}
|
|
|
905b4d |
+ };
|
|
|
905b4d |
+
|
|
|
905b4d |
+ return responder_test_cli_protocol_version;
|
|
|
905b4d |
+}
|
|
|
905b4d |
+
|
|
|
905b4d |
+void test_uid_csv_to_uid_list(void **state)
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ TALLOC_CTX *tmp_ctx;
|
|
|
905b4d |
+ errno_t ret;
|
|
|
905b4d |
+ size_t count;
|
|
|
905b4d |
+ uid_t *list;
|
|
|
905b4d |
+
|
|
|
905b4d |
+ tmp_ctx = talloc_new(global_talloc_context);
|
|
|
905b4d |
+ assert_non_null(tmp_ctx);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ check_leaks_push(tmp_ctx);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ ret = csv_string_to_uid_array(tmp_ctx, "1, 2, 3", false, &count, &list);
|
|
|
905b4d |
+ assert_int_equal(ret, EOK);
|
|
|
905b4d |
+ assert_int_equal(count, 3);
|
|
|
905b4d |
+ assert_int_equal(list[0], 1);
|
|
|
905b4d |
+ assert_int_equal(list[1], 2);
|
|
|
905b4d |
+ assert_int_equal(list[2], 3);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ talloc_free(list);
|
|
|
905b4d |
+ check_leaks_pop(tmp_ctx);
|
|
|
905b4d |
+ talloc_free(tmp_ctx);
|
|
|
905b4d |
+}
|
|
|
905b4d |
+
|
|
|
905b4d |
+void test_name_csv_to_uid_list(void **state)
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ TALLOC_CTX *tmp_ctx;
|
|
|
905b4d |
+ errno_t ret;
|
|
|
905b4d |
+ size_t count;
|
|
|
905b4d |
+ uid_t *list;
|
|
|
905b4d |
+
|
|
|
905b4d |
+ tmp_ctx = talloc_new(global_talloc_context);
|
|
|
905b4d |
+ assert_non_null(tmp_ctx);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ check_leaks_push(tmp_ctx);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ ret = csv_string_to_uid_array(tmp_ctx, "sssd, foobar", true, &count, &list);
|
|
|
905b4d |
+ assert_int_equal(ret, EOK);
|
|
|
905b4d |
+ assert_int_equal(count, 2);
|
|
|
905b4d |
+ assert_int_equal(list[0], 123);
|
|
|
905b4d |
+ assert_int_equal(list[1], 10001);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ talloc_free(list);
|
|
|
905b4d |
+ check_leaks_pop(tmp_ctx);
|
|
|
905b4d |
+ talloc_free(tmp_ctx);
|
|
|
905b4d |
+}
|
|
|
905b4d |
+
|
|
|
905b4d |
+void test_csv_to_uid_list_neg(void **state)
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ TALLOC_CTX *tmp_ctx;
|
|
|
905b4d |
+ errno_t ret;
|
|
|
905b4d |
+ size_t count;
|
|
|
905b4d |
+ uid_t *list = NULL;
|
|
|
905b4d |
+
|
|
|
905b4d |
+ tmp_ctx = talloc_new(global_talloc_context);
|
|
|
905b4d |
+ assert_non_null(tmp_ctx);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ check_leaks_push(tmp_ctx);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ ret = csv_string_to_uid_array(tmp_ctx, "nosuchuser", true, &count, &list);
|
|
|
905b4d |
+ assert_int_not_equal(ret, EOK);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ check_leaks_pop(tmp_ctx);
|
|
|
905b4d |
+ talloc_free(tmp_ctx);
|
|
|
905b4d |
+}
|
|
|
905b4d |
+
|
|
|
905b4d |
+int main(int argc, const char *argv[])
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ poptContext pc;
|
|
|
905b4d |
+ int opt;
|
|
|
905b4d |
+ struct poptOption long_options[] = {
|
|
|
905b4d |
+ POPT_AUTOHELP
|
|
|
905b4d |
+ SSSD_DEBUG_OPTS
|
|
|
905b4d |
+ POPT_TABLEEND
|
|
|
905b4d |
+ };
|
|
|
905b4d |
+
|
|
|
905b4d |
+ const UnitTest tests[] = {
|
|
|
905b4d |
+ unit_test(test_uid_csv_to_uid_list),
|
|
|
905b4d |
+ unit_test(test_name_csv_to_uid_list),
|
|
|
905b4d |
+ unit_test(test_csv_to_uid_list_neg),
|
|
|
905b4d |
+ };
|
|
|
905b4d |
+
|
|
|
905b4d |
+ /* Set debug level to invalid value so we can deside if -d 0 was used. */
|
|
|
905b4d |
+ debug_level = SSSDBG_INVALID;
|
|
|
905b4d |
+
|
|
|
905b4d |
+ pc = poptGetContext(argv[0], argc, argv, long_options, 0);
|
|
|
905b4d |
+ while((opt = poptGetNextOpt(pc)) != -1) {
|
|
|
905b4d |
+ switch(opt) {
|
|
|
905b4d |
+ default:
|
|
|
905b4d |
+ fprintf(stderr, "\nInvalid option %s: %s\n\n",
|
|
|
905b4d |
+ poptBadOption(pc, 0), poptStrerror(opt));
|
|
|
905b4d |
+ poptPrintUsage(pc, stderr, 0);
|
|
|
905b4d |
+ return 1;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+ poptFreeContext(pc);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ DEBUG_CLI_INIT(debug_level);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ tests_set_cwd();
|
|
|
905b4d |
+
|
|
|
905b4d |
+ return run_tests(tests);
|
|
|
905b4d |
+}
|
|
|
905b4d |
diff --git a/src/tests/cwrap/test_usertools.c b/src/tests/cwrap/test_usertools.c
|
|
|
905b4d |
new file mode 100644
|
|
|
905b4d |
index 0000000000000000000000000000000000000000..6423059456a06f0c8f8ebdd803641b7207e862fd
|
|
|
905b4d |
--- /dev/null
|
|
|
905b4d |
+++ b/src/tests/cwrap/test_usertools.c
|
|
|
905b4d |
@@ -0,0 +1,106 @@
|
|
|
905b4d |
+/*
|
|
|
905b4d |
+ Authors:
|
|
|
905b4d |
+ Jakub Hrozek <jhrozek@redhat.com>
|
|
|
905b4d |
+
|
|
|
905b4d |
+ Copyright (C) 2014 Red Hat
|
|
|
905b4d |
+
|
|
|
905b4d |
+ SSSD tests: User utilities
|
|
|
905b4d |
+
|
|
|
905b4d |
+ This program is free software; you can redistribute it and/or modify
|
|
|
905b4d |
+ it under the terms of the GNU General Public License as published by
|
|
|
905b4d |
+ the Free Software Foundation; either version 3 of the License, or
|
|
|
905b4d |
+ (at your option) any later version.
|
|
|
905b4d |
+
|
|
|
905b4d |
+ This program is distributed in the hope that it will be useful,
|
|
|
905b4d |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
905b4d |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
905b4d |
+ GNU General Public License for more details.
|
|
|
905b4d |
+
|
|
|
905b4d |
+ You should have received a copy of the GNU General Public License
|
|
|
905b4d |
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
905b4d |
+*/
|
|
|
905b4d |
+
|
|
|
905b4d |
+#include <sys/types.h>
|
|
|
905b4d |
+#include <sys/stat.h>
|
|
|
905b4d |
+#include <fcntl.h>
|
|
|
905b4d |
+
|
|
|
905b4d |
+#include <popt.h>
|
|
|
905b4d |
+#include "util/util.h"
|
|
|
905b4d |
+#include "tests/cmocka/common_mock.h"
|
|
|
905b4d |
+
|
|
|
905b4d |
+void test_get_user_num(void **state)
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ uid_t uid;
|
|
|
905b4d |
+ gid_t gid;
|
|
|
905b4d |
+ errno_t ret;
|
|
|
905b4d |
+
|
|
|
905b4d |
+ ret = sss_user_by_name_or_uid("123", &uid, &gid;;
|
|
|
905b4d |
+ assert_int_equal(ret, EOK);
|
|
|
905b4d |
+ assert_int_equal(uid, 123);
|
|
|
905b4d |
+ assert_int_equal(gid, 456);
|
|
|
905b4d |
+}
|
|
|
905b4d |
+
|
|
|
905b4d |
+void test_get_user_str(void **state)
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ uid_t uid;
|
|
|
905b4d |
+ gid_t gid;
|
|
|
905b4d |
+ errno_t ret;
|
|
|
905b4d |
+
|
|
|
905b4d |
+ ret = sss_user_by_name_or_uid("sssd", &uid, &gid;;
|
|
|
905b4d |
+ assert_int_equal(ret, EOK);
|
|
|
905b4d |
+ assert_int_equal(uid, 123);
|
|
|
905b4d |
+ assert_int_equal(gid, 456);
|
|
|
905b4d |
+}
|
|
|
905b4d |
+
|
|
|
905b4d |
+void test_get_user_nullparm(void **state)
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ uid_t uid;
|
|
|
905b4d |
+ gid_t gid;
|
|
|
905b4d |
+ errno_t ret;
|
|
|
905b4d |
+
|
|
|
905b4d |
+ ret = sss_user_by_name_or_uid("sssd", &uid, NULL);
|
|
|
905b4d |
+ assert_int_equal(ret, EOK);
|
|
|
905b4d |
+ assert_int_equal(uid, 123);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ ret = sss_user_by_name_or_uid("sssd", NULL, &gid;;
|
|
|
905b4d |
+ assert_int_equal(ret, EOK);
|
|
|
905b4d |
+ assert_int_equal(gid, 456);
|
|
|
905b4d |
+}
|
|
|
905b4d |
+
|
|
|
905b4d |
+int main(int argc, const char *argv[])
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ poptContext pc;
|
|
|
905b4d |
+ int opt;
|
|
|
905b4d |
+ struct poptOption long_options[] = {
|
|
|
905b4d |
+ POPT_AUTOHELP
|
|
|
905b4d |
+ SSSD_DEBUG_OPTS
|
|
|
905b4d |
+ POPT_TABLEEND
|
|
|
905b4d |
+ };
|
|
|
905b4d |
+
|
|
|
905b4d |
+ const UnitTest tests[] = {
|
|
|
905b4d |
+ unit_test(test_get_user_num),
|
|
|
905b4d |
+ unit_test(test_get_user_str),
|
|
|
905b4d |
+ unit_test(test_get_user_nullparm),
|
|
|
905b4d |
+ };
|
|
|
905b4d |
+
|
|
|
905b4d |
+ /* Set debug level to invalid value so we can deside if -d 0 was used. */
|
|
|
905b4d |
+ debug_level = SSSDBG_INVALID;
|
|
|
905b4d |
+
|
|
|
905b4d |
+ pc = poptGetContext(argv[0], argc, argv, long_options, 0);
|
|
|
905b4d |
+ while((opt = poptGetNextOpt(pc)) != -1) {
|
|
|
905b4d |
+ switch(opt) {
|
|
|
905b4d |
+ default:
|
|
|
905b4d |
+ fprintf(stderr, "\nInvalid option %s: %s\n\n",
|
|
|
905b4d |
+ poptBadOption(pc, 0), poptStrerror(opt));
|
|
|
905b4d |
+ poptPrintUsage(pc, stderr, 0);
|
|
|
905b4d |
+ return 1;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+ poptFreeContext(pc);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ DEBUG_CLI_INIT(debug_level);
|
|
|
905b4d |
+
|
|
|
905b4d |
+ tests_set_cwd();
|
|
|
905b4d |
+
|
|
|
905b4d |
+ return run_tests(tests);
|
|
|
905b4d |
+}
|
|
|
905b4d |
diff --git a/src/util/usertools.c b/src/util/usertools.c
|
|
|
905b4d |
index 809b42d67c7b1cdfa0729c3a7e835fab37297596..a0b914e2fe8f65a71015944e63cb2d2813345d84 100644
|
|
|
905b4d |
--- a/src/util/usertools.c
|
|
|
905b4d |
+++ b/src/util/usertools.c
|
|
|
905b4d |
@@ -23,8 +23,11 @@
|
|
|
905b4d |
#include <pcre.h>
|
|
|
905b4d |
#include <errno.h>
|
|
|
905b4d |
#include <talloc.h>
|
|
|
905b4d |
+#include <pwd.h>
|
|
|
905b4d |
+#include <grp.h>
|
|
|
905b4d |
|
|
|
905b4d |
#include "confdb/confdb.h"
|
|
|
905b4d |
+#include "util/strtonum.h"
|
|
|
905b4d |
#include "util/util.h"
|
|
|
905b4d |
#include "util/safe-format-string.h"
|
|
|
905b4d |
#include "responder/common/responder.h"
|
|
|
905b4d |
@@ -659,3 +662,44 @@ sss_get_domain_name(TALLOC_CTX *mem_ctx,
|
|
|
905b4d |
|
|
|
905b4d |
return user_name;
|
|
|
905b4d |
}
|
|
|
905b4d |
+
|
|
|
905b4d |
+errno_t sss_user_by_name_or_uid(const char *input, uid_t *_uid, gid_t *_gid)
|
|
|
905b4d |
+{
|
|
|
905b4d |
+ uid_t uid;
|
|
|
905b4d |
+ errno_t ret;
|
|
|
905b4d |
+ char *endptr;
|
|
|
905b4d |
+ struct passwd *pwd;
|
|
|
905b4d |
+
|
|
|
905b4d |
+ /* Try if it's an ID first */
|
|
|
905b4d |
+ errno = 0;
|
|
|
905b4d |
+ uid = strtouint32(input, &endptr, 10);
|
|
|
905b4d |
+ if (errno != 0 || *endptr != '\0') {
|
|
|
905b4d |
+ ret = errno;
|
|
|
905b4d |
+ if (ret == ERANGE) {
|
|
|
905b4d |
+ DEBUG(SSSDBG_OP_FAILURE,
|
|
|
905b4d |
+ "UID [%s] is out of range.\n", input);
|
|
|
905b4d |
+ return ret;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ /* Nope, maybe a username? */
|
|
|
905b4d |
+ pwd = getpwnam(input);
|
|
|
905b4d |
+ } else {
|
|
|
905b4d |
+ pwd = getpwuid(uid);
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ if (pwd == NULL) {
|
|
|
905b4d |
+ DEBUG(SSSDBG_OP_FAILURE,
|
|
|
905b4d |
+ "[%s] is neither a valid UID nor a user name which could be "
|
|
|
905b4d |
+ "resolved by getpwnam().\n", input);
|
|
|
905b4d |
+ return EINVAL;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ if (_uid) {
|
|
|
905b4d |
+ *_uid = pwd->pw_uid;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+
|
|
|
905b4d |
+ if (_gid) {
|
|
|
905b4d |
+ *_gid = pwd->pw_gid;
|
|
|
905b4d |
+ }
|
|
|
905b4d |
+ return EOK;
|
|
|
905b4d |
+}
|
|
|
905b4d |
diff --git a/src/util/util.c b/src/util/util.c
|
|
|
905b4d |
index 7f80771ecd9868feaf43e34cbd61e44dd8ae5f3a..d78d37d975e6591bca6ac3f2fa36b5b9f4659a29 100644
|
|
|
905b4d |
--- a/src/util/util.c
|
|
|
905b4d |
+++ b/src/util/util.c
|
|
|
905b4d |
@@ -21,6 +21,7 @@
|
|
|
905b4d |
#include <ctype.h>
|
|
|
905b4d |
#include <netdb.h>
|
|
|
905b4d |
#include <poll.h>
|
|
|
905b4d |
+#include <sys/types.h>
|
|
|
905b4d |
#include <sys/socket.h>
|
|
|
905b4d |
#include <arpa/inet.h>
|
|
|
905b4d |
#include <talloc.h>
|
|
|
905b4d |
diff --git a/src/util/util.h b/src/util/util.h
|
|
|
905b4d |
index df83aac7d53ccadb806e8a1be90f0e45abb829ae..69074c93c1640a1e4a7e590b7f9feb6cc04804a4 100644
|
|
|
905b4d |
--- a/src/util/util.h
|
|
|
905b4d |
+++ b/src/util/util.h
|
|
|
905b4d |
@@ -404,6 +404,8 @@ bool check_ipv6_addr(struct in6_addr *addr, uint8_t check);
|
|
|
905b4d |
|
|
|
905b4d |
const char * const * get_known_services(void);
|
|
|
905b4d |
|
|
|
905b4d |
+errno_t sss_user_by_name_or_uid(const char *input, uid_t *_uid, gid_t *_gid);
|
|
|
905b4d |
+
|
|
|
905b4d |
int split_on_separator(TALLOC_CTX *mem_ctx, const char *str,
|
|
|
905b4d |
const char sep, bool trim, bool skip_empty,
|
|
|
905b4d |
char ***_list, int *size);
|
|
|
905b4d |
--
|
|
|
905b4d |
1.9.3
|
|
|
905b4d |
|