File: mqtt-options.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (320 lines) | stat: -rw-r--r-- 8,123 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Copyright (c) 2021 One Identity
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#include "mqtt-options.h"
#include "syslog-ng.h"

#include <MQTTClient.h>
#include <string.h>
#include <stddef.h>


#define DEFAULT_ADDRESS "tcp://localhost:1883"
#define DEFAULT_KEEPALIVE 60
#define DEFAULT_QOS 0



static gboolean
_validate_protocol(const gchar *address)
{
  const gchar *valid_type[] = {"tcp", "ssl", "ws", "wss"};
  gint i;

  for (i = 0; i < G_N_ELEMENTS(valid_type); ++i)
    if (strncmp(valid_type[i], address, strlen(valid_type[i])) == 0)
      return TRUE;

  return FALSE;
}

static gboolean
_validate_address(const gchar *address)
{
  if (strstr(address, "://") == NULL)
    return FALSE;

  if (!_validate_protocol(address))
    return FALSE;

  return TRUE;
}

void
mqtt_client_options_defaults(MQTTClientOptions *self)
{
  self->address = g_strdup(DEFAULT_ADDRESS);
  self->keepalive = DEFAULT_KEEPALIVE;
  self->qos = DEFAULT_QOS;

  self->ssl_version = MQTT_SSL_VERSION_DEFAULT;
  self->peer_verify = TRUE;
  self->use_system_cert_store = FALSE;
}

void
mqtt_client_options_destroy(MQTTClientOptions *self)
{
  g_free(self->address);
  g_free(self->client_id);

  g_free(self->username);
  g_free(self->password);
  g_free(self->http_proxy);

  g_free(self->ca_dir);
  g_free(self->ca_file);
  g_free(self->cert_file);
  g_free(self->key_file);
  g_free(self->ciphers);
}

void
mqtt_client_options_set_keepalive(MQTTClientOptions *self, const gint keepalive)
{
  self->keepalive = keepalive;
}

gboolean
mqtt_client_options_set_address(MQTTClientOptions *self, const gchar *address)
{
  if (!_validate_address(address))
    return FALSE;

  g_free(self->address);
  self->address = g_strdup(address);
  return TRUE;
}

void
mqtt_client_options_set_qos (MQTTClientOptions *self, const gint qos)
{
  self->qos = qos;
}

gboolean
mqtt_client_options_set_client_id(MQTTClientOptions *self, const gchar *client_id)
{
  if(strcmp("", client_id) == 0)
    return FALSE;

  g_free(self->client_id);
  self->client_id = g_strdup(client_id);

  return TRUE;
}

void
mqtt_client_options_set_cleansession(MQTTClientOptions *self, gboolean cleansession)
{
  self->cleansession = cleansession;
}

void
mqtt_client_options_set_username(MQTTClientOptions *self, const gchar *username)
{
  g_free(self->username);
  self->username = g_strdup(username);
}

void
mqtt_client_options_set_password(MQTTClientOptions *self, const gchar *password)
{
  g_free(self->password);
  self->password = g_strdup(password);
}

void
mqtt_client_options_set_http_proxy(MQTTClientOptions *self, const gchar *http_proxy)
{
  g_free(self->http_proxy);
  self->http_proxy = g_strdup(http_proxy);
}


void
mqtt_client_options_set_ca_dir(MQTTClientOptions *self, const gchar *ca_dir)
{
  g_free(self->ca_dir);
  self->ca_dir = g_strdup(ca_dir);
}

void
mqtt_client_options_set_ca_file(MQTTClientOptions *self, const gchar *ca_file)
{
  g_free(self->ca_file);
  self->ca_file = g_strdup(ca_file);
}

void
mqtt_client_options_set_cert_file(MQTTClientOptions *self, const gchar *cert_file)
{
  g_free(self->cert_file);
  self->cert_file = g_strdup(cert_file);
}

void
mqtt_client_options_set_key_file(MQTTClientOptions *self, const gchar *key_file)
{
  g_free(self->key_file);
  self->key_file = g_strdup(key_file);
}

void
mqtt_client_options_set_cipher_suite(MQTTClientOptions *self, const gchar *ciphers)
{
  g_free(self->ciphers);
  self->ciphers = g_strdup(ciphers);
}

