dhodovsk / source-git / pacemaker

Forked from source-git/pacemaker 3 years ago
Clone

Blame lib/cib/cib_remote.c

rpm-build 3ee90c
/*
rpm-build 3ee90c
 * Copyright 2008-2018 Andrew Beekhof <andrew@beekhof.net>
rpm-build 3ee90c
 *
rpm-build 3ee90c
 * This source code is licensed under the GNU Lesser General Public License
rpm-build 3ee90c
 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
rpm-build 3ee90c
 */
rpm-build 3ee90c
rpm-build 3ee90c
#include <crm_internal.h>
rpm-build 3ee90c
rpm-build 3ee90c
#include <unistd.h>
rpm-build 3ee90c
#include <stdlib.h>
rpm-build 3ee90c
#include <stdio.h>
rpm-build 3ee90c
#include <stdarg.h>
rpm-build 3ee90c
#include <string.h>
rpm-build 3ee90c
#include <netdb.h>
rpm-build 3ee90c
#include <termios.h>
rpm-build 3ee90c
#include <sys/socket.h>
rpm-build 3ee90c
rpm-build 3ee90c
#include <glib.h>
rpm-build 3ee90c
rpm-build 3ee90c
#include <crm/crm.h>
rpm-build 3ee90c
#include <crm/cib/internal.h>
rpm-build 3ee90c
#include <crm/msg_xml.h>
rpm-build 3ee90c
#include <crm/common/ipcs.h>
rpm-build 3ee90c
#include <crm/common/mainloop.h>
rpm-build 3ee90c
#include <crm/common/remote_internal.h>
rpm-build 3ee90c
rpm-build 3ee90c
#ifdef HAVE_GNUTLS_GNUTLS_H
rpm-build 3ee90c
#  undef KEYFILE
rpm-build 3ee90c
#  include <gnutls/gnutls.h>
rpm-build 3ee90c
gnutls_anon_client_credentials_t anon_cred_c;
rpm-build 3ee90c
rpm-build 3ee90c
#  define DEFAULT_CLIENT_HANDSHAKE_TIMEOUT 5000 /* 5 seconds */
rpm-build 3ee90c
rpm-build 3ee90c
const int kx_prio[] = {
rpm-build 3ee90c
    GNUTLS_KX_ANON_DH,
rpm-build 3ee90c
    0
rpm-build 3ee90c
};
rpm-build 3ee90c
rpm-build 3ee90c
static gboolean remote_gnutls_credentials_init = FALSE;
rpm-build 3ee90c
#else
rpm-build 3ee90c
typedef void gnutls_session_t;
rpm-build 3ee90c
#endif
rpm-build 3ee90c
rpm-build 3ee90c
#include <arpa/inet.h>
rpm-build 3ee90c
#ifndef ON_BSD
rpm-build 3ee90c
#  include <sgtty.h>
rpm-build 3ee90c
#endif
rpm-build 3ee90c
rpm-build 3ee90c
#define DH_BITS 1024
rpm-build 3ee90c
rpm-build 3ee90c
typedef struct cib_remote_opaque_s {
rpm-build 3ee90c
    int flags;
rpm-build 3ee90c
    int socket;
rpm-build 3ee90c
    int port;
rpm-build 3ee90c
    char *server;
rpm-build 3ee90c
    char *user;
rpm-build 3ee90c
    char *passwd;
rpm-build 3ee90c
    gboolean encrypted;
rpm-build 3ee90c
    crm_remote_t command;
rpm-build 3ee90c
    crm_remote_t callback;
rpm-build 3ee90c
rpm-build 3ee90c
} cib_remote_opaque_t;
rpm-build 3ee90c
rpm-build 3ee90c
void cib_remote_connection_destroy(gpointer user_data);
rpm-build 3ee90c
int cib_remote_callback_dispatch(gpointer user_data);
rpm-build 3ee90c
int cib_remote_command_dispatch(gpointer user_data);
rpm-build 3ee90c
int cib_remote_signon(cib_t * cib, const char *name, enum cib_conn_type type);
rpm-build 3ee90c
int cib_remote_signoff(cib_t * cib);
rpm-build 3ee90c
int cib_remote_free(cib_t * cib);
rpm-build 3ee90c
rpm-build 3ee90c
int cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char *section,
rpm-build 3ee90c
                          xmlNode * data, xmlNode ** output_data, int call_options,
