annotate src/syslogconfig.cpp @ 1:551433a01cab

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