From 5bd39b6e13f08dbe4b1e96bcef6bf36ef1efca70 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Tue, 27 Nov 2012 12:23:43 +0100 Subject: [PATCH] Refactor out config domain identification logic Make sure we use the same logic for figure out a realm name from an sssd.conf section, as well as the type. https://bugs.freedesktop.org/show_bug.cgi?id=55041 --- service/realm-sssd-config.c | 57 +++++++++++++++++++++++++++++++++++++++++++ service/realm-sssd-config.h | 6 +++++ service/realm-sssd-provider.c | 45 +++++++++++----------------------- service/realm-sssd.c | 35 +++++++++++++------------- 4 files changed, 95 insertions(+), 48 deletions(-) diff --git a/service/realm-sssd-config.c b/service/realm-sssd-config.c index c91f899..8414ac0 100644 --- a/service/realm-sssd-config.c +++ b/service/realm-sssd-config.c @@ -185,3 +185,60 @@ realm_sssd_config_remove_domain (RealmIniConfig *config, return realm_ini_config_finish_change (config, error); } + +gboolean +realm_sssd_config_load_domain (RealmIniConfig *config, + const gchar *domain, + gchar **out_section, + gchar **id_provider, + gchar **realm_name) +{ + const gchar *field_name; + gchar *section; + gchar *type; + gchar *name; + + g_return_val_if_fail (REALM_IS_INI_CONFIG (config), FALSE); + g_return_val_if_fail (domain != NULL, FALSE); + + section = realm_sssd_config_domain_to_section (domain); + type = realm_ini_config_get (config, section, "id_provider"); + + if (g_strcmp0 (type, "ad") == 0) { + field_name = "ad_domain"; + + } else if (g_strcmp0 (type, "ipa") == 0) { + field_name = "ipa_domain"; + + } else { + g_free (section); + g_free (type); + return FALSE; + } + + name = realm_ini_config_get (config, section, field_name); + if (name == NULL) + name = realm_ini_config_get (config, section, "krb5_realm"); + if (name == NULL) + name = g_strdup (domain); + + if (realm_name) { + *realm_name = name; + name = NULL; + } + + if (id_provider) { + *id_provider = type; + type = NULL; + } + + if (out_section) { + *out_section = section; + section = NULL; + } + + g_free (type); + g_free (section); + g_free (name); + return TRUE; +} diff --git a/service/realm-sssd-config.h b/service/realm-sssd-config.h index 80efc7b..41d7aa9 100644 --- a/service/realm-sssd-config.h +++ b/service/realm-sssd-config.h @@ -42,6 +42,12 @@ gboolean realm_sssd_config_remove_domain (RealmIniConfig * const gchar *domain, GError **error); +gboolean realm_sssd_config_load_domain (RealmIniConfig *config, + const gchar *domain, + gchar **section, + gchar **id_provider, + gchar **realm_name); + G_END_DECLS #endif /* __REALM_SSSD_CONFIG_H__ */ diff --git a/service/realm-sssd-provider.c b/service/realm-sssd-provider.c index 34fcd83..9741fda 100644 --- a/service/realm-sssd-provider.c +++ b/service/realm-sssd-provider.c @@ -61,12 +61,9 @@ realm_sssd_provider_constructed (GObject *obj) { RealmSssdProvider *self; GType realm_type; - const gchar *name; gchar **domains; - gchar *section; - gchar *realm; gchar *type; - gchar *domain; + gchar *name; gint i; G_OBJECT_CLASS (realm_sssd_provider_parent_class)->constructed (obj); @@ -77,34 +74,20 @@ realm_sssd_provider_constructed (GObject *obj) domains = realm_sssd_config_get_domains (self->config); for (i = 0; domains && domains[i] != 0; i++) { - section = realm_sssd_config_domain_to_section (domains[i]); - type = realm_ini_config_get (self->config, section, "id_provider"); - realm = realm_ini_config_get (self->config, section, "krb5_realm"); - domain = NULL; - - if (g_strcmp0 (type, "ad") == 0) { - name = domain = realm_ini_config_get (self->config, section, "ad_domain"); - realm_type = REALM_TYPE_SSSD_AD; - } else if (g_strcmp0 (type, "ipa") == 0) { - name = domain = realm_ini_config_get (self->config, section, "ipa_domain"); - realm_type = REALM_TYPE_SSSD_IPA; - } else { - name = domain = NULL; - realm_type = 0; + if (realm_sssd_config_load_domain (self->config, domains[i], NULL, &type, &name)) { + if (g_strcmp0 (type, "ad") == 0) + realm_type = REALM_TYPE_SSSD_AD; + else if (g_strcmp0 (type, "ipa") == 0) + realm_type = REALM_TYPE_SSSD_IPA; + else + realm_type = 0; + + if (realm_type) + realm_provider_lookup_or_register_realm (REALM_PROVIDER (self), realm_type, name, NULL); + + g_free (name); + g_free (type); } - - if (name == NULL) - name = realm; - if (name == NULL) - name = domains[i]; - - if (realm_type) - realm_provider_lookup_or_register_realm (REALM_PROVIDER (self), realm_type, name, NULL); - - g_free (realm); - g_free (type); - g_free (domain); - g_free (section); } g_strfreev (domains); } diff --git a/service/realm-sssd.c b/service/realm-sssd.c index b3c597e..d528d72 100644 --- a/service/realm-sssd.c +++ b/service/realm-sssd.c @@ -341,36 +341,37 @@ void realm_sssd_update_properties (RealmSssd *self) { GObject *obj = G_OBJECT (self); - const gchar *name; + const gchar *my_name; + gchar *name = NULL; gchar *section = NULL; - gchar *domain = NULL; gchar **domains; - gchar *realm; gint i; g_object_freeze_notify (obj); + g_free (self->pv->section); + self->pv->section = NULL; + + g_free (self->pv->domain); + self->pv->domain = NULL; + /* Find the config domain with our realm */ domains = realm_sssd_config_get_domains (self->pv->config); - name = realm_kerberos_get_name (REALM_KERBEROS (self)); - for (i = 0; domains && domains[i]; i++) { - section = realm_sssd_config_domain_to_section (domains[i]); - realm = realm_ini_config_get (self->pv->config, section, "krb5_realm"); - if (realm && name && g_ascii_strcasecmp (realm, name) == 0) { - domain = g_strdup (domains[i]); - break; - } else { + my_name = realm_kerberos_get_name (REALM_KERBEROS (self)); + for (i = 0; self->pv->section == NULL && domains && domains[i]; i++) { + if (realm_sssd_config_load_domain (self->pv->config, domains[i], §ion, NULL, &name)) { + if (my_name && name && g_ascii_strcasecmp (my_name, name) == 0) { + self->pv->domain = g_strdup (domains[i]); + self->pv->section = section; + section = NULL; + } + g_free (section); - section = NULL; + g_free (name); } } g_strfreev (domains); - g_free (self->pv->section); - self->pv->section = section; - g_free (self->pv->domain); - self->pv->domain = domain; - /* Update all the other properties */ update_enrolled (self); update_realm_name (self); -- 1.8.1