28 #include <sys/socket.h>
38 #ifdef HAVE_GNUTLS_GNUTLS_H
40 # include <gnutls/gnutls.h>
41 gnutls_anon_client_credentials_t anon_cred_c;
43 # define DEFAULT_CLIENT_HANDSHAKE_TIMEOUT 5000
45 const int kx_prio[] = {
50 static gboolean remote_gnutls_credentials_init = FALSE;
55 #include <arpa/inet.h>
62 typedef struct cib_remote_opaque_s {
83 xmlNode *
data, xmlNode ** output_data,
int call_options,
87 cib_remote_inputfd(
cib_t * cib)
91 return private->callback.tcp_socket;
95 cib_remote_set_connection_dnotify(
cib_t * cib,
void (*dnotify) (gpointer user_data))
97 return -EPROTONOSUPPORT;
101 cib_remote_register_notification(
cib_t * cib,
const char *callback,
int enabled)
115 cib_remote_new(
const char *server,
const char *user,
const char *passwd,
int port,
127 private->server = strdup(server);
131 private->user = strdup(user);
135 private->passwd = strdup(passwd);
138 private->port = port;
139 private->encrypted = encrypted;
155 cib_tls_close(
cib_t * cib)
159 #ifdef HAVE_GNUTLS_GNUTLS_H
160 if (private->encrypted) {
161 if (private->command.tls_session) {
162 gnutls_bye(*(private->command.tls_session), GNUTLS_SHUT_RDWR);
163 gnutls_deinit(*(private->command.tls_session));
164 gnutls_free(private->command.tls_session);
167 if (private->callback.tls_session) {
168 gnutls_bye(*(private->callback.tls_session), GNUTLS_SHUT_RDWR);
169 gnutls_deinit(*(private->callback.tls_session));
170 gnutls_free(private->callback.tls_session);
172 private->command.tls_session = NULL;
173 private->callback.tls_session = NULL;
174 if (remote_gnutls_credentials_init) {
175 gnutls_anon_free_client_credentials(anon_cred_c);
176 gnutls_global_deinit();
177 remote_gnutls_credentials_init = FALSE;
182 if (private->command.tcp_socket) {
183 shutdown(private->command.tcp_socket, SHUT_RDWR);
184 close(private->command.tcp_socket);
186 if (private->callback.tcp_socket) {
187 shutdown(private->callback.tcp_socket, SHUT_RDWR);
188 close(private->callback.tcp_socket);
190 private->command.tcp_socket = 0;
191 private->callback.tcp_socket = 0;
193 free(private->command.buffer);
194 free(private->callback.buffer);
195 private->command.buffer = NULL;
196 private->callback.buffer = NULL;
207 int disconnected = 0;
209 xmlNode *answer = NULL;
210 xmlNode *login = NULL;
219 #ifdef HAVE_GNUTLS_GNUTLS_H
220 connection->tls_session = NULL;
224 crm_perror(LOG_ERR,
"remote tcp connection to %s:%d failed", private->server,
231 if (private->encrypted) {
233 #ifdef HAVE_GNUTLS_GNUTLS_H
234 if (remote_gnutls_credentials_init == FALSE) {
235 crm_gnutls_global_init();
236 gnutls_anon_allocate_client_credentials(&anon_cred_c);
237 remote_gnutls_credentials_init = TRUE;
244 crm_err(
"Session creation for %s:%d failed", private->server, private->port);
246 gnutls_deinit(*connection->tls_session);
247 gnutls_free(connection->tls_session);
248 connection->tls_session = NULL;
253 return -EPROTONOSUPPORT;
276 if (answer == NULL) {
285 crm_err(
"Invalid registration message: %s", msg_type);
288 }
else if (tmp_ticket == NULL) {
292 connection->
token = strdup(tmp_ticket);
303 crm_trace(
"remote client connection established");
313 crm_err(
"Connection destroyed");
314 #ifdef HAVE_GNUTLS_GNUTLS_H
315 cib_tls_close(user_data);
323 int disconnected = 0;
324 cib_t *cib = user_data;
329 free(private->command.buffer);
330 private->command.buffer = NULL;
331 crm_err(
"received late reply for remote cib connection, discarding");
342 cib_t *cib = user_data;
346 int disconnected = 0;
348 crm_info(
"Message on callback channel");
356 crm_trace(
"Activating %s callbacks...", type);
365 crm_err(
"Unknown message type: %s", type);
385 if (private->passwd == NULL) {
386 struct termios settings;
388 rc = tcgetattr(0, &settings);
390 settings.c_lflag &= ~ECHO;
391 rc = tcsetattr(0, TCSANOW, &settings);
395 fprintf(stderr,
"Password: ");
396 private->passwd = calloc(1, 1024);
397 rc = scanf(
"%1023s", private->passwd);
398 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_notice(
"%s: Opened connection to %s:%d", name, private->server, private->port);
436 fprintf(stderr,
"%s: Connection to %s:%d failed: %s\n",
450 crm_debug(
"Signing out of the CIB Service");
451 #ifdef HAVE_GNUTLS_GNUTLS_H
472 free(private->server);
474 free(private->passwd);
486 xmlNode *
data, xmlNode ** output_data,
int call_options,
const char *name)
489 int disconnected = 0;
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 CIB service", op);
539 }
else if (!(call_options & cib_sync_call)) {
543 crm_trace(
"Waiting for a synchronous reply");
545 start_time = time(NULL);
548 while (remaining_time > 0 && !disconnected) {
552 crm_remote_recv(&private->command, remaining_time * 1000, &disconnected);
561 if (reply_id == msg_id) {
564 }
else if (reply_id < msg_id) {
565 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
568 }
else if ((reply_id - 10000) > msg_id) {
570 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
573 crm_err(
"Received a __future__ reply:" " %d (wanted %d)", reply_id, msg_id);
580 remaining_time = time(NULL) - start_time;
590 crm_err(
"Disconnected while waiting for reply.");
592 }
else if (op_reply == NULL) {
593 crm_err(
"No reply message - empty");
609 if (rc ==
pcmk_ok || rc == -EPERM) {
618 if (output_data == NULL) {
621 }
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)
gboolean crm_remote_recv(crm_remote_t *remote, int total_timeout, int *disconnected)
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)
const char * pcmk_strerror(int rc)
int cib_remote_command_dispatch(gpointer user_data)
int(* inputfd)(cib_t *cib)
int(* dispatch)(gpointer userdata)
int(* set_connection_dnotify)(cib_t *cib, void(*dnotify)(gpointer user_data))
int crm_remote_tcp_connect(const char *host, int port)
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 pcmk_err_diff_resync
#define crm_warn(fmt, args...)
cib_api_operations_t * cmds
#define crm_debug(fmt, args...)
int crm_initiate_client_tls_handshake(crm_remote_t *remote, int timeout_ms)
int cib_remote_callback_dispatch(gpointer user_data)
void(* destroy)(gpointer userdata)
xmlNode * crm_remote_parse_buffer(crm_remote_t *remote)
#define crm_trace(fmt, args...)
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
#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 crm_element_value_int(xmlNode *data, const char *name, int *dest)
const char * crm_element_value(xmlNode *data, const char *name)
int cib_remote_free(cib_t *cib)
void free_xml(xmlNode *child)
#define F_CIB_NOTIFY_TYPE
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
#define crm_perror(level, fmt, args...)
Log a system error message.
#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)
void * crm_create_anon_tls_session(int sock, int type, void *credentials)
#define safe_str_eq(a, b)
int crm_remote_send(crm_remote_t *remote, xmlNode *msg)
#define crm_info(fmt, args...)
enum crm_ais_msg_types type
int cib_remote_signon(cib_t *cib, const char *name, enum cib_conn_type type)