ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/branches/1.0.x/src/dnsbl.c
Revision: 5427
Committed: Wed Jan 28 13:51:01 2015 UTC (10 years, 6 months ago) by michael
Content type: text/x-csrc
File size: 7976 byte(s)
Log Message:
- memory.c:MyFree(): removed extraneous pointer test; renamed MyFree to xfree

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id