19 #include <sys/socket.h> 31 #ifdef HAVE_GNUTLS_GNUTLS_H 33 # include <gnutls/gnutls.h> 36 #define TLS_HANDSHAKE_TIMEOUT 5 38 static gnutls_anon_client_credentials_t anon_cred_c;
39 static gboolean remote_gnutls_credentials_init = FALSE;
41 #endif // HAVE_GNUTLS_GNUTLS_H 43 #include <arpa/inet.h> 45 typedef struct cib_remote_opaque_s {
59 cib_remote_perform_op(
cib_t *cib,
const char *op,
const char *
host,
60 const char *section, xmlNode *
data,
61 xmlNode **output_data,
int call_options,
62 const char *user_name)
65 int remaining_time = 0;
68 xmlNode *op_msg = NULL;
69 xmlNode *op_reply = NULL;
77 if (output_data != NULL) {
82 crm_err(
"No operation specified");
98 crm_trace(
"Sending %s message to the CIB manager", op);
114 crm_trace(
"Waiting for a synchronous reply");
116 start_time = time(NULL);
120 while (remaining_time > 0 && (rc != ENOTCONN)) {
125 remaining_time * 1000);
134 if (reply_id == msg_id) {
137 }
else if (reply_id < msg_id) {
138 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
141 }
else if ((reply_id - 10000) > msg_id) {
143 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
146 crm_err(
"Received a __future__ reply:" " %d (wanted %d)", reply_id, msg_id);
153 remaining_time = time(NULL) - start_time;
156 if (rc == ENOTCONN) {
157 crm_err(
"Disconnected while waiting for reply.");
159 }
else if (op_reply == NULL) {
160 crm_err(
"No reply message - empty");
176 if (rc ==
pcmk_ok || rc == -EPERM) {
184 if (output_data == NULL) {
205 cib_remote_callback_dispatch(gpointer user_data)
208 cib_t *cib = user_data;
212 const char *
type = NULL;
218 if (private->start_time == 0) {
219 private->start_time = time(NULL);
230 if (time(NULL) >= private->start_time + private->timeout_sec) {
231 crm_info(
"Error reading from CIB manager connection: %s",
241 crm_info(
"Error reading from CIB manager connection: %s",
248 private->start_time = 0;
265 private->start_time = 0;
270 cib_remote_command_dispatch(gpointer user_data)
273 cib_t *cib = user_data;
277 if (private->start_time == 0) {
278 private->start_time = time(NULL);
284 if (time(NULL) >= private->start_time + private->timeout_sec) {
285 crm_info(
"Error reading from CIB manager connection: %s",
294 free(private->command.buffer);
295 private->command.buffer = NULL;
296 crm_err(
"received late reply for remote cib connection, discarding");
299 crm_info(
"Error reading from CIB manager connection: %s",
304 private->start_time = 0;
309 cib_tls_close(
cib_t *cib)
313 #ifdef HAVE_GNUTLS_GNUTLS_H 314 if (private->encrypted) {
315 if (private->command.tls_session) {
316 gnutls_bye(*(private->command.tls_session), GNUTLS_SHUT_RDWR);
317 gnutls_deinit(*(private->command.tls_session));
318 gnutls_free(private->command.tls_session);
321 if (private->callback.tls_session) {
322 gnutls_bye(*(private->callback.tls_session), GNUTLS_SHUT_RDWR);
323 gnutls_deinit(*(private->callback.tls_session));
324 gnutls_free(private->callback.tls_session);
326 private->command.tls_session = NULL;
327 private->callback.tls_session = NULL;
328 if (remote_gnutls_credentials_init) {
329 gnutls_anon_free_client_credentials(anon_cred_c);
330 gnutls_global_deinit();
331 remote_gnutls_credentials_init = FALSE;
336 if (private->command.tcp_socket) {
337 shutdown(private->command.tcp_socket, SHUT_RDWR);
338 close(private->command.tcp_socket);
340 if (private->callback.tcp_socket) {
341 shutdown(private->callback.tcp_socket, SHUT_RDWR);
342 close(private->callback.tcp_socket);
344 private->command.tcp_socket = 0;
345 private->callback.tcp_socket = 0;
347 free(private->command.buffer);
348 free(private->callback.buffer);
349 private->command.buffer = NULL;
350 private->callback.buffer = NULL;
356 cib_remote_connection_destroy(gpointer user_data)
358 crm_err(
"Connection destroyed");
359 #ifdef HAVE_GNUTLS_GNUTLS_H 360 cib_tls_close(user_data);
370 xmlNode *answer = NULL;
371 xmlNode *login = NULL;
376 event_channel ? cib_remote_callback_dispatch : cib_remote_command_dispatch;
377 cib_fd_callbacks.
destroy = cib_remote_connection_destroy;
380 #ifdef HAVE_GNUTLS_GNUTLS_H 381 connection->tls_session = NULL;
386 crm_info(
"Remote connection to %s:%d failed: %s " CRM_XS " rc=%d",
387 private->server, private->port,
pcmk_rc_str(rc), rc);
391 if (private->encrypted) {
392 int tls_rc = GNUTLS_E_SUCCESS;
395 #ifdef HAVE_GNUTLS_GNUTLS_H 396 if (remote_gnutls_credentials_init == FALSE) {
397 crm_gnutls_global_init();
398 gnutls_anon_allocate_client_credentials(&anon_cred_c);
399 remote_gnutls_credentials_init = TRUE;
403 connection->tls_session = pcmk__new_tls_session(connection->
tcp_socket,
407 if (connection->tls_session == NULL) {
412 rc = pcmk__tls_client_handshake(connection, TLS_HANDSHAKE_TIMEOUT,
415 crm_err(
"Remote CIB session creation for %s:%d failed: %s",
416 private->server, private->port,
417 (rc == EPROTO)? gnutls_strerror(tls_rc) :
pcmk_rc_str(rc));
418 gnutls_deinit(*connection->tls_session);
419 gnutls_free(connection->tls_session);
420 connection->tls_session = NULL;
425 return -EPROTONOSUPPORT;
447 if (answer == NULL) {
457 crm_err(
"Invalid registration message: %s", msg_type);
460 }
else if (tmp_ticket == NULL) {
464 connection->
token = strdup(tmp_ticket);
475 crm_trace(
"remote client connection established");
476 private->timeout_sec = 60;
488 xmlNode *hello = NULL;
490 if (private->passwd == NULL) {
491 if (private->out == NULL) {
497 private->out->prompt(
"Password",
false, &(private->passwd));
501 if (private->server == NULL || private->user == NULL) {
506 rc = cib_tls_signon(cib, &(private->command), FALSE);
510 rc = cib_tls_signon(cib, &(private->callback), TRUE);
525 crm_info(
"Opened connection to %s:%d for %s",
526 private->server, private->port,
name);
531 crm_info(
"Connection to %s:%d for %s failed: %s\n",
539 cib_remote_signoff(
cib_t *cib)
543 crm_debug(
"Disconnecting from the CIB manager");
544 #ifdef HAVE_GNUTLS_GNUTLS_H 556 cib_remote_free(
cib_t *cib)
562 rc = cib_remote_signoff(cib);
566 free(private->server);
568 free(private->passwd);
580 cib_remote_inputfd(
cib_t * cib)
584 return private->callback.tcp_socket;
588 cib_remote_register_notification(
cib_t * cib,
const char *callback,
int enabled)
602 cib_remote_set_connection_dnotify(
cib_t * cib,
void (*dnotify) (gpointer user_data))
604 return -EPROTONOSUPPORT;
625 cib_remote_client_id(
const cib_t *cib,
const char **async_id,
626 const char **sync_id)
630 if (async_id != NULL) {
632 *async_id =
private->callback.token;
634 if (sync_id != NULL) {
636 *sync_id =
private->command.token;
642 cib_remote_new(
const char *server,
const char *user,
const char *passwd,
int port,
654 if (
private == NULL) {
665 private->port = port;
666 private->encrypted = encrypted;
void cib__set_output(cib_t *cib, pcmk__output_t *out)
xmlNode * pcmk__xml_copy(xmlNode *parent, xmlNode *src)
#define PCMK__VALUE_PASSWORD
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)
int pcmk_rc2legacy(int rc)
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.
#define PCMK__XA_CIB_CLIENTID
int(* signoff)(cib_t *cib)
#define PCMK__XE_CIB_COMMAND
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.
#define PCMK__XA_CIB_NOTIFY_TYPE
Wrappers for and extensions to glib mainloop.
int pcmk__read_available_remote_data(pcmk__remote_t *remote)
void cib_native_notify(gpointer data, gpointer user_data)
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.
xmlNode * pcmk__xe_first_child(const xmlNode *parent, const char *node_name, const char *attr_n, const char *attr_v)
#define crm_trace(fmt, args...)
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
#define PCMK__XE_CIB_CALLDATA
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)
Wrappers for and extensions to libxml2.
struct cib_remote_opaque_s cib_remote_opaque_t
#define crm_log_xml_warn(xml, text)
int pcmk__remote_send_xml(pcmk__remote_t *remote, const xmlNode *msg)
void free_xml(xmlNode *child)
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)
#define pcmk__str_copy(str)
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
#define pcmk_err_diff_resync
int(* end_transaction)(cib_t *cib, bool commit, int call_options)
End and optionally commit this client's CIB transaction.
#define crm_err(fmt, args...)
int cib__extend_transaction(cib_t *cib, xmlNode *request)
#define PCMK__XA_PASSWORD
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)
xmlNode * pcmk__xe_create(xmlNode *parent, const char *name)
#define PCMK__XA_CIB_NOTIFY_ACTIVATE
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 PCMK__XA_CIB_CALLID
#define crm_info(fmt, args...)
Process request when the client commits the active transaction.
xmlNode * pcmk__remote_message_xml(pcmk__remote_t *remote)
#define PCMK__VALUE_CIB_NOTIFY