annotate src/syslogconfig.cpp @ 35:d2ceebcf6595 stable-1-7

add message description in patterns
author carl
date Tue, 18 Sep 2007 09:54:22 -0700
parents 601bc0e075e1
children 6a2f26976898
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
551433a01cab initial coding
carl
parents:
diff changeset
1 /***************************************************************************
551433a01cab initial coding
carl
parents:
diff changeset
2 * Copyright (C) 2005 by 510 Software Group *
551433a01cab initial coding
carl
parents:
diff changeset
3 * *
551433a01cab initial coding
carl
parents:
diff changeset
4 * *
551433a01cab initial coding
carl
parents:
diff changeset
5 * This program is free software; you can redistribute it and/or modify *
551433a01cab initial coding
carl
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by *
551433a01cab initial coding
carl
parents:
diff changeset
7 * the Free Software Foundation; either version 2 of the License, or *
551433a01cab initial coding
carl
parents:
diff changeset
8 * (at your option) any later version. *
551433a01cab initial coding
carl
parents:
diff changeset
9 * *
551433a01cab initial coding
carl
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful, *
551433a01cab initial coding
carl
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
551433a01cab initial coding
carl
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
551433a01cab initial coding
carl
parents:
diff changeset
13 * GNU General Public License for more details. *
551433a01cab initial coding
carl
parents:
diff changeset
14 * *
551433a01cab initial coding
carl
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License *
551433a01cab initial coding
carl
parents:
diff changeset
16 * along with this program; if not, write to the *
551433a01cab initial coding
carl
parents:
diff changeset
17 * Free Software Foundation, Inc., *
551433a01cab initial coding
carl
parents:
diff changeset
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
551433a01cab initial coding
carl
parents:
diff changeset
19 ***************************************************************************/
551433a01cab initial coding
carl
parents:
diff changeset
20
551433a01cab initial coding
carl
parents:
diff changeset
21 #include "includes.h"
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
22 #include <fcntl.h>
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
23 #include <sys/socket.h>
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
24 #include <netinet/in.h>
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
25 #include <arpa/inet.h>
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
26 #include <netdb.h>
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
27 #include <limits.h>
1
551433a01cab initial coding
carl
parents:
diff changeset
28
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
29 static char* syslogconfig_version = "$Id$";
1
551433a01cab initial coding
carl
parents:
diff changeset
30
27
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
31 char *token_add;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
32 char *token_bucket;
1
551433a01cab initial coding
carl
parents:
diff changeset
33 char *token_file;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
34 char *token_ignore;
1
551433a01cab initial coding
carl
parents:
diff changeset
35 char *token_include;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
36 char *token_index;
1
551433a01cab initial coding
carl
parents:
diff changeset
37 char *token_lbrace;
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
38 char *token_message;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
39 char *token_pattern;
1
551433a01cab initial coding
carl
parents:
diff changeset
40 char *token_rbrace;
27
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
41 char *token_remove;
1
551433a01cab initial coding
carl
parents:
diff changeset
42 char *token_semi;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
43 char *token_slash;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
44 char *token_threshold;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
45
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
46 struct ltint
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
47 {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
48 bool operator()(const int s1, const int s2) const
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
49 {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
50 return (unsigned)s1 < (unsigned)s2;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
51 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
52 };
1
551433a01cab initial coding
carl
parents:
diff changeset
53
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
54 struct bucket {
2737ab01659a initial coding
carl
parents: 3
diff changeset
55 int count;
2737ab01659a initial coding
carl
parents: 3
diff changeset
56 bool latch; // true iff ever count>threshold
2737ab01659a initial coding
carl
parents: 3
diff changeset
57 };
2737ab01659a initial coding
carl
parents: 3
diff changeset
58
1
551433a01cab initial coding
carl
parents:
diff changeset
59 string_set all_strings; // owns all the strings, only modified by the config loader thread
551433a01cab initial coding
carl
parents:
diff changeset
60 const int maxlen = 1000; // used for snprintf buffers
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
61 typedef map<int, bucket, ltint> ip_buckets;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
62
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
63 class IPR {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
64 ip_buckets violations;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
65 public:
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
66 void add(int ip, int amount, CONFIG &con, char *file_name, int pattern_index, char *message);
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
67 void leak(int amount, CONFIG &con);
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
68 void update(int ip, bool added, char *file_name, int pattern_index, char *message);
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
69 void changed(CONFIG &con, int ip, bool added);
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
70 };
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
71
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
72 IPR recorder;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
73
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
74
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
75 ////////////////////////////////////////////////
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
76 //
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
77 void IPR::add(int ip, int amount, CONFIG &con, char *file_name, int pattern_index, char *message) {
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
78 if (con.looking(ip)) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
79 ip_buckets::iterator i = violations.find(ip);
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
80 if (i == violations.end()) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
81 bucket b;
2737ab01659a initial coding
carl
parents: 3
diff changeset
82 b.count = amount;
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
83 b.latch = (con.get_threshold() <= b.count);
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
84 violations[ip] = b;
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
85 if (b.latch) {
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
86 update(ip, true, file_name, pattern_index, message);
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
87 changed(con, ip, true);
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
88 }
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
89 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
90 else {
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
91 bucket &b = (*i).second;
2737ab01659a initial coding
carl
parents: 3
diff changeset
92 if (b.count < (INT_MAX-amount)) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
93 int t = con.get_threshold();
2737ab01659a initial coding
carl
parents: 3
diff changeset
94 int c = b.count;
2737ab01659a initial coding
carl
parents: 3
diff changeset
95 b.count += amount;
2737ab01659a initial coding
carl
parents: 3
diff changeset
96 if ((!b.latch) && (c < t) && (t <= b.count)) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
97 b.latch = true;
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
98 update(ip, true, file_name, pattern_index, message);
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
99 changed(con, ip, true);
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
100 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
101 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
102 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
103 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
104 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
105
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
106
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
107 void IPR::leak(int amount, CONFIG &con) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
108 for (ip_buckets::iterator i=violations.begin(); i!=violations.end(); ) {
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
109 int ip = (*i).first;
2737ab01659a initial coding
carl
parents: 3
diff changeset
110 bucket &b = (*i).second;
2737ab01659a initial coding
carl
parents: 3
diff changeset
111 if (b.count <= amount) {
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
112 if (b.latch) {
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
113 update(ip, false, NULL, 0, NULL);
24
ec051169fdfd don't flush input chain, use -D option instead
carl
parents: 20
diff changeset
114 changed(con, ip, false);
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
115 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
116 violations.erase(i++);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
117 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
118 else {
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
119 b.count -= amount;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
120 i++;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
121 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
122 }
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
123 }
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
124
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
125
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
126 void IPR::update(int ip, bool added, char *file_name, int pattern_index, char *message) {
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
127 if (debug_syslog > 2) {
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
128 char buf[maxlen];
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
129 in_addr ad;
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
130 ad.s_addr = htonl(ip);
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
131 if (added) {
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
132 if (message) snprintf(buf, maxlen, "dropping traffic from/to %s based on %s in %s", inet_ntoa(ad), message, file_name);
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
133 else snprintf(buf, maxlen, "dropping traffic from/to %s based on pattern match %d in %s", inet_ntoa(ad), pattern_index, file_name);
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
134 }
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
135 else snprintf(buf, maxlen, "allowing traffic from/to %s", inet_ntoa(ad));
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
136 my_syslog(buf);
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
137 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
138 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
139
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
140
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
141 void IPR::changed(CONFIG &con, int ip, bool added) {
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
142 int t = con.get_threshold();
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
143 char buf[maxlen];
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
144 if (added) {
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
145 bucket &b = violations[ip];
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
146 if (con.looking(ip) && (b.count > t)) {
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
147 in_addr ad;
2737ab01659a initial coding
carl
parents: 3
diff changeset
148 ad.s_addr = htonl(ip);
27
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
149 snprintf(buf, maxlen, con.add_command, inet_ntoa(ad));
5
276c4edc8521 initial coding
carl
parents: 4
diff changeset
150 system(buf);
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
151 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
152 }
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
153 else {
24
ec051169fdfd don't flush input chain, use -D option instead
carl
parents: 20
diff changeset
154 in_addr ad;
ec051169fdfd don't flush input chain, use -D option instead
carl
parents: 20
diff changeset
155 ad.s_addr = htonl(ip);
27
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
156 snprintf(buf, maxlen, con.remove_command, inet_ntoa(ad));
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
157 system(buf);
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
158 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
159 }
1
551433a01cab initial coding
carl
parents:
diff changeset
160
551433a01cab initial coding
carl
parents:
diff changeset
161
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
162 ////////////////////////////////////////////////
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
163 //
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
164 int ip_address(char *have);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
165 int ip_address(char *have) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
166 int ipaddr = 0;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
167 in_addr ip;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
168 if (inet_aton(have, &ip)) ipaddr = ip.s_addr;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
169 else {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
170 struct hostent *host = gethostbyname(have);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
171 if (host && host->h_addrtype == AF_INET) memcpy(&ipaddr, host->h_addr, sizeof(ipaddr));
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
172 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
173 return ntohl(ipaddr);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
174 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
175
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
176
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
177 ////////////////////////////////////////////////
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
178 //
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
179 PATTERN::PATTERN(TOKEN &tok, char *pattern_, int index_, int amount_, char *msg_) {
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
180 pattern = pattern_;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
181 index = index_;
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
182 amount = amount_;
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
183 message = msg_;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
184 if (pattern) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
185 int rc = regcomp(&re, pattern, REG_ICASE | REG_EXTENDED);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
186 if (rc) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
187 char bu[maxlen];
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
188 regerror(rc, &re, bu, maxlen);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
189 char buf[maxlen];
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
190 snprintf(buf, sizeof(buf), "pattern %s not valid - %s", pattern, bu);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
191 tok.token_error(buf);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
192 pattern = NULL;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
193 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
194 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
195 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
196
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
197
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
198 PATTERN::~PATTERN() {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
199 regfree(&re);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
200 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
201
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
202
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
203 bool PATTERN::process(char *buf, CONFIG &con, char *file_name, int pattern_index) {
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
204 if (pattern) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
205 const int nmatch = index+1;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
206 regmatch_t match[nmatch];
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
207 if (0 == regexec(&re, buf, nmatch, match, 0)) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
208 int s = match[index].rm_so;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
209 int e = match[index].rm_eo;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
210 if (s != -1) {
5
276c4edc8521 initial coding
carl
parents: 4
diff changeset
211 if (debug_syslog > 3) {
276c4edc8521 initial coding
carl
parents: 4
diff changeset
212 my_syslog(buf); // show lines with matches
276c4edc8521 initial coding
carl
parents: 4
diff changeset
213 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
214 buf[e] = '\0';
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
215 int ip = ip_address(buf+s);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
216 if (ip) {
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
217 recorder.add(ip, amount, con, file_name, pattern_index, message);
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
218 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
219 return true;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
220 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
221 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
222 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
223 return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
224 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
225
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
226
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
227 void PATTERN::dump(int level) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
228 char indent[maxlen];
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
229 int i = min(maxlen-1, level*4);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
230 memset(indent, ' ', i);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
231 indent[i] = '\0';
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
232 printf("%s pattern \"%s\" {; \n", indent, pattern);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
233 printf("%s index %d; \n", indent, index);
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
234 printf("%s bucket %d; \n", indent, amount);
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
235 if (message) printf("%s message \"%s\"; \n", indent, message);
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
236 printf("%s }; \n", indent);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
237 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
238
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
239
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
240 ////////////////////////////////////////////////
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
241 //
1
551433a01cab initial coding
carl
parents:
diff changeset
242 CONFIG::CONFIG() {
551433a01cab initial coding
carl
parents:
diff changeset
243 reference_count = 0;
551433a01cab initial coding
carl
parents:
diff changeset
244 generation = 0;
551433a01cab initial coding
carl
parents:
diff changeset
245 load_time = 0;
27
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
246 threshold = 500;
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
247 add_command = "/sbin/iptables -I INPUT --src %s --jump DROP";
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
248 remove_command = "/sbin/iptables -D INPUT --src %s --jump DROP";
1
551433a01cab initial coding
carl
parents:
diff changeset
249 }
551433a01cab initial coding
carl
parents:
diff changeset
250
551433a01cab initial coding
carl
parents:
diff changeset
251
551433a01cab initial coding
carl
parents:
diff changeset
252 CONFIG::~CONFIG() {
551433a01cab initial coding
carl
parents:
diff changeset
253 for (syslogconfig_list::iterator i=syslogconfigs.begin(); i!=syslogconfigs.end(); i++) {
551433a01cab initial coding
carl
parents:
diff changeset
254 SYSLOGCONFIG *c = *i;
551433a01cab initial coding
carl
parents:
diff changeset
255 delete c;
551433a01cab initial coding
carl
parents:
diff changeset
256 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
257 ignore.clear();
1
551433a01cab initial coding
carl
parents:
diff changeset
258 }
551433a01cab initial coding
carl
parents:
diff changeset
259
551433a01cab initial coding
carl
parents:
diff changeset
260
551433a01cab initial coding
carl
parents:
diff changeset
261 void CONFIG::add_syslogconfig(SYSLOGCONFIGP con) {
551433a01cab initial coding
carl
parents:
diff changeset
262 syslogconfigs.push_back(con);
551433a01cab initial coding
carl
parents:
diff changeset
263 }
551433a01cab initial coding
carl
parents:
diff changeset
264
551433a01cab initial coding
carl
parents:
diff changeset
265
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
266 void CONFIG::add_pair(IPPAIR pair) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
267 ignore.push_back(pair);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
268 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
269
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
270
1
551433a01cab initial coding
carl
parents:
diff changeset
271 void CONFIG::dump() {
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
272 printf(" threshold %d; \n\n", threshold);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
273
27
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
274 printf(" add_command \"%s\"; \n", add_command);
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
275 printf(" remove_command \"%s\"; \n\n", remove_command);
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
276
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
277 printf(" ignore { \n");
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
278 for (ippair_list::iterator i=ignore.begin(); i!=ignore.end(); i++) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
279 IPPAIR &p = *i;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
280 in_addr ip;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
281 ip.s_addr = htonl(p.first);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
282 printf(" %s/%d; \n", inet_ntoa(ip), p.cidr);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
283 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
284 printf(" }; \n\n");
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
285
1
551433a01cab initial coding
carl
parents:
diff changeset
286 for (syslogconfig_list::iterator i=syslogconfigs.begin(); i!=syslogconfigs.end(); i++) {
551433a01cab initial coding
carl
parents:
diff changeset
287 SYSLOGCONFIGP c = *i;
551433a01cab initial coding
carl
parents:
diff changeset
288 c->dump(0);
551433a01cab initial coding
carl
parents:
diff changeset
289 }
551433a01cab initial coding
carl
parents:
diff changeset
290 }
551433a01cab initial coding
carl
parents:
diff changeset
291
551433a01cab initial coding
carl
parents:
diff changeset
292
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
293 void CONFIG::read() {
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
294 while (true) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
295 bool have = false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
296 for (syslogconfig_list::iterator i=syslogconfigs.begin(); i!=syslogconfigs.end(); i++) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
297 SYSLOGCONFIGP c = *i;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
298 have |= c->read(*this);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
299 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
300 if (!have) break;
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
301 }
6e88da080f08 initial coding
carl
parents: 1
diff changeset
302 }
6e88da080f08 initial coding
carl
parents: 1
diff changeset
303
6e88da080f08 initial coding
carl
parents: 1
diff changeset
304
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
305 void CONFIG::sleep(int duration, time_t &previous) {
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
306 ::sleep(duration);
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
307 time_t now = time(NULL);
2737ab01659a initial coding
carl
parents: 3
diff changeset
308 recorder.leak(now-previous, *this);
2737ab01659a initial coding
carl
parents: 3
diff changeset
309 previous = now;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
310 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
311
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
312
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
313 bool CONFIG::looking(int ip) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
314 for (ippair_list::iterator i=ignore.begin(); i!=ignore.end(); i++) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
315 IPPAIR &p = *i;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
316 if ((p.first <= ip) && (ip <= p.last)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
317 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
318 return true;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
319 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
320
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
321 ////////////////////////////////////////////////
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
322 //
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
323 SYSLOGCONFIG::SYSLOGCONFIG(TOKEN &tok, char *file_name_) {
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
324 tokp = &tok;
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
325 file_name = file_name_;
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
326 open(true);
1
551433a01cab initial coding
carl
parents:
diff changeset
327 }
551433a01cab initial coding
carl
parents:
diff changeset
328
551433a01cab initial coding
carl
parents:
diff changeset
329
551433a01cab initial coding
carl
parents:
diff changeset
330 SYSLOGCONFIG::~SYSLOGCONFIG() {
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
331 close();
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
332 for (pattern_list::iterator i=patterns.begin(); i!=patterns.end(); i++) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
333 PATTERN *p = *i;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
334 delete p;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
335 }
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
336 }
6e88da080f08 initial coding
carl
parents: 1
diff changeset
337
6e88da080f08 initial coding
carl
parents: 1
diff changeset
338
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
339 void SYSLOGCONFIG::open(bool msg) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
340 fd = ::open(file_name, O_RDONLY);
2737ab01659a initial coding
carl
parents: 3
diff changeset
341 len = 0;
2737ab01659a initial coding
carl
parents: 3
diff changeset
342 if (fd == -1) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
343 if (msg) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
344 char buf[maxlen];
2737ab01659a initial coding
carl
parents: 3
diff changeset
345 snprintf(buf, sizeof(buf), "syslog file %s not readable", file_name);
2737ab01659a initial coding
carl
parents: 3
diff changeset
346 tokp->token_error(buf);
2737ab01659a initial coding
carl
parents: 3
diff changeset
347 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
348 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
349 else {
5
276c4edc8521 initial coding
carl
parents: 4
diff changeset
350 if (debug_syslog > 1) {
276c4edc8521 initial coding
carl
parents: 4
diff changeset
351 snprintf(buf, sizeof(buf), "syslog file %s opened", file_name);
276c4edc8521 initial coding
carl
parents: 4
diff changeset
352 my_syslog(buf);
276c4edc8521 initial coding
carl
parents: 4
diff changeset
353 }
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
354 lseek(fd, 0, SEEK_END);
2737ab01659a initial coding
carl
parents: 3
diff changeset
355 if (fstat(fd, &openfdstat)) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
356 close();
2737ab01659a initial coding
carl
parents: 3
diff changeset
357 snprintf(buf, sizeof(buf), "syslog file %s cannot stat after open", file_name);
2737ab01659a initial coding
carl
parents: 3
diff changeset
358 tokp->token_error(buf);
2737ab01659a initial coding
carl
parents: 3
diff changeset
359 }
31
carl
parents: 28
diff changeset
360 // specify that this fd gets closed on exec, so that selinux
carl
parents: 28
diff changeset
361 // won't complain about iptables trying to read log files.
carl
parents: 28
diff changeset
362 int oldflags = fcntl(fd, F_GETFD, 0);
carl
parents: 28
diff changeset
363 if (oldflags >= 0) {
carl
parents: 28
diff changeset
364 fcntl(fd, F_SETFD, oldflags | FD_CLOEXEC);
carl
parents: 28
diff changeset
365 }
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
366 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
367 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
368
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
369
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
370 bool SYSLOGCONFIG::read(CONFIG &con) {
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
371 if (failed()) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
372 open(false);
2737ab01659a initial coding
carl
parents: 3
diff changeset
373 if (failed()) return false;
2737ab01659a initial coding
carl
parents: 3
diff changeset
374 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
375 int n = ::read(fd, buf+len, buflen-len);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
376 bool have = (n > 0);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
377 if (have) {
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
378 len += n;
6e88da080f08 initial coding
carl
parents: 1
diff changeset
379 while (true) {
6e88da080f08 initial coding
carl
parents: 1
diff changeset
380 char *p = (char*)memchr(buf, '\n', len);
6e88da080f08 initial coding
carl
parents: 1
diff changeset
381 if (!p) break;
6e88da080f08 initial coding
carl
parents: 1
diff changeset
382 n = p-buf;
6e88da080f08 initial coding
carl
parents: 1
diff changeset
383 *p = '\0';
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
384 process(con); // process null terminated string
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
385 len -= n+1;
6e88da080f08 initial coding
carl
parents: 1
diff changeset
386 memmove(buf, p+1, len);
6e88da080f08 initial coding
carl
parents: 1
diff changeset
387 }
6e88da080f08 initial coding
carl
parents: 1
diff changeset
388 // no <lf> in a full buffer
6e88da080f08 initial coding
carl
parents: 1
diff changeset
389 if (len == buflen) len = 0;
6e88da080f08 initial coding
carl
parents: 1
diff changeset
390 }
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
391 else {
2737ab01659a initial coding
carl
parents: 3
diff changeset
392 // check for file close
2737ab01659a initial coding
carl
parents: 3
diff changeset
393 struct stat filenamest;
2737ab01659a initial coding
carl
parents: 3
diff changeset
394 if (0 == stat(file_name, &filenamest)) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
395 if ((filenamest.st_dev != openfdstat.st_dev) ||
2737ab01659a initial coding
carl
parents: 3
diff changeset
396 (filenamest.st_ino != openfdstat.st_ino)) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
397 close();
2737ab01659a initial coding
carl
parents: 3
diff changeset
398 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
399 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
400 else {
2737ab01659a initial coding
carl
parents: 3
diff changeset
401 // filename no longer exists
2737ab01659a initial coding
carl
parents: 3
diff changeset
402 close();
2737ab01659a initial coding
carl
parents: 3
diff changeset
403 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
404 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
405 return have;
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
406 }
6e88da080f08 initial coding
carl
parents: 1
diff changeset
407
6e88da080f08 initial coding
carl
parents: 1
diff changeset
408
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
409 void SYSLOGCONFIG::close() {
5
276c4edc8521 initial coding
carl
parents: 4
diff changeset
410 if (debug_syslog > 1) {
276c4edc8521 initial coding
carl
parents: 4
diff changeset
411 snprintf(buf, sizeof(buf), "syslog file %s closed", file_name);
276c4edc8521 initial coding
carl
parents: 4
diff changeset
412 my_syslog(buf);
276c4edc8521 initial coding
carl
parents: 4
diff changeset
413 }
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
414 if (fd != -1) ::close(fd);
2737ab01659a initial coding
carl
parents: 3
diff changeset
415 fd = -1;
2737ab01659a initial coding
carl
parents: 3
diff changeset
416 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
417
2737ab01659a initial coding
carl
parents: 3
diff changeset
418
2737ab01659a initial coding
carl
parents: 3
diff changeset
419 void SYSLOGCONFIG::add_pattern(PATTERNP pat) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
420 patterns.push_back(pat);
2737ab01659a initial coding
carl
parents: 3
diff changeset
421 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
422
2737ab01659a initial coding
carl
parents: 3
diff changeset
423
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
424 void SYSLOGCONFIG::process(CONFIG &con) {
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
425 int pi=0;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
426 for (pattern_list::iterator i=patterns.begin(); i!=patterns.end(); i++) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
427 PATTERN *p = *i;
20
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
428 if (p->process(buf, con, file_name, pi)) break;
0d65c3de34fd add better logging
carl
parents: 12
diff changeset
429 pi++;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
430 }
1
551433a01cab initial coding
carl
parents:
diff changeset
431 }
551433a01cab initial coding
carl
parents:
diff changeset
432
551433a01cab initial coding
carl
parents:
diff changeset
433
551433a01cab initial coding
carl
parents:
diff changeset
434 void SYSLOGCONFIG::dump(int level) {
551433a01cab initial coding
carl
parents:
diff changeset
435 char indent[maxlen];
551433a01cab initial coding
carl
parents:
diff changeset
436 int i = min(maxlen-1, level*4);
551433a01cab initial coding
carl
parents:
diff changeset
437 memset(indent, ' ', i);
551433a01cab initial coding
carl
parents:
diff changeset
438 indent[i] = '\0';
551433a01cab initial coding
carl
parents:
diff changeset
439 char buf[maxlen];
551433a01cab initial coding
carl
parents:
diff changeset
440 printf("%s file \"%s\" {\n", indent, file_name);
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
441 for (pattern_list::iterator i=patterns.begin(); i!=patterns.end(); i++) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
442 PATTERN *p = *i;
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
443 p->dump(level+1);
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
444 }
1
551433a01cab initial coding
carl
parents:
diff changeset
445 printf("%s }; \n", indent);
551433a01cab initial coding
carl
parents:
diff changeset
446 }
551433a01cab initial coding
carl
parents:
diff changeset
447
551433a01cab initial coding
carl
parents:
diff changeset
448
551433a01cab initial coding
carl
parents:
diff changeset
449 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
450 // helper to discard the strings held by a string_set
551433a01cab initial coding
carl
parents:
diff changeset
451 //
551433a01cab initial coding
carl
parents:
diff changeset
452 void discard(string_set &s) {
551433a01cab initial coding
carl
parents:
diff changeset
453 for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
551433a01cab initial coding
carl
parents:
diff changeset
454 free(*i);
551433a01cab initial coding
carl
parents:
diff changeset
455 }
551433a01cab initial coding
carl
parents:
diff changeset
456 s.clear();
551433a01cab initial coding
carl
parents:
diff changeset
457 }
551433a01cab initial coding
carl
parents:
diff changeset
458
551433a01cab initial coding
carl
parents:
diff changeset
459
551433a01cab initial coding
carl
parents:
diff changeset
460 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
461 // helper to register a string in a string set
551433a01cab initial coding
carl
parents:
diff changeset
462 //
551433a01cab initial coding
carl
parents:
diff changeset
463 char* register_string(string_set &s, char *name) {
551433a01cab initial coding
carl
parents:
diff changeset
464 string_set::iterator i = s.find(name);
551433a01cab initial coding
carl
parents:
diff changeset
465 if (i != s.end()) return *i;
551433a01cab initial coding
carl
parents:
diff changeset
466 char *x = strdup(name);
551433a01cab initial coding
carl
parents:
diff changeset
467 s.insert(x);
551433a01cab initial coding
carl
parents:
diff changeset
468 return x;
551433a01cab initial coding
carl
parents:
diff changeset
469 }
551433a01cab initial coding
carl
parents:
diff changeset
470
551433a01cab initial coding
carl
parents:
diff changeset
471
551433a01cab initial coding
carl
parents:
diff changeset
472 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
473 // register a global string
551433a01cab initial coding
carl
parents:
diff changeset
474 //
551433a01cab initial coding
carl
parents:
diff changeset
475 char* register_string(char *name) {
551433a01cab initial coding
carl
parents:
diff changeset
476 return register_string(all_strings, name);
551433a01cab initial coding
carl
parents:
diff changeset
477 }
551433a01cab initial coding
carl
parents:
diff changeset
478
551433a01cab initial coding
carl
parents:
diff changeset
479
551433a01cab initial coding
carl
parents:
diff changeset
480 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
481 //
551433a01cab initial coding
carl
parents:
diff changeset
482 bool tsa(TOKEN &tok, char *token);
551433a01cab initial coding
carl
parents:
diff changeset
483 bool tsa(TOKEN &tok, char *token) {
551433a01cab initial coding
carl
parents:
diff changeset
484 char *have = tok.next();
551433a01cab initial coding
carl
parents:
diff changeset
485 if (have == token) return true;
551433a01cab initial coding
carl
parents:
diff changeset
486 tok.token_error(token, have);
551433a01cab initial coding
carl
parents:
diff changeset
487 return false;
551433a01cab initial coding
carl
parents:
diff changeset
488 }
551433a01cab initial coding
carl
parents:
diff changeset
489
551433a01cab initial coding
carl
parents:
diff changeset
490
551433a01cab initial coding
carl
parents:
diff changeset
491 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
492 //
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
493 bool parse_pattern(TOKEN &tok, SYSLOGCONFIG &con);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
494 bool parse_pattern(TOKEN &tok, SYSLOGCONFIG &con) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
495 char *pat = tok.next();
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
496 int ind, buc;
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
497 char *msg = NULL;
1
551433a01cab initial coding
carl
parents:
diff changeset
498 if (!tsa(tok, token_lbrace)) return false;
551433a01cab initial coding
carl
parents:
diff changeset
499 while (true) {
551433a01cab initial coding
carl
parents:
diff changeset
500 char *have = tok.next();
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
501 if (!have) break;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
502 if (have == token_rbrace) break;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
503 if (have == token_index) {
1
551433a01cab initial coding
carl
parents:
diff changeset
504 have = tok.next();
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
505 ind = atoi(have);
1
551433a01cab initial coding
carl
parents:
diff changeset
506 if (!tsa(tok, token_semi)) return false;
551433a01cab initial coding
carl
parents:
diff changeset
507 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
508 else if (have == token_bucket) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
509 have = tok.next();
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
510 buc = atoi(have);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
511 if (!tsa(tok, token_semi)) return false;
1
551433a01cab initial coding
carl
parents:
diff changeset
512 }
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
513 else if (have == token_message) {
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
514 msg = tok.next();
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
515 if (!tsa(tok, token_semi)) return false;
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
516 }
1
551433a01cab initial coding
carl
parents:
diff changeset
517 else {
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
518 tok.token_error("index/bucket", have);
1
551433a01cab initial coding
carl
parents:
diff changeset
519 return false;
551433a01cab initial coding
carl
parents:
diff changeset
520 }
551433a01cab initial coding
carl
parents:
diff changeset
521 }
551433a01cab initial coding
carl
parents:
diff changeset
522 if (!tsa(tok, token_semi)) return false;
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
523 PATTERNP patt = new PATTERN(tok, pat, ind, buc, msg);
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
524 con.add_pattern(patt);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
525 return true;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
526 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
527
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
528
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
529 ////////////////////////////////////////////////
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
530 //
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
531 bool parse_ignore(TOKEN &tok, CONFIG &dc);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
532 bool parse_ignore(TOKEN &tok, CONFIG &dc) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
533 if (!tsa(tok, token_lbrace)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
534 while (true) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
535 char *have = tok.next();
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
536 if (!have) break;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
537 if (have == token_rbrace) break;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
538 int ipaddr = ip_address(have);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
539 if (ipaddr == 0) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
540 tok.token_error("ip address", have);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
541 return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
542 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
543 if (!tsa(tok, token_slash)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
544 have = tok.next();
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
545 int mask = atoi(have);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
546 if ((mask < 8) || (mask > 32)) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
547 tok.token_error("cidr 8..32 value", have);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
548 return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
549 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
550 if (!tsa(tok, token_semi)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
551 IPPAIR pair;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
552 const int masks[33] = {0xffffffff, // 0
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
553 0x7fffffff, // 1
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
554 0x3fffffff, // 2
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
555 0x1fffffff, // 3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
556 0x0fffffff, // 4
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
557 0x07ffffff, // 5
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
558 0x03ffffff, // 6
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
559 0x01ffffff, // 7
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
560 0x00ffffff, // 8
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
561 0x007fffff, // 9
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
562 0x003fffff, // 10
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
563 0x001fffff, // 11
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
564 0x000fffff, // 12
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
565 0x0007ffff, // 13
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
566 0x0003ffff, // 14
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
567 0x0001ffff, // 15
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
568 0x0000ffff, // 16
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
569 0x00007fff, // 17
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
570 0x00003fff, // 18
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
571 0x00001fff, // 19
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
572 0x00000fff, // 20
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
573 0x000007ff, // 21
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
574 0x000003ff, // 22
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
575 0x000001ff, // 23
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
576 0x000000ff, // 24
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
577 0x0000007f, // 25
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
578 0x0000003f, // 26
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
579 0x0000001f, // 27
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
580 0x0000000f, // 28
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
581 0x00000007, // 29
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
582 0x00000003, // 30
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
583 0x00000001, // 31
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
584 0x00000000}; // 32
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
585 pair.first = ipaddr;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
586 pair.last = ipaddr | masks[mask];
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
587 pair.cidr = mask;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
588 dc.add_pair(pair);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
589 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
590 if (!tsa(tok, token_semi)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
591 return true;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
592 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
593
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
594
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
595 ////////////////////////////////////////////////
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
596 //
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
597 bool parse_syslogconfig(TOKEN &tok, CONFIG &dc);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
598 bool parse_syslogconfig(TOKEN &tok, CONFIG &dc) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
599 char *name = tok.next();
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
600 if (!tsa(tok, token_lbrace)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
601 SYSLOGCONFIGP con = new SYSLOGCONFIG(tok, name);
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
602 if (con->failed()) {
6e88da080f08 initial coding
carl
parents: 1
diff changeset
603 delete con;
6e88da080f08 initial coding
carl
parents: 1
diff changeset
604 return false;
6e88da080f08 initial coding
carl
parents: 1
diff changeset
605 }
1
551433a01cab initial coding
carl
parents:
diff changeset
606 dc.add_syslogconfig(con);
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
607 while (true) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
608 char *have = tok.next();
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
609 if (!have) break;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
610 if (have == token_rbrace) break;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
611 if (have == token_pattern) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
612 if (!parse_pattern(tok, *con)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
613 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
614 else {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
615 tok.token_error("pattern", have);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
616 return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
617 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
618 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
619 if (!tsa(tok, token_semi)) return false;
1
551433a01cab initial coding
carl
parents:
diff changeset
620 return true;
551433a01cab initial coding
carl
parents:
diff changeset
621 }
551433a01cab initial coding
carl
parents:
diff changeset
622
551433a01cab initial coding
carl
parents:
diff changeset
623
551433a01cab initial coding
carl
parents:
diff changeset
624 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
625 // parse a config file
551433a01cab initial coding
carl
parents:
diff changeset
626 //
551433a01cab initial coding
carl
parents:
diff changeset
627 bool load_conf(CONFIG &dc, char *fn) {
551433a01cab initial coding
carl
parents:
diff changeset
628 int count = 0;
551433a01cab initial coding
carl
parents:
diff changeset
629 TOKEN tok(fn, &dc.config_files);
551433a01cab initial coding
carl
parents:
diff changeset
630 while (true) {
551433a01cab initial coding
carl
parents:
diff changeset
631 char *have = tok.next();
551433a01cab initial coding
carl
parents:
diff changeset
632 if (!have) break;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
633 if (have == token_threshold) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
634 have = tok.next();
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
635 dc.set_threshold(atoi(have));
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
636 if (!tsa(tok, token_semi)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
637 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
638 else if (have == token_ignore) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
639 if (!parse_ignore(tok, dc)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
640 }
27
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
641 else if (have == token_add) {
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
642 have = tok.next();
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
643 dc.set_add(have);
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
644 if (!tsa(tok, token_semi)) return false;
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
645 }
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
646 else if (have == token_remove) {
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
647 have = tok.next();
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
648 dc.set_remove(have);
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
649 if (!tsa(tok, token_semi)) return false;
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
650 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
651 else if (have == token_file) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
652 if (!parse_syslogconfig(tok, dc)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
653 count++;
1
551433a01cab initial coding
carl
parents:
diff changeset
654 }
551433a01cab initial coding
carl
parents:
diff changeset
655 else {
28
6465d8640489 make add/remove commands configureable
carl
parents: 27
diff changeset
656 tok.token_error("threshold/ignore/add_command/remove_command/file", have);
1
551433a01cab initial coding
carl
parents:
diff changeset
657 return false;
551433a01cab initial coding
carl
parents:
diff changeset
658 }
551433a01cab initial coding
carl
parents:
diff changeset
659 }
551433a01cab initial coding
carl
parents:
diff changeset
660 tok.token_error("load_conf() found %d syslog files in %s", count, fn);
551433a01cab initial coding
carl
parents:
diff changeset
661 return (!dc.syslogconfigs.empty());
551433a01cab initial coding
carl
parents:
diff changeset
662 }
551433a01cab initial coding
carl
parents:
diff changeset
663
551433a01cab initial coding
carl
parents:
diff changeset
664
551433a01cab initial coding
carl
parents:
diff changeset
665 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
666 // init the tokens
551433a01cab initial coding
carl
parents:
diff changeset
667 //
551433a01cab initial coding
carl
parents:
diff changeset
668 void token_init() {
27
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
669 token_add = register_string("add_command");
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
670 token_bucket = register_string("bucket");
1
551433a01cab initial coding
carl
parents:
diff changeset
671 token_file = register_string("file");
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
672 token_ignore = register_string("ignore");
1
551433a01cab initial coding
carl
parents:
diff changeset
673 token_include = register_string("include");
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
674 token_index = register_string("index");
1
551433a01cab initial coding
carl
parents:
diff changeset
675 token_lbrace = register_string("{");
35
d2ceebcf6595 add message description in patterns
carl
parents: 31
diff changeset
676 token_message = register_string("message");
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
677 token_pattern = register_string("pattern");
1
551433a01cab initial coding
carl
parents:
diff changeset
678 token_rbrace = register_string("}");
27
28fec0c67646 make add/remove commands configureable
carl
parents: 24
diff changeset
679 token_remove = register_string("remove_command");
1
551433a01cab initial coding
carl
parents:
diff changeset
680 token_semi = register_string(";");
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
681 token_slash = register_string("/");
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
682 token_threshold = register_string("threshold");
1
551433a01cab initial coding
carl
parents:
diff changeset
683 }