]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* |
2 | * charybdis: A slightly useful ircd. | |
3 | * blacklist.c: Manages DNS blacklist entries and lookups | |
4 | * | |
0a1e77c2 | 5 | * Copyright (C) 2006-2011 charybdis development team |
212380e3 AC |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions are | |
9 | * met: | |
10 | * | |
11 | * 1. Redistributions of source code must retain the above copyright notice, | |
12 | * this list of conditions and the following disclaimer. | |
13 | * | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in the | |
16 | * documentation and/or other materials provided with the distribution. | |
17 | * | |
18 | * 3. The name of the author may not be used to endorse or promote products | |
19 | * derived from this software without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
22 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
23 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
24 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | |
25 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
26 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
27 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
29 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
30 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
31 | * POSSIBILITY OF SUCH DAMAGE. | |
32 | * | |
212380e3 AC |
33 | */ |
34 | ||
35 | #include "stdinc.h" | |
36 | #include "client.h" | |
1d02144f | 37 | #include "dns.h" |
212380e3 AC |
38 | #include "numeric.h" |
39 | #include "reject.h" | |
40 | #include "s_conf.h" | |
41 | #include "s_user.h" | |
42 | #include "blacklist.h" | |
77d3d2db | 43 | #include "send.h" |
212380e3 | 44 | |
b2f0da88 | 45 | rb_dlink_list blacklist_list = { NULL, NULL, 0 }; |
212380e3 AC |
46 | |
47 | /* private interfaces */ | |
48 | static struct Blacklist *find_blacklist(char *name) | |
49 | { | |
b2f0da88 | 50 | rb_dlink_node *nptr; |
212380e3 | 51 | |
b2f0da88 | 52 | RB_DLINK_FOREACH(nptr, blacklist_list.head) |
212380e3 AC |
53 | { |
54 | struct Blacklist *blptr = (struct Blacklist *) nptr->data; | |
55 | ||
56 | if (!irccmp(blptr->host, name)) | |
57 | return blptr; | |
58 | } | |
59 | ||
60 | return NULL; | |
61 | } | |
62 | ||
1d02144f | 63 | static inline int blacklist_check_reply(struct BlacklistClient *blcptr, const char *ipaddr) |
3c93d380 EM |
64 | { |
65 | struct Blacklist *blptr = blcptr->blacklist; | |
1d02144f | 66 | const char *lastoctet; |
3c93d380 EM |
67 | rb_dlink_node *ptr; |
68 | ||
3c93d380 EM |
69 | /* No filters and entry found - thus positive match */ |
70 | if (!rb_dlink_list_length(&blptr->filters)) | |
71 | return 1; | |
72 | ||
3c93d380 EM |
73 | /* Below will prolly have to change too if the above changes */ |
74 | if ((lastoctet = strrchr(ipaddr, '.')) == NULL || *(++lastoctet) == '\0') | |
75 | goto blwarn; | |
76 | ||
77 | RB_DLINK_FOREACH(ptr, blcptr->blacklist->filters.head) | |
78 | { | |
79 | struct BlacklistFilter *filter = ptr->data; | |
1d02144f | 80 | const char *cmpstr; |
3c93d380 EM |
81 | |
82 | if (filter->type == BLACKLIST_FILTER_ALL) | |
83 | cmpstr = ipaddr; | |
84 | else if (filter->type == BLACKLIST_FILTER_LAST) | |
85 | cmpstr = lastoctet; | |
86 | else | |
87 | { | |
88 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
89 | "blacklist_check_reply(): Unknown filtertype (BUG!)"); | |
90 | continue; | |
91 | } | |
92 | ||
93 | if (strcmp(cmpstr, filter->filterstr) == 0) | |
94 | /* Match! */ | |
95 | return 1; | |
96 | } | |
97 | ||
98 | return 0; | |
99 | blwarn: | |
100 | if (blcptr->blacklist->lastwarning + 3600 < rb_current_time()) | |
101 | { | |
102 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
103 | "Garbage reply from blacklist %s", | |
104 | blcptr->blacklist->host); | |
105 | blcptr->blacklist->lastwarning = rb_current_time(); | |
106 | } | |
107 | return 0; | |
108 | } | |
109 | ||
1d02144f | 110 | static void blacklist_dns_callback(const char *result, int status, int aftype, void *vptr) |
212380e3 AC |
111 | { |
112 | struct BlacklistClient *blcptr = (struct BlacklistClient *) vptr; | |
137d856d | 113 | int listed = 0; |
212380e3 AC |
114 | |
115 | if (blcptr == NULL || blcptr->client_p == NULL) | |
116 | return; | |
117 | ||
118 | if (blcptr->client_p->preClient == NULL) | |
119 | { | |
120 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
121 | "blacklist_dns_callback(): blcptr->client_p->preClient (%s) is NULL", get_client_name(blcptr->client_p, HIDE_IP)); | |
637c4932 | 122 | rb_free(blcptr); |
212380e3 AC |
123 | return; |
124 | } | |
125 | ||
1d02144f | 126 | if (result != NULL && status) |
137d856d | 127 | { |
1d02144f | 128 | if (blacklist_check_reply(blcptr, result)) |
96274734 | 129 | listed = TRUE; |
137d856d JT |
130 | } |
131 | ||
212380e3 | 132 | /* they have a blacklist entry for this client */ |
137d856d | 133 | if (listed && blcptr->client_p->preClient->dnsbl_listed == NULL) |
212380e3 AC |
134 | { |
135 | blcptr->client_p->preClient->dnsbl_listed = blcptr->blacklist; | |
136 | /* reference to blacklist moves from blcptr to client_p->preClient... */ | |
137 | } | |
138 | else | |
139 | unref_blacklist(blcptr->blacklist); | |
140 | ||
b2f0da88 | 141 | rb_dlinkDelete(&blcptr->node, &blcptr->client_p->preClient->dnsbl_queries); |
212380e3 AC |
142 | |
143 | /* yes, it can probably happen... */ | |
b2f0da88 | 144 | if (rb_dlink_list_length(&blcptr->client_p->preClient->dnsbl_queries) == 0 && blcptr->client_p->flags & FLAGS_SENTUSER && !EmptyString(blcptr->client_p->name)) |
21251822 | 145 | register_local_user(blcptr->client_p, blcptr->client_p); |
212380e3 | 146 | |
637c4932 | 147 | rb_free(blcptr); |
212380e3 AC |
148 | } |
149 | ||
212380e3 AC |
150 | static void initiate_blacklist_dnsquery(struct Blacklist *blptr, struct Client *client_p) |
151 | { | |
eddc2ab6 | 152 | struct BlacklistClient *blcptr = rb_malloc(sizeof(struct BlacklistClient)); |
db3efb7a | 153 | char buf[IRCD_RES_HOSTLEN + 1]; |
5d21ef50 | 154 | uint8_t *ip; |
212380e3 AC |
155 | |
156 | blcptr->blacklist = blptr; | |
157 | blcptr->client_p = client_p; | |
158 | ||
0a1e77c2 EM |
159 | if ((client_p->localClient->ip.ss_family == AF_INET) && blptr->ipv4) |
160 | { | |
161 | ip = (uint8_t *)&((struct sockaddr_in *)&client_p->localClient->ip)->sin_addr.s_addr; | |
162 | ||
163 | /* becomes 2.0.0.127.torbl.ahbl.org or whatever */ | |
164 | rb_snprintf(buf, sizeof buf, "%d.%d.%d.%d.%s", | |
165 | (unsigned int) ip[3], | |
166 | (unsigned int) ip[2], | |
167 | (unsigned int) ip[1], | |
168 | (unsigned int) ip[0], | |
169 | blptr->host); | |
170 | } | |
171 | /* IPv6 is supported now. --Elizabeth */ | |
172 | else if ((client_p->localClient->ip.ss_family == AF_INET6) && blptr->ipv6) | |
173 | { | |
174 | /* Breaks it into ip[0] = 0x00, ip[1] = 0x00... ip[16] = 0x01 for localhost | |
175 | * I wish there was a uint4_t for C, this would make the below cleaner, but... */ | |
176 | ip = (uint8_t *)&((struct sockaddr_in6 *)&client_p->localClient->ip)->sin6_addr.s6_addr; | |
177 | char *bufptr = buf; | |
178 | int i; | |
179 | ||
180 | /* The below will give us something like | |
181 | * 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.foobl.invalid | |
182 | */ | |
183 | ||
184 | /* Going backwards */ | |
185 | for (i = 15; i >= 0; i--) | |
186 | { | |
187 | /* Get upper and lower nibbles (yes this works fine on | |
188 | * both big and little endian) */ | |
189 | uint8_t hi = (ip[i] >> 4) & 0x0F; | |
190 | uint8_t lo = ip[i] & 0x0F; | |
191 | ||
192 | /* One part... (why 5? rb_snprintf adds \0) */ | |
193 | rb_snprintf(bufptr, 5, "%1x.%1x.", | |
194 | (unsigned int) lo, /* Remember, backwards */ | |
195 | (unsigned int) hi); | |
196 | ||
197 | /* Lurch forward to next position */ | |
198 | bufptr += 4; | |
199 | } | |
212380e3 | 200 | |
0a1e77c2 EM |
201 | /* Tack host on */ |
202 | strcpy(bufptr, blptr->host); | |
203 | } | |
204 | /* This shouldn't happen... */ | |
205 | else | |
206 | return; | |
212380e3 | 207 | |
1d02144f | 208 | blcptr->dns_id = lookup_hostname(buf, AF_INET, blacklist_dns_callback, blcptr); |
212380e3 | 209 | |
b2f0da88 | 210 | rb_dlinkAdd(blcptr, &blcptr->node, &client_p->preClient->dnsbl_queries); |
212380e3 AC |
211 | blptr->refcount++; |
212 | } | |
213 | ||
214 | /* public interfaces */ | |
3c93d380 | 215 | struct Blacklist *new_blacklist(char *name, char *reject_reason, int ipv4, int ipv6, rb_dlink_list *filters) |
212380e3 AC |
216 | { |
217 | struct Blacklist *blptr; | |
218 | ||
219 | if (name == NULL || reject_reason == NULL) | |
220 | return NULL; | |
221 | ||
222 | blptr = find_blacklist(name); | |
223 | if (blptr == NULL) | |
224 | { | |
eddc2ab6 | 225 | blptr = rb_malloc(sizeof(struct Blacklist)); |
b2f0da88 | 226 | rb_dlinkAddAlloc(blptr, &blacklist_list); |
212380e3 AC |
227 | } |
228 | else | |
229 | blptr->status &= ~CONF_ILLEGAL; | |
3c93d380 | 230 | |
db3efb7a | 231 | rb_strlcpy(blptr->host, name, IRCD_RES_HOSTLEN + 1); |
f427c8b0 | 232 | rb_strlcpy(blptr->reject_reason, reject_reason, IRCD_BUFSIZE); |
0a1e77c2 EM |
233 | blptr->ipv4 = ipv4; |
234 | blptr->ipv6 = ipv6; | |
3c93d380 EM |
235 | |
236 | rb_dlinkMoveList(filters, &blptr->filters); | |
237 | ||
96274734 | 238 | blptr->lastwarning = 0; |
212380e3 AC |
239 | |
240 | return blptr; | |
241 | } | |
242 | ||
243 | void unref_blacklist(struct Blacklist *blptr) | |
244 | { | |
3c93d380 EM |
245 | rb_dlink_node *ptr, *next_ptr; |
246 | ||
212380e3 AC |
247 | blptr->refcount--; |
248 | if (blptr->status & CONF_ILLEGAL && blptr->refcount <= 0) | |
249 | { | |
3c93d380 EM |
250 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, blptr->filters.head) |
251 | { | |
3c93d380 | 252 | rb_dlinkDelete(ptr, &blptr->filters); |
b84e58f9 | 253 | rb_free(ptr); |
3c93d380 EM |
254 | } |
255 | ||
b2f0da88 | 256 | rb_dlinkFindDestroy(blptr, &blacklist_list); |
637c4932 | 257 | rb_free(blptr); |
212380e3 AC |
258 | } |
259 | } | |
260 | ||
261 | void lookup_blacklists(struct Client *client_p) | |
262 | { | |
b2f0da88 | 263 | rb_dlink_node *nptr; |
212380e3 | 264 | |
b2f0da88 | 265 | RB_DLINK_FOREACH(nptr, blacklist_list.head) |
212380e3 AC |
266 | { |
267 | struct Blacklist *blptr = (struct Blacklist *) nptr->data; | |
268 | ||
269 | if (!(blptr->status & CONF_ILLEGAL)) | |
270 | initiate_blacklist_dnsquery(blptr, client_p); | |
271 | } | |
272 | } | |
273 | ||
274 | void abort_blacklist_queries(struct Client *client_p) | |
275 | { | |
637c4932 | 276 | rb_dlink_node *ptr, *next_ptr; |
212380e3 AC |
277 | struct BlacklistClient *blcptr; |
278 | ||
279 | if (client_p->preClient == NULL) | |
280 | return; | |
637c4932 | 281 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->preClient->dnsbl_queries.head) |
212380e3 AC |
282 | { |
283 | blcptr = ptr->data; | |
b2f0da88 | 284 | rb_dlinkDelete(&blcptr->node, &client_p->preClient->dnsbl_queries); |
212380e3 | 285 | unref_blacklist(blcptr->blacklist); |
1d02144f | 286 | cancel_lookup(blcptr->dns_id); |
637c4932 | 287 | rb_free(blcptr); |
212380e3 AC |
288 | } |
289 | } | |
290 | ||
291 | void destroy_blacklists(void) | |
292 | { | |
637c4932 | 293 | rb_dlink_node *ptr, *next_ptr; |
212380e3 AC |
294 | struct Blacklist *blptr; |
295 | ||
637c4932 | 296 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, blacklist_list.head) |
212380e3 AC |
297 | { |
298 | blptr = ptr->data; | |
299 | blptr->hits = 0; /* keep it simple and consistent */ | |
300 | if (blptr->refcount > 0) | |
301 | blptr->status |= CONF_ILLEGAL; | |
302 | else | |
303 | { | |
637c4932 | 304 | rb_free(ptr->data); |
b2f0da88 | 305 | rb_dlinkDestroy(ptr, &blacklist_list); |
212380e3 AC |
306 | } |
307 | } | |
308 | } |