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