summaryrefslogtreecommitdiffstats
path: root/output/ulogd_output_JSON.c
blob: f80d0e2921aacbd14651cd8a372b0d0764ce0d0d (plain)
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/* ulogd_output_JSON.c
 *
 * ulogd output target for logging to a file in JSON format.
 *
 * (C) 2014 by Eric Leblond <eric@regit.org>
 *
 *  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.
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <ulogd/ulogd.h>
#include <ulogd/conffile.h>
#include <jansson.h>

#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX 108
#endif

#ifndef ULOGD_JSON_DEFAULT
#define ULOGD_JSON_DEFAULT	"/var/log/ulogd.json"
#endif

#ifndef ULOGD_JSON_DEFAULT_DEVICE
#define ULOGD_JSON_DEFAULT_DEVICE "Netfilter"
#endif

#define host_ce(x)	(x->ces[JSON_CONF_HOST])
#define port_ce(x)	(x->ces[JSON_CONF_PORT])
#define mode_ce(x)	(x->ces[JSON_CONF_MODE])
#define file_ce(x)	(x->ces[JSON_CONF_FILENAME])
#define unlikely(x) __builtin_expect((x),0)

struct json_priv {
	FILE *of;
	int sec_idx;
	int usec_idx;
	long cached_gmtoff;
	char cached_tz[6];	/* eg +0200 */
	int mode;
	int sock;
};

enum json_mode {
	JSON_MODE_FILE = 0,
	JSON_MODE_TCP,
	JSON_MODE_UDP,
	JSON_MODE_UNIX
};

enum json_conf {
	JSON_CONF_FILENAME = 0,
	JSON_CONF_SYNC,
	JSON_CONF_TIMESTAMP,
	JSON_CONF_EVENTV1,
	JSON_CONF_DEVICE,
	JSON_CONF_BOOLEAN_LABEL,
	JSON_CONF_MODE,
	JSON_CONF_HOST,
	JSON_CONF_PORT,
	JSON_CONF_MAX
};

static struct config_keyset json_kset = {
	.num_ces = JSON_CONF_MAX,
	.ces = {
		[JSON_CONF_FILENAME] = {
			.key = "file",
			.type = CONFIG_TYPE_STRING,
			.options = CONFIG_OPT_NONE,
			.u = {.string = ULOGD_JSON_DEFAULT },
		},
		[JSON_CONF_SYNC] = {
			.key = "sync",
			.type = CONFIG_TYPE_INT,
			.options = CONFIG_OPT_NONE,
			.u = { .value = 0 },
		},
		[JSON_CONF_TIMESTAMP] = {
			.key = "timestamp",
			.type = CONFIG_TYPE_INT,
			.options = CONFIG_OPT_NONE,
			.u = { .value = 1 },
		},
		[JSON_CONF_EVENTV1] = {
			.key = "eventv1",
			.type = CONFIG_TYPE_INT,
			.options = CONFIG_OPT_NONE,
			.u = { .value = 0 },
		},
		[JSON_CONF_DEVICE] = {
			.key = "device",
			.type = CONFIG_TYPE_STRING,
			.options = CONFIG_OPT_NONE,
			.u = { .string = ULOGD_JSON_DEFAULT_DEVICE },
		},
		[JSON_CONF_BOOLEAN_LABEL] = {
			.key = "boolean_label",
			.type = CONFIG_TYPE_INT,
			.options = CONFIG_OPT_NONE,
			.u = { .value = 0 },
		},
		[JSON_CONF_MODE] = {
			.key = "mode",
			.type = CONFIG_TYPE_STRING,
			.options = CONFIG_OPT_NONE,
			.u = { .string = "file" },
		},
		[JSON_CONF_HOST] = {
			.key = "host",
			.type = CONFIG_TYPE_STRING,
			.options = CONFIG_OPT_NONE,
			.u = { .string = "127.0.0.1" },
		},
		[JSON_CONF_PORT] = {
			.key = "port",
			.type = CONFIG_TYPE_STRING,
			.options = CONFIG_OPT_NONE,
			.u = { .string = "12345" },
		},
	},
};

static void close_socket(struct json_priv *op) {
	if (op->sock != -1) {
		close(op->sock);
		op->sock = -1;
	}
}

static int _connect_socket_unix(struct ulogd_pluginstance *pi)
{
	const char *socket_path = file_ce(pi->config_kset).u.string;
	struct json_priv *op = (struct json_priv *) &pi->private;
	struct sockaddr_un u_addr = { .sun_family = AF_UNIX };
	int sfd;

	close_socket(op);

	ulogd_log(ULOGD_DEBUG, "connecting to unix:%s\n", socket_path);
	strcpy(u_addr.sun_path, socket_path);

	sfd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (sfd == -1)
		return -1;

	if (connect(sfd, (struct sockaddr *) &u_addr, sizeof(u_addr)) == -1) {
		close(sfd);
		return -1;
	}

	op->sock = sfd;

	return 0;
}

