From dfa034be1865d79d2a11f99978c1ee1755d89703 Mon Sep 17 00:00:00 2001 From: john30 Date: Sat, 11 Apr 2020 16:07:50 +0200 Subject: [PATCH 1/2] add --mqttinsecure option (fixes #334) --- src/ebusd/mqtthandler.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/ebusd/mqtthandler.cpp b/src/ebusd/mqtthandler.cpp index 78df67af..5c38db73 100644 --- a/src/ebusd/mqtthandler.cpp +++ b/src/ebusd/mqtthandler.cpp @@ -45,6 +45,7 @@ using std::dec; #define O_CERT (O_CAFI+1) #define O_KEYF (O_CERT+1) #define O_KEPA (O_KEYF+1) +#define O_INSE (O_KEPA+1) /** the definition of the MQTT arguments. */ static const struct argp_option g_mqtt_argp_options[] = { @@ -74,6 +75,7 @@ static const struct argp_option g_mqtt_argp_options[] = { {"mqttcert", O_CERT, "CERTFILE", 0, "Use CERTFILE for MQTT TLS client certificate (no default)", 0 }, {"mqttkey", O_KEYF, "KEYFILE", 0, "Use KEYFILE for MQTT TLS client certificate (no default)", 0 }, {"mqttkeypass", O_KEPA, "PASSWORD", 0, "Use PASSWORD for the encrypted KEYFILE (no default)", 0 }, + {"mqttinsecure",O_INSE, nullptr, 0, "Allow insecure TLS connection (e.g. using a self signed certificate)", 0 }, #endif {nullptr, 0, nullptr, 0, nullptr, 0 }, @@ -105,6 +107,7 @@ static const char* g_capath = nullptr; //!< CA path for TLS static const char* g_certfile = nullptr; //!< client certificate file for TLS static const char* g_keyfile = nullptr; //!< client key file for TLS static const char* g_keypass = nullptr; //!< client key file password for TLS +static bool g_insecure = false; //!< whether to allow insecure TLS connection #endif bool parseTopic(const string& topic, vector* strs, vector* fields); @@ -248,6 +251,9 @@ static error_t mqtt_parse_opt(int key, char *arg, struct argp_state *state) { } g_keypass = replaceSecret(arg); break; + case O_INSE: //--mqttinsecure + g_insecure = true; + break; #endif default: @@ -508,6 +514,11 @@ MqttHandler::MqttHandler(UserInfo* userInfo, BusHandler* busHandler, MessageMap* ret = mosquitto_tls_set(m_mosquitto, g_cafile, g_capath, g_certfile, g_keyfile, on_keypassword); if (ret != MOSQ_ERR_SUCCESS) { logOtherError("mqtt", "unable to set TLS: %d", ret); + } else if (g_insecure) { + ret = mosquitto_tls_insecure_set(m_mosquitto, true); + if (ret != MOSQ_ERR_SUCCESS) { + logOtherError("mqtt", "unable to set TLS insecure: %d", ret); + } } } #endif From 9e152daaa88311fe10ad7ebba2949ceebe25135d Mon Sep 17 00:00:00 2001 From: john30 Date: Sat, 11 Apr 2020 16:31:47 +0200 Subject: [PATCH 2/2] resolve net device host during connect (fixes #336) --- src/lib/ebus/device.cpp | 30 +++++++++++++++--------------- src/lib/ebus/device.h | 17 ++++++++++++----- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/lib/ebus/device.cpp b/src/lib/ebus/device.cpp index 78ec839d..caa2e6d4 100755 --- a/src/lib/ebus/device.cpp +++ b/src/lib/ebus/device.cpp @@ -74,21 +74,10 @@ Device* Device::create(const char* name, bool checkDevice, bool readOnly, bool i free(in); return nullptr; // invalid port } - struct sockaddr_in address; - memset(reinterpret_cast(&address), 0, sizeof(address)); *portpos = 0; - if (inet_aton(addrpos, &address.sin_addr) == 0) { - struct hostent* h = gethostbyname(addrpos); - if (h == nullptr) { - free(in); - return nullptr; // invalid host - } - memcpy(&address.sin_addr, h->h_addr_list[0], h->h_length); - } + char* hostOrIp = strdup(addrpos); free(in); - address.sin_family = AF_INET; - address.sin_port = (in_port_t)htons((uint16_t)port); - return new NetworkDevice(name, address, readOnly, initialSend, udp); + return new NetworkDevice(name, hostOrIp, port, readOnly, initialSend, udp); } return new SerialDevice(name, checkDevice, readOnly, initialSend); } @@ -296,13 +285,24 @@ result_t NetworkDevice::open() { if (m_fd != -1) { close(); } + struct sockaddr_in address; + memset(reinterpret_cast(&address), 0, sizeof(address)); + if (inet_aton(m_hostOrIp, &address.sin_addr) == 0) { + struct hostent* h = gethostbyname(m_hostOrIp); + if (h == nullptr) { + return RESULT_ERR_GENERIC_IO; // invalid host + } + memcpy(&address.sin_addr, h->h_addr_list[0], h->h_length); + } + address.sin_family = AF_INET; + address.sin_port = (in_port_t)htons(m_port); + m_fd = socket(AF_INET, m_udp ? SOCK_DGRAM : SOCK_STREAM, 0); if (m_fd < 0) { return RESULT_ERR_GENERIC_IO; } int ret; if (m_udp) { - struct sockaddr_in address = m_address; address.sin_addr.s_addr = INADDR_ANY; ret = bind(m_fd, (struct sockaddr*)&address, sizeof(address)); } else { @@ -318,7 +318,7 @@ result_t NetworkDevice::open() { setsockopt(m_fd, IPPROTO_TCP, TCP_KEEPCNT, reinterpret_cast(&value), sizeof(value)); } if (ret >= 0) { - ret = connect(m_fd, (struct sockaddr*)&m_address, sizeof(m_address)); + ret = connect(m_fd, (struct sockaddr*)&address, sizeof(address)); } if (ret < 0) { close(); diff --git a/src/lib/ebus/device.h b/src/lib/ebus/device.h index f306bbc7..fa53fd4e 100755 --- a/src/lib/ebus/device.h +++ b/src/lib/ebus/device.h @@ -237,19 +237,23 @@ class NetworkDevice : public Device { * Construct a new instance. * @param name the device name (e.g. "/dev/ttyUSB0" for serial, "127.0.0.1:1234" for network). * @param address the socket address of the device. + * @param hostOrIp the host name or IP address of the device. + * @param port the TCP or UDP port of the device. * @param readOnly whether to allow read access to the device only. * @param initialSend whether to send an initial @a ESC symbol in @a open(). * @param udp true for UDP, false to TCP. */ - NetworkDevice(const char* name, const struct sockaddr_in& address, bool readOnly, bool initialSend, - bool udp) - : Device(name, true, readOnly, initialSend), m_address(address), m_udp(udp), + NetworkDevice(const char* name, const char* hostOrIp, uint16_t port, bool readOnly, bool initialSend, bool udp) + : Device(name, true, readOnly, initialSend), m_hostOrIp(hostOrIp), m_port(port), m_udp(udp), m_buffer(nullptr), m_bufSize(0), m_bufLen(0), m_bufPos(0) {} /** * Destructor. */ virtual ~NetworkDevice() { + if (m_hostOrIp) { + free((void*)m_hostOrIp); + } if (m_buffer) { free(m_buffer); } @@ -279,8 +283,11 @@ class NetworkDevice : public Device { private: - /** the socket address of the device. */ - const struct sockaddr_in m_address; + /** the host name or IP address of the device. */ + const char* m_hostOrIp; + + /** the TCP or UDP port of the device. */ + const uint16_t m_port; /** true for UDP, false to TCP. */ const bool m_udp;