]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* |
2 | * ircd-ratbox: A slightly useful ircd | |
3 | * reject.c: reject users with prejudice | |
4 | * | |
5 | * Copyright (C) 2003 Aaron Sethman <androsyn@ratbox.org> | |
6 | * Copyright (C) 2003-2005 ircd-ratbox development team | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
21 | * USA | |
22 | * | |
8017ad2d | 23 | * $Id: reject.c 3456 2007-05-18 19:14:18Z jilles $ |
212380e3 AC |
24 | */ |
25 | ||
26 | #include "stdinc.h" | |
27 | #include "config.h" | |
28 | #include "patricia.h" | |
29 | #include "client.h" | |
30 | #include "s_conf.h" | |
31 | #include "event.h" | |
32 | #include "tools.h" | |
33 | #include "reject.h" | |
34 | #include "s_stats.h" | |
35 | #include "msg.h" | |
36 | ||
37 | static patricia_tree_t *reject_tree; | |
38 | dlink_list delay_exit; | |
39 | static dlink_list reject_list; | |
40 | ||
54015b5f JT |
41 | static patricia_tree_t *unknown_tree; |
42 | ||
212380e3 AC |
43 | struct reject_data |
44 | { | |
45 | dlink_node rnode; | |
46 | time_t time; | |
47 | unsigned int count; | |
48 | }; | |
49 | ||
54015b5f JT |
50 | static patricia_tree_t *unknown_tree; |
51 | ||
212380e3 AC |
52 | static void |
53 | reject_exit(void *unused) | |
54 | { | |
55 | struct Client *client_p; | |
56 | dlink_node *ptr, *ptr_next; | |
57 | ||
58 | DLINK_FOREACH_SAFE(ptr, ptr_next, delay_exit.head) | |
59 | { | |
60 | client_p = ptr->data; | |
61 | if(IsDead(client_p)) | |
62 | continue; | |
63 | ||
64 | /* this MUST be here, to prevent the possibility | |
65 | * sendto_one() generates a write error, and then a client | |
66 | * ends up on the dead_list and the abort_list --fl | |
67 | * | |
68 | * new disconnect notice stolen from ircu --nenolod | |
69 | * no, this only happens when someone's IP has some | |
70 | * ban on it and rejects them rather longer than the | |
71 | * ircu message suggests --jilles | |
72 | */ | |
73 | if(!IsIOError(client_p)) | |
54015b5f JT |
74 | { |
75 | if(IsExUnknown(client_p)) | |
76 | sendto_one(client_p, "ERROR :Closing Link: %s (*** Too many unknown connections)", client_p->host); | |
77 | else | |
78 | sendto_one(client_p, "ERROR :Closing Link: %s (*** Banned (cache))", client_p->host); | |
79 | } | |
212380e3 AC |
80 | close_connection(client_p); |
81 | SetDead(client_p); | |
82 | dlinkAddAlloc(client_p, &dead_list); | |
83 | } | |
84 | ||
85 | delay_exit.head = delay_exit.tail = NULL; | |
86 | delay_exit.length = 0; | |
87 | } | |
88 | ||
89 | static void | |
90 | reject_expires(void *unused) | |
91 | { | |
92 | dlink_node *ptr, *next; | |
93 | patricia_node_t *pnode; | |
94 | struct reject_data *rdata; | |
95 | ||
96 | DLINK_FOREACH_SAFE(ptr, next, reject_list.head) | |
97 | { | |
98 | pnode = ptr->data; | |
99 | rdata = pnode->data; | |
100 | ||
101 | if(rdata->time + ConfigFileEntry.reject_duration > CurrentTime) | |
102 | continue; | |
103 | ||
104 | dlinkDelete(ptr, &reject_list); | |
105 | MyFree(rdata); | |
106 | patricia_remove(reject_tree, pnode); | |
107 | } | |
108 | } | |
109 | ||
110 | void | |
111 | init_reject(void) | |
112 | { | |
113 | reject_tree = New_Patricia(PATRICIA_BITS); | |
54015b5f | 114 | unknown_tree = New_Patricia(PATRICIA_BITS); |
212380e3 AC |
115 | eventAdd("reject_exit", reject_exit, NULL, DELAYED_EXIT_TIME); |
116 | eventAdd("reject_expires", reject_expires, NULL, 60); | |
117 | } | |
118 | ||
119 | ||
120 | void | |
121 | add_reject(struct Client *client_p) | |
122 | { | |
123 | patricia_node_t *pnode; | |
124 | struct reject_data *rdata; | |
125 | ||
126 | /* Reject is disabled */ | |
127 | if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_ban_time == 0) | |
128 | return; | |
129 | ||
130 | if((pnode = match_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip)) != NULL) | |
131 | { | |
132 | rdata = pnode->data; | |
133 | rdata->time = CurrentTime; | |
134 | rdata->count++; | |
135 | } | |
136 | else | |
137 | { | |
138 | int bitlen = 32; | |
139 | #ifdef IPV6 | |
140 | if(client_p->localClient->ip.ss_family == AF_INET6) | |
141 | bitlen = 128; | |
142 | #endif | |
143 | pnode = make_and_lookup_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip, bitlen); | |
144 | pnode->data = rdata = MyMalloc(sizeof(struct reject_data)); | |
145 | dlinkAddTail(pnode, &rdata->rnode, &reject_list); | |
146 | rdata->time = CurrentTime; | |
147 | rdata->count = 1; | |
148 | } | |
149 | } | |
150 | ||
151 | int | |
152 | check_reject(struct Client *client_p) | |
153 | { | |
154 | patricia_node_t *pnode; | |
155 | struct reject_data *rdata; | |
156 | ||
157 | /* Reject is disabled */ | |
158 | if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_ban_time == 0 || | |
159 | ConfigFileEntry.reject_duration == 0) | |
160 | return 0; | |
161 | ||
162 | pnode = match_ip(reject_tree, (struct sockaddr *)&client_p->localClient->ip); | |
163 | if(pnode != NULL) | |
164 | { | |
165 | rdata = pnode->data; | |
166 | ||
167 | rdata->time = CurrentTime; | |
168 | if(rdata->count > ConfigFileEntry.reject_after_count) | |
169 | { | |
170 | ServerStats->is_rej++; | |
171 | SetReject(client_p); | |
172 | comm_setselect(client_p->localClient->fd, FDLIST_NONE, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0); | |
173 | SetClosing(client_p); | |
174 | dlinkMoveNode(&client_p->localClient->tnode, &unknown_list, &delay_exit); | |
175 | return 1; | |
176 | } | |
177 | } | |
178 | /* Caller does what it wants */ | |
179 | return 0; | |
180 | } | |
181 | ||
182 | void | |
183 | flush_reject(void) | |
184 | { | |
185 | dlink_node *ptr, *next; | |
186 | patricia_node_t *pnode; | |
187 | struct reject_data *rdata; | |
188 | ||
189 | DLINK_FOREACH_SAFE(ptr, next, reject_list.head) | |
190 | { | |
191 | pnode = ptr->data; | |
192 | rdata = pnode->data; | |
193 | dlinkDelete(ptr, &reject_list); | |
194 | MyFree(rdata); | |
195 | patricia_remove(reject_tree, pnode); | |
196 | } | |
197 | } | |
198 | ||
199 | int | |
200 | remove_reject(const char *ip) | |
201 | { | |
202 | patricia_node_t *pnode; | |
203 | ||
204 | /* Reject is disabled */ | |
205 | if(ConfigFileEntry.reject_after_count == 0 || ConfigFileEntry.reject_ban_time == 0 || | |
206 | ConfigFileEntry.reject_duration == 0) | |
207 | return -1; | |
208 | ||
209 | if((pnode = match_string(reject_tree, ip)) != NULL) | |
210 | { | |
211 | struct reject_data *rdata = pnode->data; | |
212 | dlinkDelete(&rdata->rnode, &reject_list); | |
213 | MyFree(rdata); | |
214 | patricia_remove(reject_tree, pnode); | |
215 | return 1; | |
216 | } | |
217 | return 0; | |
218 | } | |
219 | ||
54015b5f JT |
220 | |
221 | int | |
222 | add_unknown_ip(struct Client *client_p) | |
223 | { | |
224 | patricia_node_t *pnode; | |
225 | ||
226 | if((pnode = match_ip(unknown_tree, (struct sockaddr *)&client_p->localClient->ip)) == NULL) | |
227 | { | |
228 | int bitlen = 32; | |
229 | #ifdef IPV6 | |
230 | if(client_p->localClient->ip.ss_family == AF_INET6) | |
231 | bitlen = 128; | |
232 | #endif | |
233 | pnode = make_and_lookup_ip(unknown_tree, (struct sockaddr *)&client_p->localClient->ip, bitlen); | |
234 | pnode->data = (void *)0; | |
235 | } | |
236 | ||
237 | if((unsigned long)pnode->data >= ConfigFileEntry.max_unknown_ip) | |
238 | { | |
239 | SetExUnknown(client_p); | |
240 | SetReject(client_p); | |
241 | comm_setselect(client_p->localClient->fd, FDLIST_NONE, COMM_SELECT_WRITE | COMM_SELECT_READ, NULL, NULL, 0); | |
242 | SetClosing(client_p); | |
243 | dlinkMoveNode(&client_p->localClient->tnode, &unknown_list, &delay_exit); | |
244 | return 1; | |
245 | } | |
246 | ||
247 | pnode->data = (void *)((unsigned long)pnode->data + 1); | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
252 | void | |
253 | del_unknown_ip(struct Client *client_p) | |
254 | { | |
255 | patricia_node_t *pnode; | |
256 | ||
257 | if((pnode = match_ip(unknown_tree, (struct sockaddr *)&client_p->localClient->ip)) != NULL) | |
258 | { | |
259 | pnode->data = (void *)((unsigned long)pnode->data - 1); | |
260 | if((unsigned long)pnode->data <= 0) | |
261 | { | |
262 | patricia_remove(unknown_tree, pnode); | |
263 | } | |
264 | } | |
8017ad2d | 265 | /* this can happen due to m_webirc.c's manipulations, for example */ |
54015b5f | 266 | } |