From af2fbb523f1963978f6a4e86340bdcda8590d12e Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Mon, 11 Feb 2013 12:48:09 +0100 Subject: [PATCH] Add a utility function for updating sssd.conf domains https://bugs.freedesktop.org/show_bug.cgi?id=55041 --- service/realm-sssd-config.c | 88 +++++++++++++++++++++++++++++++++++---------- service/realm-sssd-config.h | 5 +++ tests/test-sssd-config.c | 55 ++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 19 deletions(-) diff --git a/service/realm-sssd-config.c b/service/realm-sssd-config.c index 8414ac0..68bd212 100644 --- a/service/realm-sssd-config.c +++ b/service/realm-sssd-config.c @@ -97,18 +97,47 @@ realm_sssd_config_have_domain (RealmIniConfig *config, return have; } +static gboolean +update_domain (RealmIniConfig *config, + const char *section, + va_list va, + GError **error) +{ + GHashTable *parameters; + const gchar *name; + const gchar *value; + gchar *shell; + + /* Always make sure this is set */ + shell = realm_ini_config_get (config, "nss", "default_shell"); + if (shell == NULL) { + realm_ini_config_set (config, "nss", "default_shell", + realm_settings_string ("users", "default-shell")); + } + g_free (shell); + + parameters = g_hash_table_new (g_str_hash, g_str_equal); + while ((name = va_arg (va, const gchar *)) != NULL) { + value = va_arg (va, const gchar *); + g_hash_table_insert (parameters, (gpointer)name, (gpointer)value); + } + + realm_ini_config_set_all (config, section, parameters); + g_hash_table_unref (parameters); + + return realm_ini_config_finish_change (config, error); + +} + gboolean realm_sssd_config_add_domain (RealmIniConfig *config, const gchar *domain, GError **error, ...) { - GHashTable *parameters; - const gchar *name; - const gchar *value; const gchar *domains[2]; + gboolean ret; gchar *section; - gchar *shell; va_list va; g_return_val_if_fail (REALM_IS_INI_CONFIG (config), FALSE); @@ -133,31 +162,52 @@ realm_sssd_config_add_domain (RealmIniConfig *config, realm_ini_config_set (config, "sssd", "config_file_version", "2"); } - /* Always make sure this is set */ - shell = realm_ini_config_get (config, "nss", "default_shell"); - if (shell == NULL) { - realm_ini_config_set (config, "nss", "default_shell", - realm_settings_string ("users", "default-shell")); - } - g_free (shell); - domains[0] = domain; domains[1] = NULL; realm_ini_config_set_list_diff (config, "sssd", "domains", ", ", domains, NULL); - parameters = g_hash_table_new (g_str_hash, g_str_equal); va_start (va, error); - while ((name = va_arg (va, const gchar *)) != NULL) { - value = va_arg (va, const gchar *); - g_hash_table_insert (parameters, (gpointer)name, (gpointer)value); + ret = update_domain (config, section, va, error); + va_end (va); + + g_free (section); + + return ret; +} + +gboolean +realm_sssd_config_update_domain (RealmIniConfig *config, + const gchar *domain, + GError **error, + ...) +{ + gchar *section; + gboolean ret; + va_list va; + + g_return_val_if_fail (REALM_IS_INI_CONFIG (config), FALSE); + g_return_val_if_fail (domain != NULL, FALSE); + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + if (!realm_ini_config_begin_change (config, error)) + return FALSE; + + section = realm_sssd_config_domain_to_section (domain); + if (!realm_ini_config_have_section (config, section)) { + realm_ini_config_abort_change (config); + g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT, + _("Don't have domain %s in sssd.conf config file"), domain); + g_free (section); + return FALSE; } + + va_start (va, error); + ret = update_domain (config, section, va, error); va_end (va); - realm_ini_config_set_all (config, section, parameters); - g_hash_table_unref (parameters); g_free (section); - return realm_ini_config_finish_change (config, error); + return ret; } gboolean diff --git a/service/realm-sssd-config.h b/service/realm-sssd-config.h index 41d7aa9..9d572b0 100644 --- a/service/realm-sssd-config.h +++ b/service/realm-sssd-config.h @@ -38,6 +38,11 @@ gboolean realm_sssd_config_add_domain (RealmIniConfig * GError **error, ...) G_GNUC_NULL_TERMINATED; +gboolean realm_sssd_config_update_domain (RealmIniConfig *config, + const gchar *domain, + GError **error, + ...); + gboolean realm_sssd_config_remove_domain (RealmIniConfig *config, const gchar *domain, GError **error); diff --git a/tests/test-sssd-config.c b/tests/test-sssd-config.c index 44fa1fc..f907c66 100644 --- a/tests/test-sssd-config.c +++ b/tests/test-sssd-config.c @@ -164,6 +164,59 @@ test_add_domain_only (Test *test, } static void +test_update_domain (Test *test, + gconstpointer unused) +{ + const gchar *data = "[domain/one]\nval=1\n[sssd]\ndomains=one"; + const gchar *check = "[domain/one]\nval=1\nuno = 1\neins = one\n[sssd]\ndomains=one\n\n[nss]\ndefault_shell = /bin/bash\n"; + GError *error = NULL; + gchar *output; + gboolean ret; + + realm_ini_config_read_string (test->config, data); + ret = realm_ini_config_write_file (test->config, "/tmp/test-sssd.conf", &error); + g_assert_no_error (error); + g_assert (ret == TRUE); + + g_assert (realm_sssd_config_have_domain (test->config, "one") == TRUE); + ret = realm_sssd_config_update_domain (test->config, "one", &error, + "uno", "1", + "eins", "one", + NULL); + g_assert_no_error (error); + g_assert (ret == TRUE); + + ret = g_file_get_contents ("/tmp/test-sssd.conf", &output, NULL, &error); + g_assert_no_error (error); + g_assert (ret == TRUE); + + g_assert_cmpstr (check, ==, output); + g_free (output); +} + +static void +test_update_domain_missing (Test *test, + gconstpointer unused) +{ + const gchar *data = "[domain/one]\nval=1\n[sssd]\ndomains=one"; + GError *error = NULL; + gboolean ret; + + realm_ini_config_read_string (test->config, data); + ret = realm_ini_config_write_file (test->config, "/tmp/test-sssd.conf", &error); + g_assert_no_error (error); + g_assert (ret == TRUE); + + g_assert (realm_sssd_config_have_domain (test->config, "another") == FALSE); + ret = realm_sssd_config_update_domain (test->config, "another", &error, + "uno", "1", + "eins", "one", + NULL); + g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT); + g_assert (ret == FALSE); +} + +static void test_remove_domain (Test *test, gconstpointer unused) { @@ -296,6 +349,8 @@ main (int argc, g_test_add ("/realmd/sssd-config/add-domain", Test, NULL, setup, test_add_domain, teardown); g_test_add ("/realmd/sssd-config/add-domain-already", Test, NULL, setup, test_add_domain_already, teardown); g_test_add ("/realmd/sssd-config/add-domain-only", Test, NULL, setup, test_add_domain_only, teardown); + g_test_add ("/realmd/sssd-config/update-domain", Test, NULL, setup, test_update_domain, teardown); + g_test_add ("/realmd/sssd-config/update-domain-missing", Test, NULL, setup, test_update_domain_missing, teardown); g_test_add ("/realmd/sssd-config/remove-domain", Test, NULL, setup, test_remove_domain, teardown); g_test_add ("/realmd/sssd-config/remove-domain-not-exist", Test, NULL, setup, test_remove_domain_not_exist, teardown); g_test_add ("/realmd/sssd-config/remove-domain-only", Test, NULL, setup, test_remove_domain_only, teardown); -- 1.8.1