1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2017 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file conf_class.c |
23 |
* \brief Configuration managment for class{} blocks |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "ircd.h" |
30 |
#include "conf.h" |
31 |
#include "hostmask.h" |
32 |
#include "irc_string.h" |
33 |
#include "memory.h" |
34 |
|
35 |
|
36 |
struct ClassItem *class_default; |
37 |
|
38 |
static dlink_list class_list; |
39 |
|
40 |
|
41 |
const dlink_list * |
42 |
class_get_list(void) |
43 |
{ |
44 |
return &class_list; |
45 |
} |
46 |
|
47 |
struct ClassItem * |
48 |
class_make(void) |
49 |
{ |
50 |
struct ClassItem *const class = xcalloc(sizeof(*class)); |
51 |
|
52 |
class->active = 1; |
53 |
class->con_freq = DEFAULT_CONNECTFREQUENCY; |
54 |
class->ping_freq = DEFAULT_PINGFREQUENCY; |
55 |
class->max_total = MAXIMUM_LINKS_DEFAULT; |
56 |
class->max_sendq = DEFAULT_SENDQ; |
57 |
class->max_recvq = DEFAULT_RECVQ; |
58 |
|
59 |
dlinkAdd(class, &class->node, &class_list); |
60 |
|
61 |
return class; |
62 |
} |
63 |
|
64 |
void |
65 |
class_free(struct ClassItem *const class) |
66 |
{ |
67 |
assert(class != class_default); |
68 |
assert(class->active == 0); |
69 |
assert(class->ref_count == 0); |
70 |
|
71 |
dlinkDelete(&class->node, &class_list); |
72 |
xfree(class->name); |
73 |
xfree(class); |
74 |
} |
75 |
|
76 |
void |
77 |
class_init(void) |
78 |
{ |
79 |
class_default = class_make(); |
80 |
class_default->name = xstrdup("default"); |
81 |
} |
82 |
|
83 |
const struct ClassItem * |
84 |
get_class_ptr(const dlink_list *const list) |
85 |
{ |
86 |
const dlink_node *const node = list->head; |
87 |
|
88 |
if (node) |
89 |
{ |
90 |
const struct MaskItem *const conf = node->data; |
91 |
|
92 |
assert(conf->class); |
93 |
assert(conf->type & (CONF_OPER | CONF_CLIENT | CONF_SERVER)); |
94 |
|
95 |
return conf->class; |
96 |
} |
97 |
|
98 |
return class_default; |
99 |
} |
100 |
|
101 |
const char * |
102 |
get_client_class(const dlink_list *const list) |
103 |
{ |
104 |
return get_class_ptr(list)->name; |
105 |
} |
106 |
|
107 |
unsigned int |
108 |
get_client_ping(const dlink_list *const list) |
109 |
{ |
110 |
return get_class_ptr(list)->ping_freq; |
111 |
} |
112 |
|
113 |
unsigned int |
114 |
get_sendq(const dlink_list *const list) |
115 |
{ |
116 |
return get_class_ptr(list)->max_sendq; |
117 |
} |
118 |
|
119 |
unsigned int |
120 |
get_recvq(const dlink_list *const list) |
121 |
{ |
122 |
return get_class_ptr(list)->max_recvq; |
123 |
} |
124 |
|
125 |
/* |
126 |
* inputs - Integer (Number of class) |
127 |
* output - Pointer to ClassItem struct. Non-NULL expected |
128 |
* side effects - NONE |
129 |
*/ |
130 |
struct ClassItem * |
131 |
class_find(const char *name, int active) |
132 |
{ |
133 |
dlink_node *node = NULL; |
134 |
|
135 |
DLINK_FOREACH(node, class_list.head) |
136 |
{ |
137 |
struct ClassItem *class = node->data; |
138 |
|
139 |
if (!irccmp(class->name, name)) |
140 |
return active && !class->active ? NULL : class; |
141 |
} |
142 |
|
143 |
return NULL; |
144 |
} |
145 |
|
146 |
/* |
147 |
* We don't delete the class table, rather mark all entries for deletion. |
148 |
* The table is cleaned up by delete_marked_classes. - avalon |
149 |
*/ |
150 |
void |
151 |
class_mark_for_deletion(void) |
152 |
{ |
153 |
dlink_node *node = NULL; |
154 |
|
155 |
DLINK_FOREACH_PREV(node, class_list.tail->prev) |
156 |
((struct ClassItem *)node->data)->active = 0; |
157 |
} |
158 |
|
159 |
void |
160 |
class_delete_marked(void) |
161 |
{ |
162 |
dlink_node *node = NULL, *node_next = NULL; |
163 |
|
164 |
DLINK_FOREACH_SAFE(node, node_next, class_list.head) |
165 |
{ |
166 |
struct ClassItem *class = node->data; |
167 |
|
168 |
if (!class->active && !class->ref_count) |
169 |
{ |
170 |
destroy_cidr_class(class); |
171 |
class_free(class); |
172 |
} |
173 |
} |
174 |
} |
175 |
|
176 |
/* |
177 |
* cidr_limit_reached |
178 |
* |
179 |
* inputs - int flag allowing over_rule of limits |
180 |
* - pointer to the ip to be added |
181 |
* - pointer to the class |
182 |
* output - non zero if limit reached |
183 |
* 0 if limit not reached |
184 |
* side effects - |
185 |
*/ |
186 |
int |
187 |
cidr_limit_reached(int over_rule, struct irc_ssaddr *ip, struct ClassItem *class) |
188 |
{ |
189 |
dlink_node *node = NULL; |
190 |
struct CidrItem *cidr = NULL; |
191 |
|
192 |
if (class->number_per_cidr == 0) |
193 |
return 0; |
194 |
|
195 |
if (ip->ss.ss_family == AF_INET) |
196 |
{ |
197 |
if (class->cidr_bitlen_ipv4 == 0) |
198 |
return 0; |
199 |
|
200 |
DLINK_FOREACH(node, class->list_ipv4.head) |
201 |
{ |
202 |
cidr = node->data; |
203 |
|
204 |
if (match_ipv4(ip, &cidr->mask, class->cidr_bitlen_ipv4)) |
205 |
{ |
206 |
if (!over_rule && (cidr->number_on_this_cidr >= class->number_per_cidr)) |
207 |
return -1; |
208 |
|
209 |
cidr->number_on_this_cidr++; |
210 |
return 0; |
211 |
} |
212 |
} |
213 |
|
214 |
cidr = xcalloc(sizeof(struct CidrItem)); |
215 |
cidr->number_on_this_cidr = 1; |
216 |
cidr->mask = *ip; |
217 |
mask_addr(&cidr->mask, class->cidr_bitlen_ipv4); |
218 |
dlinkAdd(cidr, &cidr->node, &class->list_ipv4); |
219 |
} |
220 |
else if (class->cidr_bitlen_ipv6 > 0) |
221 |
{ |
222 |
DLINK_FOREACH(node, class->list_ipv6.head) |
223 |
{ |
224 |
cidr = node->data; |
225 |
|
226 |
if (match_ipv6(ip, &cidr->mask, class->cidr_bitlen_ipv6)) |
227 |
{ |
228 |
if (!over_rule && (cidr->number_on_this_cidr >= class->number_per_cidr)) |
229 |
return -1; |
230 |
|
231 |
cidr->number_on_this_cidr++; |
232 |
return 0; |
233 |
} |
234 |
} |
235 |
|
236 |
cidr = xcalloc(sizeof(struct CidrItem)); |
237 |
cidr->number_on_this_cidr = 1; |
238 |
cidr->mask = *ip; |
239 |
mask_addr(&cidr->mask, class->cidr_bitlen_ipv6); |
240 |
dlinkAdd(cidr, &cidr->node, &class->list_ipv6); |
241 |
} |
242 |
|
243 |
return 0; |
244 |
} |
245 |
|
246 |
/* |
247 |
* remove_from_cidr_check |
248 |
* |
249 |
* inputs - pointer to the ip to be removed |
250 |
* - pointer to the class |
251 |
* output - NONE |
252 |
* side effects - |
253 |
*/ |
254 |
void |
255 |
remove_from_cidr_check(struct irc_ssaddr *ip, struct ClassItem *aclass) |
256 |
{ |
257 |
dlink_node *node = NULL, *node_next = NULL; |
258 |
|
259 |
if (aclass->number_per_cidr == 0) |
260 |
return; |
261 |
|
262 |
if (ip->ss.ss_family == AF_INET) |
263 |
{ |
264 |
if (aclass->cidr_bitlen_ipv4 == 0) |
265 |
return; |
266 |
|
267 |
DLINK_FOREACH_SAFE(node, node_next, aclass->list_ipv4.head) |
268 |
{ |
269 |
struct CidrItem *cidr = node->data; |
270 |
|
271 |
if (match_ipv4(ip, &cidr->mask, aclass->cidr_bitlen_ipv4)) |
272 |
{ |
273 |
cidr->number_on_this_cidr--; |
274 |
|
275 |
if (cidr->number_on_this_cidr == 0) |
276 |
{ |
277 |
dlinkDelete(node, &aclass->list_ipv4); |
278 |
xfree(cidr); |
279 |
return; |
280 |
} |
281 |
} |
282 |
} |
283 |
} |
284 |
else if (aclass->cidr_bitlen_ipv6 > 0) |
285 |
{ |
286 |
DLINK_FOREACH_SAFE(node, node_next, aclass->list_ipv6.head) |
287 |
{ |
288 |
struct CidrItem *cidr = node->data; |
289 |
|
290 |
if (match_ipv6(ip, &cidr->mask, aclass->cidr_bitlen_ipv6)) |
291 |
{ |
292 |
cidr->number_on_this_cidr--; |
293 |
|
294 |
if (cidr->number_on_this_cidr == 0) |
295 |
{ |
296 |
dlinkDelete(node, &aclass->list_ipv6); |
297 |
xfree(cidr); |
298 |
return; |
299 |
} |
300 |
} |
301 |
} |
302 |
} |
303 |
} |
304 |
|
305 |
void |
306 |
rebuild_cidr_list(struct ClassItem *class) |
307 |
{ |
308 |
dlink_node *node = NULL; |
309 |
|
310 |
destroy_cidr_class(class); |
311 |
|
312 |
DLINK_FOREACH(node, local_client_list.head) |
313 |
{ |
314 |
struct Client *client_p = node->data; |
315 |
struct MaskItem *conf = client_p->connection->confs.tail->data; |
316 |
|
317 |
if (conf && (conf->type == CONF_CLIENT)) |
318 |
if (conf->class == class) |
319 |
cidr_limit_reached(1, &client_p->connection->ip, class); |
320 |
} |
321 |
} |
322 |
|
323 |
/* |
324 |
* destroy_cidr_list |
325 |
* |
326 |
* inputs - pointer to class dlink list of cidr blocks |
327 |
* output - none |
328 |
* side effects - completely destroys the class link list of cidr blocks |
329 |
*/ |
330 |
static void |
331 |
destroy_cidr_list(dlink_list *list) |
332 |
{ |
333 |
while (list->head) |
334 |
{ |
335 |
struct CidrItem *cidr = list->head->data; |
336 |
dlinkDelete(&cidr->node, list); |
337 |
xfree(cidr); |
338 |
} |
339 |
} |
340 |
|
341 |
/* |
342 |
* destroy_cidr_class |
343 |
* |
344 |
* inputs - pointer to class |
345 |
* output - none |
346 |
* side effects - completely destroys the class link list of cidr blocks |
347 |
*/ |
348 |
void |
349 |
destroy_cidr_class(struct ClassItem *class) |
350 |
{ |
351 |
destroy_cidr_list(&class->list_ipv4); |
352 |
destroy_cidr_list(&class->list_ipv6); |
353 |
} |