Initial import
[ratbox-ambernet.git] / src / s_conf.c
blob5af9f89f9b7cb8ca6bba53d8986c24313789134f
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * s_conf.c: Configuration file functions.
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
24 * $Id: s_conf.c 23955 2007-05-14 17:22:36Z leeh $
27 #include "stdinc.h"
28 #include "ircd_defs.h"
29 #include "tools.h"
30 #include "s_conf.h"
31 #include "s_newconf.h"
32 #include "s_serv.h"
33 #include "s_stats.h"
34 #include "channel.h"
35 #include "class.h"
36 #include "client.h"
37 #include "common.h"
38 #include "event.h"
39 #include "hash.h"
40 #include "irc_string.h"
41 #include "sprintf_irc.h"
42 #include "ircd.h"
43 #include "listener.h"
44 #include "hostmask.h"
45 #include "modules.h"
46 #include "numeric.h"
47 #include "commio.h"
48 #include "s_log.h"
49 #include "send.h"
50 #include "s_gline.h"
51 #include "memory.h"
52 #include "balloc.h"
53 #include "patricia.h"
54 #include "reject.h"
55 #include "cache.h"
57 struct config_server_hide ConfigServerHide;
59 extern int yyparse(); /* defined in y.tab.c */
60 extern char linebuf[];
62 #ifndef INADDR_NONE
63 #define INADDR_NONE ((unsigned int) 0xffffffff)
64 #endif
66 static BlockHeap *confitem_heap = NULL;
68 dlink_list temp_klines[LAST_TEMP_TYPE];
69 dlink_list temp_dlines[LAST_TEMP_TYPE];
71 #ifdef ENABLE_SERVICES
72 dlink_list service_list;
73 #endif
75 /* internally defined functions */
76 static void set_default_conf(void);
77 static void validate_conf(void);
78 static void read_conf(FILE *);
79 static void clear_out_old_conf(void);
81 static void expire_temp_kd(void *list);
82 static void reorganise_temp_kd(void *list);
84 FILE *conf_fbfile_in;
85 extern char yytext[];
87 static int verify_access(struct Client *client_p, const char *username);
88 static int attach_iline(struct Client *, struct ConfItem *);
90 void
91 init_s_conf(void)
93 confitem_heap = BlockHeapCreate(sizeof(struct ConfItem), CONFITEM_HEAP_SIZE);
95 eventAddIsh("expire_temp_klines", expire_temp_kd, &temp_klines[TEMP_MIN], 60);
96 eventAddIsh("expire_temp_dlines", expire_temp_kd, &temp_dlines[TEMP_MIN], 60);
98 eventAddIsh("expire_temp_klines_hour", reorganise_temp_kd,
99 &temp_klines[TEMP_HOUR], 3600);
100 eventAddIsh("expire_temp_dlines_hour", reorganise_temp_kd,
101 &temp_dlines[TEMP_HOUR], 3600);
102 eventAddIsh("expire_temp_klines_day", reorganise_temp_kd,
103 &temp_klines[TEMP_DAY], 86400);
104 eventAddIsh("expire_temp_dlines_day", reorganise_temp_kd,
105 &temp_dlines[TEMP_DAY], 86400);
106 eventAddIsh("expire_temp_klines_week", reorganise_temp_kd,
107 &temp_klines[TEMP_WEEK], 604800);
108 eventAddIsh("expire_temp_dlines_week", reorganise_temp_kd,
109 &temp_dlines[TEMP_WEEK], 604800);
113 * make_conf
115 * inputs - none
116 * output - pointer to new conf entry
117 * side effects - none
119 struct ConfItem *
120 make_conf()
122 struct ConfItem *aconf;
124 aconf = BlockHeapAlloc(confitem_heap);
125 aconf->status = CONF_ILLEGAL;
126 return (aconf);
130 * free_conf
132 * inputs - pointer to conf to free
133 * output - none
134 * side effects - crucial password fields are zeroed, conf is freed
136 void
137 free_conf(struct ConfItem *aconf)
139 s_assert(aconf != NULL);
140 if(aconf == NULL)
141 return;
143 /* security.. */
144 if(aconf->passwd)
145 memset(aconf->passwd, 0, strlen(aconf->passwd));
146 if(aconf->spasswd)
147 memset(aconf->spasswd, 0, strlen(aconf->spasswd));
149 MyFree(aconf->passwd);
150 MyFree(aconf->spasswd);
151 MyFree(aconf->name);
152 MyFree(aconf->user);
153 MyFree(aconf->host);
155 BlockHeapFree(confitem_heap, aconf);
159 * check_client
161 * inputs - pointer to client
162 * output - 0 = Success
163 * NOT_AUTHORISED (-1) = Access denied (no I line match)
164 * SOCKET_ERROR (-2) = Bad socket.
165 * I_LINE_FULL (-3) = I-line is full
166 * TOO_MANY (-4) = Too many connections from hostname
167 * BANNED_CLIENT (-5) = K-lined
168 * side effects - Ordinary client access check.
169 * Look for conf lines which have the same
170 * status as the flags passed.
173 check_client(struct Client *client_p, struct Client *source_p, const char *username)
175 int i;
177 if((i = verify_access(source_p, username)))
179 ilog(L_FUSER, "Access denied: %s[%s]",
180 source_p->name, source_p->sockhost);
183 switch (i)
185 case SOCKET_ERROR:
186 exit_client(client_p, source_p, &me, "Socket Error");
187 break;
189 case TOO_MANY_LOCAL:
190 sendto_realops_flags(UMODE_FULL, L_ALL,
191 "Too many local connections for %s!%s%s@%s",
192 source_p->name, IsGotId(source_p) ? "" : "~",
193 source_p->username, show_ip(NULL, source_p) ? source_p->sockhost : source_p->host);
195 ilog(L_FUSER, "Too many local connections from %s!%s%s@%s",
196 source_p->name, IsGotId(source_p) ? "" : "~",
197 source_p->username, source_p->sockhost);
199 ServerStats->is_ref++;
200 exit_client(client_p, source_p, &me, "Too many host connections (local)");
201 break;
203 case TOO_MANY_GLOBAL:
204 sendto_realops_flags(UMODE_FULL, L_ALL,
205 "Too many global connections for %s!%s%s@%s",
206 source_p->name, IsGotId(source_p) ? "" : "~",
207 source_p->username, show_ip(NULL, source_p) ? source_p->sockhost : source_p->host);
208 ilog(L_FUSER, "Too many global connections from %s!%s%s@%s",
209 source_p->name, IsGotId(source_p) ? "" : "~",
210 source_p->username, source_p->sockhost);
212 ServerStats->is_ref++;
213 exit_client(client_p, source_p, &me, "Too many host connections (global)");
214 break;
216 case TOO_MANY_IDENT:
217 sendto_realops_flags(UMODE_FULL, L_ALL,
218 "Too many user connections for %s!%s%s@%s",
219 source_p->name, IsGotId(source_p) ? "" : "~",
220 source_p->username, show_ip(NULL, source_p) ? source_p->sockhost : source_p->host);
221 ilog(L_FUSER, "Too many user connections from %s!%s%s@%s",
222 source_p->name, IsGotId(source_p) ? "" : "~",
223 source_p->username, source_p->sockhost);
225 ServerStats->is_ref++;
226 exit_client(client_p, source_p, &me, "Too many user connections (global)");
227 break;
229 case I_LINE_FULL:
230 sendto_realops_flags(UMODE_FULL, L_ALL,
231 "I-line is full for %s!%s%s@%s (%s).",
232 source_p->name, IsGotId(source_p) ? "" : "~",
233 source_p->username, source_p->host,
234 show_ip(NULL, source_p) ? source_p->sockhost : source_p->host);
236 ilog(L_FUSER, "Too many connections from %s!%s%s@%s.",
237 source_p->name, IsGotId(source_p) ? "" : "~",
238 source_p->username, source_p->sockhost);
240 ServerStats->is_ref++;
241 exit_client(client_p, source_p, &me,
242 "No more connections allowed in your connection class");
243 break;
245 case NOT_AUTHORISED:
247 int port = -1;
248 #ifdef IPV6
249 if(source_p->localClient->ip.ss_family == AF_INET6)
250 port = ntohs(((struct sockaddr_in6 *)&source_p->localClient->listener->addr)->sin6_port);
251 else
252 #endif
253 port = ntohs(((struct sockaddr_in *)&source_p->localClient->listener->addr)->sin_port);
255 ServerStats->is_ref++;
256 /* jdc - lists server name & port connections are on */
257 /* a purely cosmetical change */
258 /* why ipaddr, and not just source_p->sockhost? --fl */
259 #if 0
260 static char ipaddr[HOSTIPLEN];
261 inetntop_sock(&source_p->localClient->ip, ipaddr, sizeof(ipaddr));
262 #endif
263 sendto_realops_flags(UMODE_UNAUTH, L_ALL,
264 "Unauthorised client connection from "
265 "%s!%s%s@%s [%s] on [%s/%u].",
266 source_p->name, IsGotId(source_p) ? "" : "~",
267 source_p->username, source_p->host,
268 source_p->sockhost,
269 source_p->localClient->listener->name, port);
271 ilog(L_FUSER,
272 "Unauthorised client connection from %s!%s%s@%s on [%s/%u].",
273 source_p->name, IsGotId(source_p) ? "" : "~",
274 source_p->username, source_p->sockhost,
275 source_p->localClient->listener->name, port);
276 add_reject(client_p);
277 exit_client(client_p, source_p, &me,
278 "You are not authorised to use this server");
279 break;
281 case BANNED_CLIENT:
282 add_reject(client_p);
283 exit_client(client_p, client_p, &me, "*** Banned ");
284 ServerStats->is_ref++;
285 break;
287 case 0:
288 default:
289 break;
291 return (i);
295 * verify_access
297 * inputs - pointer to client to verify
298 * - pointer to proposed username
299 * output - 0 if success -'ve if not
300 * side effect - find the first (best) I line to attach.
302 static int
303 verify_access(struct Client *client_p, const char *username)
305 struct ConfItem *aconf;
306 char non_ident[USERLEN + 1];
308 if(IsGotId(client_p))
310 aconf = find_address_conf(client_p->host, client_p->sockhost,
311 client_p->username,
312 (struct sockaddr *) &client_p->localClient->ip,
313 client_p->localClient->ip.ss_family);
315 else
317 strlcpy(non_ident, "~", sizeof(non_ident));
318 strlcat(non_ident, username, sizeof(non_ident));
319 aconf = find_address_conf(client_p->host, client_p->sockhost,
320 non_ident,
321 (struct sockaddr *) &client_p->localClient->ip,
322 client_p->localClient->ip.ss_family);
325 if(aconf == NULL)
326 return NOT_AUTHORISED;
328 if(aconf->status & CONF_CLIENT)
330 if(aconf->flags & CONF_FLAGS_REDIR)
332 sendto_one(client_p, form_str(RPL_REDIR),
333 me.name, client_p->name,
334 aconf->name ? aconf->name : "", aconf->port);
335 return (NOT_AUTHORISED);
339 /* Thanks for spoof idea amm */
340 if(IsConfDoSpoofIp(aconf))
342 char *p;
344 /* show_ip() depends on this --fl */
345 SetIPSpoof(client_p);
347 if(IsConfSpoofNotice(aconf))
349 sendto_realops_flags(UMODE_ALL, L_ALL,
350 "%s spoofing: %s as %s",
351 client_p->name,
352 show_ip(NULL, client_p) ? client_p->host : aconf->name,
353 aconf->name);
356 /* user@host spoof */
357 if((p = strchr(aconf->name, '@')) != NULL)
359 char *host = p+1;
360 *p = '\0';
362 strlcpy(client_p->username, aconf->name,
363 sizeof(client_p->username));
364 strlcpy(client_p->host, host,
365 sizeof(client_p->host));
366 *p = '@';
368 else
369 strlcpy(client_p->host, aconf->name, sizeof(client_p->host));
371 return (attach_iline(client_p, aconf));
373 else if(aconf->status & CONF_KILL)
375 if(ConfigFileEntry.kline_with_reason)
377 sendto_one(client_p,
378 ":%s NOTICE %s :*** Banned %s",
379 me.name, client_p->name, aconf->passwd);
381 return (BANNED_CLIENT);
383 else if(aconf->status & CONF_GLINE)
385 sendto_one(client_p, ":%s NOTICE %s :*** G-lined", me.name, client_p->name);
387 if(ConfigFileEntry.kline_with_reason)
388 sendto_one(client_p,
389 ":%s NOTICE %s :*** Banned %s",
390 me.name, client_p->name, aconf->passwd);
392 return (BANNED_CLIENT);
395 return NOT_AUTHORISED;
400 * add_ip_limit
402 * Returns 1 if successful 0 if not
404 * This checks if the user has exceed the limits for their class
405 * unless of course they are exempt..
408 static int
409 add_ip_limit(struct Client *client_p, struct ConfItem *aconf)
411 patricia_node_t *pnode;
413 /* If the limits are 0 don't do anything.. */
414 if(ConfCidrAmount(aconf) == 0 || ConfCidrBitlen(aconf) == 0)
415 return -1;
417 pnode = match_ip(ConfIpLimits(aconf), (struct sockaddr *)&client_p->localClient->ip);
419 if(pnode == NULL)
420 pnode = make_and_lookup_ip(ConfIpLimits(aconf), (struct sockaddr *)&client_p->localClient->ip, ConfCidrBitlen(aconf));
422 s_assert(pnode != NULL);
424 if(pnode != NULL)
426 if(((long) pnode->data) >= ConfCidrAmount(aconf)
427 && !IsConfExemptLimits(aconf))
429 /* This should only happen if the limits are set to 0 */
430 if((unsigned long) pnode->data == 0)
432 patricia_remove(ConfIpLimits(aconf), pnode);
434 return (0);
437 pnode->data++;
439 return 1;
442 static void
443 remove_ip_limit(struct Client *client_p, struct ConfItem *aconf)
445 patricia_node_t *pnode;
447 /* If the limits are 0 don't do anything.. */
448 if(ConfCidrAmount(aconf) == 0 || ConfCidrBitlen(aconf) == 0)
449 return;
451 pnode = match_ip(ConfIpLimits(aconf), (struct sockaddr *)&client_p->localClient->ip);
452 if(pnode == NULL)
453 return;
455 pnode->data--;
456 if(((unsigned long) pnode->data) == 0)
458 patricia_remove(ConfIpLimits(aconf), pnode);
464 * attach_iline
466 * inputs - client pointer
467 * - conf pointer
468 * output -
469 * side effects - do actual attach
471 static int
472 attach_iline(struct Client *client_p, struct ConfItem *aconf)
474 struct Client *target_p;
475 dlink_node *ptr;
476 int local_count = 0;
477 int global_count = 0;
478 int ident_count = 0;
479 int unidented = 0;
481 if(IsConfExemptLimits(aconf))
482 return (attach_conf(client_p, aconf));
484 if(*client_p->username == '~')
485 unidented = 1;
488 /* find_hostname() returns the head of the list to search */
489 DLINK_FOREACH(ptr, find_hostname(client_p->host))
491 target_p = ptr->data;
493 if(irccmp(client_p->host, target_p->host) != 0)
494 continue;
496 if(MyConnect(target_p))
497 local_count++;
499 global_count++;
501 if(unidented)
503 if(*target_p->username == '~')
504 ident_count++;
506 else if(irccmp(target_p->username, client_p->username) == 0)
507 ident_count++;
509 if(ConfMaxLocal(aconf) && local_count >= ConfMaxLocal(aconf))
510 return (TOO_MANY_LOCAL);
511 else if(ConfMaxGlobal(aconf) && global_count >= ConfMaxGlobal(aconf))
512 return (TOO_MANY_GLOBAL);
513 else if(ConfMaxIdent(aconf) && ident_count >= ConfMaxIdent(aconf))
514 return (TOO_MANY_IDENT);
518 return (attach_conf(client_p, aconf));
522 * detach_conf
524 * inputs - pointer to client to detach
525 * output - 0 for success, -1 for failure
526 * side effects - Disassociate configuration from the client.
527 * Also removes a class from the list if marked for deleting.
530 detach_conf(struct Client *client_p)
532 struct ConfItem *aconf;
534 aconf = client_p->localClient->att_conf;
536 if(aconf != NULL)
538 if(ClassPtr(aconf))
540 remove_ip_limit(client_p, aconf);
542 if(ConfCurrUsers(aconf) > 0)
543 --ConfCurrUsers(aconf);
545 if(ConfMaxUsers(aconf) == -1 && ConfCurrUsers(aconf) == 0)
547 free_class(ClassPtr(aconf));
548 ClassPtr(aconf) = NULL;
553 aconf->clients--;
554 if(!aconf->clients && IsIllegal(aconf))
555 free_conf(aconf);
557 client_p->localClient->att_conf = NULL;
558 return 0;
561 return -1;
565 * attach_conf
567 * inputs - client pointer
568 * - conf pointer
569 * output -
570 * side effects - Associate a specific configuration entry to a *local*
571 * client (this is the one which used in accepting the
572 * connection). Note, that this automatically changes the
573 * attachment if there was an old one...
576 attach_conf(struct Client *client_p, struct ConfItem *aconf)
578 if(IsIllegal(aconf))
579 return (NOT_AUTHORISED);
581 if(ClassPtr(aconf))
583 if(!add_ip_limit(client_p, aconf))
584 return (TOO_MANY_LOCAL);
587 if((aconf->status & CONF_CLIENT) &&
588 ConfCurrUsers(aconf) >= ConfMaxUsers(aconf) && ConfMaxUsers(aconf) > 0)
590 if(!IsConfExemptLimits(aconf))
592 return (I_LINE_FULL);
594 else
596 sendto_one(client_p, ":%s NOTICE %s :*** I: line is full, but you have an >I: line!",
597 me.name, client_p->name);
598 SetExemptLimits(client_p);
603 if(client_p->localClient->att_conf != NULL)
604 detach_conf(client_p);
606 client_p->localClient->att_conf = aconf;
608 aconf->clients++;
609 ConfCurrUsers(aconf)++;
610 return (0);
614 * rehash
616 * Actual REHASH service routine. Called with sig == 0 if it has been called
617 * as a result of an operator issuing this command, else assume it has been
618 * called as a result of the server receiving a HUP signal.
621 rehash(int sig)
623 if(sig != 0)
625 sendto_realops_flags(UMODE_ALL, L_ALL,
626 "Got signal SIGHUP, reloading ircd conf. file");
629 restart_resolver();
630 /* don't close listeners until we know we can go ahead with the rehash */
631 read_conf_files(NO);
633 if(ServerInfo.description != NULL)
634 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
635 else
636 strlcpy(me.info, "unknown", sizeof(me.info));
638 open_logfiles();
639 return (0);
642 static struct banconf_entry
644 const char **filename;
645 void (*func) (FILE *);
646 int perm;
647 } banconfs[] = {
648 { &ConfigFileEntry.klinefile, parse_k_file, 0 },
649 { &ConfigFileEntry.klinefile, parse_k_file, 1 },
650 { &ConfigFileEntry.dlinefile, parse_d_file, 0 },
651 { &ConfigFileEntry.dlinefile, parse_d_file, 1 },
652 { &ConfigFileEntry.xlinefile, parse_x_file, 0 },
653 { &ConfigFileEntry.xlinefile, parse_x_file, 1 },
654 { &ConfigFileEntry.resvfile, parse_resv_file,0 },
655 { &ConfigFileEntry.resvfile, parse_resv_file,1 },
656 { NULL, NULL, 0 }
659 void
660 rehash_bans(int sig)
662 FILE *file;
663 char buf[MAXPATHLEN];
664 int i;
666 if(sig != 0)
667 sendto_realops_flags(UMODE_ALL, L_ALL,
668 "Got signal SIGUSR2, reloading ban confs");
670 clear_out_address_conf_bans();
671 clear_s_newconf_bans();
673 for(i = 0; banconfs[i].filename; i++)
675 if(banconfs[i].perm)
676 snprintf(buf, sizeof(buf), "%s.perm", *banconfs[i].filename);
677 else
678 snprintf(buf, sizeof(buf), "%s", *banconfs[i].filename);
680 if((file = fopen(buf, "r")) == NULL)
682 if(banconfs[i].perm)
683 continue;
685 ilog(L_MAIN, "Failed reading ban file %s",
686 *banconfs[i].filename);
687 sendto_realops_flags(UMODE_ALL, L_ALL,
688 "Can't open %s file bans could be missing!",
689 *banconfs[i].filename);
691 else
693 (banconfs[i].func)(file);
694 fclose(file);
698 check_banned_lines();
702 * set_default_conf()
704 * inputs - NONE
705 * output - NONE
706 * side effects - Set default values here.
707 * This is called **PRIOR** to parsing the
708 * configuration file. If you want to do some validation
709 * of values later, put them in validate_conf().
712 #define YES 1
713 #define NO 0
715 static void
716 set_default_conf(void)
718 /* ServerInfo.name is not rehashable */
719 /* ServerInfo.name = ServerInfo.name; */
720 ServerInfo.description = NULL;
721 DupString(ServerInfo.network_name, NETWORK_NAME_DEFAULT);
722 DupString(ServerInfo.network_desc, NETWORK_DESC_DEFAULT);
724 memset(&ServerInfo.ip, 0, sizeof(ServerInfo.ip));
725 ServerInfo.specific_ipv4_vhost = 0;
726 #ifdef IPV6
727 memset(&ServerInfo.ip6, 0, sizeof(ServerInfo.ip6));
728 ServerInfo.specific_ipv6_vhost = 0;
729 #endif
730 ServerInfo.use_ts6 = YES;
731 ServerInfo.default_max_clients = MAXCONNECTIONS;
733 /* Don't reset hub, as that will break lazylinks */
734 /* ServerInfo.hub = NO; */
735 AdminInfo.name = NULL;
736 AdminInfo.email = NULL;
737 AdminInfo.description = NULL;
739 DupString(ConfigFileEntry.default_operstring, "is an IRC operator");
740 DupString(ConfigFileEntry.default_adminstring, "is a Server Administrator");
742 ConfigFileEntry.failed_oper_notice = YES;
743 ConfigFileEntry.anti_nick_flood = NO;
744 ConfigFileEntry.disable_fake_channels = NO;
745 ConfigFileEntry.max_nick_time = 20;
746 ConfigFileEntry.max_nick_changes = 5;
747 ConfigFileEntry.max_accept = 20;
748 ConfigFileEntry.max_monitor = 60;
749 ConfigFileEntry.nick_delay = 900; /* 15 minutes */
750 ConfigFileEntry.target_change = YES;
751 ConfigFileEntry.anti_spam_exit_message_time = 0;
752 ConfigFileEntry.ts_warn_delta = TS_WARN_DELTA_DEFAULT;
753 ConfigFileEntry.ts_max_delta = TS_MAX_DELTA_DEFAULT;
754 ConfigFileEntry.client_exit = YES;
755 ConfigFileEntry.dline_with_reason = YES;
756 ConfigFileEntry.kline_with_reason = YES;
757 ConfigFileEntry.kline_delay = 0;
758 ConfigFileEntry.warn_no_nline = YES;
759 ConfigFileEntry.non_redundant_klines = YES;
760 ConfigFileEntry.stats_e_disabled = NO;
761 ConfigFileEntry.stats_o_oper_only = NO;
762 ConfigFileEntry.stats_k_oper_only = 1; /* masked */
763 ConfigFileEntry.stats_i_oper_only = 1; /* masked */
764 ConfigFileEntry.stats_P_oper_only = NO;
765 ConfigFileEntry.stats_c_oper_only = NO;
766 ConfigFileEntry.stats_y_oper_only = NO;
767 ConfigFileEntry.stats_h_oper_only = NO;
768 ConfigFileEntry.map_oper_only = YES;
769 ConfigFileEntry.operspy_admin_only = NO;
770 ConfigFileEntry.pace_wait = 10;
771 ConfigFileEntry.caller_id_wait = 60;
772 ConfigFileEntry.pace_wait_simple = 1;
773 ConfigFileEntry.short_motd = NO;
774 ConfigFileEntry.no_oper_flood = NO;
775 ConfigFileEntry.default_invisible = NO;
776 ConfigFileEntry.fname_userlog = NULL;
777 ConfigFileEntry.fname_fuserlog = NULL;
778 ConfigFileEntry.fname_operlog = NULL;
779 ConfigFileEntry.fname_foperlog = NULL;
780 ConfigFileEntry.fname_serverlog = NULL;
781 ConfigFileEntry.fname_glinelog = NULL;
782 ConfigFileEntry.fname_klinelog = NULL;
783 ConfigFileEntry.fname_operspylog = NULL;
784 ConfigFileEntry.fname_ioerrorlog = NULL;
785 ConfigFileEntry.glines = NO;
786 ConfigFileEntry.use_egd = NO;
787 ConfigFileEntry.gline_time = 12 * 3600;
788 ConfigFileEntry.gline_min_cidr = 16;
789 ConfigFileEntry.gline_min_cidr6 = 48;
790 ConfigFileEntry.hide_error_messages = 1;
791 ConfigFileEntry.idletime = 0;
792 ConfigFileEntry.dots_in_ident = 0;
793 ConfigFileEntry.max_targets = MAX_TARGETS_DEFAULT;
794 DupString(ConfigFileEntry.servlink_path, SLPATH);
795 ConfigFileEntry.egdpool_path = NULL;
796 ConfigFileEntry.use_whois_actually = YES;
797 ConfigFileEntry.burst_away = NO;
798 ConfigFileEntry.hide_spoof_ips = YES;
800 #ifdef HAVE_LIBZ
801 ConfigFileEntry.compression_level = 4;
802 #endif
804 ConfigFileEntry.oper_umodes = UMODE_LOCOPS | UMODE_SERVNOTICE |
805 UMODE_OPERWALL | UMODE_WALLOP;
806 ConfigFileEntry.oper_only_umodes = UMODE_DEBUG|UMODE_OPERSPY;
808 ConfigChannel.use_except = YES;
809 ConfigChannel.use_invex = YES;
810 ConfigChannel.use_knock = YES;
811 ConfigChannel.knock_delay = 300;
812 ConfigChannel.knock_delay_channel = 60;
813 ConfigChannel.max_chans_per_user = 15;
814 ConfigChannel.max_bans = 25;
815 ConfigChannel.burst_topicwho = NO;
816 ConfigChannel.invite_ops_only = YES;
818 ConfigChannel.default_split_user_count = 15000;
819 ConfigChannel.default_split_server_count = 10;
820 ConfigChannel.no_join_on_split = NO;
821 ConfigChannel.no_create_on_split = YES;
823 ConfigServerHide.flatten_links = 0;
824 ConfigServerHide.links_delay = 300;
825 ConfigServerHide.hidden = 0;
826 ConfigServerHide.disable_hidden = 0;
828 ConfigFileEntry.min_nonwildcard = 4;
829 ConfigFileEntry.min_nonwildcard_simple = 3;
830 ConfigFileEntry.default_floodcount = 8;
831 ConfigFileEntry.client_flood = CLIENT_FLOOD_DEFAULT;
832 ConfigFileEntry.tkline_expire_notices = 0;
834 ConfigFileEntry.reject_after_count = 5;
835 ConfigFileEntry.reject_ban_time = 300;
836 ConfigFileEntry.reject_duration = 120;
837 ConfigFileEntry.max_unknown_ip = 2;
840 #undef YES
841 #undef NO
844 * read_conf()
847 * inputs - file descriptor pointing to config file to use
848 * output - None
849 * side effects - Read configuration file.
851 static void
852 read_conf(FILE * file)
854 lineno = 0;
856 set_default_conf(); /* Set default values prior to conf parsing */
857 yyparse(); /* Load the values from the conf */
858 validate_conf(); /* Check to make sure some values are still okay. */
859 /* Some global values are also loaded here. */
860 check_class(); /* Make sure classes are valid */
863 static void
864 validate_conf(void)
866 if(ConfigFileEntry.ts_warn_delta < TS_WARN_DELTA_MIN)
867 ConfigFileEntry.ts_warn_delta = TS_WARN_DELTA_DEFAULT;
869 if(ConfigFileEntry.ts_max_delta < TS_MAX_DELTA_MIN)
870 ConfigFileEntry.ts_max_delta = TS_MAX_DELTA_DEFAULT;
872 if(ConfigFileEntry.servlink_path == NULL)
873 DupString(ConfigFileEntry.servlink_path, SLPATH);
875 if(ServerInfo.network_name == NULL)
876 DupString(ServerInfo.network_name, NETWORK_NAME_DEFAULT);
878 if(ServerInfo.network_desc == NULL)
879 DupString(ServerInfo.network_desc, NETWORK_DESC_DEFAULT);
881 if((ConfigFileEntry.client_flood < CLIENT_FLOOD_MIN) ||
882 (ConfigFileEntry.client_flood > CLIENT_FLOOD_MAX))
883 ConfigFileEntry.client_flood = CLIENT_FLOOD_MAX;
885 GlobalSetOptions.idletime = (ConfigFileEntry.idletime * 60);
887 if(!split_users || !split_servers ||
888 (!ConfigChannel.no_create_on_split && !ConfigChannel.no_join_on_split))
890 eventDelete(check_splitmode, NULL);
891 splitmode = 0;
892 splitchecking = 0;
897 * lookup_confhost - start DNS lookups of all hostnames in the conf
898 * line and convert an IP addresses in a.b.c.d number for to IP#s.
903 * conf_connect_allowed
905 * inputs - pointer to inaddr
906 * - int type ipv4 or ipv6
907 * output - ban info or NULL
908 * side effects - none
910 struct ConfItem *
911 conf_connect_allowed(struct sockaddr *addr, int aftype)
913 struct ConfItem *aconf = find_dline(addr, aftype);
915 /* DLINE exempt also gets you out of static limits/pacing... */
916 if(aconf && (aconf->status & CONF_EXEMPTDLINE))
917 return NULL;
919 if(aconf != NULL)
920 return aconf;
922 return NULL;
925 /* add_temp_kline()
927 * inputs - pointer to struct ConfItem
928 * output - none
929 * Side effects - links in given struct ConfItem into
930 * temporary kline link list
932 void
933 add_temp_kline(struct ConfItem *aconf)
935 if(aconf->hold >= CurrentTime + (10080 * 60))
937 dlinkAddAlloc(aconf, &temp_klines[TEMP_WEEK]);
938 aconf->port = TEMP_WEEK;
940 else if(aconf->hold >= CurrentTime + (1440 * 60))
942 dlinkAddAlloc(aconf, &temp_klines[TEMP_DAY]);
943 aconf->port = TEMP_DAY;
945 else if(aconf->hold >= CurrentTime + (60 * 60))
947 dlinkAddAlloc(aconf, &temp_klines[TEMP_HOUR]);
948 aconf->port = TEMP_HOUR;
950 else
952 dlinkAddAlloc(aconf, &temp_klines[TEMP_MIN]);
953 aconf->port = TEMP_MIN;
956 aconf->flags |= CONF_FLAGS_TEMPORARY;
957 add_conf_by_address(aconf->host, CONF_KILL, aconf->user, aconf);
960 /* add_temp_dline()
962 * input - pointer to struct ConfItem
963 * output - none
964 * side effects - added to tdline link list and address hash
966 void
967 add_temp_dline(struct ConfItem *aconf)
969 if(aconf->hold >= CurrentTime + (10080 * 60))
971 dlinkAddAlloc(aconf, &temp_dlines[TEMP_WEEK]);
972 aconf->port = TEMP_WEEK;
974 else if(aconf->hold >= CurrentTime + (1440 * 60))
976 dlinkAddAlloc(aconf, &temp_dlines[TEMP_DAY]);
977 aconf->port = TEMP_DAY;
979 else if(aconf->hold >= CurrentTime + (60 * 60))
981 dlinkAddAlloc(aconf, &temp_dlines[TEMP_HOUR]);
982 aconf->port = TEMP_HOUR;
984 else
986 dlinkAddAlloc(aconf, &temp_dlines[TEMP_MIN]);
987 aconf->port = TEMP_MIN;
990 aconf->flags |= CONF_FLAGS_TEMPORARY;
991 add_conf_by_address(aconf->host, CONF_DLINE, aconf->user, aconf);
994 /* expire_tkline()
996 * inputs - list pointer
997 * - type
998 * output - NONE
999 * side effects - expire tklines and moves them between lists
1001 static void
1002 expire_temp_kd(void *list)
1004 dlink_node *ptr;
1005 dlink_node *next_ptr;
1006 struct ConfItem *aconf;
1008 DLINK_FOREACH_SAFE(ptr, next_ptr, ((dlink_list *) list)->head)
1010 aconf = ptr->data;
1012 if(aconf->hold <= CurrentTime)
1014 /* Alert opers that a TKline expired - Hwy */
1015 if(ConfigFileEntry.tkline_expire_notices)
1016 sendto_realops_flags(UMODE_ALL, L_ALL,
1017 "Temporary K-line for [%s@%s] expired",
1018 (aconf->user) ? aconf->
1019 user : "*", (aconf->host) ? aconf->host : "*");
1021 delete_one_address_conf(aconf->host, aconf);
1022 dlinkDestroy(ptr, list);
1027 static void
1028 reorganise_temp_kd(void *list)
1030 struct ConfItem *aconf;
1031 dlink_node *ptr, *next_ptr;
1033 DLINK_FOREACH_SAFE(ptr, next_ptr, ((dlink_list *) list)->head)
1035 aconf = ptr->data;
1037 if(aconf->hold < (CurrentTime + (60 * 60)))
1039 dlinkMoveNode(ptr, list, (aconf->status == CONF_KILL) ?
1040 &temp_klines[TEMP_MIN] : &temp_dlines[TEMP_MIN]);
1041 aconf->port = TEMP_MIN;
1043 else if(aconf->port > TEMP_HOUR)
1045 if(aconf->hold < (CurrentTime + (1440 * 60)))
1047 dlinkMoveNode(ptr, list, (aconf->status == CONF_KILL) ?
1048 &temp_klines[TEMP_HOUR] : &temp_dlines[TEMP_HOUR]);
1049 aconf->port = TEMP_HOUR;
1051 else if(aconf->port > TEMP_DAY &&
1052 (aconf->hold < (CurrentTime + (10080 * 60))))
1054 dlinkMoveNode(ptr, list, (aconf->status == CONF_KILL) ?
1055 &temp_klines[TEMP_DAY] : &temp_dlines[TEMP_DAY]);
1056 aconf->port = TEMP_DAY;
1063 /* const char* get_oper_name(struct Client *client_p)
1064 * Input: A client to find the active oper{} name for.
1065 * Output: The nick!user@host{oper} of the oper.
1066 * "oper" is server name for remote opers
1067 * Side effects: None.
1069 char *
1070 get_oper_name(struct Client *client_p)
1072 /* +5 for !,@,{,} and null */
1073 static char buffer[NICKLEN + USERLEN + HOSTLEN + HOSTLEN + 5];
1075 if(MyOper(client_p))
1077 ircsnprintf(buffer, sizeof(buffer), "%s!%s@%s{%s}",
1078 client_p->name, client_p->username,
1079 client_p->host, client_p->localClient->opername);
1080 return buffer;
1083 ircsnprintf(buffer, sizeof(buffer), "%s!%s@%s{%s}",
1084 client_p->name, client_p->username,
1085 client_p->host, client_p->servptr->name);
1086 return buffer;
1089 /* returns class name */
1090 char *
1091 get_class_name(struct ConfItem *aconf)
1093 static char zero[] = "default";
1095 if(aconf == NULL || aconf->c_class == NULL || EmptyString(ConfClassName(aconf)))
1096 return zero;
1098 return ConfClassName(aconf);
1102 * get_printable_conf
1104 * inputs - struct ConfItem
1106 * output - name
1107 * - host
1108 * - pass
1109 * - user
1110 * - port
1112 * side effects -
1113 * Examine the struct struct ConfItem, setting the values
1114 * of name, host, pass, user to values either
1115 * in aconf, or "<NULL>" port is set to aconf->port in all cases.
1117 void
1118 get_printable_conf(struct ConfItem *aconf, char **name, char **host,
1119 char **pass, char **user, int *port, char **classname)
1121 static char null[] = "<NULL>";
1123 *name = EmptyString(aconf->name) ? null : aconf->name;
1124 *host = EmptyString(aconf->host) ? null : aconf->host;
1125 *pass = EmptyString(aconf->passwd) ? null : aconf->passwd;
1126 *user = EmptyString(aconf->user) ? null : aconf->user;
1127 *classname = get_class_name(aconf);
1128 *port = (int) aconf->port;
1131 void
1132 get_printable_kline(struct Client *source_p, struct ConfItem *aconf,
1133 char **host, char **reason,
1134 char **user, char **oper_reason)
1136 static char null[] = "<NULL>";
1138 *host = EmptyString(aconf->host) ? null : aconf->host;
1139 *reason = EmptyString(aconf->passwd) ? null : aconf->passwd;
1140 *user = EmptyString(aconf->user) ? null : aconf->user;
1142 if(EmptyString(aconf->spasswd) || !IsOper(source_p))
1143 *oper_reason = NULL;
1144 else
1145 *oper_reason = aconf->spasswd;
1149 * read_conf_files
1151 * inputs - cold start YES or NO
1152 * output - none
1153 * side effects - read all conf files needed, ircd.conf kline.conf etc.
1155 void
1156 read_conf_files(int cold)
1158 const char *filename;
1160 conf_fbfile_in = NULL;
1162 filename = get_conf_name(CONF_TYPE);
1164 /* We need to know the initial filename for the yyerror() to report
1165 FIXME: The full path is in conffilenamebuf first time since we
1166 dont know anything else
1168 - Gozem 2002-07-21
1170 strlcpy(conffilebuf, filename, sizeof(conffilebuf));
1172 if((conf_fbfile_in = fopen(filename, "r")) == NULL)
1174 if(cold)
1176 ilog(L_MAIN, "Failed in reading configuration file %s", filename);
1177 exit(-1);
1179 else
1181 sendto_realops_flags(UMODE_ALL, L_ALL,
1182 "Can't open file '%s' - aborting rehash!", filename);
1183 return;
1187 if(!cold)
1189 clear_out_old_conf();
1192 read_conf(conf_fbfile_in);
1193 fclose(conf_fbfile_in);
1197 * clear_out_old_conf
1199 * inputs - none
1200 * output - none
1201 * side effects - Clear out the old configuration
1203 static void
1204 clear_out_old_conf(void)
1206 struct Class *cltmp;
1207 dlink_node *ptr;
1208 #ifdef ENABLE_SERVICES
1209 dlink_node *next_ptr;
1210 #endif
1213 * don't delete the class table, rather mark all entries
1214 * for deletion. The table is cleaned up by check_class. - avalon
1216 DLINK_FOREACH(ptr, class_list.head)
1218 cltmp = ptr->data;
1219 MaxUsers(cltmp) = -1;
1222 clear_out_address_conf();
1223 clear_s_newconf();
1225 /* clean out module paths */
1226 #ifndef STATIC_MODULES
1227 mod_clear_paths();
1228 mod_add_path(MODULE_DIR);
1229 mod_add_path(MODULE_DIR "/autoload");
1230 #endif
1232 /* clean out ServerInfo */
1233 MyFree(ServerInfo.description);
1234 ServerInfo.description = NULL;
1235 MyFree(ServerInfo.network_name);
1236 ServerInfo.network_name = NULL;
1237 MyFree(ServerInfo.network_desc);
1238 ServerInfo.network_desc = NULL;
1240 /* clean out AdminInfo */
1241 MyFree(AdminInfo.name);
1242 AdminInfo.name = NULL;
1243 MyFree(AdminInfo.email);
1244 AdminInfo.email = NULL;
1245 MyFree(AdminInfo.description);
1246 AdminInfo.description = NULL;
1248 /* operator{} and class{} blocks are freed above */
1249 /* clean out listeners */
1250 close_listeners();
1252 /* auth{}, quarantine{}, shared{}, connect{}, kill{}, deny{}, exempt{}
1253 * and gecos{} blocks are freed above too
1256 /* clean out general */
1257 MyFree(ConfigFileEntry.servlink_path);
1258 ConfigFileEntry.servlink_path = NULL;
1260 #ifdef ENABLE_SERVICES
1261 DLINK_FOREACH_SAFE(ptr, next_ptr, service_list.head)
1263 MyFree(ptr->data);
1264 dlinkDestroy(ptr, &service_list);
1266 #endif
1268 /* OK, that should be everything... */
1272 /* write_confitem()
1274 * inputs - kline, dline or resv type flag
1275 * - client pointer to report to
1276 * - user name of target
1277 * - host name of target
1278 * - reason for target
1279 * - time string
1280 * - type of xline
1281 * output - NONE
1282 * side effects - This function takes care of finding the right conf
1283 * file and adding the line to it, as well as notifying
1284 * opers and the user.
1286 void
1287 write_confitem(KlineType type, struct Client *source_p, char *user,
1288 char *host, const char *reason, const char *oper_reason,
1289 const char *current_date, int xtype)
1291 char buffer[1024];
1292 FILE *out;
1293 const char *filename; /* filename to use for kline */
1295 filename = get_conf_name(type);
1298 if((out = fopen(filename, "a")) == NULL)
1300 sendto_realops_flags(UMODE_ALL, L_ALL, "*** Problem opening %s ", filename);
1301 return;
1304 if(oper_reason == NULL)
1305 oper_reason = "";
1307 if(type == KLINE_TYPE)
1309 ircsnprintf(buffer, sizeof(buffer),
1310 "\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",%ld\n",
1311 user, host, reason, oper_reason, current_date,
1312 get_oper_name(source_p), CurrentTime);
1314 else if(type == DLINE_TYPE)
1316 ircsnprintf(buffer, sizeof(buffer),
1317 "\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",%ld\n", host,
1318 reason, oper_reason, current_date, get_oper_name(source_p), CurrentTime);
1320 else if(type == RESV_TYPE)
1322 ircsnprintf(buffer, sizeof(buffer), "\"%s\",\"%s\",\"%s\",%ld\n",
1323 host, reason, get_oper_name(source_p), CurrentTime);
1326 if(fputs(buffer, out) == -1)
1328 sendto_realops_flags(UMODE_ALL, L_ALL, "*** Problem writing to %s", filename);
1329 fclose(out);
1330 return;
1333 fclose(out);
1335 if(type == KLINE_TYPE)
1337 if(EmptyString(oper_reason))
1339 sendto_realops_flags(UMODE_ALL, L_ALL,
1340 "%s added K-Line for [%s@%s] [%s]",
1341 get_oper_name(source_p), user,
1342 host, reason);
1343 ilog(L_KLINE, "K %s 0 %s %s %s",
1344 get_oper_name(source_p), user, host, reason);
1346 else
1348 sendto_realops_flags(UMODE_ALL, L_ALL,
1349 "%s added K-Line for [%s@%s] [%s|%s]",
1350 get_oper_name(source_p), user,
1351 host, reason, oper_reason);
1352 ilog(L_KLINE, "K %s 0 %s %s %s|%s",
1353 get_oper_name(source_p), user, host,
1354 reason, oper_reason);
1357 sendto_one_notice(source_p, ":Added K-Line [%s@%s]",
1358 user, host);
1360 else if(type == DLINE_TYPE)
1362 if(EmptyString(oper_reason))
1364 sendto_realops_flags(UMODE_ALL, L_ALL,
1365 "%s added D-Line for [%s] [%s]",
1366 get_oper_name(source_p), host, reason);
1367 ilog(L_KLINE, "D %s 0 %s %s",
1368 get_oper_name(source_p), host, reason);
1370 else
1372 sendto_realops_flags(UMODE_ALL, L_ALL,
1373 "%s added D-Line for [%s] [%s|%s]",
1374 get_oper_name(source_p), host,
1375 reason, oper_reason);
1376 ilog(L_KLINE, "D %s 0 %s %s|%s",
1377 get_oper_name(source_p), host,
1378 reason, oper_reason);
1381 sendto_one(source_p,
1382 ":%s NOTICE %s :Added D-Line [%s] to %s", me.name,
1383 source_p->name, host, filename);
1386 else if(type == RESV_TYPE)
1388 sendto_realops_flags(UMODE_ALL, L_ALL,
1389 "%s added RESV for [%s] [%s]",
1390 get_oper_name(source_p), host, reason);
1391 ilog(L_KLINE, "R %s 0 %s %s",
1392 get_oper_name(source_p), host, reason);
1394 sendto_one_notice(source_p, ":Added RESV for [%s] [%s]",
1395 host, reason);
1399 /* get_conf_name
1401 * inputs - type of conf file to return name of file for
1402 * output - pointer to filename for type of conf
1403 * side effects - none
1405 const char *
1406 get_conf_name(KlineType type)
1408 if(type == CONF_TYPE)
1410 return (ConfigFileEntry.configfile);
1412 else if(type == DLINE_TYPE)
1414 return (ConfigFileEntry.dlinefile);
1416 else if(type == RESV_TYPE)
1418 return (ConfigFileEntry.resvfile);
1421 return ConfigFileEntry.klinefile;
1425 * conf_add_class_to_conf
1426 * inputs - pointer to config item
1427 * output - NONE
1428 * side effects - Add a class pointer to a conf
1431 void
1432 conf_add_class_to_conf(struct ConfItem *aconf, const char *classname)
1434 if(EmptyString(classname))
1436 ClassPtr(aconf) = default_class;
1437 return;
1440 ClassPtr(aconf) = find_class(classname);
1442 if(ClassPtr(aconf) == default_class)
1444 if(aconf->status == CONF_CLIENT)
1446 sendto_realops_flags(UMODE_ALL, L_ALL,
1447 "Warning -- Using default class for missing class \"%s\" in auth{} for %s@%s",
1448 classname, aconf->user, aconf->host);
1451 return;
1454 if(ConfMaxUsers(aconf) < 0)
1456 ClassPtr(aconf) = default_class;
1457 return;
1462 * conf_add_d_conf
1463 * inputs - pointer to config item
1464 * output - NONE
1465 * side effects - Add a d/D line
1467 void
1468 conf_add_d_conf(struct ConfItem *aconf)
1470 if(aconf->host == NULL)
1471 return;
1473 aconf->user = NULL;
1475 /* XXX - Should 'd' ever be in the old conf? For new conf we don't
1476 * need this anyway, so I will disable it for now... -A1kmm
1479 if(parse_netmask(aconf->host, NULL, NULL) == HM_HOST)
1481 ilog(L_MAIN, "Invalid Dline %s ignored", aconf->host);
1482 free_conf(aconf);
1484 else
1486 add_conf_by_address(aconf->host, CONF_DLINE, NULL, aconf);
1492 * yyerror
1494 * inputs - message from parser
1495 * output - none
1496 * side effects - message to opers and log file entry is made
1498 void
1499 yyerror(const char *msg)
1501 char newlinebuf[BUFSIZE];
1503 strip_tabs(newlinebuf, (const unsigned char *) linebuf, strlen(linebuf));
1505 sendto_realops_flags(UMODE_ALL, L_ALL, "\"%s\", line %d: %s at '%s'",
1506 conffilebuf, lineno + 1, msg, newlinebuf);
1508 ilog(L_MAIN, "\"%s\", line %d: %s at '%s'", conffilebuf, lineno + 1, msg, newlinebuf);
1512 conf_fgets(char *lbuf, int max_size, FILE * fb)
1514 char *p;
1516 if(fgets(lbuf, max_size, fb) == NULL)
1517 return (0);
1519 if((p = strpbrk(lbuf, "\r\n")) != NULL)
1521 *p++ = '\n';
1522 *p = '\0';
1524 return (strlen(lbuf));
1528 conf_yy_fatal_error(const char *msg)
1530 return (0);