Importing all of DRL, including ulogd and all of its files.
[distributedratelimiting.git] / extensions / ulogd_SYSLOG.c
1 /* ulogd_SYSLOG.c, Version $Revision: 6433 $
2  *
3  * ulogd output target for real syslog() logging
4  *
5  * This target produces a syslog entries identical to the LOG target.
6  *
7  * (C) 2003 by Harald Welte <laforge@gnumonks.org>
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License version 2 
11  *  as published by the Free Software Foundation
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * $Id: ulogd_SYSLOG.c 6433 2006-01-25 11:22:03Z /C=DE/ST=Berlin/L=Berlin/O=Netfilter Project/OU=Development/CN=laforge/emailAddress=laforge@netfilter.org $
23  *
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <syslog.h>
31 #include <ulogd/ulogd.h>
32 #include <ulogd/conffile.h>
33 #include "printpkt.h"
34
35 #ifndef SYSLOG_FACILITY_DEFAULT
36 #define SYSLOG_FACILITY_DEFAULT "LOG_KERN"
37 #endif
38
39 #ifndef SYSLOG_LEVEL_DEFAULT 
40 #define SYSLOG_LEVEL_DEFAULT "LOG_NOTICE"
41 #endif
42
43 static config_entry_t facility_ce = { 
44         .key = "facility", 
45         .type = CONFIG_TYPE_STRING, 
46         .options = CONFIG_OPT_NONE, 
47         .u = { .string = SYSLOG_FACILITY_DEFAULT } 
48 };
49
50 static config_entry_t level_ce = { 
51         .next = &facility_ce, 
52         .key = "level", 
53         .type = CONFIG_TYPE_STRING,
54         .options = CONFIG_OPT_NONE, 
55         .u = { .string = SYSLOG_LEVEL_DEFAULT }
56 };
57
58 static int syslog_level, syslog_facility;
59
60 static int _output_syslog(ulog_iret_t *res)
61 {
62         static char buf[4096];
63         
64         printpkt_print(res, buf, 0);
65         syslog(syslog_level|syslog_facility, buf);
66
67         return 0;
68 }
69                 
70 static int syslog_init(void)
71 {
72         /* FIXME: error handling */
73         config_parse_file("SYSLOG", &level_ce);
74
75         if (!strcmp(facility_ce.u.string, "LOG_DAEMON"))
76                 syslog_facility = LOG_DAEMON;
77         else if (!strcmp(facility_ce.u.string, "LOG_KERN"))
78                 syslog_facility = LOG_KERN;
79         else if (!strcmp(facility_ce.u.string, "LOG_LOCAL0"))
80                 syslog_facility = LOG_LOCAL0;
81         else if (!strcmp(facility_ce.u.string, "LOG_LOCAL1"))
82                 syslog_facility = LOG_LOCAL1;
83         else if (!strcmp(facility_ce.u.string, "LOG_LOCAL2"))
84                 syslog_facility = LOG_LOCAL2;
85         else if (!strcmp(facility_ce.u.string, "LOG_LOCAL3"))
86                 syslog_facility = LOG_LOCAL3;
87         else if (!strcmp(facility_ce.u.string, "LOG_LOCAL4"))
88                 syslog_facility = LOG_LOCAL4;
89         else if (!strcmp(facility_ce.u.string, "LOG_LOCAL5"))
90                 syslog_facility = LOG_LOCAL5;
91         else if (!strcmp(facility_ce.u.string, "LOG_LOCAL6"))
92                 syslog_facility = LOG_LOCAL6;
93         else if (!strcmp(facility_ce.u.string, "LOG_LOCAL7"))
94                 syslog_facility = LOG_LOCAL7;
95         else if (!strcmp(facility_ce.u.string, "LOG_USER"))
96                 syslog_facility = LOG_USER;
97         else {
98                 ulogd_log(ULOGD_FATAL, "unknown facility '%s'\n",
99                           facility_ce.u.string);
100                 exit(2);
101         }
102
103         if (!strcmp(level_ce.u.string, "LOG_EMERG"))
104                 syslog_level = LOG_EMERG;
105         else if (!strcmp(level_ce.u.string, "LOG_ALERT"))
106                 syslog_level = LOG_ALERT;
107         else if (!strcmp(level_ce.u.string, "LOG_CRIT"))
108                 syslog_level = LOG_CRIT;
109         else if (!strcmp(level_ce.u.string, "LOG_ERR"))
110                 syslog_level = LOG_ERR;
111         else if (!strcmp(level_ce.u.string, "LOG_WARNING"))
112                 syslog_level = LOG_WARNING;
113         else if (!strcmp(level_ce.u.string, "LOG_NOTICE"))
114                 syslog_level = LOG_NOTICE;
115         else if (!strcmp(level_ce.u.string, "LOG_INFO"))
116                 syslog_level = LOG_INFO;
117         else if (!strcmp(level_ce.u.string, "LOG_DEBUG"))
118                 syslog_level = LOG_DEBUG;
119         else {
120                 ulogd_log(ULOGD_FATAL, "unknown level '%s'\n",
121                           level_ce.u.string);
122                 exit(2);
123         }
124
125         openlog("ulogd", LOG_NDELAY|LOG_PID, syslog_facility);
126
127         return 0;
128 }
129
130 static void syslog_fini(void)
131 {
132         closelog();
133 }
134
135 static ulog_output_t syslog_op = { 
136         .name = "syslog", 
137         .init = &syslog_init,
138         .fini = &syslog_fini,
139         .output = &_output_syslog,
140 };
141
142
143 void _init(void)
144 {
145         if (printpkt_init())
146                 ulogd_log(ULOGD_ERROR, "can't resolve all keyhash id's\n");
147
148         register_output(&syslog_op);
149 }