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,
59 const char *user_name)
62 int remaining_time = 0;
65 xmlNode *op_msg = NULL;
66 xmlNode *op_reply = NULL;
74 if (output_data != NULL) {
79 crm_err(
"No operation specified");
95 crm_trace(
"Sending %s message to the CIB manager", op);
111 crm_trace(
"Waiting for a synchronous reply");
113 start_time = time(NULL);
117 while (remaining_time > 0 && (rc != ENOTCONN)) {
122 remaining_time * 1000);
131 if (reply_id == msg_id) {
134 }
else if (reply_id < msg_id) {
135 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
138 }
else if ((reply_id - 10000) > msg_id) {
140 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
143 crm_err(
"Received a __future__ reply:" " %d (wanted %d)", reply_id, msg_id);
150 remaining_time = time(NULL) - start_time;
159 if (rc == ENOTCONN) {
160 crm_err(
"Disconnected while waiting for reply.");
162 }
else if (op_reply == NULL) {
163 crm_err(
"No reply message - empty");
179 if (rc ==
pcmk_ok || rc == -EPERM) {
188 if (output_data == NULL) {
207 cib_remote_callback_dispatch(gpointer user_data)
210 cib_t *cib = user_data;
215 crm_info(
"Message on callback channel");
239 if (rc == ENOTCONN) {
247 cib_remote_command_dispatch(gpointer user_data)
250 cib_t *cib = user_data;
255 free(private->command.buffer);
256 private->command.buffer = NULL;
257 crm_err(
"received late reply for remote cib connection, discarding");
259 if (rc == ENOTCONN) {
266 cib_tls_close(
cib_t *cib)
270 #ifdef HAVE_GNUTLS_GNUTLS_H 271 if (private->encrypted) {
272 if (private->command.tls_session) {
273 gnutls_bye(*(private->command.tls_session), GNUTLS_SHUT_RDWR);
274 gnutls_deinit(*(private->command.tls_session));
275 gnutls_free(private->command.tls_session);
278 if (private->callback.tls_session) {
279 gnutls_bye(*(private->callback.tls_session), GNUTLS_SHUT_RDWR);
280 gnutls_deinit(*(private->callback.tls_session));
281 gnutls_free(private->callback.tls_session);
283 private->command.tls_session = NULL;
284 private->callback.tls_session = NULL;
285 if (remote_gnutls_credentials_init) {
286 gnutls_anon_free_client_credentials(anon_cred_c);
287 gnutls_global_deinit();
288 remote_gnutls_credentials_init = FALSE;
293 if (private->command.tcp_socket) {
294 shutdown(private->command.tcp_socket, SHUT_RDWR);
295 close(private->command.tcp_socket);
297 if (private->callback.tcp_socket) {
298 shutdown(private->callback.tcp_socket, SHUT_RDWR);
299 close(private->callback.tcp_socket);
301 private->command.tcp_socket = 0;
302 private->callback.tcp_socket = 0;
304 free(private->command.buffer);
305 free(private->callback.buffer);
306 private->command.buffer = NULL;
307 private->callback.buffer = NULL;
313 cib_remote_connection_destroy(gpointer user_data)
315 crm_err(
"Connection destroyed");
316 #ifdef HAVE_GNUTLS_GNUTLS_H 317 cib_tls_close(user_data);
327 xmlNode *answer = NULL;
328 xmlNode *login = NULL;
333 event_channel ? cib_remote_callback_dispatch : cib_remote_command_dispatch;
334 cib_fd_callbacks.
destroy = cib_remote_connection_destroy;
337 #ifdef HAVE_GNUTLS_GNUTLS_H 338 connection->tls_session = NULL;
343 crm_info(
"Remote connection to %s:%d failed: %s " CRM_XS " rc=%d",
344 private->server, private->port,
pcmk_rc_str(rc), rc);
348 if (private->encrypted) {
350 #ifdef HAVE_GNUTLS_GNUTLS_H 351 if (remote_gnutls_credentials_init == FALSE) {
352 crm_gnutls_global_init();
353 gnutls_anon_allocate_client_credentials(&anon_cred_c);
354 remote_gnutls_credentials_init = TRUE;
358 connection->tls_session = pcmk__new_tls_session(connection->
tcp_socket,
362 if (connection->tls_session == NULL) {
367 if (pcmk__tls_client_handshake(connection, TLS_HANDSHAKE_TIMEOUT_MS)
369 crm_err(
"Session creation for %s:%d failed", private->server, private->port);
371 gnutls_deinit(*connection->tls_session);
372 gnutls_free(connection->tls_session);
373 connection->tls_session = NULL;
378 return -EPROTONOSUPPORT;
400 if (answer == NULL) {
409 crm_err(
"Invalid registration message: %s", msg_type);
412 }
else if (tmp_ticket == NULL) {
416 connection->
token = strdup(tmp_ticket);
427 crm_trace(
"remote client connection established");
439 xmlNode *hello = NULL;
441 if (private->passwd == NULL) {
442 if (private->out == NULL) {
448 private->out->prompt(
"Password",
false, &(private->passwd));
452 if (private->server == NULL || private->user == NULL) {
457 rc = cib_tls_signon(cib, &(private->command), FALSE);
461 rc = cib_tls_signon(cib, &(private->callback), TRUE);
476 crm_info(
"Opened connection to %s:%d for %s",
477 private->server, private->port,
name);
482 crm_info(
"Connection to %s:%d for %s failed: %s\n",
490 cib_remote_signoff(
cib_t *cib)
494 crm_debug(
"Disconnecting from the CIB manager");
495 #ifdef HAVE_GNUTLS_GNUTLS_H 507 cib_remote_free(
cib_t *cib)
513 rc = cib_remote_signoff(cib);
517 free(private->server);
519 free(private->passwd);
531 cib_remote_inputfd(
cib_t * cib)
535 return private->callback.tcp_socket;
539 cib_remote_register_notification(
cib_t * cib,
const char *callback,
int enabled)
553 cib_remote_set_connection_dnotify(
cib_t * cib,
void (*dnotify) (gpointer user_data))
555 return -EPROTONOSUPPORT;
576 cib_remote_client_id(
const cib_t *cib,
const char **async_id,
577 const char **sync_id)
581 if (async_id != NULL) {
583 *async_id =
private->callback.token;
585 if (sync_id != NULL) {
587 *sync_id =
private->command.token;
593 cib_remote_new(
const char *server,
const char *user,
const char *passwd,
int port,
605 if (
private == NULL) {
617 private->port = port;
618 private->encrypted = encrypted;
void cib__set_output(cib_t *cib, pcmk__output_t *out)
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.
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...)
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
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)
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)
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
#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)
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...)
Process request when the client commits the active transaction.
xmlNode * pcmk__remote_message_xml(pcmk__remote_t *remote)