13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg/*
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg Authors:
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg Pavel Březina <pbrezina@redhat.com>
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg Copyright (C) 2016 Red Hat
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg This program is free software; you can redistribute it and/or modify
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg it under the terms of the GNU General Public License as published by
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg the Free Software Foundation; either version 3 of the License, or
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg (at your option) any later version.
0662ed52e814f8f08ef0e09956413a792584eddffuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg This program is distributed in the hope that it will be useful,
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg but WITHOUT ANY WARRANTY; without even the implied warranty of
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg GNU General Public License for more details.
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg You should have received a copy of the GNU General Public License
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg along with this program. If not, see <http://www.gnu.org/licenses/>.
16b55a35cff91315d261d1baa776138af465c4e4fuankg*/
16b55a35cff91315d261d1baa776138af465c4e4fuankg
1deefaad8846266c903a2622d2d5f9df9ef91ad1fuankg#include <stdio.h>
1deefaad8846266c903a2622d2d5f9df9ef91ad1fuankg#include <string.h>
16b55a35cff91315d261d1baa776138af465c4e4fuankg#include <talloc.h>
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg#include "util/util.h"
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg#include "tools/sssctl/sssctl.h"
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg#define ERR_SSSD _("Check that SSSD is running and " \
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg "the InfoPipe responder is enabled. " \
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg "Make sure 'ifp' is listed in the " \
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg "'services' option in sssd.conf.\n")
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankgstruct sssctl_sifp_data {
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg sss_sifp_ctx *sifp;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg};
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankgstatic int sssctl_sifp_data_destructor(struct sssctl_sifp_data *ctx)
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg{
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg if (ctx->sifp != NULL) {
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg sss_sifp_free(&ctx->sifp);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg }
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg return 0;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg}
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankgstatic void *sssctl_sifp_talloc(size_t size, void *pvt)
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg{
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg return talloc_size(pvt, size);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg}
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankgstatic void sssctl_sifp_talloc_free(void *ptr, void *pvt)
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg{
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg talloc_free(ptr);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg}
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankgsss_sifp_error sssctl_sifp_init(struct sss_tool_ctx *tool_ctx,
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg sss_sifp_ctx **_sifp)
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg{
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg struct sssctl_sifp_data *sifp_data;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg sss_sifp_error error;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg sifp_data = talloc_zero(tool_ctx, struct sssctl_sifp_data);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg if (sifp_data == NULL) {
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg return SSS_SIFP_OUT_OF_MEMORY;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg }
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg error = sss_sifp_init_ex(sifp_data, sssctl_sifp_talloc,
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg sssctl_sifp_talloc_free, &sifp_data->sifp);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg if (error != SSS_SIFP_OK) {
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg *_sifp = sifp_data->sifp;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg return error;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg }
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg talloc_set_destructor(sifp_data, sssctl_sifp_data_destructor);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg *_sifp = sifp_data->sifp;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg return SSS_SIFP_OK;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg}
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankgvoid _sssctl_sifp_error(sss_sifp_ctx *sifp,
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg sss_sifp_error error,
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg const char *message)
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg{
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg const char *dbus_code;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg const char *dbus_msg;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg const char *sifp_msg;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg sifp_msg = sss_sifp_strerr(error);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg switch (error) {
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg case SSS_SIFP_OK:
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg break;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg case SSS_SIFP_IO_ERROR:
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg dbus_code = sss_sifp_get_last_io_error_name(sifp);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg dbus_msg = sss_sifp_get_last_io_error_message(sifp);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg fprintf(stderr, "%s [%d]: %s\n", message, error, sifp_msg);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg fprintf(stderr, "%s: %s\n", dbus_code, dbus_msg);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg if (strcmp(dbus_code, DBUS_ERROR_SERVICE_UNKNOWN) == 0) {
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg fprintf(stderr, ERR_SSSD);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg break;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg }
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg if (strcmp(dbus_code, DBUS_ERROR_SPAWN_CHILD_EXITED) == 0) {
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg fprintf(stderr, ERR_SSSD);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg break;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg }
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
0662ed52e814f8f08ef0e09956413a792584eddffuankg if (strcmp(dbus_code, DBUS_ERROR_NO_REPLY) == 0) {
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg fprintf(stderr, ERR_SSSD);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg break;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg }
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg break;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg default:
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg fprintf(stderr, "%s [%d]: %s\n", message, error, sifp_msg);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg break;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg }
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg}
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankgsss_sifp_error _sssctl_sifp_send(TALLOC_CTX *mem_ctx,
0662ed52e814f8f08ef0e09956413a792584eddffuankg sss_sifp_ctx *sifp,
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg DBusMessage **_reply,
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg const char *path,
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg const char *iface,
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg const char *method,
0662ed52e814f8f08ef0e09956413a792584eddffuankg int first_arg_type,
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg ...)
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg{
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg sss_sifp_error error;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg DBusMessage *msg;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg dbus_bool_t bret;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg errno_t ret;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg va_list va;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg msg = sss_sifp_create_message(path, iface, method);
0662ed52e814f8f08ef0e09956413a792584eddffuankg if (msg == NULL) {
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create D-Bus message\n");
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg return SSS_SIFP_OUT_OF_MEMORY;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg }
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg va_start(va, first_arg_type);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg bret = dbus_message_append_args_valist(msg, first_arg_type, va);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg va_end(va);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg if (!bret) {
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg DEBUG(SSSDBG_CRIT_FAILURE, "Failed to build message\n");
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg error = SSS_SIFP_OUT_OF_MEMORY;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg goto done;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg }
fd7c287b8fcccc3e1e7834bf8daf8fe7fd6cd49dfuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg error = sss_sifp_send_message(sifp, msg, _reply);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg if (error != SSS_SIFP_OK) {
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg goto done;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg }
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg ret = sbus_talloc_bound_message(mem_ctx, *_reply);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg if (ret != EOK) {
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg error = SSS_SIFP_OUT_OF_MEMORY;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg goto done;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg }
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankgdone:
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg dbus_message_unref(msg);
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg return error;
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg}
13b501825bce68e7e49b4bc775da93e38d9bd9f3fuankg