From dc71f9157d3744d4d24b02c2bab258047b2f904d Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 8 May 2014 23:18:50 +0100 Subject: [PATCH 1/8] Use $(STRIP) for stripping binaries when installing, to allow easier cross compilation. Thanks to Frank Pagliughi. --- ChangeLog.txt | 4 ++++ client/Makefile | 4 ++-- lib/Makefile | 2 +- lib/cpp/Makefile | 2 +- src/Makefile | 4 ++-- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 106ef56b..02782d30 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -19,6 +19,10 @@ Broker: Client library: - Fix topic matching edge case. +General: +- Use $(STRIP) for stripping binaries when installing, to allow easier cross + compilation. + 1.3.1 - 20140324 ================ diff --git a/client/Makefile b/client/Makefile index cfd8c76a..af9bb8bf 100644 --- a/client/Makefile +++ b/client/Makefile @@ -24,8 +24,8 @@ client_shared.o : client_shared.c client_shared.h install : all $(INSTALL) -d ${DESTDIR}$(prefix)/bin - $(INSTALL) -s mosquitto_pub ${DESTDIR}${prefix}/bin/mosquitto_pub - $(INSTALL) -s mosquitto_sub ${DESTDIR}${prefix}/bin/mosquitto_sub + $(INSTALL) -s --strip-program=$(STRIP) mosquitto_pub ${DESTDIR}${prefix}/bin/mosquitto_pub + $(INSTALL) -s --strip-program=$(STRIP) mosquitto_sub ${DESTDIR}${prefix}/bin/mosquitto_sub uninstall : -rm -f ${DESTDIR}${prefix}/bin/mosquitto_pub diff --git a/lib/Makefile b/lib/Makefile index 518d4566..5fb35c23 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -24,7 +24,7 @@ all : libmosquitto.so.${SOVERSION} libmosquitto.a install : all $(INSTALL) -d ${DESTDIR}$(prefix)/lib${LIB_SUFFIX}/ - $(INSTALL) -s libmosquitto.so.${SOVERSION} ${DESTDIR}${prefix}/lib${LIB_SUFFIX}/libmosquitto.so.${SOVERSION} + $(INSTALL) -s --strip-program=$(STRIP) libmosquitto.so.${SOVERSION} ${DESTDIR}${prefix}/lib${LIB_SUFFIX}/libmosquitto.so.${SOVERSION} ln -sf libmosquitto.so.${SOVERSION} ${DESTDIR}${prefix}/lib${LIB_SUFFIX}/libmosquitto.so $(INSTALL) -d ${DESTDIR}${prefix}/include/ $(INSTALL) mosquitto.h ${DESTDIR}${prefix}/include/mosquitto.h diff --git a/lib/cpp/Makefile b/lib/cpp/Makefile index 9dc8d809..dc609bd9 100644 --- a/lib/cpp/Makefile +++ b/lib/cpp/Makefile @@ -10,7 +10,7 @@ all : libmosquittopp.so.${SOVERSION} install : all $(INSTALL) -d ${DESTDIR}$(prefix)/lib${LIB_SUFFIX}/ - $(INSTALL) -s libmosquittopp.so.${SOVERSION} ${DESTDIR}${prefix}/lib${LIB_SUFFIX}/libmosquittopp.so.${SOVERSION} + $(INSTALL) -s --strip-program=$(STRIP) libmosquittopp.so.${SOVERSION} ${DESTDIR}${prefix}/lib${LIB_SUFFIX}/libmosquittopp.so.${SOVERSION} ln -sf libmosquittopp.so.${SOVERSION} ${DESTDIR}${prefix}/lib${LIB_SUFFIX}/libmosquittopp.so $(INSTALL) -d ${DESTDIR}${prefix}/include/ $(INSTALL) mosquittopp.h ${DESTDIR}${prefix}/include/mosquittopp.h diff --git a/src/Makefile b/src/Makefile index 5dbf06fa..e3a562de 100644 --- a/src/Makefile +++ b/src/Makefile @@ -100,10 +100,10 @@ mosquitto_passwd.o : mosquitto_passwd.c install : all $(INSTALL) -d ${DESTDIR}$(prefix)/sbin - $(INSTALL) -s mosquitto ${DESTDIR}${prefix}/sbin/mosquitto + $(INSTALL) -s --strip-program=$(STRIP) mosquitto ${DESTDIR}${prefix}/sbin/mosquitto $(INSTALL) mosquitto_plugin.h ${DESTDIR}${prefix}/include/mosquitto_plugin.h ifeq ($(WITH_TLS),yes) - $(INSTALL) -s mosquitto_passwd ${DESTDIR}${prefix}/bin/mosquitto_passwd + $(INSTALL) -s --strip-program=$(STRIP) mosquitto_passwd ${DESTDIR}${prefix}/bin/mosquitto_passwd endif uninstall : From 7b5b5cf43ef649003862b45d68d83dd45c1a12dc Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 8 May 2014 23:27:11 +0100 Subject: [PATCH 2/8] Add local_username, local_password for bridge connections to authenticate to the local broker. --- mosquitto.conf | 7 ++++++ src/bridge.c | 7 ++++++ src/conf.c | 50 ++++++++++++++++++++++++++++++++++++++++++ src/mosquitto_broker.h | 2 ++ 4 files changed, 66 insertions(+) diff --git a/mosquitto.conf b/mosquitto.conf index 7c22e4f4..e22fd750 100644 --- a/mosquitto.conf +++ b/mosquitto.conf @@ -672,6 +672,13 @@ # username is also set. #password +# Set the username to use on the local broker. +#local_username + +# Set the password to use on the local broker. +# This option is only used if local_username is also set. +#local_password + # ----------------------------------------------------------------- # Certificate based SSL/TLS support # ----------------------------------------------------------------- diff --git a/src/bridge.c b/src/bridge.c index 43e70b09..83048a2f 100644 --- a/src/bridge.c +++ b/src/bridge.c @@ -162,6 +162,13 @@ int mqtt3_bridge_connect(struct mosquitto_db *db, struct mosquitto *context) mqtt3_db_messages_delete(context); } + rc = mosquitto_unpwd_check(db, context->bridge->local_username, context->bridge->local_password); + if(rc == MOSQ_ERR_AUTH){ + _mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Bridge %s failed authentication on local broker.", context->id); + return rc; + } + rc = 0; + /* Delete all local subscriptions even for clean_session==false. We don't * remove any messages and the next loop carries out the resubscription * anyway. This means any unwanted subs will be removed. diff --git a/src/conf.c b/src/conf.c index b603a933..dd3f07d5 100644 --- a/src/conf.c +++ b/src/conf.c @@ -250,6 +250,8 @@ void mqtt3_config_cleanup(struct mqtt3_config *config) if(config->bridges[i].clientid) _mosquitto_free(config->bridges[i].clientid); if(config->bridges[i].username) _mosquitto_free(config->bridges[i].username); if(config->bridges[i].password) _mosquitto_free(config->bridges[i].password); + if(config->bridges[i].local_username) _mosquitto_free(config->bridges[i].local_username); + if(config->bridges[i].local_password) _mosquitto_free(config->bridges[i].local_password); if(config->bridges[i].topics){ for(j=0; jbridges[i].topic_count; j++){ if(config->bridges[i].topics[j].topic) _mosquitto_free(config->bridges[i].topics[j].topic); @@ -1140,6 +1142,54 @@ int _config_read_file(struct mqtt3_config *config, bool reload, const char *file _mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Empty listener value in configuration."); return MOSQ_ERR_INVAL; } + }else if(!strcmp(token, "local_password")){ +#ifdef WITH_BRIDGE + if(reload) continue; // FIXME + if(!cur_bridge){ + _mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration."); + return MOSQ_ERR_INVAL; + } + token = strtok_r(NULL, " ", &saveptr); + if(token){ + if(cur_bridge->local_password){ + _mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Duplicate local_password value in bridge configuration."); + return MOSQ_ERR_INVAL; + } + cur_bridge->local_password = _mosquitto_strdup(token); + if(!cur_bridge->local_password){ + _mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory"); + return MOSQ_ERR_NOMEM; + } + }else{ + cur_bridge->local_password = NULL; + } +#else + _mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge support not available."); +#endif + }else if(!strcmp(token, "local_username")){ +#ifdef WITH_BRIDGE + if(reload) continue; // FIXME + if(!cur_bridge){ + _mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration."); + return MOSQ_ERR_INVAL; + } + token = strtok_r(NULL, " ", &saveptr); + if(token){ + if(cur_bridge->local_username){ + _mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Duplicate local_username value in bridge configuration."); + return MOSQ_ERR_INVAL; + } + cur_bridge->local_username = _mosquitto_strdup(token); + if(!cur_bridge->local_username){ + _mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory"); + return MOSQ_ERR_NOMEM; + } + }else{ + cur_bridge->local_username = NULL; + } +#else + _mosquitto_log_printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge support not available."); +#endif }else if(!strcmp(token, "log_dest")){ token = strtok_r(NULL, " ", &saveptr); if(token){ diff --git a/src/mosquitto_broker.h b/src/mosquitto_broker.h index 497f3d9a..ece47008 100644 --- a/src/mosquitto_broker.h +++ b/src/mosquitto_broker.h @@ -258,6 +258,8 @@ struct _mqtt3_bridge{ time_t restart_t; char *username; char *password; + char *local_username; + char *local_password; bool notifications; char *notification_topic; enum mosquitto_bridge_start_type start_type; From 06625420e24900fde2e17f62e989293dfc4e0829 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 8 May 2014 23:27:35 +0100 Subject: [PATCH 3/8] Fix for local_username/password bridge checks. --- src/security.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/security.c b/src/security.c index 802c188b..a019ada5 100644 --- a/src/security.c +++ b/src/security.c @@ -197,7 +197,15 @@ int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, cons if(!db->auth_plugin.lib){ return mosquitto_acl_check_default(db, context, topic, access); }else{ - return db->auth_plugin.acl_check(db->auth_plugin.user_data, context->id, context->username, topic, access); +#ifdef WITH_BRIDGE + if(context->bridge){ + return db->auth_plugin.acl_check(db->auth_plugin.user_data, context->id, context->bridge->local_username, topic, access); + }else{ +#endif + return db->auth_plugin.acl_check(db->auth_plugin.user_data, context->id, context->username, topic, access); +#ifdef WITH_BRIDGE + } +#endif } } From b6f2d3e08785ab009814596e7e464973bdf62bab Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Fri, 16 May 2014 10:20:45 +0100 Subject: [PATCH 4/8] Fix callback deadlocks after calling mosquitto_disconnect(), when using the threaded interfaces. Closes bug #1313725. Thanks to Michael Frommberger. --- ChangeLog.txt | 2 ++ lib/net_mosq.c | 1 + 2 files changed, 3 insertions(+) diff --git a/ChangeLog.txt b/ChangeLog.txt index 02782d30..d51cdd20 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -18,6 +18,8 @@ Broker: Client library: - Fix topic matching edge case. +- Fix callback deadlocks after calling mosquitto_disconnect(), when using the + threaded interfaces. Closes bug #1313725. General: - Use $(STRIP) for stripping binaries when installing, to allow easier cross diff --git a/lib/net_mosq.c b/lib/net_mosq.c index 42ea4ce9..a3977e25 100644 --- a/lib/net_mosq.c +++ b/lib/net_mosq.c @@ -776,6 +776,7 @@ int _mosquitto_packet_write(struct mosquitto *mosq) mosq->on_disconnect(mosq, mosq->userdata, 0); mosq->in_callback = false; } + pthread_mutex_unlock(&mosq->callback_mutex); pthread_mutex_unlock(&mosq->current_out_packet_mutex); return MOSQ_ERR_SUCCESS; } From 3767ef8684faedfd366754e17a2072e09871875a Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Sun, 18 May 2014 22:06:10 +0100 Subject: [PATCH 5/8] Man page documentation for local_username and local_password. --- man/mosquitto.conf.5.xml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/man/mosquitto.conf.5.xml b/man/mosquitto.conf.5.xml index 03ca0805..27f88a3d 100644 --- a/man/mosquitto.conf.5.xml +++ b/man/mosquitto.conf.5.xml @@ -902,6 +902,22 @@ to 60 seconds. + + password + + Configure the password to be used when connecting + this bridge to the local broker. This may be important + when authentication and ACLs are being used. + + + + username + + Configure the username to be used when connecting + this bridge to the local broker. This may be important + when authentication and ACLs are being used. + + [ true | false ] From cb0e355d9e0245435443aabefd2501b864eec35b Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Mon, 19 May 2014 14:23:38 +0100 Subject: [PATCH 6/8] Only provide srv capabilities ifdef WITH_SRV. --- client/client_shared.c | 2 ++ client/pub_client.c | 6 ++++++ client/sub_client.c | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/client/client_shared.c b/client/client_shared.c index 5b73bfe1..343bb07e 100644 --- a/client/client_shared.c +++ b/client/client_shared.c @@ -394,8 +394,10 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c }else{ cfg->pub_mode = MSGMODE_STDIN_FILE; } +#ifdef WITH_SRV }else if(!strcmp(argv[i], "-S")){ cfg->use_srv = true; +#endif }else if(!strcmp(argv[i], "-t") || !strcmp(argv[i], "--topic")){ if(i==argc-1){ fprintf(stderr, "Error: -t argument given but no topic specified.\n\n"); diff --git a/client/pub_client.c b/client/pub_client.c index 3a0c8972..60842e9f 100644 --- a/client/pub_client.c +++ b/client/pub_client.c @@ -201,7 +201,11 @@ void print_usage(void) printf("mosquitto_pub is a simple mqtt client that will publish a message on a single topic and exit.\n"); printf("mosquitto_pub version %s running on libmosquitto %d.%d.%d.\n\n", VERSION, major, minor, revision); printf("Usage: mosquitto_pub [-h host] [-p port] [-q qos] [-r] {-f file | -l | -n | -m message} -t topic\n"); +#ifdef WITH_SRV printf(" [-A bind_address] [-S]\n"); +#else + printf(" [-A bind_address]\n"); +#endif printf(" [-i id] [-I id_prefix]\n"); printf(" [-d] [--quiet]\n"); printf(" [-M max_inflight]\n"); @@ -231,7 +235,9 @@ void print_usage(void) printf(" -q : quality of service level to use for all messages. Defaults to 0.\n"); printf(" -r : message should be retained.\n"); printf(" -s : read message from stdin, sending the entire input as a message.\n"); +#ifdef WITH_SRV printf(" -S : use SRV lookups to determine which host to connect to.\n"); +#endif printf(" -t : mqtt topic to publish to.\n"); printf(" -u : provide a username (requires MQTT 3.1 broker)\n"); printf(" -P : provide a password (requires MQTT 3.1 broker)\n"); diff --git a/client/sub_client.c b/client/sub_client.c index 58855eb9..dab5cc92 100644 --- a/client/sub_client.c +++ b/client/sub_client.c @@ -127,7 +127,11 @@ void print_usage(void) printf("mosquitto_sub version %s running on libmosquitto %d.%d.%d.\n\n", VERSION, major, minor, revision); printf("Usage: mosquitto_sub [-c] [-h host] [-k keepalive] [-p port] [-q qos] [-R] -t topic ...\n"); printf(" [-1] [-T filter_out]\n"); +#ifdef WITH_SRV printf(" [-A bind_address] [-S]\n"); +#else + printf(" [-A bind_address]\n"); +#endif printf(" [-i id] [-I id_prefix]\n"); printf(" [-d] [-N] [--quiet] [-v]\n"); printf(" [-u username [-P password]]\n"); @@ -154,7 +158,9 @@ void print_usage(void) printf(" -p : network port to connect to. Defaults to 1883.\n"); printf(" -q : quality of service level to use for the subscription. Defaults to 0.\n"); printf(" -R : do not print stale messages (those with retain set).\n"); +#ifdef WITH_SRV printf(" -S : use SRV lookups to determine which host to connect to.\n"); +#endif printf(" -t : mqtt topic to subscribe to. May be repeated multiple times.\n"); printf(" -u : provide a username (requires MQTT 3.1 broker)\n"); printf(" -v : print published messages verbosely.\n"); From aab7a2688cf102db24a2cb9aaad050f35e8733e2 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Mon, 19 May 2014 22:03:26 +0100 Subject: [PATCH 7/8] Fix SRV support when building with CMake. --- CMakeLists.txt | 2 ++ ChangeLog.txt | 1 + client/CMakeLists.txt | 4 ++++ lib/CMakeLists.txt | 2 +- 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 782185c9..30d6f598 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,6 +65,8 @@ else (${WITH_TLS} STREQUAL ON) set (OPENSSL_INCLUDE_DIR "") endif (${WITH_TLS} STREQUAL ON) +option(WITH_SRV "Include SRV lookup support?" ON) + # ======================================== # Include projects # ======================================== diff --git a/ChangeLog.txt b/ChangeLog.txt index d51cdd20..172606a7 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -20,6 +20,7 @@ Client library: - Fix topic matching edge case. - Fix callback deadlocks after calling mosquitto_disconnect(), when using the threaded interfaces. Closes bug #1313725. +- Fix SRV support when building with CMake. General: - Use $(STRIP) for stripping binaries when installing, to allow easier cross diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 14abcbec..97e5dae4 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -4,6 +4,10 @@ link_directories(${mosquitto_BINARY_DIR}/lib) set(shared_src client_shared.c client_shared.h) +if (${WITH_SRV} STREQUAL ON) + add_definitions("-DWITH_SRV") +endif (${WITH_SRV} STREQUAL ON) + add_executable(mosquitto_pub pub_client.c ${shared_src}) add_executable(mosquitto_sub sub_client.c ${shared_src}) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 2ae6f6a6..b7c37290 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -50,8 +50,8 @@ if (WIN32) set (LIBRARIES ${LIBRARIES} ws2_32) endif (WIN32) -option(WITH_SRV "Include SRV lookup support?" ON) if (${WITH_SRV} STREQUAL ON) + add_definitions("-DWITH_SRV") set (LIBRARIES ${LIBRARIES} cares) endif (${WITH_SRV} STREQUAL ON) From f23f48ecce693af5ce23244bef4387b2027fc064 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Tue, 20 May 2014 10:22:06 +0100 Subject: [PATCH 8/8] Don't use exit() on SRV failure. --- lib/srv_mosq.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/srv_mosq.c b/lib/srv_mosq.c index 276080d6..b6f2e46d 100644 --- a/lib/srv_mosq.c +++ b/lib/srv_mosq.c @@ -40,7 +40,14 @@ static void srv_callback(void *arg, int status, int timeouts, unsigned char *abu } }else{ _mosquitto_log_printf(mosq, MOSQ_LOG_ERR, "Error: SRV lookup failed (%d).", status); - exit(1); + /* FIXME - calling on_disconnect here isn't correct. */ + pthread_mutex_lock(&mosq->callback_mutex); + if(mosq->on_disconnect){ + mosq->in_callback = true; + mosq->on_disconnect(mosq, mosq->userdata, 2); + mosq->in_callback = false; + } + pthread_mutex_unlock(&mosq->callback_mutex); } } #endif