0
|
1 /*
|
|
2
|
13
|
3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under
|
|
4 the GPL version 3 or any later version at your choice available at
|
|
5 http://www.gnu.org/licenses/gpl-3.0.txt
|
0
|
6
|
|
7 */
|
|
8
|
|
9 #include "includes.h"
|
|
10
|
|
11 static char* context_version="$Id$";
|
|
12
|
|
13 char *token_envfrom;
|
3
|
14 char *token_include;
|
0
|
15 char *token_lbrace;
|
|
16 char *token_rbrace;
|
|
17 char *token_rcptto;
|
13
|
18 char *token_remove;
|
0
|
19 char *token_semi;
|
|
20
|
|
21 string_set all_strings; // owns all the strings, only modified by the config loader thread
|
|
22 const int maxlen = 1000; // used for snprintf buffers
|
|
23
|
|
24 CONFIG::CONFIG() {
|
|
25 reference_count = 0;
|
|
26 generation = 0;
|
|
27 load_time = 0;
|
|
28 }
|
|
29
|
|
30
|
|
31 CONFIG::~CONFIG() {
|
|
32 }
|
|
33
|
|
34
|
13
|
35 bool CONFIG::find(char *needle, string_set &haystack) {
|
|
36 string_set::iterator i = haystack.find(needle);
|
|
37 if (i != haystack.end()) return true; // found user@domain.tld key
|
0
|
38 char *x = strchr(needle, '@');
|
|
39 if (x) {
|
|
40 x++;
|
|
41 i = haystack.find(x);
|
13
|
42 if (i != haystack.end()) return true; // found domain.tld key
|
0
|
43 char y = *x;
|
|
44 *x = '\0';
|
|
45 i = haystack.find(needle);
|
|
46 *x = y;
|
13
|
47 if (i != haystack.end()) return true; // found user@ key
|
|
48 }
|
|
49 return false;
|
|
50 }
|
|
51
|
|
52
|
|
53 char *CONFIG::find(char *needle, string_map &haystack) {
|
|
54 string_map::iterator i = haystack.find(needle);
|
|
55 if (i != haystack.end()) return (*i).second; // found user@domain.tld key
|
|
56 char *x = strchr(needle, '@');
|
|
57 if (x) {
|
|
58 x++;
|
|
59 i = haystack.find(x);
|
|
60 if (i != haystack.end()) return (*i).second; // found domain.tld key
|
|
61 char y = *x;
|
|
62 *x = '\0';
|
|
63 i = haystack.find(needle);
|
|
64 *x = y;
|
|
65 if (i != haystack.end()) return (*i).second; // found user@ key
|
0
|
66 }
|
|
67 return NULL;
|
|
68 }
|
|
69
|
|
70
|
|
71 void CONFIG::dump() {
|
|
72 printf("rcpt_to {\n");
|
|
73 for (string_map::iterator i=rcpt_to.begin(); i!=rcpt_to.end(); i++) {
|
|
74 char *to = (*i).first;
|
|
75 char *target = (*i).second;
|
13
|
76 if (!target) target = "\"\"";
|
|
77 bool rem = find_remove(to);
|
|
78 printf(" %s \t %s%s;\n", to, target, (rem) ? " remove" : "");
|
0
|
79 }
|
|
80 printf("};\n");
|
|
81 printf("env_from {\n");
|
|
82 for (string_map::iterator i=env_from.begin(); i!=env_from.end(); i++) {
|
|
83 char *from = (*i).first;
|
|
84 char *target = (*i).second;
|
13
|
85 if (!target) target = "\"\"";
|
|
86 printf(" %s \t %s;\n", from, target);
|
0
|
87 }
|
|
88 printf("};\n");
|
|
89 }
|
|
90
|
|
91
|
|
92 ////////////////////////////////////////////////
|
|
93 // helper to discard the strings held by a string_set
|
|
94 //
|
|
95 void discard(string_set &s) {
|
|
96 for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
|
|
97 free(*i);
|
|
98 }
|
|
99 s.clear();
|
|
100 }
|
|
101
|
|
102
|
|
103 ////////////////////////////////////////////////
|
|
104 // helper to register a string in a string set
|
|
105 //
|
|
106 char* register_string(string_set &s, char *name) {
|
|
107 string_set::iterator i = s.find(name);
|
|
108 if (i != s.end()) return *i;
|
|
109 char *x = strdup(name);
|
|
110 s.insert(x);
|
|
111 return x;
|
|
112 }
|
|
113
|
|
114
|
|
115 ////////////////////////////////////////////////
|
|
116 // register a global string
|
|
117 //
|
|
118 char* register_string(char *name) {
|
|
119 return register_string(all_strings, name);
|
|
120 }
|
|
121
|
|
122
|
|
123 ////////////////////////////////////////////////
|
13
|
124 // clear all global strings, helper for valgrind checking
|
|
125 //
|
|
126 void clear_strings() {
|
|
127 discard(all_strings);
|
|
128 }
|
|
129
|
|
130
|
|
131 ////////////////////////////////////////////////
|
0
|
132 //
|
|
133 bool tsa(TOKEN &tok, char *token);
|
|
134 bool tsa(TOKEN &tok, char *token) {
|
|
135 char *have = tok.next();
|
|
136 if (have == token) return true;
|
|
137 tok.token_error(token, have);
|
|
138 return false;
|
|
139 }
|
|
140
|
|
141
|
|
142 ////////////////////////////////////////////////
|
|
143 //
|
|
144 bool parse_rcpt_to(TOKEN &tok, CONFIG &dc);
|
|
145 bool parse_rcpt_to(TOKEN &tok, CONFIG &dc) {
|
|
146 if (!tsa(tok, token_lbrace)) return false;
|
|
147 while (true) {
|
|
148 char *have = tok.next();
|
|
149 if (!have) break;
|
|
150 if (have == token_rbrace) break;
|
13
|
151 char *target = tok.next();
|
|
152 dc.add_to(have, target);
|
|
153 target = tok.next();
|
|
154 if (target == token_remove) {
|
|
155 dc.add_remove(have);
|
|
156 target = tok.next();
|
0
|
157 }
|
13
|
158 if (target != token_semi) {
|
|
159 tok.token_error(token_semi, target);
|
|
160 break;
|
0
|
161 }
|
|
162 }
|
|
163 return tsa(tok, token_semi);
|
|
164 }
|
|
165
|
|
166
|
|
167 ////////////////////////////////////////////////
|
|
168 //
|
|
169 bool parse_env_from(TOKEN &tok, CONFIG &dc);
|
|
170 bool parse_env_from(TOKEN &tok, CONFIG &dc) {
|
|
171 if (!tsa(tok, token_lbrace)) return false;
|
|
172 while (true) {
|
|
173 char *have = tok.next();
|
|
174 if (!have) break;
|
|
175 if (have == token_rbrace) break;
|
|
176 if (have == token_semi) {
|
|
177 // optional separators
|
|
178 }
|
|
179 else {
|
|
180 char *target = tok.next();
|
|
181 dc.add_from(have, target);
|
|
182 }
|
|
183 }
|
|
184 return tsa(tok, token_semi);
|
|
185 }
|
|
186
|
|
187
|
|
188 ////////////////////////////////////////////////
|
|
189 // parse a config file
|
|
190 //
|
|
191 bool load_conf(CONFIG &dc, char *fn) {
|
|
192 TOKEN tok(fn, &dc.config_files);
|
|
193 while (true) {
|
|
194 char *have = tok.next();
|
|
195 if (!have) break;
|
|
196 if (have == token_envfrom) {
|
|
197 if (!parse_env_from(tok, dc)) {
|
|
198 tok.token_error("load_conf() failed to parse env_from");
|
|
199 return false;
|
|
200 }
|
|
201 }
|
|
202 else if (have == token_rcptto) {
|
|
203 if (!parse_rcpt_to(tok, dc)) {
|
|
204 tok.token_error("load_conf() failed to parse rcpt_to");
|
|
205 return false;
|
|
206 }
|
|
207 }
|
|
208 else {
|
3
|
209 tok.token_error("env_from/rcpt_to", have);
|
0
|
210 return false;
|
|
211 }
|
|
212 }
|
|
213 return true;
|
|
214 }
|
|
215
|
|
216
|
|
217 ////////////////////////////////////////////////
|
|
218 // init the tokens
|
|
219 //
|
|
220 void token_init() {
|
|
221 token_envfrom = register_string("env_from");
|
3
|
222 token_include = register_string("include");
|
0
|
223 token_lbrace = register_string("{");
|
|
224 token_rbrace = register_string("}");
|
|
225 token_rcptto = register_string("rcpt_to");
|
13
|
226 token_remove = register_string("remove");
|
0
|
227 token_semi = register_string(";");
|
|
228 }
|