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