]> jfr.im git - irc/rqf/shadowircd.git/blame - src/blacklist.c
Fix VERSION and RPL_MYINFO to only show modes that are enabled. Also fix up some...
[irc/rqf/shadowircd.git] / src / blacklist.c
CommitLineData
212380e3 1/*
2 * charybdis: A slightly useful ircd.
3 * blacklist.c: Manages DNS blacklist entries and lookups
4 *
eb6e811f 5 * Copyright (C) 2006-2008 charybdis development team
212380e3 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 33 */
34
35#include "stdinc.h"
36#include "client.h"
37#include "res.h"
212380e3 38#include "numeric.h"
39#include "reject.h"
40#include "s_conf.h"
41#include "s_user.h"
42#include "blacklist.h"
43
38e6acdd 44rb_dlink_list blacklist_list = { NULL, NULL, 0 };
212380e3 45
46/* private interfaces */
47static struct Blacklist *find_blacklist(char *name)
48{
38e6acdd 49 rb_dlink_node *nptr;
212380e3 50
38e6acdd 51 RB_DLINK_FOREACH(nptr, blacklist_list.head)
212380e3 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
62static void blacklist_dns_callback(void *vptr, struct DNSReply *reply)
63{
64 struct BlacklistClient *blcptr = (struct BlacklistClient *) vptr;
bc71fb3f 65 int listed = 0;
212380e3 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));
90a3c35b 74 rb_free(blcptr);
212380e3 75 return;
76 }
77
bc71fb3f
JT
78 if (reply != NULL)
79 {
9a9f3cf8 80 /* only accept 127.x.y.z as a listing */
c41a85df 81 if (reply->addr.ss_family == AF_INET &&
9a9f3cf8 82 !memcmp(&((struct sockaddr_in *)&reply->addr)->sin_addr, "\177", 1))
c41a85df 83 listed = TRUE;
9f6bbe3c 84 else if (blcptr->blacklist->lastwarning + 3600 < rb_current_time())
c41a85df
JT
85 {
86 sendto_realops_snomask(SNO_GENERAL, L_ALL,
87 "Garbage reply from blacklist %s",
88 blcptr->blacklist->host);
9f6bbe3c 89 blcptr->blacklist->lastwarning = rb_current_time();
c41a85df 90 }
bc71fb3f
JT
91 }
92
212380e3 93 /* they have a blacklist entry for this client */
bc71fb3f 94 if (listed && blcptr->client_p->preClient->dnsbl_listed == NULL)
212380e3 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
38e6acdd 102 rb_dlinkDelete(&blcptr->node, &blcptr->client_p->preClient->dnsbl_queries);
212380e3 103
104 /* yes, it can probably happen... */
38e6acdd 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 106 {
107 char buf[USERLEN + 1];
907468c4 108 rb_strlcpy(buf, blcptr->client_p->username, sizeof buf);
212380e3 109 register_local_user(blcptr->client_p, blcptr->client_p, buf);
110 }
111
90a3c35b 112 rb_free(blcptr);
212380e3 113}
114
115/* XXX: no IPv6 implementation, not to concerned right now though. */
116static void initiate_blacklist_dnsquery(struct Blacklist *blptr, struct Client *client_p)
117{
8e43b0b4 118 struct BlacklistClient *blcptr = rb_malloc(sizeof(struct BlacklistClient));
570ca08a 119 char buf[IRCD_RES_HOSTLEN + 1];
212380e3 120 int ip[4];
121
122 blcptr->blacklist = blptr;
123 blcptr->client_p = client_p;
124
125 blcptr->dns_query.ptr = blcptr;
126 blcptr->dns_query.callback = blacklist_dns_callback;
127
128 /* XXX: yes I know this is bad, I don't really care right now */
129 sscanf(client_p->sockhost, "%d.%d.%d.%d", &ip[3], &ip[2], &ip[1], &ip[0]);
130
131 /* becomes 2.0.0.127.torbl.ahbl.org or whatever */
570ca08a 132 rb_snprintf(buf, sizeof buf, "%d.%d.%d.%d.%s", ip[0], ip[1], ip[2], ip[3], blptr->host);
212380e3 133
134 gethost_byname_type(buf, &blcptr->dns_query, T_A);
135
38e6acdd 136 rb_dlinkAdd(blcptr, &blcptr->node, &client_p->preClient->dnsbl_queries);
212380e3 137 blptr->refcount++;
138}
139
140/* public interfaces */
141struct Blacklist *new_blacklist(char *name, char *reject_reason)
142{
143 struct Blacklist *blptr;
144
145 if (name == NULL || reject_reason == NULL)
146 return NULL;
147
148 blptr = find_blacklist(name);
149 if (blptr == NULL)
150 {
8e43b0b4 151 blptr = rb_malloc(sizeof(struct Blacklist));
38e6acdd 152 rb_dlinkAddAlloc(blptr, &blacklist_list);
212380e3 153 }
154 else
155 blptr->status &= ~CONF_ILLEGAL;
570ca08a 156 rb_strlcpy(blptr->host, name, IRCD_RES_HOSTLEN + 1);
907468c4 157 rb_strlcpy(blptr->reject_reason, reject_reason, IRCD_BUFSIZE);
c41a85df 158 blptr->lastwarning = 0;
212380e3 159
160 return blptr;
161}
162
163void unref_blacklist(struct Blacklist *blptr)
164{
165 blptr->refcount--;
166 if (blptr->status & CONF_ILLEGAL && blptr->refcount <= 0)
167 {
38e6acdd 168 rb_dlinkFindDestroy(blptr, &blacklist_list);
90a3c35b 169 rb_free(blptr);
212380e3 170 }
171}
172
173void lookup_blacklists(struct Client *client_p)
174{
38e6acdd 175 rb_dlink_node *nptr;
212380e3 176
177 /* We don't do IPv6 right now, sorry! */
178 if (client_p->localClient->ip.ss_family == AF_INET6)
179 return;
180
38e6acdd 181 RB_DLINK_FOREACH(nptr, blacklist_list.head)
212380e3 182 {
183 struct Blacklist *blptr = (struct Blacklist *) nptr->data;
184
185 if (!(blptr->status & CONF_ILLEGAL))
186 initiate_blacklist_dnsquery(blptr, client_p);
187 }
188}
189
190void abort_blacklist_queries(struct Client *client_p)
191{
90a3c35b 192 rb_dlink_node *ptr, *next_ptr;
212380e3 193 struct BlacklistClient *blcptr;
194
195 if (client_p->preClient == NULL)
196 return;
90a3c35b 197 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->preClient->dnsbl_queries.head)
212380e3 198 {
199 blcptr = ptr->data;
38e6acdd 200 rb_dlinkDelete(&blcptr->node, &client_p->preClient->dnsbl_queries);
212380e3 201 unref_blacklist(blcptr->blacklist);
202 delete_resolver_queries(&blcptr->dns_query);
90a3c35b 203 rb_free(blcptr);
212380e3 204 }
205}
206
207void destroy_blacklists(void)
208{
90a3c35b 209 rb_dlink_node *ptr, *next_ptr;
212380e3 210 struct Blacklist *blptr;
211
90a3c35b 212 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, blacklist_list.head)
212380e3 213 {
214 blptr = ptr->data;
215 blptr->hits = 0; /* keep it simple and consistent */
216 if (blptr->refcount > 0)
217 blptr->status |= CONF_ILLEGAL;
218 else
219 {
90a3c35b 220 rb_free(ptr->data);
38e6acdd 221 rb_dlinkDestroy(ptr, &blacklist_list);
212380e3 222 }
223 }
224}