gboolean
mqtt_client_options_set_ssl_version(MQTTClientOptions *self, const gchar *value)
{
  if (strcasecmp(value, "default") == 0)
    self->ssl_version = MQTT_SSL_VERSION_DEFAULT;
  else if (strcasecmp(value, "tlsv1_0") == 0)
    self->ssl_version = MQTT_SSL_VERSION_TLS_1_0;
  else if (strcasecmp(value, "tlsv1_1") == 0)
    self->ssl_version = MQTT_SSL_VERSION_TLS_1_1;
  else if (strcasecmp(value, "tlsv1_2") == 0)
    self->ssl_version = MQTT_SSL_VERSION_TLS_1_2;
  else
    return FALSE;

  return TRUE;
}

void
mqtt_client_options_set_peer_verify(MQTTClientOptions *self, gboolean verify)
{
  self->peer_verify = verify;
}

void
mqtt_client_options_use_system_cert_store(MQTTClientOptions *self, gboolean use_system_cert_store)
{
  /* TODO: auto_detect_ca_file() from the HTTP module */
  self->use_system_cert_store = use_system_cert_store;
}

void
mqtt_client_options_set_log_ssl_error_fn(MQTTClientOptions *self, gpointer context, gint(*log_error)(const gchar *str,
                                         gsize len, gpointer u))
{
  self->context = context;
  self->log_error = log_error;
}

gchar *
mqtt_client_options_get_address(MQTTClientOptions *self)
{
  return self->address;
}

gint
mqtt_client_options_get_qos(MQTTClientOptions *self)
{
  return self->qos;
}

gchar *
mqtt_client_options_get_client_id(MQTTClientOptions *self)
{
  return self->client_id;
}

static void
_set_ssl_options(MQTTClientOptions *self, MQTTClient_SSLOptions *ssl_opts)
{
  *ssl_opts = (MQTTClient_SSLOptions) MQTTClient_SSLOptions_initializer;
  ssl_opts->trustStore = self->ca_file;
  ssl_opts->CApath = self->ca_dir;
  ssl_opts->keyStore = self->cert_file;
  ssl_opts->privateKey = self->key_file;
  ssl_opts->enabledCipherSuites = self->ciphers;
  ssl_opts->sslVersion = self->ssl_version;
  ssl_opts->enableServerCertAuth = self->peer_verify;
  ssl_opts->verify = self->peer_verify;
  ssl_opts->disableDefaultTrustStore = !self->use_system_cert_store;
  ssl_opts->ssl_error_cb = self->log_error;
  ssl_opts->ssl_error_context = self->context;
}

void
mqtt_client_options_to_mqtt_client_connection_option(MQTTClientOptions *self, MQTTClient_connectOptions *conn_opts,
                                                     MQTTClient_SSLOptions *ssl_opts)
{
  *conn_opts = (MQTTClient_connectOptions) MQTTClient_connectOptions_initializer;
  conn_opts->keepAliveInterval = self->keepalive;
  conn_opts->cleansession = self->cleansession;
  conn_opts->username = self->username;
  conn_opts->password = self->password;

#if SYSLOG_NG_HAVE_PAHO_HTTP_PROXY
  if (self->http_proxy)
    {
      conn_opts->httpProxy = self->http_proxy;
      conn_opts->httpsProxy = self->http_proxy;
    }
#endif

  _set_ssl_options(self, ssl_opts);
  conn_opts->ssl = ssl_opts;
}

static gboolean
_key_or_cert_file_is_not_specified(MQTTClientOptions *self)
{
  return (!self->key_file || !self->cert_file);
}

static gboolean
_is_using_tls(MQTTClientOptions *self)
{
  return (strncmp("ssl", self->address, 3) == 0) ||
         (strncmp("wss", self->address, 3) == 0);
}

gboolean
mqtt_client_options_checker(MQTTClientOptions *self)
{

#if !SYSLOG_NG_HAVE_PAHO_HTTP_PROXY
  if (self->http_proxy)
    {
      msg_warning_once("WARNING: the http-proxy() option of the mqtt() destination "
                       "is not supported on the current libpaho-mqtt version. "
                       "If you would like to use this feature, update to at least libpaho-mqtt 1.3.7");
      g_free(self->http_proxy);
      self->http_proxy = NULL;
    }
#endif

  if (_is_using_tls(self) && _key_or_cert_file_is_not_specified(self))
    {
      msg_warning("MQTT: You have a TLS enabled without a X.509 keypair."
                  " Make sure you have tls(key-file() and cert-file()) options, "
                  "TLS handshake to this source will fail");
    }

  return TRUE;
}