From 1d3949bce0415475579360bb0fccee1307e2ca55 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Wed, 5 Dec 2018 16:39:45 +0000 Subject: [PATCH] Improve broker generated client ids for the non-Linux case. Removes libuuid dependency. --- ChangeLog.txt | 4 +++ compiling.txt | 1 - config.mk | 11 ------ readme.md | 1 - snap/snapcraft.yaml | 3 -- src/CMakeLists.txt | 9 ----- src/handle_connect.c | 86 ++++++++++++++++++++++++++++++++++---------- travis-install.sh | 2 +- 8 files changed, 72 insertions(+), 45 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 8ebaa2a9..bb3fc58f 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,6 +1,10 @@ 1.6 - 2018xxxx ============== +Broker features: +- Improved general support for broker generated client ids. Removed libuuid + dependency. + Client library features: - Add mosquitto_subscribe_multiple() for sending subscriptions to multiple topics in one command. diff --git a/compiling.txt b/compiling.txt index 99ddc56e..8d44b449 100644 --- a/compiling.txt +++ b/compiling.txt @@ -3,7 +3,6 @@ are optional. * openssl * c-ares (for DNS-SRV support, disabled by default) -* libuuid (from util-linux, can be disabled) * tcp-wrappers (optional, package name libwrap0-dev) * libwebsockets (optional, disabled by default, version 1.3 and above) * On Windows, a pthreads library is required if threading support is to be diff --git a/config.mk b/config.mk index 6f3f3ac5..f7a030db 100644 --- a/config.mk +++ b/config.mk @@ -64,10 +64,6 @@ WITH_SYSTEMD:=no # Build with SRV lookup support. WITH_SRV:=no -# Build using libuuid for clientid generation (Linux only - please report if -# supported on your platform). -WITH_UUID:=yes - # Build with websockets support on the broker. WITH_WEBSOCKETS:=no @@ -209,13 +205,6 @@ ifeq ($(WITH_SOCKS),yes) CLIENT_CFLAGS:=$(CLIENT_CFLAGS) -DWITH_SOCKS endif -ifeq ($(WITH_UUID),yes) - ifeq ($(UNAME),Linux) - BROKER_CFLAGS:=$(BROKER_CFLAGS) -DWITH_UUID - BROKER_LIBS:=$(BROKER_LIBS) -luuid - endif -endif - ifeq ($(WITH_BRIDGE),yes) BROKER_CFLAGS:=$(BROKER_CFLAGS) -DWITH_BRIDGE endif diff --git a/readme.md b/readme.md index 32726894..ee038fbc 100644 --- a/readme.md +++ b/readme.md @@ -66,7 +66,6 @@ already be built. Use `make binary` to skip building the man pages, or install ### Build Dependencies * c-ares (libc-ares-dev on Debian based systems) - disable with `make WITH_SRV=no` -* libuuid (uuid-dev) - disable with `make WITH_UUID=no` * libwebsockets (libwebsockets-dev) - enable with `make WITH_WEBSOCKETS=yes` * openssl (libssl-dev on Debian based systems) - disable with `make WITH_TLS=no` * xsltproc (xsltproc and docbook-xsl on Debian based systems) - only needed when building from git sources - disable with `make WITH_DOCS=no` diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index c7569010..7ce01902 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -51,14 +51,12 @@ parts: build-packages: - libssl-dev - - uuid-dev - xsltproc - docbook-xsl - gcc - g++ stage-packages: - libssl1.0.0 - - libuuid1 prime: - usr/sbin/mosquitto - usr/bin/mosquitto_pub @@ -66,7 +64,6 @@ parts: - usr/lib/libmosquitto.so* - lib/*-linux-gnu/libcrypto.so* - lib/*-linux-gnu/libssl.so* - - lib/*-linux-gnu/libuuid.so* lws: plugin: cmake diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e1eb6f46..960b263a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -160,15 +160,6 @@ if (${WITH_WEBSOCKETS} STREQUAL ON) endif (${STATIC_WEBSOCKETS} STREQUAL ON) endif (${WITH_WEBSOCKETS} STREQUAL ON) -# Simple detect libuuid -if(NOT APPLE) - FIND_PATH(UUID_HEADER uuid/uuid.h) - if (UUID_HEADER) - add_definitions(-DWITH_UUID) - set (MOSQ_LIBS ${MOSQ_LIBS} uuid) - endif (UUID_HEADER) -endif(NOT APPLE) - add_executable(mosquitto ${MOSQ_SRCS}) target_link_libraries(mosquitto ${MOSQ_LIBS}) diff --git a/src/handle_connect.c b/src/handle_connect.c index ad737b8f..0c92d6d3 100644 --- a/src/handle_connect.c +++ b/src/handle_connect.c @@ -30,28 +30,75 @@ Contributors: #include "tls_mosq.h" #include "util_mosq.h" -#ifdef WITH_UUID -# include +#ifdef WITH_TLS +# include +#endif + +#ifdef __linux__ +# include #endif #ifdef WITH_WEBSOCKETS # include #endif + +static int random_16_bytes(uint8_t *bytes) +{ + int rc = MOSQ_ERR_UNKNOWN; + +#ifdef WITH_TLS + if(RAND_bytes(bytes, 16) == 1){ + rc = MOSQ_ERR_SUCCESS; + } +#else +# ifdef __GLIBC__ + if(getrandom(bytes, 16, 0) == 0){ + rc = MOSQ_ERR_SUCCESS; + } +# elif defined(WIN32) + HRYPTPROV provider; + + if(!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)){ + return MOSQ_ERR_UNKNOWN; + } + + if(CryptGenRandom(provider, 16, bytes)){ + rc = MOSQ_ERR_SUCCESS; + } + + CryptReleaseContext(provider, 0); +# else + int i; + + for(i=0; i<16; i++){ + bytes[i] = (uint8_t )(random()&0xFF); + } + rc = MOSQ_ERR_SUCCESS; +# endif +#endif + return rc; +} + +static char nibble_to_hex(uint8_t value) +{ + if(value < 0x0A){ + return '0'+value; + }else{ + return 'A'+value-0x0A; + } +} + static char *client_id_gen(struct mosquitto_db *db, int *idlen, const char *auto_id_prefix, int auto_id_prefix_len) { char *client_id; -#ifdef WITH_UUID - uuid_t uuid; -#else + uint8_t rnd[16]; int i; -#endif + int pos; + + if(random_16_bytes(rnd)) return NULL; -#ifdef WITH_UUID *idlen = 36 + auto_id_prefix_len; -#else - *idlen = 64 + auto_id_prefix_len; -#endif client_id = (char *)mosquitto__calloc((*idlen) + 1, sizeof(char)); if(!client_id){ @@ -61,16 +108,17 @@ static char *client_id_gen(struct mosquitto_db *db, int *idlen, const char *auto memcpy(client_id, auto_id_prefix, auto_id_prefix_len); } - -#ifdef WITH_UUID - uuid_generate_random(uuid); - uuid_unparse_lower(uuid, &client_id[auto_id_prefix_len]); -#else - for(i=0; i<64; i++){ - client_id[i+auto_id_prefix_len] = (rand()%73)+48; + pos = 0; + for(i=0; i<16; i++){ + client_id[auto_id_prefix_len + pos + 0] = nibble_to_hex(rnd[i] & 0x0F); + client_id[auto_id_prefix_len + pos + 1] = nibble_to_hex((rnd[i] >> 4) & 0x0F); + pos += 2; + if(pos == 8 || pos == 13 || pos == 18 || pos == 23){ + client_id[auto_id_prefix_len + pos] = '-'; + pos++; + } } - client_id[i] = '\0'; -#endif + return client_id; } diff --git a/travis-install.sh b/travis-install.sh index f1f31dd1..4c8fbb89 100755 --- a/travis-install.sh +++ b/travis-install.sh @@ -3,7 +3,7 @@ if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get update -qq - sudo apt-get install -y debhelper libc-ares-dev libssl-dev libwrap0-dev python-all python3-all uthash-dev uuid-dev libuuid1 xsltproc docbook-xsl libcunit1-dev + sudo apt-get install -y debhelper libc-ares-dev libssl-dev libwrap0-dev python-all python3-all uthash-dev xsltproc docbook-xsl libcunit1-dev fi if [ "$TRAVIS_OS_NAME" == "osx" ]; then