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