rpm-build 3ee90c
                          const char *name);
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
cib_remote_inputfd(cib_t * cib)
rpm-build 3ee90c
{
rpm-build 3ee90c
    cib_remote_opaque_t *private = cib->variant_opaque;
rpm-build 3ee90c
rpm-build 3ee90c
    return private->callback.tcp_socket;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
cib_remote_set_connection_dnotify(cib_t * cib, void (*dnotify) (gpointer user_data))
rpm-build 3ee90c
{
rpm-build 3ee90c
    return -EPROTONOSUPPORT;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
cib_remote_register_notification(cib_t * cib, const char *callback, int enabled)
rpm-build 3ee90c
{
rpm-build 3ee90c
    xmlNode *notify_msg = create_xml_node(NULL, "cib_command");
rpm-build 3ee90c
    cib_remote_opaque_t *private = cib->variant_opaque;
rpm-build 3ee90c
rpm-build 3ee90c
    crm_xml_add(notify_msg, F_CIB_OPERATION, T_CIB_NOTIFY);
rpm-build 3ee90c
    crm_xml_add(notify_msg, F_CIB_NOTIFY_TYPE, callback);
rpm-build 3ee90c
    crm_xml_add_int(notify_msg, F_CIB_NOTIFY_ACTIVATE, enabled);
rpm-build 3ee90c
    crm_remote_send(&private->callback, notify_msg);
rpm-build 3ee90c
    free_xml(notify_msg);
rpm-build 3ee90c
    return pcmk_ok;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
cib_t *
rpm-build 3ee90c
cib_remote_new(const char *server, const char *user, const char *passwd, int port,
rpm-build 3ee90c
               gboolean encrypted)
rpm-build 3ee90c
{
rpm-build 3ee90c
    cib_remote_opaque_t *private = NULL;
rpm-build 3ee90c
    cib_t *cib = cib_new_variant();
rpm-build 3ee90c
rpm-build 3ee90c
    private = calloc(1, sizeof(cib_remote_opaque_t));
rpm-build 3ee90c
rpm-build 3ee90c
    cib->variant = cib_remote;
rpm-build 3ee90c
    cib->variant_opaque = private;
rpm-build 3ee90c
rpm-build 3ee90c
    if (server) {
rpm-build 3ee90c
        private->server = strdup(server);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (user) {
rpm-build 3ee90c
        private->user = strdup(user);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (passwd) {
rpm-build 3ee90c
        private->passwd = strdup(passwd);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    private->port = port;
rpm-build 3ee90c
    private->encrypted = encrypted;
rpm-build 3ee90c
rpm-build 3ee90c
    /* assign variant specific ops */
rpm-build 3ee90c
    cib->delegate_fn = cib_remote_perform_op;
rpm-build 3ee90c
    cib->cmds->signon = cib_remote_signon;
rpm-build 3ee90c
    cib->cmds->signoff = cib_remote_signoff;
rpm-build 3ee90c
    cib->cmds->free = cib_remote_free;
rpm-build 3ee90c
    cib->cmds->inputfd = cib_remote_inputfd;
rpm-build 3ee90c
rpm-build 3ee90c
    cib->cmds->register_notification = cib_remote_register_notification;
rpm-build 3ee90c
    cib->cmds->set_connection_dnotify = cib_remote_set_connection_dnotify;
rpm-build 3ee90c
rpm-build 3ee90c
    return cib;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
cib_tls_close(cib_t * cib)
rpm-build 3ee90c
{
rpm-build 3ee90c
    cib_remote_opaque_t *private = cib->variant_opaque;
rpm-build 3ee90c
rpm-build 3ee90c
#ifdef HAVE_GNUTLS_GNUTLS_H
rpm-build 3ee90c
    if (private->encrypted) {
rpm-build 3ee90c
        if (private->command.tls_session) {
rpm-build 3ee90c
            gnutls_bye(*(private->command.tls_session), GNUTLS_SHUT_RDWR);
rpm-build 3ee90c
            gnutls_deinit(*(private->command.tls_session));
rpm-build 3ee90c
            gnutls_free(private->command.tls_session);
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        if (private->callback.tls_session) {
rpm-build 3ee90c
            gnutls_bye(*(private->callback.tls_session), GNUTLS_SHUT_RDWR);
rpm-build 3ee90c
            gnutls_deinit(*(private->callback.tls_session));
rpm-build 3ee90c
            gnutls_free(private->callback.tls_session);
rpm-build 3ee90c
        }
rpm-build 3ee90c
        private->command.tls_session = NULL;
rpm-build 3ee90c
        private->callback.tls_session = NULL;
rpm-build 3ee90c
        if (remote_gnutls_credentials_init) {
rpm-build 3ee90c
            gnutls_anon_free_client_credentials(anon_cred_c);
rpm-build 3ee90c
            gnutls_global_deinit();
rpm-build 3ee90c
            remote_gnutls_credentials_init = FALSE;
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
#endif
rpm-build 3ee90c
rpm-build 3ee90c
    if (private->command.tcp_socket) {
rpm-build 3ee90c
        shutdown(private->command.tcp_socket, SHUT_RDWR);       /* no more receptions */
rpm-build 3ee90c
        close(private->command.tcp_socket);
rpm-build 3ee90c
    }
rpm-build 3ee90c
    if (private->callback.tcp_socket) {
rpm-build 3ee90c
        shutdown(private->callback.tcp_socket, SHUT_RDWR);      /* no more receptions */
rpm-build 3ee90c
        close(private->callback.tcp_socket);
rpm-build 3ee90c
    }
rpm-build 3ee90c
    private->command.tcp_socket = 0;
rpm-build 3ee90c
    private->callback.tcp_socket = 0;
rpm-build 3ee90c
rpm-build 3ee90c
    free(private->command.buffer);
rpm-build 3ee90c
    free(private->callback.buffer);
rpm-build 3ee90c
    private->command.buffer = NULL;
rpm-build 3ee90c
    private->callback.buffer = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    return 0;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
static int
rpm-build 3ee90c
cib_tls_signon(cib_t * cib, crm_remote_t * connection, gboolean event_channel)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int sock;
rpm-build 3ee90c
    cib_remote_opaque_t *private = cib->variant_opaque;
rpm-build 3ee90c
    int rc = 0;
rpm-build 3ee90c
    int disconnected = 0;
rpm-build 3ee90c
rpm-build 3ee90c
    xmlNode *answer = NULL;
rpm-build 3ee90c
    xmlNode *login = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    static struct mainloop_fd_callbacks cib_fd_callbacks = { 0, };
rpm-build 3ee90c
rpm-build 3ee90c
    cib_fd_callbacks.dispatch =
rpm-build 3ee90c
        event_channel ? cib_remote_callback_dispatch : cib_remote_command_dispatch;
rpm-build 3ee90c
    cib_fd_callbacks.destroy = cib_remote_connection_destroy;
rpm-build 3ee90c
rpm-build 3ee90c
    connection->tcp_socket = 0;
rpm-build 3ee90c
#ifdef HAVE_GNUTLS_GNUTLS_H
rpm-build 3ee90c
    connection->tls_session = NULL;
rpm-build 3ee90c
#endif
rpm-build 3ee90c
    sock = crm_remote_tcp_connect(private->server, private->port);
rpm-build 3ee90c
    if (sock < 0) {
rpm-build 3ee90c
        crm_perror(LOG_ERR, "remote tcp connection to %s:%d failed", private->server,
rpm-build 3ee90c
                   private->port);
rpm-build 3ee90c
        return -ENOTCONN;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    connection->tcp_socket = sock;
rpm-build 3ee90c
rpm-build 3ee90c
    if (private->encrypted) {
rpm-build 3ee90c
        /* initialize GnuTls lib */
rpm-build 3ee90c
#ifdef HAVE_GNUTLS_GNUTLS_H
rpm-build 3ee90c
        if (remote_gnutls_credentials_init == FALSE) {
rpm-build 3ee90c
            crm_gnutls_global_init();
rpm-build 3ee90c
            gnutls_anon_allocate_client_credentials(&anon_cred_c);
rpm-build 3ee90c
            remote_gnutls_credentials_init = TRUE;
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        /* bind the socket to GnuTls lib */
rpm-build 3ee90c
        connection->tls_session = pcmk__new_tls_session(sock, GNUTLS_CLIENT,
rpm-build 3ee90c
                                                        GNUTLS_CRD_ANON,
rpm-build 3ee90c
                                                        anon_cred_c);
rpm-build 3ee90c
        if (connection->tls_session == NULL) {
rpm-build 3ee90c
            cib_tls_close(cib);
rpm-build 3ee90c
            return -1;
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        if (crm_initiate_client_tls_handshake(connection, DEFAULT_CLIENT_HANDSHAKE_TIMEOUT) != 0) {
rpm-build 3ee90c
            crm_err("Session creation for %s:%d failed", private->server, private->port);
rpm-build 3ee90c
rpm-build 3ee90c
            gnutls_deinit(*connection->tls_session);
rpm-build 3ee90c
            gnutls_free(connection->tls_session);
rpm-build 3ee90c
            connection->tls_session = NULL;
rpm-build 3ee90c
            cib_tls_close(cib);
rpm-build 3ee90c
            return -1;
rpm-build 3ee90c
        }
rpm-build 3ee90c
#else
rpm-build 3ee90c
        return -EPROTONOSUPPORT;
rpm-build 3ee90c
#endif
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    /* login to server */
rpm-build 3ee90c
    login = create_xml_node(NULL, "cib_command");
rpm-build 3ee90c
    crm_xml_add(login, "op", "authenticate");
rpm-build 3ee90c
    crm_xml_add(login, "user", private->user);
rpm-build 3ee90c
    crm_xml_add(login, "password", private->passwd);
rpm-build 3ee90c
    crm_xml_add(login, "hidden", "password");
rpm-build 3ee90c
rpm-build 3ee90c
    crm_remote_send(connection, login);
rpm-build 3ee90c
    free_xml(login);
rpm-build 3ee90c
rpm-build 3ee90c
    crm_remote_recv(connection, -1, &disconnected);
rpm-build 3ee90c
rpm-build 3ee90c
    if (disconnected) {
rpm-build 3ee90c
        rc = -ENOTCONN;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    answer = crm_remote_parse_buffer(connection);
rpm-build 3ee90c
rpm-build 3ee90c
    crm_log_xml_trace(answer, "Reply");
rpm-build 3ee90c
    if (answer == NULL) {
rpm-build 3ee90c
        rc = -EPROTO;
rpm-build 3ee90c
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        /* grab the token */
rpm-build 3ee90c
        const char *msg_type = crm_element_value(answer, F_CIB_OPERATION);
rpm-build 3ee90c
        const char *tmp_ticket = crm_element_value(answer, F_CIB_CLIENTID);
rpm-build 3ee90c
rpm-build 3ee90c
        if (safe_str_neq(msg_type, CRM_OP_REGISTER)) {
rpm-build 3ee90c
            crm_err("Invalid registration message: %s", msg_type);
rpm-build 3ee90c
            rc = -EPROTO;
rpm-build 3ee90c
rpm-build 3ee90c
        } else if (tmp_ticket == NULL) {
rpm-build 3ee90c
            rc = -EPROTO;
rpm-build 3ee90c
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            connection->token = strdup(tmp_ticket);
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
    free_xml(answer);
rpm-build 3ee90c
    answer = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    if (rc != 0) {
rpm-build 3ee90c
        cib_tls_close(cib);
rpm-build 3ee90c
        return rc;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    crm_trace("remote client connection established");
rpm-build 3ee90c
    connection->source =
rpm-build 3ee90c
        mainloop_add_fd("cib-remote", G_PRIORITY_HIGH, sock, cib,
rpm-build 3ee90c
                        &cib_fd_callbacks);
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
void
rpm-build 3ee90c
cib_remote_connection_destroy(gpointer user_data)
rpm-build 3ee90c
{
rpm-build 3ee90c
    crm_err("Connection destroyed");
rpm-build 3ee90c
#ifdef HAVE_GNUTLS_GNUTLS_H
rpm-build 3ee90c
    cib_tls_close(user_data);
rpm-build 3ee90c
#endif
rpm-build 3ee90c
    return;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
int
rpm-build 3ee90c
cib_remote_command_dispatch(gpointer user_data)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int disconnected = 0;
rpm-build 3ee90c
    cib_t *cib = user_data;
rpm-build 3ee90c
    cib_remote_opaque_t *private = cib->variant_opaque;
rpm-build 3ee90c
rpm-build 3ee90c
    crm_remote_recv(&private->command, -1, &disconnected);
rpm-build 3ee90c
rpm-build 3ee90c
    free(private->command.buffer);
rpm-build 3ee90c
    private->command.buffer = NULL;
rpm-build 3ee90c
    crm_err("received late reply for remote cib connection, discarding");
rpm-build 3ee90c
rpm-build 3ee90c
    if (disconnected) {
rpm-build 3ee90c
        return -1;
rpm-build 3ee90c
    }
rpm-build 3ee90c
    return 0;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
int
rpm-build 3ee90c
cib_remote_callback_dispatch(gpointer user_data)
rpm-build 3ee90c
{
rpm-build 3ee90c
    cib_t *cib = user_data;
rpm-build 3ee90c
    cib_remote_opaque_t *private = cib->variant_opaque;
rpm-build 3ee90c
rpm-build 3ee90c
    xmlNode *msg = NULL;
rpm-build 3ee90c
    int disconnected = 0;
rpm-build 3ee90c
rpm-build 3ee90c
    crm_info("Message on callback channel");
rpm-build 3ee90c
rpm-build 3ee90c
    crm_remote_recv(&private->callback, -1, &disconnected);
rpm-build 3ee90c
rpm-build 3ee90c
    msg = crm_remote_parse_buffer(&private->callback);
rpm-build 3ee90c
    while (msg) {
rpm-build 3ee90c
        const char *type = crm_element_value(msg, F_TYPE);
rpm-build 3ee90c
rpm-build 3ee90c
        crm_trace("Activating %s callbacks...", type);
rpm-build 3ee90c
rpm-build 3ee90c
        if (safe_str_eq(type, T_CIB)) {
rpm-build 3ee90c
            cib_native_callback(cib, msg, 0, 0);
rpm-build 3ee90c
rpm-build 3ee90c
        } else if (safe_str_eq(type, T_CIB_NOTIFY)) {
rpm-build 3ee90c
            g_list_foreach(cib->notify_list, cib_native_notify, msg);
rpm-build 3ee90c
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            crm_err("Unknown message type: %s", type);
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        free_xml(msg);
rpm-build 3ee90c
        msg = crm_remote_parse_buffer(&private->callback);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (disconnected) {
rpm-build 3ee90c
        return -1;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    return 0;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
int
rpm-build 3ee90c
cib_remote_signon(cib_t * cib, const char *name, enum cib_conn_type type)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = pcmk_ok;
rpm-build 3ee90c
    cib_remote_opaque_t *private = cib->variant_opaque;
rpm-build 3ee90c
rpm-build 3ee90c
    if (private->passwd == NULL) {
rpm-build 3ee90c
        struct termios settings;
rpm-build 3ee90c
rpm-build 3ee90c
        rc = tcgetattr(0, &settings);
rpm-build 3ee90c
        if(rc == 0) {
rpm-build 3ee90c
            settings.c_lflag &= ~ECHO;
rpm-build 3ee90c
            rc = tcsetattr(0, TCSANOW, &settings);
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        if(rc == 0) {
rpm-build 3ee90c
            fprintf(stderr, "Password: ");
rpm-build 3ee90c
            private->passwd = calloc(1, 1024);
rpm-build 3ee90c
            rc = scanf("%1023s", private->passwd);
rpm-build 3ee90c
            fprintf(stderr, "\n");
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        /* fprintf(stderr, "entered: '%s'\n", buffer); */
rpm-build 3ee90c
        if (rc < 1) {
rpm-build 3ee90c
            private->passwd = NULL;
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        settings.c_lflag |= ECHO;
rpm-build 3ee90c
        rc = tcsetattr(0, TCSANOW, &settings);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (private->server == NULL || private->user == NULL) {
rpm-build 3ee90c
        rc = -EINVAL;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (rc == pcmk_ok) {
rpm-build 3ee90c
        rc = cib_tls_signon(cib, &(private->command), FALSE);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (rc == pcmk_ok) {
rpm-build 3ee90c
        rc = cib_tls_signon(cib, &(private->callback), TRUE);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (rc == pcmk_ok) {
rpm-build 3ee90c
        xmlNode *hello =
rpm-build 3ee90c
            cib_create_op(0, private->callback.token, CRM_OP_REGISTER, NULL, NULL, NULL, 0, NULL);
rpm-build 3ee90c
        crm_xml_add(hello, F_CIB_CLIENTNAME, name);
rpm-build 3ee90c
        crm_remote_send(&private->command, hello);
rpm-build 3ee90c
        free_xml(hello);
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (rc == pcmk_ok) {
rpm-build 3ee90c
        crm_notice("%s: Opened connection to %s:%d", name, private->server, private->port);
rpm-build 3ee90c
        cib->state = cib_connected_command;
rpm-build 3ee90c
        cib->type = cib_command;
rpm-build 3ee90c
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        fprintf(stderr, "%s: Connection to %s:%d failed: %s\n",
rpm-build 3ee90c
                name, private->server, private->port, pcmk_strerror(rc));
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
int
rpm-build 3ee90c
cib_remote_signoff(cib_t * cib)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = pcmk_ok;
rpm-build 3ee90c
rpm-build 3ee90c
    /* cib_remote_opaque_t *private = cib->variant_opaque; */
rpm-build 3ee90c
rpm-build 3ee90c
    crm_debug("Disconnecting from the CIB manager");
rpm-build 3ee90c
#ifdef HAVE_GNUTLS_GNUTLS_H
rpm-build 3ee90c
    cib_tls_close(cib);
rpm-build 3ee90c
#endif
rpm-build 3ee90c
rpm-build 3ee90c
    cib->state = cib_disconnected;
rpm-build 3ee90c
    cib->type = cib_no_connection;
rpm-build 3ee90c
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
int
rpm-build 3ee90c
cib_remote_free(cib_t * cib)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = pcmk_ok;
rpm-build 3ee90c
rpm-build 3ee90c
    crm_warn("Freeing CIB");
rpm-build 3ee90c
    if (cib->state != cib_disconnected) {
rpm-build 3ee90c
        rc = cib_remote_signoff(cib);
rpm-build 3ee90c
        if (rc == pcmk_ok) {
rpm-build 3ee90c
            cib_remote_opaque_t *private = cib->variant_opaque;
rpm-build 3ee90c
rpm-build 3ee90c
            free(private->server);
rpm-build 3ee90c
            free(private->user);
rpm-build 3ee90c
            free(private->passwd);
rpm-build 3ee90c
            free(cib->cmds);
rpm-build 3ee90c
            free(private);
rpm-build 3ee90c
            free(cib);
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}
rpm-build 3ee90c
rpm-build 3ee90c
int
rpm-build 3ee90c
cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char *section,
rpm-build 3ee90c
                      xmlNode * data, xmlNode ** output_data, int call_options, const char *name)
rpm-build 3ee90c
{
rpm-build 3ee90c
    int rc = pcmk_ok;
rpm-build 3ee90c
    int disconnected = 0;
rpm-build 3ee90c
    int remaining_time = 0;
rpm-build 3ee90c
    time_t start_time;
rpm-build 3ee90c
rpm-build 3ee90c
    xmlNode *op_msg = NULL;
rpm-build 3ee90c
    xmlNode *op_reply = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
    cib_remote_opaque_t *private = cib->variant_opaque;
rpm-build 3ee90c
rpm-build 3ee90c
    if (cib->state == cib_disconnected) {
rpm-build 3ee90c
        return -ENOTCONN;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (output_data != NULL) {
rpm-build 3ee90c
        *output_data = NULL;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (op == NULL) {
rpm-build 3ee90c
        crm_err("No operation specified");
rpm-build 3ee90c
        return -EINVAL;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    cib->call_id++;
rpm-build 3ee90c
    /* prevent call_id from being negative (or zero) and conflicting
rpm-build 3ee90c
     *    with the cib_errors enum
rpm-build 3ee90c
     * use 2 because we use it as (cib->call_id - 1) below
rpm-build 3ee90c
     */
rpm-build 3ee90c
    if (cib->call_id < 1) {
rpm-build 3ee90c
        cib->call_id = 1;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    op_msg =
rpm-build 3ee90c
        cib_create_op(cib->call_id, private->callback.token, op, host, section, data, call_options,
rpm-build 3ee90c
                      NULL);
rpm-build 3ee90c
    if (op_msg == NULL) {
rpm-build 3ee90c
        return -EPROTO;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    crm_trace("Sending %s message to the CIB manager", op);
rpm-build 3ee90c
    if (!(call_options & cib_sync_call)) {
rpm-build 3ee90c
        crm_remote_send(&private->callback, op_msg);
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
        crm_remote_send(&private->command, op_msg);
rpm-build 3ee90c
    }
rpm-build 3ee90c
    free_xml(op_msg);
rpm-build 3ee90c
rpm-build 3ee90c
    if ((call_options & cib_discard_reply)) {
rpm-build 3ee90c
        crm_trace("Discarding reply");
rpm-build 3ee90c
        return pcmk_ok;
rpm-build 3ee90c
rpm-build 3ee90c
    } else if (!(call_options & cib_sync_call)) {
rpm-build 3ee90c
        return cib->call_id;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    crm_trace("Waiting for a synchronous reply");
rpm-build 3ee90c
rpm-build 3ee90c
    start_time = time(NULL);
rpm-build 3ee90c
    remaining_time = cib->call_timeout ? cib->call_timeout : 60;
rpm-build 3ee90c
rpm-build 3ee90c
    while (remaining_time > 0 && !disconnected) {
rpm-build 3ee90c
        int reply_id = -1;
rpm-build 3ee90c
        int msg_id = cib->call_id;
rpm-build 3ee90c
rpm-build 3ee90c
        crm_remote_recv(&private->command, remaining_time * 1000, &disconnected);
rpm-build 3ee90c
        op_reply = crm_remote_parse_buffer(&private->command);
rpm-build 3ee90c
rpm-build 3ee90c
        if (!op_reply) {
rpm-build 3ee90c
            break;
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        crm_element_value_int(op_reply, F_CIB_CALLID, &reply_id);
rpm-build 3ee90c
rpm-build 3ee90c
        if (reply_id == msg_id) {
rpm-build 3ee90c
            break;
rpm-build 3ee90c
rpm-build 3ee90c
        } else if (reply_id < msg_id) {
rpm-build 3ee90c
            crm_debug("Received old reply: %d (wanted %d)", reply_id, msg_id);
rpm-build 3ee90c
            crm_log_xml_trace(op_reply, "Old reply");
rpm-build 3ee90c
rpm-build 3ee90c
        } else if ((reply_id - 10000) > msg_id) {
rpm-build 3ee90c
            /* wrap-around case */
rpm-build 3ee90c
            crm_debug("Received old reply: %d (wanted %d)", reply_id, msg_id);
rpm-build 3ee90c
            crm_log_xml_trace(op_reply, "Old reply");
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            crm_err("Received a __future__ reply:" " %d (wanted %d)", reply_id, msg_id);
rpm-build 3ee90c
        }
rpm-build 3ee90c
rpm-build 3ee90c
        free_xml(op_reply);
rpm-build 3ee90c
        op_reply = NULL;
rpm-build 3ee90c
rpm-build 3ee90c
        /* wasn't the right reply, try and read some more */
rpm-build 3ee90c
        remaining_time = time(NULL) - start_time;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    /* if(IPC_ISRCONN(native->command_channel) == FALSE) { */
rpm-build 3ee90c
    /*      crm_err("The CIB manager disconnected: %d",  */
rpm-build 3ee90c
    /*              native->command_channel->ch_status); */
rpm-build 3ee90c
    /*      cib->state = cib_disconnected; */
rpm-build 3ee90c
    /* } */
rpm-build 3ee90c
rpm-build 3ee90c
    if (disconnected) {
rpm-build 3ee90c
        crm_err("Disconnected while waiting for reply.");
rpm-build 3ee90c
        return -ENOTCONN;
rpm-build 3ee90c
    } else if (op_reply == NULL) {
rpm-build 3ee90c
        crm_err("No reply message - empty");
rpm-build 3ee90c
        return -ENOMSG;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    crm_trace("Synchronous reply received");
rpm-build 3ee90c
rpm-build 3ee90c
    /* Start processing the reply... */
rpm-build 3ee90c
    if (crm_element_value_int(op_reply, F_CIB_RC, &rc) != 0) {
rpm-build 3ee90c
        rc = -EPROTO;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (rc == -pcmk_err_diff_resync) {
rpm-build 3ee90c
        /* This is an internal value that clients do not and should not care about */
rpm-build 3ee90c
        rc = pcmk_ok;
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (rc == pcmk_ok || rc == -EPERM) {
rpm-build 3ee90c
        crm_log_xml_debug(op_reply, "passed");
rpm-build 3ee90c
rpm-build 3ee90c
    } else {
rpm-build 3ee90c
/* 	} else if(rc == -ETIME) { */
rpm-build 3ee90c
        crm_err("Call failed: %s", pcmk_strerror(rc));
rpm-build 3ee90c
        crm_log_xml_warn(op_reply, "failed");
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    if (output_data == NULL) {
rpm-build 3ee90c
        /* do nothing more */
rpm-build 3ee90c
rpm-build 3ee90c
    } else if (!(call_options & cib_discard_reply)) {
rpm-build 3ee90c
        xmlNode *tmp = get_message_xml(op_reply, F_CIB_CALLDATA);
rpm-build 3ee90c
rpm-build 3ee90c
        if (tmp == NULL) {
rpm-build 3ee90c
            crm_trace("No output in reply to \"%s\" command %d", op, cib->call_id - 1);
rpm-build 3ee90c
        } else {
rpm-build 3ee90c
            *output_data = copy_xml(tmp);
rpm-build 3ee90c
        }
rpm-build 3ee90c
    }
rpm-build 3ee90c
rpm-build 3ee90c
    free_xml(op_reply);
rpm-build 3ee90c
rpm-build 3ee90c
    return rc;
rpm-build 3ee90c
}