1 |
/* |
2 |
Copyright (C) 2002-2003 Erik Fears |
3 |
|
4 |
This program is free software; you can redistribute it and/or |
5 |
modify it under the terms of the GNU General Public License |
6 |
as published by the Free Software Foundation; either version 2 |
7 |
of the License, or (at your option) any later version. |
8 |
|
9 |
This program is distributed in the hope that it will be useful, |
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
GNU General Public License for more details. |
13 |
|
14 |
You should have received a copy of the GNU General Public License |
15 |
along with this program; if not, write to the Free Software |
16 |
|
17 |
Foundation, Inc. |
18 |
59 Temple Place - Suite 330 |
19 |
Boston, MA 02111-1307, USA. |
20 |
|
21 |
*/ |
22 |
|
23 |
#include "setup.h" |
24 |
|
25 |
#include <stdio.h> |
26 |
#include <stdlib.h> |
27 |
#include <string.h> |
28 |
#include <sys/types.h> |
29 |
#include <sys/socket.h> |
30 |
#include <netinet/in.h> |
31 |
#include <arpa/inet.h> |
32 |
#include <time.h> |
33 |
#include <errno.h> |
34 |
#include <assert.h> |
35 |
|
36 |
#include "compat.h" |
37 |
#include "config.h" |
38 |
#include "dnsbl.h" |
39 |
#include "list.h" |
40 |
#include "log.h" |
41 |
#include "main.h" |
42 |
#include "malloc.h" |
43 |
#include "match.h" |
44 |
#include "scan.h" |
45 |
#include "irc.h" |
46 |
#include "stats.h" |
47 |
|
48 |
|
49 |
/* |
50 |
* Work out the DNSBL zones and send the dns query |
51 |
*/ |
52 |
void |
53 |
dnsbl_add(struct scan_struct *ss) |
54 |
{ |
55 |
struct in_addr in; |
56 |
unsigned char a, b, c, d; |
57 |
char lookup[128]; |
58 |
node_t *p; |
59 |
int res; |
60 |
struct dnsbl_scan *ds; |
61 |
|
62 |
if (inet_pton(AF_INET, ss->ip, &in) <= 0) |
63 |
{ |
64 |
log_printf("DNSBL -> Invalid address '%s', ignoring.", ss->ip); |
65 |
return; |
66 |
} |
67 |
|
68 |
d = (unsigned char)(in.s_addr >> 24) & 0xFF; |
69 |
c = (unsigned char)(in.s_addr >> 16) & 0xFF; |
70 |
b = (unsigned char)(in.s_addr >> 8) & 0xFF; |
71 |
a = (unsigned char) in.s_addr & 0xFF; |
72 |
|
73 |
LIST_FOREACH(p, OpmItem->blacklists->head) |
74 |
{ |
75 |
struct BlacklistConf *bl = p->data; |
76 |
|
77 |
#ifdef WORDS_BIGENDIAN |
78 |
snprintf(lookup, sizeof(lookup), "%d.%d.%d.%d.%s", a, b, c, d, bl->name); |
79 |
#else |
80 |
snprintf(lookup, sizeof(lookup), "%d.%d.%d.%d.%s", d, c, b, a, bl->name); |
81 |
#endif |
82 |
|
83 |
ds = xcalloc(sizeof *ds); |
84 |
ds->ss = ss; |
85 |
ds->bl = bl; |
86 |
|
87 |
if (OPT_DEBUG) |
88 |
log_printf("DNSBL -> Passed '%s' to resolver", lookup); |
89 |
|
90 |
res = firedns_getip(FDNS_QRY_A, lookup, ds); |
91 |
|
92 |
if (res == -1 && fdns_errno != FDNS_ERR_FDLIMIT) |
93 |
{ |
94 |
log_printf("DNSBL -> Error sending dns lookup for '%s': %s", lookup, firedns_strerror(fdns_errno)); |
95 |
MyFree(ds); |
96 |
} |
97 |
else |
98 |
++ss->scans; /* Increase scan count - one for each blacklist */ |
99 |
} |
100 |
} |
101 |
|
102 |
static void |
103 |
dnsbl_positive(struct scan_struct *ss, struct BlacklistConf *bl, unsigned char type) |
104 |
{ |
105 |
char text_type[128] = ""; |
106 |
node_t *p; |
107 |
|
108 |
if (bl->type == A_BITMASK) |
109 |
{ |
110 |
LIST_FOREACH(p, bl->reply->head) |
111 |
{ |
112 |
const struct BlacklistReplyConf *item = p->data; |
113 |
|
114 |
if (item->number & type) |
115 |
{ |
116 |
strncat(text_type, item->type, sizeof(text_type) - strlen(text_type) - 2); |
117 |
text_type[sizeof(text_type) - 2] = '\0'; |
118 |
|
119 |
strncat(text_type, ", ", sizeof(text_type) - strlen(text_type) - 1); |
120 |
text_type[sizeof(text_type) - 1] = '\0'; |
121 |
} |
122 |
} |
123 |
|
124 |
if (text_type[0]) |
125 |
*(strrchr(text_type, ',')) = '\0'; |
126 |
} |
127 |
else |
128 |
{ |
129 |
LIST_FOREACH(p, bl->reply->head) |
130 |
{ |
131 |
const struct BlacklistReplyConf *item = p->data; |
132 |
|
133 |
if (item->number == type) |
134 |
{ |
135 |
strlcpy(text_type, item->type, sizeof(text_type)); |
136 |
break; |
137 |
} |
138 |
} |
139 |
} |
140 |
|
141 |
if (text_type[0] == '\0' && bl->ban_unknown == 0) |
142 |
{ |
143 |
if (OPT_DEBUG) |
144 |
log_printf("DNSBL -> Unknown result from BL zone %s (%d)", bl->name, type); |
145 |
|
146 |
return; |
147 |
} |
148 |
|
149 |
if (ss->manual_target) |
150 |
irc_send("PRIVMSG %s :CHECK -> DNSBL -> %s appears in BL zone %s (%s)", |
151 |
ss->manual_target->name, ss->ip, bl->name, text_type); |
152 |
else if (ss->positive == 0) |
153 |
{ |
154 |
/* Only report it if no other scans have found positives yet. */ |
155 |
scan_positive(ss, (bl->kline[0] ? bl->kline : IRCItem->kline), text_type); |
156 |
|
157 |
irc_send_channels("DNSBL -> %s!%s@%s appears in BL zone %s (%s)", |
158 |
ss->irc_nick, ss->irc_username, ss->irc_hostname, bl->name, |
159 |
text_type); |
160 |
log_printf("DNSBL -> %s!%s@%s appears in BL zone %s (%s)", |
161 |
ss->irc_nick, ss->irc_username, ss->irc_hostname, bl->name, |
162 |
text_type); |
163 |
} |
164 |
|
165 |
/* Record stat */ |
166 |
stats_dnsblrecv(bl); |
167 |
} |
168 |
|
169 |
void |
170 |
dnsbl_result(struct firedns_result *res) |
171 |
{ |
172 |
struct dnsbl_scan *const ds = res->info; |
173 |
|
174 |
if (OPT_DEBUG) |
175 |
{ |
176 |
if (ds->ss->manual_target) |
177 |
log_printf("DNSBL -> Lookup result for %s (%s) %d.%d.%d.%d (error: %d)", |
178 |
ds->ss->ip, |
179 |
res->lookup, |
180 |
(unsigned char)res->text[0], |
181 |
(unsigned char)res->text[1], |
182 |
(unsigned char)res->text[2], |
183 |
(unsigned char)res->text[3], fdns_errno); |
184 |
else |
185 |
log_printf("DNSBL -> Lookup result for %s!%s@%s (%s) %d.%d.%d.%d (error: %d)", |
186 |
ds->ss->irc_nick, |
187 |
ds->ss->irc_username, |
188 |
ds->ss->irc_hostname, |
189 |
res->lookup, |
190 |
(unsigned char)res->text[0], |
191 |
(unsigned char)res->text[1], |
192 |
(unsigned char)res->text[2], |
193 |
(unsigned char)res->text[3], fdns_errno); |
194 |
} |
195 |
|
196 |
/* Everything is OK */ |
197 |
if (res->text[0] == '\0' && fdns_errno == FDNS_ERR_NXDOMAIN) |
198 |
{ |
199 |
if (ds->ss->manual_target) |
200 |
irc_send("PRIVMSG %s :CHECK -> DNSBL -> %s does not appear in BL zone %s", |
201 |
ds->ss->manual_target->name, ds->ss->ip, |
202 |
(strlen(ds->ss->ip) < strlen(res->lookup)) ? (res->lookup + strlen(ds->ss->ip) + 1) : res->lookup); |
203 |
|
204 |
--ds->ss->scans; /* We are done with ss here */ |
205 |
scan_checkfinished(ds->ss); /* This could free ss, don't use ss after this point */ |
206 |
MyFree(ds); /* No longer need our information */ |
207 |
return; |
208 |
} |
209 |
|
210 |
/* Either an error, or a positive lookup */ |
211 |
if (fdns_errno == FDNS_ERR_NONE) |
212 |
dnsbl_positive(ds->ss, ds->bl, (unsigned char)res->text[3]); |
213 |
else |
214 |
{ |
215 |
log_printf("DNSBL -> Lookup error on %s: %s", res->lookup, |
216 |
firedns_strerror(fdns_errno)); |
217 |
|
218 |
if (fdns_errno != FDNS_ERR_TIMEOUT) |
219 |
irc_send_channels("DNSBL -> Lookup error on %s: %s", res->lookup, |
220 |
firedns_strerror(fdns_errno)); |
221 |
} |
222 |
|
223 |
/* Check if ss has any remaining scans */ |
224 |
--ds->ss->scans; /* We are done with ss here */ |
225 |
scan_checkfinished(ds->ss); /* This could free ss, don't use ss after this point */ |
226 |
MyFree(ds); /* Finished with dnsbl_scan too */ |
227 |
} |
228 |
|
229 |
void |
230 |
dnsbl_cycle(void) |
231 |
{ |
232 |
firedns_cycle(); |
233 |
} |
234 |
|
235 |
#define DNSBL_REPORT_VERSION "3.1.3" |
236 |
|
237 |
/* |
238 |
* Send an email to report this open proxy. |
239 |
*/ |
240 |
void |
241 |
dnsbl_report(const struct scan_struct *ss) |
242 |
{ |
243 |
char buf[4096], cmdbuf[512]; |
244 |
FILE *fp; |
245 |
|
246 |
assert(ss->ip); |
247 |
|
248 |
if (EmptyString(OpmItem->dnsbl_to) || EmptyString(OpmItem->dnsbl_from) || EmptyString(OpmItem->sendmail)) |
249 |
return; |
250 |
|
251 |
snprintf(cmdbuf, sizeof(cmdbuf), "%s -t", OpmItem->sendmail); |
252 |
snprintf(buf, sizeof(buf), |
253 |
"From: %s <%s>\n" |
254 |
"To: %s\n" |
255 |
"Subject: BOPM Report\n" |
256 |
"X-BOPM-Version: %s\n\n" |
257 |
"%s: %s:%d\n\n" |
258 |
"%s\n", IRCItem->nick, OpmItem->dnsbl_from, OpmItem->dnsbl_to, |
259 |
DNSBL_REPORT_VERSION, scan_gettype(ss->remote->protocol), ss->ip, |
260 |
ss->remote->port, ss->proof); |
261 |
|
262 |
if (OPT_DEBUG >= 3) |
263 |
log_printf("DNSBL -> Sending following email:\n%s\n", buf); |
264 |
|
265 |
if ((fp = popen(cmdbuf, "w")) == NULL) |
266 |
{ |
267 |
log_printf("DNSBL -> Failed to create pipe to '%s' for email report!", cmdbuf); |
268 |
irc_send_channels("I was trying to create a pipe to'%s' to send a DNSBL " |
269 |
"report, and it failed! I'll give up for now.", |
270 |
cmdbuf); |
271 |
return; |
272 |
} |
273 |
|
274 |
fputs(buf, fp); |
275 |
pclose(fp); |
276 |
|
277 |
log_printf("DNSBL -> Sent report to %s [%s]", OpmItem->dnsbl_to, ss->ip); |
278 |
|
279 |
/* Record send in stats */ |
280 |
stats_dnsblsend(); |
281 |
} |