]>
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" | |
37 | #include "res.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" | |
43 | ||
b2f0da88 | 44 | rb_dlink_list blacklist_list = { NULL, NULL, 0 }; |
212380e3 AC |
45 | |
46 | /* private interfaces */ | |
47 | static struct Blacklist *find_blacklist(char *name) | |
48 | { | |
b2f0da88 | 49 | rb_dlink_node *nptr; |
212380e3 | 50 | |
b2f0da88 | 51 | RB_DLINK_FOREACH(nptr, blacklist_list.head) |
212380e3 AC |
52 | { |
53 | struct Blacklist *blptr = (struct Blacklist *) nptr->data; | |
54 | ||
55 | if (!irccmp(blptr->host, name)) | |
56 | return blptr; | |
57 | } | |
58 | ||
59 | return NULL; | |
60 | } | |
61 | ||
62 | static void blacklist_dns_callback(void *vptr, struct DNSReply *reply) | |
63 | { | |
64 | struct BlacklistClient *blcptr = (struct BlacklistClient *) vptr; | |
137d856d | 65 | int listed = 0; |
212380e3 AC |
66 | |
67 | if (blcptr == NULL || blcptr->client_p == NULL) | |
68 | return; | |
69 | ||
70 | if (blcptr->client_p->preClient == NULL) | |
71 | { | |
72 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
73 | "blacklist_dns_callback(): blcptr->client_p->preClient (%s) is NULL", get_client_name(blcptr->client_p, HIDE_IP)); | |
637c4932 | 74 | rb_free(blcptr); |
212380e3 AC |
75 | return; |
76 | } | |
77 | ||
137d856d JT |
78 | if (reply != NULL) |
79 | { | |
b0c41d32 | 80 | /* only accept 127.x.y.z as a listing */ |
96274734 | 81 | if (reply->addr.ss_family == AF_INET && |
b0c41d32 | 82 | !memcmp(&((struct sockaddr_in *)&reply->addr)->sin_addr, "\177", 1)) |
96274734 | 83 | listed = TRUE; |
e3354945 | 84 | else if (blcptr->blacklist->lastwarning + 3600 < rb_current_time()) |
96274734 JT |
85 | { |
86 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
87 | "Garbage reply from blacklist %s", | |
88 | blcptr->blacklist->host); | |
e3354945 | 89 | blcptr->blacklist->lastwarning = rb_current_time(); |
96274734 | 90 | } |
137d856d JT |
91 | } |
92 | ||
212380e3 | 93 | /* they have a blacklist entry for this client */ |
137d856d | 94 | if (listed && blcptr->client_p->preClient->dnsbl_listed == NULL) |
212380e3 AC |
95 | { |
96 | blcptr->client_p->preClient->dnsbl_listed = blcptr->blacklist; | |
97 | /* reference to blacklist moves from blcptr to client_p->preClient... */ | |
98 | } | |
99 | else | |
100 | unref_blacklist(blcptr->blacklist); | |
101 | ||
b2f0da88 | 102 | rb_dlinkDelete(&blcptr->node, &blcptr->client_p->preClient->dnsbl_queries); |
212380e3 AC |
103 | |
104 | /* yes, it can probably happen... */ | |
b2f0da88 | 105 | if (rb_dlink_list_length(&blcptr->client_p->preClient->dnsbl_queries) == 0 && blcptr->client_p->flags & FLAGS_SENTUSER && !EmptyString(blcptr->client_p->name)) |
212380e3 AC |
106 | { |
107 | char buf[USERLEN + 1]; | |
f427c8b0 | 108 | rb_strlcpy(buf, blcptr->client_p->username, sizeof buf); |
212380e3 AC |
109 | register_local_user(blcptr->client_p, blcptr->client_p, buf); |
110 | } | |
111 | ||
637c4932 | 112 | rb_free(blcptr); |
212380e3 AC |
113 | } |
114 | ||
212380e3 AC |
115 | static void initiate_blacklist_dnsquery(struct Blacklist *blptr, struct Client *client_p) |
116 | { | |
eddc2ab6 | 117 | struct BlacklistClient *blcptr = rb_malloc(sizeof(struct BlacklistClient)); |
db3efb7a | 118 | char buf[IRCD_RES_HOSTLEN + 1]; |
5d21ef50 | 119 | uint8_t *ip; |
212380e3 AC |
120 | |
121 | blcptr->blacklist = blptr; | |
122 | blcptr->client_p = client_p; | |
123 | ||
124 | blcptr->dns_query.ptr = blcptr; | |
125 | blcptr->dns_query.callback = blacklist_dns_callback; | |
126 | ||
0a1e77c2 EM |
127 | if ((client_p->localClient->ip.ss_family == AF_INET) && blptr->ipv4) |
128 | { | |
129 | ip = (uint8_t *)&((struct sockaddr_in *)&client_p->localClient->ip)->sin_addr.s_addr; | |
130 | ||
131 | /* becomes 2.0.0.127.torbl.ahbl.org or whatever */ | |
132 | rb_snprintf(buf, sizeof buf, "%d.%d.%d.%d.%s", | |
133 | (unsigned int) ip[3], | |
134 | (unsigned int) ip[2], | |
135 | (unsigned int) ip[1], | |
136 | (unsigned int) ip[0], | |
137 | blptr->host); | |
138 | } | |
139 | /* IPv6 is supported now. --Elizabeth */ | |
140 | else if ((client_p->localClient->ip.ss_family == AF_INET6) && blptr->ipv6) | |
141 | { | |
142 | /* Breaks it into ip[0] = 0x00, ip[1] = 0x00... ip[16] = 0x01 for localhost | |
143 | * I wish there was a uint4_t for C, this would make the below cleaner, but... */ | |
144 | ip = (uint8_t *)&((struct sockaddr_in6 *)&client_p->localClient->ip)->sin6_addr.s6_addr; | |
145 | char *bufptr = buf; | |
146 | int i; | |
147 | ||
148 | /* The below will give us something like | |
149 | * 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 | |
150 | */ | |
151 | ||
152 | /* Going backwards */ | |
153 | for (i = 15; i >= 0; i--) | |
154 | { | |
155 | /* Get upper and lower nibbles (yes this works fine on | |
156 | * both big and little endian) */ | |
157 | uint8_t hi = (ip[i] >> 4) & 0x0F; | |
158 | uint8_t lo = ip[i] & 0x0F; | |
159 | ||
160 | /* One part... (why 5? rb_snprintf adds \0) */ | |
161 | rb_snprintf(bufptr, 5, "%1x.%1x.", | |
162 | (unsigned int) lo, /* Remember, backwards */ | |
163 | (unsigned int) hi); | |
164 | ||
165 | /* Lurch forward to next position */ | |
166 | bufptr += 4; | |
167 | } | |
212380e3 | 168 | |
0a1e77c2 EM |
169 | /* Tack host on */ |
170 | strcpy(bufptr, blptr->host); | |
171 | } | |
172 | /* This shouldn't happen... */ | |
173 | else | |
174 | return; | |
212380e3 AC |
175 | |
176 | gethost_byname_type(buf, &blcptr->dns_query, T_A); | |
177 | ||
b2f0da88 | 178 | rb_dlinkAdd(blcptr, &blcptr->node, &client_p->preClient->dnsbl_queries); |
212380e3 AC |
179 | blptr->refcount++; |
180 | } | |
181 | ||
182 | /* public interfaces */ | |
0a1e77c2 | 183 | struct Blacklist *new_blacklist(char *name, char *reject_reason, int ipv4, int ipv6) |
212380e3 AC |
184 | { |
185 | struct Blacklist *blptr; | |
186 | ||
187 | if (name == NULL || reject_reason == NULL) | |
188 | return NULL; | |
189 | ||
190 | blptr = find_blacklist(name); | |
191 | if (blptr == NULL) | |
192 | { | |
eddc2ab6 | 193 | blptr = rb_malloc(sizeof(struct Blacklist)); |
b2f0da88 | 194 | rb_dlinkAddAlloc(blptr, &blacklist_list); |
212380e3 AC |
195 | } |
196 | else | |
197 | blptr->status &= ~CONF_ILLEGAL; | |
db3efb7a | 198 | rb_strlcpy(blptr->host, name, IRCD_RES_HOSTLEN + 1); |
f427c8b0 | 199 | rb_strlcpy(blptr->reject_reason, reject_reason, IRCD_BUFSIZE); |
0a1e77c2 EM |
200 | blptr->ipv4 = ipv4; |
201 | blptr->ipv6 = ipv6; | |
96274734 | 202 | blptr->lastwarning = 0; |
212380e3 AC |
203 | |
204 | return blptr; | |
205 | } | |
206 | ||
207 | void unref_blacklist(struct Blacklist *blptr) | |
208 | { | |
209 | blptr->refcount--; | |
210 | if (blptr->status & CONF_ILLEGAL && blptr->refcount <= 0) | |
211 | { | |
b2f0da88 | 212 | rb_dlinkFindDestroy(blptr, &blacklist_list); |
637c4932 | 213 | rb_free(blptr); |
212380e3 AC |
214 | } |
215 | } | |
216 | ||
217 | void lookup_blacklists(struct Client *client_p) | |
218 | { | |
b2f0da88 | 219 | rb_dlink_node *nptr; |
212380e3 | 220 | |
b2f0da88 | 221 | RB_DLINK_FOREACH(nptr, blacklist_list.head) |
212380e3 AC |
222 | { |
223 | struct Blacklist *blptr = (struct Blacklist *) nptr->data; | |
224 | ||
225 | if (!(blptr->status & CONF_ILLEGAL)) | |
226 | initiate_blacklist_dnsquery(blptr, client_p); | |
227 | } | |
228 | } | |
229 | ||
230 | void abort_blacklist_queries(struct Client *client_p) | |
231 | { | |
637c4932 | 232 | rb_dlink_node *ptr, *next_ptr; |
212380e3 AC |
233 | struct BlacklistClient *blcptr; |
234 | ||
235 | if (client_p->preClient == NULL) | |
236 | return; | |
637c4932 | 237 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->preClient->dnsbl_queries.head) |
212380e3 AC |
238 | { |
239 | blcptr = ptr->data; | |
b2f0da88 | 240 | rb_dlinkDelete(&blcptr->node, &client_p->preClient->dnsbl_queries); |
212380e3 AC |
241 | unref_blacklist(blcptr->blacklist); |
242 | delete_resolver_queries(&blcptr->dns_query); | |
637c4932 | 243 | rb_free(blcptr); |
212380e3 AC |
244 | } |
245 | } | |
246 | ||
247 | void destroy_blacklists(void) | |
248 | { | |
637c4932 | 249 | rb_dlink_node *ptr, *next_ptr; |
212380e3 AC |
250 | struct Blacklist *blptr; |
251 | ||
637c4932 | 252 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, blacklist_list.head) |
212380e3 AC |
253 | { |
254 | blptr = ptr->data; | |
255 | blptr->hits = 0; /* keep it simple and consistent */ | |
256 | if (blptr->refcount > 0) | |
257 | blptr->status |= CONF_ILLEGAL; | |
258 | else | |
259 | { | |
637c4932 | 260 | rb_free(ptr->data); |
b2f0da88 | 261 | rb_dlinkDestroy(ptr, &blacklist_list); |
212380e3 AC |
262 | } |
263 | } | |
264 | } |