-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
misc.c
391 lines (336 loc) · 9.22 KB
/
misc.c
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
* OwnTracks Recorder
* Copyright (C) 2015-2024 Jan-Piet Mens <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, 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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <libconfig.h>
#include "utstring.h"
#include "ctype.h"
#include "udata.h"
#include "misc.h"
#include "util.h"
#include "json.h"
#include "version.h"
char *bindump(char *buf, long buflen)
{
static UT_string *out = NULL;
int i, ch;
utstring_renew(out);
for (i = 0; i < buflen; i++) {
ch = buf[i];
if (isprint(ch)) {
utstring_printf(out, "%c", ch);
} else {
utstring_printf(out, " %02X ", ch & 0xFF);
}
}
return (UB(out));
}
/*
* At each received message, the recorder invokes this function with the
* current epoch time and the topic being handled. Use this to update
* a monitoring hoook. If we have Redis, use that exclusively.
*/
void monitorhook(struct udata *userdata, time_t now, char *topic)
{
// struct udata *ud = (struct udata *)userdata;
/* TODO: add monitor hook to a "monitor" key in LMDB ? */
char mpath[BUFSIZ];
static UT_string *us = NULL;
utstring_renew(us);
utstring_printf(us, "%ld %s\n", (long)now, topic);
snprintf(mpath, sizeof(mpath), "%s/monitor", STORAGEDIR);
safewrite(mpath, UB(us));
}
/*
* Return a pointer to a static area containing the last monitor entry or NULL.
*/
char *monitor_get()
{
char mpath[BUFSIZ];
static char monitorline[BUFSIZ], *ret = NULL;
FILE *fp;
snprintf(mpath, sizeof(mpath), "%s/monitor", STORAGEDIR);
if ((fp = fopen(mpath, "r")) != NULL) {
if (fgets(monitorline, sizeof(monitorline), fp) != NULL) {
char *bp = strchr(monitorline, '\n');
if (bp)
*bp = 0;
ret = monitorline;
}
fclose(fp);
}
return (ret);
}
/*
* Set the value of configuration `property' to the optional default
* at `def', then try to find it in the open config file at `cf',
* and finally the environment. Return a new copy.
* `cf' is NULL if the config file couldn't be read, in which case
* we check environment only.
*/
static char *c_str(config_t *cf, char *property, char *def)
{
const char *value;
char *val = def, *p;
if (cf) {
if (config_lookup_string(cf, property, &value) != CONFIG_FALSE) {
val = (char *)value;
}
}
if ((p = getenv(property)) != NULL) {
val = p;
}
return val ? strdup(val) : val;
}
static int c_int(config_t *cf, char *property, int def)
{
#if LIBCONFIG_VER_MAJOR == 1
# if LIBCONFIG_VER_MINOR >= 4
int ival;
# endif
# else
long ival;
#endif
int val = def;
char *p;
if (cf) {
if (config_lookup_int(cf, property, &ival) != CONFIG_FALSE) {
val = (int)ival;
}
}
if ((p = getenv(property)) != NULL) {
val = atol(p);
}
return val;
}
/*
* Fill in some defaults
*/
void get_defaults(char *filename, struct udata *ud)
{
config_t cfg, *cf = NULL;
char *v;
int iv;
if (access(filename, R_OK) == -1) {
olog(LOG_ERR, "Skipping open defaults file %s: %s", filename, strerror(errno));
cf = NULL;
} else {
config_init(cf = &cfg);
if (!config_read_file(cf, filename)) {
olog(LOG_ERR, "Syntax error in %s:%d - %s",
filename,
config_error_line(cf),
config_error_text(cf));
config_destroy(cf);
exit(2);
}
v = c_str(cf, "OTR_STORAGEDIR", STORAGEDEFAULT);
strcpy(STORAGEDIR, v);
}
if (ud == NULL) {
/* being invoked by ocat; return */
return;
}
#if WITH_MQTT
ud->hostname = c_str(cf, "OTR_HOST", "localhost");
ud->username = c_str(cf, "OTR_USER", NULL);
ud->password = c_str(cf, "OTR_PASS", NULL);
ud->clientid = c_str(cf, "OTR_CLIENTID", ud->clientid);
ud->capath = c_str(cf, "OTR_CAPATH", NULL);
ud->cafile = c_str(cf, "OTR_CAFILE", NULL);
ud->certfile = c_str(cf, "OTR_CERTFILE", NULL);
ud->keyfile = c_str(cf, "OTR_KEYFILE", NULL);
ud->identity = c_str(cf, "OTR_IDENTITY", ud->identity);
ud->psk = c_str(cf, "OTR_PSK", ud->psk);
ud->port = c_int(cf, "OTR_PORT", 1883);
ud->qos = c_int(cf, "OTR_QOS", ud->qos);
/* Topics is a blank-separated string of words; split and add to JSON array */
v = c_str(cf, "OTR_TOPICS", NULL);
if (v && *v) {
// if ((v = c_str(cf, "OTR_TOPICS", NULL)) != NULL) {
char *parts[40];
int np, n;
if (ud->topics) json_delete(ud->topics);
if ((np = splitter((char *)v, " ", parts)) < 1) {
olog(LOG_ERR, "Illegal value in OTR_TOPICS");
exit(2);
}
ud->topics = json_mkarray();
for (n = 0; n < np; n++) {
json_append_element(ud->topics, json_mkstring(parts[n]));
}
splitterfree(parts);
}
#endif /* WITH_MQTT */
ud->geokey = c_str(cf, "OTR_GEOKEY", NULL);
iv = c_int(cf, "OTR_PRECISION", GHASHPREC);
geohash_setprec(iv);
#if WITH_HTTP
ud->http_host = c_str(cf, "OTR_HTTPHOST", NULL);
ud->http_logdir = c_str(cf, "OTR_HTTPLOGDIR", NULL);
ud->browser_apikey = c_str(cf, "OTR_BROWSERAPIKEY", NULL);
ud->viewsdir = c_str(cf, "OTR_VIEWSDIR", ud->viewsdir);
ud->http_port = c_int(cf, "OTR_HTTPPORT", ud->http_port);
#ifdef WITH_TOURS
ud->http_prefix = c_str(cf, "OTR_HTTPPREFIX", NULL);
# endif /* WITH_TOURS */
#endif /* WITH_HTTP */
#if WITH_LUA
ud->luascript = c_str(cf, "OTR_LUASCRIPT", NULL);
#endif
ud->label = c_str(cf, "OTR_SERVERLABEL", ud->label);
ud->clean_age = c_int(cf, "OTR_CLEAN_AGE", ud->clean_age);
if (cf) {
config_destroy(cf);
}
}
static void j_int(JsonNode *j, char *property, int ival)
{
json_append_member(j, property, json_mknumber(ival));
}
static void j_str(JsonNode *j, char *property, char *val)
{
if (val && *val) {
json_append_member(j, property, json_mkstring(val));
} else {
json_append_member(j, property, json_mknull());
}
}
static void j_bool(JsonNode *j, char *property, bool tf)
{
json_append_member(j, property, json_mkbool(tf));
}
void display_json_variables(struct udata *ud, bool plain)
{
JsonNode *json = json_mkobject();
char *js;
j_str(json, "VERSION", VERSION);
j_str(json, "CONFIGFILE", CONFIGFILE);
j_str(json, "OTR_STORAGEDIR", STORAGEDIR);
#if WITH_MQTT
// char *pubprefix; /* If not NULL (default), republish modified payload to <pubprefix>/topic */
j_str(json, "OTR_HOST", ud->hostname);
j_int(json, "OTR_PORT", ud->port);
j_str(json, "OTR_USER", ud->username);
j_str(json, "OTR_PASS", ud->password);
j_int(json, "OTR_QOS", ud->qos);
j_str(json, "OTR_CLIENTID", ud->clientid);
j_str(json, "OTR_CAFILE", ud->cafile);
j_str(json, "OTR_CAPATH", ud->capath);
j_str(json, "OTR_CERTFILE", ud->certfile);
j_str(json, "OTR_KEYFILE", ud->keyfile);
j_str(json, "OTR_IDENTITY", ud->identity);
j_str(json, "OTR_PSK", ud->psk);
#endif
j_bool(json, "skip_demo", ud->skipdemo);
j_bool(json, "perform_reverse_geo", ud->revgeo);
j_str(json, "OTR_GEOKEY", ud->geokey);
j_int(json, "OTR_PRECISION", geohash_prec());
j_bool(json, "do_not_write_.rec", ud->norec);
#ifdef WITH_HTTP
j_str(json, "OTR_HTTPHOST", ud->http_host);
j_int(json, "OTR_HTTPPORT", ud->http_port);
j_str(json, "OTR_HTTPLOGDIR", ud->http_logdir);
j_str(json, "OTR_BROWSERAPIKEY", ud->browser_apikey);
j_str(json, "OTR_VIEWSDIR", ud->viewsdir);
# ifdef WITH_TOURS
j_str(json, "OTR_HTTPPREFIX", ud->http_prefix);
# endif /* WITH_TOURS */
#endif
#ifdef WITH_LUA
j_str(json, "OTR_LUASCRIPT", ud->luascript);
#endif
j_str(json, "OTR_SERVERLABEL", ud->label);
j_int(json, "OTR_CLEAN_AGE", ud->clean_age);
#ifdef WITH_TZ
j_str(json, "TZDATADB", TZDATADB);
#endif
/* WITH_ flags */
#ifdef WITH_ENCRYPT
j_bool(json, "WITH_ENCRYPT", true);
#else
j_bool(json, "WITH_ENCRYPT", false);
#endif
#ifdef WITH_HTTP
j_bool(json, "WITH_HTTP", true);
#else
j_bool(json, "WITH_HTTP", false);
#endif
#ifdef WITH_KILL
j_bool(json, "WITH_KILL", true);
#else
j_bool(json, "WITH_KILL", false);
#endif
#ifdef WITH_LUA
j_bool(json, "WITH_LUA", true);
#else
j_bool(json, "WITH_LUA", false);
#endif
#ifdef WITH_MQTT
j_bool(json, "WITH_MQTT", true);
#else
j_bool(json, "WITH_MQTT", false);
#endif
#ifdef WITH_TOURS
j_bool(json, "WITH_TOURS", true);
#else
j_bool(json, "WITH_TOURS", false);
#endif
#ifdef WITH_PING
j_bool(json, "WITH_PING", true);
#else
j_bool(json, "WITH_PING", false);
#endif
#ifdef WITH_TZ
j_bool(json, "WITH_TZ", true);
#else
j_bool(json, "WITH_TZ", false);
#endif
if (plain) {
JsonNode *j;
json_foreach(j, json) {
printf("%-25s", j->key);
switch (j->tag) {
case JSON_NULL:
printf("<null>\n");
break;
case JSON_BOOL:
printf("%s\n", j->bool_ ? "true" : "false");
break;
case JSON_STRING:
printf("%s\n", j->string_);
break;
case JSON_NUMBER:
printf("%.0lf\n", j->number_);
break;
case JSON_ARRAY:
case JSON_OBJECT:
break;
}
}
} else {
if ((js = json_stringify(json, " ")) != NULL) {
printf("%s\n", js);
free(js);
}
}
json_delete(json);
}