Importing all of DRL, including ulogd and all of its files.
[distributedratelimiting.git] / extensions / ulogd_LOGEMU.c
1 /* ulogd_LOGEMU.c, Version $Revision: 5252 $
2  *
3  * ulogd output target for syslog logging emulation
4  *
5  * This target produces a file which looks the same like the syslog-entries
6  * of the LOG target.
7  *
8  * (C) 2000-2001 by Harald Welte <laforge@gnumonks.org>
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License version 2 
12  *  as published by the Free Software Foundation
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  * $Id: ulogd_LOGEMU.c 5252 2005-02-14 16:12:49Z laforge $
24  *
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <ulogd/ulogd.h>
32 #include <ulogd/conffile.h>
33 #include "printpkt.c"
34
35 #ifndef ULOGD_LOGEMU_DEFAULT
36 #define ULOGD_LOGEMU_DEFAULT    "/var/log/ulogd.syslogemu"
37 #endif
38
39 #ifndef ULOGD_LOGEMU_SYNC_DEFAULT
40 #define ULOGD_LOGEMU_SYNC_DEFAULT       0
41 #endif
42
43 #define NIPQUAD(addr) \
44         ((unsigned char *)&addr)[0], \
45         ((unsigned char *)&addr)[1], \
46         ((unsigned char *)&addr)[2], \
47         ((unsigned char *)&addr)[3]
48
49 static config_entry_t syslogf_ce = { 
50         .key = "file", 
51         .type = CONFIG_TYPE_STRING, 
52         .options = CONFIG_OPT_NONE, 
53         .u = { .string = ULOGD_LOGEMU_DEFAULT }
54 };
55
56 static config_entry_t syslsync_ce = { 
57         .next = &syslogf_ce, 
58         .key = "sync", 
59         .type = CONFIG_TYPE_INT, 
60         .options = CONFIG_OPT_NONE, 
61         .u = { .value = ULOGD_LOGEMU_SYNC_DEFAULT }
62 };
63
64 static FILE *of = NULL;
65
66 static int _output_logemu(ulog_iret_t *res)
67 {
68         static char buf[4096];
69
70         printpkt_print(res, buf, 1);
71
72         fprintf(of, "%s", buf);
73
74         if (syslsync_ce.u.value) 
75                 fflush(of);
76
77         return 0;
78 }
79
80 static void signal_handler_logemu(int signal)
81 {
82         switch (signal) {
83         case SIGHUP:
84                 ulogd_log(ULOGD_NOTICE, "syslogemu: reopening logfile\n");
85                 fclose(of);
86                 of = fopen(syslogf_ce.u.string, "a");
87                 if (!of) {
88                         ulogd_log(ULOGD_FATAL, "can't open syslogemu: %s\n",
89                                 strerror(errno));
90                         exit(2);
91                 }
92                 break;
93         default:
94                 break;
95         }
96 }
97                 
98
99 static int init_logemu(void) {
100         /* FIXME: error handling */
101         config_parse_file("LOGEMU", &syslsync_ce);
102
103 #ifdef DEBUG_LOGEMU
104         of = stdout;
105 #else
106         of = fopen(syslogf_ce.u.string, "a");
107         if (!of) {
108                 ulogd_log(ULOGD_FATAL, "can't open syslogemu: %s\n", 
109                         strerror(errno));
110                 exit(2);
111         }               
112 #endif
113         if (printpkt_init()) {
114                 ulogd_log(ULOGD_ERROR, "can't resolve all keyhash id's\n");
115         }
116
117         return 1;
118 }
119
120 static void fini_logemu(void) {
121         if (of != stdout)
122                 fclose(of);
123 }
124
125 static ulog_output_t logemu_op = { 
126         .name = "syslogemu",
127         .init = &init_logemu,
128         .fini = &fini_logemu,
129         .output = &_output_logemu, 
130         .signal = &signal_handler_logemu,
131 };
132
133 void _init(void)
134 {
135         register_output(&logemu_op);
136 }