dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0068-SYSDB-Allow-storing-non-POSIX-users.patch

bb7cd1
From ea8a4436b66877bbae1a73d11917ecdb3bf72718 Mon Sep 17 00:00:00 2001
bb7cd1
From: Jakub Hrozek <jhrozek@redhat.com>
bb7cd1
Date: Wed, 22 Mar 2017 13:00:31 +0100
bb7cd1
Subject: [PATCH 68/72] SYSDB: Allow storing non-POSIX users
bb7cd1
bb7cd1
Related to:
bb7cd1
https://pagure.io/SSSD/sssd/issue/3310
bb7cd1
bb7cd1
We already do the same for groups. If the user does not have UID number
bb7cd1
set but does have the POSIX: false attribute set, then we save the user
bb7cd1
with zero UID and the non-POSIX flag.
bb7cd1
bb7cd1
Reviewed-by: Sumit Bose <sbose@redhat.com>
bb7cd1
---
bb7cd1
 src/db/sysdb_ops.c      | 32 ++++++++++++++++++++--------
bb7cd1
 src/tests/sysdb-tests.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++
bb7cd1
 2 files changed, 79 insertions(+), 9 deletions(-)
bb7cd1
bb7cd1
diff --git a/src/db/sysdb_ops.c b/src/db/sysdb_ops.c
bb7cd1
index 919f22370ff87eff2bf0bb569ca90f1ee699a61e..3cf9d903f25b9ccd506d7957c94040bdc7d658a3 100644
bb7cd1
--- a/src/db/sysdb_ops.c
bb7cd1
+++ b/src/db/sysdb_ops.c
bb7cd1
@@ -1855,6 +1855,7 @@ int sysdb_add_user(struct sss_domain_info *domain,
bb7cd1
     struct sysdb_attrs *id_attrs;
bb7cd1
     uint32_t id;
bb7cd1
     int ret;
bb7cd1
+    bool posix;
bb7cd1
 
bb7cd1
     if (domain->mpg) {
bb7cd1
         if (gid != 0) {
bb7cd1
@@ -1926,7 +1927,28 @@ int sysdb_add_user(struct sss_domain_info *domain,
bb7cd1
         /* Not fatal */
bb7cd1
     }
bb7cd1
 
bb7cd1
-    if (uid == 0) {
bb7cd1
+    if (!attrs) {
bb7cd1
+        attrs = sysdb_new_attrs(tmp_ctx);
bb7cd1
+        if (!attrs) {
bb7cd1
+            ret = ENOMEM;
bb7cd1
+            goto done;
bb7cd1
+        }
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    ret = sysdb_attrs_get_bool(attrs, SYSDB_POSIX, &posix);
bb7cd1
+    if (ret == ENOENT) {
bb7cd1
+        posix = true;
bb7cd1
+        ret = sysdb_attrs_add_bool(attrs, SYSDB_POSIX, true);
bb7cd1
+        if (ret) {
bb7cd1
+            DEBUG(SSSDBG_TRACE_LIBS, "Failed to add posix attribute.\n");
bb7cd1
+            goto done;
bb7cd1
+        }
bb7cd1
+    } else if (ret != EOK) {
bb7cd1
+        DEBUG(SSSDBG_TRACE_LIBS, "Failed to get posix attribute.\n");
bb7cd1
+        goto done;
bb7cd1
+    }
bb7cd1
+
bb7cd1
+    if (uid == 0 && posix == true) {
bb7cd1
         ret = sysdb_get_new_id(domain, &id;;
bb7cd1
         if (ret) goto done;
bb7cd1
 
bb7cd1
@@ -1948,14 +1970,6 @@ int sysdb_add_user(struct sss_domain_info *domain,
bb7cd1
         if (ret) goto done;
bb7cd1
     }
bb7cd1
 
bb7cd1
-    if (!attrs) {
bb7cd1
-        attrs = sysdb_new_attrs(tmp_ctx);
bb7cd1
-        if (!attrs) {
bb7cd1
-            ret = ENOMEM;
bb7cd1
-            goto done;
bb7cd1
-        }
bb7cd1
-    }
bb7cd1
-
bb7cd1
     if (!now) {
bb7cd1
         now = time(NULL);
bb7cd1
     }
bb7cd1
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
bb7cd1
index 1767dc3c734c6b2e5f74564debd603e2442f491b..6ec82ce4ca5c4f918bc9f3144c21f33b270ea47e 100644
bb7cd1
--- a/src/tests/sysdb-tests.c
bb7cd1
+++ b/src/tests/sysdb-tests.c
bb7cd1
@@ -1428,6 +1428,59 @@ START_TEST (test_sysdb_get_user_attr_subdomain)
bb7cd1
 }
bb7cd1
 END_TEST
bb7cd1
 
bb7cd1
+START_TEST (test_sysdb_add_nonposix_user)
bb7cd1
+{
bb7cd1
+    struct sysdb_test_ctx *test_ctx;
bb7cd1
+    const char *get_attrs[] = { SYSDB_GIDNUM,
bb7cd1
+                                SYSDB_UIDNUM,
bb7cd1
+                                SYSDB_POSIX,
bb7cd1
+                                NULL };
bb7cd1
+    struct ldb_result *res;
bb7cd1
+    const char *attrval;
bb7cd1
+    const char *username = "test_sysdb_add_nonposix_user";
bb7cd1
+    const char *fq_name;
bb7cd1
+    struct sysdb_attrs *user_attrs;
bb7cd1
+    int ret;
bb7cd1
+    uint64_t id;
bb7cd1
+
bb7cd1
+    /* Setup */
bb7cd1
+    ret = setup_sysdb_tests(&test_ctx);
bb7cd1
+    fail_if(ret != EOK, "Could not set up the test");
bb7cd1
+
bb7cd1
+    /* Create user */
bb7cd1
+    fq_name = sss_create_internal_fqname(test_ctx, username, test_ctx->domain->name);
bb7cd1
+    fail_if(fq_name == NULL, "Failed to create fq name.");
bb7cd1
+
bb7cd1
+    user_attrs = sysdb_new_attrs(test_ctx);
bb7cd1
+    fail_if(user_attrs == NULL);
bb7cd1
+
bb7cd1
+    ret = sysdb_attrs_add_bool(user_attrs, SYSDB_POSIX, false);
bb7cd1
+    fail_if(ret != EOK, "Could not add attribute");
bb7cd1
+
bb7cd1
+    ret = sysdb_add_user(test_ctx->domain, fq_name, 0, 0, "Gecos",
bb7cd1
+                         "/home/userhome", "/bin/bash", NULL, user_attrs, 0, 0);
bb7cd1
+    fail_if(ret != EOK, "sysdb_add_user failed.");
bb7cd1
+
bb7cd1
+    /* Test */
bb7cd1
+    ret = sysdb_get_user_attr(test_ctx, test_ctx->domain, fq_name,
bb7cd1
+                              get_attrs, &res;;
bb7cd1
+    fail_if(ret != EOK, "Could not get user attributes.");
bb7cd1
+    fail_if(res->count != 1, "Invalid number of entries, expected 1, got %d",
bb7cd1
+            res->count);
bb7cd1
+
bb7cd1
+    attrval = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_POSIX, NULL);
bb7cd1
+    fail_if(strcasecmp(attrval, "false") != 0, "Got bad attribute value.");
bb7cd1
+
bb7cd1
+    id = ldb_msg_find_attr_as_uint64(res->msgs[0], SYSDB_UIDNUM, 123);
bb7cd1
+    fail_unless(id == 0, "Wrong UID value");
bb7cd1
+
bb7cd1
+    id = ldb_msg_find_attr_as_uint64(res->msgs[0], SYSDB_GIDNUM, 123);
bb7cd1
+    fail_unless(id == 0, "Wrong GID value");
bb7cd1
+
bb7cd1
+    talloc_free(test_ctx);
bb7cd1
+}
bb7cd1
+END_TEST
bb7cd1
+
bb7cd1
 START_TEST (test_sysdb_add_group_member)
bb7cd1
 {
bb7cd1
     struct sysdb_test_ctx *test_ctx;
bb7cd1
@@ -7044,6 +7097,9 @@ Suite *create_sysdb_suite(void)
bb7cd1
     /* Test GetUserAttr with subdomain user */
bb7cd1
     tcase_add_test(tc_sysdb, test_sysdb_get_user_attr_subdomain);
bb7cd1
 
bb7cd1
+    /* Test adding a non-POSIX user */
bb7cd1
+    tcase_add_test(tc_sysdb, test_sysdb_add_nonposix_user);
bb7cd1
+
bb7cd1
 /* ===== NETGROUP TESTS ===== */
bb7cd1
 
bb7cd1
     /* Create a new netgroup */
bb7cd1
-- 
bb7cd1
2.9.3
bb7cd1