1
|
1 /***************************************************************************
|
|
2 * Copyright (C) 2005 by 510 Software Group *
|
|
3 * *
|
|
4 * *
|
|
5 * This program is free software; you can redistribute it and/or modify *
|
|
6 * it under the terms of the GNU General Public License as published by *
|
|
7 * the Free Software Foundation; either version 2 of the License, or *
|
|
8 * (at your option) any later version. *
|
|
9 * *
|
|
10 * This program is distributed in the hope that it will be useful, *
|
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
13 * GNU General Public License for more details. *
|
|
14 * *
|
|
15 * You should have received a copy of the GNU General Public License *
|
|
16 * along with this program; if not, write to the *
|
|
17 * Free Software Foundation, Inc., *
|
|
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
19 ***************************************************************************/
|
|
20
|
|
21 #include "includes.h"
|
2
|
22 #include <fcntl.h>
|
1
|
23
|
|
24 static char* syslogconfig_version="$Id$";
|
|
25
|
|
26 char *token_cisco;
|
|
27 char *token_file;
|
|
28 char *token_include;
|
|
29 char *token_lbrace;
|
|
30 char *token_parser;
|
|
31 char *token_rbrace;
|
|
32 char *token_semi;
|
|
33 char *token_ssh;
|
|
34
|
|
35 string_set all_strings; // owns all the strings, only modified by the config loader thread
|
|
36 const int maxlen = 1000; // used for snprintf buffers
|
|
37
|
|
38
|
|
39 CONFIG::CONFIG() {
|
|
40 reference_count = 0;
|
|
41 generation = 0;
|
|
42 load_time = 0;
|
|
43 }
|
|
44
|
|
45
|
|
46 CONFIG::~CONFIG() {
|
|
47 for (syslogconfig_list::iterator i=syslogconfigs.begin(); i!=syslogconfigs.end(); i++) {
|
|
48 SYSLOGCONFIG *c = *i;
|
|
49 delete c;
|
|
50 }
|
|
51 }
|
|
52
|
|
53
|
|
54 void CONFIG::add_syslogconfig(SYSLOGCONFIGP con) {
|
|
55 syslogconfigs.push_back(con);
|
|
56 }
|
|
57
|
|
58
|
|
59 void CONFIG::dump() {
|
|
60 for (syslogconfig_list::iterator i=syslogconfigs.begin(); i!=syslogconfigs.end(); i++) {
|
|
61 SYSLOGCONFIGP c = *i;
|
|
62 c->dump(0);
|
|
63 }
|
|
64 }
|
|
65
|
|
66
|
2
|
67 void CONFIG::read() {
|
|
68 for (syslogconfig_list::iterator i=syslogconfigs.begin(); i!=syslogconfigs.end(); i++) {
|
|
69 SYSLOGCONFIGP c = *i;
|
|
70 c->read();
|
|
71 }
|
|
72 }
|
|
73
|
|
74
|
|
75 SYSLOGCONFIG::SYSLOGCONFIG(TOKEN &tok, char *file_name_, parser_style parser_) {
|
|
76 file_name = file_name_;
|
|
77 parser = parser_;
|
|
78 fd = open(file_name, O_RDONLY);
|
|
79 len = 0;
|
|
80 if (fd == -1) {
|
|
81 char buf[maxlen];
|
|
82 snprintf(buf, sizeof(buf), "syslog file %s not readable", file_name);
|
|
83 tok.token_error(buf);
|
|
84 }
|
|
85 else {
|
|
86 lseek(fd, 0, SEEK_END);
|
|
87 }
|
1
|
88 }
|
|
89
|
|
90
|
|
91 SYSLOGCONFIG::~SYSLOGCONFIG() {
|
2
|
92 if (fd != -1) close(fd);
|
|
93 fd = -1;
|
|
94 }
|
|
95
|
|
96
|
|
97 void SYSLOGCONFIG::read() {
|
|
98 if (failed()) return;
|
|
99 int n = ::read(fd, buf, buflen-len);
|
|
100 if (n > 0) {
|
|
101 len += n;
|
|
102 while (true) {
|
|
103 char *p = (char*)memchr(buf, '\n', len);
|
|
104 if (!p) break;
|
|
105 n = p-buf;
|
|
106 *p = '\0';
|
|
107 process(); // process null terminated string
|
|
108 len -= n+1;
|
|
109 memmove(buf, p+1, len);
|
|
110 }
|
|
111 // no <lf> in a full buffer
|
|
112 if (len == buflen) len = 0;
|
|
113 }
|
|
114 }
|
|
115
|
|
116
|
|
117 void SYSLOGCONFIG::process() {
|
|
118 my_syslog(buf);
|
1
|
119 }
|
|
120
|
|
121
|
|
122 void SYSLOGCONFIG::dump(int level) {
|
|
123 char indent[maxlen];
|
|
124 int i = min(maxlen-1, level*4);
|
|
125 memset(indent, ' ', i);
|
|
126 indent[i] = '\0';
|
|
127 char buf[maxlen];
|
|
128 printf("%s file \"%s\" {\n", indent, file_name);
|
|
129 switch (parser) {
|
|
130 case cisco:
|
|
131 printf("%s parser cisco; \n", indent);
|
|
132 break;
|
|
133 case ssh:
|
|
134 printf("%s parser ssh; \n", indent);
|
|
135 break;
|
|
136 }
|
|
137 printf("%s }; \n", indent);
|
|
138 }
|
|
139
|
|
140
|
|
141 ////////////////////////////////////////////////
|
|
142 // helper to discard the strings held by a string_set
|
|
143 //
|
|
144 void discard(string_set &s) {
|
|
145 for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
|
|
146 free(*i);
|
|
147 }
|
|
148 s.clear();
|
|
149 }
|
|
150
|
|
151
|
|
152 ////////////////////////////////////////////////
|
|
153 // helper to register a string in a string set
|
|
154 //
|
|
155 char* register_string(string_set &s, char *name) {
|
|
156 string_set::iterator i = s.find(name);
|
|
157 if (i != s.end()) return *i;
|
|
158 char *x = strdup(name);
|
|
159 s.insert(x);
|
|
160 return x;
|
|
161 }
|
|
162
|
|
163
|
|
164 ////////////////////////////////////////////////
|
|
165 // register a global string
|
|
166 //
|
|
167 char* register_string(char *name) {
|
|
168 return register_string(all_strings, name);
|
|
169 }
|
|
170
|
|
171
|
|
172 ////////////////////////////////////////////////
|
|
173 //
|
|
174 bool tsa(TOKEN &tok, char *token);
|
|
175 bool tsa(TOKEN &tok, char *token) {
|
|
176 char *have = tok.next();
|
|
177 if (have == token) return true;
|
|
178 tok.token_error(token, have);
|
|
179 return false;
|
|
180 }
|
|
181
|
|
182
|
|
183 ////////////////////////////////////////////////
|
|
184 //
|
|
185 bool parse_syslogconfig(TOKEN &tok, CONFIG &dc);
|
|
186 bool parse_syslogconfig(TOKEN &tok, CONFIG &dc) {
|
|
187 char *name = tok.next();
|
|
188 parser_style parser;
|
|
189 if (!tsa(tok, token_lbrace)) return false;
|
|
190 while (true) {
|
|
191 char *have = tok.next();
|
|
192 if (have == token_parser) {
|
|
193 have = tok.next();
|
|
194 if (have == token_cisco) parser = cisco;
|
|
195 else if (have == token_ssh) parser = ssh;
|
|
196 else {
|
|
197 tok.token_error("cisco/ssh", have);
|
|
198 return false;
|
|
199 }
|
|
200 if (!tsa(tok, token_semi)) return false;
|
|
201 }
|
|
202 else if (have == token_rbrace) {
|
|
203 break; // done
|
|
204 }
|
|
205 else {
|
|
206 tok.token_error("file keyword", have);
|
|
207 return false;
|
|
208 }
|
|
209 }
|
|
210 if (!tsa(tok, token_semi)) return false;
|
2
|
211 SYSLOGCONFIGP con = new SYSLOGCONFIG(tok, name, parser);
|
|
212 if (con->failed()) {
|
|
213 delete con;
|
|
214 return false;
|
|
215 }
|
1
|
216 dc.add_syslogconfig(con);
|
|
217 return true;
|
|
218 }
|
|
219
|
|
220
|
|
221 ////////////////////////////////////////////////
|
|
222 // parse a config file
|
|
223 //
|
|
224 bool load_conf(CONFIG &dc, char *fn) {
|
|
225 int count = 0;
|
|
226 TOKEN tok(fn, &dc.config_files);
|
|
227 while (true) {
|
|
228 char *have = tok.next();
|
|
229 if (!have) break;
|
|
230 if (have == token_file) {
|
|
231 if (!parse_syslogconfig(tok, dc)) {
|
|
232 tok.token_error("load_conf() failed to parse syslogconfig");
|
|
233 return false;
|
|
234 }
|
|
235 else count++;
|
|
236 }
|
|
237 else {
|
|
238 tok.token_error(token_file, have);
|
|
239 return false;
|
|
240 }
|
|
241 }
|
|
242 tok.token_error("load_conf() found %d syslog files in %s", count, fn);
|
|
243 return (!dc.syslogconfigs.empty());
|
|
244 }
|
|
245
|
|
246
|
|
247 ////////////////////////////////////////////////
|
|
248 // init the tokens
|
|
249 //
|
|
250 void token_init() {
|
|
251 token_cisco = register_string("cisco");
|
|
252 token_file = register_string("file");
|
|
253 token_include = register_string("include");
|
|
254 token_lbrace = register_string("{");
|
|
255 token_parser = register_string("parser");
|
|
256 token_rbrace = register_string("}");
|
|
257 token_semi = register_string(";");
|
|
258 token_ssh = register_string("ssh");
|
|
259 }
|