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 return pcmk__tls_client_handshake(remote, DEFAULT_CLIENT_HANDSHAKE_TIMEOUT);
205 xmlNode *answer = NULL;
206 xmlNode *login = NULL;
215 #ifdef HAVE_GNUTLS_GNUTLS_H
216 connection->tls_session = NULL;
221 crm_info(
"Remote connection to %s:%d failed: %s " CRM_XS " rc=%d",
226 if (private->encrypted) {
228 #ifdef HAVE_GNUTLS_GNUTLS_H
229 if (remote_gnutls_credentials_init == FALSE) {
230 crm_gnutls_global_init();
231 gnutls_anon_allocate_client_credentials(&anon_cred_c);
232 remote_gnutls_credentials_init = TRUE;
236 connection->tls_session = pcmk__new_tls_session(connection->
tcp_socket,
240 if (connection->tls_session == NULL) {
245 if (cib__tls_client_handshake(connection) !=
pcmk_rc_ok) {
246 crm_err(
"Session creation for %s:%d failed", private->server, private->port);
248 gnutls_deinit(*connection->tls_session);
249 gnutls_free(connection->tls_session);
250 connection->tls_session = NULL;
255 return -EPROTONOSUPPORT;
277 if (answer == NULL) {
286 crm_err(
"Invalid registration message: %s", msg_type);
289 }
else if (tmp_ticket == NULL) {
293 connection->
token = strdup(tmp_ticket);
304 crm_trace(
"remote client connection established");
314 crm_err(
"Connection destroyed");
315 #ifdef HAVE_GNUTLS_GNUTLS_H
316 cib_tls_close(user_data);
325 cib_t *cib = user_data;
330 free(private->command.buffer);
331 private->command.buffer = NULL;
332 crm_err(
"received late reply for remote cib connection, discarding");
334 if (
rc == ENOTCONN) {
344 cib_t *cib = user_data;
349 crm_info(
"Message on callback channel");
373 if (
rc == ENOTCONN) {
386 if (private->passwd == NULL) {
387 struct termios settings;
389 rc = tcgetattr(0, &settings);
391 settings.c_lflag &= ~ECHO;
392 rc = tcsetattr(0, TCSANOW, &settings);
396 fprintf(stderr,
"Password: ");
397 private->passwd = calloc(1, 1024);
398 rc = scanf(
"%1023s", private->passwd);
399 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_info(
"Opened connection to %s:%d for %s",
432 private->server, private->port,
name);
437 crm_info(
"Connection to %s:%d for %s failed: %s\n",
451 crm_debug(
"Disconnecting from the CIB manager");
452 #ifdef HAVE_GNUTLS_GNUTLS_H
473 free(private->server);
475 free(private->passwd);
487 xmlNode *
data, xmlNode ** output_data,
int call_options,
const char *
name)
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 the CIB manager", op);
543 crm_trace(
"Waiting for a synchronous reply");
545 start_time = time(NULL);
549 while (remaining_time > 0 && (
rc != ENOTCONN)) {
554 remaining_time * 1000);
563 if (reply_id == msg_id) {
566 }
else if (reply_id < msg_id) {
567 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
570 }
else if ((reply_id - 10000) > msg_id) {
572 crm_debug(
"Received old reply: %d (wanted %d)", reply_id, msg_id);
575 crm_err(
"Received a __future__ reply:" " %d (wanted %d)", reply_id, msg_id);
582 remaining_time = time(NULL) - start_time;
591 if (
rc == ENOTCONN) {
592 crm_err(
"Disconnected while waiting for reply.");
594 }
else if (op_reply == NULL) {
595 crm_err(
"No reply message - empty");
620 if (output_data == NULL) {