]> jfr.im git - solanum.git/blob - ircd/blacklist.c
extensions/helpops: implement DEHELPER command
[solanum.git] / ircd / blacklist.c
1 /*
2 * charybdis: A slightly useful ircd.
3 * blacklist.c: Manages DNS blacklist entries and lookups
4 *
5 * Copyright (C) 2006-2011 charybdis development team
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 *
33 */
34
35 #include "stdinc.h"
36 #include "client.h"
37 #include "dns.h"
38 #include "numeric.h"
39 #include "reject.h"
40 #include "s_conf.h"
41 #include "s_user.h"
42 #include "blacklist.h"
43 #include "send.h"
44
45 rb_dlink_list blacklist_list = { NULL, NULL, 0 };
46
47 /* private interfaces */
48 static struct Blacklist *find_blacklist(char *name)
49 {
50 rb_dlink_node *nptr;
51
52 RB_DLINK_FOREACH(nptr, blacklist_list.head)
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
63 static inline int blacklist_check_reply(struct BlacklistClient *blcptr, const char *ipaddr)
64 {
65 struct Blacklist *blptr = blcptr->blacklist;
66 const char *lastoctet;
67 rb_dlink_node *ptr;
68
69 /* No filters and entry found - thus positive match */
70 if (!rb_dlink_list_length(&blptr->filters))
71 return 1;
72
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;
80 const char *cmpstr;
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
110 static void blacklist_dns_callback(const char *result, int status, int aftype, void *vptr)
111 {
112 struct BlacklistClient *blcptr = (struct BlacklistClient *) vptr;
113 int listed = 0;
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));
122 rb_free(blcptr);
123 return;
124 }
125
126 if (result != NULL && status)
127 {
128 if (blacklist_check_reply(blcptr, result))
129 listed = TRUE;
130 }
131
132 /* they have a blacklist entry for this client */
133 if (listed && blcptr->client_p->preClient->dnsbl_listed == NULL)
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
141 rb_dlinkDelete(&blcptr->node, &blcptr->client_p->preClient->dnsbl_queries);
142
143 /* yes, it can probably happen... */
144 if (rb_dlink_list_length(&blcptr->client_p->preClient->dnsbl_queries) == 0 && blcptr->client_p->flags & FLAGS_SENTUSER && !EmptyString(blcptr->client_p->name))
145 register_local_user(blcptr->client_p, blcptr->client_p);
146
147 rb_free(blcptr);
148 }
149
150 static void initiate_blacklist_dnsquery(struct Blacklist *blptr, struct Client *client_p)
151 {
152 struct BlacklistClient *blcptr = rb_malloc(sizeof(struct BlacklistClient));
153 char buf[IRCD_RES_HOSTLEN + 1];
154 uint8_t *ip;
155
156 blcptr->blacklist = blptr;
157 blcptr->client_p = client_p;
158
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 }
200
201 /* Tack host on */
202 strcpy(bufptr, blptr->host);
203 }
204 /* This shouldn't happen... */
205 else
206 return;
207
208 blcptr->dns_id = lookup_hostname(buf, AF_INET, blacklist_dns_callback, blcptr);
209
210 rb_dlinkAdd(blcptr, &blcptr->node, &client_p->preClient->dnsbl_queries);
211 blptr->refcount++;
212 }
213
214 /* public interfaces */
215 struct Blacklist *new_blacklist(char *name, char *reject_reason, int ipv4, int ipv6, rb_dlink_list *filters)
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 {
225 blptr = rb_malloc(sizeof(struct Blacklist));
226 rb_dlinkAddAlloc(blptr, &blacklist_list);
227 }
228 else
229 blptr->status &= ~CONF_ILLEGAL;
230
231 rb_strlcpy(blptr->host, name, IRCD_RES_HOSTLEN + 1);
232 rb_strlcpy(blptr->reject_reason, reject_reason, IRCD_BUFSIZE);
233 blptr->ipv4 = ipv4;
234 blptr->ipv6 = ipv6;
235
236 rb_dlinkMoveList(filters, &blptr->filters);
237
238 blptr->lastwarning = 0;
239
240 return blptr;
241 }
242
243 void unref_blacklist(struct Blacklist *blptr)
244 {
245 rb_dlink_node *ptr, *next_ptr;
246
247 blptr->refcount--;
248 if (blptr->status & CONF_ILLEGAL && blptr->refcount <= 0)
249 {
250 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, blptr->filters.head)
251 {
252 rb_dlinkDelete(ptr, &blptr->filters);
253 rb_free(ptr);
254 }
255
256 rb_dlinkFindDestroy(blptr, &blacklist_list);
257 rb_free(blptr);
258 }
259 }
260
261 void lookup_blacklists(struct Client *client_p)
262 {
263 rb_dlink_node *nptr;
264
265 RB_DLINK_FOREACH(nptr, blacklist_list.head)
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 {
276 rb_dlink_node *ptr, *next_ptr;
277 struct BlacklistClient *blcptr;
278
279 if (client_p->preClient == NULL)
280 return;
281 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->preClient->dnsbl_queries.head)
282 {
283 blcptr = ptr->data;
284 rb_dlinkDelete(&blcptr->node, &client_p->preClient->dnsbl_queries);
285 unref_blacklist(blcptr->blacklist);
286 cancel_lookup(blcptr->dns_id);
287 rb_free(blcptr);
288 }
289 }
290
291 void destroy_blacklists(void)
292 {
293 rb_dlink_node *ptr, *next_ptr;
294 struct Blacklist *blptr;
295
296 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, blacklist_list.head)
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 {
304 rb_free(ptr->data);
305 rb_dlinkDestroy(ptr, &blacklist_list);
306 }
307 }
308 }