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