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