annotate src/context.cpp @ 5:b8a3b366635a

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