From 95a2ae812c5d2fd85a17ec7c98eb5ae55ee67aa6 Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Wed, 2 Jun 2010 11:45:24 +0200 Subject: [PATCH 1/2] Handle host objects like other objects --- src/providers/ipa/ipa_access.c | 271 +++++++++++++++++++++++----------------- src/providers/ipa/ipa_access.h | 3 +- 2 files changed, 156 insertions(+), 118 deletions(-) diff --git a/src/providers/ipa/ipa_access.c b/src/providers/ipa/ipa_access.c index 3d66c16..25302f8 100644 --- a/src/providers/ipa/ipa_access.c +++ b/src/providers/ipa/ipa_access.c @@ -159,6 +159,136 @@ static errno_t hbac_sysdb_data_recv(TALLOC_CTX *mem_ctx, return EOK; } +static errno_t set_local_and_remote_host_info(TALLOC_CTX *mem_ctx, + size_t host_count, + struct sysdb_attrs **host_list, + const char *local_hostname, + const char *remote_hostname, + struct hbac_host_info **local_hhi, + struct hbac_host_info **remote_hhi) + +{ + size_t c; + int ret; + struct hbac_host_info *hhi; + struct ldb_message_element *el; + + if (local_hostname == NULL || *local_hostname == '\0') { + DEBUG(1, ("Missing local hostname.\n")); + ret = EINVAL; + goto fail; + } + + if (host_count == 0) { + DEBUG(1, ("No host data available.\n")); + ret = EINVAL; + goto fail; + } + + for (c = 0; c < host_count; c++) { + hhi = talloc_zero(mem_ctx, struct hbac_host_info); + if (hhi == NULL) { + DEBUG(1, ("talloc_zero failed.\n")); + ret = ENOMEM; + goto fail; + } + + ret = sysdb_attrs_get_el(host_list[c], SYSDB_ORIG_DN, &el); + if (ret != EOK) { + DEBUG(1, ("sysdb_attrs_get_el failed.\n")); + goto fail; + } + if (el->num_values == 0) { + DEBUG(1, ("Missing OriginalDN.\n")); + ret = EINVAL; + goto fail; + } + DEBUG(9, ("OriginalDN: [%.*s].\n", el->values[0].length, + (char *)el->values[0].data)); + hhi->dn = talloc_strndup(hhi, (char *)el->values[0].data, + el->values[0].length); + if (hhi->dn == NULL) { + DEBUG(1, ("talloc_strndup failed.\n")); + ret = ENOMEM; + goto fail; + } + + ret = sysdb_attrs_get_el(host_list[c], IPA_HOST_SERVERHOSTNAME, &el); + if (ret != EOK) { + DEBUG(1, ("sysdb_attrs_get_el failed.\n")); + goto fail; + } + if (el->num_values == 0) { + DEBUG(1, ("Missing ServerHostName.\n")); + ret = EINVAL; + goto fail; + } + DEBUG(9, ("ServerHostName: [%.*s].\n", el->values[0].length, + (char *)el->values[0].data)); + hhi->serverhostname = talloc_strndup(hhi, (char *)el->values[0].data, + el->values[0].length); + if (hhi->serverhostname == NULL) { + DEBUG(1, ("talloc_strndup failed.\n")); + ret = ENOMEM; + goto fail; + } + + ret = sysdb_attrs_get_el(host_list[c], IPA_HOST_FQDN, &el); + if (ret != EOK) { + DEBUG(1, ("sysdb_attrs_get_el failed.\n")); + goto fail; + } + if (el->num_values == 0) { + DEBUG(1, ("Missing FQDN.\n")); + ret = EINVAL; + goto fail; + } + DEBUG(9, ("FQDN: [%.*s].\n", el->values[0].length, + (char *)el->values[0].data)); + hhi->fqdn = talloc_strndup(hhi, (char *)el->values[0].data, + el->values[0].length); + if (hhi->fqdn == NULL) { + DEBUG(1, ("talloc_strndup failed.\n")); + ret = ENOMEM; + goto fail; + } + + ret = sysdb_attrs_get_string_array(host_list[c], SYSDB_ORIG_MEMBEROF, + hhi, &hhi->memberof); + if (ret != EOK) { + if (ret != ENOENT) { + DEBUG(1, ("sysdb_attrs_get_string_array failed.\n")); + goto fail; + } + + hhi->memberof = talloc_array(hhi, const char *, 1); + if (hhi->memberof == NULL) { + DEBUG(1, ("talloc_array failed.\n")); + ret = ENOMEM; + goto fail; + } + hhi->memberof[0] = NULL; + } + + if (strcmp(hhi->fqdn, local_hostname) == 0 || + strcmp(hhi->serverhostname, local_hostname) == 0) { + *local_hhi = hhi; + } + + if (remote_hostname != NULL && *remote_hostname != '\0') { + if (strcmp(hhi->fqdn, remote_hostname) == 0 || + strcmp(hhi->serverhostname, remote_hostname) == 0) { + *remote_hhi = hhi; + } + } + } + + return EOK; + +fail: + return ret; +} + static void ipa_access_reply(struct be_req *be_req, int pam_status) { struct pam_data *pd; @@ -657,7 +787,7 @@ static struct tevent_req *hbac_get_host_info_send(TALLOC_CTX *memctx, goto fail; } - state->host_attrs = talloc_array(state, const char *, 7); + state->host_attrs = talloc_array(state, const char *, 8); if (state->host_attrs == NULL) { DEBUG(1, ("Failed to allocate host attribute list.\n")); ret = ENOMEM; @@ -669,7 +799,8 @@ static struct tevent_req *hbac_get_host_info_send(TALLOC_CTX *memctx, state->host_attrs[3] = "objectClass"; state->host_attrs[4] = SYSDB_ORIG_DN; state->host_attrs[5] = SYSDB_ORIG_MEMBEROF; - state->host_attrs[6] = NULL; + state->host_attrs[6] = IPA_UNIQUE_ID; + state->host_attrs[7] = NULL; if (offline) { ret = hbac_sysdb_data_recv(state, state->sysdb, @@ -678,7 +809,7 @@ static struct tevent_req *hbac_get_host_info_send(TALLOC_CTX *memctx, state->host_attrs, &state->host_reply_count, &state->host_reply_list); - if (ret) { + if (ret != EOK) { DEBUG(1, ("hbac_sysdb_data_recv failed.\n")); goto fail; } @@ -793,7 +924,6 @@ static void hbac_get_host_memberof(struct tevent_req *req) int ret; int i; struct ldb_message_element *el; - struct hbac_host_info **hhi; char *object_name; if (state->host_reply_count == 0) { @@ -802,100 +932,6 @@ static void hbac_get_host_memberof(struct tevent_req *req) goto fail; } - hhi = talloc_array(state, struct hbac_host_info *, state->host_reply_count + 1); - if (hhi == NULL) { - DEBUG(1, ("talloc_array failed.\n")); - ret = ENOMEM; - goto fail; - } - memset(hhi, 0, - sizeof(struct hbac_host_info *) * (state->host_reply_count + 1)); - - for (i = 0; i < state->host_reply_count; i++) { - hhi[i] = talloc_zero(hhi, struct hbac_host_info); - if (hhi[i] == NULL) { - ret = ENOMEM; - goto fail; - } - - ret = sysdb_attrs_get_el(state->host_reply_list[i], SYSDB_ORIG_DN, &el); - if (ret != EOK) { - DEBUG(1, ("sysdb_attrs_get_el failed.\n")); - goto fail; - } - if (el->num_values == 0) { - ret = EINVAL; - goto fail; - } - DEBUG(9, ("OriginalDN: [%.*s].\n", el->values[0].length, - (char *)el->values[0].data)); - hhi[i]->dn = talloc_strndup(hhi, (char *)el->values[0].data, - el->values[0].length); - if (hhi[i]->dn == NULL) { - ret = ENOMEM; - goto fail; - } - - ret = sysdb_attrs_get_el(state->host_reply_list[i], - IPA_HOST_SERVERHOSTNAME, &el); - if (ret != EOK) { - DEBUG(1, ("sysdb_attrs_get_el failed.\n")); - goto fail; - } - if (el->num_values == 0) { - ret = EINVAL; - goto fail; - } - DEBUG(9, ("ServerHostName: [%.*s].\n", el->values[0].length, - (char *)el->values[0].data)); - hhi[i]->serverhostname = talloc_strndup(hhi, (char *)el->values[0].data, - el->values[0].length); - if (hhi[i]->serverhostname == NULL) { - ret = ENOMEM; - goto fail; - } - - ret = sysdb_attrs_get_el(state->host_reply_list[i], - IPA_HOST_FQDN, &el); - if (ret != EOK) { - DEBUG(1, ("sysdb_attrs_get_el failed.\n")); - goto fail; - } - if (el->num_values == 0) { - ret = EINVAL; - goto fail; - } - DEBUG(9, ("FQDN: [%.*s].\n", el->values[0].length, - (char *)el->values[0].data)); - hhi[i]->fqdn = talloc_strndup(hhi, (char *)el->values[0].data, - el->values[0].length); - if (hhi[i]->fqdn == NULL) { - ret = ENOMEM; - goto fail; - } - - ret = sysdb_attrs_get_string_array(state->host_reply_list[i], - state->offline ? SYSDB_ORIG_MEMBEROF : - IPA_MEMBEROF, - hhi, &(hhi[i]->memberof)); - if (ret != EOK) { - if (ret != ENOENT) { - DEBUG(1, ("sysdb_attrs_get_string_array failed.\n")); - goto fail; - } - - hhi[i]->memberof = talloc_array(hhi, const char *, 1); - if (hhi[i]->memberof == NULL) { - DEBUG(1, ("talloc_array failed.\n")); - ret = ENOMEM; - goto fail; - } - hhi[i]->memberof[0] = NULL; - } - } - - state->hbac_host_info = hhi; - if (state->offline) { tevent_req_done(req); return; @@ -959,14 +995,21 @@ fail: } static int hbac_get_host_info_recv(struct tevent_req *req, TALLOC_CTX *memctx, - struct hbac_host_info ***hhi) + size_t *hbac_hosts_count, + struct sysdb_attrs ***hbac_hosts_list) { + size_t c; struct hbac_get_host_info_state *state = tevent_req_data(req, struct hbac_get_host_info_state); TEVENT_REQ_RETURN_ON_ERROR(req); - *hhi = talloc_steal(memctx, state->hbac_host_info); + *hbac_hosts_count = state->host_reply_count; + *hbac_hosts_list = talloc_steal(memctx, state->host_reply_list); + for (c = 0; c < state->host_reply_count; c++) { + talloc_steal(memctx, state->host_reply_list[c]); + } + return EOK; } @@ -1900,9 +1943,9 @@ static void hbac_get_host_info_done(struct tevent_req *req) int pam_status = PAM_SYSTEM_ERR; const char *ipa_hostname; struct hbac_host_info *local_hhi = NULL; - int i; - ret = hbac_get_host_info_recv(req, hbac_ctx, &hbac_ctx->hbac_host_info); + ret = hbac_get_host_info_recv(req, hbac_ctx, &hbac_ctx->hbac_hosts_count, + &hbac_ctx->hbac_hosts_list); talloc_zfree(req); if (ret != EOK) { goto fail; @@ -1915,21 +1958,15 @@ static void hbac_get_host_info_done(struct tevent_req *req) goto fail; } - for (i = 0; hbac_ctx->hbac_host_info[i] != NULL; i++) { - if (strcmp(hbac_ctx->hbac_host_info[i]->fqdn, ipa_hostname) == 0 || - strcmp(hbac_ctx->hbac_host_info[i]->serverhostname, - ipa_hostname) == 0) { - local_hhi = hbac_ctx->hbac_host_info[i]; - } - if (hbac_ctx->pd->rhost != NULL && *hbac_ctx->pd->rhost != '\0') { - if (strcmp(hbac_ctx->hbac_host_info[i]->fqdn, - hbac_ctx->pd->rhost) == 0 || - strcmp(hbac_ctx->hbac_host_info[i]->serverhostname, - hbac_ctx->pd->rhost) == 0) { - hbac_ctx->remote_hhi = hbac_ctx->hbac_host_info[i]; - } - } - } + ret = set_local_and_remote_host_info(hbac_ctx, hbac_ctx->hbac_hosts_count, + hbac_ctx->hbac_hosts_list, ipa_hostname, + hbac_ctx->pd->rhost, &local_hhi, + &hbac_ctx->remote_hhi); + if (ret != EOK) { + DEBUG(1, ("set_local_and_remote_host_info failed.\n")); + goto fail; + } + if (local_hhi == NULL) { DEBUG(1, ("Missing host info for [%s].\n", ipa_hostname)); ret = EINVAL; diff --git a/src/providers/ipa/ipa_access.h b/src/providers/ipa/ipa_access.h index 514afc0..d99f240 100644 --- a/src/providers/ipa/ipa_access.h +++ b/src/providers/ipa/ipa_access.h @@ -51,7 +51,8 @@ struct hbac_ctx { struct time_rules_ctx *tr_ctx; struct be_req *be_req; struct pam_data *pd; - struct hbac_host_info **hbac_host_info; + struct sysdb_attrs **hbac_hosts_list; + size_t hbac_hosts_count; struct hbac_host_info *remote_hhi; struct sysdb_attrs **hbac_rule_list; size_t hbac_rule_count; -- 1.7.0.1