static int _connect_socket_net(struct ulogd_pluginstance *pi)
{
	struct json_priv *op = (struct json_priv *) &pi->private;
	struct addrinfo hints;
	struct addrinfo *result, *rp;
	int sfd, s;

	close_socket(op);

	ulogd_log(ULOGD_DEBUG, "connecting to %s:%s\n",
		  host_ce(pi->config_kset).u.string,
		  port_ce(pi->config_kset).u.string);

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = op->mode == JSON_MODE_UDP ? SOCK_DGRAM : SOCK_STREAM;
	hints.ai_protocol = 0;
	hints.ai_flags = 0;

	s = getaddrinfo(host_ce(pi->config_kset).u.string,
			port_ce(pi->config_kset).u.string, &hints, &result);
	if (s != 0) {
		ulogd_log(ULOGD_ERROR, "getaddrinfo: %s\n", gai_strerror(s));
		return -1;
	}

	for (rp = result; rp != NULL; rp = rp->ai_next) {
		int on = 1;

		sfd = socket(rp->ai_family, rp->ai_socktype,
				rp->ai_protocol);
		if (sfd == -1)
			continue;

		setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,
			   (char *) &on, sizeof(on));

		if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
			break;

		close(sfd);
	}

	freeaddrinfo(result);

	if (rp == NULL) {
		return -1;
	}

	op->sock = sfd;

	return 0;
}

static int _connect_socket(struct ulogd_pluginstance *pi)
{
	struct json_priv *op = (struct json_priv *) &pi->private;

	if (op->mode == JSON_MODE_UNIX)
		return _connect_socket_unix(pi);
	else
		return _connect_socket_net(pi);
}

static int json_interp_socket(struct ulogd_pluginstance *upi, char *buf, int buflen)
{
	struct json_priv *opi = (struct json_priv *) &upi->private;
	int ret = 0;

	if (opi->sock != -1)
		ret = send(opi->sock, buf, buflen, MSG_NOSIGNAL);
	free(buf);
	if (ret != buflen) {
		ulogd_log(ULOGD_ERROR, "Failure sending message: %s\n",
			  strerror(errno));
		if (ret == -1 || opi->sock == -1)
			return _connect_socket(upi);
		else
			return ULOGD_IRET_ERR;
	}

	return ULOGD_IRET_OK;
}

static int json_interp_file(struct ulogd_pluginstance *upi, char *buf)
{
	struct json_priv *opi = (struct json_priv *) &upi->private;

	fprintf(opi->of, "%s", buf);
	free(buf);

	if (upi->config_kset->ces[JSON_CONF_SYNC].u.value != 0)
		fflush(opi->of);

	return ULOGD_IRET_OK;
}

#define MAX_LOCAL_TIME_STRING 80

