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");
527 if (op_msg == NULL) {
531 crm_trace(
"Sending %s message to the CIB manager", op);
547 crm_trace(
"Waiting for a synchronous reply");
549 start_time = time(NULL);
553 while (remaining_time > 0 && (
rc != ENOTCONN)) {
558 remaining_time * 1000);
567 if (reply_id == msg_id) {
570 }
else if (reply_id < msg_id) {
571 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
574 }
else if ((reply_id - 10000) > msg_id) {
576 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
579 crm_err(
"Received a __future__ reply:" " %d (wanted %d)", reply_id, msg_id);
586 remaining_time = time(NULL) - start_time;
595 if (
rc == ENOTCONN) {
596 crm_err(
"Disconnected while waiting for reply.");
598 }
else if (op_reply == NULL) {
599 crm_err(
"No reply message - empty");
624 if (output_data == NULL) {