From 4881df48e30fa333993ce4d12819abc09f940ecb Mon Sep 17 00:00:00 2001
From: Jan Zeleny <jzeleny@redhat.com>
Date: Tue, 7 Feb 2012 07:01:20 -0500
Subject: [PATCH] IPA hosts refactoring

---
 src/db/sysdb.h                       |    4 +
 src/providers/ipa/ipa_access.c       |   24 ++------
 src/providers/ipa/ipa_access.h       |    1 -
 src/providers/ipa/ipa_common.c       |   48 +++++++++++-----
 src/providers/ipa/ipa_common.h       |   13 ++++
 src/providers/ipa/ipa_hbac_hosts.c   |    6 +-
 src/providers/ipa/ipa_hbac_private.h |    6 --
 src/providers/ipa/ipa_hostid.c       |   10 +---
 src/providers/ipa/ipa_hosts.c        |  101 +++++++++++++++-------------------
 src/providers/ipa/ipa_hosts.h        |    6 +-
 src/providers/ipa/ipa_session.c      |   25 +-------
 src/providers/ldap/sdap.h            |    3 +-
 12 files changed, 113 insertions(+), 134 deletions(-)

diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index e9a89606b37e02560be8c01c813043c29d1e9e64..a9d4b0f8fad9fa47934fb19416da46f58e7bb59e 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -45,6 +45,7 @@
 #define SYSDB_GROUP_CLASS "group"
 #define SYSDB_NETGROUP_CLASS "netgroup"
 #define SYSDB_HOST_CLASS "host"
+#define SYSDB_HOSTGROUP_CLASS "hostgroup"
 #define SYSDB_SELINUX_USERMAP_CLASS "selinuxusermap"
 #define SYSDB_SELINUX_CLASS "selinux"
 
@@ -95,6 +96,9 @@
 #define SYSDB_NETGROUP_MEMBER "memberNisNetgroup"
 #define SYSDB_DESCRIPTION   "description"
 
+#define SYSDB_FQDN "fqdn"
+#define SYSDB_SERVERHOSTNAME "serverHostname"
+
 #define SYSDB_SELINUX_SEEALSO "seeAlso"
 #define SYSDB_SELINUX_USER "selinuxUser"
 #define SYSDB_SELINUX_ENABLED "enabled"
diff --git a/src/providers/ipa/ipa_access.c b/src/providers/ipa/ipa_access.c
index 1eed86c333a3242e253ac4407d1dcc45a0d5783a..5fc348906d8c7df98b4976807509de26615c7f99 100644
--- a/src/providers/ipa/ipa_access.c
+++ b/src/providers/ipa/ipa_access.c
@@ -272,20 +272,6 @@ static int hbac_get_host_info_step(struct hbac_ctx *hbac_ctx)
     const char *hostname;
     struct tevent_req *req;
 
