]>
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; | |
68 | ||
69 | if (blcptr == NULL || blcptr->client_p == NULL) | |
70 | return; | |
71 | ||
72 | if (blcptr->client_p->preClient == NULL) | |
73 | { | |
74 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
75 | "blacklist_dns_callback(): blcptr->client_p->preClient (%s) is NULL", get_client_name(blcptr->client_p, HIDE_IP)); | |
76 | MyFree(blcptr); | |
77 | return; | |
78 | } | |
79 | ||
80 | /* they have a blacklist entry for this client */ | |
81 | if (reply != NULL && blcptr->client_p->preClient->dnsbl_listed == NULL) | |
82 | { | |
83 | blcptr->client_p->preClient->dnsbl_listed = blcptr->blacklist; | |
84 | /* reference to blacklist moves from blcptr to client_p->preClient... */ | |
85 | } | |
86 | else | |
87 | unref_blacklist(blcptr->blacklist); | |
88 | ||
89 | dlinkDelete(&blcptr->node, &blcptr->client_p->preClient->dnsbl_queries); | |
90 | ||
91 | /* yes, it can probably happen... */ | |
92 | if (dlink_list_length(&blcptr->client_p->preClient->dnsbl_queries) == 0 && blcptr->client_p->flags & FLAGS_SENTUSER && !EmptyString(blcptr->client_p->name)) | |
93 | { | |
94 | char buf[USERLEN + 1]; | |
95 | strlcpy(buf, blcptr->client_p->username, sizeof buf); | |
96 | register_local_user(blcptr->client_p, blcptr->client_p, buf); | |
97 | } | |
98 | ||
99 | MyFree(blcptr); | |
100 | } | |
101 | ||
102 | /* XXX: no IPv6 implementation, not to concerned right now though. */ | |
103 | static void initiate_blacklist_dnsquery(struct Blacklist *blptr, struct Client *client_p) | |
104 | { | |
105 | struct BlacklistClient *blcptr = MyMalloc(sizeof(struct BlacklistClient)); | |
106 | char buf[IRCD_BUFSIZE]; | |
107 | int ip[4]; | |
108 | ||
109 | blcptr->blacklist = blptr; | |
110 | blcptr->client_p = client_p; | |
111 | ||
112 | blcptr->dns_query.ptr = blcptr; | |
113 | blcptr->dns_query.callback = blacklist_dns_callback; | |
114 | ||
115 | /* XXX: yes I know this is bad, I don't really care right now */ | |
116 | sscanf(client_p->sockhost, "%d.%d.%d.%d", &ip[3], &ip[2], &ip[1], &ip[0]); | |
117 | ||
118 | /* becomes 2.0.0.127.torbl.ahbl.org or whatever */ | |
119 | ircsnprintf(buf, IRCD_BUFSIZE, "%d.%d.%d.%d.%s", ip[0], ip[1], ip[2], ip[3], blptr->host); | |
120 | ||
121 | gethost_byname_type(buf, &blcptr->dns_query, T_A); | |
122 | ||
123 | dlinkAdd(blcptr, &blcptr->node, &client_p->preClient->dnsbl_queries); | |
124 | blptr->refcount++; | |
125 | } | |
126 | ||
127 | /* public interfaces */ | |
128 | struct Blacklist *new_blacklist(char *name, char *reject_reason) | |
129 | { | |
130 | struct Blacklist *blptr; | |
131 | ||
132 | if (name == NULL || reject_reason == NULL) | |
133 | return NULL; | |
134 | ||
135 | blptr = find_blacklist(name); | |
136 | if (blptr == NULL) | |
137 | { | |
138 | blptr = MyMalloc(sizeof(struct Blacklist)); | |
139 | dlinkAddAlloc(blptr, &blacklist_list); | |
140 | } | |
141 | else | |
142 | blptr->status &= ~CONF_ILLEGAL; | |
143 | strlcpy(blptr->host, name, HOSTLEN); | |
144 | strlcpy(blptr->reject_reason, reject_reason, IRCD_BUFSIZE); | |
145 | ||
146 | return blptr; | |
147 | } | |
148 | ||
149 | void unref_blacklist(struct Blacklist *blptr) | |
150 | { | |
151 | blptr->refcount--; | |
152 | if (blptr->status & CONF_ILLEGAL && blptr->refcount <= 0) | |
153 | { | |
154 | dlinkFindDestroy(blptr, &blacklist_list); | |
155 | MyFree(blptr); | |
156 | } | |
157 | } | |
158 | ||
159 | void lookup_blacklists(struct Client *client_p) | |
160 | { | |
161 | dlink_node *nptr; | |
162 | ||
163 | /* We don't do IPv6 right now, sorry! */ | |
164 | if (client_p->localClient->ip.ss_family == AF_INET6) | |
165 | return; | |
166 | ||
167 | DLINK_FOREACH(nptr, blacklist_list.head) | |
168 | { | |
169 | struct Blacklist *blptr = (struct Blacklist *) nptr->data; | |
170 | ||
171 | if (!(blptr->status & CONF_ILLEGAL)) | |
172 | initiate_blacklist_dnsquery(blptr, client_p); | |
173 | } | |
174 | } | |
175 | ||
176 | void abort_blacklist_queries(struct Client *client_p) | |
177 | { | |
178 | dlink_node *ptr, *next_ptr; | |
179 | struct BlacklistClient *blcptr; | |
180 | ||
181 | if (client_p->preClient == NULL) | |
182 | return; | |
183 | DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->preClient->dnsbl_queries.head) | |
184 | { | |
185 | blcptr = ptr->data; | |
186 | dlinkDelete(&blcptr->node, &client_p->preClient->dnsbl_queries); | |
187 | unref_blacklist(blcptr->blacklist); | |
188 | delete_resolver_queries(&blcptr->dns_query); | |
189 | MyFree(blcptr); | |
190 | } | |
191 | } | |
192 | ||
193 | void destroy_blacklists(void) | |
194 | { | |
195 | dlink_node *ptr, *next_ptr; | |
196 | struct Blacklist *blptr; | |
197 | ||
198 | DLINK_FOREACH_SAFE(ptr, next_ptr, blacklist_list.head) | |
199 | { | |
200 | blptr = ptr->data; | |
201 | blptr->hits = 0; /* keep it simple and consistent */ | |
202 | if (blptr->refcount > 0) | |
203 | blptr->status |= CONF_ILLEGAL; | |
204 | else | |
205 | { | |
206 | MyFree(ptr->data); | |
207 | dlinkDestroy(ptr, &blacklist_list); | |
208 | } | |
209 | } | |
210 | } |