19 #include <sys/socket.h>
30 #ifdef HAVE_GNUTLS_GNUTLS_H
32 # include <gnutls/gnutls.h>
33 gnutls_anon_client_credentials_t anon_cred_c;
35 # define DEFAULT_CLIENT_HANDSHAKE_TIMEOUT 5000
37 const int kx_prio[] = {
42 static gboolean remote_gnutls_credentials_init = FALSE;
47 #include <arpa/inet.h>
54 typedef struct cib_remote_opaque_s {
75 xmlNode *
data, xmlNode ** output_data,
int call_options,
79 cib_remote_inputfd(
cib_t * cib)
83 return private->callback.tcp_socket;
87 cib_remote_set_connection_dnotify(
cib_t * cib,
void (*dnotify) (gpointer user_data))
89 return -EPROTONOSUPPORT;
93 cib_remote_register_notification(
cib_t * cib,
const char *callback,
int enabled)
107 cib_remote_new(
const char *server,
const char *user,
const char *passwd,
int port,
119 private->server = strdup(server);
123 private->user = strdup(user);
127 private->passwd = strdup(passwd);
130 private->port = port;
131 private->encrypted = encrypted;
147 cib_tls_close(
cib_t * cib)
151 #ifdef HAVE_GNUTLS_GNUTLS_H
152 if (private->encrypted) {
153 if (private->command.tls_session) {
154 gnutls_bye(*(private->command.tls_session), GNUTLS_SHUT_RDWR);
155 gnutls_deinit(*(private->command.tls_session));
156 gnutls_free(private->command.tls_session);
159 if (private->callback.tls_session) {
160 gnutls_bye(*(private->callback.tls_session), GNUTLS_SHUT_RDWR);
161 gnutls_deinit(*(private->callback.tls_session));
162 gnutls_free(private->callback.tls_session);
164 private->command.tls_session = NULL;
165 private->callback.tls_session = NULL;
166 if (remote_gnutls_credentials_init) {
167 gnutls_anon_free_client_credentials(anon_cred_c);
168 gnutls_global_deinit();
169 remote_gnutls_credentials_init = FALSE;
174 if (private->command.tcp_socket) {
175 shutdown(private->command.tcp_socket, SHUT_RDWR);
176 close(private->command.tcp_socket);
178 if (private->callback.tcp_socket) {
179 shutdown(private->callback.tcp_socket, SHUT_RDWR);
180 close(private->callback.tcp_socket);
182 private->command.tcp_socket = 0;
183 private->callback.tcp_socket = 0;
185 free(private->command.buffer);
186 free(private->callback.buffer);
187 private->command.buffer = NULL;
188 private->callback.buffer = NULL;
196 return pcmk__tls_client_handshake(remote, DEFAULT_CLIENT_HANDSHAKE_TIMEOUT);
205 xmlNode *answer = NULL;
206 xmlNode *login = NULL;
215 #ifdef HAVE_GNUTLS_GNUTLS_H
216 connection->tls_session = NULL;
221 crm_info(
"Remote connection to %s:%d failed: %s " CRM_XS " rc=%d",
222 private->server, private->port,
pcmk_rc_str(rc), rc);
226 if (private->encrypted) {
228 #ifdef HAVE_GNUTLS_GNUTLS_H
229 if (remote_gnutls_credentials_init == FALSE) {
230 crm_gnutls_global_init();
231 gnutls_anon_allocate_client_credentials(&anon_cred_c);
232 remote_gnutls_credentials_init = TRUE;
236 connection->tls_session = pcmk__new_tls_session(connection->
tcp_socket,
240 if (connection->tls_session == NULL) {
245 if (cib__tls_client_handshake(connection) !=
pcmk_rc_ok) {
246 crm_err(
"Session creation for %s:%d failed", private->server, private->port);
248 gnutls_deinit(*connection->tls_session);
249 gnutls_free(connection->tls_session);
250 connection->tls_session = NULL;
255 return -EPROTONOSUPPORT;
277 if (answer == NULL) {
286 crm_err(
"Invalid registration message: %s", msg_type);
289 }
else if (tmp_ticket == NULL) {
293 connection->
token = strdup(tmp_ticket);
304 crm_trace(
"remote client connection established");
314 crm_err(
"Connection destroyed");
315 #ifdef HAVE_GNUTLS_GNUTLS_H
316 cib_tls_close(user_data);
325 cib_t *cib = user_data;
330 free(private->command.buffer);
331 private->command.buffer = NULL;
332 crm_err(
"received late reply for remote cib connection, discarding");
334 if (rc == ENOTCONN) {
344 cib_t *cib = user_data;
349 crm_info(
"Message on callback channel");
357 crm_trace(
"Activating %s callbacks...", type);
366 crm_err(
"Unknown message type: %s", type);
373 if (rc == ENOTCONN) {
386 if (private->passwd == NULL) {
387 struct termios settings;
389 rc = tcgetattr(0, &settings);
391 settings.c_lflag &= ~ECHO;
392 rc = tcsetattr(0, TCSANOW, &settings);
396 fprintf(stderr,
"Password: ");
397 private->passwd = calloc(1, 1024);
398 rc = scanf(
"%1023s", private->passwd);
399 fprintf(stderr,
"\n");
403 private->passwd = NULL;
406 settings.c_lflag |= ECHO;
407 rc = tcsetattr(0, TCSANOW, &settings);
410 if (private->server == NULL || private->user == NULL) {
415 rc = cib_tls_signon(cib, &(private->command), FALSE);
419 rc = cib_tls_signon(cib, &(private->callback), TRUE);
431 crm_info(
"Opened connection to %s:%d for %s",
432 private->server, private->port, name);
437 crm_info(
"Connection to %s:%d for %s failed: %s\n",
451 crm_debug(
"Disconnecting from the CIB manager");
452 #ifdef HAVE_GNUTLS_GNUTLS_H
473 free(private->server);
475 free(private->passwd);
487 xmlNode *
data, xmlNode ** output_data,
int call_options,
const char *
name)
490 int remaining_time = 0;
493 xmlNode *op_msg = NULL;
494 xmlNode *op_reply = NULL;
502 if (output_data != NULL) {
507 crm_err(
"No operation specified");
523 if (op_msg == NULL) {
527 crm_trace(
"Sending %s message to the CIB manager", op);
539 }
else if (!(call_options & cib_sync_call)) {
543 crm_trace(
"Waiting for a synchronous reply");
545 start_time = time(NULL);
549 while (remaining_time > 0 && (rc != ENOTCONN)) {
554 remaining_time * 1000);
563 if (reply_id == msg_id) {
566 }
else if (reply_id < msg_id) {
567 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
570 }
else if ((reply_id - 10000) > msg_id) {
572 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
575 crm_err(
"Received a __future__ reply:" " %d (wanted %d)", reply_id, msg_id);
582 remaining_time = time(NULL) - start_time;
591 if (rc == ENOTCONN) {
592 crm_err(
"Disconnected while waiting for reply.");
594 }
else if (op_reply == NULL) {
595 crm_err(
"No reply message - empty");
611 if (rc ==
pcmk_ok || rc == -EPERM) {
620 if (output_data == NULL) {
623 }
else if (!(call_options & cib_discard_reply)) {
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)
gboolean safe_str_neq(const char *a, const char *b)
mainloop_io_t * mainloop_add_fd(const char *name, int priority, int fd, void *userdata, struct mainloop_fd_callbacks *callbacks)
cib_t * cib_remote_new(const char *server, const char *user, const char *passwd, int port, gboolean encrypted)
void cib_remote_connection_destroy(gpointer user_data)
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.
int(* inputfd)(cib_t *cib)
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
int(* dispatch)(gpointer userdata)
int(* set_connection_dnotify)(cib_t *cib, void(*dnotify)(gpointer user_data))
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)
cib_t * cib_new_variant(void)
#define crm_warn(fmt, args...)
cib_api_operations_t * cmds
#define crm_debug(fmt, args...)
int cib_remote_callback_dispatch(gpointer user_data)
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
void(* destroy)(gpointer userdata)
#define crm_trace(fmt, args...)
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
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)
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)
#define F_CIB_NOTIFY_TYPE
#define pcmk_err_diff_resync
#define crm_err(fmt, args...)
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
void cib_native_callback(cib_t *cib, xmlNode *msg, int call_id, int rc)
#define crm_log_xml_trace(xml, text)
int(* signoff)(cib_t *cib)
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)
#define safe_str_eq(a, b)
#define crm_info(fmt, args...)
xmlNode * pcmk__remote_message_xml(pcmk__remote_t *remote)
enum crm_ais_msg_types type
int cib_remote_signon(cib_t *cib, const char *name, enum cib_conn_type type)