]> jfr.im git - solanum.git/blame_incremental - ircd/s_newconf.c
Add .travis.yml
[solanum.git] / ircd / s_newconf.c
... / ...
CommitLineData
1/*
2 * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
3 * s_newconf.c - code for dealing with conf stuff
4 *
5 * Copyright (C) 2004 Lee Hardy <lee@leeh.co.uk>
6 * Copyright (C) 2004-2005 ircd-ratbox development team
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
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.
19 *
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.
31 *
32 * $Id: s_newconf.c 3508 2007-06-04 16:04:49Z jilles $
33 */
34
35#include "stdinc.h"
36#include "ircd_defs.h"
37#include "common.h"
38#include "s_conf.h"
39#include "s_newconf.h"
40#include "client.h"
41#include "s_serv.h"
42#include "send.h"
43#include "hostmask.h"
44#include "newconf.h"
45#include "hash.h"
46#include "irc_dictionary.h"
47#include "irc_radixtree.h"
48#include "s_assert.h"
49#include "logger.h"
50#include "dns.h"
51
52rb_dlink_list shared_conf_list;
53rb_dlink_list cluster_conf_list;
54rb_dlink_list oper_conf_list;
55rb_dlink_list hubleaf_conf_list;
56rb_dlink_list server_conf_list;
57rb_dlink_list xline_conf_list;
58rb_dlink_list resv_conf_list; /* nicks only! */
59rb_dlink_list nd_list; /* nick delay */
60rb_dlink_list tgchange_list;
61
62rb_patricia_tree_t *tgchange_tree;
63
64static rb_bh *nd_heap = NULL;
65
66static void expire_temp_rxlines(void *unused);
67static void expire_nd_entries(void *unused);
68
69struct ev_entry *expire_nd_entries_ev = NULL;
70struct ev_entry *expire_temp_rxlines_ev = NULL;
71
72void
73init_s_newconf(void)
74{
75 tgchange_tree = rb_new_patricia(PATRICIA_BITS);
76 nd_heap = rb_bh_create(sizeof(struct nd_entry), ND_HEAP_SIZE, "nd_heap");
77 expire_nd_entries_ev = rb_event_addish("expire_nd_entries", expire_nd_entries, NULL, 30);
78 expire_temp_rxlines_ev = rb_event_addish("expire_temp_rxlines", expire_temp_rxlines, NULL, 60);
79}
80
81void
82clear_s_newconf(void)
83{
84 struct server_conf *server_p;
85 rb_dlink_node *ptr;
86 rb_dlink_node *next_ptr;
87
88 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, shared_conf_list.head)
89 {
90 /* ptr here is ptr->data->node */
91 rb_dlinkDelete(ptr, &shared_conf_list);
92 free_remote_conf(ptr->data);
93 }
94
95 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, cluster_conf_list.head)
96 {
97 rb_dlinkDelete(ptr, &cluster_conf_list);
98 free_remote_conf(ptr->data);
99 }
100
101 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, hubleaf_conf_list.head)
102 {
103 rb_dlinkDelete(ptr, &hubleaf_conf_list);
104 free_remote_conf(ptr->data);
105 }
106
107 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, oper_conf_list.head)
108 {
109 free_oper_conf(ptr->data);
110 rb_dlinkDestroy(ptr, &oper_conf_list);
111 }
112
113 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, server_conf_list.head)
114 {
115 server_p = ptr->data;
116
117 if(!server_p->servers)
118 {
119 rb_dlinkDelete(ptr, &server_conf_list);
120 free_server_conf(ptr->data);
121 }
122 else
123 server_p->flags |= SERVER_ILLEGAL;
124 }
125}
126
127void
128clear_s_newconf_bans(void)
129{
130 struct ConfItem *aconf;
131 rb_dlink_node *ptr, *next_ptr;
132
133 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, xline_conf_list.head)
134 {
135 aconf = ptr->data;
136
137 if(aconf->hold)
138 continue;
139
140 free_conf(aconf);
141 rb_dlinkDestroy(ptr, &xline_conf_list);
142 }
143
144 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, resv_conf_list.head)
145 {
146 aconf = ptr->data;
147
148 /* temporary resv */
149 if(aconf->hold)
150 continue;
151
152 free_conf(aconf);
153 rb_dlinkDestroy(ptr, &resv_conf_list);
154 }
155
156 clear_resv_hash();
157}
158
159struct remote_conf *
160make_remote_conf(void)
161{
162 struct remote_conf *remote_p = rb_malloc(sizeof(struct remote_conf));
163 return remote_p;
164}
165
166void
167free_remote_conf(struct remote_conf *remote_p)
168{
169 s_assert(remote_p != NULL);
170 if(remote_p == NULL)
171 return;
172
173 rb_free(remote_p->username);
174 rb_free(remote_p->host);
175 rb_free(remote_p->server);
176 rb_free(remote_p);
177}
178
179int
180find_shared_conf(const char *username, const char *host,
181 const char *server, int flags)
182{
183 struct remote_conf *shared_p;
184 rb_dlink_node *ptr;
185
186 RB_DLINK_FOREACH(ptr, shared_conf_list.head)
187 {
188 shared_p = ptr->data;
189
190 if(match(shared_p->username, username) &&
191 match(shared_p->host, host) &&
192 match(shared_p->server, server))
193 {
194 if(shared_p->flags & flags)
195 return YES;
196 else
197 return NO;
198 }
199 }
200
201 return NO;
202}
203
204void
205propagate_generic(struct Client *source_p, const char *command,
206 const char *target, int cap, const char *format, ...)
207{
208 char buffer[BUFSIZE];
209 va_list args;
210
211 va_start(args, format);
212 rb_vsnprintf(buffer, sizeof(buffer), format, args);
213 va_end(args);
214
215 sendto_match_servs(source_p, target, cap, NOCAPS,
216 "%s %s %s",
217 command, target, buffer);
218 sendto_match_servs(source_p, target, CAP_ENCAP, cap,
219 "ENCAP %s %s %s",
220 target, command, buffer);
221}
222
223void
224cluster_generic(struct Client *source_p, const char *command,
225 int cltype, int cap, const char *format, ...)
226{
227 char buffer[BUFSIZE];
228 struct remote_conf *shared_p;
229 va_list args;
230 rb_dlink_node *ptr;
231
232 va_start(args, format);
233 rb_vsnprintf(buffer, sizeof(buffer), format, args);
234 va_end(args);
235
236 RB_DLINK_FOREACH(ptr, cluster_conf_list.head)
237 {
238 shared_p = ptr->data;
239
240 if(!(shared_p->flags & cltype))
241 continue;
242
243 sendto_match_servs(source_p, shared_p->server, cap, NOCAPS,
244 "%s %s %s",
245 command, shared_p->server, buffer);
246 sendto_match_servs(source_p, shared_p->server, CAP_ENCAP, cap,
247 "ENCAP %s %s %s",
248 shared_p->server, command, buffer);
249 }
250}
251
252struct oper_conf *
253make_oper_conf(void)
254{
255 struct oper_conf *oper_p = rb_malloc(sizeof(struct oper_conf));
256 return oper_p;
257}
258
259void
260free_oper_conf(struct oper_conf *oper_p)
261{
262 s_assert(oper_p != NULL);
263 if(oper_p == NULL)
264 return;
265
266 rb_free(oper_p->username);
267 rb_free(oper_p->host);
268 rb_free(oper_p->name);
269 rb_free(oper_p->certfp);
270
271 if(oper_p->passwd)
272 {
273 memset(oper_p->passwd, 0, strlen(oper_p->passwd));
274 rb_free(oper_p->passwd);
275 }
276
277#ifdef HAVE_LIBCRYPTO
278 rb_free(oper_p->rsa_pubkey_file);
279
280 if(oper_p->rsa_pubkey)
281 RSA_free(oper_p->rsa_pubkey);
282#endif
283
284 rb_free(oper_p);
285}
286
287struct oper_conf *
288find_oper_conf(const char *username, const char *host, const char *locip, const char *name)
289{
290 struct oper_conf *oper_p;
291 struct rb_sockaddr_storage ip, cip;
292 char addr[HOSTLEN+1];
293 int bits, cbits;
294 rb_dlink_node *ptr;
295
296 parse_netmask(locip, &cip, &cbits);
297
298 RB_DLINK_FOREACH(ptr, oper_conf_list.head)
299 {
300 oper_p = ptr->data;
301
302 /* name/username doesnt match.. */
303 if(irccmp(oper_p->name, name) || !match(oper_p->username, username))
304 continue;
305
306 rb_strlcpy(addr, oper_p->host, sizeof(addr));
307
308 if(parse_netmask(addr, &ip, &bits) != HM_HOST)
309 {
310 if(ip.ss_family == cip.ss_family &&
311 comp_with_mask_sock((struct sockaddr *)&ip, (struct sockaddr *)&cip, bits))
312 return oper_p;
313 }
314
315 /* we have to compare against the host as well, because its
316 * valid to set a spoof to an IP, which if we only compare
317 * in ip form to sockhost will not necessarily match --anfl
318 */
319 if(match(oper_p->host, host))
320 return oper_p;
321 }
322
323 return NULL;
324}
325
326struct server_conf *
327make_server_conf(void)
328{
329 struct server_conf *server_p = rb_malloc(sizeof(struct server_conf));
330 server_p->aftype = AF_INET;
331 return server_p;
332}
333
334void
335free_server_conf(struct server_conf *server_p)
336{
337 s_assert(server_p != NULL);
338 if(server_p == NULL)
339 return;
340
341 if(!EmptyString(server_p->passwd))
342 {
343 memset(server_p->passwd, 0, strlen(server_p->passwd));
344 rb_free(server_p->passwd);
345 }
346
347 if(!EmptyString(server_p->spasswd))
348 {
349 memset(server_p->spasswd, 0, strlen(server_p->spasswd));
350 rb_free(server_p->spasswd);
351 }
352
353 rb_free(server_p->name);
354 rb_free(server_p->host);
355 rb_free(server_p->class_name);
356 rb_free(server_p);
357}
358
359/*
360 * conf_dns_callback
361 * inputs - pointer to struct ConfItem
362 * - pointer to adns reply
363 * output - none
364 * side effects - called when resolver query finishes
365 * if the query resulted in a successful search, hp will contain
366 * a non-null pointer, otherwise hp will be null.
367 * if successful save hp in the conf item it was called with
368 */
369static void
370conf_dns_callback(const char *result, int status, int aftype, void *data)
371{
372 struct server_conf *server_p = data;
373
374 if(status == 1)
375 rb_inet_pton_sock(result, (struct sockaddr *)&server_p->my_ipnum);
376
377 server_p->dns_query = 0;
378}
379
380void
381add_server_conf(struct server_conf *server_p)
382{
383 if(EmptyString(server_p->class_name))
384 {
385 server_p->class_name = rb_strdup("default");
386 server_p->class = default_class;
387 return;
388 }
389
390 server_p->class = find_class(server_p->class_name);
391
392 if(server_p->class == default_class)
393 {
394 conf_report_error("Warning connect::class invalid for %s",
395 server_p->name);
396
397 rb_free(server_p->class_name);
398 server_p->class_name = rb_strdup("default");
399 }
400
401 if(strpbrk(server_p->host, "*?"))
402 return;
403
404 server_p->dns_query =
405 lookup_hostname(server_p->host, GET_SS_FAMILY(&server_p->my_ipnum), conf_dns_callback, server_p);
406}
407
408struct server_conf *
409find_server_conf(const char *name)
410{
411 struct server_conf *server_p;
412 rb_dlink_node *ptr;
413
414 RB_DLINK_FOREACH(ptr, server_conf_list.head)
415 {
416 server_p = ptr->data;
417
418 if(ServerConfIllegal(server_p))
419 continue;
420
421 if(match(name, server_p->name))
422 return server_p;
423 }
424
425 return NULL;
426}
427
428void
429attach_server_conf(struct Client *client_p, struct server_conf *server_p)
430{
431 /* already have an attached conf */
432 if(client_p->localClient->att_sconf)
433 {
434 /* short circuit this special case :) */
435 if(client_p->localClient->att_sconf == server_p)
436 return;
437
438 detach_server_conf(client_p);
439 }
440
441 CurrUsers(server_p->class)++;
442
443 client_p->localClient->att_sconf = server_p;
444 server_p->servers++;
445}
446
447void
448detach_server_conf(struct Client *client_p)
449{
450 struct server_conf *server_p = client_p->localClient->att_sconf;
451
452 if(server_p == NULL)
453 return;
454
455 client_p->localClient->att_sconf = NULL;
456 server_p->servers--;
457 CurrUsers(server_p->class)--;
458
459 if(ServerConfIllegal(server_p) && !server_p->servers)
460 {
461 /* the class this one is using may need destroying too */
462 if(MaxUsers(server_p->class) < 0 && CurrUsers(server_p->class) <= 0)
463 free_class(server_p->class);
464
465 rb_dlinkDelete(&server_p->node, &server_conf_list);
466 free_server_conf(server_p);
467 }
468}
469
470void
471set_server_conf_autoconn(struct Client *source_p, const char *name, int newval)
472{
473 struct server_conf *server_p;
474
475 if((server_p = find_server_conf(name)) != NULL)
476 {
477 if(newval)
478 server_p->flags |= SERVER_AUTOCONN;
479 else
480 server_p->flags &= ~SERVER_AUTOCONN;
481
482 sendto_realops_snomask(SNO_GENERAL, L_ALL,
483 "%s has changed AUTOCONN for %s to %i",
484 get_oper_name(source_p), name, newval);
485 }
486 else
487 sendto_one_notice(source_p, ":Can't find %s", name);
488}
489
490void
491disable_server_conf_autoconn(const char *name)
492{
493 struct server_conf *server_p;
494
495 server_p = find_server_conf(name);
496 if(server_p != NULL && server_p->flags & SERVER_AUTOCONN)
497 {
498 server_p->flags &= ~SERVER_AUTOCONN;
499
500 sendto_realops_snomask(SNO_GENERAL, L_ALL,
501 "Disabling AUTOCONN for %s because of error",
502 name);
503 ilog(L_SERVER, "Disabling AUTOCONN for %s because of error",
504 name);
505 }
506}
507
508struct ConfItem *
509find_xline(const char *gecos, int counter)
510{
511 struct ConfItem *aconf;
512 rb_dlink_node *ptr;
513
514 RB_DLINK_FOREACH(ptr, xline_conf_list.head)
515 {
516 aconf = ptr->data;
517
518 if(match_esc(aconf->host, gecos))
519 {
520 if(counter)
521 aconf->port++;
522 return aconf;
523 }
524 }
525
526 return NULL;
527}
528
529struct ConfItem *
530find_xline_mask(const char *gecos)
531{
532 struct ConfItem *aconf;
533 rb_dlink_node *ptr;
534
535 RB_DLINK_FOREACH(ptr, xline_conf_list.head)
536 {
537 aconf = ptr->data;
538
539 if(!irccmp(aconf->host, gecos))
540 return aconf;
541 }
542
543 return NULL;
544}
545
546struct ConfItem *
547find_nick_resv(const char *name)
548{
549 struct ConfItem *aconf;
550 rb_dlink_node *ptr;
551
552 RB_DLINK_FOREACH(ptr, resv_conf_list.head)
553 {
554 aconf = ptr->data;
555
556 if(match_esc(aconf->host, name))
557 {
558 aconf->port++;
559 return aconf;
560 }
561 }
562
563 return NULL;
564}
565
566struct ConfItem *
567find_nick_resv_mask(const char *name)
568{
569 struct ConfItem *aconf;
570 rb_dlink_node *ptr;
571
572 RB_DLINK_FOREACH(ptr, resv_conf_list.head)
573 {
574 aconf = ptr->data;
575
576 if(!irccmp(aconf->host, name))
577 return aconf;
578 }
579
580 return NULL;
581}
582
583/* clean_resv_nick()
584 *
585 * inputs - nick
586 * outputs - 1 if nick is vaild resv, 0 otherwise
587 * side effects -
588 */
589int
590clean_resv_nick(const char *nick)
591{
592 char tmpch;
593 int as = 0;
594 int q = 0;
595 int ch = 0;
596
597 if(*nick == '-' || IsDigit(*nick))
598 return 0;
599
600 while ((tmpch = *nick++))
601 {
602 if(tmpch == '?' || tmpch == '@' || tmpch == '#')
603 q++;
604 else if(tmpch == '*')
605 as++;
606 else if(IsNickChar(tmpch))
607 ch++;
608 else
609 return 0;
610 }
611
612 if(!ch && as)
613 return 0;
614
615 return 1;
616}
617
618/* valid_wild_card_simple()
619 *
620 * inputs - "thing" to test
621 * outputs - 1 if enough wildcards, else 0
622 * side effects -
623 */
624int
625valid_wild_card_simple(const char *data)
626{
627 const char *p;
628 char tmpch;
629 int nonwild = 0;
630 int wild = 0;
631
632 /* check the string for minimum number of nonwildcard chars */
633 p = data;
634
635 while((tmpch = *p++))
636 {
637 /* found an escape, p points to the char after it, so skip
638 * that and move on.
639 */
640 if(tmpch == '\\' && *p)
641 {
642 p++;
643 if(++nonwild >= ConfigFileEntry.min_nonwildcard_simple)
644 return 1;
645 }
646 else if(!IsMWildChar(tmpch))
647 {
648 /* if we have enough nonwildchars, return */
649 if(++nonwild >= ConfigFileEntry.min_nonwildcard_simple)
650 return 1;
651 }
652 else
653 wild++;
654 }
655
656 /* strings without wilds are also ok */
657 return wild == 0;
658}
659
660time_t
661valid_temp_time(const char *p)
662{
663 time_t result = 0;
664
665 while(*p)
666 {
667 if(IsDigit(*p))
668 {
669 result *= 10;
670 result += ((*p) & 0xF);
671 p++;
672 }
673 else
674 return -1;
675 }
676
677 if(result > (60 * 24 * 7 * 52))
678 result = (60 * 24 * 7 * 52);
679
680 return(result * 60);
681}
682
683/* Propagated bans are expired elsewhere. */
684static void
685expire_temp_rxlines(void *unused)
686{
687 struct ConfItem *aconf;
688 rb_dlink_node *ptr;
689 rb_dlink_node *next_ptr;
690 int i;
691 struct irc_radixtree_iteration_state state;
692
693 IRC_RADIXTREE_FOREACH(aconf, &state, resv_tree)
694 {
695 if(aconf->lifetime != 0)
696 continue;
697 if(aconf->hold && aconf->hold <= rb_current_time())
698 {
699 if(ConfigFileEntry.tkline_expire_notices)
700 sendto_realops_snomask(SNO_GENERAL, L_ALL,
701 "Temporary RESV for [%s] expired",
702 aconf->host);
703
704 irc_radixtree_delete(resv_tree, aconf->host);
705 free_conf(aconf);
706 }
707 }
708
709 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, resv_conf_list.head)
710 {
711 aconf = ptr->data;
712
713 if(aconf->lifetime != 0)
714 continue;
715 if(aconf->hold && aconf->hold <= rb_current_time())
716 {
717 if(ConfigFileEntry.tkline_expire_notices)
718 sendto_realops_snomask(SNO_GENERAL, L_ALL,
719 "Temporary RESV for [%s] expired",
720 aconf->host);
721 free_conf(aconf);
722 rb_dlinkDestroy(ptr, &resv_conf_list);
723 }
724 }
725
726 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, xline_conf_list.head)
727 {
728 aconf = ptr->data;
729
730 if(aconf->lifetime != 0)
731 continue;
732 if(aconf->hold && aconf->hold <= rb_current_time())
733 {
734 if(ConfigFileEntry.tkline_expire_notices)
735 sendto_realops_snomask(SNO_GENERAL, L_ALL,
736 "Temporary X-line for [%s] expired",
737 aconf->host);
738 free_conf(aconf);
739 rb_dlinkDestroy(ptr, &xline_conf_list);
740 }
741 }
742}
743
744unsigned long
745get_nd_count(void)
746{
747 return(rb_dlink_list_length(&nd_list));
748}
749
750void
751add_nd_entry(const char *name)
752{
753 struct nd_entry *nd;
754
755 if(irc_dictionary_find(nd_dict, name) != NULL)
756 return;
757
758 nd = rb_bh_alloc(nd_heap);
759
760 rb_strlcpy(nd->name, name, sizeof(nd->name));
761 nd->expire = rb_current_time() + ConfigFileEntry.nick_delay;
762
763 /* this list is ordered */
764 rb_dlinkAddTail(nd, &nd->lnode, &nd_list);
765
766 irc_dictionary_add(nd_dict, nd->name, nd);
767}
768
769void
770free_nd_entry(struct nd_entry *nd)
771{
772 irc_dictionary_delete(nd_dict, nd->name);
773
774 rb_dlinkDelete(&nd->lnode, &nd_list);
775 rb_bh_free(nd_heap, nd);
776}
777
778void
779expire_nd_entries(void *unused)
780{
781 struct nd_entry *nd;
782 rb_dlink_node *ptr;
783 rb_dlink_node *next_ptr;
784
785 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, nd_list.head)
786 {
787 nd = ptr->data;
788
789 /* this list is ordered - we can stop when we hit the first
790 * entry that doesnt expire..
791 */
792 if(nd->expire > rb_current_time())
793 return;
794
795 free_nd_entry(nd);
796 }
797}
798
799void
800add_tgchange(const char *host)
801{
802 tgchange *target;
803 rb_patricia_node_t *pnode;
804
805 if(find_tgchange(host))
806 return;
807
808 target = rb_malloc(sizeof(tgchange));
809 pnode = make_and_lookup(tgchange_tree, host);
810
811 pnode->data = target;
812 target->pnode = pnode;
813
814 target->ip = rb_strdup(host);
815 target->expiry = rb_current_time() + (60*60*12);
816
817 rb_dlinkAdd(target, &target->node, &tgchange_list);
818}
819
820tgchange *
821find_tgchange(const char *host)
822{
823 rb_patricia_node_t *pnode;
824
825 if((pnode = rb_match_exact_string(tgchange_tree, host)))
826 return pnode->data;
827
828 return NULL;
829}
830