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 #ifdef HAVE_GNUTLS_GNUTLS_H 197 return pcmk__tls_client_handshake(remote, DEFAULT_CLIENT_HANDSHAKE_TIMEOUT);
209 xmlNode *answer = NULL;
210 xmlNode *login = NULL;
219 #ifdef HAVE_GNUTLS_GNUTLS_H 220 connection->tls_session = NULL;
225 crm_info(
"Remote connection to %s:%d failed: %s " CRM_XS " rc=%d",
230 if (private->encrypted) {
232 #ifdef HAVE_GNUTLS_GNUTLS_H 233 if (remote_gnutls_credentials_init == FALSE) {
234 crm_gnutls_global_init();
235 gnutls_anon_allocate_client_credentials(&anon_cred_c);
236 remote_gnutls_credentials_init = TRUE;
240 connection->tls_session = pcmk__new_tls_session(connection->
tcp_socket,
244 if (connection->tls_session == NULL) {
249 if (cib__tls_client_handshake(connection) !=
pcmk_rc_ok) {
250 crm_err(
"Session creation for %s:%d failed", private->server, private->port);
252 gnutls_deinit(*connection->tls_session);
253 gnutls_free(connection->tls_session);
254 connection->tls_session = NULL;
259 return -EPROTONOSUPPORT;
281 if (answer == NULL) {
290 crm_err(
"Invalid registration message: %s", msg_type);
293 }
else if (tmp_ticket == NULL) {
297 connection->
token = strdup(tmp_ticket);
308 crm_trace(
"remote client connection established");
318 crm_err(
"Connection destroyed");
319 #ifdef HAVE_GNUTLS_GNUTLS_H 320 cib_tls_close(user_data);
329 cib_t *cib = user_data;
334 free(private->command.buffer);
335 private->command.buffer = NULL;
336 crm_err(
"received late reply for remote cib connection, discarding");
338 if (
rc == ENOTCONN) {
348 cib_t *cib = user_data;
353 crm_info(
"Message on callback channel");
377 if (
rc == ENOTCONN) {
390 if (private->passwd == NULL) {
391 struct termios settings;
393 rc = tcgetattr(0, &settings);
395 settings.c_lflag &= ~ECHO;
396 rc = tcsetattr(0, TCSANOW, &settings);
400 fprintf(stderr,
"Password: ");
401 private->passwd = calloc(1, 1024);
402 rc = scanf(
"%1023s", private->passwd);
403 fprintf(stderr,
"\n");
407 private->passwd = NULL;
410 settings.c_lflag |= ECHO;
411 rc = tcsetattr(0, TCSANOW, &settings);
414 if (private->server == NULL || private->user == NULL) {
419 rc = cib_tls_signon(cib, &(private->command), FALSE);
423 rc = cib_tls_signon(cib, &(private->callback), TRUE);
435 crm_info(
"Opened connection to %s:%d for %s",
436 private->server, private->port,
name);
441 crm_info(
"Connection to %s:%d for %s failed: %s\n",
455 crm_debug(
"Disconnecting from the CIB manager");
456 #ifdef HAVE_GNUTLS_GNUTLS_H 477 free(private->server);
479 free(private->passwd);
491 xmlNode *
data, xmlNode ** output_data,
int call_options,
const char *
name)
494 int remaining_time = 0;
497 xmlNode *op_msg = NULL;
498 xmlNode *op_reply = NULL;
506 if (output_data != NULL) {
511 crm_err(
"No operation specified");
523 if (op_msg == NULL) {
527 crm_trace(
"Sending %s message to the CIB manager", op);
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");
620 if (output_data == NULL) {
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(* destroy)(gpointer userdata)
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)
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)
cib_t * cib_new_variant(void)
int(* set_connection_dnotify)(cib_t *cib, void(*dnotify)(gpointer user_data))
#define crm_warn(fmt, args...)
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)
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...)
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)
enum crm_ais_msg_types type
int cib_remote_signon(cib_t *cib, const char *name, enum cib_conn_type type)