141edb306SCy Schubert #include "ipf.h"
241edb306SCy Schubert #include "ipmon.h"
341edb306SCy Schubert #include <syslog.h>
441edb306SCy Schubert
541edb306SCy Schubert static void *syslog_parse(char **);
641edb306SCy Schubert static void syslog_destroy(void *);
741edb306SCy Schubert static int syslog_send(void *, ipmon_msg_t *);
841edb306SCy Schubert static void syslog_print(void *);
941edb306SCy Schubert
1041edb306SCy Schubert typedef struct syslog_opts_s {
1141edb306SCy Schubert int facpri;
1241edb306SCy Schubert int fac;
1341edb306SCy Schubert int pri;
1441edb306SCy Schubert } syslog_opts_t;
1541edb306SCy Schubert
1641edb306SCy Schubert ipmon_saver_t syslogsaver = {
1741edb306SCy Schubert "syslog",
1841edb306SCy Schubert syslog_destroy,
1941edb306SCy Schubert NULL, /* dup */
2041edb306SCy Schubert NULL, /* match */
2141edb306SCy Schubert syslog_parse,
2241edb306SCy Schubert syslog_print,
2341edb306SCy Schubert syslog_send
2441edb306SCy Schubert };
2541edb306SCy Schubert
2641edb306SCy Schubert
2741edb306SCy Schubert static void *
syslog_parse(char ** strings)2841edb306SCy Schubert syslog_parse(char **strings)
2941edb306SCy Schubert {
3041edb306SCy Schubert syslog_opts_t *ctx;
3141edb306SCy Schubert char *str;
3241edb306SCy Schubert char *s;
3341edb306SCy Schubert
3441edb306SCy Schubert ctx = calloc(1, sizeof(*ctx));
3541edb306SCy Schubert if (ctx == NULL)
36*2582ae57SCy Schubert return (NULL);
3741edb306SCy Schubert
3841edb306SCy Schubert ctx->facpri = -1;
3941edb306SCy Schubert
4041edb306SCy Schubert if (strings[0] != NULL && strings[0][0] != '\0') {
4141edb306SCy Schubert str = strdup(*strings);
4241edb306SCy Schubert if (str != NULL && *str != '\0') {
4341edb306SCy Schubert int fac = -1, pri = -1;
4441edb306SCy Schubert
4541edb306SCy Schubert s = strchr(str, '.');
4641edb306SCy Schubert if (s != NULL)
4741edb306SCy Schubert *s++ = '\0';
4841edb306SCy Schubert
4941edb306SCy Schubert if (*str != '\0') {
5041edb306SCy Schubert fac = fac_findname(str);
5141edb306SCy Schubert if (fac == -1) {
5241edb306SCy Schubert free(str);
5341edb306SCy Schubert free(ctx);
54*2582ae57SCy Schubert return (NULL);
5541edb306SCy Schubert }
5641edb306SCy Schubert }
5741edb306SCy Schubert
5841edb306SCy Schubert if (s != NULL && *s != '\0') {
5941edb306SCy Schubert pri = pri_findname(s);
6041edb306SCy Schubert if (pri == -1) {
6141edb306SCy Schubert free(str);
6241edb306SCy Schubert free(ctx);
63*2582ae57SCy Schubert return (NULL);
6441edb306SCy Schubert }
6541edb306SCy Schubert }
6641edb306SCy Schubert free(str);
6741edb306SCy Schubert
6841edb306SCy Schubert ctx->fac = fac;
6941edb306SCy Schubert ctx->pri = pri;
7041edb306SCy Schubert if (pri == -1)
7141edb306SCy Schubert ctx->facpri = fac;
7241edb306SCy Schubert else if (fac == -1)
7341edb306SCy Schubert ctx->facpri = pri;
7441edb306SCy Schubert else
7541edb306SCy Schubert ctx->facpri = fac | pri;
7641edb306SCy Schubert } else {
7741edb306SCy Schubert if (str != NULL)
7841edb306SCy Schubert free(str);
7941edb306SCy Schubert free(ctx);
8041edb306SCy Schubert ctx = NULL;
8141edb306SCy Schubert }
8241edb306SCy Schubert }
8341edb306SCy Schubert
84*2582ae57SCy Schubert return (ctx);
8541edb306SCy Schubert }
8641edb306SCy Schubert
8741edb306SCy Schubert
8841edb306SCy Schubert static void
syslog_print(void * ctx)89efeb8bffSCy Schubert syslog_print(void *ctx)
9041edb306SCy Schubert {
9141edb306SCy Schubert syslog_opts_t *sys = ctx;
9241edb306SCy Schubert
9341edb306SCy Schubert if (sys->facpri == -1)
9441edb306SCy Schubert return;
9541edb306SCy Schubert
9641edb306SCy Schubert if (sys->fac == -1) {
9741edb306SCy Schubert printf(".%s", pri_toname(sys->pri));
9841edb306SCy Schubert } else if (sys->pri == -1) {
9941edb306SCy Schubert printf("%s.", fac_toname(sys->fac));
10041edb306SCy Schubert } else {
10141edb306SCy Schubert printf("%s.%s", fac_toname(sys->facpri & LOG_FACMASK),
10241edb306SCy Schubert pri_toname(sys->facpri & LOG_PRIMASK));
10341edb306SCy Schubert }
10441edb306SCy Schubert }
10541edb306SCy Schubert
10641edb306SCy Schubert
10741edb306SCy Schubert static void
syslog_destroy(void * ctx)108efeb8bffSCy Schubert syslog_destroy(void *ctx)
10941edb306SCy Schubert {
11041edb306SCy Schubert free(ctx);
11141edb306SCy Schubert }
11241edb306SCy Schubert
11341edb306SCy Schubert
11441edb306SCy Schubert static int
syslog_send(void * ctx,ipmon_msg_t * msg)115efeb8bffSCy Schubert syslog_send(void *ctx, ipmon_msg_t *msg)
11641edb306SCy Schubert {
11741edb306SCy Schubert syslog_opts_t *sys = ctx;
11841edb306SCy Schubert int facpri;
11941edb306SCy Schubert
12041edb306SCy Schubert if (sys->facpri == -1) {
12141edb306SCy Schubert facpri = msg->imm_loglevel;
12241edb306SCy Schubert } else {
12341edb306SCy Schubert if (sys->pri == -1) {
12441edb306SCy Schubert facpri = sys->fac | (msg->imm_loglevel & LOG_PRIMASK);
12541edb306SCy Schubert } else if (sys->fac == -1) {
12641edb306SCy Schubert facpri = sys->pri | (msg->imm_loglevel & LOG_FACMASK);
12741edb306SCy Schubert } else {
12841edb306SCy Schubert facpri = sys->facpri;
12941edb306SCy Schubert }
13041edb306SCy Schubert }
13141edb306SCy Schubert syslog(facpri, "%s", msg->imm_msg);
132*2582ae57SCy Schubert return (0);
13341edb306SCy Schubert }
134