-    hbac_ctx->host_attrs = talloc_array(hbac_ctx, const char *, 8);
-    if (hbac_ctx->host_attrs == NULL) {
-        DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to allocate host attribute list.\n"));
-        return ENOMEM;
-    }
-    hbac_ctx->host_attrs[0] = "objectClass";
-    hbac_ctx->host_attrs[1] = IPA_HOST_SERVERHOSTNAME;
-    hbac_ctx->host_attrs[2] = IPA_HOST_FQDN;
-    hbac_ctx->host_attrs[3] = IPA_UNIQUE_ID;
-    hbac_ctx->host_attrs[4] = IPA_MEMBER;
-    hbac_ctx->host_attrs[5] = IPA_MEMBEROF;
-    hbac_ctx->host_attrs[6] = IPA_CN;
-    hbac_ctx->host_attrs[7] = NULL;
-
     if (dp_opt_get_bool(hbac_ctx->ipa_options, IPA_HBAC_SUPPORT_SRCHOST)) {
         /* Support srchost
          * -> we don't want any particular host,
@@ -301,7 +287,9 @@ static int hbac_get_host_info_step(struct hbac_ctx *hbac_ctx)
                              hbac_ctx_sysdb(hbac_ctx),
                              sdap_id_op_handle(hbac_ctx->sdap_op),
                              hbac_ctx_sdap_id_ctx(hbac_ctx)->opts,
-                             hostname, hbac_ctx->host_attrs, NULL, 0, true,
+                             hostname,
+                             hbac_ctx->sdap_ctx->opts->host_map,
+                             hbac_ctx->sdap_ctx->opts->hostgroup_map,
                              hbac_ctx->access_ctx->host_search_bases);
     if (req == NULL) {
         DEBUG(1, ("Could not get host info\n"));
@@ -375,7 +363,7 @@ static void hbac_get_rule_info_step(struct tevent_req *req)
 
     for (i = 0; i < hbac_ctx->host_count; i++) {
         ret = sysdb_attrs_get_string(hbac_ctx->hosts[i],
-                                     IPA_HOST_FQDN,
+                                     SYSDB_FQDN,
                                      &hostname);
         if (ret != EOK) {
             DEBUG(1, ("Could not locate IPA host\n"));
@@ -480,9 +468,9 @@ static void hbac_sysdb_save(struct tevent_req *req)
 
     /* Save the hosts */
     ret = ipa_hbac_sysdb_save(sysdb, domain,
-                              HBAC_HOSTS_SUBDIR, IPA_HOST_FQDN,
+                              HBAC_HOSTS_SUBDIR, SYSDB_FQDN,
                               hbac_ctx->host_count, hbac_ctx->hosts,
-                              HBAC_HOSTGROUPS_SUBDIR, IPA_CN,
+                              HBAC_HOSTGROUPS_SUBDIR, SYSDB_NAME,
                               hbac_ctx->hostgroup_count,
                               hbac_ctx->hostgroups);
     if (ret != EOK) {
diff --git a/src/providers/ipa/ipa_access.h b/src/providers/ipa/ipa_access.h
index ddfb2cf562054fc0d1d9c6200b4e8ffff055a015..9daf9592588e1c37589aecb3af1302a08212e297 100644
--- a/src/providers/ipa/ipa_access.h
+++ b/src/providers/ipa/ipa_access.h
@@ -54,7 +54,6 @@ struct hbac_ctx {
     struct sdap_search_base **search_bases;
 
     /* Hosts */
-    const char **host_attrs;
     size_t host_count;
     struct sysdb_attrs **hosts;
     size_t hostgroup_count;
diff --git a/src/providers/ipa/ipa_common.c b/src/providers/ipa/ipa_common.c
index 3620c35de7bb3b02f3c3fd454adaba3b9da02253..66e22034ec4c0672f4ab8b8093a99efb4e709da7 100644
--- a/src/providers/ipa/ipa_common.c
+++ b/src/providers/ipa/ipa_common.c
@@ -181,22 +181,33 @@ struct sdap_attr_map ipa_netgroup_map[] = {
 
 struct sdap_attr_map ipa_host_map[] = {
     { "ipa_host_object_class", "ipaHost", SYSDB_HOST_CLASS, NULL },
-    { "ipa_host_fqdn", "fqdn", SYSDB_NAME, NULL },
-    { "ipa_host_member_of", "memberOf", SYSDB_MEMBEROF, NULL },
-    { "ipa_host_ssh_public_key", "ipaSshPubKey", SYSDB_SSH_PUBKEY, NULL }
+    { "ipa_host_name", "cn", SYSDB_NAME, NULL },
+    { "ipa_host_fqdn", "fqdn", SYSDB_FQDN, NULL },
+    { "ipa_host_serverhostname", "serverHostname", SYSDB_SERVERHOSTNAME, NULL },
+    { "ipa_host_member_of", "memberOf", SYSDB_ORIG_MEMBEROF, NULL },
+    { "ipa_host_ssh_public_key", "ipaSshPubKey", SYSDB_SSH_PUBKEY, NULL },
+    { "ipa_hostgroup_uuid", "ipaUniqueID", SYSDB_UUID, NULL}
+};
+
+static struct sdap_attr_map ipa_hostgroup_map[] = {
+    { "ipa_hostgroup_objectclass", "ipaHostgroup", SYSDB_HOSTGROUP_CLASS, NULL},
+    { "ipa_hostgroup_name", "cn", SYSDB_NAME, NULL},
+    { "ipa_hostgroup_member", "member", SYSDB_MEMBER, NULL},
+    { "ipa_hostgroup_memberof", "memberOf", SYSDB_ORIG_MEMBEROF, NULL},
+    { "ipa_hostgroup_uuid", "ipaUniqueID", SYSDB_UUID, NULL}
 };
 
 static struct sdap_attr_map ipa_selinux_user_map[] = {
-    {"ipa_selinux_usermap_object_class", "ipaselinuxusermap", SYSDB_SELINUX_USERMAP_CLASS, NULL},
-    {"ipa_selinux_usermap_name", "cn", SYSDB_NAME, NULL},
-    {"ipa_selinux_usermap_member_user", "memberUser", SYSDB_ORIG_MEMBER_USER, NULL},
-    {"ipa_selinux_usermap_member_host", "memberHost", SYSDB_ORIG_MEMBER_HOST, NULL},
-    {"ipa_selinux_usermap_see_also", "seeAlso", SYSDB_SELINUX_SEEALSO, NULL},
-    {"ipa_selinux_usermap_selinux_user", "ipaSELinuxUser", SYSDB_SELINUX_USER, NULL},
-    {"ipa_selinux_usermap_enabled", "ipaEnabledFlag", SYSDB_SELINUX_ENABLED, NULL},
-    {"ipa_selinux_usermap_user_category", "userCategory", SYSDB_USER_CATEGORY, NULL},
-    {"ipa_selinux_usermap_host_category", "hostCategory", SYSDB_HOST_CATEGORY, NULL},
-    {"ipa_selinux_usermap_uuid", "ipaUniqueID", SYSDB_UUID, NULL}
+    { "ipa_selinux_usermap_object_class", "ipaselinuxusermap", SYSDB_SELINUX_USERMAP_CLASS, NULL},
+    { "ipa_selinux_usermap_name", "cn", SYSDB_NAME, NULL},
+    { "ipa_selinux_usermap_member_user", "memberUser", SYSDB_ORIG_MEMBER_USER, NULL},
+    { "ipa_selinux_usermap_member_host", "memberHost", SYSDB_ORIG_MEMBER_HOST, NULL},
+    { "ipa_selinux_usermap_see_also", "seeAlso", SYSDB_SELINUX_SEEALSO, NULL},
+    { "ipa_selinux_usermap_selinux_user", "ipaSELinuxUser", SYSDB_SELINUX_USER, NULL},
+    { "ipa_selinux_usermap_enabled", "ipaEnabledFlag", SYSDB_SELINUX_ENABLED, NULL},
+    { "ipa_selinux_usermap_user_category", "userCategory", SYSDB_USER_CATEGORY, NULL},
+    { "ipa_selinux_usermap_host_category", "hostCategory", SYSDB_HOST_CATEGORY, NULL},
+    { "ipa_selinux_usermap_uuid", "ipaUniqueID", SYSDB_UUID, NULL}
 };
 
 struct dp_option ipa_def_krb5_opts[] = {
@@ -720,8 +731,17 @@ int ipa_get_id_options(struct ipa_options *ipa_opts,
 
     ret = sdap_get_map(ipa_opts->id,
                        cdb, conf_path,
+                       ipa_hostgroup_map,
+                       IPA_OPTS_HOSTGROUP,
+                       &ipa_opts->id->hostgroup_map);
+    if (ret != EOK) {
+        goto done;
+    }
+
+    ret = sdap_get_map(ipa_opts->id,
+                       cdb, conf_path,
                        ipa_service_map,
-                       IPA_OPTS_HOST,
+                       SDAP_OPTS_SERVICES,
                        &ipa_opts->id->service_map);
     if (ret != EOK) {
         goto done;
diff --git a/src/providers/ipa/ipa_common.h b/src/providers/ipa/ipa_common.h
index 2d0e0e1d46d12ca264fa29fcea6442a5dc46c99b..5b7a84ee67b51132b96bb0b65289ebad3033b991 100644
--- a/src/providers/ipa/ipa_common.h
+++ b/src/providers/ipa/ipa_common.h
@@ -77,13 +77,26 @@ enum ipa_netgroup_attrs {
 
 enum ipa_host_attrs {
     IPA_OC_HOST = 0,
+    IPA_AT_HOST_NAME,
     IPA_AT_HOST_FQDN,
+    IPA_AT_HOST_SERVERHOSTNAME,
     IPA_AT_HOST_MEMBER_OF,
     IPA_AT_HOST_SSH_PUBLIC_KEY,
+    IPA_AT_HOST_UUID,
 
     IPA_OPTS_HOST /* attrs counter */
 };
 
+enum ipa_hostgroup_attrs {
+    IPA_OC_HOSTGROUP = 0,
+    IPA_AT_HOSTGROUP_NAME,
+    IPA_AT_HOSTGROUP_MEMBER,
+    IPA_AT_HOSTGROUP_MEMBER_OF,
+    IPA_AT_HOSTGROUP_UUID,
+
+    IPA_OPTS_HOSTGROUP /* attrs counter */
+};
+
 enum ipa_selinux_usermap_attrs {
     IPA_OC_SELINUX_USERMAP = 0,
     IPA_AT_SELINUX_USERMAP_NAME,
diff --git a/src/providers/ipa/ipa_hbac_hosts.c b/src/providers/ipa/ipa_hbac_hosts.c
index 1626f178a319e43ceb323a2d67deac13bf4be2f6..23f7816be0356f1cd89467225d6223f3b1d0d69a 100644
--- a/src/providers/ipa/ipa_hbac_hosts.c
+++ b/src/providers/ipa/ipa_hbac_hosts.c
@@ -40,7 +40,7 @@ static errno_t hbac_host_attrs_to_rule(TALLOC_CTX *mem_ctx,
     errno_t ret;
     TALLOC_CTX *tmp_ctx;
     struct hbac_rule_element *new_hosts;
-    const char *attrs[] = { IPA_HOST_FQDN, IPA_CN, NULL };
+    const char *attrs[] = { SYSDB_FQDN, SYSDB_NAME, NULL };
     struct ldb_message_element *el;
     size_t num_hosts = 0;
     size_t num_hostgroups = 0;
@@ -131,7 +131,7 @@ static errno_t hbac_host_attrs_to_rule(TALLOC_CTX *mem_ctx,
 
             /* Original DN matched a single host. Get the hostname */
             name = ldb_msg_find_attr_as_string(msgs[0],
-                                               IPA_HOST_FQDN,
+                                               SYSDB_FQDN,
                                                NULL);
             if (name == NULL) {
                 DEBUG(1, ("FQDN is missing!\n"));
@@ -167,7 +167,7 @@ static errno_t hbac_host_attrs_to_rule(TALLOC_CTX *mem_ctx,
                 }
 
                 /* Original DN matched a single group. Get the groupname */
-                name = ldb_msg_find_attr_as_string(msgs[0], IPA_CN, NULL);
+                name = ldb_msg_find_attr_as_string(msgs[0], SYSDB_NAME, NULL);
                 if (name == NULL) {
                     DEBUG(1, ("Hostgroup name is missing!\n"));
                     ret = EFAULT;
diff --git a/src/providers/ipa/ipa_hbac_private.h b/src/providers/ipa/ipa_hbac_private.h
index acf5671578dd6611b600bcb549cc0e3e1e86251e..bb1ea4ec1eb11c68ad2bfb6ec1939ed34f7fb1c3 100644
--- a/src/providers/ipa/ipa_hbac_private.h
+++ b/src/providers/ipa/ipa_hbac_private.h
@@ -28,14 +28,9 @@
 
 #define IPA_HBAC_RULE "ipaHBACRule"
 
-#define IPA_HOST "ipaHost"
-#define IPA_HOSTGROUP "ipaHostGroup"
-
 #define IPA_HBAC_SERVICE "ipaHBACService"
 #define IPA_HBAC_SERVICE_GROUP "ipaHBACServiceGroup"
 
-#define IPA_HOST_SERVERHOSTNAME "serverHostName"
-#define IPA_HOST_FQDN "fqdn"
 #define IPA_UNIQUE_ID "ipauniqueid"
 
 #define IPA_MEMBER "member"
@@ -61,7 +56,6 @@
 #define IPA_SERVICE_CATEGORY "serviceCategory"
 #define IPA_TRUE_VALUE "TRUE"
 
-#define IPA_HOST_BASE_TMPL "cn=computers,cn=accounts,%s"
 #define IPA_HBAC_BASE_TMPL "cn=hbac,%s"
 #define IPA_SERVICES_BASE_TMPL "cn=hbacservices,cn=accounts,%s"
 
diff --git a/src/providers/ipa/ipa_hostid.c b/src/providers/ipa/ipa_hostid.c
index 873cc529668d45c8cd76cc12cfbe8f6cfcf6b37e..b610749a8692cd05a6378f6dd4f82bfcaf9945ec 100644
--- a/src/providers/ipa/ipa_hostid.c
+++ b/src/providers/ipa/ipa_hostid.c
@@ -33,7 +33,6 @@ struct hosts_get_state {
     struct sysdb_ctx *sysdb;
     struct sss_domain_info *domain;
     const char *name;
-    const char **attrs;
 
     size_t count;
     struct sysdb_attrs **hosts;
@@ -176,11 +175,6 @@ hosts_get_send(TALLOC_CTX *memctx,
     state->domain = ctx->be->domain;
     state->name = name;
 
-    /* TODO: handle attrs_type */
-    ret = build_attrs_from_map(state, ctx->opts->host_map,
-                               IPA_OPTS_HOST, &state->attrs);
-    if (ret != EOK) goto fail;
-
     ret = hosts_get_retry(req);
     if (ret != EOK) {
         goto fail;
@@ -234,8 +228,8 @@ hosts_get_connect_done(struct tevent_req *subreq)
     subreq = ipa_host_info_send(state, state->ev, state->sysdb,
                                 sdap_id_op_handle(state->op),
                                 ctx->opts, state->name,
-                                state->attrs, ctx->opts->host_map,
-                                IPA_OPTS_HOST, false,
+                                ctx->opts->host_map,
+                                ctx->opts->hostgroup_map,
                                 state->ctx->host_search_bases);
     if (!subreq) {
         tevent_req_error(req, ENOMEM);
diff --git a/src/providers/ipa/ipa_hosts.c b/src/providers/ipa/ipa_hosts.c
index e939ab7f17ea837de12889543e192fac1b659c23..67e6fa2a279c24c3947a0f27702ea1f28a248a9a 100644
--- a/src/providers/ipa/ipa_hosts.c
+++ b/src/providers/ipa/ipa_hosts.c
@@ -24,8 +24,7 @@
 #include "db/sysdb.h"
 #include "providers/ldap/sdap_async.h"
 #include "providers/ipa/ipa_hosts.h"
-/* FIXME: this is temporary, use proper map instead */
-#include "providers/ipa/ipa_hbac_private.h"
+#include "providers/ipa/ipa_common.h"
 
 struct ipa_host_state {
     struct tevent_context *ev;
@@ -33,8 +32,8 @@ struct ipa_host_state {
     struct sdap_handle *sh;
     struct sdap_options *opts;
     const char **attrs;
-    struct sdap_attr_map *map;
-    int map_num_attrs;
+    struct sdap_attr_map *host_map;
+    struct sdap_attr_map *hostgroup_map;
 
     struct sdap_search_base **search_bases;
     int search_base_iter;
@@ -48,19 +47,9 @@ struct ipa_host_state {
     size_t host_count;
     struct sysdb_attrs **hosts;
 
-    bool fetch_hostgroups;
     size_t hostgroup_count;
     struct sysdb_attrs **hostgroups;
-    struct sdap_attr_map_info *hostgroup_map;
-};
-
-#define HOSTGROUP_MAP_ATTRS_COUNT 5
-static struct sdap_attr_map hostgroup_map[] = {
-    {"objectclass", "ipahostgroup", "hostgroup", NULL},
-    {"name_attr", IPA_CN, IPA_CN, NULL},
-    {"member", IPA_MEMBER, SYSDB_ORIG_MEMBER, NULL},
-    {"memberof", IPA_MEMBEROF, SYSDB_ORIG_MEMBEROF, NULL},
-    {"ipa_id", IPA_UNIQUE_ID, IPA_UNIQUE_ID, NULL}
+    struct sdap_attr_map_info *ipa_hostgroup_map;
 };
 
 static void
@@ -76,6 +65,12 @@ static errno_t
 ipa_hostgroup_info_next(struct tevent_req *req,
                              struct ipa_host_state *state);
 
+/**
+ * hostname == NULL -> look up all hosts / host groups
+ * hostname != NULL -> look up only given host and groups
+ *                     it's member of
+ * hostgroup_map == NULL -> skip looking up hostgroups
+ */
 struct tevent_req *
 ipa_host_info_send(TALLOC_CTX *mem_ctx,
                    struct tevent_context *ev,
@@ -83,10 +78,8 @@ ipa_host_info_send(TALLOC_CTX *mem_ctx,
                    struct sdap_handle *sh,
                    struct sdap_options *opts,
                    const char *hostname,
-                   const char **attrs,
-                   struct sdap_attr_map *map,
-                   int map_num_attrs,
-                   bool fetch_hostgroups,
+                   struct sdap_attr_map *host_map,
+                   struct sdap_attr_map *hostgroup_map,
                    struct sdap_search_base **search_bases)
 {
     errno_t ret;
@@ -106,17 +99,22 @@ ipa_host_info_send(TALLOC_CTX *mem_ctx,
     state->search_bases = search_bases;
     state->search_base_iter = 0;
     state->cur_filter = NULL;
-    state->attrs = attrs;
-    state->map = map;
-    state->map_num_attrs = map_num_attrs;
-    state->fetch_hostgroups = fetch_hostgroups;
+    state->host_map = host_map;
+    state->hostgroup_map = hostgroup_map;
+
+    ret = build_attrs_from_map(state, host_map, IPA_OPTS_HOST, &state->attrs);
+    if (ret != EOK) {
+        goto immediate;
+    }
 
     if (hostname == NULL) {
         state->host_filter = talloc_asprintf(state, "(objectClass=%s)",
-                                             IPA_HOST);
+                                             host_map[IPA_OC_HOST].name);
     } else {
         state->host_filter = talloc_asprintf(state, "(&(objectClass=%s)(%s=%s))",
-                                             IPA_HOST, IPA_HOST_FQDN, hostname);
+                                             host_map[IPA_OC_HOST].name,
+                                             host_map[IPA_AT_HOST_FQDN].name,
+                                             hostname);
     }
     if (state->host_filter == NULL) {
         ret = ENOMEM;
@@ -166,8 +164,8 @@ static errno_t ipa_host_info_next(struct tevent_req *req,
     subreq = sdap_get_generic_send(state, state->ev, state->opts,
                                    state->sh, base->basedn,
                                    base->scope, state->cur_filter,
-                                   state->attrs, state->map,
-                                   state->map_num_attrs,
+                                   state->attrs, state->host_map,
+                                   IPA_OPTS_HOST,
                                    dp_opt_get_int(state->opts->basic,
                                                   SDAP_ENUM_SEARCH_TIMEOUT));
     if (subreq == NULL) {
@@ -189,7 +187,6 @@ ipa_host_info_done(struct tevent_req *subreq)
     struct ipa_host_state *state =
             tevent_req_data(req, struct ipa_host_state);
     const char *host_dn;
-    int i;
 
     ret = sdap_get_generic_recv(subreq, state,
                                 &state->host_count,
@@ -212,34 +209,20 @@ ipa_host_info_done(struct tevent_req *subreq)
         return;
     }
 
-    ret = replace_attribute_name(IPA_MEMBEROF, SYSDB_ORIG_MEMBEROF,
-                                 state->host_count,
-                                 state->hosts);
-    if (ret != EOK) {
-        DEBUG(SSSDBG_CRIT_FAILURE, ("Could not replace attribute names\n"));
-        tevent_req_error(req, ret);
-        return;
-    }
-
-    /* Complete the map */
-    for (i = 0; i < HOSTGROUP_MAP_ATTRS_COUNT; i++) {
-        /* These are allocated on the state, so the next time they'll
-         * have to be allocated again
-         */
-        hostgroup_map[i].name = talloc_strdup(state,
-                                              hostgroup_map[i].def_name);
-        if (hostgroup_map[i].name == NULL) {
+    if (state->hostgroup_map) {
+        talloc_free(state->attrs);
+        ret = build_attrs_from_map(state, state->hostgroup_map,
+                                   IPA_OPTS_HOSTGROUP, &state->attrs);
+        if (ret != EOK) {
             tevent_req_error(req, ret);
             return;
         }
-    }
 
-    if (state->fetch_hostgroups) {
         /* Look up host groups */
         if (state->hostname == NULL) {
             talloc_zfree(state->host_filter);
             state->host_filter = talloc_asprintf(state, "(objectClass=%s)",
-                                                 IPA_HOSTGROUP);
+                                    state->hostgroup_map[IPA_OC_HOSTGROUP].name);
             if (state->host_filter == NULL) {
                 tevent_req_error(req, ENOMEM);
                 return;
@@ -256,13 +239,13 @@ ipa_host_info_done(struct tevent_req *subreq)
                 return;
             }
         } else {
-            state->hostgroup_map = talloc_zero(state, struct sdap_attr_map_info);
-            if (state->hostgroup_map == NULL) {
+            state->ipa_hostgroup_map = talloc_zero(state, struct sdap_attr_map_info);
+            if (state->ipa_hostgroup_map == NULL) {
                 tevent_req_error(req, ENOMEM);
                 return;
             }
-            state->hostgroup_map->map = hostgroup_map;
-            state->hostgroup_map->num_attrs = HOSTGROUP_MAP_ATTRS_COUNT;
+            state->ipa_hostgroup_map->map = state->hostgroup_map;
+            state->ipa_hostgroup_map->num_attrs = IPA_OPTS_HOSTGROUP;
 
             ret = sysdb_attrs_get_string(state->hosts[0], SYSDB_ORIG_DN, &host_dn);
             if (ret != EOK) {
@@ -271,8 +254,10 @@ ipa_host_info_done(struct tevent_req *subreq)
             }
 
             subreq = sdap_deref_search_send(state, state->ev, state->opts, state->sh,
-                                            host_dn, IPA_MEMBEROF, state->attrs,
-                                            1, state->hostgroup_map,
+                                            host_dn,
+                                            state->hostgroup_map[IPA_AT_HOSTGROUP_MEMBER_OF].name,
+                                            state->attrs,
+                                            1, state->ipa_hostgroup_map,
                                             dp_opt_get_int(state->opts->basic,
                                                            SDAP_ENUM_SEARCH_TIMEOUT));
             if (subreq == NULL) {
@@ -308,8 +293,9 @@ static errno_t ipa_hostgroup_info_next(struct tevent_req *req,
 
     subreq = sdap_get_generic_send(state, state->ev, state->opts, state->sh,
                                    base->basedn, base->scope,
-                                   state->cur_filter, state->attrs, hostgroup_map,
-                                   HOSTGROUP_MAP_ATTRS_COUNT,
+                                   state->cur_filter, state->attrs,
+                                   state->hostgroup_map,
+                                   IPA_OPTS_HOSTGROUP,
                                    dp_opt_get_int(state->opts->basic,
                                                   SDAP_ENUM_SEARCH_TIMEOUT));
     if (subreq == NULL) {
@@ -407,7 +393,8 @@ ipa_hostgroup_info_done(struct tevent_req *subreq)
                 }
 
                 ret = sysdb_attrs_get_string(deref_result[i]->attrs,
-                                             IPA_CN, &hostgroup_name);
+                             state->hostgroup_map[IPA_AT_HOSTGROUP_NAME].sys_name,
+                             &hostgroup_name);
                 if (ret != EOK) goto done;
 
                 DEBUG(SSSDBG_FUNC_DATA, ("Dereferenced host group: %s\n",
diff --git a/src/providers/ipa/ipa_hosts.h b/src/providers/ipa/ipa_hosts.h
index 53cabbb79154b184149696c56a6d4910a352a14b..34ceec90531ec653f041a70bff1413b6e15e9e45 100644
--- a/src/providers/ipa/ipa_hosts.h
+++ b/src/providers/ipa/ipa_hosts.h
@@ -30,10 +30,8 @@ ipa_host_info_send(TALLOC_CTX *mem_ctx,
                    struct sdap_handle *sh,
                    struct sdap_options *opts,
                    const char *hostname,
-                   const char **attrs,
-                   struct sdap_attr_map *map,
-                   int map_num_attrs,
-                   bool fetch_hostgroups,
+                   struct sdap_attr_map *host_map,
+                   struct sdap_attr_map *hostgroup_map,
                    struct sdap_search_base **search_bases);
 
 errno_t
diff --git a/src/providers/ipa/ipa_session.c b/src/providers/ipa/ipa_session.c
index 6b7fc8efb617c871369b445b671ed05fac575025..8876b7221bbf171691ede2780402f2dcce13d1b4 100644
--- a/src/providers/ipa/ipa_session.c
+++ b/src/providers/ipa/ipa_session.c
@@ -35,18 +35,12 @@
 #include "providers/ipa/ipa_selinux_common.h"
 #include "providers/ipa/ipa_selinux_maps.h"
 
-/* FIXME: this is temporary until host map is implemented in ipa_common.c */
-#include "providers/ipa/ipa_hbac_private.h"
-
 struct ipa_get_selinux_state {
     struct be_req *be_req;
     struct pam_data *pd;
     struct ipa_session_ctx *session_ctx;
     struct sdap_id_op *op;
 
-    /* Just tmp stuff so we can free it after query */
-    const char **attrs;
-
     const char *hostname;
     struct sysdb_attrs *host;
     struct sysdb_attrs *user;
@@ -251,21 +245,13 @@ static void ipa_get_selinux_connect_done(struct tevent_req *subreq)
     /* FIXME: detect if HBAC is configured
      * - if yes, we can skip host retrieval and get it directly from sysdb
      */
-    state->attrs = talloc_array(state, const char *, 3);
-    if (state->attrs == NULL) {
-        ret = ENOMEM;
-        goto fail;
-    }
-    state->attrs[0] = "objectClass";
-    state->attrs[1] = IPA_MEMBEROF;
-    state->attrs[2] = NULL;
-
     subreq = ipa_host_info_send(state, bctx->ev, bctx->sysdb,
                                 sdap_id_op_handle(state->op),
                                 id_ctx->sdap_id_ctx->opts,
                                 state->hostname,
-                                state->attrs, NULL, 0,
-                                false, state->session_ctx->host_search_bases);
+                                id_ctx->sdap_id_ctx->opts->host_map,
+                                NULL,
+                                state->session_ctx->host_search_bases);
     if (subreq == NULL) {
         ret = ENOMEM;
         goto fail;
@@ -300,11 +286,6 @@ static void ipa_get_selinux_hosts_done(struct tevent_req *subreq)
     }
     state->host = host[0];
 
-    ret = sysdb_attrs_add_string(state->host, SYSDB_NAME, state->hostname);
-    if (ret != EOK) {
-        goto done;
-    }
-
     ret = sss_selinux_extract_user(state, bctx->sysdb,
                                    state->pd->user, &state->user);
     if (ret != EOK) {
diff --git a/src/providers/ldap/sdap.h b/src/providers/ldap/sdap.h
index 5d42384661946008e27355101788f4b91d9aecee..c6fafe503ff2b55343d633ea73f93adb48a5f566 100644
--- a/src/providers/ldap/sdap.h
+++ b/src/providers/ldap/sdap.h
@@ -348,7 +348,6 @@ struct sdap_options {
     struct sdap_attr_map *user_map;
     struct sdap_attr_map *group_map;
     struct sdap_attr_map *netgroup_map;
-    struct sdap_attr_map *host_map;
     struct sdap_attr_map *service_map;
 
     /* FIXME - should this go to a special struct to avoid mixing with name-service-switch maps? */
@@ -356,6 +355,8 @@ struct sdap_options {
     struct sdap_attr_map *autofs_mobject_map;
     struct sdap_attr_map *autofs_entry_map;
     struct sdap_attr_map *selinuxuser_map;
+    struct sdap_attr_map *host_map;
+    struct sdap_attr_map *hostgroup_map;
 
     /* supported schema types */
     enum schema_type {
-- 
1.7.6.4

