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;
44 #endif // HAVE_GNUTLS_GNUTLS_H 46 #include <arpa/inet.h> 50 typedef struct cib_remote_opaque_s {
72 xmlNode *
data, xmlNode ** output_data,
int call_options,
76 cib_remote_inputfd(
cib_t * cib)
80 return private->callback.tcp_socket;
84 cib_remote_set_connection_dnotify(
cib_t * cib,
void (*dnotify) (gpointer user_data))
86 return -EPROTONOSUPPORT;
90 cib_remote_register_notification(
cib_t * cib,
const char *callback,
int enabled)
104 cib_remote_new(
const char *server,
const char *user,
const char *passwd,
int port,
116 if (
private == NULL) {
128 private->port = port;
129 private->encrypted = encrypted;
145 cib_tls_close(
cib_t * cib)
149 #ifdef HAVE_GNUTLS_GNUTLS_H 150 if (private->encrypted) {
151 if (private->command.tls_session) {
152 gnutls_bye(*(private->command.tls_session), GNUTLS_SHUT_RDWR);
153 gnutls_deinit(*(private->command.tls_session));
154 gnutls_free(private->command.tls_session);
157 if (private->callback.tls_session) {
158 gnutls_bye(*(private->callback.tls_session), GNUTLS_SHUT_RDWR);
159 gnutls_deinit(*(private->callback.tls_session));
160 gnutls_free(private->callback.tls_session);
162 private->command.tls_session = NULL;
163 private->callback.tls_session = NULL;
164 if (remote_gnutls_credentials_init) {
165 gnutls_anon_free_client_credentials(anon_cred_c);
166 gnutls_global_deinit();
167 remote_gnutls_credentials_init = FALSE;
172 if (private->command.tcp_socket) {
173 shutdown(private->command.tcp_socket, SHUT_RDWR);
174 close(private->command.tcp_socket);
176 if (private->callback.tcp_socket) {
177 shutdown(private->callback.tcp_socket, SHUT_RDWR);
178 close(private->callback.tcp_socket);
180 private->command.tcp_socket = 0;
181 private->callback.tcp_socket = 0;
183 free(private->command.buffer);
184 free(private->callback.buffer);
185 private->command.buffer = NULL;
186 private->callback.buffer = NULL;
197 xmlNode *answer = NULL;
198 xmlNode *login = NULL;
207 #ifdef HAVE_GNUTLS_GNUTLS_H 208 connection->tls_session = NULL;
213 crm_info(
"Remote connection to %s:%d failed: %s " CRM_XS " rc=%d",
214 private->server, private->port,
pcmk_rc_str(rc), rc);
218 if (private->encrypted) {
220 #ifdef HAVE_GNUTLS_GNUTLS_H 221 if (remote_gnutls_credentials_init == FALSE) {
222 crm_gnutls_global_init();
223 gnutls_anon_allocate_client_credentials(&anon_cred_c);
224 remote_gnutls_credentials_init = TRUE;
228 connection->tls_session = pcmk__new_tls_session(connection->
tcp_socket,
232 if (connection->tls_session == NULL) {
237 if (pcmk__tls_client_handshake(connection, TLS_HANDSHAKE_TIMEOUT_MS)
239 crm_err(
"Session creation for %s:%d failed", private->server, private->port);
241 gnutls_deinit(*connection->tls_session);
242 gnutls_free(connection->tls_session);
243 connection->tls_session = NULL;
248 return -EPROTONOSUPPORT;
270 if (answer == NULL) {
279 crm_err(
"Invalid registration message: %s", msg_type);
282 }
else if (tmp_ticket == NULL) {
286 connection->
token = strdup(tmp_ticket);
297 crm_trace(
"remote client connection established");
307 crm_err(
"Connection destroyed");
308 #ifdef HAVE_GNUTLS_GNUTLS_H 309 cib_tls_close(user_data);
318 cib_t *cib = user_data;
323 free(private->command.buffer);
324 private->command.buffer = NULL;
325 crm_err(
"received late reply for remote cib connection, discarding");
327 if (rc == ENOTCONN) {
337 cib_t *cib = user_data;
342 crm_info(
"Message on callback channel");
366 if (rc == ENOTCONN) {
379 if (private->passwd == NULL) {
380 if (private->out == NULL) {
386 private->out->prompt(
"Password",
false, &(private->passwd));
390 if (private->server == NULL || private->user == NULL) {
395 rc = cib_tls_signon(cib, &(private->command), FALSE);
399 rc = cib_tls_signon(cib, &(private->callback), TRUE);
411 crm_info(
"Opened connection to %s:%d for %s",
412 private->server, private->port,
name);
417 crm_info(
"Connection to %s:%d for %s failed: %s\n",
431 crm_debug(
"Disconnecting from the CIB manager");
432 #ifdef HAVE_GNUTLS_GNUTLS_H 453 free(private->server);
455 free(private->passwd);
467 xmlNode *
data, xmlNode ** output_data,
int call_options,
const char *
name)
470 int remaining_time = 0;
473 xmlNode *op_msg = NULL;
474 xmlNode *op_reply = NULL;
482 if (output_data != NULL) {
487 crm_err(
"No operation specified");
499 if (op_msg == NULL) {
503 crm_trace(
"Sending %s message to the CIB manager", op);
519 crm_trace(
"Waiting for a synchronous reply");
521 start_time = time(NULL);
525 while (remaining_time > 0 && (rc != ENOTCONN)) {
530 remaining_time * 1000);
539 if (reply_id == msg_id) {
542 }
else if (reply_id < msg_id) {
543 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
546 }
else if ((reply_id - 10000) > msg_id) {
548 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
551 crm_err(
"Received a __future__ reply:" " %d (wanted %d)", reply_id, msg_id);
558 remaining_time = time(NULL) - start_time;
567 if (rc == ENOTCONN) {
568 crm_err(
"Disconnected while waiting for reply.");
570 }
else if (op_reply == NULL) {
571 crm_err(
"No reply message - empty");
587 if (rc ==
pcmk_ok || rc == -EPERM) {
596 if (output_data == NULL) {
void cib__set_output(cib_t *cib, pcmk__output_t *out)
int pcmk__remote_send_xml(pcmk__remote_t *remote, xmlNode *msg)
xmlNode * get_message_xml(xmlNode *msg, const char *field)
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 cib_remote_connection_destroy(gpointer user_data)
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)
int cib_remote_command_dispatch(gpointer user_data)
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
xmlNode * cib_create_op(int call_id, const char *token, const char *op, const char *host, const char *section, xmlNode *data, int call_options, const char *user_name)
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 cib_remote_callback_dispatch(gpointer user_data)
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)
int cib_remote_free(cib_t *cib)
void free_xml(xmlNode *child)
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
#define F_CIB_NOTIFY_TYPE
#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 cib_remote_perform_op(cib_t *cib, const char *op, const char *host, const char *section, xmlNode *data, xmlNode **output_data, int call_options, const char *name)
int cib_remote_signoff(cib_t *cib)
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)
#define crm_info(fmt, args...)
xmlNode * pcmk__remote_message_xml(pcmk__remote_t *remote)
int cib_remote_signon(cib_t *cib, const char *name, enum cib_conn_type type)