xref: /netbsd/external/bsd/ipf/dist/lib/save_syslog.c (revision 26945a25)
1*26945a25Schristos /*	$NetBSD: save_syslog.c,v 1.1.1.1 2012/03/23 21:20:10 christos Exp $	*/
2*26945a25Schristos 
3*26945a25Schristos #include "ipf.h"
4*26945a25Schristos #include "ipmon.h"
5*26945a25Schristos #include <syslog.h>
6*26945a25Schristos 
7*26945a25Schristos static void *syslog_parse __P((char **));
8*26945a25Schristos static void syslog_destroy __P((void *));
9*26945a25Schristos static int syslog_send __P((void *, ipmon_msg_t *));
10*26945a25Schristos static void syslog_print __P((void *));
11*26945a25Schristos 
12*26945a25Schristos typedef struct syslog_opts_s {
13*26945a25Schristos 	int	facpri;
14*26945a25Schristos 	int	fac;
15*26945a25Schristos 	int	pri;
16*26945a25Schristos } syslog_opts_t;
17*26945a25Schristos 
18*26945a25Schristos ipmon_saver_t syslogsaver = {
19*26945a25Schristos 	"syslog",
20*26945a25Schristos 	syslog_destroy,
21*26945a25Schristos 	NULL,			/* dup */
22*26945a25Schristos 	NULL,			/* match */
23*26945a25Schristos 	syslog_parse,
24*26945a25Schristos 	syslog_print,
25*26945a25Schristos 	syslog_send
26*26945a25Schristos };
27*26945a25Schristos 
28*26945a25Schristos 
29*26945a25Schristos static void *
syslog_parse(char ** strings)30*26945a25Schristos syslog_parse(char **strings)
31*26945a25Schristos {
32*26945a25Schristos 	syslog_opts_t *ctx;
33*26945a25Schristos 	char *str;
34*26945a25Schristos 	char *s;
35*26945a25Schristos 
36*26945a25Schristos 	ctx = calloc(1, sizeof(*ctx));
37*26945a25Schristos 	if (ctx == NULL)
38*26945a25Schristos 		return NULL;
39*26945a25Schristos 
40*26945a25Schristos 	ctx->facpri = -1;
41*26945a25Schristos 
42*26945a25Schristos 	if (strings[0] != NULL && strings[0][0] != '\0') {
43*26945a25Schristos 		str = strdup(*strings);
44*26945a25Schristos 		if (str != NULL && *str != '\0') {
45*26945a25Schristos 			int fac = -1, pri = -1;
46*26945a25Schristos 
47*26945a25Schristos 			s = strchr(str, '.');
48*26945a25Schristos 			if (s != NULL)
49*26945a25Schristos 				*s++ = '\0';
50*26945a25Schristos 
51*26945a25Schristos 			if (*str != '\0') {
52*26945a25Schristos 				fac = fac_findname(str);
53*26945a25Schristos 				if (fac == -1) {
54*26945a25Schristos 					free(str);
55*26945a25Schristos 					free(ctx);
56*26945a25Schristos 					return NULL;
57*26945a25Schristos 				}
58*26945a25Schristos 			}
59*26945a25Schristos 
60*26945a25Schristos 			if (s != NULL && *s != '\0') {
61*26945a25Schristos 				pri = pri_findname(s);
62*26945a25Schristos 				if (pri == -1) {
63*26945a25Schristos 					free(str);
64*26945a25Schristos 					free(ctx);
65*26945a25Schristos 					return NULL;
66*26945a25Schristos 				}
67*26945a25Schristos 			}
68*26945a25Schristos 			free(str);
69*26945a25Schristos 
70*26945a25Schristos 			ctx->fac = fac;
71*26945a25Schristos 			ctx->pri = pri;
72*26945a25Schristos 			if (pri == -1)
73*26945a25Schristos 				ctx->facpri = fac;
74*26945a25Schristos 			else if (fac == -1)
75*26945a25Schristos 				ctx->facpri = pri;
76*26945a25Schristos 			else
77*26945a25Schristos 				ctx->facpri = fac | pri;
78*26945a25Schristos 		} else {
79*26945a25Schristos 			if (str != NULL)
80*26945a25Schristos 				free(str);
81*26945a25Schristos 			free(ctx);
82*26945a25Schristos 			ctx = NULL;
83*26945a25Schristos 		}
84*26945a25Schristos 	}
85*26945a25Schristos 
86*26945a25Schristos 	return ctx;
87*26945a25Schristos }
88*26945a25Schristos 
89*26945a25Schristos 
90*26945a25Schristos static void
syslog_print(ctx)91*26945a25Schristos syslog_print(ctx)
92*26945a25Schristos 	void *ctx;
93*26945a25Schristos {
94*26945a25Schristos 	syslog_opts_t *sys = ctx;
95*26945a25Schristos 
96*26945a25Schristos 	if (sys->facpri == -1)
97*26945a25Schristos 		return;
98*26945a25Schristos 
99*26945a25Schristos 	if (sys->fac == -1) {
100*26945a25Schristos 		printf(".%s", pri_toname(sys->pri));
101*26945a25Schristos 	} else if (sys->pri == -1) {
102*26945a25Schristos 		printf("%s.", fac_toname(sys->fac));
103*26945a25Schristos 	} else {
104*26945a25Schristos 		printf("%s.%s", fac_toname(sys->facpri & LOG_FACMASK),
105*26945a25Schristos 		       pri_toname(sys->facpri & LOG_PRIMASK));
106*26945a25Schristos 	}
107*26945a25Schristos }
108*26945a25Schristos 
109*26945a25Schristos 
110*26945a25Schristos static void
syslog_destroy(ctx)111*26945a25Schristos syslog_destroy(ctx)
112*26945a25Schristos 	void *ctx;
113*26945a25Schristos {
114*26945a25Schristos 	free(ctx);
115*26945a25Schristos }
116*26945a25Schristos 
117*26945a25Schristos 
118*26945a25Schristos static int
syslog_send(ctx,msg)119*26945a25Schristos syslog_send(ctx, msg)
120*26945a25Schristos 	void *ctx;
121*26945a25Schristos 	ipmon_msg_t *msg;
122*26945a25Schristos {
123*26945a25Schristos 	syslog_opts_t *sys = ctx;
124*26945a25Schristos 	int facpri;
125*26945a25Schristos 
126*26945a25Schristos 	if (sys->facpri == -1) {
127*26945a25Schristos 		facpri = msg->imm_loglevel;
128*26945a25Schristos 	} else {
129*26945a25Schristos 		if (sys->pri == -1) {
130*26945a25Schristos 			facpri = sys->fac | (msg->imm_loglevel & LOG_PRIMASK);
131*26945a25Schristos 		} else if (sys->fac == -1) {
132*26945a25Schristos 			facpri = sys->pri | (msg->imm_loglevel & LOG_FACMASK);
133*26945a25Schristos 		} else {
134*26945a25Schristos 			facpri = sys->facpri;
135*26945a25Schristos 		}
136*26945a25Schristos 	}
137*26945a25Schristos 	syslog(facpri, "%s", msg->imm_msg);
138*26945a25Schristos 	return 0;
139*26945a25Schristos }
140