annotate src/context.cpp @ 100:63e8633abc34 stable-5-8

allow empty env_to at global context to remove all restrictions on child contexts
author carl
date Sun, 25 Sep 2005 08:38:29 -0700
parents f8963ddf7143
children 6ac7ba4a8312
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
94
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1 /*
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
2
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
3 Copyright (c) 2004 Carl Byington - 510 Software Group, released under
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
4 the GPL version 2 or any later version at your choice available at
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
5 http://www.fsf.org/licenses/gpl.txt
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
6
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
7 */
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
8
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
9 #include "includes.h"
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
10
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
11 // needed for socket io
96
1edd4e8d3a60 fix missing include, not all systems define HOST_NAME_MAX
carl
parents: 94
diff changeset
12 #include <unistd.h>
94
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
13 #include <sys/ioctl.h>
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
14 #include <net/if.h>
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
15 #include <arpa/inet.h>
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
16 #include <netinet/in.h>
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
17 #include <netinet/tcp.h>
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
18 #include <netdb.h>
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
19 #include <sys/socket.h>
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
20 #include <sys/un.h>
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
21
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
22 static char* context_version="$Id$";
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
23
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
24 char *token_black;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
25 char *token_content;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
26 char *token_context;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
27 char *token_dccfrom;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
28 char *token_dccto;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
29 char *token_default;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
30 char *token_dnsbl;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
31 char *token_dnsbll;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
32 char *token_envfrom;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
33 char *token_envto;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
34 char *token_filter;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
35 char *token_host_limit;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
36 char *token_html_limit;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
37 char *token_html_tags;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
38 char *token_ignore;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
39 char *token_include;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
40 char *token_inherit;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
41 char *token_lbrace;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
42 char *token_mailhost;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
43 char *token_many;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
44 char *token_off;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
45 char *token_ok2;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
46 char *token_ok;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
47 char *token_on;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
48 char *token_rbrace;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
49 char *token_semi;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
50 char *token_soft;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
51 char *token_substitute;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
52 char *token_tld;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
53 char *token_unknown;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
54 char *token_verify;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
55 char *token_white;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
56
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
57 char *token_myhostname;
96
1edd4e8d3a60 fix missing include, not all systems define HOST_NAME_MAX
carl
parents: 94
diff changeset
58 #ifndef HOST_NAME_MAX
1edd4e8d3a60 fix missing include, not all systems define HOST_NAME_MAX
carl
parents: 94
diff changeset
59 #define HOST_NAME_MAX 255
1edd4e8d3a60 fix missing include, not all systems define HOST_NAME_MAX
carl
parents: 94
diff changeset
60 #endif
94
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
61 char myhostname[HOST_NAME_MAX+1];
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
62
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
63 verify_map verifiers;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
64 string_set all_strings; // owns all the strings, only modified by the config loader thread
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
65 const int maxlen = 1000; // used for snprintf buffers
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
66 const int maxage = 120; // smtp verify sockets older than this are ancient
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
67 extern int NULL_SOCKET;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
68 extern time_t ERROR_SOCKET_TIME; // number of seconds between attempts to open a socket an smtp host for address verification
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
69
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
70
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
71 int SMTP::writer() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
72 #ifdef VERIFY_DEBUG
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
73 log("writer() sees buffer with %s", buffer);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
74 log("writer() sees error %d", (int)error);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
75 #endif
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
76 int rs = 0;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
77 if (!error) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
78 int len = strlen(buffer);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
79 while (rs < len) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
80 int ws = write(fd, buffer+rs, len-rs);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
81 if (ws > 0) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
82 rs += ws;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
83 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
84 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
85 // peer closed the socket!
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
86 rs = 0;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
87 error = true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
88 break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
89 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
90 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
91 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
92 return rs;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
93 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
94
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
95
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
96 int SMTP::reader() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
97 // read some bytes terminated by lf or end of buffer.
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
98 // we may have a multi line response or part thereof in the buffer.
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
99 #ifdef VERIFY_DEBUG
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
100 log("reader() sees error %d", (int)error);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
101 #endif
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
102 if (error) return 0;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
103 int len = maxlen-1; // room for null terminator
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
104 while (pending < len) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
105 int ws = read(fd, buffer+pending, len-pending);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
106 if (ws > 0) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
107 pending += ws;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
108 if (buffer[pending-1] == '\n') break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
109 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
110 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
111 // peer closed the socket!
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
112 pending = 0;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
113 error = true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
114 break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
115 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
116 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
117 buffer[pending] = '\0';
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
118 #ifdef VERIFY_DEBUG
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
119 log("reader() sees buffer with %s", buffer);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
120 #endif
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
121 return pending;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
122 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
123
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
124
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
125 int SMTP::read_line() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
126 char *lf = strchr(buffer, '\n');
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
127 if (!lf) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
128 reader(); // get a lf
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
129 lf = strchr(buffer, '\n');
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
130 if (!lf) lf = buffer + pending - 1;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
131 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
132 return (lf-buffer)+1; // number of bytes in this line
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
133 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
134
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
135
97
cc3b79349c9c fix int function not returning value
carl
parents: 96
diff changeset
136 void SMTP::flush_line(int r) {
94
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
137 if (pending > r) memmove(buffer, buffer+r, pending-r);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
138 pending -= r;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
139 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
140
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
141
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
142 int SMTP::read_response() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
143 pending = 0;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
144 buffer[pending] = '\0';
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
145 while (true) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
146 int r = read_line();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
147 #ifdef VERIFY_DEBUG
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
148 log("read_response() sees line with %s", buffer);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
149 log("read_response() sees line length %d", r);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
150 #endif
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
151 if (r == 0) return 0; // failed to read any bytes
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
152 if ((r > 4) && (buffer[3] == '-')) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
153 flush_line(r);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
154 continue;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
155 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
156 return atoi(buffer);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
157 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
158 return 0;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
159 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
160
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
161
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
162 int SMTP::cmd(char *c) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
163 if (c) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
164 init();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
165 append(c);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
166 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
167 append("\r\n");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
168 writer();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
169 return read_response();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
170 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
171
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
172
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
173 int SMTP::helo() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
174 if (read_response() != 220) return 0;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
175 init();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
176 append("HELO ");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
177 append(token_myhostname);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
178 return cmd(NULL);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
179 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
180
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
181
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
182 int SMTP::rset() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
183 int rc = cmd("RSET");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
184 efrom[0] = '\0';
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
185 return rc;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
186 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
187
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
188
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
189 int SMTP::from(char *f) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
190 if (strncmp(efrom, f, maxlen)) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
191 rset();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
192 strncpy(efrom, f, maxlen);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
193 init();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
194 append("MAIL FROM:<");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
195 append(f);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
196 append(">");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
197 return cmd(NULL);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
198 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
199 return 250; // pretend it worked
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
200 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
201
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
202
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
203 int SMTP::rcpt(char *t) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
204 init();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
205 append("RCPT TO:<");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
206 append(t);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
207 append(">");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
208 return cmd(NULL);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
209 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
210
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
211
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
212 int SMTP::quit() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
213 return cmd("QUIT");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
214 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
215
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
216
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
217 void SMTP::closefd() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
218 shutdown(fd, SHUT_RDWR);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
219 close(fd);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
220 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
221
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
222
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
223 #ifdef VERIFY_DEBUG
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
224 void SMTP::log(char *m, int v) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
225 char buf[maxlen];
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
226 snprintf(buf, maxlen, m, v);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
227 my_syslog(buf);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
228 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
229
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
230
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
231 void SMTP::log(char *m, char *v) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
232 char buf[maxlen];
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
233 snprintf(buf, maxlen, m, v);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
234 my_syslog(buf);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
235 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
236 #endif
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
237
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
238
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
239 VERIFY::VERIFY(char *h) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
240 host = h;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
241 last_err = 0;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
242 pthread_mutex_init(&mutex, 0);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
243 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
244
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
245
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
246 void VERIFY::closer() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
247 bool ok = true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
248 while (ok) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
249 SMTP *conn = NULL;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
250 pthread_mutex_lock(&mutex);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
251 if (connections.empty()) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
252 ok = false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
253 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
254 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
255 conn = connections.front();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
256 time_t now = time(NULL);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
257 if ((now - conn->get_stamp()) > maxage) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
258 // this connection is ancient, remove it
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
259 connections.pop_front();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
260 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
261 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
262 ok = false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
263 conn = NULL;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
264 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
265 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
266 pthread_mutex_unlock(&mutex);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
267 // avoid doing this work inside the mutex lock
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
268 if (conn) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
269 #ifdef VERIFY_DEBUG
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
270 conn->log("closer() closes ancient %d", conn->get_fd());
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
271 #endif
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
272 delete conn;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
273 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
274 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
275 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
276
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
277
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
278 SMTP* VERIFY::get_connection() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
279 SMTP *conn = NULL;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
280 pthread_mutex_lock(&mutex);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
281 if (!connections.empty()) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
282 conn = connections.front();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
283 connections.pop_front();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
284 #ifdef VERIFY_DEBUG
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
285 conn->log("get_connection() %d from cache", conn->get_fd());
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
286 #endif
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
287 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
288 pthread_mutex_unlock(&mutex);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
289 if (conn) return conn;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
290 time_t now = time(NULL);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
291 int sock = NULL_SOCKET;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
292 if ((now - last_err) > ERROR_SOCKET_TIME) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
293 // nothing recent, maybe this time it will work
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
294 hostent *h = gethostbyname(host);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
295 if (h) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
296 sockaddr_in server;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
297 server.sin_family = h->h_addrtype;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
298 server.sin_port = htons(25);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
299 memcpy(&server.sin_addr, h->h_addr_list[0], h->h_length);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
300 sock = socket(PF_INET, SOCK_STREAM, 0);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
301 if (sock != NULL_SOCKET) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
302 bool rc = (connect(sock, (sockaddr *)&server, sizeof(server)) == 0);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
303 if (!rc) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
304 shutdown(sock, SHUT_RDWR);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
305 close(sock);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
306 sock = NULL_SOCKET;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
307 last_err = now;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
308 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
309 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
310 else last_err = now;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
311 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
312 else last_err = now;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
313 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
314 if (sock != NULL_SOCKET) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
315 conn = new SMTP(sock);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
316 #ifdef VERIFY_DEBUG
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
317 conn->log("get_connection() %d new socket", conn->get_fd());
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
318 #endif
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
319 if (conn->helo() == 250) return conn;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
320 delete conn;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
321 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
322 return NULL;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
323 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
324
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
325
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
326 void VERIFY::put_connection(SMTP *conn) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
327 if (conn->err()) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
328 #ifdef VERIFY_DEBUG
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
329 conn->log("put_socket() %d with error, close it", conn->get_fd());
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
330 #endif
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
331 delete conn;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
332 last_err = time(NULL);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
333 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
334 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
335 #ifdef VERIFY_DEBUG
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
336 conn->log("put_socket() %d", conn->get_fd());
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
337 #endif
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
338 conn->now();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
339 pthread_mutex_lock(&mutex);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
340 connections.push_back(conn);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
341 pthread_mutex_unlock(&mutex);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
342 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
343 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
344
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
345
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
346 bool VERIFY::ok(char *from, char *to) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
347 if (host == token_myhostname) return true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
348 SMTP *conn = get_connection();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
349 if (!conn) return true; // cannot verify right now, we have socket errors
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
350 int rc;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
351 rc = conn->from(from);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
352 #ifdef VERIFY_DEBUG
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
353 conn->log("verify::ok() from sees %d", rc);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
354 #endif
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
355 if (rc != 250) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
356 conn->rset();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
357 put_connection(conn);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
358 return (rc >= 500) ? false : true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
359 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
360 rc = conn->rcpt(to);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
361 #ifdef VERIFY_DEBUG
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
362 conn->log("verify::ok() rcpt sees %d", rc);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
363 #endif
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
364 put_connection(conn);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
365 return (rc >= 500) ? false : true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
366 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
367
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
368
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
369 DNSBL::DNSBL(char *n, char *s, char *m) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
370 name = n;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
371 suffix = s;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
372 message = m;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
373 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
374
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
375
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
376 bool DNSBL::operator==(const DNSBL &rhs) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
377 return (strcmp(name, rhs.name) == 0) &&
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
378 (strcmp(suffix, rhs.suffix) == 0) &&
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
379 (strcmp(message, rhs.message) == 0);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
380 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
381
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
382
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
383 CONFIG::CONFIG() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
384 reference_count = 0;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
385 generation = 0;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
386 load_time = 0;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
387 default_context = NULL;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
388 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
389
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
390
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
391 CONFIG::~CONFIG() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
392 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
393 CONTEXT *c = *i;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
394 delete c;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
395 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
396 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
397
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
398
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
399 void CONFIG::add_context(CONTEXTP con) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
400 contexts.push_back(con);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
401 if (!default_context && !con->get_parent()) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
402 // first global context
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
403 default_context = con;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
404 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
405 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
406
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
407
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
408 void CONFIG::add_to(char *to, CONTEXTP con) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
409 context_map::iterator i = env_to.find(to);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
410 if (i != env_to.end()) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
411 CONTEXTP c = (*i).second;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
412 int s = strlen(to);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
413 bool at = s && (to[s-1] == '@');
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
414 if (at && con->is_parent(c->get_parent())) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
415 if (debug_syslog) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
416 char oldname[maxlen];
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
417 char newname[maxlen];
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
418 char *oldn = c->get_full_name(oldname, maxlen);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
419 char *newn = con->get_full_name(newname, maxlen);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
420 char buf[maxlen*3];
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
421 snprintf(buf, maxlen*3, "both %s and %s claim envelope to %s, the first one wins", oldn, newn, to);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
422 my_syslog(buf);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
423 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
424 return; // don't take over user@ entries from your ancestors children
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
425 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
426 if ((c != con) && (c != con->get_parent())) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
427 if (debug_syslog) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
428 char oldname[maxlen];
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
429 char newname[maxlen];
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
430 char *oldn = c->get_full_name(oldname, maxlen);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
431 char *newn = con->get_full_name(newname, maxlen);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
432 char buf[maxlen*3];
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
433 snprintf(buf, maxlen*3, "both %s and %s claim envelope to %s, the second one wins", oldn, newn, to);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
434 my_syslog(buf);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
435 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
436 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
437 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
438 env_to[to] = con;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
439 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
440
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
441
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
442 CONTEXTP CONFIG::find_context(char *to) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
443 context_map::iterator i = env_to.find(to);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
444 if (i != env_to.end()) return (*i).second; // found user@domain.tld key
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
445 char *x = strchr(to, '@');
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
446 if (x) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
447 x++;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
448 i = env_to.find(x);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
449 if (i != env_to.end()) return (*i).second; // found domain.tld key
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
450 char y = *x;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
451 *x = '\0';
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
452 i = env_to.find(to);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
453 *x = y;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
454 if (i != env_to.end()) return (*i).second; // found user@ key
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
455 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
456 return default_context;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
457 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
458
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
459
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
460 void CONFIG::dump() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
461 if (default_context) default_context->dump();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
462 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
463 CONTEXTP c = *i;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
464 CONTEXTP p = c->get_parent();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
465 if (!p && (c != default_context)) c->dump();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
466 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
467 char buf[maxlen];
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
468 for (context_map::iterator i=env_to.begin(); i!=env_to.end(); i++) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
469 char *to = (*i).first;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
470 CONTEXTP con = (*i).second;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
471 printf("// envelope to %s \t-> context %s \n", to, con->get_full_name(buf,maxlen));
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
472 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
473 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
474
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
475
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
476 CONTEXT::CONTEXT(CONTEXTP parent_, char *name_) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
477 parent = parent_;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
478 name = name_;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
479 verify_host = NULL;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
480 env_from_default = (parent) ? token_inherit : token_unknown;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
481 content_filtering = (parent) ? parent->content_filtering : false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
482 content_suffix = NULL;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
483 content_message = NULL;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
484 host_limit = (parent) ? parent->host_limit : 0;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
485 host_limit_message = NULL;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
486 host_random = (parent) ? parent->host_random : false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
487 tag_limit = (parent) ? parent->tag_limit : 0;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
488 tag_limit_message = NULL;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
489 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
490
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
491
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
492 CONTEXT::~CONTEXT() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
493 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
494 DNSBLP d = (*i).second;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
495 // delete the underlying DNSBL objects.
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
496 delete d;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
497 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
498 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
499
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
500
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
501 bool CONTEXT::is_parent(CONTEXTP p) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
502 if (p == parent) return true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
503 if (!parent) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
504 return parent->is_parent(p);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
505 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
506
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
507
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
508 char *CONTEXT::get_full_name(char *buffer, int size) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
509 if (!parent) return name;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
510 char buf[maxlen];
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
511 snprintf(buffer, size, "%s.%s", parent->get_full_name(buf, maxlen), name);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
512 return buffer;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
513 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
514
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
515
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
516 bool CONTEXT::cover_env_to(char *to) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
517 char buffer[maxlen];
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
518 char *x = strchr(to, '@');
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
519 if (x) x++;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
520 else x = to;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
521 if (*x == '\0') return true; // always allow covering addresses with no domain name, eg abuse@
100
63e8633abc34 allow empty env_to at global context to remove all restrictions on child contexts
carl
parents: 99
diff changeset
522 if (!parent && env_to.empty()) return true; // empty env_to at global level covers everything
94
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
523 string_set::iterator i = env_to.find(x);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
524 if (i != env_to.end()) return true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
525 return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
526 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
527
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
528
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
529 VERIFYP CONTEXT::find_verify(char *to) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
530 if (verify_host && (verify_host != token_myhostname) && cover_env_to(to)) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
531 verify_map::iterator i = verifiers.find(verify_host);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
532 if (i == verifiers.end()) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
533 if (debug_syslog) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
534 char buf[maxlen];
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
535 snprintf(buf, maxlen, "cannot find struc for %s", verify_host);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
536 my_syslog(buf);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
537 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
538 return NULL;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
539 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
540 VERIFYP v = (*i).second;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
541
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
542 return v;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
543 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
544 else if (parent) return parent->find_verify(to);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
545 else return NULL;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
546 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
547
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
548
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
549 char *CONTEXT::find_from(char *from) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
550 char *rc = token_inherit;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
551 string_map::iterator i = env_from.find(from);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
552 if (i != env_from.end()) rc = (*i).second; // found user@domain.tld key
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
553 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
554 char *x = strchr(from, '@');
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
555 if (x) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
556 x++;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
557 i = env_from.find(x);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
558 if (i != env_from.end()) rc = (*i).second; // found domain.tld key
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
559 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
560 char y = *x;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
561 *x = '\0';
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
562 i = env_from.find(from);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
563 *x = y;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
564 if (i != env_from.end()) rc = (*i).second; // found user@ key
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
565 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
566 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
567 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
568 if (rc == token_inherit) rc = env_from_default;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
569 if ((rc == token_inherit) && parent) return parent->find_from(from);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
570 return (rc == token_inherit) ? token_unknown : rc;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
571 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
572
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
573
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
574 CONTEXTP CONTEXT::find_context(char *from) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
575 context_map::iterator i = env_from_context.find(from);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
576 if (i != env_from_context.end()) return (*i).second; // found user@domain.tld key
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
577 char *x = strchr(from, '@');
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
578 if (x) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
579 x++;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
580 i = env_from_context.find(x);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
581 if (i != env_from_context.end()) return (*i).second; // found domain.tld key
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
582 char y = *x;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
583 *x = '\0';
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
584 i = env_from_context.find(from);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
585 *x = y;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
586 if (i != env_from_context.end()) return (*i).second; // found user@ key
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
587 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
588 return this;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
589 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
590
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
591
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
592 CONTEXTP CONTEXT::find_from_context_name(char *name) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
593 context_map::iterator i = children.find(name);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
594 if (i != children.end()) return (*i).second;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
595 return NULL;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
596 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
597
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
598
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
599 DNSBLP CONTEXT::find_dnsbl(char *name) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
600 dnsblp_map::iterator i = dnsbl_names.find(name);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
601 if (i != dnsbl_names.end()) return (*i).second;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
602 if (parent) return parent->find_dnsbl(name);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
603 return NULL;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
604 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
605
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
606
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
607 char* CONTEXT::get_content_suffix() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
608 if (!content_suffix && parent) return parent->get_content_suffix();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
609 return content_suffix;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
610 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
611
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
612
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
613 char* CONTEXT::get_content_message() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
614 if (!content_message && parent) return parent->get_content_message();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
615 return content_message;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
616 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
617
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
618
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
619 string_set& CONTEXT::get_content_host_ignore() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
620 if (content_host_ignore.empty() && parent) return parent->get_content_host_ignore();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
621 return content_host_ignore;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
622 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
623
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
624
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
625 string_set& CONTEXT::get_content_tlds() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
626 if (content_tlds.empty() && parent) return parent->get_content_tlds();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
627 return content_tlds;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
628 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
629
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
630
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
631 string_set& CONTEXT::get_html_tags() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
632 if (html_tags.empty() && parent) return parent->get_html_tags();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
633 return html_tags;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
634 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
635
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
636
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
637 dnsblp_list& CONTEXT::get_dnsbl_list() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
638 if (dnsbl_list.empty() && parent) return parent->get_dnsbl_list();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
639 return dnsbl_list;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
640 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
641
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
642
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
643 bool CONTEXT::acceptable_content(recorder &memory, char *&msg) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
644 if (memory.excessive_bad_tags(tag_limit)) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
645 msg = tag_limit_message;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
646 return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
647 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
648 if (!host_random && memory.excessive_hosts(host_limit)) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
649 msg = host_limit_message;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
650 return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
651 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
652 return true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
653 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
654
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
655
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
656 void CONTEXT::dump(int level) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
657 char indent[maxlen];
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
658 int i = min(maxlen-1, level*4);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
659 memset(indent, ' ', i);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
660 indent[i] = '\0';
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
661 char buf[maxlen];
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
662 char *fullname = get_full_name(buf,maxlen);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
663 printf("%s context %s { \t// %s\n", indent, name, fullname);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
664
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
665 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
666 char *n = (*i).first;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
667 DNSBL &d = *(*i).second;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
668 printf("%s dnsbl %s %s \"%s\"; \n", indent, n, d.suffix, d.message);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
669 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
670
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
671 if (!dnsbl_list.empty()) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
672 printf("%s dnsbl_list", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
673 for (dnsblp_list::iterator i=dnsbl_list.begin(); i!=dnsbl_list.end(); i++) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
674 DNSBL &d = *(*i);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
675 printf(" %s", d.name);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
676 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
677 printf("; \n");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
678 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
679
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
680 if (content_filtering) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
681 printf("%s content on { \n", indent, env_from_default);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
682 if (content_suffix) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
683 printf("%s filter %s \"%s\"; \n", indent, content_suffix, content_message);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
684 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
685 if (!content_host_ignore.empty()) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
686 printf("%s ignore { \n", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
687 for (string_set::iterator i=content_host_ignore.begin(); i!=content_host_ignore.end(); i++) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
688 printf("%s %s; \n", indent, *i);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
689 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
690 printf("%s }; \n", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
691 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
692 if (!content_tlds.empty()) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
693 printf("%s tld { \n", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
694 printf("%s ", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
695 for (string_set::iterator i=content_tlds.begin(); i!=content_tlds.end(); i++) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
696 printf("%s; ", *i);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
697 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
698 printf("\n%s }; \n", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
699 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
700 if (!html_tags.empty()) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
701 printf("%s html_tags { \n", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
702 printf("%s ", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
703 for (string_set::iterator i=html_tags.begin(); i!=html_tags.end(); i++) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
704 printf("%s; ", *i);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
705 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
706 printf("\n%s }; \n", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
707 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
708 if (host_limit_message) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
709 printf("%s host_limit on %d \"%s\"; \n", indent, host_limit, host_limit_message);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
710 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
711 else if (host_random) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
712 printf("%s host_limit soft %d; \n", indent, host_limit);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
713 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
714 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
715 printf("%s host_limit off; \n", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
716 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
717 if (tag_limit_message) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
718 printf("%s html_limit on %d \"%s\"; \n", indent, tag_limit, tag_limit_message);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
719 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
720 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
721 printf("%s html_limit off; \n", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
722 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
723 printf("%s }; \n", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
724 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
725 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
726 printf("%s content off {}; \n", indent, env_from_default);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
727 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
728
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
729 printf("%s env_to { \t// %s\n", indent, fullname);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
730 for (string_set::iterator i=env_to.begin(); i!=env_to.end(); i++) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
731 printf("%s %s; \n", indent, *i);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
732 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
733 printf("%s }; \n", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
734
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
735 if (verify_host) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
736 printf("%s verify %s; \n", indent, verify_host);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
737 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
738
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
739 for (context_map::iterator i=children.begin(); i!=children.end(); i++) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
740 CONTEXTP c = (*i).second;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
741 c->dump(level+1);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
742 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
743
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
744 printf("%s env_from %s { \t// %s\n", indent, env_from_default, fullname);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
745 if (!env_from.empty()) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
746 printf("%s // white/black/unknown \n", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
747 for (string_map::iterator i=env_from.begin(); i!=env_from.end(); i++) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
748 char *f = (*i).first;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
749 char *t = (*i).second;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
750 printf("%s %s \t%s; \n", indent, f, t);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
751 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
752 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
753 if (!env_from_context.empty()) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
754 printf("%s // child contexts \n", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
755 for (context_map::iterator j=env_from_context.begin(); j!=env_from_context.end(); j++) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
756 char *f = (*j).first;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
757 CONTEXTP t = (*j).second;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
758 printf("%s %s \t%s; \n", indent, f, t->name);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
759 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
760 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
761 printf("%s }; \n", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
762
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
763 printf("%s }; \n", indent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
764 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
765
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
766
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
767 ////////////////////////////////////////////////
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
768 // helper to discard the strings held by a string_set
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
769 //
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
770 void discard(string_set &s) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
771 for (string_set::iterator i=s.begin(); i!=s.end(); i++) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
772 free(*i);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
773 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
774 s.clear();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
775 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
776
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
777
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
778 ////////////////////////////////////////////////
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
779 // helper to register a string in a string set
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
780 //
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
781 char* register_string(string_set &s, char *name) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
782 string_set::iterator i = s.find(name);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
783 if (i != s.end()) return *i;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
784 char *x = strdup(name);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
785 s.insert(x);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
786 return x;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
787 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
788
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
789
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
790 ////////////////////////////////////////////////
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
791 // register a global string
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
792 //
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
793 char* register_string(char *name) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
794 return register_string(all_strings, name);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
795 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
796
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
797
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
798 ////////////////////////////////////////////////
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
799 //
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
800 bool tsa(TOKEN &tok, char *token);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
801 bool tsa(TOKEN &tok, char *token) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
802 char *have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
803 if (have == token) return true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
804 tok.token_error(token, have);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
805 return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
806 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
807
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
808
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
809 ////////////////////////////////////////////////
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
810 //
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
811 bool parse_dnsbl(TOKEN &tok, CONFIG &dc, CONTEXT &me);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
812 bool parse_dnsbl(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
813 char *name = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
814 char *suf = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
815 char *msg = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
816 if (!tsa(tok, token_semi)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
817 DNSBLP dnsnew = new DNSBL(name, suf, msg);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
818 DNSBLP dnsold = me.find_dnsbl(name);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
819 if (dnsold && (*dnsold == *dnsnew)) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
820 // duplicate redefinition, ignore it
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
821 delete dnsnew;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
822 return true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
823 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
824 me.add_dnsbl(name, dnsnew);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
825 return true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
826 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
827
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
828
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
829 ////////////////////////////////////////////////
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
830 //
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
831 bool parse_dnsbll(TOKEN &tok, CONFIG &dc, CONTEXT &me);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
832 bool parse_dnsbll(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
833 while (true) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
834 char *have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
835 if (!have) break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
836 if (have == token_semi) break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
837 DNSBLP dns = me.find_dnsbl(have);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
838 if (dns) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
839 me.add_dnsbl(dns);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
840 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
841 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
842 tok.token_error("dnsbl name", have);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
843 return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
844 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
845 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
846 return true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
847 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
848
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
849
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
850 ////////////////////////////////////////////////
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
851 //
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
852 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
853 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
854 char *setting = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
855 if (setting == token_on) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
856 me.set_content_filtering(true);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
857 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
858 else if (setting == token_off) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
859 me.set_content_filtering(false);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
860 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
861 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
862 tok.token_error("on/off", setting);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
863 return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
864 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
865 if (!tsa(tok, token_lbrace)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
866 while (true) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
867 char *have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
868 if (!have) break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
869 if (have == token_filter) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
870 char *suffix = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
871 char *messag = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
872 me.set_content_suffix(suffix);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
873 me.set_content_message(messag);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
874 if (!tsa(tok, token_semi)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
875 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
876 else if (have == token_ignore) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
877 if (!tsa(tok, token_lbrace)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
878 while (true) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
879 if (!have) break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
880 char *have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
881 if (have == token_rbrace) break; // done
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
882 me.add_ignore(have);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
883 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
884 if (!tsa(tok, token_semi)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
885 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
886 else if (have == token_tld) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
887 if (!tsa(tok, token_lbrace)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
888 while (true) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
889 char *have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
890 if (!have) break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
891 if (have == token_rbrace) break; // done
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
892 me.add_tld(have);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
893 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
894 if (!tsa(tok, token_semi)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
895 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
896 else if (have == token_html_limit) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
897 have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
898 if (have == token_on) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
899 me.set_tag_limit(tok.nextint());
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
900 me.set_tag_message(tok.next());
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
901 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
902 else if (have == token_off) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
903 me.set_tag_limit(0);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
904 me.set_tag_message(NULL);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
905 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
906 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
907 tok.token_error("on/off", have);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
908 return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
909 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
910 if (!tsa(tok, token_semi)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
911 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
912 else if (have == token_html_tags) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
913 if (!tsa(tok, token_lbrace)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
914 while (true) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
915 char *have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
916 if (!have) break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
917 if (have == token_rbrace) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
918 break; // done
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
919 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
920 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
921 me.add_tag(have); // base version
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
922 char buf[200];
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
923 snprintf(buf, sizeof(buf), "/%s", have);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
924 me.add_tag(register_string(buf)); // leading /
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
925 snprintf(buf, sizeof(buf), "%s/", have);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
926 me.add_tag(register_string(buf)); // trailing /
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
927 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
928 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
929 if (!tsa(tok, token_semi)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
930 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
931 else if (have == token_host_limit) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
932 have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
933 if (have == token_on) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
934 me.set_host_limit(tok.nextint());
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
935 me.set_host_message(tok.next());
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
936 me.set_host_random(false);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
937 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
938 else if (have == token_off) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
939 me.set_host_limit(0);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
940 me.set_host_message(NULL);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
941 me.set_host_random(false);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
942 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
943 else if (have == token_soft) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
944 me.set_host_limit(tok.nextint());
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
945 me.set_host_message(NULL);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
946 me.set_host_random(true);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
947 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
948 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
949 tok.token_error("on/off/soft", have);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
950 return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
951 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
952 if (!tsa(tok, token_semi)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
953 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
954 else if (have == token_rbrace) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
955 break; // done
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
956 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
957 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
958 tok.token_error("content keyword", have);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
959 return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
960 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
961 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
962 return tsa(tok, token_semi);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
963 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
964
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
965
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
966 ////////////////////////////////////////////////
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
967 //
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
968 bool parse_envto(TOKEN &tok, CONFIG &dc, CONTEXT &me);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
969 bool parse_envto(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
970 if (!tsa(tok, token_lbrace)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
971 while (true) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
972 char *have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
973 if (!have) break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
974 if (have == token_rbrace) break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
975 if (have == token_semi) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
976 // optional separators
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
977 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
978 else if (have == token_dccto) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
979 char *flavor = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
980 if (!tsa(tok, token_lbrace)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
981 bool keeping = false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
982 while (true) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
983 char *have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
984 if (!have) break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
985 if (have == token_rbrace) break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
986 if (have == flavor) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
987 keeping = true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
988 continue;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
989 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
990 else if ((have == token_ok) || (have == token_ok2) || (have == token_many)) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
991 keeping = false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
992 continue;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
993 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
994 if (have == token_envto) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
995 have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
996 if (keeping) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
997 if (me.allow_env_to(have)) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
998 me.add_to(have);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
999 dc.add_to(have, &me);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1000 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1001 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1002 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1003 //else if (have == token_substitute) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1004 // if (tok.next() == token_mailhost) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1005 // have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1006 // if (keeping) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1007 // if (me.allow_env_to(have)) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1008 // me.add_to(have);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1009 // dc.add_to(have, &me);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1010 // }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1011 // }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1012 // }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1013 //}
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1014 tok.skipeol();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1015 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1016 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1017 else if (me.allow_env_to(have)) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1018 me.add_to(have);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1019 dc.add_to(have, &me);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1020 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1021 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1022 tok.token_error("user@ or user@domain.tld or domain.tld where domain.tld allowed by parent context", have);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1023 return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1024 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1025 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1026 return tsa(tok, token_semi);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1027 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1028
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1029
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1030 ////////////////////////////////////////////////
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1031 //
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1032 bool parse_verify(TOKEN &tok, CONFIG &dc, CONTEXT &me);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1033 bool parse_verify(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1034 char *host = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1035 if (!tsa(tok, token_semi)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1036 me.set_verify(host);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1037 add_verify_host(host);
99
f8963ddf7143 failed to return a value from parse_verify()
carl
parents: 97
diff changeset
1038 return true;
94
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1039 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1040
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1041
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1042 ////////////////////////////////////////////////
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1043 //
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1044 bool parse_envfrom(TOKEN &tok, CONFIG &dc, CONTEXT &me);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1045 bool parse_envfrom(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1046 char *st = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1047 if ((st == token_black) || (st == token_white) || (st == token_unknown) || (st == token_inherit)) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1048 me.set_from_default(st);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1049 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1050 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1051 tok.push(st);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1052 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1053 if (!tsa(tok, token_lbrace)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1054 while (true) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1055 char *have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1056 if (!have) break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1057 if (have == token_rbrace) break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1058 if (have == token_semi) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1059 // optional separators
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1060 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1061 else if (have == token_dccfrom) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1062 if (!tsa(tok, token_lbrace)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1063 bool keeping = false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1064 bool many = false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1065 while (true) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1066 char *have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1067 if (!have) break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1068 if (have == token_rbrace) break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1069 if (have == token_ok) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1070 keeping = true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1071 many = false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1072 continue;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1073 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1074 else if (have == token_many) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1075 keeping = true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1076 many = true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1077 continue;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1078 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1079 else if (have == token_ok2) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1080 keeping = false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1081 continue;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1082 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1083 if (have == token_envfrom) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1084 have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1085 if (keeping) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1086 me.add_from(have, (many) ? token_black : token_white);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1087 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1088 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1089 else if (have == token_substitute) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1090 if (tok.next() == token_mailhost) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1091 have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1092 me.add_from(have, (many) ? token_black : token_white);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1093 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1094 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1095 tok.skipeol();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1096 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1097 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1098 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1099 // may be a valid email address or domain name
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1100 char *st = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1101 if ((st == token_black) || (st == token_white) || (st == token_unknown)) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1102 me.add_from(have, st);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1103 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1104 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1105 CONTEXTP con = me.find_from_context_name(st);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1106 if (con) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1107 me.add_from_context(have, con);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1108 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1109 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1110 tok.token_error("white/black/unknown or child context name", st);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1111 return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1112 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1113 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1114 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1115 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1116 return tsa(tok, token_semi);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1117 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1118
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1119
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1120 ////////////////////////////////////////////////
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1121 //
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1122 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1123 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1124 char *name = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1125 if (!tsa(tok, token_lbrace)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1126 CONTEXTP con = new CONTEXT(parent, name);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1127
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1128 while (true) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1129 char *have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1130 if (!have) break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1131 if (have == token_rbrace) break; // done
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1132 if (have == token_dnsbl) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1133 if (!parse_dnsbl(tok, dc, *con)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1134 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1135 else if (have == token_dnsbll) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1136 if (!parse_dnsbll(tok, dc, *con)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1137 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1138 else if (have == token_content) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1139 if (!parse_content(tok, dc, *con)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1140 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1141 else if (have == token_envto) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1142 if (!parse_envto(tok, dc, *con)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1143 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1144 else if (have == token_verify) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1145 if (!parse_verify(tok, dc, *con)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1146 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1147 else if (have == token_envfrom) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1148 if (!parse_envfrom(tok, dc, *con)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1149 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1150 else if (have == token_context) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1151 if (!parse_context(tok, dc, con)) return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1152 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1153 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1154 tok.token_error("context keyword", have);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1155 return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1156 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1157 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1158
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1159 if (!tsa(tok, token_semi)) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1160 delete con;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1161 return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1162 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1163 dc.add_context(con);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1164 if (parent) parent->add_context(con);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1165 return true;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1166 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1167
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1168
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1169 ////////////////////////////////////////////////
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1170 // parse a config file
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1171 //
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1172 bool load_conf(CONFIG &dc, char *fn) {
99
f8963ddf7143 failed to return a value from parse_verify()
carl
parents: 97
diff changeset
1173 int count = 0;
94
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1174 TOKEN tok(fn, &dc.config_files);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1175 while (true) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1176 char *have = tok.next();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1177 if (!have) break;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1178 if (have == token_context) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1179 if (!parse_context(tok, dc, NULL)) {
99
f8963ddf7143 failed to return a value from parse_verify()
carl
parents: 97
diff changeset
1180 tok.token_error("load_conf() failed to parse context");
94
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1181 return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1182 }
99
f8963ddf7143 failed to return a value from parse_verify()
carl
parents: 97
diff changeset
1183 else count++;
94
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1184 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1185 else {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1186 tok.token_error(token_context, have);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1187 return false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1188 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1189 }
99
f8963ddf7143 failed to return a value from parse_verify()
carl
parents: 97
diff changeset
1190 tok.token_error("load_conf() found %d contexts in %s", count, fn);
94
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1191 return (dc.default_context) ? true : false;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1192 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1193
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1194
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1195 ////////////////////////////////////////////////
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1196 // setup a new smtp verify host
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1197 //
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1198 void add_verify_host(char *host) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1199 verify_map::iterator i = verifiers.find(host);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1200 if (i == verifiers.end()) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1201 VERIFYP v = new VERIFY(host);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1202 verifiers[host] = v;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1203 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1204 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1205
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1206
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1207 ////////////////////////////////////////////////
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1208 // thread to check for verify hosts with old sockets that we can close
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1209 //
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1210 void* verify_closer(void *arg) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1211 while (true) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1212 sleep(maxage);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1213 for (verify_map::iterator i=verifiers.begin(); i!=verifiers.end(); i++) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1214 VERIFYP v = (*i).second;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1215 v->closer();
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1216 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1217 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1218 return NULL;
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1219 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1220
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1221
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1222 ////////////////////////////////////////////////
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1223 // init the tokens
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1224 //
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1225 void token_init() {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1226 token_black = register_string("black");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1227 token_content = register_string("content");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1228 token_context = register_string("context");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1229 token_dccfrom = register_string("dcc_from");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1230 token_dccto = register_string("dcc_to");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1231 token_default = register_string("default");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1232 token_dnsbl = register_string("dnsbl");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1233 token_dnsbll = register_string("dnsbl_list");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1234 token_envfrom = register_string("env_from");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1235 token_envto = register_string("env_to");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1236 token_filter = register_string("filter");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1237 token_host_limit = register_string("host_limit");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1238 token_html_limit = register_string("html_limit");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1239 token_html_tags = register_string("html_tags");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1240 token_ignore = register_string("ignore");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1241 token_include = register_string("include");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1242 token_inherit = register_string("inherit");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1243 token_lbrace = register_string("{");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1244 token_mailhost = register_string("mail_host");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1245 token_many = register_string("many");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1246 token_off = register_string("off");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1247 token_ok = register_string("ok");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1248 token_ok2 = register_string("ok2");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1249 token_on = register_string("on");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1250 token_rbrace = register_string("}");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1251 token_semi = register_string(";");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1252 token_soft = register_string("soft");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1253 token_substitute = register_string("substitute");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1254 token_tld = register_string("tld");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1255 token_unknown = register_string("unknown");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1256 token_verify = register_string("verify");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1257 token_white = register_string("white");
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1258
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1259 if (gethostname(myhostname, HOST_NAME_MAX+1) != 0) {
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1260 strncpy(myhostname, "localhost", HOST_NAME_MAX+1);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1261 }
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1262 myhostname[HOST_NAME_MAX] = '\0'; // ensure null termination
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1263 token_myhostname = register_string(myhostname);
e107ade3b1c0 fix dos line terminators
carl
parents: 92
diff changeset
1264 }