19 #include <sys/socket.h> 32 #include <gnutls/gnutls.h> 35 #define TLS_HANDSHAKE_TIMEOUT 5 39 #include <arpa/inet.h> 41 typedef struct cib_remote_opaque_s {
55 cib_remote_perform_op(
cib_t *cib,
const char *op,
const char *
host,
56 const char *section, xmlNode *
data,
57 xmlNode **output_data,
int call_options,
58 const char *user_name)
61 int remaining_time = 0;
64 xmlNode *op_msg = NULL;
65 xmlNode *op_reply = NULL;
73 if (output_data != NULL) {
78 crm_err(
"No operation specified");
94 crm_trace(
"Sending %s message to the CIB manager", op);
110 crm_trace(
"Waiting for a synchronous reply");
112 start_time = time(NULL);
116 while (remaining_time > 0 && (rc != ENOTCONN)) {
121 remaining_time * 1000);
130 if (reply_id == msg_id) {
133 }
else if (reply_id < msg_id) {
134 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
137 }
else if ((reply_id - 10000) > msg_id) {
139 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
142 crm_err(
"Received a __future__ reply:" " %d (wanted %d)", reply_id, msg_id);
149 remaining_time = time(NULL) - start_time;
152 if (rc == ENOTCONN) {
153 crm_err(
"Disconnected while waiting for reply.");
155 }
else if (op_reply == NULL) {
156 crm_err(
"No reply message - empty");
172 if (rc ==
pcmk_ok || rc == -EPERM) {
180 if (output_data == NULL) {
201 cib_remote_callback_dispatch(gpointer user_data)
204 cib_t *cib = user_data;
208 const char *
type = NULL;
214 if (private->start_time == 0) {
215 private->start_time = time(NULL);
226 if (time(NULL) >= private->start_time + private->timeout_sec) {
227 crm_info(
"Error reading from CIB manager connection: %s",
237 crm_info(
"Error reading from CIB manager connection: %s",
244 private->start_time = 0;
261 private->start_time = 0;
266 cib_remote_command_dispatch(gpointer user_data)
269 cib_t *cib = user_data;
273 if (private->start_time == 0) {
274 private->start_time = time(NULL);
280 if (time(NULL) >= private->start_time + private->timeout_sec) {
281 crm_info(
"Error reading from CIB manager connection: %s",
290 free(private->command.buffer);
291 private->command.buffer = NULL;
292 crm_err(
"received late reply for remote cib connection, discarding");
295 crm_info(
"Error reading from CIB manager connection: %s",
300 private->start_time = 0;
305 cib_tls_close(
cib_t *cib)
309 if (private->encrypted) {
310 if (private->command.tls_session) {
311 gnutls_bye(private->command.tls_session, GNUTLS_SHUT_RDWR);
312 gnutls_deinit(private->command.tls_session);
315 if (private->callback.tls_session) {
316 gnutls_bye(private->callback.tls_session, GNUTLS_SHUT_RDWR);
317 gnutls_deinit(private->callback.tls_session);
320 private->command.tls_session = NULL;
321 private->callback.tls_session = NULL;
326 if (private->command.tcp_socket) {
327 shutdown(private->command.tcp_socket, SHUT_RDWR);
328 close(private->command.tcp_socket);
330 if (private->callback.tcp_socket) {
331 shutdown(private->callback.tcp_socket, SHUT_RDWR);
332 close(private->callback.tcp_socket);
334 private->command.tcp_socket = 0;
335 private->callback.tcp_socket = 0;
337 free(private->command.buffer);
338 free(private->callback.buffer);
339 private->command.buffer = NULL;
340 private->callback.buffer = NULL;
346 cib_remote_connection_destroy(gpointer user_data)
348 crm_err(
"Connection destroyed");
349 cib_tls_close(user_data);
358 xmlNode *answer = NULL;
359 xmlNode *login = NULL;
364 event_channel ? cib_remote_callback_dispatch : cib_remote_command_dispatch;
365 cib_fd_callbacks.
destroy = cib_remote_connection_destroy;
372 crm_info(
"Remote connection to %s:%d failed: %s " QB_XS
" rc=%d",
373 private->server, private->port,
pcmk_rc_str(rc), rc);
377 if (private->encrypted) {
379 int tls_rc = GNUTLS_E_SUCCESS;
381 rc =
pcmk__init_tls(&tls,
false, use_cert ? GNUTLS_CRD_CERTIFICATE : GNUTLS_CRD_ANON);
396 crm_err(
"Remote CIB session creation for %s:%d failed: %s",
397 private->server, private->port,
398 (rc == EPROTO)? gnutls_strerror(tls_rc) :
pcmk_rc_str(rc));
431 if (answer == NULL) {
441 crm_err(
"Invalid registration message: %s", msg_type);
444 }
else if (tmp_ticket == NULL) {
448 connection->
token = strdup(tmp_ticket);
459 crm_trace(
"remote client connection established");
460 private->timeout_sec = 60;
472 xmlNode *hello = NULL;
478 if (private->passwd == NULL) {
479 if (private->out == NULL) {
485 private->out->prompt(
"Password",
false, &(private->passwd));
489 if (private->server == NULL || private->user == NULL) {
494 rc = cib_tls_signon(cib, &(private->command), FALSE);
499 rc = cib_tls_signon(cib, &(private->callback), TRUE);
516 crm_info(
"Opened connection to %s:%d for %s",
517 private->server, private->port,
name);
522 crm_info(
"Connection to %s:%d for %s failed: %s\n",
530 cib_remote_signoff(
cib_t *cib)
534 crm_debug(
"Disconnecting from the CIB manager");
545 cib_remote_free(
cib_t *cib)
551 rc = cib_remote_signoff(cib);
555 free(private->server);
557 free(private->passwd);
569 cib_remote_register_notification(
cib_t * cib,
const char *callback,
int enabled)
583 cib_remote_set_connection_dnotify(
cib_t * cib,
void (*dnotify) (gpointer user_data))
585 return -EPROTONOSUPPORT;
606 cib_remote_client_id(
const cib_t *cib,
const char **async_id,
607 const char **sync_id)
611 if (async_id != NULL) {
613 *async_id =
private->callback.token;
615 if (sync_id != NULL) {
617 *sync_id =
private->command.token;
623 cib_remote_new(
const char *server,
const char *user,
const char *passwd,
int port,
635 if (
private == NULL) {
646 private->port = port;
647 private->encrypted = encrypted;
void cib__set_output(cib_t *cib, pcmk__output_t *out)
xmlNode * pcmk__xml_copy(xmlNode *parent, xmlNode *src)
#define PCMK__VALUE_PASSWORD
xmlNode * pcmk__xe_first_child(const xmlNode *parent, const char *node_name, const char *attr_n, const char *attr_v)
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)
int pcmk_rc2legacy(int rc)
void void void void void pcmk__text_prompt(const char *prompt, bool echo, char **dest)
enum pcmk_ipc_server type
void(* destroy)(gpointer userdata)
Destroy function for mainloop file descriptor client data.
#define PCMK__XA_CIB_CLIENTID
int(* signoff)(cib_t *cib)
#define PCMK__XE_CIB_COMMAND
int pcmk__init_tls(pcmk__tls_t **tls, bool server, gnutls_credentials_type_t cred_type)
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
#define PCMK__XA_CIB_NOTIFY_TYPE
Wrappers for and extensions to glib mainloop.
xmlNode * pcmk__xe_create(xmlNode *parent, const char *name)
int pcmk__tls_client_handshake(pcmk__remote_t *remote, int timeout_sec, int *gnutls_rc)
int pcmk__read_available_remote_data(pcmk__remote_t *remote)
void pcmk__xml_free(xmlNode *xml)
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
Create an XML attribute with specified name and value.
void cib_native_notify(gpointer data, gpointer user_data)
int(* dispatch)(gpointer userdata)
Dispatch function for mainloop file descriptor with data ready.
cib_t * cib_new_variant(void)
int(* set_connection_dnotify)(cib_t *cib, void(*dnotify)(gpointer user_data))
#define crm_warn(fmt, args...)
Formatted output for pacemaker tools.
cib_api_operations_t * cmds
#define crm_debug(fmt, args...)
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...)
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
#define PCMK__XE_CIB_CALLDATA
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)
Wrappers for and extensions to libxml2.
struct cib_remote_opaque_s cib_remote_opaque_t
#define crm_log_xml_warn(xml, text)
int pcmk__remote_send_xml(pcmk__remote_t *remote, const xmlNode *msg)
gnutls_session_t pcmk__new_tls_session(pcmk__tls_t *tls, int csock)
void pcmk__free_tls(pcmk__tls_t *tls)
int cib__create_op(cib_t *cib, const char *op, const char *host, const char *section, xmlNode *data, int call_options, const char *user_name, const char *client_name, xmlNode **op_msg)
#define pcmk__str_copy(str)
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
#define TLS_HANDSHAKE_TIMEOUT
#define pcmk_err_diff_resync
int(* end_transaction)(cib_t *cib, bool commit, int call_options)
End and optionally commit this client's CIB transaction.
bool pcmk__x509_enabled(void)
#define crm_err(fmt, args...)
int cib__extend_transaction(cib_t *cib, xmlNode *request)
int crm_element_value_int(const xmlNode *data, const char *name, int *dest)
Retrieve the integer value of an XML attribute.
#define PCMK__XA_PASSWORD
This structure contains everything that makes up a single output formatter.
gnutls_session_t tls_session
void cib_native_callback(cib_t *cib, xmlNode *msg, int call_id, int rc)
#define crm_log_xml_trace(xml, text)
int pcmk__read_remote_message(pcmk__remote_t *remote, int timeout_ms)
void pcmk__tls_check_cert_expiration(gnutls_session_t session)
cib_t * cib_remote_new(const char *server, const char *user, const char *passwd, int port, gboolean encrypted)
#define PCMK__XA_CIB_NOTIFY_ACTIVATE
int(* client_id)(const cib_t *cib, const char **async_id, const char **sync_id)
Get the given CIB connection's unique client identifier(s)
#define PCMK__XA_CIB_CALLID
#define crm_info(fmt, args...)
Process request when the client commits the active transaction.
xmlNode * pcmk__remote_message_xml(pcmk__remote_t *remote)
#define PCMK__VALUE_CIB_NOTIFY
const char * crm_xml_add_int(xmlNode *node, const char *name, int value)
Create an XML attribute with specified name and integer value.