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"
51 rb_dlink_list shared_conf_list
;
52 rb_dlink_list cluster_conf_list
;
53 rb_dlink_list oper_conf_list
;
54 rb_dlink_list hubleaf_conf_list
;
55 rb_dlink_list server_conf_list
;
56 rb_dlink_list xline_conf_list
;
57 rb_dlink_list resv_conf_list
; /* nicks only! */
58 rb_dlink_list nd_list
; /* nick delay */
59 rb_dlink_list tgchange_list
;
61 rb_patricia_tree_t
*tgchange_tree
;
63 static rb_bh
*nd_heap
= NULL
;
65 static void expire_temp_rxlines(void *unused
);
66 static void expire_nd_entries(void *unused
);
68 struct ev_entry
*expire_nd_entries_ev
= NULL
;
69 struct ev_entry
*expire_temp_rxlines_ev
= NULL
;
74 tgchange_tree
= rb_new_patricia(PATRICIA_BITS
);
75 nd_heap
= rb_bh_create(sizeof(struct nd_entry
), ND_HEAP_SIZE
, "nd_heap");
76 expire_nd_entries_ev
= rb_event_addish("expire_nd_entries", expire_nd_entries
, NULL
, 30);
77 expire_temp_rxlines_ev
= rb_event_addish("expire_temp_rxlines", expire_temp_rxlines
, NULL
, 60);
83 struct server_conf
*server_p
;
85 rb_dlink_node
*next_ptr
;
87 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, shared_conf_list
.head
)
89 /* ptr here is ptr->data->node */
90 rb_dlinkDelete(ptr
, &shared_conf_list
);
91 free_remote_conf(ptr
->data
);
94 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, cluster_conf_list
.head
)
96 rb_dlinkDelete(ptr
, &cluster_conf_list
);
97 free_remote_conf(ptr
->data
);
100 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, hubleaf_conf_list
.head
)
102 rb_dlinkDelete(ptr
, &hubleaf_conf_list
);
103 free_remote_conf(ptr
->data
);
106 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, oper_conf_list
.head
)
108 free_oper_conf(ptr
->data
);
109 rb_dlinkDestroy(ptr
, &oper_conf_list
);
112 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, server_conf_list
.head
)
114 server_p
= ptr
->data
;
116 if(!server_p
->servers
)
118 rb_dlinkDelete(ptr
, &server_conf_list
);
119 free_server_conf(ptr
->data
);
122 server_p
->flags
|= SERVER_ILLEGAL
;
127 clear_s_newconf_bans(void)
129 struct ConfItem
*aconf
;
130 rb_dlink_node
*ptr
, *next_ptr
;
132 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, xline_conf_list
.head
)
140 rb_dlinkDestroy(ptr
, &xline_conf_list
);
143 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, resv_conf_list
.head
)
152 rb_dlinkDestroy(ptr
, &resv_conf_list
);
159 make_remote_conf(void)
161 struct remote_conf
*remote_p
= rb_malloc(sizeof(struct remote_conf
));
166 free_remote_conf(struct remote_conf
*remote_p
)
168 s_assert(remote_p
!= NULL
);
172 rb_free(remote_p
->username
);
173 rb_free(remote_p
->host
);
174 rb_free(remote_p
->server
);
179 find_shared_conf(const char *username
, const char *host
,
180 const char *server
, int flags
)
182 struct remote_conf
*shared_p
;
185 RB_DLINK_FOREACH(ptr
, shared_conf_list
.head
)
187 shared_p
= ptr
->data
;
189 if(match(shared_p
->username
, username
) &&
190 match(shared_p
->host
, host
) &&
191 match(shared_p
->server
, server
))
193 if(shared_p
->flags
& flags
)
204 propagate_generic(struct Client
*source_p
, const char *command
,
205 const char *target
, int cap
, const char *format
, ...)
207 char buffer
[BUFSIZE
];
210 va_start(args
, format
);
211 rb_vsnprintf(buffer
, sizeof(buffer
), format
, args
);
214 sendto_match_servs(source_p
, target
, cap
, NOCAPS
,
216 command
, target
, buffer
);
217 sendto_match_servs(source_p
, target
, CAP_ENCAP
, cap
,
219 target
, command
, buffer
);
223 cluster_generic(struct Client
*source_p
, const char *command
,
224 int cltype
, int cap
, const char *format
, ...)
226 char buffer
[BUFSIZE
];
227 struct remote_conf
*shared_p
;
231 va_start(args
, format
);
232 rb_vsnprintf(buffer
, sizeof(buffer
), format
, args
);
235 RB_DLINK_FOREACH(ptr
, cluster_conf_list
.head
)
237 shared_p
= ptr
->data
;
239 if(!(shared_p
->flags
& cltype
))
242 sendto_match_servs(source_p
, shared_p
->server
, cap
, NOCAPS
,
244 command
, shared_p
->server
, buffer
);
245 sendto_match_servs(source_p
, shared_p
->server
, CAP_ENCAP
, cap
,
247 shared_p
->server
, command
, buffer
);
254 struct oper_conf
*oper_p
= rb_malloc(sizeof(struct oper_conf
));
259 free_oper_conf(struct oper_conf
*oper_p
)
261 s_assert(oper_p
!= NULL
);
265 rb_free(oper_p
->username
);
266 rb_free(oper_p
->host
);
267 rb_free(oper_p
->name
);
268 rb_free(oper_p
->certfp
);
272 memset(oper_p
->passwd
, 0, strlen(oper_p
->passwd
));
273 rb_free(oper_p
->passwd
);
276 #ifdef HAVE_LIBCRYPTO
277 rb_free(oper_p
->rsa_pubkey_file
);
279 if(oper_p
->rsa_pubkey
)
280 RSA_free(oper_p
->rsa_pubkey
);
287 find_oper_conf(const char *username
, const char *host
, const char *locip
, const char *name
)
289 struct oper_conf
*oper_p
;
290 struct rb_sockaddr_storage ip
, cip
;
291 char addr
[HOSTLEN
+1];
295 parse_netmask(locip
, &cip
, &cbits
);
297 RB_DLINK_FOREACH(ptr
, oper_conf_list
.head
)
301 /* name/username doesnt match.. */
302 if(irccmp(oper_p
->name
, name
) || !match(oper_p
->username
, username
))
305 rb_strlcpy(addr
, oper_p
->host
, sizeof(addr
));
307 if(parse_netmask(addr
, &ip
, &bits
) != HM_HOST
)
309 if(ip
.ss_family
== cip
.ss_family
&&
310 comp_with_mask_sock((struct sockaddr
*)&ip
, (struct sockaddr
*)&cip
, bits
))
314 /* we have to compare against the host as well, because its
315 * valid to set a spoof to an IP, which if we only compare
316 * in ip form to sockhost will not necessarily match --anfl
318 if(match(oper_p
->host
, host
))
326 make_server_conf(void)
328 struct server_conf
*server_p
= rb_malloc(sizeof(struct server_conf
));
329 server_p
->aftype
= AF_INET
;
334 free_server_conf(struct server_conf
*server_p
)
336 s_assert(server_p
!= NULL
);
340 if(!EmptyString(server_p
->passwd
))
342 memset(server_p
->passwd
, 0, strlen(server_p
->passwd
));
343 rb_free(server_p
->passwd
);
346 if(!EmptyString(server_p
->spasswd
))
348 memset(server_p
->spasswd
, 0, strlen(server_p
->spasswd
));
349 rb_free(server_p
->spasswd
);
352 rb_free(server_p
->name
);
353 rb_free(server_p
->host
);
354 rb_free(server_p
->class_name
);
360 * inputs - pointer to struct ConfItem
361 * - pointer to adns reply
363 * side effects - called when resolver query finishes
364 * if the query resulted in a successful search, hp will contain
365 * a non-null pointer, otherwise hp will be null.
366 * if successful save hp in the conf item it was called with
369 conf_dns_callback(const char *result
, int status
, int aftype
, void *data
)
371 struct server_conf
*server_p
= data
;
374 rb_inet_pton_sock(result
, (struct sockaddr
*)&server_p
->my_ipnum
);
376 server_p
->dns_query
= 0;
380 add_server_conf(struct server_conf
*server_p
)
382 if(EmptyString(server_p
->class_name
))
384 server_p
->class_name
= rb_strdup("default");
385 server_p
->class = default_class
;
389 server_p
->class = find_class(server_p
->class_name
);
391 if(server_p
->class == default_class
)
393 conf_report_error("Warning connect::class invalid for %s",
396 rb_free(server_p
->class_name
);
397 server_p
->class_name
= rb_strdup("default");
400 if(strpbrk(server_p
->host
, "*?"))
403 server_p
->dns_query
=
404 lookup_hostname(server_p
->host
, GET_SS_FAMILY(&server_p
->my_ipnum
), conf_dns_callback
, server_p
);
408 find_server_conf(const char *name
)
410 struct server_conf
*server_p
;
413 RB_DLINK_FOREACH(ptr
, server_conf_list
.head
)
415 server_p
= ptr
->data
;
417 if(ServerConfIllegal(server_p
))
420 if(match(name
, server_p
->name
))
428 attach_server_conf(struct Client
*client_p
, struct server_conf
*server_p
)
430 /* already have an attached conf */
431 if(client_p
->localClient
->att_sconf
)
433 /* short circuit this special case :) */
434 if(client_p
->localClient
->att_sconf
== server_p
)
437 detach_server_conf(client_p
);
440 CurrUsers(server_p
->class)++;
442 client_p
->localClient
->att_sconf
= server_p
;
447 detach_server_conf(struct Client
*client_p
)
449 struct server_conf
*server_p
= client_p
->localClient
->att_sconf
;
454 client_p
->localClient
->att_sconf
= NULL
;
456 CurrUsers(server_p
->class)--;
458 if(ServerConfIllegal(server_p
) && !server_p
->servers
)
460 /* the class this one is using may need destroying too */
461 if(MaxUsers(server_p
->class) < 0 && CurrUsers(server_p
->class) <= 0)
462 free_class(server_p
->class);
464 rb_dlinkDelete(&server_p
->node
, &server_conf_list
);
465 free_server_conf(server_p
);
470 set_server_conf_autoconn(struct Client
*source_p
, const char *name
, int newval
)
472 struct server_conf
*server_p
;
474 if((server_p
= find_server_conf(name
)) != NULL
)
477 server_p
->flags
|= SERVER_AUTOCONN
;
479 server_p
->flags
&= ~SERVER_AUTOCONN
;
481 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
482 "%s has changed AUTOCONN for %s to %i",
483 get_oper_name(source_p
), name
, newval
);
486 sendto_one_notice(source_p
, ":Can't find %s", name
);
490 disable_server_conf_autoconn(const char *name
)
492 struct server_conf
*server_p
;
494 server_p
= find_server_conf(name
);
495 if(server_p
!= NULL
&& server_p
->flags
& SERVER_AUTOCONN
)
497 server_p
->flags
&= ~SERVER_AUTOCONN
;
499 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
500 "Disabling AUTOCONN for %s because of error",
502 ilog(L_SERVER
, "Disabling AUTOCONN for %s because of error",
508 find_xline(const char *gecos
, int counter
)
510 struct ConfItem
*aconf
;
513 RB_DLINK_FOREACH(ptr
, xline_conf_list
.head
)
517 if(match_esc(aconf
->host
, gecos
))
529 find_xline_mask(const char *gecos
)
531 struct ConfItem
*aconf
;
534 RB_DLINK_FOREACH(ptr
, xline_conf_list
.head
)
538 if(!irccmp(aconf
->host
, gecos
))
546 find_nick_resv(const char *name
)
548 struct ConfItem
*aconf
;
551 RB_DLINK_FOREACH(ptr
, resv_conf_list
.head
)
555 if(match_esc(aconf
->host
, name
))
566 find_nick_resv_mask(const char *name
)
568 struct ConfItem
*aconf
;
571 RB_DLINK_FOREACH(ptr
, resv_conf_list
.head
)
575 if(!irccmp(aconf
->host
, name
))
585 * outputs - 1 if nick is vaild resv, 0 otherwise
589 clean_resv_nick(const char *nick
)
596 if(*nick
== '-' || IsDigit(*nick
))
599 while ((tmpch
= *nick
++))
601 if(tmpch
== '?' || tmpch
== '@' || tmpch
== '#')
603 else if(tmpch
== '*')
605 else if(IsNickChar(tmpch
))
617 /* valid_wild_card_simple()
619 * inputs - "thing" to test
620 * outputs - 1 if enough wildcards, else 0
624 valid_wild_card_simple(const char *data
)
631 /* check the string for minimum number of nonwildcard chars */
634 while((tmpch
= *p
++))
636 /* found an escape, p points to the char after it, so skip
639 if(tmpch
== '\\' && *p
)
642 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard_simple
)
645 else if(!IsMWildChar(tmpch
))
647 /* if we have enough nonwildchars, return */
648 if(++nonwild
>= ConfigFileEntry
.min_nonwildcard_simple
)
655 /* strings without wilds are also ok */
660 valid_temp_time(const char *p
)
669 result
+= ((*p
) & 0xF);
676 if(result
> (60 * 24 * 7 * 52))
677 result
= (60 * 24 * 7 * 52);
682 /* Propagated bans are expired elsewhere. */
684 expire_temp_rxlines(void *unused
)
686 struct ConfItem
*aconf
;
688 rb_dlink_node
*next_ptr
;
691 HASH_WALK_SAFE(i
, R_MAX
, ptr
, next_ptr
, resvTable
)
695 if(aconf
->lifetime
!= 0)
697 if(aconf
->hold
&& aconf
->hold
<= rb_current_time())
699 if(ConfigFileEntry
.tkline_expire_notices
)
700 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
701 "Temporary RESV for [%s] expired",
705 rb_dlinkDestroy(ptr
, &resvTable
[i
]);
710 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, resv_conf_list
.head
)
714 if(aconf
->lifetime
!= 0)
716 if(aconf
->hold
&& aconf
->hold
<= rb_current_time())
718 if(ConfigFileEntry
.tkline_expire_notices
)
719 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
720 "Temporary RESV for [%s] expired",
723 rb_dlinkDestroy(ptr
, &resv_conf_list
);
727 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, xline_conf_list
.head
)
731 if(aconf
->lifetime
!= 0)
733 if(aconf
->hold
&& aconf
->hold
<= rb_current_time())
735 if(ConfigFileEntry
.tkline_expire_notices
)
736 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
737 "Temporary X-line for [%s] expired",
740 rb_dlinkDestroy(ptr
, &xline_conf_list
);
748 return(rb_dlink_list_length(&nd_list
));
752 add_nd_entry(const char *name
)
756 if(irc_dictionary_find(nd_dict
, name
) != NULL
)
759 nd
= rb_bh_alloc(nd_heap
);
761 rb_strlcpy(nd
->name
, name
, sizeof(nd
->name
));
762 nd
->expire
= rb_current_time() + ConfigFileEntry
.nick_delay
;
764 /* this list is ordered */
765 rb_dlinkAddTail(nd
, &nd
->lnode
, &nd_list
);
767 irc_dictionary_add(nd_dict
, nd
->name
, nd
);
771 free_nd_entry(struct nd_entry
*nd
)
773 irc_dictionary_delete(nd_dict
, nd
->name
);
775 rb_dlinkDelete(&nd
->lnode
, &nd_list
);
776 rb_bh_free(nd_heap
, nd
);
780 expire_nd_entries(void *unused
)
784 rb_dlink_node
*next_ptr
;
786 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, nd_list
.head
)
790 /* this list is ordered - we can stop when we hit the first
791 * entry that doesnt expire..
793 if(nd
->expire
> rb_current_time())
801 add_tgchange(const char *host
)
804 rb_patricia_node_t
*pnode
;
806 if(find_tgchange(host
))
809 target
= rb_malloc(sizeof(tgchange
));
810 pnode
= make_and_lookup(tgchange_tree
, host
);
812 pnode
->data
= target
;
813 target
->pnode
= pnode
;
815 target
->ip
= rb_strdup(host
);
816 target
->expiry
= rb_current_time() + (60*60*12);
818 rb_dlinkAdd(target
, &target
->node
, &tgchange_list
);
822 find_tgchange(const char *host
)
824 rb_patricia_node_t
*pnode
;
826 if((pnode
= rb_match_exact_string(tgchange_tree
, host
)))