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