]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* |
2 | * charybdis: A slightly useful ircd. | |
3 | * blacklist.c: Manages DNS blacklist entries and lookups | |
4 | * | |
5 | * Copyright (C) 2006 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 "tools.h" | |
40 | #include "memory.h" | |
41 | #include "numeric.h" | |
42 | #include "reject.h" | |
43 | #include "s_conf.h" | |
44 | #include "s_user.h" | |
45 | #include "blacklist.h" | |
46 | ||
47 | dlink_list blacklist_list = { NULL, NULL, 0 }; | |
48 | ||
49 | /* private interfaces */ | |
50 | static struct Blacklist *find_blacklist(char *name) | |
51 | { | |
52 | dlink_node *nptr; | |
53 | ||
54 | DLINK_FOREACH(nptr, blacklist_list.head) | |
55 | { | |
56 | struct Blacklist *blptr = (struct Blacklist *) nptr->data; | |
57 | ||
58 | if (!irccmp(blptr->host, name)) | |
59 | return blptr; | |
60 | } | |
61 | ||
62 | return NULL; | |
63 | } | |
64 | ||
65 | static void blacklist_dns_callback(void *vptr, struct DNSReply *reply) | |
66 | { | |
67 | struct BlacklistClient *blcptr = (struct BlacklistClient *) vptr; | |
137d856d | 68 | int listed = 0; |
212380e3 AC |
69 | |
70 | if (blcptr == NULL || blcptr->client_p == NULL) | |
71 | return; | |
72 | ||
73 | if (blcptr->client_p->preClient == NULL) | |
74 | { | |
75 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
76 | "blacklist_dns_callback(): blcptr->client_p->preClient (%s) is NULL", get_client_name(blcptr->client_p, HIDE_IP)); | |
77 | MyFree(blcptr); | |
78 | return; | |
79 | } | |
80 | ||
137d856d JT |
81 | if (reply != NULL) |
82 | { | |
83 | /* only accept 127.0.0.x as a listing */ | |
84 | listed = reply->addr.ss_family == AF_INET && | |
85 | !memcmp(&((struct sockaddr_in *)&reply->addr)->sin_addr, "\177\0\0", 3); | |
86 | } | |
87 | ||
212380e3 | 88 | /* they have a blacklist entry for this client */ |
137d856d | 89 | if (listed && blcptr->client_p->preClient->dnsbl_listed == NULL) |
212380e3 AC |
90 | { |
91 | blcptr->client_p->preClient->dnsbl_listed = blcptr->blacklist; | |
92 | /* reference to blacklist moves from blcptr to client_p->preClient... */ | |
93 | } | |
94 | else | |
95 | unref_blacklist(blcptr->blacklist); | |
96 | ||
97 | dlinkDelete(&blcptr->node, &blcptr->client_p->preClient->dnsbl_queries); | |
98 | ||
99 | /* yes, it can probably happen... */ | |
100 | if (dlink_list_length(&blcptr->client_p->preClient->dnsbl_queries) == 0 && blcptr->client_p->flags & FLAGS_SENTUSER && !EmptyString(blcptr->client_p->name)) | |
101 | { | |
102 | char buf[USERLEN + 1]; | |
103 | strlcpy(buf, blcptr->client_p->username, sizeof buf); | |
104 | register_local_user(blcptr->client_p, blcptr->client_p, buf); | |
105 | } | |
106 | ||
107 | MyFree(blcptr); | |
108 | } | |
109 | ||
110 | /* XXX: no IPv6 implementation, not to concerned right now though. */ | |
111 | static void initiate_blacklist_dnsquery(struct Blacklist *blptr, struct Client *client_p) | |
112 | { | |
113 | struct BlacklistClient *blcptr = MyMalloc(sizeof(struct BlacklistClient)); | |
114 | char buf[IRCD_BUFSIZE]; | |
115 | int ip[4]; | |
116 | ||
117 | blcptr->blacklist = blptr; | |
118 | blcptr->client_p = client_p; | |
119 | ||
120 | blcptr->dns_query.ptr = blcptr; | |
121 | blcptr->dns_query.callback = blacklist_dns_callback; | |
122 | ||
123 | /* XXX: yes I know this is bad, I don't really care right now */ | |
124 | sscanf(client_p->sockhost, "%d.%d.%d.%d", &ip[3], &ip[2], &ip[1], &ip[0]); | |
125 | ||
126 | /* becomes 2.0.0.127.torbl.ahbl.org or whatever */ | |
127 | ircsnprintf(buf, IRCD_BUFSIZE, "%d.%d.%d.%d.%s", ip[0], ip[1], ip[2], ip[3], blptr->host); | |
128 | ||
129 | gethost_byname_type(buf, &blcptr->dns_query, T_A); | |
130 | ||
131 | dlinkAdd(blcptr, &blcptr->node, &client_p->preClient->dnsbl_queries); | |
132 | blptr->refcount++; | |
133 | } | |
134 | ||
135 | /* public interfaces */ | |
136 | struct Blacklist *new_blacklist(char *name, char *reject_reason) | |
137 | { | |
138 | struct Blacklist *blptr; | |
139 | ||
140 | if (name == NULL || reject_reason == NULL) | |
141 | return NULL; | |
142 | ||
143 | blptr = find_blacklist(name); | |
144 | if (blptr == NULL) | |
145 | { | |
146 | blptr = MyMalloc(sizeof(struct Blacklist)); | |
147 | dlinkAddAlloc(blptr, &blacklist_list); | |
148 | } | |
149 | else | |
150 | blptr->status &= ~CONF_ILLEGAL; | |
151 | strlcpy(blptr->host, name, HOSTLEN); | |
152 | strlcpy(blptr->reject_reason, reject_reason, IRCD_BUFSIZE); | |
153 | ||
154 | return blptr; | |
155 | } | |
156 | ||
157 | void unref_blacklist(struct Blacklist *blptr) | |
158 | { | |
159 | blptr->refcount--; | |
160 | if (blptr->status & CONF_ILLEGAL && blptr->refcount <= 0) | |
161 | { | |
162 | dlinkFindDestroy(blptr, &blacklist_list); | |
163 | MyFree(blptr); | |
164 | } | |
165 | } | |
166 | ||
167 | void lookup_blacklists(struct Client *client_p) | |
168 | { | |
169 | dlink_node *nptr; | |
170 | ||
171 | /* We don't do IPv6 right now, sorry! */ | |
172 | if (client_p->localClient->ip.ss_family == AF_INET6) | |
173 | return; | |
174 | ||
175 | DLINK_FOREACH(nptr, blacklist_list.head) | |
176 | { | |
177 | struct Blacklist *blptr = (struct Blacklist *) nptr->data; | |
178 | ||
179 | if (!(blptr->status & CONF_ILLEGAL)) | |
180 | initiate_blacklist_dnsquery(blptr, client_p); | |
181 | } | |
182 | } | |
183 | ||
184 | void abort_blacklist_queries(struct Client *client_p) | |
185 | { | |
186 | dlink_node *ptr, *next_ptr; | |
187 | struct BlacklistClient *blcptr; | |
188 | ||
189 | if (client_p->preClient == NULL) | |
190 | return; | |
191 | DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->preClient->dnsbl_queries.head) | |
192 | { | |
193 | blcptr = ptr->data; | |
194 | dlinkDelete(&blcptr->node, &client_p->preClient->dnsbl_queries); | |
195 | unref_blacklist(blcptr->blacklist); | |
196 | delete_resolver_queries(&blcptr->dns_query); | |
197 | MyFree(blcptr); | |
198 | } | |
199 | } | |
200 | ||
201 | void destroy_blacklists(void) | |
202 | { | |
203 | dlink_node *ptr, *next_ptr; | |
204 | struct Blacklist *blptr; | |
205 | ||
206 | DLINK_FOREACH_SAFE(ptr, next_ptr, blacklist_list.head) | |
207 | { | |
208 | blptr = ptr->data; | |
209 | blptr->hits = 0; /* keep it simple and consistent */ | |
210 | if (blptr->refcount > 0) | |
211 | blptr->status |= CONF_ILLEGAL; | |
212 | else | |
213 | { | |
214 | MyFree(ptr->data); | |
215 | dlinkDestroy(ptr, &blacklist_list); | |
216 | } | |
217 | } | |
218 | } |