static int json_interp(struct ulogd_pluginstance *upi)
{
	struct json_priv *opi = (struct json_priv *) &upi->private;
	char *dvc, *buf, *tmp;
	unsigned int i;
	size_t buflen;
	json_t *msg;

	msg = json_object();
	if (!msg) {
		ulogd_log(ULOGD_ERROR, "Unable to create JSON object\n");
		return ULOGD_IRET_ERR;
	}

	if (upi->config_kset->ces[JSON_CONF_EVENTV1].u.value != 0)
		json_object_set_new(msg, "@version", json_integer(1));

	if (upi->config_kset->ces[JSON_CONF_TIMESTAMP].u.value != 0) {
		time_t now;
		char timestr[MAX_LOCAL_TIME_STRING];
		struct tm *t;
		struct tm result;
		struct ulogd_key *inp = upi->input.keys;


		if (pp_is_valid(inp, opi->sec_idx))
			now = (time_t) ikey_get_u64(&inp[opi->sec_idx]);
		else
			now = time(NULL);
		t = localtime_r(&now, &result);
		if (unlikely(*opi->cached_tz = '\0' || t->tm_gmtoff != opi->cached_gmtoff)) {
			int gmtoff = t->tm_gmtoff % 86400;
			int gmtoff_hours = gmtoff / 3600;
			int gmtoff_minutes = abs(gmtoff) / 60 % 60;

			snprintf(opi->cached_tz, sizeof(opi->cached_tz),
				 "%+03d%02d", gmtoff_hours, gmtoff_minutes);
		}

		if (pp_is_valid(inp, opi->usec_idx)) {
			snprintf(timestr, MAX_LOCAL_TIME_STRING,
					"%04d-%02d-%02dT%02d:%02d:%02d.%06u%s",
					t->tm_year + 1900, t->tm_mon + 1,
					t->tm_mday, t->tm_hour,
					t->tm_min, t->tm_sec,
					ikey_get_u32(&inp[opi->usec_idx]),
					opi->cached_tz);
		} else {
			snprintf(timestr, MAX_LOCAL_TIME_STRING,
					"%04d-%02d-%02dT%02d:%02d:%02d%s",
					t->tm_year + 1900, t->tm_mon + 1,
					t->tm_mday, t->tm_hour,
					t->tm_min, t->tm_sec,
					opi->cached_tz);
		}

		if (upi->config_kset->ces[JSON_CONF_EVENTV1].u.value != 0)
			json_object_set_new(msg, "@timestamp", json_string(timestr));
		else
			json_object_set_new(msg, "timestamp", json_string(timestr));
	}

	dvc = upi->config_kset->ces[JSON_CONF_DEVICE].u.string;
	json_object_set_new(msg, "dvc", json_string(dvc));

	for (i = 0; i < upi->input.num_keys; i++) {
		struct ulogd_key *key = upi->input.keys[i].u.source;
		char *field_name;

		if (!key)
			continue;

		if (!IS_VALID(*key))
			continue;

		field_name = key->cim_name ? key->cim_name : key->name;

		switch (key->type) {
		case ULOGD_RET_STRING:
			json_object_set_new(msg, field_name, json_string(key->u.value.ptr));
			break;
		case ULOGD_RET_BOOL:
		case ULOGD_RET_INT8:
			json_object_set_new(msg, field_name, json_integer(key->u.value.i8));
			break;
		case ULOGD_RET_INT16:
			json_object_set_new(msg, field_name, json_integer(key->u.value.i16));
			break;
		case ULOGD_RET_INT32:
			json_object_set_new(msg, field_name, json_integer(key->u.value.i32));
			break;
		case ULOGD_RET_INT64:
			json_object_set_new(msg, field_name, json_integer(key->u.value.i64));
			break;
		case ULOGD_RET_UINT8:
			if ((upi->config_kset->ces[JSON_CONF_BOOLEAN_LABEL].u.value != 0)
					&& (!strcmp(key->name, "raw.label"))) {
				if (key->u.value.ui8)
					json_object_set_new(msg, "action", json_string("allowed"));
				else
					json_object_set_new(msg, "action", json_string("blocked"));
				break;
			}
			json_object_set_new(msg, field_name, json_integer(key->u.value.ui8));
			break;
		case ULOGD_RET_UINT16:
			json_object_set_new(msg, field_name, json_integer(key->u.value.ui16));
			break;
		case ULOGD_RET_UINT32:
			json_object_set_new(msg, field_name, json_integer(key->u.value.ui32));
			break;
		case ULOGD_RET_UINT64:
			json_object_set_new(msg, field_name, json_integer(key->u.value.ui64));
			break;
		default:
			/* don't know how to interpret this key. */
			break;
		}
	}

	buf = json_dumps(msg, 0);
	json_decref(msg);
	if (buf == NULL) {
		ulogd_log(ULOGD_ERROR, "Could not create message\n");
		return ULOGD_IRET_ERR;
	}
	buflen = strlen(buf);
	tmp = realloc(buf, buflen + sizeof("\n"));
	if (tmp == NULL) {
		free(buf);
		ulogd_log(ULOGD_ERROR, "Could not create message\n");
		return ULOGD_IRET_ERR;
	}
	buf = tmp;
	buf[buflen++] = '\n';
	buf[buflen]   = '\0';

	if (opi->mode == JSON_MODE_FILE)
		return json_interp_file(upi, buf);
	else
		return json_interp_socket(upi, buf, buflen);
}

static void reopen_file(struct ulogd_pluginstance *upi)
{
	struct json_priv *oi = (struct json_priv *) &upi->private;
	FILE *old = oi->of;

	ulogd_log(ULOGD_NOTICE, "JSON: reopening logfile\n");
	oi->of = fopen(upi->config_kset->ces[0].u.string, "a");
	if (!oi->of) {
		ulogd_log(ULOGD_ERROR, "can't open JSON "
				       "log file: %s\n",
			  strerror(errno));
		oi->of = old;
	} else {
		fclose(old);
	}
}

static int validate_unix_socket(struct ulogd_pluginstance *upi)
{
	const char *socket_path = file_ce(upi->config_kset).u.string;

	if (!socket_path[0]) {
		ulogd_log(ULOGD_ERROR, "missing unix socket path");
		return -1;
	}
	if (strlen(socket_path) >= UNIX_PATH_MAX) {
		ulogd_log(ULOGD_ERROR, "unix socket path `%s' is longer than %u\n",
			  file_ce(upi->config_kset).u.string, UNIX_PATH_MAX);
		return -1;
	}

	return 0;
}

