root/lib/cib/cib_remote.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. cib_remote_inputfd
  2. cib_remote_set_connection_dnotify
  3. cib_remote_register_notification
  4. cib_remote_new
  5. cib_tls_close
  6. cib_tls_signon
  7. cib_remote_connection_destroy
  8. cib_remote_command_dispatch
  9. cib_remote_callback_dispatch
  10. cib_remote_signon
  11. cib_remote_signoff
  12. cib_remote_free
  13. cib_remote_perform_op
  14. cib__set_output

   1 /*
   2  * Copyright 2008-2021 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <unistd.h>
  13 #include <stdlib.h>
  14 #include <stdio.h>
  15 #include <stdarg.h>
  16 #include <string.h>
  17 #include <netdb.h>
  18 #include <termios.h>
  19 #include <sys/socket.h>
  20 
  21 #include <glib.h>
  22 
  23 #include <crm/crm.h>
  24 #include <crm/cib/internal.h>
  25 #include <crm/msg_xml.h>
  26 #include <crm/common/ipc_internal.h>
  27 #include <crm/common/mainloop.h>
  28 #include <crm/common/remote_internal.h>
  29 #include <crm/common/output_internal.h>
  30 
  31 #ifdef HAVE_GNUTLS_GNUTLS_H
  32 #  undef KEYFILE
  33 #  include <gnutls/gnutls.h>
  34 gnutls_anon_client_credentials_t anon_cred_c;
  35 
  36 #define TLS_HANDSHAKE_TIMEOUT_MS 5000
  37 
  38 const int kx_prio[] = {
  39     GNUTLS_KX_ANON_DH,
  40     0
  41 };
  42 
  43 static gboolean remote_gnutls_credentials_init = FALSE;
  44 #else
  45 typedef void gnutls_session_t;
  46 #endif
  47 
  48 #include <arpa/inet.h>
  49 
  50 #define DH_BITS 1024
  51 
  52 typedef struct cib_remote_opaque_s {
  53     int flags;
  54     int socket;
  55     int port;
  56     char *server;
  57     char *user;
  58     char *passwd;
  59     gboolean encrypted;
  60     pcmk__remote_t command;
  61     pcmk__remote_t callback;
  62     pcmk__output_t *out;
  63 
  64 } cib_remote_opaque_t;
  65 
  66 void cib_remote_connection_destroy(gpointer user_data);
  67 int cib_remote_callback_dispatch(gpointer user_data);
  68 int cib_remote_command_dispatch(gpointer user_data);
  69 int cib_remote_signon(cib_t * cib, const char *name, enum cib_conn_type type);
  70 int cib_remote_signoff(cib_t * cib);
  71 int cib_remote_free(cib_t * cib);
  72 
  73 int cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char *section,
  74                           xmlNode * data, xmlNode ** output_data, int call_options,
  75                           const char *name);
  76 
  77 static int
  78 cib_remote_inputfd(cib_t * cib)
     /* [previous][next][first][last][top][bottom][index][help] */
  79 {
  80     cib_remote_opaque_t *private = cib->variant_opaque;
  81 
  82     return private->callback.tcp_socket;
  83 }
  84 
  85 static int
  86 cib_remote_set_connection_dnotify(cib_t * cib, void (*dnotify) (gpointer user_data))
     /* [previous][next][first][last][top][bottom][index][help] */
  87 {
  88     return -EPROTONOSUPPORT;
  89 }
  90 
  91 static int
  92 cib_remote_register_notification(cib_t * cib, const char *callback, int enabled)
     /* [previous][next][first][last][top][bottom][index][help] */
  93 {
  94     xmlNode *notify_msg = create_xml_node(NULL, "cib_command");
  95     cib_remote_opaque_t *private = cib->variant_opaque;
  96 
  97     crm_xml_add(notify_msg, F_CIB_OPERATION, T_CIB_NOTIFY);
  98     crm_xml_add(notify_msg, F_CIB_NOTIFY_TYPE, callback);
  99     crm_xml_add_int(notify_msg, F_CIB_NOTIFY_ACTIVATE, enabled);
 100     pcmk__remote_send_xml(&private->callback, notify_msg);
 101     free_xml(notify_msg);
 102     return pcmk_ok;
 103 }
 104 
 105 cib_t *
 106 cib_remote_new(const char *server, const char *user, const char *passwd, int port,
     /* [previous][next][first][last][top][bottom][index][help] */
 107                gboolean encrypted)
 108 {
 109     cib_remote_opaque_t *private = NULL;
 110     cib_t *cib = cib_new_variant();
 111 
 112     private = calloc(1, sizeof(cib_remote_opaque_t));
 113 
 114     cib->variant = cib_remote;
 115     cib->variant_opaque = private;
 116 
 117     if (server) {
 118         private->server = strdup(server);
 119     }
 120 
 121     if (user) {
 122         private->user = strdup(user);
 123     }
 124 
 125     if (passwd) {
 126         private->passwd = strdup(passwd);
 127     }
 128 
 129     private->port = port;
 130     private->encrypted = encrypted;
 131 
 132     /* assign variant specific ops */
 133     cib->delegate_fn = cib_remote_perform_op;
 134     cib->cmds->signon = cib_remote_signon;
 135     cib->cmds->signoff = cib_remote_signoff;
 136     cib->cmds->free = cib_remote_free;
 137     cib->cmds->inputfd = cib_remote_inputfd;
 138 
 139     cib->cmds->register_notification = cib_remote_register_notification;
 140     cib->cmds->set_connection_dnotify = cib_remote_set_connection_dnotify;
 141 
 142     return cib;
 143 }
 144 
 145 static int
 146 cib_tls_close(cib_t * cib)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148     cib_remote_opaque_t *private = cib->variant_opaque;
 149 
 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);
 156         }
 157 
 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);
 162         }
 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;
 169         }
 170     }
 171 #endif
 172 
 173     if (private->command.tcp_socket) {
 174         shutdown(private->command.tcp_socket, SHUT_RDWR);       /* no more receptions */
 175         close(private->command.tcp_socket);
 176     }
 177     if (private->callback.tcp_socket) {
 178         shutdown(private->callback.tcp_socket, SHUT_RDWR);      /* no more receptions */
 179         close(private->callback.tcp_socket);
 180     }
 181     private->command.tcp_socket = 0;
 182     private->callback.tcp_socket = 0;
 183 
 184     free(private->command.buffer);
 185     free(private->callback.buffer);
 186     private->command.buffer = NULL;
 187     private->callback.buffer = NULL;
 188 
 189     return 0;
 190 }
 191 
 192 static int
 193 cib_tls_signon(cib_t *cib, pcmk__remote_t *connection, gboolean event_channel)
     /* [previous][next][first][last][top][bottom][index][help] */
 194 {
 195     cib_remote_opaque_t *private = cib->variant_opaque;
 196     int rc;
 197 
 198     xmlNode *answer = NULL;
 199     xmlNode *login = NULL;
 200 
 201     static struct mainloop_fd_callbacks cib_fd_callbacks = { 0, };
 202 
 203     cib_fd_callbacks.dispatch =
 204         event_channel ? cib_remote_callback_dispatch : cib_remote_command_dispatch;
 205     cib_fd_callbacks.destroy = cib_remote_connection_destroy;
 206 
 207     connection->tcp_socket = -1;
 208 #ifdef HAVE_GNUTLS_GNUTLS_H
 209     connection->tls_session = NULL;
 210 #endif
 211     rc = pcmk__connect_remote(private->server, private->port, 0, NULL,
 212                               &(connection->tcp_socket), NULL, NULL);
 213     if (rc != pcmk_rc_ok) {
 214         crm_info("Remote connection to %s:%d failed: %s " CRM_XS " rc=%d",
 215                  private->server, private->port, pcmk_rc_str(rc), rc);
 216         return -ENOTCONN;
 217     }
 218 
 219     if (private->encrypted) {
 220         /* initialize GnuTls lib */
 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;
 226         }
 227 
 228         /* bind the socket to GnuTls lib */
 229         connection->tls_session = pcmk__new_tls_session(connection->tcp_socket,
 230                                                         GNUTLS_CLIENT,
 231                                                         GNUTLS_CRD_ANON,
 232                                                         anon_cred_c);
 233         if (connection->tls_session == NULL) {
 234             cib_tls_close(cib);
 235             return -1;
 236         }
 237 
 238         if (pcmk__tls_client_handshake(connection, TLS_HANDSHAKE_TIMEOUT_MS)
 239                 != pcmk_rc_ok) {
 240             crm_err("Session creation for %s:%d failed", private->server, private->port);
 241 
 242             gnutls_deinit(*connection->tls_session);
 243             gnutls_free(connection->tls_session);
 244             connection->tls_session = NULL;
 245             cib_tls_close(cib);
 246             return -1;
 247         }
 248 #else
 249         return -EPROTONOSUPPORT;
 250 #endif
 251     }
 252 
 253     /* login to server */
 254     login = create_xml_node(NULL, "cib_command");
 255     crm_xml_add(login, "op", "authenticate");
 256     crm_xml_add(login, "user", private->user);
 257     crm_xml_add(login, "password", private->passwd);
 258     crm_xml_add(login, "hidden", "password");
 259 
 260     pcmk__remote_send_xml(connection, login);
 261     free_xml(login);
 262 
 263     rc = pcmk_ok;
 264     if (pcmk__read_remote_message(connection, -1) == ENOTCONN) {
 265         rc = -ENOTCONN;
 266     }
 267 
 268     answer = pcmk__remote_message_xml(connection);
 269 
 270     crm_log_xml_trace(answer, "Reply");
 271     if (answer == NULL) {
 272         rc = -EPROTO;
 273 
 274     } else {
 275         /* grab the token */
 276         const char *msg_type = crm_element_value(answer, F_CIB_OPERATION);
 277         const char *tmp_ticket = crm_element_value(answer, F_CIB_CLIENTID);
 278 
 279         if (!pcmk__str_eq(msg_type, CRM_OP_REGISTER, pcmk__str_casei)) {
 280             crm_err("Invalid registration message: %s", msg_type);
 281             rc = -EPROTO;
 282 
 283         } else if (tmp_ticket == NULL) {
 284             rc = -EPROTO;
 285 
 286         } else {
 287             connection->token = strdup(tmp_ticket);
 288         }
 289     }
 290     free_xml(answer);
 291     answer = NULL;
 292 
 293     if (rc != 0) {
 294         cib_tls_close(cib);
 295         return rc;
 296     }
 297 
 298     crm_trace("remote client connection established");
 299     connection->source = mainloop_add_fd("cib-remote", G_PRIORITY_HIGH,
 300                                          connection->tcp_socket, cib,
 301                                          &cib_fd_callbacks);
 302     return rc;
 303 }
 304 
 305 void
 306 cib_remote_connection_destroy(gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 307 {
 308     crm_err("Connection destroyed");
 309 #ifdef HAVE_GNUTLS_GNUTLS_H
 310     cib_tls_close(user_data);
 311 #endif
 312     return;
 313 }
 314 
 315 int
 316 cib_remote_command_dispatch(gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 317 {
 318     int rc;
 319     cib_t *cib = user_data;
 320     cib_remote_opaque_t *private = cib->variant_opaque;
 321 
 322     rc = pcmk__read_remote_message(&private->command, -1);
 323 
 324     free(private->command.buffer);
 325     private->command.buffer = NULL;
 326     crm_err("received late reply for remote cib connection, discarding");
 327 
 328     if (rc == ENOTCONN) {
 329         return -1;
 330     }
 331     return 0;
 332 }
 333 
 334 int
 335 cib_remote_callback_dispatch(gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 336 {
 337     int rc;
 338     cib_t *cib = user_data;
 339     cib_remote_opaque_t *private = cib->variant_opaque;
 340 
 341     xmlNode *msg = NULL;
 342 
 343     crm_info("Message on callback channel");
 344 
 345     rc = pcmk__read_remote_message(&private->callback, -1);
 346 
 347     msg = pcmk__remote_message_xml(&private->callback);
 348     while (msg) {
 349         const char *type = crm_element_value(msg, F_TYPE);
 350 
 351         crm_trace("Activating %s callbacks...", type);
 352 
 353         if (pcmk__str_eq(type, T_CIB, pcmk__str_casei)) {
 354             cib_native_callback(cib, msg, 0, 0);
 355 
 356         } else if (pcmk__str_eq(type, T_CIB_NOTIFY, pcmk__str_casei)) {
 357             g_list_foreach(cib->notify_list, cib_native_notify, msg);
 358 
 359         } else {
 360             crm_err("Unknown message type: %s", type);
 361         }
 362 
 363         free_xml(msg);
 364         msg = pcmk__remote_message_xml(&private->callback);
 365     }
 366 
 367     if (rc == ENOTCONN) {
 368         return -1;
 369     }
 370 
 371     return 0;
 372 }
 373 
 374 int
 375 cib_remote_signon(cib_t * cib, const char *name, enum cib_conn_type type)
     /* [previous][next][first][last][top][bottom][index][help] */
 376 {
 377     int rc = pcmk_ok;
 378     cib_remote_opaque_t *private = cib->variant_opaque;
 379 
 380     if (private->passwd == NULL) {
 381         if (private->out == NULL) {
 382             /* If no pcmk__output_t is set, just assume that a text prompt
 383              * is good enough.
 384              */
 385             pcmk__text_prompt("Password", false, &(private->passwd));
 386         } else {
 387             private->out->prompt("Password", false, &(private->passwd));
 388         }
 389     }
 390 
 391     if (private->server == NULL || private->user == NULL) {
 392         rc = -EINVAL;
 393     }
 394 
 395     if (rc == pcmk_ok) {
 396         rc = cib_tls_signon(cib, &(private->command), FALSE);
 397     }
 398 
 399     if (rc == pcmk_ok) {
 400         rc = cib_tls_signon(cib, &(private->callback), TRUE);
 401     }
 402 
 403     if (rc == pcmk_ok) {
 404         xmlNode *hello =
 405             cib_create_op(0, private->callback.token, CRM_OP_REGISTER, NULL, NULL, NULL, 0, NULL);
 406         crm_xml_add(hello, F_CIB_CLIENTNAME, name);
 407         pcmk__remote_send_xml(&private->command, hello);
 408         free_xml(hello);
 409     }
 410 
 411     if (rc == pcmk_ok) {
 412         crm_info("Opened connection to %s:%d for %s",
 413                  private->server, private->port, name);
 414         cib->state = cib_connected_command;
 415         cib->type = cib_command;
 416 
 417     } else {
 418         crm_info("Connection to %s:%d for %s failed: %s\n",
 419                  private->server, private->port, name, pcmk_strerror(rc));
 420     }
 421 
 422     return rc;
 423 }
 424 
 425 int
 426 cib_remote_signoff(cib_t * cib)
     /* [previous][next][first][last][top][bottom][index][help] */
 427 {
 428     int rc = pcmk_ok;
 429 
 430     /* cib_remote_opaque_t *private = cib->variant_opaque; */
 431 
 432     crm_debug("Disconnecting from the CIB manager");
 433 #ifdef HAVE_GNUTLS_GNUTLS_H
 434     cib_tls_close(cib);
 435 #endif
 436 
 437     cib->state = cib_disconnected;
 438     cib->type = cib_no_connection;
 439 
 440     return rc;
 441 }
 442 
 443 int
 444 cib_remote_free(cib_t * cib)
     /* [previous][next][first][last][top][bottom][index][help] */
 445 {
 446     int rc = pcmk_ok;
 447 
 448     crm_warn("Freeing CIB");
 449     if (cib->state != cib_disconnected) {
 450         rc = cib_remote_signoff(cib);
 451         if (rc == pcmk_ok) {
 452             cib_remote_opaque_t *private = cib->variant_opaque;
 453 
 454             free(private->server);
 455             free(private->user);
 456             free(private->passwd);
 457             free(cib->cmds);
 458             free(private);
 459             free(cib);
 460         }
 461     }
 462 
 463     return rc;
 464 }
 465 
 466 int
 467 cib_remote_perform_op(cib_t * cib, const char *op, const char *host, const char *section,
     /* [previous][next][first][last][top][bottom][index][help] */
 468                       xmlNode * data, xmlNode ** output_data, int call_options, const char *name)
 469 {
 470     int rc;
 471     int remaining_time = 0;
 472     time_t start_time;
 473 
 474     xmlNode *op_msg = NULL;
 475     xmlNode *op_reply = NULL;
 476 
 477     cib_remote_opaque_t *private = cib->variant_opaque;
 478 
 479     if (cib->state == cib_disconnected) {
 480         return -ENOTCONN;
 481     }
 482 
 483     if (output_data != NULL) {
 484         *output_data = NULL;
 485     }
 486 
 487     if (op == NULL) {
 488         crm_err("No operation specified");
 489         return -EINVAL;
 490     }
 491 
 492     cib->call_id++;
 493     if (cib->call_id < 1) {
 494         cib->call_id = 1;
 495     }
 496 
 497     op_msg =
 498         cib_create_op(cib->call_id, private->callback.token, op, host, section, data, call_options,
 499                       NULL);
 500     if (op_msg == NULL) {
 501         return -EPROTO;
 502     }
 503 
 504     crm_trace("Sending %s message to the CIB manager", op);
 505     if (!(call_options & cib_sync_call)) {
 506         pcmk__remote_send_xml(&private->callback, op_msg);
 507     } else {
 508         pcmk__remote_send_xml(&private->command, op_msg);
 509     }
 510     free_xml(op_msg);
 511 
 512     if ((call_options & cib_discard_reply)) {
 513         crm_trace("Discarding reply");
 514         return pcmk_ok;
 515 
 516     } else if (!(call_options & cib_sync_call)) {
 517         return cib->call_id;
 518     }
 519 
 520     crm_trace("Waiting for a synchronous reply");
 521 
 522     start_time = time(NULL);
 523     remaining_time = cib->call_timeout ? cib->call_timeout : 60;
 524 
 525     rc = pcmk_rc_ok;
 526     while (remaining_time > 0 && (rc != ENOTCONN)) {
 527         int reply_id = -1;
 528         int msg_id = cib->call_id;
 529 
 530         rc = pcmk__read_remote_message(&private->command,
 531                                        remaining_time * 1000);
 532         op_reply = pcmk__remote_message_xml(&private->command);
 533 
 534         if (!op_reply) {
 535             break;
 536         }
 537 
 538         crm_element_value_int(op_reply, F_CIB_CALLID, &reply_id);
 539 
 540         if (reply_id == msg_id) {
 541             break;
 542 
 543         } else if (reply_id < msg_id) {
 544             crm_debug("Received old reply: %d (wanted %d)", reply_id, msg_id);
 545             crm_log_xml_trace(op_reply, "Old reply");
 546 
 547         } else if ((reply_id - 10000) > msg_id) {
 548             /* wrap-around case */
 549             crm_debug("Received old reply: %d (wanted %d)", reply_id, msg_id);
 550             crm_log_xml_trace(op_reply, "Old reply");
 551         } else {
 552             crm_err("Received a __future__ reply:" " %d (wanted %d)", reply_id, msg_id);
 553         }
 554 
 555         free_xml(op_reply);
 556         op_reply = NULL;
 557 
 558         /* wasn't the right reply, try and read some more */
 559         remaining_time = time(NULL) - start_time;
 560     }
 561 
 562     /* if(IPC_ISRCONN(native->command_channel) == FALSE) { */
 563     /*      crm_err("The CIB manager disconnected: %d",  */
 564     /*              native->command_channel->ch_status); */
 565     /*      cib->state = cib_disconnected; */
 566     /* } */
 567 
 568     if (rc == ENOTCONN) {
 569         crm_err("Disconnected while waiting for reply.");
 570         return -ENOTCONN;
 571     } else if (op_reply == NULL) {
 572         crm_err("No reply message - empty");
 573         return -ENOMSG;
 574     }
 575 
 576     crm_trace("Synchronous reply received");
 577 
 578     /* Start processing the reply... */
 579     if (crm_element_value_int(op_reply, F_CIB_RC, &rc) != 0) {
 580         rc = -EPROTO;
 581     }
 582 
 583     if (rc == -pcmk_err_diff_resync) {
 584         /* This is an internal value that clients do not and should not care about */
 585         rc = pcmk_ok;
 586     }
 587 
 588     if (rc == pcmk_ok || rc == -EPERM) {
 589         crm_log_xml_debug(op_reply, "passed");
 590 
 591     } else {
 592 /*      } else if(rc == -ETIME) { */
 593         crm_err("Call failed: %s", pcmk_strerror(rc));
 594         crm_log_xml_warn(op_reply, "failed");
 595     }
 596 
 597     if (output_data == NULL) {
 598         /* do nothing more */
 599 
 600     } else if (!(call_options & cib_discard_reply)) {
 601         xmlNode *tmp = get_message_xml(op_reply, F_CIB_CALLDATA);
 602 
 603         if (tmp == NULL) {
 604             crm_trace("No output in reply to \"%s\" command %d", op, cib->call_id - 1);
 605         } else {
 606             *output_data = copy_xml(tmp);
 607         }
 608     }
 609 
 610     free_xml(op_reply);
 611 
 612     return rc;
 613 }
 614 
 615 void
 616 cib__set_output(cib_t *cib, pcmk__output_t *out)
     /* [previous][next][first][last][top][bottom][index][help] */
 617 {
 618     cib_remote_opaque_t *private;
 619 
 620     if (cib->variant != cib_remote) {
 621         return;
 622     }
 623 
 624     private = cib->variant_opaque;
 625     private->out = out;
 626 }

/* [previous][next][first][last][top][bottom][index][help] */