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

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