summaryrefslogtreecommitdiffstats
path: root/ulogd/ulogd.c
blob: a60cf5fdef123e4b15e3212eb7bab3d0b086506a (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
/* ulogd, Version $Revision: 1.11 $
 *
 * first try of a logging daemon for my netfilter ULOG target
 * for the linux 2.4 netfilter subsystem.
 *
 * (C) 2000 by Harald Welte <laforge@sunbeam.franken.de>
 *
 * this code is released under the terms of GNU GPL
 *
 * $Id: ulogd.c,v 1.11 2000/11/16 17:20:52 laforge Exp $
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <dlfcn.h>
#include <sys/types.h>
#include <dirent.h>
#include <libipulog/libipulog.h>
#include "conffile.h"
#include "ulogd.h"

#define MYBUFSIZ 2048

#ifdef DEBUG
#define DEBUGP(format, args...) fprintf(stderr, format, ## args)
#else
#define DEBUGP(format, args...) 
#endif

/* default config parameters, if not changed in configfile */
#ifndef ULOGD_PLUGINDIR_DEFAULT
#define ULOGD_PLUGINDIR_DEFAULT	"/usr/local/lib/ulogd"
#endif
#ifndef ULOGD_LOGFILE_DEFAULT
#define ULOGD_LOGFILE_DEFAULT	"/var/log/ulogd.log"
#endif
#ifndef ULOGD_NLGROUP_DEFAULT
#define ULOGD_NLGROUP_DEFAULT	32
#endif

/* where to look for the config file */
#ifndef ULOGD_CONFIGFILE
#define ULOGD_CONFIGFILE	"/etc/ulogd.conf"
#endif


FILE *logfile = NULL;
/* linked list for all registered interpreters */
static ulog_interpreter_t *ulogd_interpreters;

/* linked list for all registered output targets */
static ulog_output_t *ulogd_outputs;

/***********************************************************************
 * INTERPRETER AND KEY HASH FUNCTIONS
 ***********************************************************************/

/* hashtable for all registered interpreters */
static ulog_interpreter_t *ulogd_interh[100];
static unsigned int ulogd_interh_ids;

/* allocate a new interpreter id and write it into the interpreter struct */
static unsigned int interh_allocid(ulog_interpreter_t *ip)
{
	unsigned int id;

	id = ++ulogd_interh_ids;
	ip->id = id;
	ulogd_interh[id] = ip;
	return id;
}

/* get interpreter d by name */
unsigned int interh_getid(const char *name)
{
	int i;
	for (i = 1; i <= ulogd_interh_ids; i++)
		if (!strcmp(name, (ulogd_interh[i])->name))
			return i;

	return 0;
}

/* dump out the contents of the interpreter hash */
static void interh_dump(void)
{
	int i;

	for (i = 1; i <= ulogd_interh_ids; i++)
		printf("ulogd_interh[%d] = %s\n", i, (ulogd_interh[i])->name);

}

struct ulogd_keyh_entry ulogd_keyh[100];
static unsigned int ulogd_keyh_ids;

/* allocate a new key_id */
static unsigned int keyh_allocid(ulog_interpreter_t *ip, unsigned int offset,
				const char *name)
{
	unsigned int id;

	id = ++ulogd_keyh_ids;

	ulogd_keyh[id].interp = ip;
	ulogd_keyh[id].offset = offset;
	ulogd_keyh[id].name = name;

	return id;
}

static void keyh_dump(void)
{
	int i;

	printf("dumping keyh\n");
	for (i = 1; i <= ulogd_keyh_ids; i++)
		printf("ulogd_keyh[%d] = %s:%d\n", i, ulogd_keyh[i].interp->name, 
				ulogd_keyh[i].offset);

}

/* get keyid by name */
unsigned int keyh_getid(const char *name)
{
	int i;
	for (i = 1; i <= ulogd_keyh_ids; i++)
		if (!strcmp(name, ulogd_keyh[i].name))
			return i;

	return 0;
}

/* get key name by keyid */
inline char *keyh_getname(unsigned int id)
{
	return ulogd_keyh[id].interp->name;
}

ulog_iret_t *keyh_getres(unsigned int id)
{
	ulog_iret_t *ret;

	ret = &ulogd_keyh[id].interp->result[ulogd_keyh[id].offset];

	if (ret->flags & ULOGD_RETF_VALID)
		return ret;

	return NULL;
}

/* try to lookup a registered interpreter for a given name */
static ulog_interpreter_t *find_interpreter(const char *name)
{
	int id;
	
	id = interh_getid(name);
	if (!id)
		return NULL;

	return ulogd_interh[id];
}

/* the function called by all interpreter plugins for registering their
 * target. */ 
void register_interpreter(ulog_interpreter_t *me)
{
	int i;

	/* check if we already have an interpreter with this name */
	if (find_interpreter(me->name)) {
		ulogd_error("interpreter `%s' already registered\n",
				me->name);
		exit(1);
	}

	ulogd_log(ULOGD_NOTICE, "registering interpreter `%s'\n", me->name);

	/* allocate a new interpreter id for it */
	interh_allocid(me);

	/* - allocate one keyh_id for each result of this interpreter 
	 * - link the elements to each other */
	for (i = 0; i < me->key_num; i++) {
		keyh_allocid(me, i, me->result[i].key);
		if (i != me->key_num - 1)
			me->result[i].next = &me->result[i+1];
	}

	if (ulogd_interpreters)
		me->result[me->key_num - 1].next = &ulogd_interpreters->result[0];

	/* all work done, we can prepend the new interpreter to the list */
	me->next = ulogd_interpreters;
	ulogd_interpreters = me;
}

/* try to lookup a registered output plugin for a given name */
static ulog_output_t *find_output(const char *name)
{
	ulog_output_t *ptr;

	for (ptr = ulogd_outputs; ptr; ptr = ptr->next) {
		if (strcmp(name, ptr->name) == 0)
				return ptr;
	}

	return NULL;
}

/* the function called by all output plugins for registering themselves */
void register_output(ulog_output_t *me)
{
	if (find_output(me->name)) {
		ulogd_error("output `%s' already registered\n",
				me->name);
		exit(1);
	}
	ulogd_log(ULOGD_NOTICE, "registering output `%s'\n", me->name);
	me->next = ulogd_outputs;
	ulogd_outputs = me;
}

/* allocate a new ulog_iret_t. Called by interpreter plugins */
ulog_iret_t *alloc_ret(const u_int16_t type, const char* key)
{
	ulog_iret_t *ptr = NULL;
	
	ptr = (ulog_iret_t *) malloc(sizeof(ulog_iret_t));
	memset(ptr, 0, sizeof(ulog_iret_t));
	strcpy(ptr->key, key);
	ptr->type = type;

	return ptr;
}

/* log message to the logfile */
void ulogd_log(int level, const char *format, ...)
{
	char *timestr;
	va_list ap;
	time_t tm;
	FILE *outfd;

	if (logfile)
		outfd = logfile;
	else
		outfd = stderr;

	va_start(ap, format);

	tm = time(NULL);
	timestr = ctime(&tm);
	timestr[strlen(timestr)-1] = '\0';
	fprintf(outfd, "%s <%1.1d>", timestr, level);
	
	vfprintf(outfd, format, ap);
	va_end(ap);
}
/* this should pass the result(s) to one or more registered output plugins,
 * but is currently only printing them out */
static void propagate_results(ulog_iret_t *ret)
{
	ulog_output_t *p;

	for (p = ulogd_outputs; p; p = p->next) {
		(*p->output)(ret);
	}
}

/* clean results (set all values to 0 and free pointers) */
static void clean_results(ulog_iret_t *ret)
{
	ulog_iret_t *r;

	for (r = ret; r; r = r->next) {
		if (r->flags & ULOGD_RETF_FREE)
			free(r->value.ptr);
		memset(&r->value, 0, sizeof(r->value));
		r->flags &= ~ULOGD_RETF_VALID;
	}
}

#define IS_VALID(x)	(x.flags & ULOGD_RETF_VALID)

/* call all registered interpreters and hand the results over to 
 * propagate_results */
static void handle_packet(ulog_packet_msg_t *pkt)
{
	ulog_iret_t *ret;
        ulog_iret_t *allret = NULL;
	ulog_interpreter_t *ip;

	unsigned int i,j;

	for (i = 1; i <= ulogd_interh_ids; i++) {
		ip = ulogd_interh[i];
		/* call interpreter */
		if (ret = ((ip)->interp)(ip, pkt)) {
			/* create references for result linked-list */
			for (j = 0; j < ip->key_num; j++) {
				if (IS_VALID(ip->result[j])) {
					ip->result[j].cur_next = allret;
					allret = &ip->result[j];
				}
			}
		}
	}
	propagate_results(allret);
	clean_results(ulogd_interpreters->result);
}

/* silly plugin loader to dlopen() all available plugins */
static void load_plugins(char *dir)
{
	DIR *ldir;
	struct dirent *dent;
	char *fname;

	ldir = opendir(dir);
	if (ldir) {
		fname = (char *) malloc(NAME_MAX + strlen(dir) 
				+ 3);
		for (dent = readdir(ldir); dent; dent = readdir(ldir)) {
			if (strncmp(dent->d_name,"ulogd", 5) == 0) {
			DEBUGP("load_plugins: %s\n", dent->d_name);
			sprintf(fname, "%s/%s", dir, dent->d_name);
			if (!dlopen(fname, RTLD_NOW))
				ulogd_error("load_plugins: %s\n", dlerror());
			}
		}
		free(fname);
	} else
		ulogd_error("No plugin directory: %s\n", dir);

	interh_dump();
	keyh_dump();

}

static int logfile_open(const char *name)
{
	logfile = fopen(name, "a");
	if (!logfile) {
		fprintf(stderr, "ERROR: unable to open logfile %s: %s\n", 
			name, strerror(errno));
		exit(2);
	}
	return 0;
}

static int parse_conffile(int final)
{
	int err;

	err = config_parse_file(final);

	switch(err) {
		case 0:
			return 0;
			break;
		case -ERROPEN:
			ulogd_error("ERROR: unable to open configfile: %s\n",
					ULOGD_CONFIGFILE);
			break;
		case -ERRMAND:
			ulogd_error("ERROR: mandatory option not found\n");
			break;
		case -ERRMULT:
			ulogd_error("ERROR: option occurred more than once\n");
			break;
		case -ERRUNKN:
			ulogd_error("ERROR: unknown config key\n");
/*				config_errce->key); */
			break;
	}
	return 1;

}

static config_entry_t logf_ce = { NULL, "logfile", CONFIG_TYPE_STRING, 
				  CONFIG_OPT_NONE, 0, 
				  { string: ULOGD_LOGFILE_DEFAULT } };
				  
static config_entry_t pldir_ce = { NULL, "plugindir", CONFIG_TYPE_STRING,
				   CONFIG_OPT_NONE, 0, 
				   { string: ULOGD_PLUGINDIR_DEFAULT } };

static config_entry_t nlgroup_ce = { NULL, "nlgroup", CONFIG_TYPE_INT,
				     CONFIG_OPT_NONE, 0,
				     { value: ULOGD_NLGROUP_DEFAULT } };
static int init_conffile(char *file)
{
	if (config_register_file(file))
		return 1;

	/* link them together */
	logf_ce.next = &pldir_ce;
	pldir_ce.next = &nlgroup_ce;

	config_register_key(&logf_ce);
	
	/* parse config file the first time (for logfile name, ...) */
	return parse_conffile(0);
}

int main(int argc, char* argv[])
{
	struct ipulog_handle *h;
	unsigned char* buf;
	size_t len;
	ulog_packet_msg_t *upkt;

	if (init_conffile(ULOGD_CONFIGFILE)) {
		exit(1);
	}
	
	logfile_open(logf_ce.u.string);
	load_plugins(pldir_ce.u.string);	

	/* parse config file the second time (for plugin options) */
	if (parse_conffile(1)) {
		ulogd_error("ERROR during second parse_conffile\n");
		exit(1);
	}

	/* allocate a receive buffer */
	buf = (unsigned char *) malloc(MYBUFSIZ);
	
	/* create ipulog handle */
	h = ipulog_create_handle(ipulog_group2gmask(nlgroup_ce.u.value));
	if (!h) {
		/* if some error occurrs, print it to stderr */
		ipulog_perror(NULL);
		exit(1);
	}

#ifndef DEBUG
	if (!fork()) { 

		fclose(stdout);
		fclose(stderr);
#endif

		/* endless loop receiving packets and handling them over to
		 * handle_packet */
		while(1) {
			len = ipulog_read(h, buf, MYBUFSIZ, 1);
			upkt = ipulog_get_packet(buf);	
			DEBUGP("==> packet received\n");
			handle_packet(upkt);
		}
	
		/* just to give it a cleaner look */
		ipulog_destroy_handle(h);
		free(buf);
		fclose(logfile);
#ifndef DEBUG
	} else {
		exit(0);
	}
#endif
}