1 |
|
/* |
2 |
< |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
2 |
> |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
|
* |
4 |
< |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
4 |
> |
* Copyright (c) 1997-2018 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 |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
18 |
> |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
|
* USA |
20 |
|
*/ |
21 |
|
|
22 |
< |
/*! \file |
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_defs.h" |
30 |
– |
#include "balloc.h" |
31 |
– |
#include "conf.h" |
32 |
– |
#include "s_serv.h" |
33 |
– |
#include "resv.h" |
34 |
– |
#include "channel.h" |
35 |
– |
#include "client.h" |
36 |
– |
#include "event.h" |
37 |
– |
#include "hook.h" |
38 |
– |
#include "irc_string.h" |
39 |
– |
#include "s_bsd.h" |
29 |
|
#include "ircd.h" |
30 |
< |
#include "listener.h" |
30 |
> |
#include "conf.h" |
31 |
|
#include "hostmask.h" |
32 |
< |
#include "modules.h" |
44 |
< |
#include "numeric.h" |
45 |
< |
#include "fdlist.h" |
46 |
< |
#include "log.h" |
47 |
< |
#include "send.h" |
48 |
< |
#include "s_gline.h" |
32 |
> |
#include "irc_string.h" |
33 |
|
#include "memory.h" |
50 |
– |
#include "irc_res.h" |
51 |
– |
#include "userhost.h" |
52 |
– |
#include "s_user.h" |
53 |
– |
#include "channel_mode.h" |
54 |
– |
#include "parse.h" |
55 |
– |
#include "s_misc.h" |
56 |
– |
#include "conf_db.h" |
34 |
|
|
35 |
|
|
36 |
|
struct ClassItem *class_default; |
37 |
|
|
38 |
< |
static dlink_list class_list = { NULL, NULL, 0 }; |
38 |
> |
static dlink_list class_list; |
39 |
|
|
40 |
|
|
41 |
|
const dlink_list * |
47 |
|
struct ClassItem * |
48 |
|
class_make(void) |
49 |
|
{ |
50 |
< |
struct ClassItem *class = MyMalloc(sizeof(*class)); |
50 |
> |
struct ClassItem *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 |
|
|
62 |
|
} |
63 |
|
|
64 |
|
void |
65 |
< |
class_free(struct ClassItem *class) |
65 |
> |
class_free(struct ClassItem *const class) |
66 |
|
{ |
67 |
< |
assert(class); |
67 |
> |
assert(class != class_default); |
68 |
|
assert(class->active == 0); |
69 |
|
assert(class->ref_count == 0); |
70 |
|
|
71 |
|
dlinkDelete(&class->node, &class_list); |
72 |
< |
MyFree(class->name); |
73 |
< |
MyFree(class); |
72 |
> |
xfree(class->name); |
73 |
> |
xfree(class); |
74 |
|
} |
75 |
|
|
76 |
|
void |
77 |
|
class_init(void) |
78 |
|
{ |
79 |
|
class_default = class_make(); |
80 |
< |
|
97 |
< |
DupString(class_default->name, "default"); |
98 |
< |
|
99 |
< |
class_default->active = 1; |
100 |
< |
class_default->con_freq = DEFAULT_CONNECTFREQUENCY; |
101 |
< |
class_default->ping_freq = DEFAULT_PINGFREQUENCY; |
102 |
< |
class_default->max_total = MAXIMUM_LINKS_DEFAULT; |
103 |
< |
class_default->max_sendq = DEFAULT_SENDQ; |
104 |
< |
class_default->max_recvq = DEFAULT_RECVQ; |
105 |
< |
|
106 |
< |
client_check_cb = register_callback("check_client", check_client); |
80 |
> |
class_default->name = xstrdup("default"); |
81 |
|
} |
82 |
|
|
83 |
< |
const char * |
84 |
< |
get_client_class(const dlink_list *const list) |
83 |
> |
const struct ClassItem * |
84 |
> |
class_get_ptr(const dlink_list *const list) |
85 |
|
{ |
86 |
< |
const dlink_node *ptr = NULL; |
86 |
> |
const dlink_node *const node = list->head; |
87 |
|
|
88 |
< |
if ((ptr = list->head)) { |
89 |
< |
const struct MaskItem *conf = ptr->data; |
88 |
> |
if (node) |
89 |
> |
{ |
90 |
> |
const struct MaskItem *const conf = node->data; |
91 |
|
|
92 |
|
assert(conf->class); |
93 |
< |
assert(conf->type & (CONF_OPERATOR | CONF_CLIENT | CONF_SERVER)); |
93 |
> |
assert(conf->type & (CONF_OPER | CONF_CLIENT | CONF_SERVER)); |
94 |
|
|
95 |
< |
return conf->class->name; |
95 |
> |
return conf->class; |
96 |
|
} |
97 |
|
|
98 |
< |
return class_default->name; |
98 |
> |
return class_default; |
99 |
|
} |
100 |
|
|
101 |
< |
unsigned int |
102 |
< |
get_client_ping(const dlink_list *const list, int *pingwarn) |
101 |
> |
const char * |
102 |
> |
get_client_class(const dlink_list *const list) |
103 |
|
{ |
104 |
< |
const dlink_node *ptr = NULL; |
105 |
< |
int ping = 0; |
131 |
< |
|
132 |
< |
if ((ptr = list->head)) { |
133 |
< |
const struct MaskItem *conf = ptr->data; |
134 |
< |
|
135 |
< |
assert(aconf->class); |
136 |
< |
assert(aconf->type & (CONF_OPERATOR | CONF_CLIENT | CONF_SERVER)); |
137 |
< |
|
138 |
< |
ping = get_conf_ping(conf, pingwarn); |
139 |
< |
if (ping > 0) |
140 |
< |
return ping; |
141 |
< |
} |
104 |
> |
return class_get_ptr(list)->name; |
105 |
> |
} |
106 |
|
|
107 |
< |
return class_default->ping_freq; |
107 |
> |
unsigned int |
108 |
> |
get_client_ping(const dlink_list *const list) |
109 |
> |
{ |
110 |
> |
return class_get_ptr(list)->ping_freq; |
111 |
|
} |
112 |
|
|
113 |
|
unsigned int |
114 |
|
get_sendq(const dlink_list *const list) |
115 |
|
{ |
116 |
< |
const dlink_node *ptr = NULL; |
150 |
< |
|
151 |
< |
if ((ptr = list->head)) { |
152 |
< |
const struct MaskItem *conf = ptr->data; |
153 |
< |
|
154 |
< |
assert(conf->class); |
155 |
< |
assert(conf->type & (CONF_OPERATOR | CONF_CLIENT | CONF_SERVER)); |
156 |
< |
|
157 |
< |
return conf->class->max_sendq; |
158 |
< |
} |
159 |
< |
|
160 |
< |
return class_default->max_sendq; |
116 |
> |
return class_get_ptr(list)->max_sendq; |
117 |
|
} |
118 |
|
|
119 |
|
unsigned int |
120 |
|
get_recvq(const dlink_list *const list) |
121 |
|
{ |
122 |
< |
const dlink_node *ptr = NULL; |
167 |
< |
|
168 |
< |
if ((ptr = list->head)) { |
169 |
< |
const struct MaskItem *conf = ptr->data; |
170 |
< |
|
171 |
< |
assert(conf->class); |
172 |
< |
assert(conf->type & (CONF_OPERATOR | CONF_CLIENT | CONF_SERVER)); |
173 |
< |
|
174 |
< |
return conf->class->max_recvq; |
175 |
< |
} |
176 |
< |
|
177 |
< |
return class_default->max_recvq; |
122 |
> |
return class_get_ptr(list)->max_recvq; |
123 |
|
} |
124 |
|
|
125 |
|
/* |
130 |
|
struct ClassItem * |
131 |
|
class_find(const char *name, int active) |
132 |
|
{ |
133 |
< |
dlink_node *ptr = NULL; |
133 |
> |
dlink_node *node; |
134 |
|
|
135 |
< |
DLINK_FOREACH(ptr, class_list.head) { |
136 |
< |
struct ClassItem *class = ptr->data; |
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; |
150 |
|
void |
151 |
|
class_mark_for_deletion(void) |
152 |
|
{ |
153 |
< |
dlink_node *ptr = NULL; |
208 |
< |
|
209 |
< |
DLINK_FOREACH(ptr, class_list.head) |
210 |
< |
{ |
211 |
< |
struct ClassItem *class = ptr->data; |
153 |
> |
dlink_node *node; |
154 |
|
|
155 |
< |
if (class != class_default) |
156 |
< |
class->active = 0; |
215 |
< |
} |
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 *ptr = NULL, *ptr_next = NULL; |
162 |
> |
dlink_node *node, *node_next; |
163 |
|
|
164 |
< |
DLINK_FOREACH_SAFE(ptr, ptr_next, class_list.head) { |
165 |
< |
struct ClassItem *class = ptr->data; |
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) |
168 |
> |
if (class->active == 0 && class->ref_count == 0) |
169 |
|
{ |
170 |
|
destroy_cidr_class(class); |
171 |
|
class_free(class); |
184 |
|
* side effects - |
185 |
|
*/ |
186 |
|
int |
187 |
< |
cidr_limit_reached(int over_rule, |
246 |
< |
struct irc_ssaddr *ip, struct ClassItem *class) |
187 |
> |
cidr_limit_reached(int over_rule, struct irc_ssaddr *ip, struct ClassItem *class) |
188 |
|
{ |
189 |
< |
dlink_node *ptr = NULL; |
249 |
< |
struct CidrItem *cidr = NULL; |
189 |
> |
dlink_node *node; |
190 |
|
|
191 |
< |
if (class->number_per_cidr <= 0) |
191 |
> |
if (class->number_per_cidr == 0) |
192 |
|
return 0; |
193 |
|
|
194 |
|
if (ip->ss.ss_family == AF_INET) |
195 |
|
{ |
196 |
< |
if (class->cidr_bitlen_ipv4 <= 0) |
196 |
> |
if (class->cidr_bitlen_ipv4 == 0) |
197 |
|
return 0; |
198 |
|
|
199 |
< |
DLINK_FOREACH(ptr, class->list_ipv4.head) |
199 |
> |
DLINK_FOREACH(node, class->list_ipv4.head) |
200 |
|
{ |
201 |
< |
cidr = ptr->data; |
201 |
> |
struct CidrItem *cidr = node->data; |
202 |
> |
|
203 |
|
if (match_ipv4(ip, &cidr->mask, class->cidr_bitlen_ipv4)) |
204 |
|
{ |
205 |
|
if (!over_rule && (cidr->number_on_this_cidr >= class->number_per_cidr)) |
206 |
|
return -1; |
207 |
+ |
|
208 |
|
cidr->number_on_this_cidr++; |
209 |
|
return 0; |
210 |
|
} |
211 |
|
} |
212 |
< |
cidr = MyMalloc(sizeof(struct CidrItem)); |
212 |
> |
|
213 |
> |
struct CidrItem *cidr = xcalloc(sizeof(*cidr)); |
214 |
|
cidr->number_on_this_cidr = 1; |
215 |
|
cidr->mask = *ip; |
216 |
|
mask_addr(&cidr->mask, class->cidr_bitlen_ipv4); |
217 |
|
dlinkAdd(cidr, &cidr->node, &class->list_ipv4); |
218 |
|
} |
276 |
– |
#ifdef IPV6 |
219 |
|
else if (class->cidr_bitlen_ipv6 > 0) |
220 |
|
{ |
221 |
< |
DLINK_FOREACH(ptr, class->list_ipv6.head) |
221 |
> |
DLINK_FOREACH(node, class->list_ipv6.head) |
222 |
|
{ |
223 |
< |
cidr = ptr->data; |
223 |
> |
struct CidrItem *cidr = node->data; |
224 |
> |
|
225 |
|
if (match_ipv6(ip, &cidr->mask, class->cidr_bitlen_ipv6)) |
226 |
|
{ |
227 |
|
if (!over_rule && (cidr->number_on_this_cidr >= class->number_per_cidr)) |
228 |
|
return -1; |
229 |
+ |
|
230 |
|
cidr->number_on_this_cidr++; |
231 |
|
return 0; |
232 |
|
} |
233 |
|
} |
234 |
< |
cidr = MyMalloc(sizeof(struct CidrItem)); |
234 |
> |
|
235 |
> |
struct CidrItem *cidr = xcalloc(sizeof(*cidr)); |
236 |
|
cidr->number_on_this_cidr = 1; |
237 |
|
cidr->mask = *ip; |
238 |
|
mask_addr(&cidr->mask, class->cidr_bitlen_ipv6); |
239 |
|
dlinkAdd(cidr, &cidr->node, &class->list_ipv6); |
240 |
|
} |
241 |
< |
#endif |
241 |
> |
|
242 |
|
return 0; |
243 |
|
} |
244 |
|
|
253 |
|
void |
254 |
|
remove_from_cidr_check(struct irc_ssaddr *ip, struct ClassItem *aclass) |
255 |
|
{ |
256 |
< |
dlink_node *ptr = NULL; |
312 |
< |
dlink_node *next_ptr = NULL; |
313 |
< |
struct CidrItem *cidr; |
256 |
> |
dlink_node *node, *node_next; |
257 |
|
|
258 |
|
if (aclass->number_per_cidr == 0) |
259 |
|
return; |
260 |
|
|
261 |
|
if (ip->ss.ss_family == AF_INET) |
262 |
|
{ |
263 |
< |
if (aclass->cidr_bitlen_ipv4 <= 0) |
263 |
> |
if (aclass->cidr_bitlen_ipv4 == 0) |
264 |
|
return; |
265 |
|
|
266 |
< |
DLINK_FOREACH_SAFE(ptr, next_ptr, aclass->list_ipv4.head) |
266 |
> |
DLINK_FOREACH_SAFE(node, node_next, aclass->list_ipv4.head) |
267 |
|
{ |
268 |
< |
cidr = ptr->data; |
268 |
> |
struct CidrItem *cidr = node->data; |
269 |
> |
|
270 |
|
if (match_ipv4(ip, &cidr->mask, aclass->cidr_bitlen_ipv4)) |
271 |
|
{ |
272 |
< |
cidr->number_on_this_cidr--; |
273 |
< |
if (cidr->number_on_this_cidr == 0) |
274 |
< |
{ |
275 |
< |
dlinkDelete(ptr, &aclass->list_ipv4); |
276 |
< |
MyFree(cidr); |
277 |
< |
return; |
278 |
< |
} |
272 |
> |
cidr->number_on_this_cidr--; |
273 |
> |
|
274 |
> |
if (cidr->number_on_this_cidr == 0) |
275 |
> |
{ |
276 |
> |
dlinkDelete(node, &aclass->list_ipv4); |
277 |
> |
xfree(cidr); |
278 |
> |
return; |
279 |
> |
} |
280 |
|
} |
281 |
|
} |
282 |
|
} |
338 |
– |
#ifdef IPV6 |
283 |
|
else if (aclass->cidr_bitlen_ipv6 > 0) |
284 |
|
{ |
285 |
< |
DLINK_FOREACH_SAFE(ptr, next_ptr, aclass->list_ipv6.head) |
285 |
> |
DLINK_FOREACH_SAFE(node, node_next, aclass->list_ipv6.head) |
286 |
|
{ |
287 |
< |
cidr = ptr->data; |
287 |
> |
struct CidrItem *cidr = node->data; |
288 |
> |
|
289 |
|
if (match_ipv6(ip, &cidr->mask, aclass->cidr_bitlen_ipv6)) |
290 |
|
{ |
291 |
< |
cidr->number_on_this_cidr--; |
292 |
< |
if (cidr->number_on_this_cidr == 0) |
293 |
< |
{ |
294 |
< |
dlinkDelete(ptr, &aclass->list_ipv6); |
295 |
< |
MyFree(cidr); |
296 |
< |
return; |
297 |
< |
} |
291 |
> |
cidr->number_on_this_cidr--; |
292 |
> |
|
293 |
> |
if (cidr->number_on_this_cidr == 0) |
294 |
> |
{ |
295 |
> |
dlinkDelete(node, &aclass->list_ipv6); |
296 |
> |
xfree(cidr); |
297 |
> |
return; |
298 |
> |
} |
299 |
|
} |
300 |
|
} |
301 |
|
} |
356 |
– |
#endif |
302 |
|
} |
303 |
|
|
304 |
< |
static void |
305 |
< |
rebuild_cidr_list(int aftype, struct ClassItem *oldcl, struct ClassItem *newcl, |
361 |
< |
dlink_list *old_list, dlink_list *new_list, int changed) |
304 |
> |
void |
305 |
> |
rebuild_cidr_list(struct ClassItem *class) |
306 |
|
{ |
307 |
< |
dlink_node *ptr; |
364 |
< |
struct Client *client_p; |
365 |
< |
struct MaskItem *conf; |
307 |
> |
dlink_node *node; |
308 |
|
|
309 |
< |
if (!changed) |
368 |
< |
{ |
369 |
< |
*new_list = *old_list; |
370 |
< |
old_list->head = old_list->tail = NULL; |
371 |
< |
old_list->length = 0; |
372 |
< |
return; |
373 |
< |
} |
309 |
> |
destroy_cidr_class(class); |
310 |
|
|
311 |
< |
DLINK_FOREACH(ptr, local_client_list.head) |
311 |
> |
DLINK_FOREACH(node, local_client_list.head) |
312 |
|
{ |
313 |
< |
client_p = ptr->data; |
314 |
< |
if (client_p->localClient->aftype != aftype) |
379 |
< |
continue; |
380 |
< |
if (dlink_list_length(&client_p->localClient->confs) == 0) |
381 |
< |
continue; |
382 |
< |
|
383 |
< |
conf = client_p->localClient->confs.tail->data; |
384 |
< |
if (conf->type == CONF_CLIENT) |
385 |
< |
if (conf->class == oldcl) |
386 |
< |
cidr_limit_reached(1, &client_p->localClient->ip, newcl); |
387 |
< |
} |
388 |
< |
} |
313 |
> |
struct Client *client_p = node->data; |
314 |
> |
struct MaskItem *conf = client_p->connection->confs.tail->data; |
315 |
|
|
316 |
< |
/* |
317 |
< |
* rebuild_cidr_class |
318 |
< |
* |
393 |
< |
* inputs - pointer to old conf |
394 |
< |
* - pointer to new_class |
395 |
< |
* output - none |
396 |
< |
* side effects - rebuilds the class link list of cidr blocks |
397 |
< |
*/ |
398 |
< |
void |
399 |
< |
rebuild_cidr_class(struct ClassItem *old_class, struct ClassItem *new_class) |
400 |
< |
{ |
401 |
< |
if (old_class->number_per_cidr > 0 && new_class->number_per_cidr > 0) |
402 |
< |
{ |
403 |
< |
if (old_class->cidr_bitlen_ipv4 > 0 && new_class->cidr_bitlen_ipv4 > 0) |
404 |
< |
rebuild_cidr_list(AF_INET, old_class, new_class, |
405 |
< |
&old_class->list_ipv4, &new_class->list_ipv4, |
406 |
< |
old_class->cidr_bitlen_ipv4 != new_class->cidr_bitlen_ipv4); |
407 |
< |
|
408 |
< |
#ifdef IPV6 |
409 |
< |
if (old_class->cidr_bitlen_ipv6 > 0 && new_class->cidr_bitlen_ipv6 > 0) |
410 |
< |
rebuild_cidr_list(AF_INET6, old_class, new_class, |
411 |
< |
&old_class->list_ipv6, &new_class->list_ipv6, |
412 |
< |
old_class->cidr_bitlen_ipv6 != new_class->cidr_bitlen_ipv6); |
413 |
< |
#endif |
316 |
> |
if (conf && (conf->type == CONF_CLIENT)) |
317 |
> |
if (conf->class == class) |
318 |
> |
cidr_limit_reached(1, &client_p->ip, class); |
319 |
|
} |
415 |
– |
|
416 |
– |
destroy_cidr_class(old_class); |
320 |
|
} |
321 |
|
|
322 |
|
/* |
329 |
|
static void |
330 |
|
destroy_cidr_list(dlink_list *list) |
331 |
|
{ |
332 |
< |
dlink_node *ptr = NULL, *next_ptr = NULL; |
430 |
< |
|
431 |
< |
DLINK_FOREACH_SAFE(ptr, next_ptr, list->head) |
332 |
> |
while (list->head) |
333 |
|
{ |
334 |
< |
dlinkDelete(ptr, list); |
335 |
< |
MyFree(ptr->data); |
334 |
> |
struct CidrItem *cidr = list->head->data; |
335 |
> |
dlinkDelete(&cidr->node, list); |
336 |
> |
xfree(cidr); |
337 |
|
} |
338 |
|
} |
339 |
|
|