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