19 #include <sys/socket.h> 31 #ifdef HAVE_GNUTLS_GNUTLS_H 33 # include <gnutls/gnutls.h> 35 # define TLS_HANDSHAKE_TIMEOUT_MS 5000 37 static gnutls_anon_client_credentials_t anon_cred_c;
38 static gboolean remote_gnutls_credentials_init = FALSE;
40 #endif // HAVE_GNUTLS_GNUTLS_H 42 #include <arpa/inet.h> 44 typedef struct cib_remote_opaque_s {
56 cib_remote_perform_op(
cib_t *cib,
const char *op,
const char *
host,
57 const char *section, xmlNode *
data,
58 xmlNode **output_data,
int call_options,
const char *
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");
93 crm_trace(
"Sending %s message to the CIB manager", op);
109 crm_trace(
"Waiting for a synchronous reply");
111 start_time = time(NULL);
115 while (remaining_time > 0 && (rc != ENOTCONN)) {
120 remaining_time * 1000);
129 if (reply_id == msg_id) {
132 }
else if (reply_id < msg_id) {
133 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
136 }
else if ((reply_id - 10000) > msg_id) {
138 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
141 crm_err(
"Received a __future__ reply:" " %d (wanted %d)", reply_id, msg_id);
148 remaining_time = time(NULL) - start_time;
157 if (rc == ENOTCONN) {
158 crm_err(
"Disconnected while waiting for reply.");
160 }
else if (op_reply == NULL) {
161 crm_err(
"No reply message - empty");
177 if (rc ==
pcmk_ok || rc == -EPERM) {
186 if (output_data == NULL) {
205 cib_remote_callback_dispatch(gpointer user_data)
208 cib_t *cib = user_data;
213 crm_info(
"Message on callback channel");
237 if (rc == ENOTCONN) {
245 cib_remote_command_dispatch(gpointer user_data)
248 cib_t *cib = user_data;
253 free(private->command.buffer);
254 private->command.buffer = NULL;
255 crm_err(
"received late reply for remote cib connection, discarding");
257 if (rc == ENOTCONN) {
264 cib_tls_close(
cib_t *cib)
268 #ifdef HAVE_GNUTLS_GNUTLS_H 269 if (private->encrypted) {
270 if (private->command.tls_session) {
271 gnutls_bye(*(private->command.tls_session), GNUTLS_SHUT_RDWR);
272 gnutls_deinit(*(private->command.tls_session));
273 gnutls_free(private->command.tls_session);
276 if (private->callback.tls_session) {
277 gnutls_bye(*(private->callback.tls_session), GNUTLS_SHUT_RDWR);
278 gnutls_deinit(*(private->callback.tls_session));
279 gnutls_free(private->callback.tls_session);
281 private->command.tls_session = NULL;
282 private->callback.tls_session = NULL;
283 if (remote_gnutls_credentials_init) {
284 gnutls_anon_free_client_credentials(anon_cred_c);
285 gnutls_global_deinit();
286 remote_gnutls_credentials_init = FALSE;
291 if (private->command.tcp_socket) {
292 shutdown(private->command.tcp_socket, SHUT_RDWR);
293 close(private->command.tcp_socket);
295 if (private->callback.tcp_socket) {
296 shutdown(private->callback.tcp_socket, SHUT_RDWR);
297 close(private->callback.tcp_socket);
299 private->command.tcp_socket = 0;
300 private->callback.tcp_socket = 0;
302 free(private->command.buffer);
303 free(private->callback.buffer);
304 private->command.buffer = NULL;
305 private->callback.buffer = NULL;
311 cib_remote_connection_destroy(gpointer user_data)
313 crm_err(
"Connection destroyed");
314 #ifdef HAVE_GNUTLS_GNUTLS_H 315 cib_tls_close(user_data);
325 xmlNode *answer = NULL;
326 xmlNode *login = NULL;
331 event_channel ? cib_remote_callback_dispatch : cib_remote_command_dispatch;
332 cib_fd_callbacks.
destroy = cib_remote_connection_destroy;
335 #ifdef HAVE_GNUTLS_GNUTLS_H 336 connection->tls_session = NULL;
341 crm_info(
"Remote connection to %s:%d failed: %s " CRM_XS " rc=%d",
342 private->server, private->port,
pcmk_rc_str(rc), rc);
346 if (private->encrypted) {
348 #ifdef HAVE_GNUTLS_GNUTLS_H 349 if (remote_gnutls_credentials_init == FALSE) {
350 crm_gnutls_global_init();
351 gnutls_anon_allocate_client_credentials(&anon_cred_c);
352 remote_gnutls_credentials_init = TRUE;
356 connection->tls_session = pcmk__new_tls_session(connection->
tcp_socket,
360 if (connection->tls_session == NULL) {
365 if (pcmk__tls_client_handshake(connection, TLS_HANDSHAKE_TIMEOUT_MS)
367 crm_err(
"Session creation for %s:%d failed", private->server, private->port);
369 gnutls_deinit(*connection->tls_session);
370 gnutls_free(connection->tls_session);
371 connection->tls_session = NULL;
376 return -EPROTONOSUPPORT;
398 if (answer == NULL) {
407 crm_err(
"Invalid registration message: %s", msg_type);
410 }
else if (tmp_ticket == NULL) {
414 connection->
token = strdup(tmp_ticket);
425 crm_trace(
"remote client connection established");
438 if (private->passwd == NULL) {
439 if (private->out == NULL) {
445 private->out->prompt(
"Password",
false, &(private->passwd));
449 if (private->server == NULL || private->user == NULL) {
454 rc = cib_tls_signon(cib, &(private->command), FALSE);
458 rc = cib_tls_signon(cib, &(private->callback), TRUE);
470 crm_info(
"Opened connection to %s:%d for %s",
471 private->server, private->port,
name);
476 crm_info(
"Connection to %s:%d for %s failed: %s\n",
484 cib_remote_signoff(
cib_t *cib)
488 crm_debug(
"Disconnecting from the CIB manager");
489 #ifdef HAVE_GNUTLS_GNUTLS_H 500 cib_remote_free(
cib_t *cib)
506 rc = cib_remote_signoff(cib);
510 free(private->server);
512 free(private->passwd);
523 cib_remote_inputfd(
cib_t * cib)
527 return private->callback.tcp_socket;
531 cib_remote_register_notification(
cib_t * cib,
const char *callback,
int enabled)
545 cib_remote_set_connection_dnotify(
cib_t * cib,
void (*dnotify) (gpointer user_data))
547 return -EPROTONOSUPPORT;
568 cib_remote_client_id(
const cib_t *cib,
const char **async_id,
569 const char **sync_id)
573 if (async_id != NULL) {
575 *async_id =
private->callback.token;
577 if (sync_id != NULL) {
579 *sync_id =
private->command.token;
585 cib_remote_new(
const char *server,
const char *user,
const char *passwd,
int port,
597 if (
private == NULL) {
609 private->port = port;
610 private->encrypted = encrypted;
void cib__set_output(cib_t *cib, pcmk__output_t *out)
int pcmk__remote_send_xml(pcmk__remote_t *remote, xmlNode *msg)
const char * pcmk_strerror(int rc)
mainloop_io_t * mainloop_add_fd(const char *name, int priority, int fd, void *userdata, struct mainloop_fd_callbacks *callbacks)
void void void void void pcmk__text_prompt(const char *prompt, bool echo, char **dest)
void(* destroy)(gpointer userdata)
Destroy function for mainloop file descriptor client data.
int(* signoff)(cib_t *cib)
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.
int(* inputfd)(cib_t *cib)
enum crm_ais_msg_types type
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
Wrappers for and extensions to glib mainloop.
#define F_CIB_NOTIFY_ACTIVATE
void cib_native_notify(gpointer data, gpointer user_data)
xmlNode * copy_xml(xmlNode *src_node)
int(* dispatch)(gpointer userdata)
Dispatch function for mainloop file descriptor with data ready.
cib_t * cib_new_variant(void)
int(* set_connection_dnotify)(cib_t *cib, void(*dnotify)(gpointer user_data))
#define crm_warn(fmt, args...)
Formatted output for pacemaker tools.
cib_api_operations_t * cmds
#define crm_debug(fmt, args...)
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
#define crm_trace(fmt, args...)
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))
#define crm_log_xml_debug(xml, text)
void pcmk__str_update(char **str, const char *value)
struct cib_remote_opaque_s cib_remote_opaque_t
xmlNode * create_xml_node(xmlNode *parent, const char *name)
#define crm_log_xml_warn(xml, text)
void free_xml(xmlNode *child)
xmlNode * get_message_xml(const xmlNode *msg, const char *field)
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
#define F_CIB_NOTIFY_TYPE
xmlNode * cib_create_op(int call_id, const char *op, const char *host, const char *section, xmlNode *data, int call_options, const char *user_name)
#define pcmk_err_diff_resync
#define crm_err(fmt, args...)
This structure contains everything that makes up a single output formatter.
void cib_native_callback(cib_t *cib, xmlNode *msg, int call_id, int rc)
#define crm_log_xml_trace(xml, text)
int pcmk__read_remote_message(pcmk__remote_t *remote, int timeout_ms)
cib_t * cib_remote_new(const char *server, const char *user, const char *passwd, int port, gboolean encrypted)
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)
#define crm_info(fmt, args...)
xmlNode * pcmk__remote_message_xml(pcmk__remote_t *remote)