19 #include <sys/socket.h>
31 #ifdef HAVE_GNUTLS_GNUTLS_H
33 # include <gnutls/gnutls.h>
34 gnutls_anon_client_credentials_t anon_cred_c;
36 #define TLS_HANDSHAKE_TIMEOUT_MS 5000
38 const int kx_prio[] = {
43 static gboolean remote_gnutls_credentials_init = FALSE;
48 #include <arpa/inet.h>
52 typedef struct cib_remote_opaque_s {
74 xmlNode *
data, xmlNode ** output_data,
int call_options,
78 cib_remote_inputfd(
cib_t * cib)
82 return private->callback.tcp_socket;
86 cib_remote_set_connection_dnotify(
cib_t * cib,
void (*dnotify) (gpointer user_data))
88 return -EPROTONOSUPPORT;
92 cib_remote_register_notification(
cib_t * cib,
const char *callback,
int enabled)
106 cib_remote_new(
const char *server,
const char *user,
const char *passwd,
int port,
118 private->server = strdup(server);
122 private->user = strdup(user);
126 private->passwd = strdup(passwd);
129 private->port = port;
130 private->encrypted = encrypted;
146 cib_tls_close(
cib_t * cib)
150 #ifdef HAVE_GNUTLS_GNUTLS_H
151 if (private->encrypted) {
152 if (private->command.tls_session) {
153 gnutls_bye(*(private->command.tls_session), GNUTLS_SHUT_RDWR);
154 gnutls_deinit(*(private->command.tls_session));
155 gnutls_free(private->command.tls_session);
158 if (private->callback.tls_session) {
159 gnutls_bye(*(private->callback.tls_session), GNUTLS_SHUT_RDWR);
160 gnutls_deinit(*(private->callback.tls_session));
161 gnutls_free(private->callback.tls_session);
163 private->command.tls_session = NULL;
164 private->callback.tls_session = NULL;
165 if (remote_gnutls_credentials_init) {
166 gnutls_anon_free_client_credentials(anon_cred_c);
167 gnutls_global_deinit();
168 remote_gnutls_credentials_init = FALSE;
173 if (private->command.tcp_socket) {
174 shutdown(private->command.tcp_socket, SHUT_RDWR);
175 close(private->command.tcp_socket);
177 if (private->callback.tcp_socket) {
178 shutdown(private->callback.tcp_socket, SHUT_RDWR);
179 close(private->callback.tcp_socket);
181 private->command.tcp_socket = 0;
182 private->callback.tcp_socket = 0;
184 free(private->command.buffer);
185 free(private->callback.buffer);
186 private->command.buffer = NULL;
187 private->callback.buffer = NULL;
198 xmlNode *answer = NULL;
199 xmlNode *login = NULL;
208 #ifdef HAVE_GNUTLS_GNUTLS_H
209 connection->tls_session = NULL;
214 crm_info(
"Remote connection to %s:%d failed: %s " CRM_XS " rc=%d",
215 private->server, private->port,
pcmk_rc_str(rc), rc);
219 if (private->encrypted) {
221 #ifdef HAVE_GNUTLS_GNUTLS_H
222 if (remote_gnutls_credentials_init == FALSE) {
223 crm_gnutls_global_init();
224 gnutls_anon_allocate_client_credentials(&anon_cred_c);
225 remote_gnutls_credentials_init = TRUE;
229 connection->tls_session = pcmk__new_tls_session(connection->
tcp_socket,
233 if (connection->tls_session == NULL) {
238 if (pcmk__tls_client_handshake(connection, TLS_HANDSHAKE_TIMEOUT_MS)
240 crm_err(
"Session creation for %s:%d failed", private->server, private->port);
242 gnutls_deinit(*connection->tls_session);
243 gnutls_free(connection->tls_session);
244 connection->tls_session = NULL;
249 return -EPROTONOSUPPORT;
271 if (answer == NULL) {
280 crm_err(
"Invalid registration message: %s", msg_type);
283 }
else if (tmp_ticket == NULL) {
287 connection->
token = strdup(tmp_ticket);
298 crm_trace(
"remote client connection established");
308 crm_err(
"Connection destroyed");
309 #ifdef HAVE_GNUTLS_GNUTLS_H
310 cib_tls_close(user_data);
319 cib_t *cib = user_data;
324 free(private->command.buffer);
325 private->command.buffer = NULL;
326 crm_err(
"received late reply for remote cib connection, discarding");
328 if (rc == ENOTCONN) {
338 cib_t *cib = user_data;
343 crm_info(
"Message on callback channel");
351 crm_trace(
"Activating %s callbacks...", type);
360 crm_err(
"Unknown message type: %s", type);
367 if (rc == ENOTCONN) {
380 if (private->passwd == NULL) {
381 if (private->out == NULL) {
387 private->out->prompt(
"Password",
false, &(private->passwd));
391 if (private->server == NULL || private->user == NULL) {
396 rc = cib_tls_signon(cib, &(private->command), FALSE);
400 rc = cib_tls_signon(cib, &(private->callback), TRUE);
412 crm_info(
"Opened connection to %s:%d for %s",
413 private->server, private->port, name);
418 crm_info(
"Connection to %s:%d for %s failed: %s\n",
432 crm_debug(
"Disconnecting from the CIB manager");
433 #ifdef HAVE_GNUTLS_GNUTLS_H
454 free(private->server);
456 free(private->passwd);
468 xmlNode *
data, xmlNode ** output_data,
int call_options,
const char *
name)
471 int remaining_time = 0;
474 xmlNode *op_msg = NULL;
475 xmlNode *op_reply = NULL;
483 if (output_data != NULL) {
488 crm_err(
"No operation specified");
500 if (op_msg == NULL) {
504 crm_trace(
"Sending %s message to the CIB manager", op);
516 }
else if (!(call_options & cib_sync_call)) {
520 crm_trace(
"Waiting for a synchronous reply");
522 start_time = time(NULL);
526 while (remaining_time > 0 && (rc != ENOTCONN)) {
531 remaining_time * 1000);
540 if (reply_id == msg_id) {
543 }
else if (reply_id < msg_id) {
544 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
547 }
else if ((reply_id - 10000) > msg_id) {
549 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
552 crm_err(
"Received a __future__ reply:" " %d (wanted %d)", reply_id, msg_id);
559 remaining_time = time(NULL) - start_time;
568 if (rc == ENOTCONN) {
569 crm_err(
"Disconnected while waiting for reply.");
571 }
else if (op_reply == NULL) {
572 crm_err(
"No reply message - empty");
588 if (rc ==
pcmk_ok || rc == -EPERM) {
597 if (output_data == NULL) {
600 }
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)
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)
void void void void void pcmk__text_prompt(const char *prompt, bool echo, char **dest)
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)
Dispatch function for mainloop file descriptor with data ready.
enum crm_ais_msg_types type
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...)
Formatted output for pacemaker tools.
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)
Destroy function for mainloop file descriptor client data.
#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)
void cib__set_output(cib_t *cib, pcmk__output_t *out)
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...)
This structure contains everything that makes up a single output formatter.
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 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)