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.
34 #include "ircd_defs.h"
37 #include "s_newconf.h"
44 #include "rb_dictionary.h"
45 #include "rb_radixtree.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 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 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
, &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
, &ip
, &bits
) != HM_HOST
)
308 if(GET_SS_FAMILY(&ip
) == GET_SS_FAMILY(&cip
) &&
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
);
359 * inputs - pointer to struct ConfItem
360 * - pointer to adns reply
362 * side effects - called when resolver query finishes
363 * if the query resulted in a successful search, hp will contain
364 * a non-null pointer, otherwise hp will be null.
365 * if successful save hp in the conf item it was called with
368 conf_dns_callback(const char *result
, int status
, int aftype
, void *data
)
370 struct server_conf
*server_p
= data
;
373 rb_inet_pton_sock(result
, (struct sockaddr
*)&server_p
->my_ipnum
);
375 server_p
->dns_query
= 0;
379 add_server_conf(struct server_conf
*server_p
)
381 if(EmptyString(server_p
->class_name
))
383 server_p
->class_name
= rb_strdup("default");
384 server_p
->class = default_class
;
388 server_p
->class = find_class(server_p
->class_name
);
390 if(server_p
->class == default_class
)
392 conf_report_error("Warning connect::class invalid for %s",
395 rb_free(server_p
->class_name
);
396 server_p
->class_name
= rb_strdup("default");
399 if(strpbrk(server_p
->host
, "*?"))
402 server_p
->dns_query
=
403 lookup_hostname(server_p
->host
, GET_SS_FAMILY(&server_p
->my_ipnum
), conf_dns_callback
, server_p
);
407 find_server_conf(const char *name
)
409 struct server_conf
*server_p
;
412 RB_DLINK_FOREACH(ptr
, server_conf_list
.head
)
414 server_p
= ptr
->data
;
416 if(ServerConfIllegal(server_p
))
419 if(match(name
, server_p
->name
))
427 attach_server_conf(struct Client
*client_p
, struct server_conf
*server_p
)
429 /* already have an attached conf */
430 if(client_p
->localClient
->att_sconf
)
432 /* short circuit this special case :) */
433 if(client_p
->localClient
->att_sconf
== server_p
)
436 detach_server_conf(client_p
);
439 CurrUsers(server_p
->class)++;
441 client_p
->localClient
->att_sconf
= server_p
;
446 detach_server_conf(struct Client
*client_p
)
448 struct server_conf
*server_p
= client_p
->localClient
->att_sconf
;
453 client_p
->localClient
->att_sconf
= NULL
;
455 CurrUsers(server_p
->class)--;
457 if(ServerConfIllegal(server_p
) && !server_p
->servers
)
459 /* the class this one is using may need destroying too */
460 if(MaxUsers(server_p
->class) < 0 && CurrUsers(server_p
->class) <= 0)
461 free_class(server_p
->class);
463 rb_dlinkDelete(&server_p
->node
, &server_conf_list
);
464 free_server_conf(server_p
);
469 set_server_conf_autoconn(struct Client
*source_p
, const char *name
, int newval
)
471 struct server_conf
*server_p
;
473 if((server_p
= find_server_conf(name
)) != NULL
)
476 server_p
->flags
|= SERVER_AUTOCONN
;
478 server_p
->flags
&= ~SERVER_AUTOCONN
;
480 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
481 "%s has changed AUTOCONN for %s to %i",
482 get_oper_name(source_p
), name
, newval
);
485 sendto_one_notice(source_p
, ":Can't find %s", name
);
489 disable_server_conf_autoconn(const char *name
)
491 struct server_conf
*server_p
;
493 server_p
= find_server_conf(name
);
494 if(server_p
!= NULL
&& server_p
->flags
& SERVER_AUTOCONN
)
496 server_p
->flags
&= ~SERVER_AUTOCONN
;
498 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
499 "Disabling AUTOCONN for %s because of error",
501 ilog(L_SERVER
, "Disabling AUTOCONN for %s because of error",
507 find_xline(const char *gecos
, int counter
)
509 struct ConfItem
*aconf
;
512 RB_DLINK_FOREACH(ptr
, xline_conf_list
.head
)
516 if(match_esc(aconf
->host
, gecos
))
528 find_xline_mask(const char *gecos
)
530 struct ConfItem
*aconf
;
533 RB_DLINK_FOREACH(ptr
, xline_conf_list
.head
)
537 if(!irccmp(aconf
->host
, gecos
))
545 find_nick_resv(const char *name
)
547 struct ConfItem
*aconf
;
550 RB_DLINK_FOREACH(ptr
, resv_conf_list
.head
)
554 if(match_esc(aconf
->host
, name
))
565 find_nick_resv_mask(const char *name
)
567 struct ConfItem
*aconf
;
570 RB_DLINK_FOREACH(ptr
, resv_conf_list
.head
)
574 if(!irccmp(aconf
->host
, name
))
584 * outputs - 1 if nick is vaild resv, 0 otherwise
588 clean_resv_nick(const char *nick
)
595 if(*nick
== '-' || IsDigit(*nick
))
598 while ((tmpch
= *nick
++))
600 if(tmpch
== '?' || tmpch
== '@' || tmpch
== '#')
602 else if(tmpch
== '*')
604 else if(IsNickChar(tmpch
))
616 /* valid_wild_card_simple()
618 * inputs - "thing" to test
619 * outputs - 1 if enough wildcards, else 0
623 valid_wild_card_simple(const char *data
)
630 /* check the string for minimum number of nonwildcard chars */
633 while((tmpch
= *p
++))
635 /* found an escape, p points to the char after it, so skip
638 if(tmpch
== '\\' && *p
)
641 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard_simple
)
644 else if(!IsMWildChar(tmpch
))
646 /* if we have enough nonwildchars, return */
647 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard_simple
)
654 /* strings without wilds are also ok */
659 valid_temp_time(const char *p
)
668 result
+= ((*p
) & 0xF);
675 if(result
> (60 * 24 * 7 * 52))
676 result
= (60 * 24 * 7 * 52);
681 /* Propagated bans are expired elsewhere. */
683 expire_temp_rxlines(void *unused
)
685 struct ConfItem
*aconf
;
687 rb_dlink_node
*next_ptr
;
689 struct rb_radixtree_iteration_state state
;
691 RB_RADIXTREE_FOREACH(aconf
, &state
, resv_tree
)
693 if(aconf
->lifetime
!= 0)
695 if(aconf
->hold
&& aconf
->hold
<= rb_current_time())
697 if(ConfigFileEntry
.tkline_expire_notices
)
698 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
699 "Temporary RESV for [%s] expired",
702 rb_radixtree_delete(resv_tree
, aconf
->host
);
707 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, resv_conf_list
.head
)
711 if(aconf
->lifetime
!= 0)
713 if(aconf
->hold
&& aconf
->hold
<= rb_current_time())
715 if(ConfigFileEntry
.tkline_expire_notices
)
716 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
717 "Temporary RESV for [%s] expired",
720 rb_dlinkDestroy(ptr
, &resv_conf_list
);
724 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, xline_conf_list
.head
)
728 if(aconf
->lifetime
!= 0)
730 if(aconf
->hold
&& aconf
->hold
<= rb_current_time())
732 if(ConfigFileEntry
.tkline_expire_notices
)
733 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
734 "Temporary X-line for [%s] expired",
737 rb_dlinkDestroy(ptr
, &xline_conf_list
);
745 return(rb_dlink_list_length(&nd_list
));
749 add_nd_entry(const char *name
)
753 if(rb_dictionary_find(nd_dict
, name
) != NULL
)
756 nd
= rb_bh_alloc(nd_heap
);
758 rb_strlcpy(nd
->name
, name
, sizeof(nd
->name
));
759 nd
->expire
= rb_current_time() + ConfigFileEntry
.nick_delay
;
761 /* this list is ordered */
762 rb_dlinkAddTail(nd
, &nd
->lnode
, &nd_list
);
764 rb_dictionary_add(nd_dict
, nd
->name
, nd
);
768 free_nd_entry(struct nd_entry
*nd
)
770 rb_dictionary_delete(nd_dict
, nd
->name
);
772 rb_dlinkDelete(&nd
->lnode
, &nd_list
);
773 rb_bh_free(nd_heap
, nd
);
777 expire_nd_entries(void *unused
)
781 rb_dlink_node
*next_ptr
;
783 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, nd_list
.head
)
787 /* this list is ordered - we can stop when we hit the first
788 * entry that doesnt expire..
790 if(nd
->expire
> rb_current_time())
798 add_tgchange(const char *host
)
801 rb_patricia_node_t
*pnode
;
803 if(find_tgchange(host
))
806 target
= rb_malloc(sizeof(tgchange
));
807 pnode
= make_and_lookup(tgchange_tree
, host
);
809 pnode
->data
= target
;
810 target
->pnode
= pnode
;
812 target
->ip
= rb_strdup(host
);
813 target
->expiry
= rb_current_time() + (60*60*12);
815 rb_dlinkAdd(target
, &target
->node
, &tgchange_list
);
819 find_tgchange(const char *host
)
821 rb_patricia_node_t
*pnode
;
823 if((pnode
= rb_match_exact_string(tgchange_tree
, host
)))