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"
|
|
22
|
|
23 static char* syslogconfig_version="$Id$";
|
|
24
|
|
25 char *token_cisco;
|
|
26 char *token_file;
|
|
27 char *token_include;
|
|
28 char *token_lbrace;
|
|
29 char *token_parser;
|
|
30 char *token_rbrace;
|
|
31 char *token_semi;
|
|
32 char *token_ssh;
|
|
33
|
|
34 string_set all_strings; // owns all the strings, only modified by the config loader thread
|
|
35 const int maxlen = 1000; // used for snprintf buffers
|
|
36
|
|
37
|
|
38 CONFIG::CONFIG() {
|
|
39 reference_count = 0;
|
|
40 generation = 0;
|
|
41 load_time = 0;
|
|
42 }
|
|
43
|
|
44
|
|
45 CONFIG::~CONFIG() {
|
|
46 for (syslogconfig_list::iterator i=syslogconfigs.begin(); i!=syslogconfigs.end(); i++) {
|
|
47 SYSLOGCONFIG *c = *i;
|
|
48 delete c;
|
|
49 }
|
|
50 }
|
|
51
|
|
52
|
|
53 void CONFIG::add_syslogconfig(SYSLOGCONFIGP con) {
|
|
54 syslogconfigs.push_back(con);
|
|
55 }
|
|
56
|
|
57
|
|
58 void CONFIG::dump() {
|
|
59 for (syslogconfig_list::iterator i=syslogconfigs.begin(); i!=syslogconfigs.end(); i++) {
|
|
60 SYSLOGCONFIGP c = *i;
|
|
61 c->dump(0);
|
|
62 }
|
|
63 }
|
|
64
|
|
65
|
|
66 SYSLOGCONFIG::SYSLOGCONFIG(char *file_name_, parser_style parser_) {
|
|
67 file_name = file_name_;
|
|
68 parser = parser_;
|
|
69 }
|
|
70
|
|
71
|
|
72 SYSLOGCONFIG::~SYSLOGCONFIG() {
|
|
73 }
|
|
74
|
|
75
|
|
76 void SYSLOGCONFIG::dump(int level) {
|
|
77 char indent[maxlen];
|
|
78 int i = min(maxlen-1, level*4);
|
|
79 memset(indent, ' ', i);
|
|
80 indent[i] = '\0';
|
|
81 char buf[maxlen];
|
|
82 printf("%s file \"%s\" {\n", indent, file_name);
|
|
83 switch (parser) {
|
|
84 case cisco:
|
|
85 printf("%s parser cisco; \n", indent);
|
|
86 break;
|
|
87 case ssh:
|
|
88 printf("%s parser ssh; \n", indent);
|
|
89 break;
|
|
90 }
|
|
91 printf("%s }; \n", indent);
|
|
92 }
|
|
93
|
|
94
|
|
95 ////////////////////////////////////////////////
|
|
96 // helper to discard the strings held by a string_set
|
|
97 //
|
|
98 void discard(string_set &s) {
|
|
99 for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
|
|
100 free(*i);
|
|
101 }
|
|
102 s.clear();
|
|
103 }
|
|
104
|
|
105
|
|
106 ////////////////////////////////////////////////
|
|
107 // helper to register a string in a string set
|
|
108 //
|
|
109 char* register_string(string_set &s, char *name) {
|
|
110 string_set::iterator i = s.find(name);
|
|
111 if (i != s.end()) return *i;
|
|
112 char *x = strdup(name);
|
|
113 s.insert(x);
|
|
114 return x;
|
|
115 }
|
|
116
|
|
117
|
|
118 ////////////////////////////////////////////////
|
|
119 // register a global string
|
|
120 //
|
|
121 char* register_string(char *name) {
|
|
122 return register_string(all_strings, name);
|
|
123 }
|
|
124
|
|
125
|
|
126 ////////////////////////////////////////////////
|
|
127 //
|
|
128 bool tsa(TOKEN &tok, char *token);
|
|
129 bool tsa(TOKEN &tok, char *token) {
|
|
130 char *have = tok.next();
|
|
131 if (have == token) return true;
|
|
132 tok.token_error(token, have);
|
|
133 return false;
|
|
134 }
|
|
135
|
|
136
|
|
137 ////////////////////////////////////////////////
|
|
138 //
|
|
139 bool parse_syslogconfig(TOKEN &tok, CONFIG &dc);
|
|
140 bool parse_syslogconfig(TOKEN &tok, CONFIG &dc) {
|
|
141 char *name = tok.next();
|
|
142 parser_style parser;
|
|
143 if (!tsa(tok, token_lbrace)) return false;
|
|
144 while (true) {
|
|
145 char *have = tok.next();
|
|
146 if (have == token_parser) {
|
|
147 have = tok.next();
|
|
148 if (have == token_cisco) parser = cisco;
|
|
149 else if (have == token_ssh) parser = ssh;
|
|
150 else {
|
|
151 tok.token_error("cisco/ssh", have);
|
|
152 return false;
|
|
153 }
|
|
154 if (!tsa(tok, token_semi)) return false;
|
|
155 }
|
|
156 else if (have == token_rbrace) {
|
|
157 break; // done
|
|
158 }
|
|
159 else {
|
|
160 tok.token_error("file keyword", have);
|
|
161 return false;
|
|
162 }
|
|
163 }
|
|
164 if (!tsa(tok, token_semi)) return false;
|
|
165 SYSLOGCONFIGP con = new SYSLOGCONFIG(name, parser);
|
|
166 dc.add_syslogconfig(con);
|
|
167 return true;
|
|
168 }
|
|
169
|
|
170
|
|
171 ////////////////////////////////////////////////
|
|
172 // parse a config file
|
|
173 //
|
|
174 bool load_conf(CONFIG &dc, char *fn) {
|
|
175 int count = 0;
|
|
176 TOKEN tok(fn, &dc.config_files);
|
|
177 while (true) {
|
|
178 char *have = tok.next();
|
|
179 if (!have) break;
|
|
180 if (have == token_file) {
|
|
181 if (!parse_syslogconfig(tok, dc)) {
|
|
182 tok.token_error("load_conf() failed to parse syslogconfig");
|
|
183 return false;
|
|
184 }
|
|
185 else count++;
|
|
186 }
|
|
187 else {
|
|
188 tok.token_error(token_file, have);
|
|
189 return false;
|
|
190 }
|
|
191 }
|
|
192 tok.token_error("load_conf() found %d syslog files in %s", count, fn);
|
|
193 return (!dc.syslogconfigs.empty());
|
|
194 }
|
|
195
|
|
196
|
|
197 ////////////////////////////////////////////////
|
|
198 // init the tokens
|
|
199 //
|
|
200 void token_init() {
|
|
201 token_cisco = register_string("cisco");
|
|
202 token_file = register_string("file");
|
|
203 token_include = register_string("include");
|
|
204 token_lbrace = register_string("{");
|
|
205 token_parser = register_string("parser");
|
|
206 token_rbrace = register_string("}");
|
|
207 token_semi = register_string(";");
|
|
208 token_ssh = register_string("ssh");
|
|
209 }
|