60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev/*
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev Authors:
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev Jan Cholasta <jcholast@redhat.com>
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev Copyright (C) 2012 Red Hat
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev This program is free software; you can redistribute it and/or modify
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev it under the terms of the GNU General Public License as published by
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev the Free Software Foundation; either version 3 of the License, or
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev (at your option) any later version.
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev This program is distributed in the hope that it will be useful,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev but WITHOUT ANY WARRANTY; without even the implied warranty of
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev GNU General Public License for more details.
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev You should have received a copy of the GNU General Public License
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev along with this program. If not, see <http://www.gnu.org/licenses/>.
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev*/
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev#include "util/util.h"
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev#include "util/crypto/sss_crypto.h"
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev#include "db/sysdb_ssh.h"
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev#include "providers/ldap/ldap_common.h"
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev#include "providers/ldap/sdap_async.h"
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev#include "providers/ldap/sdap_hostid.h"
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevstruct hosts_get_state {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct tevent_context *ev;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct sdap_id_ctx *id_ctx;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct sdap_id_op *op;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct sss_domain_info *domain;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev const char *name;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev const char *alias;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev size_t count;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct sysdb_attrs **hosts;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev int dp_error;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev};
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevstatic errno_t
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevhosts_get_retry(struct tevent_req *req);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevstatic void
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevhosts_get_connect_done(struct tevent_req *subreq);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevstatic void
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevhosts_get_done(struct tevent_req *subreq);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevstruct tevent_req *
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevhosts_get_send(TALLOC_CTX *memctx,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct tevent_context *ev,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct sdap_id_ctx *id_ctx,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev const char *name,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev const char *alias)
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev{
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct tevent_req *req;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct hosts_get_state *state;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev errno_t ret;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev req = tevent_req_create(memctx, &state, struct hosts_get_state);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (!req) return NULL;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state->ev = ev;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state->id_ctx = id_ctx;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state->dp_error = DP_ERR_FATAL;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state->op = sdap_id_op_create(state, id_ctx->conn->conn_cache);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (!state->op) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev DEBUG(SSSDBG_OP_FAILURE, "sdap_id_op_create failed\n");
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev ret = ENOMEM;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev goto fail;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state->domain = id_ctx->be->domain;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state->name = name;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state->alias = alias;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev ret = hosts_get_retry(req);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (ret != EOK) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev goto fail;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev return req;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevfail:
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev tevent_req_error(req, ret);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev tevent_req_post(req, ev);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev return req;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev}
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevstatic errno_t
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevhosts_get_retry(struct tevent_req *req)
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev{
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct hosts_get_state *state = tevent_req_data(req,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct hosts_get_state);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct tevent_req *subreq;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev errno_t ret = EOK;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev subreq = sdap_id_op_connect_send(state->op, state, &ret);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (!subreq) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev return ret;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev tevent_req_set_callback(subreq, hosts_get_connect_done, req);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev return EOK;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev}
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevstatic void
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevhosts_get_connect_done(struct tevent_req *subreq)
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev{
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct tevent_req *req = tevent_req_callback_data(subreq,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct tevent_req);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct hosts_get_state *state = tevent_req_data(req,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct hosts_get_state);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev int dp_error = DP_ERR_FATAL;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev errno_t ret;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev ret = sdap_id_op_connect_recv(subreq, &dp_error);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev talloc_zfree(subreq);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (ret != EOK) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state->dp_error = dp_error;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev tevent_req_error(req, ret);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev return;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev subreq = sdap_host_info_send(state, state->ev,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev sdap_id_op_handle(state->op),
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state->id_ctx->opts, state->name,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state->id_ctx->opts->host_map,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state->id_ctx->opts->sdom->host_search_bases);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (!subreq) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev tevent_req_error(req, ENOMEM);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev return;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev tevent_req_set_callback(subreq, hosts_get_done, req);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev}
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevstatic void
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevhosts_get_done(struct tevent_req *subreq)
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev{
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct tevent_req *req = tevent_req_callback_data(subreq,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct tevent_req);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct hosts_get_state *state = tevent_req_data(req,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct hosts_get_state);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev int dp_error = DP_ERR_FATAL;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev errno_t ret;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct sysdb_attrs *attrs;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev time_t now = time(NULL);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev ret = sdap_host_info_recv(subreq, state,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev &state->count, &state->hosts);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev talloc_zfree(subreq);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev ret = sdap_id_op_done(state->op, ret, &dp_error);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (dp_error == DP_ERR_OK && ret != EOK) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev /* retry */
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev ret = hosts_get_retry(req);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (ret != EOK) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev goto done;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev return;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (ret != EOK && ret != ENOENT) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev goto done;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (state->count == 0) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev DEBUG(SSSDBG_OP_FAILURE,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev "No host with name [%s] found.\n", state->name);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev ret = sysdb_delete_ssh_host(state->domain, state->name);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (ret != EOK && ret != ENOENT) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev goto done;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev ret = EINVAL;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev goto done;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (state->count > 1) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev DEBUG(SSSDBG_CRIT_FAILURE,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev "Found more than one host with name [%s].\n", state->name);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev ret = EINVAL;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev goto done;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev attrs = sysdb_new_attrs(state);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (!attrs) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev ret = ENOMEM;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev goto done;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev /* we are interested only in the host keys */
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev ret = sysdb_attrs_copy_values(state->hosts[0], attrs, SYSDB_SSH_PUBKEY);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (ret != EOK) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev goto done;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev ret = sysdb_store_ssh_host(state->domain, state->name, state->alias,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state->domain->ssh_host_timeout, now, attrs);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (ret != EOK) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev goto done;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev dp_error = DP_ERR_OK;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevdone:
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state->dp_error = dp_error;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (ret == EOK) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev tevent_req_done(req);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev } else {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev tevent_req_error(req, ret);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev}
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevstatic errno_t
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevhosts_get_recv(struct tevent_req *req,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev int *dp_error_out)
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev{
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct hosts_get_state *state = tevent_req_data(req,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct hosts_get_state);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (dp_error_out) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev *dp_error_out = state->dp_error;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev TEVENT_REQ_RETURN_ON_ERROR(req);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev return EOK;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev}
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevstruct sdap_hostid_handler_state {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct dp_reply_std reply;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev};
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevstatic void sdap_hostid_handler_done(struct tevent_req *subreq);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevstruct tevent_req *
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevsdap_hostid_handler_send(TALLOC_CTX *mem_ctx,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct sdap_id_ctx *id_ctx,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct dp_hostid_data *data,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct dp_req_params *params)
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev{
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct sdap_hostid_handler_state *state;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct tevent_req *subreq;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct tevent_req *req;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev errno_t ret;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev req = tevent_req_create(mem_ctx, &state, struct sdap_hostid_handler_state);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (req == NULL) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev DEBUG(SSSDBG_CRIT_FAILURE, "tevent_req_create() failed\n");
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev return NULL;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev subreq = hosts_get_send(state, params->ev, id_ctx,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev data->name, data->alias);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev if (subreq == NULL) {
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev DEBUG(SSSDBG_CRIT_FAILURE, "Unable to send request\n");
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev ret = ENOMEM;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev goto immediately;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev }
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev tevent_req_set_callback(subreq, sdap_hostid_handler_done, req);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev return req;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevimmediately:
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev dp_reply_std_set(&state->reply, DP_ERR_DECIDE, ret, NULL);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev /* TODO For backward compatibility we always return EOK to DP now. */
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev tevent_req_done(req);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev tevent_req_post(req, params->ev);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev return req;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev}
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevstatic void sdap_hostid_handler_done(struct tevent_req *subreq)
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev{
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct sdap_hostid_handler_state *state;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct tevent_req *req;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev int dp_error;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev errno_t ret;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev req = tevent_req_callback_data(subreq, struct tevent_req);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state = tevent_req_data(req, struct sdap_hostid_handler_state);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev ret = hosts_get_recv(subreq, &dp_error);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev talloc_zfree(subreq);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev /* TODO For backward compatibility we always return EOK to DP now. */
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev dp_reply_std_set(&state->reply, dp_error, ret, NULL);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev tevent_req_done(req);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev}
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Veneverrno_t
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venevsdap_hostid_handler_recv(TALLOC_CTX *mem_ctx,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct tevent_req *req,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct dp_reply_std *data)
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev{
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct sdap_hostid_handler_state *state = NULL;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev state = tevent_req_data(req, struct sdap_hostid_handler_state);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev TEVENT_REQ_RETURN_ON_ERROR(req);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev *data = state->reply;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev return EOK;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev}
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Veneverrno_t sdap_hostid_init(TALLOC_CTX *mem_ctx,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct be_ctx *be_ctx,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct sdap_id_ctx *id_ctx,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct dp_method *dp_methods)
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev{
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev (void)be_ctx;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev dp_set_method(dp_methods, DPM_HOSTID_HANDLER,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev sdap_hostid_handler_send, sdap_hostid_handler_recv, id_ctx,
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev struct sdap_id_ctx, struct dp_hostid_data, struct dp_reply_std);
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev return EOK;
60a715a0dd79873d2d2607eab8fdfaf0ffd2e7d3Hristo Venev}