Improve broker generated client ids for the non-Linux case.

Removes libuuid dependency.
This commit is contained in:
Roger A. Light 2018-12-05 16:39:45 +00:00
parent f01042fb97
commit 1d3949bce0
8 changed files with 72 additions and 45 deletions

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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`

View File

@ -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

View File

@ -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})

View File

@ -30,28 +30,75 @@ Contributors:
#include "tls_mosq.h"
#include "util_mosq.h"
#ifdef WITH_UUID
# include <uuid/uuid.h>
#ifdef WITH_TLS
# include <openssl/rand.h>
#endif
#ifdef __linux__
# include <sys/random.h>
#endif
#ifdef WITH_WEBSOCKETS
# include <libwebsockets.h>
#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;
}

View File

@ -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