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