]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* |
2 | * charybdis: A slightly useful ircd. | |
3 | * blacklist.c: Manages DNS blacklist entries and lookups | |
4 | * | |
05ee179e | 5 | * Copyright (C) 2006-2008 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 | * | |
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" | |
212380e3 AC |
39 | #include "numeric.h" |
40 | #include "reject.h" | |
41 | #include "s_conf.h" | |
42 | #include "s_user.h" | |
43 | #include "blacklist.h" | |
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 | ||
63 | static void blacklist_dns_callback(void *vptr, struct DNSReply *reply) | |
64 | { | |
65 | struct BlacklistClient *blcptr = (struct BlacklistClient *) vptr; | |
137d856d | 66 | int listed = 0; |
212380e3 AC |
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)); | |
637c4932 | 75 | rb_free(blcptr); |
212380e3 AC |
76 | return; |
77 | } | |
78 | ||
137d856d JT |
79 | if (reply != NULL) |
80 | { | |
b0c41d32 | 81 | /* only accept 127.x.y.z as a listing */ |
96274734 | 82 | if (reply->addr.ss_family == AF_INET && |
b0c41d32 | 83 | !memcmp(&((struct sockaddr_in *)&reply->addr)->sin_addr, "\177", 1)) |
96274734 | 84 | listed = TRUE; |
e3354945 | 85 | else if (blcptr->blacklist->lastwarning + 3600 < rb_current_time()) |
96274734 JT |
86 | { |
87 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
88 | "Garbage reply from blacklist %s", | |
89 | blcptr->blacklist->host); | |
e3354945 | 90 | blcptr->blacklist->lastwarning = rb_current_time(); |
96274734 | 91 | } |
137d856d JT |
92 | } |
93 | ||
212380e3 | 94 | /* they have a blacklist entry for this client */ |
137d856d | 95 | if (listed && blcptr->client_p->preClient->dnsbl_listed == NULL) |
212380e3 AC |
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 | ||
b2f0da88 | 103 | rb_dlinkDelete(&blcptr->node, &blcptr->client_p->preClient->dnsbl_queries); |
212380e3 AC |
104 | |
105 | /* yes, it can probably happen... */ | |
b2f0da88 | 106 | 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 |
107 | { |
108 | char buf[USERLEN + 1]; | |
f427c8b0 | 109 | rb_strlcpy(buf, blcptr->client_p->username, sizeof buf); |
212380e3 AC |
110 | register_local_user(blcptr->client_p, blcptr->client_p, buf); |
111 | } | |
112 | ||
637c4932 | 113 | rb_free(blcptr); |
212380e3 AC |
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 | { | |
eddc2ab6 | 119 | struct BlacklistClient *blcptr = rb_malloc(sizeof(struct BlacklistClient)); |
db3efb7a | 120 | char buf[IRCD_RES_HOSTLEN + 1]; |
5d21ef50 | 121 | uint8_t *ip; |
212380e3 AC |
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 | ||
5d21ef50 | 129 | ip = (uint8_t *)&((struct sockaddr_in *)&client_p->localClient->ip)->sin_addr.s_addr; |
212380e3 AC |
130 | |
131 | /* becomes 2.0.0.127.torbl.ahbl.org or whatever */ | |
5d21ef50 AC |
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); | |
212380e3 AC |
138 | |
139 | gethost_byname_type(buf, &blcptr->dns_query, T_A); | |
140 | ||
b2f0da88 | 141 | rb_dlinkAdd(blcptr, &blcptr->node, &client_p->preClient->dnsbl_queries); |
212380e3 AC |
142 | blptr->refcount++; |
143 | } | |
144 | ||
145 | /* public interfaces */ | |
146 | struct Blacklist *new_blacklist(char *name, char *reject_reason) | |
147 | { | |
148 | struct Blacklist *blptr; | |
149 | ||
150 | if (name == NULL || reject_reason == NULL) | |
151 | return NULL; | |
152 | ||
153 | blptr = find_blacklist(name); | |
154 | if (blptr == NULL) | |
155 | { | |
eddc2ab6 | 156 | blptr = rb_malloc(sizeof(struct Blacklist)); |
b2f0da88 | 157 | rb_dlinkAddAlloc(blptr, &blacklist_list); |
212380e3 AC |
158 | } |
159 | else | |
160 | blptr->status &= ~CONF_ILLEGAL; | |
db3efb7a | 161 | rb_strlcpy(blptr->host, name, IRCD_RES_HOSTLEN + 1); |
f427c8b0 | 162 | rb_strlcpy(blptr->reject_reason, reject_reason, IRCD_BUFSIZE); |
96274734 | 163 | blptr->lastwarning = 0; |
212380e3 AC |
164 | |
165 | return blptr; | |
166 | } | |
167 | ||
168 | void unref_blacklist(struct Blacklist *blptr) | |
169 | { | |
170 | blptr->refcount--; | |
171 | if (blptr->status & CONF_ILLEGAL && blptr->refcount <= 0) | |
172 | { | |
b2f0da88 | 173 | rb_dlinkFindDestroy(blptr, &blacklist_list); |
637c4932 | 174 | rb_free(blptr); |
212380e3 AC |
175 | } |
176 | } | |
177 | ||
178 | void lookup_blacklists(struct Client *client_p) | |
179 | { | |
b2f0da88 | 180 | rb_dlink_node *nptr; |
212380e3 AC |
181 | |
182 | /* We don't do IPv6 right now, sorry! */ | |
183 | if (client_p->localClient->ip.ss_family == AF_INET6) | |
184 | return; | |
185 | ||
b2f0da88 | 186 | RB_DLINK_FOREACH(nptr, blacklist_list.head) |
212380e3 AC |
187 | { |
188 | struct Blacklist *blptr = (struct Blacklist *) nptr->data; | |
189 | ||
190 | if (!(blptr->status & CONF_ILLEGAL)) | |
191 | initiate_blacklist_dnsquery(blptr, client_p); | |
192 | } | |
193 | } | |
194 | ||
195 | void abort_blacklist_queries(struct Client *client_p) | |
196 | { | |
637c4932 | 197 | rb_dlink_node *ptr, *next_ptr; |
212380e3 AC |
198 | struct BlacklistClient *blcptr; |
199 | ||
200 | if (client_p->preClient == NULL) | |
201 | return; | |
637c4932 | 202 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->preClient->dnsbl_queries.head) |
212380e3 AC |
203 | { |
204 | blcptr = ptr->data; | |
b2f0da88 | 205 | rb_dlinkDelete(&blcptr->node, &client_p->preClient->dnsbl_queries); |
212380e3 AC |
206 | unref_blacklist(blcptr->blacklist); |
207 | delete_resolver_queries(&blcptr->dns_query); | |
637c4932 | 208 | rb_free(blcptr); |
212380e3 AC |
209 | } |
210 | } | |
211 | ||
212 | void destroy_blacklists(void) | |
213 | { | |
637c4932 | 214 | rb_dlink_node *ptr, *next_ptr; |
212380e3 AC |
215 | struct Blacklist *blptr; |
216 | ||
637c4932 | 217 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, blacklist_list.head) |
212380e3 AC |
218 | { |
219 | blptr = ptr->data; | |
220 | blptr->hits = 0; /* keep it simple and consistent */ | |
221 | if (blptr->refcount > 0) | |
222 | blptr->status |= CONF_ILLEGAL; | |
223 | else | |
224 | { | |
637c4932 | 225 | rb_free(ptr->data); |
b2f0da88 | 226 | rb_dlinkDestroy(ptr, &blacklist_list); |
212380e3 AC |
227 | } |
228 | } | |
229 | } |