summaryrefslogtreecommitdiffstats
path: root/contrib/ipfilter/lib/save_syslog.c
blob: c1efdf41d9844651fa499b2b702c405f5b9b7a5c (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
#include "ipf.h"
#include "ipmon.h"
#include <syslog.h>

static void *syslog_parse __P((char **));
static void syslog_destroy __P((void *));
static int syslog_send __P((void *, ipmon_msg_t *));
static void syslog_print __P((void *));

typedef struct syslog_opts_s {
	int	facpri;
	int	fac;
	int	pri;
} syslog_opts_t;

ipmon_saver_t syslogsaver = {
	"syslog",
	syslog_destroy,
	NULL,			/* dup */
	NULL,			/* match */
	syslog_parse,
	syslog_print,
	syslog_send
};


static void *
syslog_parse(char **strings)
{
	syslog_opts_t *ctx;
	char *str;
	char *s;

	ctx = calloc(1, sizeof(*ctx));
	if (ctx == NULL)
		return NULL;

	ctx->facpri = -1;

	if (strings[0] != NULL && strings[0][0] != '\0') {
		str = strdup(*strings);
		if (str != NULL && *str != '\0') {
			int fac = -1, pri = -1;

			s = strchr(str, '.');
			if (s != NULL)
				*s++ = '\0';

			if (*str != '\0') {
				fac = fac_findname(str);
				if (fac == -1) {
					free(str);
					free(ctx);
					return NULL;
				}
			}

			if (s != NULL && *s != '\0') {
				pri = pri_findname(s);
				if (pri == -1) {
					free(str);
					free(ctx);
					return NULL;
				}
			}
			free(str);

			ctx->fac = fac;
			ctx->pri = pri;
			if (pri == -1)
				ctx->facpri = fac;
			else if (fac == -1)
				ctx->facpri = pri;
			else
				ctx->facpri = fac | pri;
		} else {
			if (str != NULL)
				free(str);
			free(ctx);
			ctx = NULL;
		}
	}

	return ctx;
}


static void
syslog_print(ctx)
	void *ctx;
{
	syslog_opts_t *sys = ctx;

	if (sys->facpri == -1)
		return;

	if (sys->fac == -1) {
		printf(".%s", pri_toname(sys->pri));
	} else if (sys->pri == -1) {
		printf("%s.", fac_toname(sys->fac));
	} else {
		printf("%s.%s", fac_toname(sys->facpri & LOG_FACMASK),
		       pri_toname(sys->facpri & LOG_PRIMASK));
	}
}


static void
syslog_destroy(ctx)
	void *ctx;
{
	free(ctx);
}


static int
syslog_send(ctx, msg)
	void *ctx;
	ipmon_msg_t *msg;
{
	syslog_opts_t *sys = ctx;
	int facpri;

	if (sys->facpri == -1) {
		facpri = msg->imm_loglevel;
	} else {
		if (sys->pri == -1) {
			facpri = sys->fac | (msg->imm_loglevel & LOG_PRIMASK);
		} else if (sys->fac == -1) {
			facpri = sys->pri | (msg->imm_loglevel & LOG_FACMASK);
		} else {
			facpri = sys->facpri;
		}
	}
	syslog(facpri, "%s", msg->imm_msg);
	return 0;
}
OpenPOWER on IntegriCloud