]> jfr.im git - solanum.git/blob - ircd/s_newconf.c
authd: do not use \n with rb_helper_write(), unnecessary
[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 * $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 "s_assert.h"
48 #include "logger.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 int
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 YES;
194 else
195 return NO;
196 }
197 }
198
199 return NO;
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 rb_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 rb_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(ip.ss_family == cip.ss_family &&
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 void
358 add_server_conf(struct server_conf *server_p)
359 {
360 if(EmptyString(server_p->class_name))
361 {
362 server_p->class_name = rb_strdup("default");
363 server_p->class = default_class;
364 return;
365 }
366
367 server_p->class = find_class(server_p->class_name);
368
369 if(server_p->class == default_class)
370 {
371 conf_report_error("Warning connect::class invalid for %s",
372 server_p->name);
373
374 rb_free(server_p->class_name);
375 server_p->class_name = rb_strdup("default");
376 }
377
378 if(strchr(server_p->host, '*') || strchr(server_p->host, '?'))
379 return;
380 }
381
382 struct server_conf *
383 find_server_conf(const char *name)
384 {
385 struct server_conf *server_p;
386 rb_dlink_node *ptr;
387
388 RB_DLINK_FOREACH(ptr, server_conf_list.head)
389 {
390 server_p = ptr->data;
391
392 if(ServerConfIllegal(server_p))
393 continue;
394
395 if(match(name, server_p->name))
396 return server_p;
397 }
398
399 return NULL;
400 }
401
402 void
403 attach_server_conf(struct Client *client_p, struct server_conf *server_p)
404 {
405 /* already have an attached conf */
406 if(client_p->localClient->att_sconf)
407 {
408 /* short circuit this special case :) */
409 if(client_p->localClient->att_sconf == server_p)
410 return;
411
412 detach_server_conf(client_p);
413 }
414
415 CurrUsers(server_p->class)++;
416
417 client_p->localClient->att_sconf = server_p;
418 server_p->servers++;
419 }
420
421 void
422 detach_server_conf(struct Client *client_p)
423 {
424 struct server_conf *server_p = client_p->localClient->att_sconf;
425
426 if(server_p == NULL)
427 return;
428
429 client_p->localClient->att_sconf = NULL;
430 server_p->servers--;
431 CurrUsers(server_p->class)--;
432
433 if(ServerConfIllegal(server_p) && !server_p->servers)
434 {
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);
438
439 rb_dlinkDelete(&server_p->node, &server_conf_list);
440 free_server_conf(server_p);
441 }
442 }
443
444 void
445 set_server_conf_autoconn(struct Client *source_p, const char *name, int newval)
446 {
447 struct server_conf *server_p;
448
449 if((server_p = find_server_conf(name)) != NULL)
450 {
451 if(newval)
452 server_p->flags |= SERVER_AUTOCONN;
453 else
454 server_p->flags &= ~SERVER_AUTOCONN;
455
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);
459 }
460 else
461 sendto_one_notice(source_p, ":Can't find %s", name);
462 }
463
464 void
465 disable_server_conf_autoconn(const char *name)
466 {
467 struct server_conf *server_p;
468
469 server_p = find_server_conf(name);
470 if(server_p != NULL && server_p->flags & SERVER_AUTOCONN)
471 {
472 server_p->flags &= ~SERVER_AUTOCONN;
473
474 sendto_realops_snomask(SNO_GENERAL, L_ALL,
475 "Disabling AUTOCONN for %s because of error",
476 name);
477 ilog(L_SERVER, "Disabling AUTOCONN for %s because of error",
478 name);
479 }
480 }
481
482 struct ConfItem *
483 find_xline(const char *gecos, int counter)
484 {
485 struct ConfItem *aconf;
486 rb_dlink_node *ptr;
487
488 RB_DLINK_FOREACH(ptr, xline_conf_list.head)
489 {
490 aconf = ptr->data;
491
492 if(match_esc(aconf->host, gecos))
493 {
494 if(counter)
495 aconf->port++;
496 return aconf;
497 }
498 }
499
500 return NULL;
501 }
502
503 struct ConfItem *
504 find_xline_mask(const char *gecos)
505 {
506 struct ConfItem *aconf;
507 rb_dlink_node *ptr;
508
509 RB_DLINK_FOREACH(ptr, xline_conf_list.head)
510 {
511 aconf = ptr->data;
512
513 if(!irccmp(aconf->host, gecos))
514 return aconf;
515 }
516
517 return NULL;
518 }
519
520 struct ConfItem *
521 find_nick_resv(const char *name)
522 {
523 struct ConfItem *aconf;
524 rb_dlink_node *ptr;
525
526 RB_DLINK_FOREACH(ptr, resv_conf_list.head)
527 {
528 aconf = ptr->data;
529
530 if(match_esc(aconf->host, name))
531 {
532 aconf->port++;
533 return aconf;
534 }
535 }
536
537 return NULL;
538 }
539
540 struct ConfItem *
541 find_nick_resv_mask(const char *name)
542 {
543 struct ConfItem *aconf;
544 rb_dlink_node *ptr;
545
546 RB_DLINK_FOREACH(ptr, resv_conf_list.head)
547 {
548 aconf = ptr->data;
549
550 if(!irccmp(aconf->host, name))
551 return aconf;
552 }
553
554 return NULL;
555 }
556
557 /* clean_resv_nick()
558 *
559 * inputs - nick
560 * outputs - 1 if nick is vaild resv, 0 otherwise
561 * side effects -
562 */
563 int
564 clean_resv_nick(const char *nick)
565 {
566 char tmpch;
567 int as = 0;
568 int q = 0;
569 int ch = 0;
570
571 if(*nick == '-' || IsDigit(*nick))
572 return 0;
573
574 while ((tmpch = *nick++))
575 {
576 if(tmpch == '?' || tmpch == '@' || tmpch == '#')
577 q++;
578 else if(tmpch == '*')
579 as++;
580 else if(IsNickChar(tmpch))
581 ch++;
582 else
583 return 0;
584 }
585
586 if(!ch && as)
587 return 0;
588
589 return 1;
590 }
591
592 /* valid_wild_card_simple()
593 *
594 * inputs - "thing" to test
595 * outputs - 1 if enough wildcards, else 0
596 * side effects -
597 */
598 int
599 valid_wild_card_simple(const char *data)
600 {
601 const char *p;
602 char tmpch;
603 int nonwild = 0;
604 int wild = 0;
605
606 /* check the string for minimum number of nonwildcard chars */
607 p = data;
608
609 while((tmpch = *p++))
610 {
611 /* found an escape, p points to the char after it, so skip
612 * that and move on.
613 */
614 if(tmpch == '\\' && *p)
615 {
616 p++;
617 if(++nonwild >= ConfigFileEntry.min_nonwildcard_simple)
618 return 1;
619 }
620 else if(!IsMWildChar(tmpch))
621 {
622 /* if we have enough nonwildchars, return */
623 if(++nonwild >= ConfigFileEntry.min_nonwildcard_simple)
624 return 1;
625 }
626 else
627 wild++;
628 }
629
630 /* strings without wilds are also ok */
631 return wild == 0;
632 }
633
634 time_t
635 valid_temp_time(const char *p)
636 {
637 time_t result = 0;
638
639 while(*p)
640 {
641 if(IsDigit(*p))
642 {
643 result *= 10;
644 result += ((*p) & 0xF);
645 p++;
646 }
647 else
648 return -1;
649 }
650
651 if(result > (60 * 24 * 7 * 52))
652 result = (60 * 24 * 7 * 52);
653
654 return(result * 60);
655 }
656
657 /* Propagated bans are expired elsewhere. */
658 static void
659 expire_temp_rxlines(void *unused)
660 {
661 struct ConfItem *aconf;
662 rb_dlink_node *ptr;
663 rb_dlink_node *next_ptr;
664 int i;
665
666 HASH_WALK_SAFE(i, R_MAX, ptr, next_ptr, resvTable)
667 {
668 aconf = ptr->data;
669
670 if(aconf->lifetime != 0)
671 continue;
672 if(aconf->hold && aconf->hold <= rb_current_time())
673 {
674 if(ConfigFileEntry.tkline_expire_notices)
675 sendto_realops_snomask(SNO_GENERAL, L_ALL,
676 "Temporary RESV for [%s] expired",
677 aconf->host);
678
679 free_conf(aconf);
680 rb_dlinkDestroy(ptr, &resvTable[i]);
681 }
682 }
683 HASH_WALK_END
684
685 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, resv_conf_list.head)
686 {
687 aconf = ptr->data;
688
689 if(aconf->lifetime != 0)
690 continue;
691 if(aconf->hold && aconf->hold <= rb_current_time())
692 {
693 if(ConfigFileEntry.tkline_expire_notices)
694 sendto_realops_snomask(SNO_GENERAL, L_ALL,
695 "Temporary RESV for [%s] expired",
696 aconf->host);
697 free_conf(aconf);
698 rb_dlinkDestroy(ptr, &resv_conf_list);
699 }
700 }
701
702 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, xline_conf_list.head)
703 {
704 aconf = ptr->data;
705
706 if(aconf->lifetime != 0)
707 continue;
708 if(aconf->hold && aconf->hold <= rb_current_time())
709 {
710 if(ConfigFileEntry.tkline_expire_notices)
711 sendto_realops_snomask(SNO_GENERAL, L_ALL,
712 "Temporary X-line for [%s] expired",
713 aconf->host);
714 free_conf(aconf);
715 rb_dlinkDestroy(ptr, &xline_conf_list);
716 }
717 }
718 }
719
720 unsigned long
721 get_nd_count(void)
722 {
723 return(rb_dlink_list_length(&nd_list));
724 }
725
726 void
727 add_nd_entry(const char *name)
728 {
729 struct nd_entry *nd;
730
731 if(irc_dictionary_find(nd_dict, name) != NULL)
732 return;
733
734 nd = rb_bh_alloc(nd_heap);
735
736 rb_strlcpy(nd->name, name, sizeof(nd->name));
737 nd->expire = rb_current_time() + ConfigFileEntry.nick_delay;
738
739 /* this list is ordered */
740 rb_dlinkAddTail(nd, &nd->lnode, &nd_list);
741
742 irc_dictionary_add(nd_dict, nd->name, nd);
743 }
744
745 void
746 free_nd_entry(struct nd_entry *nd)
747 {
748 irc_dictionary_delete(nd_dict, nd->name);
749
750 rb_dlinkDelete(&nd->lnode, &nd_list);
751 rb_bh_free(nd_heap, nd);
752 }
753
754 void
755 expire_nd_entries(void *unused)
756 {
757 struct nd_entry *nd;
758 rb_dlink_node *ptr;
759 rb_dlink_node *next_ptr;
760
761 RB_DLINK_FOREACH_SAFE(ptr, next_ptr, nd_list.head)
762 {
763 nd = ptr->data;
764
765 /* this list is ordered - we can stop when we hit the first
766 * entry that doesnt expire..
767 */
768 if(nd->expire > rb_current_time())
769 return;
770
771 free_nd_entry(nd);
772 }
773 }
774
775 void
776 add_tgchange(const char *host)
777 {
778 tgchange *target;
779 rb_patricia_node_t *pnode;
780
781 if(find_tgchange(host))
782 return;
783
784 target = rb_malloc(sizeof(tgchange));
785 pnode = make_and_lookup(tgchange_tree, host);
786
787 pnode->data = target;
788 target->pnode = pnode;
789
790 target->ip = rb_strdup(host);
791 target->expiry = rb_current_time() + (60*60*12);
792
793 rb_dlinkAdd(target, &target->node, &tgchange_list);
794 }
795
796 tgchange *
797 find_tgchange(const char *host)
798 {
799 rb_patricia_node_t *pnode;
800
801 if((pnode = rb_match_exact_string(tgchange_tree, host)))
802 return pnode->data;
803
804 return NULL;
805 }
806