28 #include <sys/socket.h> 39 #ifdef HAVE_GNUTLS_GNUTLS_H 41 # include <gnutls/gnutls.h> 42 gnutls_anon_client_credentials_t anon_cred_c;
44 # define DEFAULT_CLIENT_HANDSHAKE_TIMEOUT 5000 46 const int kx_prio[] = {
51 static gboolean remote_gnutls_credentials_init = FALSE;
56 #include <arpa/inet.h> 63 typedef struct cib_remote_opaque_s {
84 xmlNode *
data, xmlNode ** output_data,
int call_options,
88 cib_remote_inputfd(
cib_t * cib)
92 return private->callback.tcp_socket;
96 cib_remote_set_connection_dnotify(
cib_t * cib,
void (*dnotify) (gpointer user_data))
98 return -EPROTONOSUPPORT;
102 cib_remote_register_notification(
cib_t * cib,
const char *callback,
int enabled)
116 cib_remote_new(
const char *server,
const char *user,
const char *passwd,
int port,
128 private->server = strdup(server);
132 private->user = strdup(user);
136 private->passwd = strdup(passwd);
139 private->port = port;
140 private->encrypted = encrypted;
156 cib_tls_close(
cib_t * cib)
160 #ifdef HAVE_GNUTLS_GNUTLS_H 161 if (private->encrypted) {
162 if (private->command.tls_session) {
163 gnutls_bye(*(private->command.tls_session), GNUTLS_SHUT_RDWR);
164 gnutls_deinit(*(private->command.tls_session));
165 gnutls_free(private->command.tls_session);
168 if (private->callback.tls_session) {
169 gnutls_bye(*(private->callback.tls_session), GNUTLS_SHUT_RDWR);
170 gnutls_deinit(*(private->callback.tls_session));
171 gnutls_free(private->callback.tls_session);
173 private->command.tls_session = NULL;
174 private->callback.tls_session = NULL;
175 if (remote_gnutls_credentials_init) {
176 gnutls_anon_free_client_credentials(anon_cred_c);
177 gnutls_global_deinit();
178 remote_gnutls_credentials_init = FALSE;
183 if (private->command.tcp_socket) {
184 shutdown(private->command.tcp_socket, SHUT_RDWR);
185 close(private->command.tcp_socket);
187 if (private->callback.tcp_socket) {
188 shutdown(private->callback.tcp_socket, SHUT_RDWR);
189 close(private->callback.tcp_socket);
191 private->command.tcp_socket = 0;
192 private->callback.tcp_socket = 0;
194 free(private->command.buffer);
195 free(private->callback.buffer);
196 private->command.buffer = NULL;
197 private->callback.buffer = NULL;
208 int disconnected = 0;
210 xmlNode *answer = NULL;
211 xmlNode *login = NULL;
220 #ifdef HAVE_GNUTLS_GNUTLS_H 221 connection->tls_session = NULL;
225 crm_perror(LOG_ERR,
"remote tcp connection to %s:%d failed", private->server,
232 if (private->encrypted) {
234 #ifdef HAVE_GNUTLS_GNUTLS_H 235 if (remote_gnutls_credentials_init == FALSE) {
236 crm_gnutls_global_init();
237 gnutls_anon_allocate_client_credentials(&anon_cred_c);
238 remote_gnutls_credentials_init = TRUE;
242 connection->tls_session = pcmk__new_tls_session(sock, GNUTLS_CLIENT,
245 if (connection->tls_session == NULL) {
250 if (crm_initiate_client_tls_handshake(connection, DEFAULT_CLIENT_HANDSHAKE_TIMEOUT) != 0) {
251 crm_err(
"Session creation for %s:%d failed", private->server, private->port);
253 gnutls_deinit(*connection->tls_session);
254 gnutls_free(connection->tls_session);
255 connection->tls_session = NULL;
260 return -EPROTONOSUPPORT;
283 if (answer == NULL) {
292 crm_err(
"Invalid registration message: %s", msg_type);
295 }
else if (tmp_ticket == NULL) {
299 connection->
token = strdup(tmp_ticket);
310 crm_trace(
"remote client connection established");
320 crm_err(
"Connection destroyed");
321 #ifdef HAVE_GNUTLS_GNUTLS_H 322 cib_tls_close(user_data);
330 int disconnected = 0;
331 cib_t *cib = user_data;
336 free(private->command.buffer);
337 private->command.buffer = NULL;
338 crm_err(
"received late reply for remote cib connection, discarding");
349 cib_t *cib = user_data;
353 int disconnected = 0;
355 crm_info(
"Message on callback channel");
363 crm_trace(
"Activating %s callbacks...", type);
372 crm_err(
"Unknown message type: %s", type);
392 if (private->passwd == NULL) {
393 struct termios settings;
395 rc = tcgetattr(0, &settings);
397 settings.c_lflag &= ~ECHO;
398 rc = tcsetattr(0, TCSANOW, &settings);
402 fprintf(stderr,
"Password: ");
403 private->passwd = calloc(1, 1024);
404 rc = scanf(
"%1023s", private->passwd);
405 fprintf(stderr,
"\n");
410 private->passwd = NULL;
413 settings.c_lflag |= ECHO;
414 rc = tcsetattr(0, TCSANOW, &settings);
417 if (private->server == NULL || private->user == NULL) {
422 rc = cib_tls_signon(cib, &(private->command), FALSE);
426 rc = cib_tls_signon(cib, &(private->callback), TRUE);
438 crm_notice(
"%s: Opened connection to %s:%d", name, private->server, private->port);
443 fprintf(stderr,
"%s: Connection to %s:%d failed: %s\n",
457 crm_debug(
"Signing out of the CIB Service");
458 #ifdef HAVE_GNUTLS_GNUTLS_H 479 free(private->server);
481 free(private->passwd);
493 xmlNode *
data, xmlNode ** output_data,
int call_options,
const char *name)
496 int disconnected = 0;
497 int remaining_time = 0;
500 xmlNode *op_msg = NULL;
501 xmlNode *op_reply = NULL;
509 if (output_data != NULL) {
514 crm_err(
"No operation specified");
530 if (op_msg == NULL) {
534 crm_trace(
"Sending %s message to CIB service", op);
546 }
else if (!(call_options & cib_sync_call)) {
550 crm_trace(
"Waiting for a synchronous reply");
552 start_time = time(NULL);
555 while (remaining_time > 0 && !disconnected) {
559 crm_remote_recv(&private->command, remaining_time * 1000, &disconnected);
568 if (reply_id == msg_id) {
571 }
else if (reply_id < msg_id) {
572 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
575 }
else if ((reply_id - 10000) > msg_id) {
577 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
580 crm_err(
"Received a __future__ reply:" " %d (wanted %d)", reply_id, msg_id);
587 remaining_time = time(NULL) - start_time;
597 crm_err(
"Disconnected while waiting for reply.");
599 }
else if (op_reply == NULL) {
600 crm_err(
"No reply message - empty");
616 if (rc ==
pcmk_ok || rc == -EPERM) {
625 if (output_data == NULL) {
628 }
else if (!(call_options & cib_discard_reply)) {
xmlNode * get_message_xml(xmlNode *msg, const char *field)
#define crm_notice(fmt, args...)
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)
void cib_remote_connection_destroy(gpointer user_data)
int crm_remote_tcp_connect(const char *host, int port)
void(* destroy)(gpointer userdata)
const char * pcmk_strerror(int rc)
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)
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 pcmk_err_diff_resync
#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)
int crm_remote_send(crm_remote_t *remote, xmlNode *msg)
const char * crm_element_value(const xmlNode *data, const char *name)
Retrieve the value of an XML attribute.
#define crm_trace(fmt, args...)
#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)
gboolean crm_remote_recv(crm_remote_t *remote, int total_timeout, int *disconnected)
#define F_CIB_NOTIFY_TYPE
#define crm_perror(level, fmt, args...)
Log a system error message.
#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)
cib_t * cib_remote_new(const char *server, const char *user, const char *passwd, int port, gboolean encrypted)
#define safe_str_eq(a, b)
#define crm_info(fmt, args...)
xmlNode * crm_remote_parse_buffer(crm_remote_t *remote)
enum crm_ais_msg_types type
int cib_remote_signon(cib_t *cib, const char *name, enum cib_conn_type type)