13 #include <sys/param.h> 15 #include <sys/types.h> 18 #include <sys/socket.h> 19 #include <arpa/inet.h> 20 #include <netinet/in.h> 21 #include <netinet/ip.h> 22 #include <netinet/tcp.h> 36 #ifdef HAVE_GNUTLS_GNUTLS_H 37 # include <gnutls/gnutls.h> 41 #ifdef HAVE_LINUX_SWAB_H 42 # include <linux/swab.h> 48 #define __swab16(x) ((uint16_t)( \ 49 (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \ 50 (((uint16_t)(x) & (uint16_t)0xff00U) >> 8))) 52 #define __swab32(x) ((uint32_t)( \ 53 (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \ 54 (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \ 55 (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \ 56 (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24))) 58 #define __swab64(x) ((uint64_t)( \ 59 (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \ 60 (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \ 61 (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \ 62 (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \ 63 (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \ 64 (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \ 65 (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \ 66 (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56))) 69 #define REMOTE_MSG_VERSION 1 70 #define ENDIAN_LOCAL 0xBADADBBD 72 struct remote_header_v0 {
97 static struct remote_header_v0 *
100 struct remote_header_v0 *header = (
struct remote_header_v0 *)remote->
buffer;
101 if(remote->
buffer_offset <
sizeof(
struct remote_header_v0)) {
109 crm_err(
"Invalid message detected, endian mismatch: %" X32T 110 " is neither %" X32T " nor the swab'd %" X32T,
116 header->flags =
__swab64(header->flags);
117 header->endian =
__swab32(header->endian);
119 header->version =
__swab32(header->version);
120 header->size_total =
__swab32(header->size_total);
121 header->payload_offset =
__swab32(header->payload_offset);
122 header->payload_compressed =
__swab32(header->payload_compressed);
123 header->payload_uncompressed =
__swab32(header->payload_uncompressed);
129 #ifdef HAVE_GNUTLS_GNUTLS_H 132 pcmk__tls_client_handshake(
pcmk__remote_t *remote,
int timeout_ms)
136 time_t time_limit = time(NULL) + timeout_ms / 1000;
139 rc = gnutls_handshake(*remote->tls_session);
140 if ((
rc == GNUTLS_E_INTERRUPTED) || (
rc == GNUTLS_E_AGAIN)) {
144 crm_trace(
"TLS handshake poll failed: %s (%d)",
155 }
while (time(NULL) < time_limit);
168 const char *dh_min_bits_s = getenv(
"PCMK_dh_min_bits");
177 if (dh_min_bits > 0) {
178 crm_info(
"Requiring server use a Diffie-Hellman prime of at least %d bits",
180 gnutls_dh_set_prime_bits(*session, dh_min_bits);
186 get_bound_dh_bits(
unsigned int dh_bits)
188 const char *dh_min_bits_s = getenv(
"PCMK_dh_min_bits");
189 const char *dh_max_bits_s = getenv(
"PCMK_dh_max_bits");
198 if ((dh_min_bits > 0) && (dh_max_bits > 0)
199 && (dh_max_bits < dh_min_bits)) {
200 crm_warn(
"Ignoring PCMK_dh_max_bits because it is less than PCMK_dh_min_bits");
204 if ((dh_min_bits > 0) && (dh_bits < dh_min_bits)) {
207 if ((dh_max_bits > 0) && (dh_bits > dh_max_bits)) {
225 pcmk__new_tls_session(
int csock,
unsigned int conn_type,
226 gnutls_credentials_type_t cred_type,
void *credentials)
228 int rc = GNUTLS_E_SUCCESS;
229 const char *prio_base = NULL;
240 prio_base = getenv(
"PCMK_tls_priorities");
241 if (prio_base == NULL) {
245 (cred_type == GNUTLS_CRD_ANON)?
"+ANON-DH" :
"+DHE-PSK:+PSK");
248 if (session == NULL) {
249 rc = GNUTLS_E_MEMORY_ERROR;
253 rc = gnutls_init(session, conn_type);
254 if (
rc != GNUTLS_E_SUCCESS) {
262 rc = gnutls_priority_set_direct(*session, prio, NULL);
263 if (
rc != GNUTLS_E_SUCCESS) {
266 if (conn_type == GNUTLS_CLIENT) {
267 set_minimum_dh_bits(session);
270 gnutls_transport_set_ptr(*session,
271 (gnutls_transport_ptr_t) GINT_TO_POINTER(csock));
273 rc = gnutls_credentials_set(*session, cred_type, credentials);
274 if (
rc != GNUTLS_E_SUCCESS) {
281 crm_err(
"Could not initialize %s TLS %s session: %s " 282 CRM_XS " rc=%d priority='%s'",
283 (cred_type == GNUTLS_CRD_ANON)?
"anonymous" :
"PSK",
284 (conn_type == GNUTLS_SERVER)?
"server" :
"client",
285 gnutls_strerror(
rc),
rc, prio);
287 if (session != NULL) {
288 gnutls_free(session);
309 pcmk__init_tls_dh(gnutls_dh_params_t *dh_params)
311 int rc = GNUTLS_E_SUCCESS;
312 unsigned int dh_bits = 0;
314 rc = gnutls_dh_params_init(dh_params);
315 if (
rc != GNUTLS_E_SUCCESS) {
319 #ifdef HAVE_GNUTLS_SEC_PARAM_TO_PK_BITS 320 dh_bits = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH,
321 GNUTLS_SEC_PARAM_NORMAL);
323 rc = GNUTLS_E_DH_PRIME_UNACCEPTABLE;
329 dh_bits = get_bound_dh_bits(dh_bits);
331 crm_info(
"Generating Diffie-Hellman parameters with %u-bit prime for TLS",
333 rc = gnutls_dh_params_generate2(*dh_params, dh_bits);
334 if (
rc != GNUTLS_E_SUCCESS) {
341 crm_err(
"Could not initialize Diffie-Hellman parameters for TLS: %s " 365 rc = gnutls_handshake(*client->
remote->tls_session);
366 }
while (
rc == GNUTLS_E_INTERRUPTED);
368 if (
rc == GNUTLS_E_AGAIN) {
373 }
else if (
rc != GNUTLS_E_SUCCESS) {
374 crm_err(
"TLS handshake with remote client failed: %s " 385 const char *unsent = iov->iov_base;
386 size_t unsent_len = iov->iov_len;
389 if (unsent == NULL) {
393 crm_trace(
"Sending TLS message of %llu bytes",
394 (
unsigned long long) unsent_len);
396 gnutls_rc = gnutls_record_send(*session, unsent, unsent_len);
398 if (gnutls_rc == GNUTLS_E_INTERRUPTED || gnutls_rc == GNUTLS_E_AGAIN) {
399 crm_trace(
"Retrying to send %llu bytes remaining",
400 (
unsigned long long) unsent_len);
402 }
else if (gnutls_rc < 0) {
405 gnutls_strerror((
int) gnutls_rc),
406 (
long long) gnutls_rc);
409 }
else if (gnutls_rc < unsent_len) {
410 crm_trace(
"Sent %lld of %llu bytes remaining",
411 (
long long) gnutls_rc, (
unsigned long long) unsent_len);
412 unsent_len -= gnutls_rc;
415 crm_trace(
"Sent all %lld bytes remaining", (
long long) gnutls_rc);
425 send_plaintext(
int sock,
struct iovec *iov)
427 const char *unsent = iov->iov_base;
428 size_t unsent_len = iov->iov_len;
431 if (unsent == NULL) {
435 crm_debug(
"Sending plaintext message of %llu bytes to socket %d",
436 (
unsigned long long) unsent_len, sock);
438 write_rc = write(sock, unsent, unsent_len);
442 if ((errno == EINTR) || (errno == EAGAIN)) {
443 crm_trace(
"Retrying to send %llu bytes remaining to socket %d",
444 (
unsigned long long) unsent_len, sock);
453 }
else if (write_rc < unsent_len) {
454 crm_trace(
"Sent %lld of %llu bytes remaining",
455 (
long long) write_rc, (
unsigned long long) unsent_len);
457 unsent_len -= write_rc;
461 crm_trace(
"Sent all %lld bytes remaining: %.100s",
462 (
long long) write_rc, (
char *) (iov->iov_base));
471 remote_send_iovs(
pcmk__remote_t *remote,
struct iovec *iov,
int iovs)
475 for (
int lpc = 0; (lpc < iovs) && (
rc ==
pcmk_rc_ok); lpc++) {
476 #ifdef HAVE_GNUTLS_GNUTLS_H 477 if (remote->tls_session) {
478 rc = send_tls(remote->tls_session, &(iov[lpc]));
485 rc = ESOCKTNOSUPPORT;
504 static uint64_t
id = 0;
505 char *xml_text = NULL;
508 struct remote_header_v0 *header;
510 CRM_CHECK((remote != NULL) && (msg != NULL),
return EINVAL);
513 CRM_CHECK(xml_text != NULL,
return EINVAL);
515 header = calloc(1,
sizeof(
struct remote_header_v0));
518 iov[0].iov_base = header;
519 iov[0].iov_len =
sizeof(
struct remote_header_v0);
521 iov[1].iov_base = xml_text;
522 iov[1].iov_len = 1 + strlen(xml_text);
528 header->payload_offset = iov[0].iov_len;
529 header->payload_uncompressed = iov[1].iov_len;
530 header->size_total = iov[0].iov_len + iov[1].iov_len;
532 rc = remote_send_iovs(remote, iov, 2);
534 crm_err(
"Could not send remote message: %s " CRM_XS " rc=%d",
538 free(iov[0].iov_base);
539 free(iov[1].iov_base);
556 struct remote_header_v0 *header = localized_remote_header(remote);
558 if (header == NULL) {
563 if (header->payload_compressed) {
565 unsigned int size_u = 1 + header->payload_uncompressed;
566 char *uncompressed = calloc(1, header->payload_offset + size_u);
568 crm_trace(
"Decompressing message data %d bytes into %d bytes",
569 header->payload_compressed, size_u);
571 rc = BZ2_bzBuffToBuffDecompress(uncompressed + header->payload_offset, &size_u,
572 remote->
buffer + header->payload_offset,
573 header->payload_compressed, 1, 0);
576 crm_warn(
"Couldn't decompress v%d message, we only understand v%d",
581 }
else if (
rc != BZ_OK) {
588 CRM_ASSERT(size_u == header->payload_uncompressed);
590 memcpy(uncompressed, remote->
buffer, header->payload_offset);
591 remote->
buffer_size = header->payload_offset + size_u;
594 remote->
buffer = uncompressed;
595 header = localized_remote_header(remote);
601 CRM_LOG_ASSERT(remote->
buffer[
sizeof(
struct remote_header_v0) + header->payload_uncompressed - 1] == 0);
605 crm_warn(
"Couldn't parse v%d message, we only understand v%d",
608 }
else if (xml == NULL) {
609 crm_err(
"Couldn't parse: '%.120s'", remote->
buffer + header->payload_offset);
618 #ifdef HAVE_GNUTLS_GNUTLS_H 619 if (remote->tls_session) {
620 void *sock_ptr = gnutls_transport_get_ptr(*remote->tls_session);
622 return GPOINTER_TO_INT(sock_ptr);
630 crm_err(
"Remote connection type undetermined (bug?)");
648 struct pollfd fds = { 0, };
654 sock = get_remote_socket(remote);
669 if (errno == EINTR && (
timeout > 0)) {
670 timeout = timeout_ms - ((time(NULL) - start) * 1000);
677 }
while (
rc < 0 && errno == EINTR);
701 size_t read_len =
sizeof(
struct remote_header_v0);
702 struct remote_header_v0 *header = localized_remote_header(remote);
703 bool received =
false;
708 read_len = header->size_total;
714 crm_trace(
"Expanding buffer to %llu bytes",
719 #ifdef HAVE_GNUTLS_GNUTLS_H 720 if (!received && remote->tls_session) {
721 read_rc = gnutls_record_recv(*(remote->tls_session),
724 if (read_rc == GNUTLS_E_INTERRUPTED) {
726 }
else if (read_rc == GNUTLS_E_AGAIN) {
728 }
else if (read_rc < 0) {
729 crm_debug(
"TLS receive failed: %s (%lld)",
730 gnutls_strerror(read_rc), (
long long) read_rc);
748 crm_err(
"Remote connection type undetermined (bug?)");
749 return ESOCKTNOSUPPORT;
757 crm_trace(
"Received %lld more bytes (%llu total)",
761 }
else if ((
rc == EINTR) || (
rc == EAGAIN)) {
762 crm_trace(
"No data available for non-blocking remote read: %s (%d)",
765 }
else if (read_rc == 0) {
766 crm_debug(
"End of remote data encountered after %llu bytes",
771 crm_debug(
"Error receiving remote data after %llu bytes: %s (%d)",
777 header = localized_remote_header(remote);
780 crm_trace(
"Read partial remote message (%llu of %u bytes)",
784 crm_trace(
"Read full remote message of %llu bytes",
807 time_t start = time(NULL);
808 int remaining_timeout = 0;
810 if (timeout_ms == 0) {
812 }
else if (timeout_ms < 0) {
816 remaining_timeout = timeout_ms;
817 while (remaining_timeout > 0) {
819 crm_trace(
"Waiting for remote data (%d ms of %d ms timeout remaining)",
820 remaining_timeout, timeout_ms);
824 crm_err(
"Timed out (%d ms) while waiting for remote data",
829 crm_debug(
"Wait for remote data aborted (will retry): %s " 833 rc = read_available_remote_data(remote);
836 }
else if (
rc == EAGAIN) {
837 crm_trace(
"Waiting for more remote data");
845 if ((
rc == ENOTCONN) || (
rc == ESOCKTNOSUPPORT)) {
849 remaining_timeout = timeout_ms - ((time(NULL) - start) * 1000);
854 struct tcp_async_cb_data {
859 void (*callback) (
void *userdata,
int rc,
int sock);
864 check_connect_finished(gpointer userdata)
866 struct tcp_async_cb_data *cb_data = userdata;
870 struct timeval ts = { 0, };
872 if (cb_data->start == 0) {
880 FD_SET(cb_data->sock, &rset);
882 rc = select(cb_data->sock + 1, &rset, &wset, NULL, &ts);
886 if ((
rc == EINPROGRESS) || (
rc == EAGAIN)) {
887 if ((time(NULL) - cb_data->start) < (cb_data->timeout_ms / 1000)) {
893 crm_trace(
"Could not check socket %d for connection success: %s (%d)",
896 }
else if (
rc == 0) {
897 if ((time(NULL) - cb_data->start) < (cb_data->timeout_ms / 1000)) {
900 crm_debug(
"Timed out while waiting for socket %d connection success",
906 }
else if (FD_ISSET(cb_data->sock, &rset)
907 || FD_ISSET(cb_data->sock, &wset)) {
911 socklen_t len =
sizeof(error);
913 if (getsockopt(cb_data->sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
915 crm_trace(
"Couldn't check socket %d for connection errors: %s (%d)",
917 }
else if (error != 0) {
919 crm_trace(
"Socket %d connected with error: %s (%d)",
926 crm_trace(
"select() succeeded, but socket %d not in resulting " 927 "read/write sets", cb_data->sock);
933 crm_trace(
"Socket %d is connected", cb_data->sock);
935 close(cb_data->sock);
939 if (cb_data->callback) {
940 cb_data->callback(cb_data->userdata,
rc, cb_data->sock);
965 connect_socket_retry(
int sock,
const struct sockaddr *addr, socklen_t addrlen,
966 int timeout_ms,
int *timer_id,
void *userdata,
967 void (*callback) (
void *userdata,
int rc,
int sock))
972 struct tcp_async_cb_data *cb_data = NULL;
976 crm_warn(
"Could not set socket non-blocking: %s " CRM_XS " rc=%d",
981 rc = connect(sock, addr, addrlen);
982 if (
rc < 0 && (errno != EINPROGRESS) && (errno != EAGAIN)) {
989 cb_data = calloc(1,
sizeof(
struct tcp_async_cb_data));
990 cb_data->userdata = userdata;
991 cb_data->callback = callback;
992 cb_data->sock = sock;
993 cb_data->timeout_ms = timeout_ms;
1003 cb_data->start = time(NULL);
1015 crm_trace(
"Scheduling check in %dms for whether connect to fd %d finished",
1017 timer = g_timeout_add(interval, check_connect_finished, cb_data);
1038 connect_socket_once(
int sock,
const struct sockaddr *addr, socklen_t addrlen)
1040 int rc = connect(sock, addr, addrlen);
1051 crm_warn(
"Could not set socket non-blocking: %s " CRM_XS " rc=%d",
1077 int *sock_fd,
void *userdata,
1078 void (*callback) (
void *userdata,
int rc,
int sock))
1080 char buffer[INET6_ADDRSTRLEN];
1081 struct addrinfo *res = NULL;
1082 struct addrinfo *rp = NULL;
1083 struct addrinfo hints;
1084 const char *server =
host;
1088 CRM_CHECK((
host != NULL) && (sock_fd != NULL),
return EINVAL);
1091 memset(&hints, 0,
sizeof(
struct addrinfo));
1092 hints.ai_family = AF_UNSPEC;
1093 hints.ai_socktype = SOCK_STREAM;
1094 hints.ai_flags = AI_CANONNAME;
1095 rc = getaddrinfo(server, NULL, &hints, &res);
1097 crm_err(
"Unable to get IP address info for %s: %s",
1098 server, gai_strerror(
rc));
1102 if (!res || !res->ai_addr) {
1103 crm_err(
"Unable to get IP address info for %s: no result", server);
1109 for (rp = res; rp != NULL; rp = rp->ai_next) {
1110 struct sockaddr *addr = rp->ai_addr;
1116 if (rp->ai_canonname) {
1117 server = res->ai_canonname;
1121 sock = socket(rp->ai_family, SOCK_STREAM, IPPROTO_TCP);
1124 crm_warn(
"Could not create socket for remote connection to %s:%d: " 1131 if (addr->sa_family == AF_INET6) {
1132 ((
struct sockaddr_in6 *)(
void*)addr)->sin6_port = htons(port);
1134 ((
struct sockaddr_in *)(
void*)addr)->sin_port = htons(port);
1137 memset(buffer, 0,
DIMOF(buffer));
1139 crm_info(
"Attempting remote connection to %s:%d", buffer, port);
1142 if (connect_socket_retry(sock, rp->ai_addr, rp->ai_addrlen,
timeout,
1143 timer_id, userdata, callback) ==
pcmk_rc_ok) {
1147 }
else if (connect_socket_once(sock, rp->ai_addr,
1181 switch (((
struct sockaddr*)sa)->sa_family) {
1183 inet_ntop(AF_INET, &(((
struct sockaddr_in *)sa)->sin_addr),
1184 s, INET6_ADDRSTRLEN);
1188 inet_ntop(AF_INET6, &(((
struct sockaddr_in6 *)sa)->sin6_addr),
1189 s, INET6_ADDRSTRLEN);
1193 strcpy(s,
"<invalid>");
1210 struct sockaddr_storage addr;
1211 socklen_t laddr =
sizeof(addr);
1212 char addr_str[INET6_ADDRSTRLEN];
1215 memset(&addr, 0,
sizeof(addr));
1216 *csock = accept(ssock, (
struct sockaddr *)&addr, &laddr);
1219 crm_err(
"Could not accept remote client connection: %s " 1224 crm_info(
"Accepted new remote client connection from %s", addr_str);
1228 crm_err(
"Could not set socket non-blocking: %s " CRM_XS " rc=%d",
1235 #ifdef TCP_USER_TIMEOUT 1240 rc = setsockopt(*csock, SOL_TCP, TCP_USER_TIMEOUT,
1241 &optval,
sizeof(optval));
1244 crm_err(
"Could not set TCP timeout to %d ms on remote connection: " 1264 static int port = 0;
1267 const char *env = getenv(
"PCMK_remote_port");
1271 port = strtol(env, NULL, 10);
1272 if (errno || (port < 1) || (port > 65535)) {
1273 crm_warn(
"Environment variable PCMK_remote_port has invalid value '%s', using %d instead",
#define CRM_CHECK(expr, failure_action)
int pcmk__remote_send_xml(pcmk__remote_t *remote, xmlNode *msg)
const char * pcmk_strerror(int rc)
const char * bz2_strerror(int rc)
uint32_t payload_compressed
void pcmk__sockaddr2str(void *sa, char *s)
int crm_parse_int(const char *text, const char *default_text)
Parse an integer value from a string.
uint32_t payload_uncompressed
#define CRM_LOG_ASSERT(expr)
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
int crm_default_remote_port()
Get the default remote connection TCP port on this host.
Wrappers for and extensions to glib mainloop.
xmlNode * string2xml(const char *input)
#define DEFAULT_REMOTE_PORT
#define PCMK_GNUTLS_PRIORITIES
#define crm_warn(fmt, args...)
#define crm_debug(fmt, args...)
#define crm_trace(fmt, args...)
Wrappers for and extensions to libxml2.
int pcmk_legacy2rc(int legacy_rc)
struct tcp_async_cb_data __attribute__
#define REMOTE_MSG_VERSION
#define crm_err(fmt, args...)
char * dump_xml_unformatted(xmlNode *msg)
int pcmk__accept_remote_connection(int ssock, int *csock)
int pcmk__remote_ready(pcmk__remote_t *remote, int timeout_ms)
int pcmk__connect_remote(const char *host, int port, int timeout, int *timer_id, int *sock_fd, void *userdata, void(*callback)(void *userdata, int rc, int sock))
int pcmk__read_remote_message(pcmk__remote_t *remote, int timeout_ms)
xmlNode * pcmk__remote_message_xml(pcmk__remote_t *remote)
struct pcmk__remote_s * remote
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
#define crm_info(fmt, args...)
long pcmk__get_sbd_timeout(void)
int pcmk__set_nonblocking(int fd)