Initial import
[ratbox-ambernet.git] / src / s_newconf.c
blob719fee6acf095cb9dec383fe265132277b1d8f0f
1 /*
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
10 * met:
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 23457 2007-01-14 00:23:40Z androsyn $
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 "tools.h"
41 #include "client.h"
42 #include "memory.h"
43 #include "s_serv.h"
44 #include "send.h"
45 #include "hostmask.h"
46 #include "newconf.h"
47 #include "hash.h"
48 #include "balloc.h"
49 #include "event.h"
50 #include "sprintf_irc.h"
52 dlink_list shared_conf_list;
53 dlink_list cluster_conf_list;
54 dlink_list oper_conf_list;
55 dlink_list hubleaf_conf_list;
56 dlink_list server_conf_list;
57 dlink_list xline_conf_list;
58 dlink_list resv_conf_list; /* nicks only! */
59 static dlink_list nd_list; /* nick delay */
60 dlink_list tgchange_list;
62 patricia_tree_t *tgchange_tree;
64 static BlockHeap *nd_heap = NULL;
66 static void expire_temp_rxlines(void *unused);
67 static void expire_nd_entries(void *unused);
69 void
70 init_s_newconf(void)
72 tgchange_tree = New_Patricia(PATRICIA_BITS);
73 nd_heap = BlockHeapCreate(sizeof(struct nd_entry), ND_HEAP_SIZE);
74 eventAddIsh("expire_nd_entries", expire_nd_entries, NULL, 30);
75 eventAddIsh("expire_temp_rxlines", expire_temp_rxlines, NULL, 60);
78 void
79 clear_s_newconf(void)
81 struct server_conf *server_p;
82 dlink_node *ptr;
83 dlink_node *next_ptr;
85 DLINK_FOREACH_SAFE(ptr, next_ptr, shared_conf_list.head)
87 /* ptr here is ptr->data->node */
88 dlinkDelete(ptr, &shared_conf_list);
89 free_remote_conf(ptr->data);
92 DLINK_FOREACH_SAFE(ptr, next_ptr, cluster_conf_list.head)
94 dlinkDelete(ptr, &cluster_conf_list);
95 free_remote_conf(ptr->data);
98 DLINK_FOREACH_SAFE(ptr, next_ptr, hubleaf_conf_list.head)
100 dlinkDelete(ptr, &hubleaf_conf_list);
101 free_remote_conf(ptr->data);
104 DLINK_FOREACH_SAFE(ptr, next_ptr, oper_conf_list.head)
106 free_oper_conf(ptr->data);
107 dlinkDestroy(ptr, &oper_conf_list);
110 DLINK_FOREACH_SAFE(ptr, next_ptr, server_conf_list.head)
112 server_p = ptr->data;
114 if(!server_p->servers)
116 dlinkDelete(ptr, &server_conf_list);
117 free_server_conf(ptr->data);
119 else
120 server_p->flags |= SERVER_ILLEGAL;
124 void
125 clear_s_newconf_bans(void)
127 struct ConfItem *aconf;
128 dlink_node *ptr, *next_ptr;
130 DLINK_FOREACH_SAFE(ptr, next_ptr, xline_conf_list.head)
132 aconf = ptr->data;
134 if(aconf->hold)
135 continue;
137 free_conf(aconf);
138 dlinkDestroy(ptr, &xline_conf_list);
141 DLINK_FOREACH_SAFE(ptr, next_ptr, resv_conf_list.head)
143 aconf = ptr->data;
145 /* temporary resv */
146 if(aconf->hold)
147 continue;
149 free_conf(aconf);
150 dlinkDestroy(ptr, &resv_conf_list);
153 clear_resv_hash();
156 struct remote_conf *
157 make_remote_conf(void)
159 struct remote_conf *remote_p = MyMalloc(sizeof(struct remote_conf));
160 return remote_p;
163 void
164 free_remote_conf(struct remote_conf *remote_p)
166 s_assert(remote_p != NULL);
167 if(remote_p == NULL)
168 return;
170 MyFree(remote_p->username);
171 MyFree(remote_p->host);
172 MyFree(remote_p->server);
173 MyFree(remote_p);
177 find_shared_conf(const char *username, const char *host,
178 const char *server, int flags)
180 struct remote_conf *shared_p;
181 dlink_node *ptr;
183 DLINK_FOREACH(ptr, shared_conf_list.head)
185 shared_p = ptr->data;
187 if(match(shared_p->username, username) &&
188 match(shared_p->host, host) &&
189 match(shared_p->server, server))
191 if(shared_p->flags & flags)
192 return YES;
193 else
194 return NO;
198 return NO;
201 void
202 propagate_generic(struct Client *source_p, const char *command,
203 const char *target, int cap, const char *format, ...)
205 char buffer[BUFSIZE];
206 va_list args;
208 va_start(args, format);
209 ircvsnprintf(buffer, sizeof(buffer), format, args);
210 va_end(args);
212 sendto_match_servs(source_p, target, cap, NOCAPS,
213 "%s %s %s",
214 command, target, buffer);
215 sendto_match_servs(source_p, target, CAP_ENCAP, cap,
216 "ENCAP %s %s %s",
217 target, command, buffer);
220 void
221 cluster_generic(struct Client *source_p, const char *command,
222 int cltype, int cap, const char *format, ...)
224 char buffer[BUFSIZE];
225 struct remote_conf *shared_p;
226 va_list args;
227 dlink_node *ptr;
229 va_start(args, format);
230 ircvsnprintf(buffer, sizeof(buffer), format, args);
231 va_end(args);
233 DLINK_FOREACH(ptr, cluster_conf_list.head)
235 shared_p = ptr->data;
237 if(!(shared_p->flags & cltype))
238 continue;
240 sendto_match_servs(source_p, shared_p->server, cap, NOCAPS,
241 "%s %s %s",
242 command, shared_p->server, buffer);
243 sendto_match_servs(source_p, shared_p->server, CAP_ENCAP, cap,
244 "ENCAP %s %s %s",
245 shared_p->server, command, buffer);
249 struct oper_conf *
250 make_oper_conf(void)
252 struct oper_conf *oper_p = MyMalloc(sizeof(struct oper_conf));
253 return oper_p;
256 void
257 free_oper_conf(struct oper_conf *oper_p)
259 s_assert(oper_p != NULL);
260 if(oper_p == NULL)
261 return;
263 MyFree(oper_p->username);
264 MyFree(oper_p->host);
265 MyFree(oper_p->name);
267 if(oper_p->passwd)
269 memset(oper_p->passwd, 0, strlen(oper_p->passwd));
270 MyFree(oper_p->passwd);
273 #ifdef HAVE_LIBCRYPTO
274 MyFree(oper_p->rsa_pubkey_file);
276 if(oper_p->rsa_pubkey)
277 RSA_free(oper_p->rsa_pubkey);
278 #endif
280 MyFree(oper_p);
283 struct oper_conf *
284 find_oper_conf(const char *username, const char *host, const char *locip, const char *name)
286 struct oper_conf *oper_p;
287 struct irc_sockaddr_storage ip, cip;
288 char addr[HOSTLEN+1];
289 int bits, cbits;
290 dlink_node *ptr;
292 parse_netmask(locip, (struct sockaddr *)&cip, &cbits);
294 DLINK_FOREACH(ptr, oper_conf_list.head)
296 oper_p = ptr->data;
298 /* name/username doesnt match.. */
299 if(irccmp(oper_p->name, name) || !match(oper_p->username, username))
300 continue;
302 strlcpy(addr, oper_p->host, sizeof(addr));
304 if(parse_netmask(addr, (struct sockaddr *)&ip, &bits) != HM_HOST)
306 if(ip.ss_family == cip.ss_family &&
307 comp_with_mask_sock((struct sockaddr *)&ip, (struct sockaddr *)&cip, bits))
308 return oper_p;
311 /* we have to compare against the host as well, because its
312 * valid to set a spoof to an IP, which if we only compare
313 * in ip form to sockhost will not necessarily match --anfl
315 if(match(oper_p->host, host))
316 return oper_p;
319 return NULL;
322 struct oper_flags
324 int flag;
325 char has;
326 char hasnt;
328 static struct oper_flags oper_flagtable[] =
330 { OPER_GLINE, 'G', 'g' },
331 { OPER_KLINE, 'K', 'k' },
332 { OPER_XLINE, 'X', 'x' },
333 { OPER_GLOBKILL, 'O', 'o' },
334 { OPER_LOCKILL, 'C', 'c' },
335 { OPER_REMOTE, 'R', 'r' },
336 { OPER_UNKLINE, 'U', 'u' },
337 { OPER_REHASH, 'H', 'h' },
338 { OPER_DIE, 'D', 'd' },
339 { OPER_ADMIN, 'A', 'a' },
340 { OPER_NICKS, 'N', 'n' },
341 { OPER_OPERWALL, 'L', 'l' },
342 { OPER_SPY, 'S', 's' },
343 { OPER_INVIS, 'P', 'p' },
344 { OPER_REMOTEBAN, 'B', 'b' },
345 { 0, '\0', '\0' }
348 const char *
349 get_oper_privs(int flags)
351 static char buf[20];
352 char *p;
353 int i;
355 p = buf;
357 for(i = 0; oper_flagtable[i].flag; i++)
359 if(flags & oper_flagtable[i].flag)
360 *p++ = oper_flagtable[i].has;
361 else
362 *p++ = oper_flagtable[i].hasnt;
365 *p = '\0';
367 return buf;
370 struct server_conf *
371 make_server_conf(void)
373 struct server_conf *server_p = MyMalloc(sizeof(struct server_conf));
374 server_p->ipnum.ss_family = AF_INET;
375 return server_p;
378 void
379 free_server_conf(struct server_conf *server_p)
381 s_assert(server_p != NULL);
382 if(server_p == NULL)
383 return;
385 if(!EmptyString(server_p->passwd))
387 memset(server_p->passwd, 0, strlen(server_p->passwd));
388 MyFree(server_p->passwd);
391 if(!EmptyString(server_p->spasswd))
393 memset(server_p->spasswd, 0, strlen(server_p->spasswd));
394 MyFree(server_p->spasswd);
397 delete_adns_queries(server_p->dns_query);
399 MyFree(server_p->name);
400 MyFree(server_p->host);
401 MyFree(server_p->class_name);
402 MyFree(server_p);
406 * conf_dns_callback
407 * inputs - pointer to struct ConfItem
408 * - pointer to adns reply
409 * output - none
410 * side effects - called when resolver query finishes
411 * if the query resulted in a successful search, hp will contain
412 * a non-null pointer, otherwise hp will be null.
413 * if successful save hp in the conf item it was called with
415 static void
416 conf_dns_callback(void *vptr, adns_answer * reply)
418 struct server_conf *server_p = (struct server_conf *) vptr;
420 if(reply && reply->status == adns_s_ok)
422 #ifdef IPV6
423 if(reply->type == adns_r_addr6)
425 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&server_p->ipnum;
426 SET_SS_LEN(server_p->ipnum, sizeof(struct sockaddr_in6));
427 in6->sin6_family = AF_INET6;
428 in6->sin6_port = 0;
429 memcpy(&in6->sin6_addr, &reply->rrs.addr->addr.inet6.sin6_addr, sizeof(struct in6_addr));
431 else
432 #endif
434 struct sockaddr_in *in = (struct sockaddr_in *)&server_p->ipnum;
435 SET_SS_LEN(server_p->ipnum, sizeof(struct sockaddr_in));
436 in->sin_family = AF_INET;
437 in->sin_port = 0;
438 in->sin_addr.s_addr = reply->rrs.addr->addr.inet.sin_addr.s_addr;
440 MyFree(reply);
443 MyFree(server_p->dns_query);
444 server_p->dns_query = NULL;
448 void
449 add_server_conf(struct server_conf *server_p)
451 if(EmptyString(server_p->class_name))
452 server_p->class = default_class;
453 else
454 server_p->class = find_class(server_p->class_name);
456 if(server_p->class == default_class)
458 conf_report_error("Warning connect::class invalid for %s",
459 server_p->name);
461 MyFree(server_p->class_name);
462 DupString(server_p->class_name, "default");
465 if(strchr(server_p->host, '*') || strchr(server_p->host, '?'))
466 return;
468 if(inetpton_sock(server_p->host, (struct sockaddr *)&server_p->ipnum) > 0)
469 return;
471 server_p->dns_query = MyMalloc(sizeof(struct DNSQuery));
472 server_p->dns_query->ptr = server_p;
473 server_p->dns_query->callback = conf_dns_callback;
474 adns_gethost(server_p->host, server_p->ipnum.ss_family, server_p->dns_query);
477 struct server_conf *
478 find_server_conf(const char *name)
480 struct server_conf *server_p;
481 dlink_node *ptr;
483 DLINK_FOREACH(ptr, server_conf_list.head)
485 server_p = ptr->data;
487 if(ServerConfIllegal(server_p))
488 continue;
490 if(match(name, server_p->name))
491 return server_p;
494 return NULL;
497 void
498 attach_server_conf(struct Client *client_p, struct server_conf *server_p)
500 /* already have an attached conf */
501 if(client_p->localClient->att_sconf)
503 /* short circuit this special case :) */
504 if(client_p->localClient->att_sconf == server_p)
505 return;
507 detach_server_conf(client_p);
510 CurrUsers(server_p->class)++;
512 client_p->localClient->att_sconf = server_p;
513 server_p->servers++;
516 void
517 detach_server_conf(struct Client *client_p)
519 struct server_conf *server_p = client_p->localClient->att_sconf;
521 if(server_p == NULL)
522 return;
524 client_p->localClient->att_sconf = NULL;
525 server_p->servers--;
526 CurrUsers(server_p->class)--;
528 if(ServerConfIllegal(server_p) && !server_p->servers)
530 /* the class this one is using may need destroying too */
531 if(MaxUsers(server_p->class) < 0 && CurrUsers(server_p->class) <= 0)
532 free_class(server_p->class);
534 dlinkDelete(&server_p->node, &server_conf_list);
535 free_server_conf(server_p);
539 void
540 set_server_conf_autoconn(struct Client *source_p, char *name, int newval)
542 struct server_conf *server_p;
544 if((server_p = find_server_conf(name)) != NULL)
546 if(newval)
547 server_p->flags |= SERVER_AUTOCONN;
548 else
549 server_p->flags &= ~SERVER_AUTOCONN;
551 sendto_realops_flags(UMODE_ALL, L_ALL,
552 "%s has changed AUTOCONN for %s to %i",
553 get_oper_name(source_p), name, newval);
555 else
556 sendto_one(source_p, ":%s NOTICE %s :Can't find %s",
557 me.name, source_p->name, name);
560 struct ConfItem *
561 find_xline(const char *gecos, int counter)
563 struct ConfItem *aconf;
564 dlink_node *ptr;
566 DLINK_FOREACH(ptr, xline_conf_list.head)
568 aconf = ptr->data;
570 if(match_esc(aconf->name, gecos))
572 if(counter)
573 aconf->port++;
574 return aconf;
578 return NULL;
581 struct ConfItem *
582 find_nick_resv(const char *name)
584 struct ConfItem *aconf;
585 dlink_node *ptr;
587 DLINK_FOREACH(ptr, resv_conf_list.head)
589 aconf = ptr->data;
591 if(match_esc(aconf->name, name))
593 aconf->port++;
594 return aconf;
598 return NULL;
601 /* clean_resv_nick()
603 * inputs - nick
604 * outputs - 1 if nick is vaild resv, 0 otherwise
605 * side effects -
608 clean_resv_nick(const char *nick)
610 char tmpch;
611 int as = 0;
612 int q = 0;
613 int ch = 0;
615 if(*nick == '-' || IsDigit(*nick))
616 return 0;
618 while ((tmpch = *nick++))
620 if(tmpch == '?' || tmpch == '@' || tmpch == '#')
621 q++;
622 else if(tmpch == '*')
623 as++;
624 else if(IsNickChar(tmpch))
625 ch++;
626 else
627 return 0;
630 if(!ch && as)
631 return 0;
633 return 1;
636 /* valid_wild_card_simple()
638 * inputs - "thing" to test
639 * outputs - 1 if enough wildcards, else 0
640 * side effects -
643 valid_wild_card_simple(const char *data)
645 const char *p;
646 char tmpch;
647 int nonwild = 0;
649 /* check the string for minimum number of nonwildcard chars */
650 p = data;
652 while((tmpch = *p++))
654 /* found an escape, p points to the char after it, so skip
655 * that and move on.
657 if(tmpch == '\\')
659 p++;
660 if(++nonwild >= ConfigFileEntry.min_nonwildcard_simple)
661 return 1;
663 else if(!IsMWildChar(tmpch))
665 /* if we have enough nonwildchars, return */
666 if(++nonwild >= ConfigFileEntry.min_nonwildcard_simple)
667 return 1;
671 return 0;
674 time_t
675 valid_temp_time(const char *p)
677 time_t result = 0;
679 while(*p)
681 if(IsDigit(*p))
683 result *= 10;
684 result += ((*p) & 0xF);
685 p++;
687 else
688 return -1;
691 if(result > (60 * 24 * 7 * 52))
692 result = (60 * 24 * 7 * 52);
694 return(result * 60);
697 static void
698 expire_temp_rxlines(void *unused)
700 struct ConfItem *aconf;
701 dlink_node *ptr;
702 dlink_node *next_ptr;
703 int i;
705 HASH_WALK_SAFE(i, R_MAX, ptr, next_ptr, resvTable)
707 aconf = ptr->data;
709 if(aconf->hold && aconf->hold <= CurrentTime)
711 if(ConfigFileEntry.tkline_expire_notices)
712 sendto_realops_flags(UMODE_ALL, L_ALL,
713 "Temporary RESV for [%s] expired",
714 aconf->name);
716 free_conf(aconf);
717 dlinkDestroy(ptr, &resvTable[i]);
720 HASH_WALK_END
722 DLINK_FOREACH_SAFE(ptr, next_ptr, resv_conf_list.head)
724 aconf = ptr->data;
726 if(aconf->hold && aconf->hold <= CurrentTime)
728 if(ConfigFileEntry.tkline_expire_notices)
729 sendto_realops_flags(UMODE_ALL, L_ALL,
730 "Temporary RESV for [%s] expired",
731 aconf->name);
732 free_conf(aconf);
733 dlinkDestroy(ptr, &resv_conf_list);
737 DLINK_FOREACH_SAFE(ptr, next_ptr, xline_conf_list.head)
739 aconf = ptr->data;
741 if(aconf->hold && aconf->hold <= CurrentTime)
743 if(ConfigFileEntry.tkline_expire_notices)
744 sendto_realops_flags(UMODE_ALL, L_ALL,
745 "Temporary X-line for [%s] expired",
746 aconf->name);
747 free_conf(aconf);
748 dlinkDestroy(ptr, &xline_conf_list);
753 unsigned long
754 get_nd_count(void)
756 return(dlink_list_length(&nd_list));
760 void
761 add_nd_entry(const char *name)
763 struct nd_entry *nd;
765 if(hash_find_nd(name) != NULL)
766 return;
768 nd = BlockHeapAlloc(nd_heap);
770 strlcpy(nd->name, name, sizeof(nd->name));
771 nd->expire = CurrentTime + ConfigFileEntry.nick_delay;
773 /* this list is ordered */
774 dlinkAddTail(nd, &nd->lnode, &nd_list);
775 add_to_nd_hash(name, nd);
778 void
779 free_nd_entry(struct nd_entry *nd)
781 dlinkDelete(&nd->lnode, &nd_list);
782 dlinkDelete(&nd->hnode, &ndTable[nd->hashv]);
783 BlockHeapFree(nd_heap, nd);
786 void
787 expire_nd_entries(void *unused)
789 struct nd_entry *nd;
790 dlink_node *ptr;
791 dlink_node *next_ptr;
793 DLINK_FOREACH_SAFE(ptr, next_ptr, nd_list.head)
795 nd = ptr->data;
797 /* this list is ordered - we can stop when we hit the first
798 * entry that doesnt expire..
800 if(nd->expire > CurrentTime)
801 return;
803 free_nd_entry(nd);
807 void
808 add_tgchange(const char *host)
810 tgchange *target;
811 patricia_node_t *pnode;
813 if(find_tgchange(host))
814 return;
816 target = MyMalloc(sizeof(tgchange));
817 pnode = make_and_lookup(tgchange_tree, host);
819 pnode->data = target;
820 target->pnode = pnode;
822 DupString(target->ip, host);
823 target->expiry = CurrentTime + (60*60*12);
825 dlinkAdd(target, &target->node, &tgchange_list);
828 tgchange *
829 find_tgchange(const char *host)
831 patricia_node_t *pnode;
833 if((pnode = match_exact_string(tgchange_tree, host)))
834 return pnode->data;
836 return NULL;