19#include <sys/socket.h>
32#include <gnutls/gnutls.h>
35#define TLS_HANDSHAKE_TIMEOUT 5
41typedef struct cib_remote_opaque_s {
55cib_remote_perform_op(
cib_t *cib,
const char *op,
const char *
host,
56 const char *section, xmlNode *
data,
57 xmlNode **output_data,
int call_options,
58 const char *user_name)
61 int remaining_time = 0;
64 xmlNode *op_msg = NULL;
65 xmlNode *op_reply = NULL;
73 if (output_data != NULL) {
78 crm_err(
"No operation specified");
94 crm_trace(
"Sending %s message to the CIB manager", op);
110 crm_trace(
"Waiting for a synchronous reply");
112 start_time = time(NULL);
116 while (remaining_time > 0 && (rc != ENOTCONN)) {
121 remaining_time * 1000);
130 if (reply_id == msg_id) {
133 }
else if (reply_id < msg_id) {
134 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
137 }
else if ((reply_id - 10000) > msg_id) {
139 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
142 crm_err(
"Received a __future__ reply:" " %d (wanted %d)", reply_id, msg_id);
149 remaining_time = time(NULL) - start_time;
152 if (rc == ENOTCONN) {
153 crm_err(
"Disconnected while waiting for reply.");
155 }
else if (op_reply == NULL) {
156 crm_err(
"No reply message - empty");
172 if (rc ==
pcmk_ok || rc == -EPERM) {
180 if (output_data == NULL) {
201cib_remote_callback_dispatch(gpointer user_data)
204 cib_t *cib = user_data;
208 const char *
type = NULL;
214 if (private->start_time == 0) {
215 private->start_time = time(NULL);
226 if (time(NULL) >= private->start_time + private->timeout_sec) {
227 crm_info(
"Error reading from CIB manager connection: %s",
237 crm_info(
"Error reading from CIB manager connection: %s",
245 private->start_time = 0;
262 private->start_time = 0;
267cib_remote_command_dispatch(gpointer user_data)
270 cib_t *cib = user_data;
274 if (private->start_time == 0) {
275 private->start_time = time(NULL);
281 if (time(NULL) >= private->start_time + private->timeout_sec) {
282 crm_info(
"Error reading from CIB manager connection: %s",
291 free(private->command.buffer);
292 private->command.buffer = NULL;
293 crm_err(
"received late reply for remote cib connection, discarding");
296 crm_info(
"Error reading from CIB manager connection: %s",
301 private->start_time = 0;
306cib_tls_close(
cib_t *cib)
310 if (private->encrypted) {
311 if (private->command.tls_session) {
312 gnutls_bye(private->command.tls_session, GNUTLS_SHUT_RDWR);
313 gnutls_deinit(private->command.tls_session);
316 if (private->callback.tls_session) {
317 gnutls_bye(private->callback.tls_session, GNUTLS_SHUT_RDWR);
318 gnutls_deinit(private->callback.tls_session);
321 private->command.tls_session = NULL;
322 private->callback.tls_session = NULL;
327 if (private->command.tcp_socket >= 0) {
328 shutdown(private->command.tcp_socket, SHUT_RDWR);
329 close(private->command.tcp_socket);
331 if (private->callback.tcp_socket >= 0) {
332 shutdown(private->callback.tcp_socket, SHUT_RDWR);
333 close(private->callback.tcp_socket);
335 private->command.tcp_socket = -1;
336 private->callback.tcp_socket = -1;
338 free(private->command.buffer);
339 free(private->callback.buffer);
340 private->command.buffer = NULL;
341 private->callback.buffer = NULL;
347cib_remote_connection_destroy(gpointer user_data)
349 crm_err(
"Connection destroyed");
350 cib_tls_close(user_data);
359 xmlNode *answer = NULL;
360 xmlNode *login = NULL;
365 event_channel ? cib_remote_callback_dispatch : cib_remote_command_dispatch;
366 cib_fd_callbacks.
destroy = cib_remote_connection_destroy;
373 crm_info(
"Remote connection to %s:%d failed: %s " QB_XS
" rc=%d",
374 private->server, private->port,
pcmk_rc_str(rc), rc);
378 if (private->encrypted) {
380 int tls_rc = GNUTLS_E_SUCCESS;
382 rc =
pcmk__init_tls(&tls,
false, use_cert ? GNUTLS_CRD_CERTIFICATE : GNUTLS_CRD_ANON);
397 crm_err(
"Remote CIB session creation for %s:%d failed: %s",
398 private->server, private->port,
399 (rc == EPROTO)? gnutls_strerror(tls_rc) :
pcmk_rc_str(rc));
432 if (answer == NULL) {
442 crm_err(
"Invalid registration message: %s", msg_type);
445 }
else if (tmp_ticket == NULL) {
449 connection->
token = strdup(tmp_ticket);
460 crm_trace(
"remote client connection established");
461 private->timeout_sec = 60;
478 if (private->passwd == NULL) {
479 if (private->out == NULL) {
485 private->out->prompt(
"Password",
false, &(private->passwd));
489 if (private->server == NULL || private->user == NULL) {
494 rc = cib_tls_signon(cib, &(private->command), FALSE);
499 rc = cib_tls_signon(cib, &(private->callback), TRUE);
503 crm_info(
"Opened connection to %s:%d for %s",
504 private->server, private->port,
name);
509 crm_info(
"Connection to %s:%d for %s failed: %s\n",
517cib_remote_signoff(
cib_t *cib)
521 crm_debug(
"Disconnecting from the CIB manager");
532cib_remote_free(
cib_t *cib)
538 rc = cib_remote_signoff(cib);
542 free(private->server);
544 free(private->passwd);
556cib_remote_register_notification(
cib_t * cib,
const char *callback,
int enabled)
570cib_remote_set_connection_dnotify(
cib_t * cib,
void (*dnotify) (gpointer user_data))
572 return -EPROTONOSUPPORT;
593cib_remote_client_id(
const cib_t *cib,
const char **async_id,
594 const char **sync_id)
598 if (async_id != NULL) {
600 *async_id =
private->callback.token;
602 if (sync_id != NULL) {
604 *sync_id =
private->command.token;
610cib_remote_new(
const char *server,
const char *user,
const char *passwd,
int port,
622 if (
private == NULL) {
633 private->port = port;
634 private->encrypted = encrypted;
int cib__extend_transaction(cib_t *cib, xmlNode *request)
void cib_native_callback(cib_t *cib, xmlNode *msg, int call_id, int rc)
cib_t * cib_new_variant(void)
int cib__create_op(cib_t *cib, const char *op, const char *host, const char *section, xmlNode *data, int call_options, const char *user_name, const char *client_name, xmlNode **op_msg)
void cib_native_notify(gpointer data, gpointer user_data)
cib_t * cib_remote_new(const char *server, const char *user, const char *passwd, int port, gboolean encrypted)
struct cib_remote_opaque_s cib_remote_opaque_t
void cib__set_output(cib_t *cib, pcmk__output_t *out)
#define TLS_HANDSHAKE_TIMEOUT
@ cib_transaction
Process request when the client commits the active transaction.
int pcmk__read_available_remote_data(pcmk__remote_t *remote)
int pcmk__remote_send_xml(pcmk__remote_t *remote, const xmlNode *msg)
int pcmk__connect_remote(const char *host, int port, int timeout_ms, int *timer_id, int *sock_fd, void *userdata, void(*callback)(void *userdata, int rc, int sock))
xmlNode * pcmk__remote_message_xml(pcmk__remote_t *remote)
int pcmk__read_remote_message(pcmk__remote_t *remote, int timeout_ms)
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
enum pcmk_ipc_server type
#define crm_info(fmt, args...)
#define crm_warn(fmt, args...)
#define crm_log_xml_debug(xml, text)
#define crm_debug(fmt, args...)
#define crm_err(fmt, args...)
#define crm_log_xml_trace(xml, text)
#define crm_log_xml_warn(xml, text)
#define crm_trace(fmt, args...)
Wrappers for and extensions to glib mainloop.
mainloop_io_t * mainloop_add_fd(const char *name, int priority, int fd, void *userdata, struct mainloop_fd_callbacks *callbacks)
#define PCMK__VALUE_PASSWORD
#define PCMK__VALUE_CIB_NOTIFY
Formatted output for pacemaker tools.
void void void void void pcmk__text_prompt(const char *prompt, bool echo, char **dest)
const char * pcmk_strerror(int rc)
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
#define pcmk_err_diff_resync
#define pcmk__str_copy(str)
int(* set_connection_dnotify)(cib_t *cib, void(*dnotify)(gpointer user_data))
int(* signoff)(cib_t *cib)
int(* end_transaction)(cib_t *cib, bool commit, int call_options)
End and optionally commit this client's CIB transaction.
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
int(* client_id)(const cib_t *cib, const char **async_id, const char **sync_id)
Get the given CIB connection's unique client identifier(s)
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
cib_api_operations_t * cmds
int(* dispatch)(gpointer userdata)
Dispatch function for mainloop file descriptor with data ready.
void(* destroy)(gpointer userdata)
Destroy function for mainloop file descriptor client data.
This structure contains everything that makes up a single output formatter.
gnutls_session_t tls_session
void pcmk__free_tls(pcmk__tls_t *tls)
bool pcmk__x509_enabled(void)
int pcmk__init_tls(pcmk__tls_t **tls, bool server, gnutls_credentials_type_t cred_type)
int pcmk__tls_client_handshake(pcmk__remote_t *remote, int timeout_sec, int *gnutls_rc)
void pcmk__tls_check_cert_expiration(gnutls_session_t session)
gnutls_session_t pcmk__new_tls_session(pcmk__tls_t *tls, int csock)
Wrappers for and extensions to libxml2.
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
Create an XML attribute with specified name and integer value.
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
xmlNode * pcmk__xe_first_child(const xmlNode *parent, const char *node_name, const char *attr_n, const char *attr_v)
xmlNode * pcmk__xe_create(xmlNode *parent, const char *name)
xmlNode * pcmk__xml_copy(xmlNode *parent, xmlNode *src)
void pcmk__xml_free(xmlNode *xml)
#define PCMK__XA_CIB_NOTIFY_ACTIVATE
#define PCMK__XA_CIB_NOTIFY_TYPE
#define PCMK__XA_CIB_CALLID
#define PCMK__XA_CIB_CLIENTID
#define PCMK__XA_PASSWORD
#define PCMK__XE_CIB_COMMAND
#define PCMK__XE_CIB_CALLDATA