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 {
57 cib_remote_perform_op(
cib_t *cib,
const char *op,
const char *
host,
58 const char *section, xmlNode *
data,
59 xmlNode **output_data,
int call_options,
60 const char *user_name)
63 int remaining_time = 0;
66 xmlNode *op_msg = NULL;
67 xmlNode *op_reply = NULL;
75 if (output_data != NULL) {
80 crm_err(
"No operation specified");
96 crm_trace(
"Sending %s message to the CIB manager", op);
112 crm_trace(
"Waiting for a synchronous reply");
114 start_time = time(NULL);
118 while (remaining_time > 0 && (rc != ENOTCONN)) {
123 remaining_time * 1000);
132 if (reply_id == msg_id) {
135 }
else if (reply_id < msg_id) {
136 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
139 }
else if ((reply_id - 10000) > msg_id) {
141 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
144 crm_err(
"Received a __future__ reply:" " %d (wanted %d)", reply_id, msg_id);
151 remaining_time = time(NULL) - start_time;
160 if (rc == ENOTCONN) {
161 crm_err(
"Disconnected while waiting for reply.");
163 }
else if (op_reply == NULL) {
164 crm_err(
"No reply message - empty");
180 if (rc ==
pcmk_ok || rc == -EPERM) {
189 if (output_data == NULL) {
210 cib_remote_callback_dispatch(gpointer user_data)
213 cib_t *cib = user_data;
218 crm_info(
"Message on callback channel");
242 if (rc == ENOTCONN) {
250 cib_remote_command_dispatch(gpointer user_data)
253 cib_t *cib = user_data;
258 free(private->command.buffer);
259 private->command.buffer = NULL;
260 crm_err(
"received late reply for remote cib connection, discarding");
262 if (rc == ENOTCONN) {
269 cib_tls_close(
cib_t *cib)
273 #ifdef HAVE_GNUTLS_GNUTLS_H 274 if (private->encrypted) {
275 if (private->command.tls_session) {
276 gnutls_bye(*(private->command.tls_session), GNUTLS_SHUT_RDWR);
277 gnutls_deinit(*(private->command.tls_session));
278 gnutls_free(private->command.tls_session);
281 if (private->callback.tls_session) {
282 gnutls_bye(*(private->callback.tls_session), GNUTLS_SHUT_RDWR);
283 gnutls_deinit(*(private->callback.tls_session));
284 gnutls_free(private->callback.tls_session);
286 private->command.tls_session = NULL;
287 private->callback.tls_session = NULL;
288 if (remote_gnutls_credentials_init) {
289 gnutls_anon_free_client_credentials(anon_cred_c);
290 gnutls_global_deinit();
291 remote_gnutls_credentials_init = FALSE;
296 if (private->command.tcp_socket) {
297 shutdown(private->command.tcp_socket, SHUT_RDWR);
298 close(private->command.tcp_socket);
300 if (private->callback.tcp_socket) {
301 shutdown(private->callback.tcp_socket, SHUT_RDWR);
302 close(private->callback.tcp_socket);
304 private->command.tcp_socket = 0;
305 private->callback.tcp_socket = 0;
307 free(private->command.buffer);
308 free(private->callback.buffer);
309 private->command.buffer = NULL;
310 private->callback.buffer = NULL;
316 cib_remote_connection_destroy(gpointer user_data)
318 crm_err(
"Connection destroyed");
319 #ifdef HAVE_GNUTLS_GNUTLS_H 320 cib_tls_close(user_data);
330 xmlNode *answer = NULL;
331 xmlNode *login = NULL;
336 event_channel ? cib_remote_callback_dispatch : cib_remote_command_dispatch;
337 cib_fd_callbacks.
destroy = cib_remote_connection_destroy;
340 #ifdef HAVE_GNUTLS_GNUTLS_H 341 connection->tls_session = NULL;
346 crm_info(
"Remote connection to %s:%d failed: %s " CRM_XS " rc=%d",
347 private->server, private->port,
pcmk_rc_str(rc), rc);
351 if (private->encrypted) {
352 int tls_rc = GNUTLS_E_SUCCESS;
355 #ifdef HAVE_GNUTLS_GNUTLS_H 356 if (remote_gnutls_credentials_init == FALSE) {
357 crm_gnutls_global_init();
358 gnutls_anon_allocate_client_credentials(&anon_cred_c);
359 remote_gnutls_credentials_init = TRUE;
363 connection->tls_session = pcmk__new_tls_session(connection->
tcp_socket,
367 if (connection->tls_session == NULL) {
372 rc = pcmk__tls_client_handshake(connection, TLS_HANDSHAKE_TIMEOUT,
375 crm_err(
"Remote CIB session creation for %s:%d failed: %s",
376 private->server, private->port,
377 (rc == EPROTO)? gnutls_strerror(tls_rc) :
pcmk_rc_str(rc));
378 gnutls_deinit(*connection->tls_session);
379 gnutls_free(connection->tls_session);
380 connection->tls_session = NULL;
385 return -EPROTONOSUPPORT;
407 if (answer == NULL) {
417 crm_err(
"Invalid registration message: %s", msg_type);
420 }
else if (tmp_ticket == NULL) {
424 connection->
token = strdup(tmp_ticket);
435 crm_trace(
"remote client connection established");
447 xmlNode *hello = NULL;
449 if (private->passwd == NULL) {
450 if (private->out == NULL) {
456 private->out->prompt(
"Password",
false, &(private->passwd));
460 if (private->server == NULL || private->user == NULL) {
465 rc = cib_tls_signon(cib, &(private->command), FALSE);
469 rc = cib_tls_signon(cib, &(private->callback), TRUE);
484 crm_info(
"Opened connection to %s:%d for %s",
485 private->server, private->port,
name);
490 crm_info(
"Connection to %s:%d for %s failed: %s\n",
498 cib_remote_signoff(
cib_t *cib)
502 crm_debug(
"Disconnecting from the CIB manager");
503 #ifdef HAVE_GNUTLS_GNUTLS_H 515 cib_remote_free(
cib_t *cib)
521 rc = cib_remote_signoff(cib);
525 free(private->server);
527 free(private->passwd);
539 cib_remote_inputfd(
cib_t * cib)
543 return private->callback.tcp_socket;
547 cib_remote_register_notification(
cib_t * cib,
const char *callback,
int enabled)
561 cib_remote_set_connection_dnotify(
cib_t * cib,
void (*dnotify) (gpointer user_data))
563 return -EPROTONOSUPPORT;
584 cib_remote_client_id(
const cib_t *cib,
const char **async_id,
585 const char **sync_id)
589 if (async_id != NULL) {
591 *async_id =
private->callback.token;
593 if (sync_id != NULL) {
595 *sync_id =
private->command.token;
601 cib_remote_new(
const char *server,
const char *user,
const char *passwd,
int port,
613 if (
private == NULL) {
624 private->port = port;
625 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.
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