root/daemons/execd/remoted_tls.c

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

DEFINITIONS

This source file includes following definitions.
  1. debug_log
  2. remoted__read_handshake_data
  3. lrmd_remote_client_msg
  4. lrmd_remote_client_destroy
  5. lrmd_auth_timeout_cb
  6. lrmd_remote_listen
  7. tls_server_dropped
  8. lrmd_tls_server_key_cb
  9. bind_and_listen
  10. get_address_info
  11. lrmd_init_remote_tls_server
  12. execd_stop_tls_server

   1 /*
   2  * Copyright 2012-2022 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 <glib.h>
  13 #include <unistd.h>
  14 
  15 #include <crm/crm.h>
  16 #include <crm/msg_xml.h>
  17 #include <crm/crm.h>
  18 #include <crm/msg_xml.h>
  19 #include <crm/common/mainloop.h>
  20 #include <crm/common/remote_internal.h>
  21 #include <crm/lrmd_internal.h>
  22 
  23 #include <netdb.h>
  24 #include <sys/socket.h>
  25 #include <netinet/in.h>
  26 #include <netinet/ip.h>
  27 #include <arpa/inet.h>
  28 
  29 #include "pacemaker-execd.h"
  30 
  31 #ifdef HAVE_GNUTLS_GNUTLS_H
  32 
  33 #  include <gnutls/gnutls.h>
  34 
  35 #  define LRMD_REMOTE_AUTH_TIMEOUT 10000
  36 gnutls_psk_server_credentials_t psk_cred_s;
  37 gnutls_dh_params_t dh_params;
  38 static int ssock = -1;
  39 extern int lrmd_call_id;
  40 
  41 static void
  42 debug_log(int level, const char *str)
     /* [previous][next][first][last][top][bottom][index][help] */
  43 {
  44     fputs(str, stderr);
  45 }
  46 
  47 /*!
  48  * \internal
  49  * \brief Read (more) TLS handshake data from client
  50  *
  51  * \param[in,out] client  IPC client doing handshake
  52  *
  53  * \return 0 on success or more data needed, -1 on error
  54  */
  55 static int
  56 remoted__read_handshake_data(pcmk__client_t *client)
     /* [previous][next][first][last][top][bottom][index][help] */
  57 {
  58     int rc = pcmk__read_handshake_data(client);
  59 
  60     if (rc == EAGAIN) {
  61         /* No more data is available at the moment. Just return for now;
  62          * we'll get invoked again once the client sends more.
  63          */
  64         return 0;
  65     } else if (rc != pcmk_rc_ok) {
  66         return -1;
  67     }
  68 
  69     if (client->remote->auth_timeout) {
  70         g_source_remove(client->remote->auth_timeout);
  71     }
  72     client->remote->auth_timeout = 0;
  73 
  74     pcmk__set_client_flags(client, pcmk__client_tls_handshake_complete);
  75     crm_notice("Remote client connection accepted");
  76 
  77     /* Only a client with access to the TLS key can connect, so we can treat
  78      * it as privileged.
  79      */
  80     pcmk__set_client_flags(client, pcmk__client_privileged);
  81 
  82     // Alert other clients of the new connection
  83     notify_of_new_client(client);
  84     return 0;
  85 }
  86 
  87 static int
  88 lrmd_remote_client_msg(gpointer data)
     /* [previous][next][first][last][top][bottom][index][help] */
  89 {
  90     int id = 0;
  91     int rc;
  92     xmlNode *request = NULL;
  93     pcmk__client_t *client = data;
  94 
  95     if (!pcmk_is_set(client->flags,
  96                      pcmk__client_tls_handshake_complete)) {
  97         return remoted__read_handshake_data(client);
  98     }
  99 
 100     switch (pcmk__remote_ready(client->remote, 0)) {
 101         case pcmk_rc_ok:
 102             break;
 103         case ETIME: // No message available to read
 104             return 0;
 105         default:    // Error
 106             crm_info("Remote client disconnected while polling it");
 107             return -1;
 108     }
 109 
 110     rc = pcmk__read_remote_message(client->remote, -1);
 111 
 112     request = pcmk__remote_message_xml(client->remote);
 113     while (request) {
 114         crm_element_value_int(request, F_LRMD_REMOTE_MSG_ID, &id);
 115         crm_trace("Processing remote client request %d", id);
 116         if (!client->name) {
 117             const char *value = crm_element_value(request, F_LRMD_CLIENTNAME);
 118 
 119             if (value) {
 120                 client->name = strdup(value);
 121             }
 122         }
 123 
 124         lrmd_call_id++;
 125         if (lrmd_call_id < 1) {
 126             lrmd_call_id = 1;
 127         }
 128 
 129         crm_xml_add(request, F_LRMD_CLIENTID, client->id);
 130         crm_xml_add(request, F_LRMD_CLIENTNAME, client->name);
 131         crm_xml_add_int(request, F_LRMD_CALLID, lrmd_call_id);
 132 
 133         process_lrmd_message(client, id, request);
 134         free_xml(request);
 135 
 136         /* process all the messages in the current buffer */
 137         request = pcmk__remote_message_xml(client->remote);
 138     }
 139 
 140     if (rc == ENOTCONN) {
 141         crm_info("Remote client disconnected while reading from it");
 142         return -1;
 143     }
 144 
 145     return 0;
 146 }
 147 
 148 static void
 149 lrmd_remote_client_destroy(gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 150 {
 151     pcmk__client_t *client = user_data;
 152 
 153     if (client == NULL) {
 154         return;
 155     }
 156 
 157     crm_notice("Cleaning up after remote client %s disconnected",
 158                pcmk__client_name(client));
 159 
 160     ipc_proxy_remove_provider(client);
 161 
 162     /* if this is the last remote connection, stop recurring
 163      * operations */
 164     if (pcmk__ipc_client_count() == 1) {
 165         client_disconnect_cleanup(NULL);
 166     }
 167 
 168     if (client->remote->tls_session) {
 169         void *sock_ptr;
 170         int csock;
 171 
 172         sock_ptr = gnutls_transport_get_ptr(*client->remote->tls_session);
 173         csock = GPOINTER_TO_INT(sock_ptr);
 174 
 175         gnutls_bye(*client->remote->tls_session, GNUTLS_SHUT_RDWR);
 176         gnutls_deinit(*client->remote->tls_session);
 177         gnutls_free(client->remote->tls_session);
 178         close(csock);
 179     }
 180 
 181     lrmd_client_destroy(client);
 182     return;
 183 }
 184 
 185 static gboolean
 186 lrmd_auth_timeout_cb(gpointer data)
     /* [previous][next][first][last][top][bottom][index][help] */
 187 {
 188     pcmk__client_t *client = data;
 189 
 190     client->remote->auth_timeout = 0;
 191 
 192     if (pcmk_is_set(client->flags,
 193                     pcmk__client_tls_handshake_complete)) {
 194         return FALSE;
 195     }
 196 
 197     mainloop_del_fd(client->remote->source);
 198     client->remote->source = NULL;
 199     crm_err("Remote client authentication timed out");
 200 
 201     return FALSE;
 202 }
 203 
 204 // Dispatch callback for remote server socket
 205 static int
 206 lrmd_remote_listen(gpointer data)
     /* [previous][next][first][last][top][bottom][index][help] */
 207 {
 208     int csock = -1;
 209     gnutls_session_t *session = NULL;
 210     pcmk__client_t *new_client = NULL;
 211 
 212     // For client socket
 213     static struct mainloop_fd_callbacks lrmd_remote_fd_cb = {
 214         .dispatch = lrmd_remote_client_msg,
 215         .destroy = lrmd_remote_client_destroy,
 216     };
 217 
 218     CRM_CHECK(ssock >= 0, return TRUE);
 219 
 220     if (pcmk__accept_remote_connection(ssock, &csock) != pcmk_rc_ok) {
 221         return TRUE;
 222     }
 223 
 224     session = pcmk__new_tls_session(csock, GNUTLS_SERVER, GNUTLS_CRD_PSK,
 225                                     psk_cred_s);
 226     if (session == NULL) {
 227         close(csock);
 228         return TRUE;
 229     }
 230 
 231     new_client = pcmk__new_unauth_client(NULL);
 232     new_client->remote = calloc(1, sizeof(pcmk__remote_t));
 233     pcmk__set_client_flags(new_client, pcmk__client_tls);
 234     new_client->remote->tls_session = session;
 235 
 236     // Require the client to authenticate within this time
 237     new_client->remote->auth_timeout = g_timeout_add(LRMD_REMOTE_AUTH_TIMEOUT,
 238                                                      lrmd_auth_timeout_cb,
 239                                                      new_client);
 240     crm_info("Remote client pending authentication "
 241              CRM_XS " %p id: %s", new_client, new_client->id);
 242 
 243     new_client->remote->source =
 244         mainloop_add_fd("pacemaker-remote-client", G_PRIORITY_DEFAULT, csock,
 245                         new_client, &lrmd_remote_fd_cb);
 246     return TRUE;
 247 }
 248 
 249 static void
 250 tls_server_dropped(gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 251 {
 252     crm_notice("TLS server session ended");
 253     /* If we are in the process of shutting down, then we should actually exit.
 254      * bz#1804259
 255      */
 256     execd_exit_if_shutting_down();
 257     return;
 258 }
 259 
 260 // \return 0 on success, -1 on error (gnutls_psk_server_credentials_function)
 261 static int
 262 lrmd_tls_server_key_cb(gnutls_session_t session, const char *username, gnutls_datum_t * key)
     /* [previous][next][first][last][top][bottom][index][help] */
 263 {
 264     return (lrmd__init_remote_key(key) == pcmk_rc_ok)? 0 : -1;
 265 }
 266 
 267 static int
 268 bind_and_listen(struct addrinfo *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 269 {
 270     int optval;
 271     int fd;
 272     int rc;
 273     char buffer[INET6_ADDRSTRLEN] = { 0, };
 274 
 275     pcmk__sockaddr2str(addr->ai_addr, buffer);
 276     crm_trace("Attempting to bind to address %s", buffer);
 277 
 278     fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
 279     if (fd < 0) {
 280         crm_perror(LOG_ERR, "Listener socket creation failed");
 281         return -1;
 282     }
 283 
 284     /* reuse address */
 285     optval = 1;
 286     rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
 287     if (rc < 0) {
 288         crm_perror(LOG_ERR, "Local address reuse not allowed on %s", buffer);
 289         close(fd);
 290         return -1;
 291     }
 292 
 293     if (addr->ai_family == AF_INET6) {
 294         optval = 0;
 295         rc = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval));
 296         if (rc < 0) {
 297             crm_perror(LOG_INFO, "Couldn't disable IPV6-only on %s", buffer);
 298             close(fd);
 299             return -1;
 300         }
 301     }
 302 
 303     if (bind(fd, addr->ai_addr, addr->ai_addrlen) != 0) {
 304         crm_perror(LOG_ERR, "Cannot bind to %s", buffer);
 305         close(fd);
 306         return -1;
 307     }
 308 
 309     if (listen(fd, 10) == -1) {
 310         crm_perror(LOG_ERR, "Cannot listen on %s", buffer);
 311         close(fd);
 312         return -1;
 313     }
 314     return fd;
 315 }
 316 
 317 static int
 318 get_address_info(const char *bind_name, int port, struct addrinfo **res)
     /* [previous][next][first][last][top][bottom][index][help] */
 319 {
 320     int rc;
 321     char port_str[6]; // at most "65535"
 322     struct addrinfo hints;
 323 
 324     memset(&hints, 0, sizeof(struct addrinfo));
 325     hints.ai_flags = AI_PASSIVE;
 326     hints.ai_family = AF_UNSPEC; // IPv6 or IPv4
 327     hints.ai_socktype = SOCK_STREAM;
 328     hints.ai_protocol = IPPROTO_TCP;
 329 
 330     snprintf(port_str, sizeof(port_str), "%d", port);
 331     rc = getaddrinfo(bind_name, port_str, &hints, res);
 332     if (rc) {
 333         crm_err("Unable to get IP address(es) for %s: %s",
 334                 (bind_name? bind_name : "local node"), gai_strerror(rc));
 335         return -EADDRNOTAVAIL;
 336     }
 337     return pcmk_ok;
 338 }
 339 
 340 int
 341 lrmd_init_remote_tls_server(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 342 {
 343     int filter;
 344     int port = crm_default_remote_port();
 345     struct addrinfo *res = NULL, *iter;
 346     gnutls_datum_t psk_key = { NULL, 0 };
 347     const char *bind_name = getenv("PCMK_remote_address");
 348 
 349     static struct mainloop_fd_callbacks remote_listen_fd_callbacks = {
 350         .dispatch = lrmd_remote_listen,
 351         .destroy = tls_server_dropped,
 352     };
 353 
 354     CRM_CHECK(ssock == -1, return ssock);
 355 
 356     crm_debug("Starting TLS listener on %s port %d",
 357               (bind_name? bind_name : "all addresses on"), port);
 358     crm_gnutls_global_init();
 359     gnutls_global_set_log_function(debug_log);
 360 
 361     if (pcmk__init_tls_dh(&dh_params) != pcmk_rc_ok) {
 362         return -1;
 363     }
 364     gnutls_psk_allocate_server_credentials(&psk_cred_s);
 365     gnutls_psk_set_server_credentials_function(psk_cred_s, lrmd_tls_server_key_cb);
 366     gnutls_psk_set_server_dh_params(psk_cred_s, dh_params);
 367 
 368     /* The key callback won't get called until the first client connection
 369      * attempt. Do it once here, so we can warn the user at start-up if we can't
 370      * read the key. We don't error out, though, because it's fine if the key is
 371      * going to be added later.
 372      */
 373     if (lrmd__init_remote_key(&psk_key) != pcmk_rc_ok) {
 374         crm_warn("A cluster connection will not be possible until the key is available");
 375     }
 376     gnutls_free(psk_key.data);
 377 
 378     if (get_address_info(bind_name, port, &res) != pcmk_ok) {
 379         return -1;
 380     }
 381 
 382     /* Currently we listen on only one address from the resulting list (the
 383      * first IPv6 address we can bind to if possible, otherwise the first IPv4
 384      * address we can bind to). When bind_name is NULL, this should be the
 385      * respective wildcard address.
 386      *
 387      * @TODO If there is demand for specifying more than one address, allow
 388      * bind_name to be a space-separated list, call getaddrinfo() for each,
 389      * and create a socket for each result (set IPV6_V6ONLY on IPv6 sockets
 390      * since IPv4 listeners will have their own sockets).
 391      */
 392     iter = res;
 393     filter = AF_INET6;
 394     while (iter) {
 395         if (iter->ai_family == filter) {
 396             ssock = bind_and_listen(iter);
 397         }
 398         if (ssock != -1) {
 399             break;
 400         }
 401 
 402         iter = iter->ai_next;
 403         if (iter == NULL && filter == AF_INET6) {
 404             iter = res;
 405             filter = AF_INET;
 406         }
 407     }
 408 
 409     if (ssock >= 0) {
 410         mainloop_add_fd("pacemaker-remote-server", G_PRIORITY_DEFAULT, ssock,
 411                         NULL, &remote_listen_fd_callbacks);
 412         crm_debug("Started TLS listener on %s port %d",
 413                   (bind_name? bind_name : "all addresses on"), port);
 414     }
 415     freeaddrinfo(res);
 416     return ssock;
 417 }
 418 
 419 void
 420 execd_stop_tls_server(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 421 {
 422     if (psk_cred_s) {
 423         gnutls_psk_free_server_credentials(psk_cred_s);
 424         psk_cred_s = 0;
 425     }
 426 
 427     if (ssock >= 0) {
 428         close(ssock);
 429         ssock = -1;
 430     }
 431 }
 432 #endif

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