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 |
|
35 |
#include "compat.h" |
36 |
#include "config.h" |
37 |
#include "dnsbl.h" |
38 |
#include "list.h" |
39 |
#include "log.h" |
40 |
#include "main.h" |
41 |
#include "malloc.h" |
42 |
#include "match.h" |
43 |
#include "scan.h" |
44 |
#include "irc.h" |
45 |
#include "stats.h" |
46 |
|
47 |
|
48 |
/* |
49 |
* Work out the DNSBL zones and send the dns query |
50 |
*/ |
51 |
void |
52 |
dnsbl_add(struct scan_struct *ss) |
53 |
{ |
54 |
struct in_addr in; |
55 |
unsigned char a, b, c, d; |
56 |
char lookup[128]; |
57 |
node_t *p; |
58 |
int res; |
59 |
struct dnsbl_scan *ds; |
60 |
|
61 |
if (inet_pton(AF_INET, ss->ip, &in) <= 0) |
62 |
{ |
63 |
log_printf("DNSBL -> Invalid address '%s', ignoring.", ss->ip); |
64 |
return; |
65 |
} |
66 |
|
67 |
d = (unsigned char)(in.s_addr >> 24) & 0xFF; |
68 |
c = (unsigned char)(in.s_addr >> 16) & 0xFF; |
69 |
b = (unsigned char)(in.s_addr >> 8) & 0xFF; |
70 |
a = (unsigned char) in.s_addr & 0xFF; |
71 |
|
72 |
LIST_FOREACH(p, OpmItem->blacklists->head) |
73 |
{ |
74 |
struct BlacklistConf *bl = p->data; |
75 |
|
76 |
#ifdef WORDS_BIGENDIAN |
77 |
snprintf(lookup, 128, "%d.%d.%d.%d.%s", a, b, c, d, bl->name); |
78 |
#else |
79 |
snprintf(lookup, 128, "%d.%d.%d.%d.%s", d, c, b, a, bl->name); |
80 |
#endif |
81 |
|
82 |
ds = xcalloc(sizeof *ds); |
83 |
ds->ss = ss; |
84 |
ds->bl = bl; |
85 |
|
86 |
if (OPT_DEBUG) |
87 |
log_printf("DNSBL -> Passed '%s' to resolver", lookup); |
88 |
|
89 |
res = firedns_getip(FDNS_QRY_A, lookup, (void *) ds); |
90 |
|
91 |
if (res == -1 && fdns_errno != FDNS_ERR_FDLIMIT) |
92 |
{ |
93 |
log_printf("DNSBL -> Error sending dns lookup for '%s': %s", lookup, firedns_strerror(fdns_errno)); |
94 |
free(ds); |
95 |
} |
96 |
else |
97 |
++ss->scans; /* Increase scan count - one for each blacklist */ |
98 |
} |
99 |
} |
100 |
|
101 |
static void |
102 |
dnsbl_positive(struct scan_struct *ss, struct BlacklistConf *bl, unsigned char type) |
103 |
{ |
104 |
char text_type[128] = ""; |
105 |
node_t *p; |
106 |
|
107 |
if (bl->type == A_BITMASK) |
108 |
{ |
109 |
LIST_FOREACH(p, bl->reply->head) |
110 |
{ |
111 |
const struct BlacklistReplyConf *item = p->data; |
112 |
|
113 |
if (item->number & type) |
114 |
{ |
115 |
strncat(text_type, item->type, sizeof(text_type) - strlen(text_type) - 2); |
116 |
text_type[sizeof(text_type) - 2] = '\0'; |
117 |
|
118 |
strncat(text_type, ", ", sizeof(text_type) - strlen(text_type) - 1); |
119 |
text_type[sizeof(text_type) - 1] = '\0'; |
120 |
} |
121 |
} |
122 |
|
123 |
if (text_type[0]) |
124 |
*(strrchr(text_type, ',')) = '\0'; |
125 |
} |
126 |
else |
127 |
{ |
128 |
LIST_FOREACH(p, bl->reply->head) |
129 |
{ |
130 |
const struct BlacklistReplyConf *item = p->data; |
131 |
|
132 |
if (item->number == type) |
133 |
{ |
134 |
strlcpy(text_type, item->type, sizeof(text_type)); |
135 |
break; |
136 |
} |
137 |
} |
138 |
} |
139 |
|
140 |
if (text_type[0] == '\0' && bl->ban_unknown == 0) |
141 |
{ |
142 |
if (OPT_DEBUG) |
143 |
log_printf("DNSBL -> Unknown result from BL zone %s (%d)", bl->name, type); |
144 |
|
145 |
return; |
146 |
} |
147 |
|
148 |
if (ss->manual_target) |
149 |
irc_send("PRIVMSG %s :CHECK -> DNSBL -> %s appears in BL zone %s (%s)", |
150 |
ss->manual_target->name, ss->ip, bl->name, text_type); |
151 |
else if (!ss->positive) |
152 |
{ |
153 |
/* Only report it if no other scans have found positives yet. */ |
154 |
scan_positive(ss, (bl->kline[0] ? bl->kline : IRCItem->kline), text_type); |
155 |
|
156 |
irc_send_channels("DNSBL -> %s!%s@%s appears in BL zone %s (%s)", |
157 |
ss->irc_nick, ss->irc_username, ss->irc_hostname, bl->name, |
158 |
text_type); |
159 |
log_printf("DNSBL -> %s!%s@%s appears in BL zone %s (%s)", |
160 |
ss->irc_nick, ss->irc_username, ss->irc_hostname, bl->name, |
161 |
text_type); |
162 |
} |
163 |
|
164 |
/* Record stat */ |
165 |
stats_dnsblrecv(bl); |
166 |
} |
167 |
|
168 |
void |
169 |
dnsbl_result(struct firedns_result *res) |
170 |
{ |
171 |
struct dnsbl_scan *ds = res->info; |
172 |
|
173 |
if (OPT_DEBUG) |
174 |
{ |
175 |
if (ds->ss->manual_target) |
176 |
log_printf("DNSBL -> Lookup result for %s (%s) %d.%d.%d.%d (error: %d)", |
177 |
ds->ss->ip, |
178 |
res->lookup, |
179 |
(unsigned char)res->text[0], |
180 |
(unsigned char)res->text[1], |
181 |
(unsigned char)res->text[2], |
182 |
(unsigned char)res->text[3], fdns_errno); |
183 |
else |
184 |
log_printf("DNSBL -> Lookup result for %s!%s@%s (%s) %d.%d.%d.%d (error: %d)", |
185 |
ds->ss->irc_nick, |
186 |
ds->ss->irc_username, |
187 |
ds->ss->irc_hostname, |
188 |
res->lookup, |
189 |
(unsigned char)res->text[0], |
190 |
(unsigned char)res->text[1], |
191 |
(unsigned char)res->text[2], |
192 |
(unsigned char)res->text[3], fdns_errno); |
193 |
} |
194 |
|
195 |
/* Everything is OK */ |
196 |
if (res->text[0] == '\0' && fdns_errno == FDNS_ERR_NXDOMAIN) |
197 |
{ |
198 |
if (ds->ss->manual_target) |
199 |
irc_send("PRIVMSG %s :CHECK -> DNSBL -> %s does not appear in BL zone %s", |
200 |
ds->ss->manual_target->name, ds->ss->ip, |
201 |
(strlen(ds->ss->ip) < strlen(res->lookup)) ? (res->lookup + strlen(ds->ss->ip) + 1) : res->lookup); |
202 |
|
203 |
--ds->ss->scans; /* We are done with ss here */ |
204 |
scan_checkfinished(ds->ss); /* This could free ss, don't use ss after this point */ |
205 |
MyFree(ds); /* No longer need our information */ |
206 |
return; |
207 |
} |
208 |
|
209 |
/* Either an error, or a positive lookup */ |
210 |
if (fdns_errno == FDNS_ERR_NONE) |
211 |
dnsbl_positive(ds->ss, ds->bl, (unsigned char)res->text[3]); |
212 |
else |
213 |
{ |
214 |
log_printf("DNSBL -> Lookup error on %s: %s", res->lookup, |
215 |
firedns_strerror(fdns_errno)); |
216 |
|
217 |
if (fdns_errno != FDNS_ERR_TIMEOUT) |
218 |
irc_send_channels("DNSBL -> Lookup error on %s: %s", res->lookup, |
219 |
firedns_strerror(fdns_errno)); |
220 |
} |
221 |
|
222 |
/* Check if ss has any remaining scans */ |
223 |
--ds->ss->scans; /* We are done with ss here */ |
224 |
scan_checkfinished(ds->ss); /* This could free ss, don't use ss after this point */ |
225 |
MyFree(ds); /* Finished with dnsbl_scan too */ |
226 |
} |
227 |
|
228 |
void |
229 |
dnsbl_cycle(void) |
230 |
{ |
231 |
firedns_cycle(); |
232 |
} |
233 |
|
234 |
#define DNSBL_REPORT_VERSION "3.1.3" |
235 |
|
236 |
/* |
237 |
* Send an email to report this open proxy. |
238 |
*/ |
239 |
void |
240 |
dnsbl_report(struct scan_struct *ss) |
241 |
{ |
242 |
char buf[4096], cmdbuf[512]; |
243 |
FILE *fp; |
244 |
|
245 |
if (ss->ip == NULL) |
246 |
return; |
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 |
} |