ipa_hostid.c revision 2d6836a90bd326391782a5753f70e8ba666b5def
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor/*
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor Authors:
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor Jan Cholasta <jcholast@redhat.com>
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor Copyright (C) 2012 Red Hat
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor This program is free software; you can redistribute it and/or modify
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor it under the terms of the GNU General Public License as published by
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor the Free Software Foundation; either version 3 of the License, or
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor (at your option) any later version.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen This program is distributed in the hope that it will be useful,
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen but WITHOUT ANY WARRANTY; without even the implied warranty of
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor GNU General Public License for more details.
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen You should have received a copy of the GNU General Public License
3f08db06526d6901aa08c110b5bc7dde6bc39905nd along with this program. If not, see <http://www.gnu.org/licenses/>.
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor*/
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor#include "util/util.h"
3f08db06526d6901aa08c110b5bc7dde6bc39905nd#include "util/crypto/sss_crypto.h"
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor#include "db/sysdb_ssh.h"
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor#include "providers/ldap/ldap_common.h"
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor#include "providers/ipa/ipa_common.h"
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung#include "providers/ipa/ipa_hostid.h"
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor#include "providers/ipa/ipa_hosts.h"
35ac4e1a6ec5aa3cfa1a34d8f20fe8a841cc46b7rbowen
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorstruct hosts_get_state {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct tevent_context *ev;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct ipa_hostid_ctx *ctx;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct sdap_id_op *op;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct sysdb_ctx *sysdb;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct sss_domain_info *domain;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor const char *name;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor const char *alias;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor size_t count;
35ac4e1a6ec5aa3cfa1a34d8f20fe8a841cc46b7rbowen struct sysdb_attrs **hosts;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor int dp_error;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor};
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorstruct tevent_req *
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorhosts_get_send(TALLOC_CTX *memctx,
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh struct tevent_context *ev,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct ipa_hostid_ctx *hostid_ctx,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor const char *name,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor const char *alias);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorstatic errno_t
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorhosts_get_recv(struct tevent_req *req,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor int *dp_error_out);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorstatic void
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzoripa_host_info_hosts_done(struct tevent_req *req);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorvoid
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzoripa_host_info_handler(struct be_req *breq)
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor{
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct ipa_hostid_ctx *hostid_ctx;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct sdap_id_ctx *ctx;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct be_host_req *hr;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct tevent_req *req;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor int dp_error = DP_ERR_FATAL;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor errno_t ret = EOK;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor const char *err = "Unknown Error";
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor hostid_ctx = talloc_get_type(breq->be_ctx->bet_info[BET_HOSTID].pvt_bet_data, struct ipa_hostid_ctx);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ctx = hostid_ctx->sdap_id_ctx;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (be_is_offline(ctx->be)) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor dp_error = DP_ERR_OFFLINE;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ret = EAGAIN;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor err = "Offline";
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor goto done;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor hr = talloc_get_type(breq->req_data, struct be_host_req);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (hr->filter_type != BE_FILTER_NAME) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ret = EINVAL;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor err = "Invalid filter type";
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor goto done;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor req = hosts_get_send(breq, breq->be_ctx->ev, hostid_ctx,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor hr->name, hr->alias);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (!req) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ret = ENOMEM;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor err = "Out of memory";
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor goto done;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor tevent_req_set_callback(req, ipa_host_info_hosts_done, breq);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ret = EOK;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzordone:
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (ret != EOK) return sdap_handler_done(breq, dp_error, ret, err);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor}
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorstatic void
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzoripa_host_info_complete(struct be_req *breq, int dp_error,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor errno_t ret, const char *default_error_text)
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor{
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor const char* error_text;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (dp_error == DP_ERR_OK) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (ret == EOK) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor error_text = NULL;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor } else {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor DEBUG(SSSDBG_CRIT_FAILURE,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ("Bug: dp_error is OK on failed request"));
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor dp_error = DP_ERR_FATAL;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor error_text = default_error_text;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor } else if (dp_error == DP_ERR_OFFLINE) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor error_text = "Offline";
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor } else if (dp_error == DP_ERR_FATAL && ret == ENOMEM) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor error_text = "Out of memory";
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor } else {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor error_text = default_error_text;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor sdap_handler_done(breq, dp_error, ret, error_text);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor}
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorstatic void
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzoripa_host_info_hosts_done(struct tevent_req *req)
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor{
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct be_req *breq = tevent_req_callback_data(req, struct be_req);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor int ret, dp_error;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ret = hosts_get_recv(req, &dp_error);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor talloc_zfree(req);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ipa_host_info_complete(breq, dp_error, ret, "Host lookup failed");
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor}
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorstatic errno_t
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorhosts_get_retry(struct tevent_req *req);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorstatic void
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorhosts_get_connect_done(struct tevent_req *subreq);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorstatic void
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorhosts_get_done(struct tevent_req *subreq);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorstruct tevent_req *
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorhosts_get_send(TALLOC_CTX *memctx,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct tevent_context *ev,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct ipa_hostid_ctx *hostid_ctx,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor const char *name,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor const char *alias)
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor{
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct tevent_req *req;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct hosts_get_state *state;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct sdap_id_ctx *ctx;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor errno_t ret;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ctx = hostid_ctx->sdap_id_ctx;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor req = tevent_req_create(memctx, &state, struct hosts_get_state);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (!req) return NULL;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor state->ev = ev;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor state->ctx = hostid_ctx;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor state->dp_error = DP_ERR_FATAL;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor state->op = sdap_id_op_create(state, ctx->conn_cache);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (!state->op) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor DEBUG(SSSDBG_OP_FAILURE, ("sdap_id_op_create failed\n"));
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ret = ENOMEM;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor goto fail;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor state->sysdb = ctx->be->sysdb;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor state->domain = ctx->be->domain;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor state->name = name;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor state->alias = alias;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ret = hosts_get_retry(req);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (ret != EOK) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor goto fail;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor return req;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorfail:
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor tevent_req_error(req, ret);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor tevent_req_post(req, ev);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor return req;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor}
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorstatic errno_t
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorhosts_get_retry(struct tevent_req *req)
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor{
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct hosts_get_state *state = tevent_req_data(req,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct hosts_get_state);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct tevent_req *subreq;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor errno_t ret = EOK;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor subreq = sdap_id_op_connect_send(state->op, state, &ret);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (!subreq) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor return ret;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor tevent_req_set_callback(subreq, hosts_get_connect_done, req);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor return EOK;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor}
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorstatic void
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorhosts_get_connect_done(struct tevent_req *subreq)
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor{
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct tevent_req *req = tevent_req_callback_data(subreq,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct tevent_req);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct hosts_get_state *state = tevent_req_data(req,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct hosts_get_state);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor int dp_error = DP_ERR_FATAL;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor errno_t ret;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ret = sdap_id_op_connect_recv(subreq, &dp_error);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor talloc_zfree(subreq);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (ret != EOK) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor state->dp_error = dp_error;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor tevent_req_error(req, ret);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor return;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor subreq = ipa_host_info_send(state, state->ev, state->sysdb,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor sdap_id_op_handle(state->op),
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor state->ctx->sdap_id_ctx->opts, state->name,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor state->ctx->ipa_opts->host_map, NULL,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor state->ctx->host_search_bases);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (!subreq) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor tevent_req_error(req, ENOMEM);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor return;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor tevent_req_set_callback(subreq, hosts_get_done, req);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor}
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorstatic void
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorhosts_get_done(struct tevent_req *subreq)
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor{
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct tevent_req *req = tevent_req_callback_data(subreq,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct tevent_req);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct hosts_get_state *state = tevent_req_data(req,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct hosts_get_state);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor int dp_error = DP_ERR_FATAL;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor errno_t ret;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct sysdb_attrs *attrs;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor time_t now = time(NULL);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ret = ipa_host_info_recv(subreq, state,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor &state->count, &state->hosts,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor NULL, NULL);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor talloc_zfree(subreq);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ret = sdap_id_op_done(state->op, ret, &dp_error);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (dp_error == DP_ERR_OK && ret != EOK) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor /* retry */
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ret = hosts_get_retry(req);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (ret != EOK) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor goto done;
35ac4e1a6ec5aa3cfa1a34d8f20fe8a841cc46b7rbowen }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor return;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (ret != EOK && ret != ENOENT) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor goto done;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (state->count == 0) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor DEBUG(SSSDBG_CRIT_FAILURE,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ("No host with name [%s] found.\n", state->name));
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ret = EINVAL;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor goto done;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (state->count > 1) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor DEBUG(SSSDBG_CRIT_FAILURE,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ("Found more than one host with name [%s].\n", state->name));
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ret = EINVAL;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor goto done;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor attrs = sysdb_new_attrs(state);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (!attrs) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ret = ENOMEM;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor goto done;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor /* we are interested only in the host keys */
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ret = sysdb_attrs_copy_values(state->hosts[0], attrs, SYSDB_SSH_PUBKEY);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (ret != EOK) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor goto done;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor ret = sysdb_store_ssh_host(state->sysdb, state->name, state->alias, now,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor attrs);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (ret != EOK) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor goto done;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor dp_error = DP_ERR_OK;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzordone:
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor state->dp_error = dp_error;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (ret == EOK) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor tevent_req_done(req);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor } else {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor tevent_req_error(req, ret);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor}
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorstatic errno_t
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzorhosts_get_recv(struct tevent_req *req,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor int *dp_error_out)
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor{
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct hosts_get_state *state = tevent_req_data(req,
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor struct hosts_get_state);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor if (dp_error_out) {
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor *dp_error_out = state->dp_error;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor }
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor TEVENT_REQ_RETURN_ON_ERROR(req);
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor return EOK;
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor}
293aa6caecb1db771e9baa2f79c92f1c381caee4gryzor