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