ifpsrv_cmd.c revision faae3d55e5cf416f16158d3b9f8c8fd475ac6acf
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina Jakub Hrozek <jhrozek@redhat.com>
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina Copyright (C) 2013 Red Hat
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina InfoPipe responder: the responder commands
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina This program is free software; you can redistribute it and/or modify
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina it under the terms of the GNU General Public License as published by
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina the Free Software Foundation; either version 3 of the License, or
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina (at your option) any later version.
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina This program is distributed in the hope that it will be useful,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina but WITHOUT ANY WARRANTY; without even the implied warranty of
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina GNU General Public License for more details.
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina You should have received a copy of the GNU General Public License
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina along with this program. If not, see <http://www.gnu.org/licenses/>.
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina#include "responder/common/responder_cache_req.h"
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina const char **attrs;
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinastatic struct tevent_req *
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinaifp_user_get_attr_send(TALLOC_CTX *mem_ctx, struct resp_ctx *rctx,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinastatic errno_t ifp_user_get_attr_recv(TALLOC_CTX *mem_ctx,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinastatic void ifp_user_get_attr_process(struct tevent_req *req);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinaifp_user_get_attr_handle_reply(struct sss_domain_info *domain,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina const char **attrs,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinaifp_user_get_attr_unpack_msg(struct ifp_attr_req *attr_req);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinaint ifp_user_get_attr(struct sbus_request *dbus_req, void *data)
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina ifp_ctx = talloc_get_type(data, struct ifp_ctx);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Invalid pointer!\n");
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina return sbus_request_return_and_finish(dbus_req, DBUS_TYPE_INVALID);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina ret = ifp_req_create(dbus_req, ifp_ctx, &ireq);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina return ifp_req_create_handle_failure(dbus_req, ret);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina attr_req = talloc_zero(ireq, struct ifp_attr_req);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina "Looking up attributes of user [%s] on behalf of %"PRIi64"\n",
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina req = ifp_user_get_attr_send(ireq, ifp_ctx->rctx,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina tevent_req_set_callback(req, ifp_user_get_attr_process, attr_req);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinaifp_user_get_attr_unpack_msg(struct ifp_attr_req *attr_req)
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina const char **whitelist = attr_req->ireq->ifp_ctx->user_whitelist;
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina parsed = sbus_request_parse_or_finish(attr_req->ireq->dbus_req,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina if (parsed == false) {
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina DEBUG(SSSDBG_OP_FAILURE, "Could not parse arguments\n");
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina /* Copy the attributes to maintain memory hierarchy with talloc */
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina attr_req->attrs = talloc_zero_array(attr_req, const char *, nattrs+1);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina for (i = 0; i < nattrs; i++) {
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina if (ifp_attr_allowed(whitelist, attrs[i]) == false) {
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina "Attribute %s not present in the whitelist, skipping\n",
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina attr_req->attrs[ai] = talloc_strdup(attr_req->attrs, attrs[i]);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinastatic void ifp_user_get_attr_process(struct tevent_req *req)
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina attr_req = tevent_req_callback_data(req, struct ifp_attr_req);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina ret = ifp_user_get_attr_recv(attr_req, req, &res, &dom);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina sbus_request_fail_and_finish(attr_req->ireq->dbus_req,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina "No such user\n"));
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina sbus_request_fail_and_finish(attr_req->ireq->dbus_req,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina "Failed to read user attribute\n"));
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina ret = ifp_user_get_attr_handle_reply(dom, attr_req->ireq,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina sbus_request_fail_and_finish(attr_req->ireq->dbus_req,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina "Failed to build a reply\n"));
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinaifp_user_get_attr_replace_space(TALLOC_CTX *mem_ctx,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina el->values[i].data = (uint8_t *) sss_replace_space(mem_ctx,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "sss_replace_space failed, skipping\n");
383840c0c9c440710352076f844a64745121d251Pavel Březinaifp_user_get_attr_handle_reply(struct sss_domain_info *domain,
383840c0c9c440710352076f844a64745121d251Pavel Březina const char **attrs,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina /* Construct a reply */
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina reply = dbus_message_new_method_return(ireq->dbus_req->message);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina return sbus_request_finish(ireq->dbus_req, NULL);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina return sbus_request_finish(ireq->dbus_req, NULL);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina el = sss_view_ldb_msg_find_element(domain, res->msgs[0], attrs[ai]);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina "Attribute %s not present or has no values\n",
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina /* Normalize white space in user names */
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina if (ireq->ifp_ctx->rctx->override_space != '\0' &&
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina ret = ifp_user_get_attr_replace_space(ireq, el,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina DEBUG(SSSDBG_MINOR_FAILURE, "Cannot normalize %s\n",
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina "Cannot add attribute %s to message\n",
4dd38025efda88f123eac672f87d3cda12f050c8Jakub Hrozek dbret = dbus_message_iter_close_container(&iter, &iter_dict);
4dd38025efda88f123eac672f87d3cda12f050c8Jakub Hrozek return sbus_request_finish(ireq->dbus_req, NULL);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina return sbus_request_finish(ireq->dbus_req, reply);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinastatic void ifp_user_get_groups_process(struct tevent_req *req);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinastatic errno_t ifp_user_get_groups_reply(struct sss_domain_info *domain,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinaint ifp_user_get_groups(struct sbus_request *dbus_req,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina ifp_ctx = talloc_get_type(data, struct ifp_ctx);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Invalid pointer!\n");
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina return sbus_request_return_and_finish(dbus_req, DBUS_TYPE_INVALID);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina ret = ifp_req_create(dbus_req, ifp_ctx, &ireq);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina return ifp_req_create_handle_failure(dbus_req, ret);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina group_req = talloc_zero(ireq, struct ifp_attr_req);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina group_req->attrs = talloc_zero_array(group_req, const char *, 2);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina group_req->attrs[0] = talloc_strdup(group_req->attrs, SYSDB_MEMBEROF);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina "Looking up groups of user [%s] on behalf of %"PRIi64"\n",
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina group_req->name, group_req->ireq->dbus_req->client);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina req = ifp_user_get_attr_send(ireq, ifp_ctx->rctx,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina tevent_req_set_callback(req, ifp_user_get_groups_process, group_req);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinastatic void ifp_user_get_groups_process(struct tevent_req *req)
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina group_req = tevent_req_callback_data(req, struct ifp_attr_req);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina ret = ifp_user_get_attr_recv(group_req, req, &res, &dom);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina sbus_request_fail_and_finish(group_req->ireq->dbus_req,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina "No such user\n"));
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina sbus_request_fail_and_finish(group_req->ireq->dbus_req,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina "Failed to read attribute\n"));
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina ret = ifp_user_get_groups_reply(dom, group_req->ireq, res);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina sbus_request_fail_and_finish(group_req->ireq->dbus_req,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina "Failed to build a reply\n"));
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinaifp_user_get_groups_reply(struct sss_domain_info *domain,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina /* one less, the first one is the user entry */
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina groupnames = talloc_zero_array(ireq, const char *, num);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina return sbus_request_finish(ireq->dbus_req, NULL);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina for (i = 0; i < num; i++) {
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina name = sss_view_ldb_msg_find_attr_as_string(domain,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina DEBUG(SSSDBG_MINOR_FAILURE, "Skipping a group with no name\n");
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina if (ireq->ifp_ctx->rctx->override_space != '\0') {
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina DEBUG(SSSDBG_MINOR_FAILURE, "Cannot normalize %s\n", name);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina groupnames[i] = sss_get_cased_name(groupnames, tmpstr,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina "sss_get_cased_name failed, skipping\n");
999d6066c7a96f102b692d31435d76114478e874Jakub Hrozek DEBUG(SSSDBG_TRACE_FUNC, "Adding group %s\n", groupnames[i]);
999d6066c7a96f102b692d31435d76114478e874Jakub Hrozek return infopipe_iface_GetUserGroups_finish(ireq->dbus_req,
999d6066c7a96f102b692d31435d76114478e874Jakub Hrozek const char *inp;
999d6066c7a96f102b692d31435d76114478e874Jakub Hrozek const char **attrs;
999d6066c7a96f102b692d31435d76114478e874Jakub Hrozekstatic void ifp_user_get_attr_lookup(struct tevent_req *subreq);
999d6066c7a96f102b692d31435d76114478e874Jakub Hrozekstatic void ifp_user_get_attr_done(struct tevent_req *subreq);
999d6066c7a96f102b692d31435d76114478e874Jakub Hrozekstatic struct tevent_req *
999d6066c7a96f102b692d31435d76114478e874Jakub Hrozekifp_user_get_attr_send(TALLOC_CTX *mem_ctx, struct resp_ctx *rctx,
999d6066c7a96f102b692d31435d76114478e874Jakub Hrozek req = tevent_req_create(mem_ctx, &state, struct ifp_user_get_attr_state);
d9065da2a103fe88b548904bb215c9bb1f21afa2Lukas Slebodnik subreq = sss_parse_inp_send(req, rctx, inp);
d9065da2a103fe88b548904bb215c9bb1f21afa2Lukas Slebodnik tevent_req_set_callback(subreq, ifp_user_get_attr_lookup, req);
d9065da2a103fe88b548904bb215c9bb1f21afa2Lukas Slebodnikifp_user_get_attr_lookup(struct tevent_req *subreq)
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina req = tevent_req_callback_data(subreq, struct tevent_req);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina state = tevent_req_data(req, struct ifp_user_get_attr_state);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina ret = sss_parse_inp_recv(subreq, state, &state->name, &state->domname);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina subreq = cache_req_send(state, state->rctx->ev, state->rctx,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina state->search_type, state->domname, state->name);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina tevent_req_set_callback(subreq, ifp_user_get_attr_done, req);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinastatic void ifp_user_get_attr_done(struct tevent_req *subreq)
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina req = tevent_req_callback_data(subreq, struct tevent_req);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina state = tevent_req_data(req, struct ifp_user_get_attr_state);
999d6066c7a96f102b692d31435d76114478e874Jakub Hrozek ret = cache_req_recv(state, subreq, &state->res, &state->dom);
999d6066c7a96f102b692d31435d76114478e874Jakub Hrozek /* throw away the result and perform attr search */
999d6066c7a96f102b692d31435d76114478e874Jakub Hrozek ret = sysdb_get_user_attr_with_views(state, state->dom, state->name,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "sysdb_get_user_attr_with_views() "
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "sysdb_get_user_attr_with_views() "
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina "returned more than one result!\n");
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina struct ifp_user_get_attr_state *state = tevent_req_data(req,
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina /* Did the request end with success but with no data? */
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinastruct cli_protocol_version *register_cli_protocol_version(void)
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina static struct cli_protocol_version ssh_cli_protocol_version[] = {
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina/* This is a throwaway method to ease the review of the patch.
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina * It will be removed later */
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březinaint ifp_ping(struct sbus_request *dbus_req, void *data)
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina struct ifp_ctx *ifp_ctx = talloc_get_type(data, struct ifp_ctx);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Invalid pointer!\n");
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina return sbus_request_return_and_finish(dbus_req, DBUS_TYPE_INVALID);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina ret = ifp_req_create(dbus_req, ifp_ctx, &ifp_req);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina return ifp_req_create_handle_failure(dbus_req, ret);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina DEBUG(SSSDBG_CONF_SETTINGS, "Got request for [%s]\n", request);
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina "Ping() only accepts ping as a param\n");
ce35bb272d25926b8fa0f9450c8b74064f25c816Pavel Březina return sbus_request_fail_and_finish(dbus_req, &dberr);