2 * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
3 * s_newconf.c - code for dealing with conf stuff
5 * Copyright (C) 2004 Lee Hardy <lee@leeh.co.uk>
6 * Copyright (C) 2004-2005 ircd-ratbox development team
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * 1.Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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 * 3.The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
32 * $Id: s_newconf.c 3508 2007-06-04 16:04:49Z jilles $
36 #include "ircd_defs.h"
39 #include "s_newconf.h"
46 #include "irc_dictionary.h"
50 rb_dlink_list shared_conf_list
;
51 rb_dlink_list cluster_conf_list
;
52 rb_dlink_list oper_conf_list
;
53 rb_dlink_list hubleaf_conf_list
;
54 rb_dlink_list server_conf_list
;
55 rb_dlink_list xline_conf_list
;
56 rb_dlink_list resv_conf_list
; /* nicks only! */
57 rb_dlink_list nd_list
; /* nick delay */
58 rb_dlink_list tgchange_list
;
60 rb_patricia_tree_t
*tgchange_tree
;
62 static rb_bh
*nd_heap
= NULL
;
64 static void expire_temp_rxlines(void *unused
);
65 static void expire_nd_entries(void *unused
);
67 struct ev_entry
*expire_nd_entries_ev
= NULL
;
68 struct ev_entry
*expire_temp_rxlines_ev
= NULL
;
73 tgchange_tree
= rb_new_patricia(PATRICIA_BITS
);
74 nd_heap
= rb_bh_create(sizeof(struct nd_entry
), ND_HEAP_SIZE
, "nd_heap");
75 expire_nd_entries_ev
= rb_event_addish("expire_nd_entries", expire_nd_entries
, NULL
, 30);
76 expire_temp_rxlines_ev
= rb_event_addish("expire_temp_rxlines", expire_temp_rxlines
, NULL
, 60);
82 struct server_conf
*server_p
;
84 rb_dlink_node
*next_ptr
;
86 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, shared_conf_list
.head
)
88 /* ptr here is ptr->data->node */
89 rb_dlinkDelete(ptr
, &shared_conf_list
);
90 free_remote_conf(ptr
->data
);
93 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, cluster_conf_list
.head
)
95 rb_dlinkDelete(ptr
, &cluster_conf_list
);
96 free_remote_conf(ptr
->data
);
99 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, hubleaf_conf_list
.head
)
101 rb_dlinkDelete(ptr
, &hubleaf_conf_list
);
102 free_remote_conf(ptr
->data
);
105 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, oper_conf_list
.head
)
107 free_oper_conf(ptr
->data
);
108 rb_dlinkDestroy(ptr
, &oper_conf_list
);
111 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, server_conf_list
.head
)
113 server_p
= ptr
->data
;
115 if(!server_p
->servers
)
117 rb_dlinkDelete(ptr
, &server_conf_list
);
118 free_server_conf(ptr
->data
);
121 server_p
->flags
|= SERVER_ILLEGAL
;
126 clear_s_newconf_bans(void)
128 struct ConfItem
*aconf
;
129 rb_dlink_node
*ptr
, *next_ptr
;
131 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, xline_conf_list
.head
)
139 rb_dlinkDestroy(ptr
, &xline_conf_list
);
142 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, resv_conf_list
.head
)
151 rb_dlinkDestroy(ptr
, &resv_conf_list
);
158 make_remote_conf(void)
160 struct remote_conf
*remote_p
= rb_malloc(sizeof(struct remote_conf
));
165 free_remote_conf(struct remote_conf
*remote_p
)
167 s_assert(remote_p
!= NULL
);
171 rb_free(remote_p
->username
);
172 rb_free(remote_p
->host
);
173 rb_free(remote_p
->server
);
178 find_shared_conf(const char *username
, const char *host
,
179 const char *server
, int flags
)
181 struct remote_conf
*shared_p
;
184 RB_DLINK_FOREACH(ptr
, shared_conf_list
.head
)
186 shared_p
= ptr
->data
;
188 if(match(shared_p
->username
, username
) &&
189 match(shared_p
->host
, host
) &&
190 match(shared_p
->server
, server
))
192 if(shared_p
->flags
& flags
)
203 propagate_generic(struct Client
*source_p
, const char *command
,
204 const char *target
, int cap
, const char *format
, ...)
206 char buffer
[BUFSIZE
];
209 va_start(args
, format
);
210 rb_vsnprintf(buffer
, sizeof(buffer
), format
, args
);
213 sendto_match_servs(source_p
, target
, cap
, NOCAPS
,
215 command
, target
, buffer
);
216 sendto_match_servs(source_p
, target
, CAP_ENCAP
, cap
,
218 target
, command
, buffer
);
222 cluster_generic(struct Client
*source_p
, const char *command
,
223 int cltype
, int cap
, const char *format
, ...)
225 char buffer
[BUFSIZE
];
226 struct remote_conf
*shared_p
;
230 va_start(args
, format
);
231 rb_vsnprintf(buffer
, sizeof(buffer
), format
, args
);
234 RB_DLINK_FOREACH(ptr
, cluster_conf_list
.head
)
236 shared_p
= ptr
->data
;
238 if(!(shared_p
->flags
& cltype
))
241 sendto_match_servs(source_p
, shared_p
->server
, cap
, NOCAPS
,
243 command
, shared_p
->server
, buffer
);
244 sendto_match_servs(source_p
, shared_p
->server
, CAP_ENCAP
, cap
,
246 shared_p
->server
, command
, buffer
);
253 struct oper_conf
*oper_p
= rb_malloc(sizeof(struct oper_conf
));
258 free_oper_conf(struct oper_conf
*oper_p
)
260 s_assert(oper_p
!= NULL
);
264 rb_free(oper_p
->username
);
265 rb_free(oper_p
->host
);
266 rb_free(oper_p
->name
);
267 rb_free(oper_p
->certfp
);
271 memset(oper_p
->passwd
, 0, strlen(oper_p
->passwd
));
272 rb_free(oper_p
->passwd
);
275 #ifdef HAVE_LIBCRYPTO
276 rb_free(oper_p
->rsa_pubkey_file
);
278 if(oper_p
->rsa_pubkey
)
279 RSA_free(oper_p
->rsa_pubkey
);
286 find_oper_conf(const char *username
, const char *host
, const char *locip
, const char *name
)
288 struct oper_conf
*oper_p
;
289 struct rb_sockaddr_storage ip
, cip
;
290 char addr
[HOSTLEN
+1];
294 parse_netmask(locip
, (struct sockaddr
*)&cip
, &cbits
);
296 RB_DLINK_FOREACH(ptr
, oper_conf_list
.head
)
300 /* name/username doesnt match.. */
301 if(irccmp(oper_p
->name
, name
) || !match(oper_p
->username
, username
))
304 rb_strlcpy(addr
, oper_p
->host
, sizeof(addr
));
306 if(parse_netmask(addr
, (struct sockaddr
*)&ip
, &bits
) != HM_HOST
)
308 if(ip
.ss_family
== cip
.ss_family
&&
309 comp_with_mask_sock((struct sockaddr
*)&ip
, (struct sockaddr
*)&cip
, bits
))
313 /* we have to compare against the host as well, because its
314 * valid to set a spoof to an IP, which if we only compare
315 * in ip form to sockhost will not necessarily match --anfl
317 if(match(oper_p
->host
, host
))
325 make_server_conf(void)
327 struct server_conf
*server_p
= rb_malloc(sizeof(struct server_conf
));
328 server_p
->aftype
= AF_INET
;
333 free_server_conf(struct server_conf
*server_p
)
335 s_assert(server_p
!= NULL
);
339 if(!EmptyString(server_p
->passwd
))
341 memset(server_p
->passwd
, 0, strlen(server_p
->passwd
));
342 rb_free(server_p
->passwd
);
345 if(!EmptyString(server_p
->spasswd
))
347 memset(server_p
->spasswd
, 0, strlen(server_p
->spasswd
));
348 rb_free(server_p
->spasswd
);
351 rb_free(server_p
->name
);
352 rb_free(server_p
->host
);
353 rb_free(server_p
->class_name
);
358 add_server_conf(struct server_conf
*server_p
)
360 if(EmptyString(server_p
->class_name
))
362 server_p
->class_name
= rb_strdup("default");
363 server_p
->class = default_class
;
367 server_p
->class = find_class(server_p
->class_name
);
369 if(server_p
->class == default_class
)
371 conf_report_error("Warning connect::class invalid for %s",
374 rb_free(server_p
->class_name
);
375 server_p
->class_name
= rb_strdup("default");
378 if(strchr(server_p
->host
, '*') || strchr(server_p
->host
, '?'))
383 find_server_conf(const char *name
)
385 struct server_conf
*server_p
;
388 RB_DLINK_FOREACH(ptr
, server_conf_list
.head
)
390 server_p
= ptr
->data
;
392 if(ServerConfIllegal(server_p
))
395 if(match(name
, server_p
->name
))
403 attach_server_conf(struct Client
*client_p
, struct server_conf
*server_p
)
405 /* already have an attached conf */
406 if(client_p
->localClient
->att_sconf
)
408 /* short circuit this special case :) */
409 if(client_p
->localClient
->att_sconf
== server_p
)
412 detach_server_conf(client_p
);
415 CurrUsers(server_p
->class)++;
417 client_p
->localClient
->att_sconf
= server_p
;
422 detach_server_conf(struct Client
*client_p
)
424 struct server_conf
*server_p
= client_p
->localClient
->att_sconf
;
429 client_p
->localClient
->att_sconf
= NULL
;
431 CurrUsers(server_p
->class)--;
433 if(ServerConfIllegal(server_p
) && !server_p
->servers
)
435 /* the class this one is using may need destroying too */
436 if(MaxUsers(server_p
->class) < 0 && CurrUsers(server_p
->class) <= 0)
437 free_class(server_p
->class);
439 rb_dlinkDelete(&server_p
->node
, &server_conf_list
);
440 free_server_conf(server_p
);
445 set_server_conf_autoconn(struct Client
*source_p
, const char *name
, int newval
)
447 struct server_conf
*server_p
;
449 if((server_p
= find_server_conf(name
)) != NULL
)
452 server_p
->flags
|= SERVER_AUTOCONN
;
454 server_p
->flags
&= ~SERVER_AUTOCONN
;
456 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
457 "%s has changed AUTOCONN for %s to %i",
458 get_oper_name(source_p
), name
, newval
);
461 sendto_one_notice(source_p
, ":Can't find %s", name
);
465 disable_server_conf_autoconn(const char *name
)
467 struct server_conf
*server_p
;
469 server_p
= find_server_conf(name
);
470 if(server_p
!= NULL
&& server_p
->flags
& SERVER_AUTOCONN
)
472 server_p
->flags
&= ~SERVER_AUTOCONN
;
474 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
475 "Disabling AUTOCONN for %s because of error",
477 ilog(L_SERVER
, "Disabling AUTOCONN for %s because of error",
483 find_xline(const char *gecos
, int counter
)
485 struct ConfItem
*aconf
;
488 RB_DLINK_FOREACH(ptr
, xline_conf_list
.head
)
492 if(match_esc(aconf
->host
, gecos
))
504 find_xline_mask(const char *gecos
)
506 struct ConfItem
*aconf
;
509 RB_DLINK_FOREACH(ptr
, xline_conf_list
.head
)
513 if(!irccmp(aconf
->host
, gecos
))
521 find_nick_resv(const char *name
)
523 struct ConfItem
*aconf
;
526 RB_DLINK_FOREACH(ptr
, resv_conf_list
.head
)
530 if(match_esc(aconf
->host
, name
))
541 find_nick_resv_mask(const char *name
)
543 struct ConfItem
*aconf
;
546 RB_DLINK_FOREACH(ptr
, resv_conf_list
.head
)
550 if(!irccmp(aconf
->host
, name
))
560 * outputs - 1 if nick is vaild resv, 0 otherwise
564 clean_resv_nick(const char *nick
)
571 if(*nick
== '-' || IsDigit(*nick
))
574 while ((tmpch
= *nick
++))
576 if(tmpch
== '?' || tmpch
== '@' || tmpch
== '#')
578 else if(tmpch
== '*')
580 else if(IsNickChar(tmpch
))
592 /* valid_wild_card_simple()
594 * inputs - "thing" to test
595 * outputs - 1 if enough wildcards, else 0
599 valid_wild_card_simple(const char *data
)
606 /* check the string for minimum number of nonwildcard chars */
609 while((tmpch
= *p
++))
611 /* found an escape, p points to the char after it, so skip
614 if(tmpch
== '\\' && *p
)
617 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard_simple
)
620 else if(!IsMWildChar(tmpch
))
622 /* if we have enough nonwildchars, return */
623 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard_simple
)
630 /* strings without wilds are also ok */
635 valid_temp_time(const char *p
)
644 result
+= ((*p
) & 0xF);
651 if(result
> (60 * 24 * 7 * 52))
652 result
= (60 * 24 * 7 * 52);
657 /* Propagated bans are expired elsewhere. */
659 expire_temp_rxlines(void *unused
)
661 struct ConfItem
*aconf
;
663 rb_dlink_node
*next_ptr
;
666 HASH_WALK_SAFE(i
, R_MAX
, ptr
, next_ptr
, resvTable
)
670 if(aconf
->lifetime
!= 0)
672 if(aconf
->hold
&& aconf
->hold
<= rb_current_time())
674 if(ConfigFileEntry
.tkline_expire_notices
)
675 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
676 "Temporary RESV for [%s] expired",
680 rb_dlinkDestroy(ptr
, &resvTable
[i
]);
685 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, resv_conf_list
.head
)
689 if(aconf
->lifetime
!= 0)
691 if(aconf
->hold
&& aconf
->hold
<= rb_current_time())
693 if(ConfigFileEntry
.tkline_expire_notices
)
694 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
695 "Temporary RESV for [%s] expired",
698 rb_dlinkDestroy(ptr
, &resv_conf_list
);
702 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, xline_conf_list
.head
)
706 if(aconf
->lifetime
!= 0)
708 if(aconf
->hold
&& aconf
->hold
<= rb_current_time())
710 if(ConfigFileEntry
.tkline_expire_notices
)
711 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
712 "Temporary X-line for [%s] expired",
715 rb_dlinkDestroy(ptr
, &xline_conf_list
);
723 return(rb_dlink_list_length(&nd_list
));
727 add_nd_entry(const char *name
)
731 if(irc_dictionary_find(nd_dict
, name
) != NULL
)
734 nd
= rb_bh_alloc(nd_heap
);
736 rb_strlcpy(nd
->name
, name
, sizeof(nd
->name
));
737 nd
->expire
= rb_current_time() + ConfigFileEntry
.nick_delay
;
739 /* this list is ordered */
740 rb_dlinkAddTail(nd
, &nd
->lnode
, &nd_list
);
742 irc_dictionary_add(nd_dict
, nd
->name
, nd
);
746 free_nd_entry(struct nd_entry
*nd
)
748 irc_dictionary_delete(nd_dict
, nd
->name
);
750 rb_dlinkDelete(&nd
->lnode
, &nd_list
);
751 rb_bh_free(nd_heap
, nd
);
755 expire_nd_entries(void *unused
)
759 rb_dlink_node
*next_ptr
;
761 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, nd_list
.head
)
765 /* this list is ordered - we can stop when we hit the first
766 * entry that doesnt expire..
768 if(nd
->expire
> rb_current_time())
776 add_tgchange(const char *host
)
779 rb_patricia_node_t
*pnode
;
781 if(find_tgchange(host
))
784 target
= rb_malloc(sizeof(tgchange
));
785 pnode
= make_and_lookup(tgchange_tree
, host
);
787 pnode
->data
= target
;
788 target
->pnode
= pnode
;
790 target
->ip
= rb_strdup(host
);
791 target
->expiry
= rb_current_time() + (60*60*12);
793 rb_dlinkAdd(target
, &target
->node
, &tgchange_list
);
797 find_tgchange(const char *host
)
799 rb_patricia_node_t
*pnode
;
801 if((pnode
= rb_match_exact_string(tgchange_tree
, host
)))