static void reopen_socket(struct ulogd_pluginstance *upi)
{
	struct json_priv *op = (struct json_priv *) &upi->private;

	ulogd_log(ULOGD_NOTICE, "JSON: reopening socket\n");

	if (op->mode == JSON_MODE_UNIX &&
	    validate_unix_socket(upi) < 0)
		return;

	if (_connect_socket(upi) < 0) {
		ulogd_log(ULOGD_ERROR, "can't open JSON "
				       "socket: %s\n",
			  strerror(errno));
	}
}

static void sighup_handler_print(struct ulogd_pluginstance *upi, int signal)
{
	struct json_priv *oi = (struct json_priv *) &upi->private;

	switch (signal) {
	case SIGHUP:
		if (oi->mode == JSON_MODE_FILE)
			reopen_file(upi);
		else
			reopen_socket(upi);
		break;
	default:
		break;
	}
}

static int json_configure(struct ulogd_pluginstance *upi,
			    struct ulogd_pluginstance_stack *stack)
{
	struct json_priv *op = (struct json_priv *) &upi->private;
	char *mode_str = mode_ce(upi->config_kset).u.string;
	int ret;

	ret = ulogd_wildcard_inputkeys(upi);
	if (ret < 0)
		return ret;

	ret = config_parse_file(upi->id, upi->config_kset);
	if (ret < 0)
		return ret;

	if (!strcasecmp(mode_str, "udp")) {
		op->mode = JSON_MODE_UDP;
	} else if (!strcasecmp(mode_str, "tcp")) {
		op->mode = JSON_MODE_TCP;
	} else if (!strcasecmp(mode_str, "unix")) {
		op->mode = JSON_MODE_UNIX;
	} else if (!strcasecmp(mode_str, "file")) {
		op->mode = JSON_MODE_FILE;
	} else {
		ulogd_log(ULOGD_ERROR, "unknown mode '%s'\n", mode_str);
		return -EINVAL;
	}

	return 0;
}

static int json_init_file(struct ulogd_pluginstance *upi)
{
	struct json_priv *op = (struct json_priv *) &upi->private;

	op->of = fopen(upi->config_kset->ces[0].u.string, "a");
	if (!op->of) {
		ulogd_log(ULOGD_FATAL, "can't open JSON log file: %s\n",
			strerror(errno));
		return -1;
	}

	return 0;
}

static int json_init_socket(struct ulogd_pluginstance *upi)
{
	struct json_priv *op = (struct json_priv *) &upi->private;

	if (op->mode == JSON_MODE_UNIX &&
	    validate_unix_socket(upi) < 0)
		return -1;

	op->sock = -1;
	return _connect_socket(upi);
}

static int json_init(struct ulogd_pluginstance *upi)
{
	struct json_priv *op = (struct json_priv *) &upi->private;
	unsigned int i;

	/* search for time */
	op->sec_idx = -1;
	op->usec_idx = -1;
	for (i = 0; i < upi->input.num_keys; i++) {
		struct ulogd_key *key = upi->input.keys[i].u.source;
		if (!strcmp(key->name, "oob.time.sec"))
			op->sec_idx = i;
		else if (!strcmp(key->name, "oob.time.usec"))
			op->usec_idx = i;
	}

	*op->cached_tz = '\0';

	if (op->mode == JSON_MODE_FILE)
		return json_init_file(upi);
	else
		return json_init_socket(upi);
}

static void close_file(FILE *of) {
	if (of != stdout)
		fclose(of);
}

static int json_fini(struct ulogd_pluginstance *pi)
{
	struct json_priv *op = (struct json_priv *) &pi->private;

	if (op->mode == JSON_MODE_FILE)
		close_file(op->of);
	else
		close_socket(op);

	return 0;
}

static struct ulogd_plugin json_plugin = {
	.name = "JSON",
	.input = {
		.type = ULOGD_DTYPE_PACKET | ULOGD_DTYPE_FLOW | ULOGD_DTYPE_SUM,
	},
	.output = {
		.type = ULOGD_DTYPE_SINK,
	},
	.configure = &json_configure,
	.interp	= &json_interp,
	.start 	= &json_init,
	.stop	= &json_fini,
	.signal = &sighup_handler_print,
	.config_kset = &json_kset,
	.version = VERSION,
};

void __attribute__ ((constructor)) init(void);

void init(void)
{
	ulogd_register_plugin(&json_plugin);
}