annotate src/syslogconfig.cpp @ 14:2a7161b03b94

final documentation, rpm builds properly
author carl
date Sat, 17 Dec 2005 17:06:28 -0800
parents c2a2e35a85ac
children 0d65c3de34fd
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$";
2737ab01659a initial coding
carl
parents: 3
diff changeset
30 static char* iptables = "/sbin/iptables";
1
551433a01cab initial coding
carl
parents:
diff changeset
31
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;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
38 char *token_pattern;
1
551433a01cab initial coding
carl
parents:
diff changeset
39 char *token_rbrace;
551433a01cab initial coding
carl
parents:
diff changeset
40 char *token_semi;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
41 char *token_slash;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
42 char *token_threshold;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
43
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
44 struct ltint
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
45 {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
46 bool operator()(const int s1, const int s2) const
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
47 {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
48 return (unsigned)s1 < (unsigned)s2;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
49 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
50 };
1
551433a01cab initial coding
carl
parents:
diff changeset
51
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
52 struct bucket {
2737ab01659a initial coding
carl
parents: 3
diff changeset
53 int count;
2737ab01659a initial coding
carl
parents: 3
diff changeset
54 bool latch; // true iff ever count>threshold
2737ab01659a initial coding
carl
parents: 3
diff changeset
55 };
2737ab01659a initial coding
carl
parents: 3
diff changeset
56
1
551433a01cab initial coding
carl
parents:
diff changeset
57 string_set all_strings; // owns all the strings, only modified by the config loader thread
551433a01cab initial coding
carl
parents:
diff changeset
58 const int maxlen = 1000; // used for snprintf buffers
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
59 typedef map<int, bucket, ltint> ip_buckets;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
60
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
61 class IPR {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
62 ip_buckets violations;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
63 public:
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
64 void add(int ip, int amount, CONFIG &con);
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
65 void changed(CONFIG &con);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
66 void leak(int amount, CONFIG &con);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
67 };
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
68
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
69 IPR recorder;
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 ////////////////////////////////////////////////
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
73 //
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
74 void IPR::add(int ip, int amount, CONFIG &con) {
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
75 if (con.looking(ip)) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
76 ip_buckets::iterator i = violations.find(ip);
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
77 if (i == violations.end()) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
78 bucket b;
2737ab01659a initial coding
carl
parents: 3
diff changeset
79 b.count = amount;
2737ab01659a initial coding
carl
parents: 3
diff changeset
80 b.latch = false;
2737ab01659a initial coding
carl
parents: 3
diff changeset
81 violations[ip] = b;
2737ab01659a initial coding
carl
parents: 3
diff changeset
82 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
83 else {
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
84 bucket &b = (*i).second;
2737ab01659a initial coding
carl
parents: 3
diff changeset
85 if (b.count < (INT_MAX-amount)) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
86 int t = con.get_threshold();
2737ab01659a initial coding
carl
parents: 3
diff changeset
87 int c = b.count;
2737ab01659a initial coding
carl
parents: 3
diff changeset
88 b.count += amount;
2737ab01659a initial coding
carl
parents: 3
diff changeset
89 if ((!b.latch) && (c < t) && (t <= b.count)) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
90 b.latch = true;
2737ab01659a initial coding
carl
parents: 3
diff changeset
91 changed(con);
2737ab01659a initial coding
carl
parents: 3
diff changeset
92 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
93 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
94 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
95 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
96 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
97
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
98
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
99 void IPR::leak(int amount, CONFIG &con) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
100 bool ch = false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
101 for (ip_buckets::iterator i=violations.begin(); i!=violations.end(); ) {
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
102 int ip = (*i).first;
2737ab01659a initial coding
carl
parents: 3
diff changeset
103 bucket &b = (*i).second;
2737ab01659a initial coding
carl
parents: 3
diff changeset
104 if (b.count <= amount) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
105 ch |= b.latch;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
106 violations.erase(i++);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
107 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
108 else {
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
109 b.count -= amount;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
110 i++;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
111 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
112 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
113 if (ch) changed(con);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
114 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
115
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
116
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
117 void IPR::changed(CONFIG &con) {
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
118 char buf[maxlen];
2737ab01659a initial coding
carl
parents: 3
diff changeset
119 snprintf(buf, maxlen, "%s -F INPUT", iptables);
5
276c4edc8521 initial coding
carl
parents: 4
diff changeset
120 if (debug_syslog > 2) {
276c4edc8521 initial coding
carl
parents: 4
diff changeset
121 my_syslog(" ");
276c4edc8521 initial coding
carl
parents: 4
diff changeset
122 my_syslog(buf);
276c4edc8521 initial coding
carl
parents: 4
diff changeset
123 }
276c4edc8521 initial coding
carl
parents: 4
diff changeset
124 system(buf);
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
125 for (ip_buckets::iterator i=violations.begin(); i!=violations.end(); i++) {
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
126 int ip = (*i).first;
2737ab01659a initial coding
carl
parents: 3
diff changeset
127 bucket &b = (*i).second;
2737ab01659a initial coding
carl
parents: 3
diff changeset
128 if (b.count > con.get_threshold()) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
129 in_addr ad;
2737ab01659a initial coding
carl
parents: 3
diff changeset
130 ad.s_addr = htonl(ip);
2737ab01659a initial coding
carl
parents: 3
diff changeset
131 snprintf(buf, maxlen, "count=%d %s -A INPUT --src %s --jump DROP", b.count, iptables, inet_ntoa(ad));
5
276c4edc8521 initial coding
carl
parents: 4
diff changeset
132 if (debug_syslog > 2) my_syslog(buf);
276c4edc8521 initial coding
carl
parents: 4
diff changeset
133 system(buf);
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
134 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
135 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
136 }
1
551433a01cab initial coding
carl
parents:
diff changeset
137
551433a01cab initial coding
carl
parents:
diff changeset
138
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
139 ////////////////////////////////////////////////
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
140 //
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
141 int ip_address(char *have);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
142 int ip_address(char *have) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
143 int ipaddr = 0;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
144 in_addr ip;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
145 if (inet_aton(have, &ip)) ipaddr = ip.s_addr;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
146 else {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
147 struct hostent *host = gethostbyname(have);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
148 if (host && host->h_addrtype == AF_INET) memcpy(&ipaddr, host->h_addr, sizeof(ipaddr));
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
149 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
150 return ntohl(ipaddr);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
151 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
152
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
153
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
154 ////////////////////////////////////////////////
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
155 //
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
156 PATTERN::PATTERN(TOKEN &tok, char *pattern_, int index_, int amount_) {
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
157 pattern = pattern_;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
158 index = index_;
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
159 amount = amount_;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
160 if (pattern) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
161 int rc = regcomp(&re, pattern, REG_ICASE | REG_EXTENDED);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
162 if (rc) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
163 char bu[maxlen];
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
164 regerror(rc, &re, bu, maxlen);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
165 char buf[maxlen];
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
166 snprintf(buf, sizeof(buf), "pattern %s not valid - %s", pattern, bu);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
167 tok.token_error(buf);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
168 pattern = NULL;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
169 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
170 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
171 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
172
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
173
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
174 PATTERN::~PATTERN() {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
175 regfree(&re);
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
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
179 bool PATTERN::process(char *buf, CONFIG &con) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
180 if (pattern) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
181 const int nmatch = index+1;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
182 regmatch_t match[nmatch];
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
183 if (0 == regexec(&re, buf, nmatch, match, 0)) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
184 int s = match[index].rm_so;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
185 int e = match[index].rm_eo;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
186 if (s != -1) {
5
276c4edc8521 initial coding
carl
parents: 4
diff changeset
187 if (debug_syslog > 3) {
276c4edc8521 initial coding
carl
parents: 4
diff changeset
188 my_syslog(buf); // show lines with matches
276c4edc8521 initial coding
carl
parents: 4
diff changeset
189 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
190 buf[e] = '\0';
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
191 int ip = ip_address(buf+s);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
192 if (ip) {
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
193 recorder.add(ip, amount, con);
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
194 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
195 return true;
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 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
199 return false;
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
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
203 void PATTERN::dump(int level) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
204 char indent[maxlen];
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
205 int i = min(maxlen-1, level*4);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
206 memset(indent, ' ', i);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
207 indent[i] = '\0';
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
208 printf("%s pattern \"%s\" {; \n", indent, pattern);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
209 printf("%s index %d; \n", indent, index);
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
210 printf("%s bucket %d; \n", indent, amount);
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
211 printf("%s }; \n", indent);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
212 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
213
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
214
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
215 ////////////////////////////////////////////////
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
216 //
1
551433a01cab initial coding
carl
parents:
diff changeset
217 CONFIG::CONFIG() {
551433a01cab initial coding
carl
parents:
diff changeset
218 reference_count = 0;
551433a01cab initial coding
carl
parents:
diff changeset
219 generation = 0;
551433a01cab initial coding
carl
parents:
diff changeset
220 load_time = 0;
551433a01cab initial coding
carl
parents:
diff changeset
221 }
551433a01cab initial coding
carl
parents:
diff changeset
222
551433a01cab initial coding
carl
parents:
diff changeset
223
551433a01cab initial coding
carl
parents:
diff changeset
224 CONFIG::~CONFIG() {
551433a01cab initial coding
carl
parents:
diff changeset
225 for (syslogconfig_list::iterator i=syslogconfigs.begin(); i!=syslogconfigs.end(); i++) {
551433a01cab initial coding
carl
parents:
diff changeset
226 SYSLOGCONFIG *c = *i;
551433a01cab initial coding
carl
parents:
diff changeset
227 delete c;
551433a01cab initial coding
carl
parents:
diff changeset
228 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
229 ignore.clear();
1
551433a01cab initial coding
carl
parents:
diff changeset
230 }
551433a01cab initial coding
carl
parents:
diff changeset
231
551433a01cab initial coding
carl
parents:
diff changeset
232
551433a01cab initial coding
carl
parents:
diff changeset
233 void CONFIG::add_syslogconfig(SYSLOGCONFIGP con) {
551433a01cab initial coding
carl
parents:
diff changeset
234 syslogconfigs.push_back(con);
551433a01cab initial coding
carl
parents:
diff changeset
235 }
551433a01cab initial coding
carl
parents:
diff changeset
236
551433a01cab initial coding
carl
parents:
diff changeset
237
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
238 void CONFIG::add_pair(IPPAIR pair) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
239 ignore.push_back(pair);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
240 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
241
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
242
1
551433a01cab initial coding
carl
parents:
diff changeset
243 void CONFIG::dump() {
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
244 printf(" threshold %d; \n\n", threshold);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
245
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
246 printf(" ignore { \n");
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
247 for (ippair_list::iterator i=ignore.begin(); i!=ignore.end(); i++) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
248 IPPAIR &p = *i;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
249 in_addr ip;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
250 ip.s_addr = htonl(p.first);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
251 printf(" %s/%d; \n", inet_ntoa(ip), p.cidr);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
252 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
253 printf(" }; \n\n");
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
254
1
551433a01cab initial coding
carl
parents:
diff changeset
255 for (syslogconfig_list::iterator i=syslogconfigs.begin(); i!=syslogconfigs.end(); i++) {
551433a01cab initial coding
carl
parents:
diff changeset
256 SYSLOGCONFIGP c = *i;
551433a01cab initial coding
carl
parents:
diff changeset
257 c->dump(0);
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
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
262 void CONFIG::read() {
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
263 while (true) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
264 bool have = false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
265 for (syslogconfig_list::iterator i=syslogconfigs.begin(); i!=syslogconfigs.end(); i++) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
266 SYSLOGCONFIGP c = *i;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
267 have |= c->read(*this);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
268 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
269 if (!have) break;
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
270 }
6e88da080f08 initial coding
carl
parents: 1
diff changeset
271 }
6e88da080f08 initial coding
carl
parents: 1
diff changeset
272
6e88da080f08 initial coding
carl
parents: 1
diff changeset
273
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
274 void CONFIG::sleep(int duration, time_t &previous) {
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
275 ::sleep(duration);
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
276 time_t now = time(NULL);
2737ab01659a initial coding
carl
parents: 3
diff changeset
277 recorder.leak(now-previous, *this);
2737ab01659a initial coding
carl
parents: 3
diff changeset
278 previous = now;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
279 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
280
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
281
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
282 bool CONFIG::looking(int ip) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
283 for (ippair_list::iterator i=ignore.begin(); i!=ignore.end(); i++) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
284 IPPAIR &p = *i;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
285 if ((p.first <= ip) && (ip <= p.last)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
286 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
287 return true;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
288 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
289
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
290 ////////////////////////////////////////////////
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
291 //
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
292 SYSLOGCONFIG::SYSLOGCONFIG(TOKEN &tok, char *file_name_) {
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
293 tokp = &tok;
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
294 file_name = file_name_;
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
295 open(true);
1
551433a01cab initial coding
carl
parents:
diff changeset
296 }
551433a01cab initial coding
carl
parents:
diff changeset
297
551433a01cab initial coding
carl
parents:
diff changeset
298
551433a01cab initial coding
carl
parents:
diff changeset
299 SYSLOGCONFIG::~SYSLOGCONFIG() {
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
300 close();
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
301 for (pattern_list::iterator i=patterns.begin(); i!=patterns.end(); i++) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
302 PATTERN *p = *i;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
303 delete p;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
304 }
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
305 }
6e88da080f08 initial coding
carl
parents: 1
diff changeset
306
6e88da080f08 initial coding
carl
parents: 1
diff changeset
307
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
308 void SYSLOGCONFIG::open(bool msg) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
309 fd = ::open(file_name, O_RDONLY);
2737ab01659a initial coding
carl
parents: 3
diff changeset
310 len = 0;
2737ab01659a initial coding
carl
parents: 3
diff changeset
311 if (fd == -1) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
312 if (msg) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
313 char buf[maxlen];
2737ab01659a initial coding
carl
parents: 3
diff changeset
314 snprintf(buf, sizeof(buf), "syslog file %s not readable", file_name);
2737ab01659a initial coding
carl
parents: 3
diff changeset
315 tokp->token_error(buf);
2737ab01659a initial coding
carl
parents: 3
diff changeset
316 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
317 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
318 else {
5
276c4edc8521 initial coding
carl
parents: 4
diff changeset
319 if (debug_syslog > 1) {
276c4edc8521 initial coding
carl
parents: 4
diff changeset
320 snprintf(buf, sizeof(buf), "syslog file %s opened", file_name);
276c4edc8521 initial coding
carl
parents: 4
diff changeset
321 my_syslog(buf);
276c4edc8521 initial coding
carl
parents: 4
diff changeset
322 }
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
323 lseek(fd, 0, SEEK_END);
2737ab01659a initial coding
carl
parents: 3
diff changeset
324 if (fstat(fd, &openfdstat)) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
325 close();
2737ab01659a initial coding
carl
parents: 3
diff changeset
326 snprintf(buf, sizeof(buf), "syslog file %s cannot stat after open", file_name);
2737ab01659a initial coding
carl
parents: 3
diff changeset
327 tokp->token_error(buf);
2737ab01659a initial coding
carl
parents: 3
diff changeset
328 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
329 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
330 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
331
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
332
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
333 bool SYSLOGCONFIG::read(CONFIG &con) {
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
334 if (failed()) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
335 open(false);
2737ab01659a initial coding
carl
parents: 3
diff changeset
336 if (failed()) return false;
2737ab01659a initial coding
carl
parents: 3
diff changeset
337 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
338 int n = ::read(fd, buf+len, buflen-len);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
339 bool have = (n > 0);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
340 if (have) {
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
341 len += n;
6e88da080f08 initial coding
carl
parents: 1
diff changeset
342 while (true) {
6e88da080f08 initial coding
carl
parents: 1
diff changeset
343 char *p = (char*)memchr(buf, '\n', len);
6e88da080f08 initial coding
carl
parents: 1
diff changeset
344 if (!p) break;
6e88da080f08 initial coding
carl
parents: 1
diff changeset
345 n = p-buf;
6e88da080f08 initial coding
carl
parents: 1
diff changeset
346 *p = '\0';
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
347 process(con); // process null terminated string
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
348 len -= n+1;
6e88da080f08 initial coding
carl
parents: 1
diff changeset
349 memmove(buf, p+1, len);
6e88da080f08 initial coding
carl
parents: 1
diff changeset
350 }
6e88da080f08 initial coding
carl
parents: 1
diff changeset
351 // no <lf> in a full buffer
6e88da080f08 initial coding
carl
parents: 1
diff changeset
352 if (len == buflen) len = 0;
6e88da080f08 initial coding
carl
parents: 1
diff changeset
353 }
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
354 else {
2737ab01659a initial coding
carl
parents: 3
diff changeset
355 // check for file close
2737ab01659a initial coding
carl
parents: 3
diff changeset
356 struct stat filenamest;
2737ab01659a initial coding
carl
parents: 3
diff changeset
357 if (0 == stat(file_name, &filenamest)) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
358 if ((filenamest.st_dev != openfdstat.st_dev) ||
2737ab01659a initial coding
carl
parents: 3
diff changeset
359 (filenamest.st_ino != openfdstat.st_ino)) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
360 close();
2737ab01659a initial coding
carl
parents: 3
diff changeset
361 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
362 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
363 else {
2737ab01659a initial coding
carl
parents: 3
diff changeset
364 // filename no longer exists
2737ab01659a initial coding
carl
parents: 3
diff changeset
365 close();
2737ab01659a initial coding
carl
parents: 3
diff changeset
366 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
367 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
368 return have;
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
369 }
6e88da080f08 initial coding
carl
parents: 1
diff changeset
370
6e88da080f08 initial coding
carl
parents: 1
diff changeset
371
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
372 void SYSLOGCONFIG::close() {
5
276c4edc8521 initial coding
carl
parents: 4
diff changeset
373 if (debug_syslog > 1) {
276c4edc8521 initial coding
carl
parents: 4
diff changeset
374 snprintf(buf, sizeof(buf), "syslog file %s closed", file_name);
276c4edc8521 initial coding
carl
parents: 4
diff changeset
375 my_syslog(buf);
276c4edc8521 initial coding
carl
parents: 4
diff changeset
376 }
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
377 if (fd != -1) ::close(fd);
2737ab01659a initial coding
carl
parents: 3
diff changeset
378 fd = -1;
2737ab01659a initial coding
carl
parents: 3
diff changeset
379 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
380
2737ab01659a initial coding
carl
parents: 3
diff changeset
381
2737ab01659a initial coding
carl
parents: 3
diff changeset
382 void SYSLOGCONFIG::add_pattern(PATTERNP pat) {
2737ab01659a initial coding
carl
parents: 3
diff changeset
383 patterns.push_back(pat);
2737ab01659a initial coding
carl
parents: 3
diff changeset
384 }
2737ab01659a initial coding
carl
parents: 3
diff changeset
385
2737ab01659a initial coding
carl
parents: 3
diff changeset
386
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
387 void SYSLOGCONFIG::process(CONFIG &con) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
388 for (pattern_list::iterator i=patterns.begin(); i!=patterns.end(); i++) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
389 PATTERN *p = *i;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
390 if (p->process(buf, con)) break;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
391 }
1
551433a01cab initial coding
carl
parents:
diff changeset
392 }
551433a01cab initial coding
carl
parents:
diff changeset
393
551433a01cab initial coding
carl
parents:
diff changeset
394
551433a01cab initial coding
carl
parents:
diff changeset
395 void SYSLOGCONFIG::dump(int level) {
551433a01cab initial coding
carl
parents:
diff changeset
396 char indent[maxlen];
551433a01cab initial coding
carl
parents:
diff changeset
397 int i = min(maxlen-1, level*4);
551433a01cab initial coding
carl
parents:
diff changeset
398 memset(indent, ' ', i);
551433a01cab initial coding
carl
parents:
diff changeset
399 indent[i] = '\0';
551433a01cab initial coding
carl
parents:
diff changeset
400 char buf[maxlen];
551433a01cab initial coding
carl
parents:
diff changeset
401 printf("%s file \"%s\" {\n", indent, file_name);
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
402 for (pattern_list::iterator i=patterns.begin(); i!=patterns.end(); i++) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
403 PATTERN *p = *i;
4
2737ab01659a initial coding
carl
parents: 3
diff changeset
404 p->dump(level+1);
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
405 }
1
551433a01cab initial coding
carl
parents:
diff changeset
406 printf("%s }; \n", indent);
551433a01cab initial coding
carl
parents:
diff changeset
407 }
551433a01cab initial coding
carl
parents:
diff changeset
408
551433a01cab initial coding
carl
parents:
diff changeset
409
551433a01cab initial coding
carl
parents:
diff changeset
410 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
411 // helper to discard the strings held by a string_set
551433a01cab initial coding
carl
parents:
diff changeset
412 //
551433a01cab initial coding
carl
parents:
diff changeset
413 void discard(string_set &s) {
551433a01cab initial coding
carl
parents:
diff changeset
414 for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
551433a01cab initial coding
carl
parents:
diff changeset
415 free(*i);
551433a01cab initial coding
carl
parents:
diff changeset
416 }
551433a01cab initial coding
carl
parents:
diff changeset
417 s.clear();
551433a01cab initial coding
carl
parents:
diff changeset
418 }
551433a01cab initial coding
carl
parents:
diff changeset
419
551433a01cab initial coding
carl
parents:
diff changeset
420
551433a01cab initial coding
carl
parents:
diff changeset
421 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
422 // helper to register a string in a string set
551433a01cab initial coding
carl
parents:
diff changeset
423 //
551433a01cab initial coding
carl
parents:
diff changeset
424 char* register_string(string_set &s, char *name) {
551433a01cab initial coding
carl
parents:
diff changeset
425 string_set::iterator i = s.find(name);
551433a01cab initial coding
carl
parents:
diff changeset
426 if (i != s.end()) return *i;
551433a01cab initial coding
carl
parents:
diff changeset
427 char *x = strdup(name);
551433a01cab initial coding
carl
parents:
diff changeset
428 s.insert(x);
551433a01cab initial coding
carl
parents:
diff changeset
429 return x;
551433a01cab initial coding
carl
parents:
diff changeset
430 }
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 // register a global string
551433a01cab initial coding
carl
parents:
diff changeset
435 //
551433a01cab initial coding
carl
parents:
diff changeset
436 char* register_string(char *name) {
551433a01cab initial coding
carl
parents:
diff changeset
437 return register_string(all_strings, name);
551433a01cab initial coding
carl
parents:
diff changeset
438 }
551433a01cab initial coding
carl
parents:
diff changeset
439
551433a01cab initial coding
carl
parents:
diff changeset
440
551433a01cab initial coding
carl
parents:
diff changeset
441 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
442 //
551433a01cab initial coding
carl
parents:
diff changeset
443 bool tsa(TOKEN &tok, char *token);
551433a01cab initial coding
carl
parents:
diff changeset
444 bool tsa(TOKEN &tok, char *token) {
551433a01cab initial coding
carl
parents:
diff changeset
445 char *have = tok.next();
551433a01cab initial coding
carl
parents:
diff changeset
446 if (have == token) return true;
551433a01cab initial coding
carl
parents:
diff changeset
447 tok.token_error(token, have);
551433a01cab initial coding
carl
parents:
diff changeset
448 return false;
551433a01cab initial coding
carl
parents:
diff changeset
449 }
551433a01cab initial coding
carl
parents:
diff changeset
450
551433a01cab initial coding
carl
parents:
diff changeset
451
551433a01cab initial coding
carl
parents:
diff changeset
452 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
453 //
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
454 bool parse_pattern(TOKEN &tok, SYSLOGCONFIG &con);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
455 bool parse_pattern(TOKEN &tok, SYSLOGCONFIG &con) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
456 char *pat = tok.next();
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
457 int ind, buc;
1
551433a01cab initial coding
carl
parents:
diff changeset
458 if (!tsa(tok, token_lbrace)) return false;
551433a01cab initial coding
carl
parents:
diff changeset
459 while (true) {
551433a01cab initial coding
carl
parents:
diff changeset
460 char *have = tok.next();
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
461 if (!have) break;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
462 if (have == token_rbrace) break;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
463 if (have == token_index) {
1
551433a01cab initial coding
carl
parents:
diff changeset
464 have = tok.next();
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
465 ind = atoi(have);
1
551433a01cab initial coding
carl
parents:
diff changeset
466 if (!tsa(tok, token_semi)) return false;
551433a01cab initial coding
carl
parents:
diff changeset
467 }
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
468 else if (have == token_bucket) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
469 have = tok.next();
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
470 buc = atoi(have);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
471 if (!tsa(tok, token_semi)) return false;
1
551433a01cab initial coding
carl
parents:
diff changeset
472 }
551433a01cab initial coding
carl
parents:
diff changeset
473 else {
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
474 tok.token_error("index/bucket", have);
1
551433a01cab initial coding
carl
parents:
diff changeset
475 return false;
551433a01cab initial coding
carl
parents:
diff changeset
476 }
551433a01cab initial coding
carl
parents:
diff changeset
477 }
551433a01cab initial coding
carl
parents:
diff changeset
478 if (!tsa(tok, token_semi)) return false;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
479 PATTERNP patt = new PATTERN(tok, pat, ind, buc);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
480 con.add_pattern(patt);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
481 return true;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
482 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
483
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
484
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
485 ////////////////////////////////////////////////
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
486 //
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
487 bool parse_ignore(TOKEN &tok, CONFIG &dc);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
488 bool parse_ignore(TOKEN &tok, CONFIG &dc) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
489 if (!tsa(tok, token_lbrace)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
490 while (true) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
491 char *have = tok.next();
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
492 if (!have) break;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
493 if (have == token_rbrace) break;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
494 int ipaddr = ip_address(have);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
495 if (ipaddr == 0) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
496 tok.token_error("ip address", have);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
497 return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
498 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
499 if (!tsa(tok, token_slash)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
500 have = tok.next();
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
501 int mask = atoi(have);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
502 if ((mask < 8) || (mask > 32)) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
503 tok.token_error("cidr 8..32 value", have);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
504 return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
505 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
506 if (!tsa(tok, token_semi)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
507 IPPAIR pair;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
508 const int masks[33] = {0xffffffff, // 0
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
509 0x7fffffff, // 1
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
510 0x3fffffff, // 2
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
511 0x1fffffff, // 3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
512 0x0fffffff, // 4
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
513 0x07ffffff, // 5
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
514 0x03ffffff, // 6
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
515 0x01ffffff, // 7
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
516 0x00ffffff, // 8
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
517 0x007fffff, // 9
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
518 0x003fffff, // 10
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
519 0x001fffff, // 11
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
520 0x000fffff, // 12
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
521 0x0007ffff, // 13
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
522 0x0003ffff, // 14
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
523 0x0001ffff, // 15
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
524 0x0000ffff, // 16
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
525 0x00007fff, // 17
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
526 0x00003fff, // 18
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
527 0x00001fff, // 19
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
528 0x00000fff, // 20
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
529 0x000007ff, // 21
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
530 0x000003ff, // 22
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
531 0x000001ff, // 23
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
532 0x000000ff, // 24
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
533 0x0000007f, // 25
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
534 0x0000003f, // 26
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
535 0x0000001f, // 27
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
536 0x0000000f, // 28
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
537 0x00000007, // 29
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
538 0x00000003, // 30
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
539 0x00000001, // 31
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
540 0x00000000}; // 32
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
541 pair.first = ipaddr;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
542 pair.last = ipaddr | masks[mask];
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
543 pair.cidr = mask;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
544 dc.add_pair(pair);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
545 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
546 if (!tsa(tok, token_semi)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
547 return true;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
548 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
549
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
550
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
551 ////////////////////////////////////////////////
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
552 //
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
553 bool parse_syslogconfig(TOKEN &tok, CONFIG &dc);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
554 bool parse_syslogconfig(TOKEN &tok, CONFIG &dc) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
555 char *name = tok.next();
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
556 if (!tsa(tok, token_lbrace)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
557 SYSLOGCONFIGP con = new SYSLOGCONFIG(tok, name);
2
6e88da080f08 initial coding
carl
parents: 1
diff changeset
558 if (con->failed()) {
6e88da080f08 initial coding
carl
parents: 1
diff changeset
559 delete con;
6e88da080f08 initial coding
carl
parents: 1
diff changeset
560 return false;
6e88da080f08 initial coding
carl
parents: 1
diff changeset
561 }
1
551433a01cab initial coding
carl
parents:
diff changeset
562 dc.add_syslogconfig(con);
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
563 while (true) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
564 char *have = tok.next();
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
565 if (!have) break;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
566 if (have == token_rbrace) break;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
567 if (have == token_pattern) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
568 if (!parse_pattern(tok, *con)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
569 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
570 else {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
571 tok.token_error("pattern", have);
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
572 return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
573 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
574 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
575 if (!tsa(tok, token_semi)) return false;
1
551433a01cab initial coding
carl
parents:
diff changeset
576 return true;
551433a01cab initial coding
carl
parents:
diff changeset
577 }
551433a01cab initial coding
carl
parents:
diff changeset
578
551433a01cab initial coding
carl
parents:
diff changeset
579
551433a01cab initial coding
carl
parents:
diff changeset
580 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
581 // parse a config file
551433a01cab initial coding
carl
parents:
diff changeset
582 //
551433a01cab initial coding
carl
parents:
diff changeset
583 bool load_conf(CONFIG &dc, char *fn) {
551433a01cab initial coding
carl
parents:
diff changeset
584 int count = 0;
551433a01cab initial coding
carl
parents:
diff changeset
585 TOKEN tok(fn, &dc.config_files);
551433a01cab initial coding
carl
parents:
diff changeset
586 while (true) {
551433a01cab initial coding
carl
parents:
diff changeset
587 char *have = tok.next();
551433a01cab initial coding
carl
parents:
diff changeset
588 if (!have) break;
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
589 if (have == token_threshold) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
590 have = tok.next();
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
591 dc.set_threshold(atoi(have));
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
592 if (!tsa(tok, token_semi)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
593 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
594 else if (have == token_ignore) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
595 if (!parse_ignore(tok, dc)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
596 }
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
597 else if (have == token_file) {
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
598 if (!parse_syslogconfig(tok, dc)) return false;
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
599 count++;
1
551433a01cab initial coding
carl
parents:
diff changeset
600 }
551433a01cab initial coding
carl
parents:
diff changeset
601 else {
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
602 tok.token_error("threshold/ignore/file", have);
1
551433a01cab initial coding
carl
parents:
diff changeset
603 return false;
551433a01cab initial coding
carl
parents:
diff changeset
604 }
551433a01cab initial coding
carl
parents:
diff changeset
605 }
551433a01cab initial coding
carl
parents:
diff changeset
606 tok.token_error("load_conf() found %d syslog files in %s", count, fn);
551433a01cab initial coding
carl
parents:
diff changeset
607 return (!dc.syslogconfigs.empty());
551433a01cab initial coding
carl
parents:
diff changeset
608 }
551433a01cab initial coding
carl
parents:
diff changeset
609
551433a01cab initial coding
carl
parents:
diff changeset
610
551433a01cab initial coding
carl
parents:
diff changeset
611 ////////////////////////////////////////////////
551433a01cab initial coding
carl
parents:
diff changeset
612 // init the tokens
551433a01cab initial coding
carl
parents:
diff changeset
613 //
551433a01cab initial coding
carl
parents:
diff changeset
614 void token_init() {
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
615 token_bucket = register_string("bucket");
1
551433a01cab initial coding
carl
parents:
diff changeset
616 token_file = register_string("file");
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
617 token_ignore = register_string("ignore");
1
551433a01cab initial coding
carl
parents:
diff changeset
618 token_include = register_string("include");
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
619 token_index = register_string("index");
1
551433a01cab initial coding
carl
parents:
diff changeset
620 token_lbrace = register_string("{");
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
621 token_pattern = register_string("pattern");
1
551433a01cab initial coding
carl
parents:
diff changeset
622 token_rbrace = register_string("}");
551433a01cab initial coding
carl
parents:
diff changeset
623 token_semi = register_string(";");
3
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
624 token_slash = register_string("/");
8fe310e5cd44 initial coding
carl
parents: 2
diff changeset
625 token_threshold = register_string("threshold");
1
551433a01cab initial coding
carl
parents:
diff changeset
626 }