ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/conf.c
Revision: 4321
Committed: Fri Aug 1 16:55:07 2014 UTC (9 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 57839 byte(s)
Log Message:
- conf.c:ipcache_remove_expired_entries(): of course should be DLINK_FOREACH_SAFE

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2014 ircd-hybrid development team
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 * USA
20 */
21
22 /*! \file conf.c
23 * \brief Configuration file functions.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include "list.h"
29 #include "ircd_defs.h"
30 #include "conf.h"
31 #include "server.h"
32 #include "resv.h"
33 #include "channel.h"
34 #include "client.h"
35 #include "event.h"
36 #include "irc_string.h"
37 #include "s_bsd.h"
38 #include "ircd.h"
39 #include "listener.h"
40 #include "hostmask.h"
41 #include "modules.h"
42 #include "numeric.h"
43 #include "fdlist.h"
44 #include "log.h"
45 #include "send.h"
46 #include "memory.h"
47 #include "mempool.h"
48 #include "res.h"
49 #include "userhost.h"
50 #include "user.h"
51 #include "channel_mode.h"
52 #include "parse.h"
53 #include "misc.h"
54 #include "conf_db.h"
55 #include "conf_class.h"
56 #include "motd.h"
57
58
59 /* general conf items link list root, other than k lines etc. */
60 dlink_list service_items = { NULL, NULL, 0 };
61 dlink_list server_items = { NULL, NULL, 0 };
62 dlink_list cluster_items = { NULL, NULL, 0 };
63 dlink_list oconf_items = { NULL, NULL, 0 };
64 dlink_list uconf_items = { NULL, NULL, 0 };
65 dlink_list xconf_items = { NULL, NULL, 0 };
66 dlink_list nresv_items = { NULL, NULL, 0 };
67 dlink_list cresv_items = { NULL, NULL, 0 };
68
69 extern unsigned int lineno;
70 extern char linebuf[];
71 extern char conffilebuf[IRCD_BUFSIZE];
72 extern int yyparse(); /* defined in y.tab.c */
73
74 /* internally defined functions */
75 static void read_conf(FILE *);
76 static void clear_out_old_conf(void);
77 static void expire_tklines(dlink_list *);
78 static void ipcache_remove_expired_entries(void *);
79 static uint32_t hash_ip(const struct irc_ssaddr *);
80 static int verify_access(struct Client *);
81 static int attach_iline(struct Client *, struct MaskItem *);
82 static struct ip_entry *find_or_add_ip(struct irc_ssaddr *);
83 static dlink_list *map_to_list(enum maskitem_type);
84 static int find_user_host(struct Client *, char *, char *, char *, unsigned int);
85
86
87 /* usually, with hash tables, you use a prime number...
88 * but in this case I am dealing with ip addresses,
89 * not ascii strings.
90 */
91 #define IP_HASH_SIZE 0x1000
92
93 struct ip_entry
94 {
95 dlink_node node; /**< Doubly linked list node. */
96 struct irc_ssaddr ip;
97 unsigned int count; /**< Number of registered users using this IP */
98 unsigned int connection_count; /**< Number of connections from this IP in the last throttle_time duration */
99 time_t last_attempt; /**< The last time someone connected from this IP */
100 };
101
102 static dlink_list ip_hash_table[IP_HASH_SIZE];
103 static mp_pool_t *ip_entry_pool = NULL;
104
105
106 /* conf_dns_callback()
107 *
108 * inputs - pointer to struct MaskItem
109 * - pointer to DNSReply reply
110 * output - none
111 * side effects - called when resolver query finishes
112 * if the query resulted in a successful search, hp will contain
113 * a non-null pointer, otherwise hp will be null.
114 * if successful save hp in the conf item it was called with
115 */
116 static void
117 conf_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name)
118 {
119 struct MaskItem *conf = vptr;
120
121 conf->dns_pending = 0;
122
123 if (addr)
124 memcpy(&conf->addr, addr, sizeof(conf->addr));
125 else
126 conf->dns_failed = 1;
127 }
128
129 /* conf_dns_lookup()
130 *
131 * do a nameserver lookup of the conf host
132 * if the conf entry is currently doing a ns lookup do nothing, otherwise
133 * allocate a dns_query and start ns lookup.
134 */
135 static void
136 conf_dns_lookup(struct MaskItem *conf)
137 {
138 if (!conf->dns_pending)
139 {
140 conf->dns_pending = 1;
141 gethost_byname(conf_dns_callback, conf, conf->host);
142 }
143 }
144
145 struct MaskItem *
146 conf_make(enum maskitem_type type)
147 {
148 struct MaskItem *conf = MyCalloc(sizeof(*conf));
149 dlink_list *list = NULL;
150
151 conf->type = type;
152 conf->active = 1;
153 conf->aftype = AF_INET;
154
155 if ((list = map_to_list(type)))
156 dlinkAdd(conf, &conf->node, list);
157 return conf;
158 }
159
160 void
161 conf_free(struct MaskItem *conf)
162 {
163 dlink_node *ptr = NULL, *ptr_next = NULL;
164 dlink_list *list = NULL;
165
166 if (conf->node.next)
167 if ((list = map_to_list(conf->type)))
168 dlinkDelete(&conf->node, list);
169
170 MyFree(conf->name);
171
172 if (conf->dns_pending)
173 delete_resolver_queries(conf);
174 if (conf->passwd)
175 memset(conf->passwd, 0, strlen(conf->passwd));
176 if (conf->spasswd)
177 memset(conf->spasswd, 0, strlen(conf->spasswd));
178
179 conf->class = NULL;
180
181 MyFree(conf->passwd);
182 MyFree(conf->spasswd);
183 MyFree(conf->reason);
184 MyFree(conf->certfp);
185 MyFree(conf->user);
186 MyFree(conf->host);
187 #ifdef HAVE_LIBCRYPTO
188 MyFree(conf->cipher_list);
189
190 if (conf->rsa_public_key)
191 RSA_free(conf->rsa_public_key);
192 #endif
193 DLINK_FOREACH_SAFE(ptr, ptr_next, conf->hub_list.head)
194 {
195 MyFree(ptr->data);
196 dlinkDelete(ptr, &conf->hub_list);
197 free_dlink_node(ptr);
198 }
199
200 DLINK_FOREACH_SAFE(ptr, ptr_next, conf->leaf_list.head)
201 {
202 MyFree(ptr->data);
203 dlinkDelete(ptr, &conf->leaf_list);
204 free_dlink_node(ptr);
205 }
206
207 DLINK_FOREACH_SAFE(ptr, ptr_next, conf->exempt_list.head)
208 {
209 struct exempt *exptr = ptr->data;
210
211 dlinkDelete(ptr, &conf->exempt_list);
212 MyFree(exptr->name);
213 MyFree(exptr->user);
214 MyFree(exptr->host);
215 MyFree(exptr);
216 }
217
218 MyFree(conf);
219 }
220
221 /* check_client()
222 *
223 * inputs - pointer to client
224 * output - 0 = Success
225 * NOT_AUTHORIZED (-1) = Access denied (no I line match)
226 * IRCD_SOCKET_ERROR (-2) = Bad socket.
227 * I_LINE_FULL (-3) = I-line is full
228 * TOO_MANY (-4) = Too many connections from hostname
229 * BANNED_CLIENT (-5) = K-lined
230 * side effects - Ordinary client access check.
231 * Look for conf lines which have the same
232 * status as the flags passed.
233 */
234 int
235 check_client(struct Client *source_p)
236 {
237 int i;
238
239 if ((i = verify_access(source_p)))
240 ilog(LOG_TYPE_IRCD, "Access denied: %s[%s]",
241 source_p->name, source_p->sockhost);
242
243 switch (i)
244 {
245 case TOO_MANY:
246 sendto_realops_flags(UMODE_FULL, L_ALL, SEND_NOTICE,
247 "Too many on IP for %s (%s).",
248 get_client_name(source_p, SHOW_IP),
249 source_p->sockhost);
250 ilog(LOG_TYPE_IRCD, "Too many connections on IP from %s.",
251 get_client_name(source_p, SHOW_IP));
252 ++ServerStats.is_ref;
253 exit_client(source_p, "No more connections allowed on that IP");
254 break;
255
256 case I_LINE_FULL:
257 sendto_realops_flags(UMODE_FULL, L_ALL, SEND_NOTICE,
258 "auth{} block is full for %s (%s).",
259 get_client_name(source_p, SHOW_IP),
260 source_p->sockhost);
261 ilog(LOG_TYPE_IRCD, "Too many connections from %s.",
262 get_client_name(source_p, SHOW_IP));
263 ++ServerStats.is_ref;
264 exit_client(source_p, "No more connections allowed in your connection class");
265 break;
266
267 case NOT_AUTHORIZED:
268 ++ServerStats.is_ref;
269 /* jdc - lists server name & port connections are on */
270 /* a purely cosmetical change */
271 sendto_realops_flags(UMODE_UNAUTH, L_ALL, SEND_NOTICE,
272 "Unauthorized client connection from %s [%s] on [%s/%u].",
273 get_client_name(source_p, SHOW_IP),
274 source_p->sockhost,
275 source_p->localClient->listener->name,
276 source_p->localClient->listener->port);
277 ilog(LOG_TYPE_IRCD,
278 "Unauthorized client connection from %s on [%s/%u].",
279 get_client_name(source_p, SHOW_IP),
280 source_p->localClient->listener->name,
281 source_p->localClient->listener->port);
282
283 exit_client(source_p, "You are not authorized to use this server");
284 break;
285
286 case BANNED_CLIENT:
287 exit_client(source_p, "Banned");
288 ++ServerStats.is_ref;
289 break;
290
291 case 0:
292 default:
293 break;
294 }
295
296 return (i < 0 ? 0 : 1);
297 }
298
299 /* verify_access()
300 *
301 * inputs - pointer to client to verify
302 * output - 0 if success -'ve if not
303 * side effect - find the first (best) I line to attach.
304 */
305 static int
306 verify_access(struct Client *client_p)
307 {
308 struct MaskItem *conf = NULL;
309 char non_ident[USERLEN + 1] = "~";
310
311 if (IsGotId(client_p))
312 {
313 conf = find_address_conf(client_p->host, client_p->username,
314 &client_p->localClient->ip,
315 client_p->localClient->aftype,
316 client_p->localClient->passwd);
317 }
318 else
319 {
320 strlcpy(non_ident + 1, client_p->username, sizeof(non_ident) - 1);
321 conf = find_address_conf(client_p->host,non_ident,
322 &client_p->localClient->ip,
323 client_p->localClient->aftype,
324 client_p->localClient->passwd);
325 }
326
327 if (conf)
328 {
329 if (IsConfClient(conf))
330 {
331 if (IsConfRedir(conf))
332 {
333 sendto_one_numeric(client_p, &me, RPL_REDIR,
334 conf->name ? conf->name : "",
335 conf->port);
336 return NOT_AUTHORIZED;
337 }
338
339 if (IsConfDoIdentd(conf))
340 SetNeedId(client_p);
341
342 /* Thanks for spoof idea amm */
343 if (IsConfDoSpoofIp(conf))
344 {
345 if (!ConfigFileEntry.hide_spoof_ips && IsConfSpoofNotice(conf))
346 sendto_realops_flags(UMODE_ALL, L_ADMIN, SEND_NOTICE,
347 "%s spoofing: %s as %s",
348 client_p->name, client_p->host, conf->name);
349 strlcpy(client_p->host, conf->name, sizeof(client_p->host));
350 AddFlag(client_p, FLAGS_IP_SPOOFING | FLAGS_AUTH_SPOOF);
351 }
352
353 return attach_iline(client_p, conf);
354 }
355 else if (IsConfKill(conf) || (ConfigFileEntry.glines && IsConfGline(conf)))
356 {
357 if (IsConfGline(conf))
358 sendto_one_notice(client_p, &me, ":*** G-lined");
359 sendto_one_notice(client_p, &me, ":*** Banned: %s", conf->reason);
360 return BANNED_CLIENT;
361 }
362 }
363
364 return NOT_AUTHORIZED;
365 }
366
367 /* attach_iline()
368 *
369 * inputs - client pointer
370 * - conf pointer
371 * output -
372 * side effects - do actual attach
373 */
374 static int
375 attach_iline(struct Client *client_p, struct MaskItem *conf)
376 {
377 struct ClassItem *class = NULL;
378 struct ip_entry *ip_found;
379 int a_limit_reached = 0;
380 unsigned int local = 0, global = 0, ident = 0;
381
382 assert(conf->class);
383
384 ip_found = find_or_add_ip(&client_p->localClient->ip);
385 ip_found->count++;
386 SetIpHash(client_p);
387
388 class = conf->class;
389
390 count_user_host(client_p->username, client_p->host,
391 &global, &local, &ident);
392
393 /* XXX blah. go down checking the various silly limits
394 * setting a_limit_reached if any limit is reached.
395 * - Dianora
396 */
397 if (class->max_total && class->ref_count >= class->max_total)
398 a_limit_reached = 1;
399 else if (class->max_perip && ip_found->count > class->max_perip)
400 a_limit_reached = 1;
401 else if (class->max_local && local >= class->max_local)
402 a_limit_reached = 1;
403 else if (class->max_global && global >= class->max_global)
404 a_limit_reached = 1;
405 else if (class->max_ident && ident >= class->max_ident &&
406 client_p->username[0] != '~')
407 a_limit_reached = 1;
408
409 if (a_limit_reached)
410 {
411 if (!IsConfExemptLimits(conf))
412 return TOO_MANY; /* Already at maximum allowed */
413
414 sendto_one_notice(client_p, &me, ":*** Your connection class is full, "
415 "but you have exceed_limit = yes;");
416 }
417
418 return attach_conf(client_p, conf);
419 }
420
421 /* init_ip_hash_table()
422 *
423 * inputs - NONE
424 * output - NONE
425 * side effects - allocate memory for ip_entry(s)
426 * - clear the ip hash table
427 */
428 void
429 ipcache_init(void)
430 {
431 static struct event event_expire_ipcache =
432 {
433 .name = "ipcache_remove_expired_entries",
434 .handler = ipcache_remove_expired_entries,
435 .when = 123
436 };
437
438 event_add(&event_expire_ipcache, NULL);
439 ip_entry_pool = mp_pool_new(sizeof(struct ip_entry), MP_CHUNK_SIZE_IP_ENTRY);
440 }
441
442 /* find_or_add_ip()
443 *
444 * inputs - pointer to struct irc_ssaddr
445 * output - pointer to a struct ip_entry
446 * side effects -
447 *
448 * If the ip # was not found, a new struct ip_entry is created, and the ip
449 * count set to 0.
450 */
451 static struct ip_entry *
452 find_or_add_ip(struct irc_ssaddr *addr)
453 {
454 dlink_node *ptr = NULL;
455 struct ip_entry *iptr = NULL;
456 uint32_t hash_index = hash_ip(addr);
457 int res = 0;
458 struct sockaddr_in *v4 = (struct sockaddr_in *)addr, *ptr_v4;
459 #ifdef IPV6
460 struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr, *ptr_v6;
461 #endif
462
463 DLINK_FOREACH(ptr, ip_hash_table[hash_index].head)
464 {
465 iptr = ptr->data;
466 #ifdef IPV6
467 if (iptr->ip.ss.ss_family != addr->ss.ss_family)
468 continue;
469
470 if (addr->ss.ss_family == AF_INET6)
471 {
472 ptr_v6 = (struct sockaddr_in6 *)&iptr->ip;
473 res = memcmp(&v6->sin6_addr, &ptr_v6->sin6_addr, sizeof(struct in6_addr));
474 }
475 else
476 #endif
477 {
478 ptr_v4 = (struct sockaddr_in *)&iptr->ip;
479 res = memcmp(&v4->sin_addr, &ptr_v4->sin_addr, sizeof(struct in_addr));
480 }
481
482 if (res == 0)
483 return iptr; /* Found entry already in hash, return it. */
484 }
485
486 iptr = mp_pool_get(ip_entry_pool);
487 memcpy(&iptr->ip, addr, sizeof(struct irc_ssaddr));
488
489 dlinkAdd(iptr, &iptr->node, &atable[hash_index]);
490
491 return iptr;
492 }
493
494 /* remove_one_ip()
495 *
496 * inputs - unsigned long IP address value
497 * output - NONE
498 * side effects - The ip address given, is looked up in ip hash table
499 * and number of ip#'s for that ip decremented.
500 * If ip # count reaches 0 and has expired,
501 * the struct ip_entry is returned to the ip_entry_heap
502 */
503 void
504 remove_one_ip(struct irc_ssaddr *addr)
505 {
506 dlink_node *ptr = NULL;
507 uint32_t hash_index = hash_ip(addr);
508 int res = 0;
509 struct sockaddr_in *v4 = (struct sockaddr_in *)addr, *ptr_v4;
510 #ifdef IPV6
511 struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addr, *ptr_v6;
512 #endif
513
514 DLINK_FOREACH(ptr, ip_hash_table[hash_index].head)
515 {
516 struct ip_entry *iptr = ptr->data;
517 #ifdef IPV6
518 if (iptr->ip.ss.ss_family != addr->ss.ss_family)
519 continue;
520 if (addr->ss.ss_family == AF_INET6)
521 {
522 ptr_v6 = (struct sockaddr_in6 *)&iptr->ip;
523 res = memcmp(&v6->sin6_addr, &ptr_v6->sin6_addr, sizeof(struct in6_addr));
524 }
525 else
526 #endif
527 {
528 ptr_v4 = (struct sockaddr_in *)&iptr->ip;
529 res = memcmp(&v4->sin_addr, &ptr_v4->sin_addr, sizeof(struct in_addr));
530 }
531
532 if (res)
533 continue;
534 assert(iptr->count > 0);
535
536 if (--iptr->count == 0 &&
537 (CurrentTime - iptr->last_attempt) >= ConfigFileEntry.throttle_time)
538 {
539 dlinkDelete(&iptr->node, &ip_hash_table[hash_index]);
540 mp_pool_release(iptr);
541 return;
542 }
543 }
544 }
545
546 /* hash_ip()
547 *
548 * input - pointer to an irc_inaddr
549 * output - integer value used as index into hash table
550 * side effects - hopefully, none
551 */
552 static uint32_t
553 hash_ip(const struct irc_ssaddr *addr)
554 {
555 if (addr->ss.ss_family == AF_INET)
556 {
557 const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr;
558 uint32_t hash = 0, ip = ntohl(v4->sin_addr.s_addr);
559
560 hash = ((ip >> 12) + ip) & (IP_HASH_SIZE - 1);
561 return hash;
562 }
563 #ifdef IPV6
564 else
565 {
566 const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr;
567 uint32_t hash = 0, *ip = (uint32_t *)&v6->sin6_addr.s6_addr;
568
569 hash = ip[0] ^ ip[3];
570 hash ^= hash >> 16;
571 hash ^= hash >> 8;
572 hash = hash & (IP_HASH_SIZE - 1);
573 return hash;
574 }
575 #else
576 return 0;
577 #endif
578 }
579
580 /* count_ip_hash()
581 *
582 * inputs - pointer to counter of number of ips hashed
583 * - pointer to memory used for ip hash
584 * output - returned via pointers input
585 * side effects - NONE
586 *
587 * number of hashed ip #'s is counted up, plus the amount of memory
588 * used in the hash.
589 */
590 void
591 count_ip_hash(unsigned int *number_ips_stored, uint64_t *mem_ips_stored)
592 {
593 *number_ips_stored = 0;
594 *mem_ips_stored = 0;
595
596 for (unsigned int i = 0; i < IP_HASH_SIZE; ++i)
597 {
598 *number_ips_stored += dlink_list_length(&ip_hash_table[i]);
599 *mem_ips_stored += dlink_list_length(&ip_hash_table[i]) * sizeof(struct ip_entry);
600 }
601 }
602
603 /* garbage_collect_ip_entries()
604 *
605 * input - NONE
606 * output - NONE
607 * side effects - free up all ip entries with no connections
608 */
609 static void
610 ipcache_remove_expired_entries(void *unused)
611 {
612 dlink_node *ptr = NULL, *ptr_next = NULL;
613
614 for (unsigned int i = 0; i < IP_HASH_SIZE; ++i)
615 {
616 DLINK_FOREACH_SAFE(ptr, ptr_next, ip_hash_table[i].head)
617 {
618 struct ip_entry *iptr = ptr->data;
619
620 if (iptr->count == 0 &&
621 (CurrentTime - iptr->last_attempt) >= ConfigFileEntry.throttle_time)
622 {
623 dlinkDelete(&iptr->node, &ip_hash_table[i]);
624 mp_pool_release(iptr);
625 }
626 }
627 }
628 }
629
630 /* detach_conf()
631 *
632 * inputs - pointer to client to detach
633 * - type of conf to detach
634 * output - 0 for success, -1 for failure
635 * side effects - Disassociate configuration from the client.
636 * Also removes a class from the list if marked for deleting.
637 */
638 void
639 detach_conf(struct Client *client_p, enum maskitem_type type)
640 {
641 dlink_node *ptr = NULL, *ptr_next = NULL;
642
643 DLINK_FOREACH_SAFE(ptr, ptr_next, client_p->localClient->confs.head)
644 {
645 struct MaskItem *conf = ptr->data;
646
647 assert(conf->type & (CONF_CLIENT | CONF_OPER | CONF_SERVER));
648 assert(conf->ref_count > 0);
649 assert(conf->class->ref_count > 0);
650
651 if (!(conf->type & type))
652 continue;
653
654 dlinkDelete(ptr, &client_p->localClient->confs);
655 free_dlink_node(ptr);
656
657 if (conf->type == CONF_CLIENT)
658 remove_from_cidr_check(&client_p->localClient->ip, conf->class);
659
660 if (--conf->class->ref_count == 0 && conf->class->active == 0)
661 {
662 class_free(conf->class);
663 conf->class = NULL;
664 }
665
666 if (--conf->ref_count == 0 && conf->active == 0)
667 conf_free(conf);
668 }
669 }
670
671 /* attach_conf()
672 *
673 * inputs - client pointer
674 * - conf pointer
675 * output -
676 * side effects - Associate a specific configuration entry to a *local*
677 * client (this is the one which used in accepting the
678 * connection). Note, that this automatically changes the
679 * attachment if there was an old one...
680 */
681 int
682 attach_conf(struct Client *client_p, struct MaskItem *conf)
683 {
684 if (dlinkFind(&client_p->localClient->confs, conf))
685 return 1;
686
687 if (conf->type == CONF_CLIENT)
688 if (cidr_limit_reached(IsConfExemptLimits(conf),
689 &client_p->localClient->ip, conf->class))
690 return TOO_MANY; /* Already at maximum allowed */
691
692 conf->class->ref_count++;
693 conf->ref_count++;
694
695 dlinkAdd(conf, make_dlink_node(), &client_p->localClient->confs);
696
697 return 0;
698 }
699
700 /* attach_connect_block()
701 *
702 * inputs - pointer to server to attach
703 * - name of server
704 * - hostname of server
705 * output - true (1) if both are found, otherwise return false (0)
706 * side effects - find connect block and attach them to connecting client
707 */
708 int
709 attach_connect_block(struct Client *client_p, const char *name,
710 const char *host)
711 {
712 dlink_node *ptr;
713 struct MaskItem *conf = NULL;
714
715 assert(client_p != NULL);
716 assert(host != NULL);
717
718 if (client_p == NULL || host == NULL)
719 return 0;
720
721 DLINK_FOREACH(ptr, server_items.head)
722 {
723 conf = ptr->data;
724
725 if (match(conf->name, name) || match(conf->host, host))
726 continue;
727
728 attach_conf(client_p, conf);
729 return -1;
730 }
731
732 return 0;
733 }
734
735 /* find_conf_name()
736 *
737 * inputs - pointer to conf link list to search
738 * - pointer to name to find
739 * - int mask of type of conf to find
740 * output - NULL or pointer to conf found
741 * side effects - find a conf entry which matches the name
742 * and has the given mask.
743 */
744 struct MaskItem *
745 find_conf_name(dlink_list *list, const char *name, enum maskitem_type type)
746 {
747 dlink_node *ptr;
748 struct MaskItem* conf;
749
750 DLINK_FOREACH(ptr, list->head)
751 {
752 conf = ptr->data;
753
754 if (conf->type == type)
755 {
756 if (conf->name && (!irccmp(conf->name, name) ||
757 !match(conf->name, name)))
758 return conf;
759 }
760 }
761
762 return NULL;
763 }
764
765 /* map_to_list()
766 *
767 * inputs - ConfType conf
768 * output - pointer to dlink_list to use
769 * side effects - none
770 */
771 static dlink_list *
772 map_to_list(enum maskitem_type type)
773 {
774 switch(type)
775 {
776 case CONF_XLINE:
777 return(&xconf_items);
778 break;
779 case CONF_ULINE:
780 return(&uconf_items);
781 break;
782 case CONF_NRESV:
783 return(&nresv_items);
784 break;
785 case CONF_CRESV:
786 return(&cresv_items);
787 case CONF_OPER:
788 return(&oconf_items);
789 break;
790 case CONF_SERVER:
791 return(&server_items);
792 break;
793 case CONF_SERVICE:
794 return(&service_items);
795 break;
796 case CONF_CLUSTER:
797 return(&cluster_items);
798 break;
799 default:
800 return NULL;
801 }
802 }
803
804 /* find_matching_name_conf()
805 *
806 * inputs - type of link list to look in
807 * - pointer to name string to find
808 * - pointer to user
809 * - pointer to host
810 * - optional flags to match on as well
811 * output - NULL or pointer to found struct MaskItem
812 * side effects - looks for a match on name field
813 */
814 struct MaskItem *
815 find_matching_name_conf(enum maskitem_type type, const char *name, const char *user,
816 const char *host, unsigned int flags)
817 {
818 dlink_node *ptr=NULL;
819 struct MaskItem *conf=NULL;
820 dlink_list *list_p = map_to_list(type);
821
822 switch (type)
823 {
824 case CONF_SERVICE:
825 DLINK_FOREACH(ptr, list_p->head)
826 {
827 conf = ptr->data;
828
829 if (EmptyString(conf->name))
830 continue;
831 if ((name != NULL) && !irccmp(name, conf->name))
832 return conf;
833 }
834 break;
835
836 case CONF_XLINE:
837 case CONF_ULINE:
838 case CONF_NRESV:
839 case CONF_CRESV:
840 DLINK_FOREACH(ptr, list_p->head)
841 {
842 conf = ptr->data;
843
844 if (EmptyString(conf->name))
845 continue;
846 if ((name != NULL) && !match(conf->name, name))
847 {
848 if ((user == NULL && (host == NULL)))
849 return conf;
850 if ((conf->flags & flags) != flags)
851 continue;
852 if (EmptyString(conf->user) || EmptyString(conf->host))
853 return conf;
854 if (!match(conf->user, user) && !match(conf->host, host))
855 return conf;
856 }
857 }
858 break;
859
860 case CONF_SERVER:
861 DLINK_FOREACH(ptr, list_p->head)
862 {
863 conf = ptr->data;
864
865 if ((name != NULL) && !match(name, conf->name))
866 return conf;
867 else if ((host != NULL) && !match(host, conf->host))
868 return conf;
869 }
870 break;
871
872 default:
873 break;
874 }
875 return NULL;
876 }
877
878 /* find_exact_name_conf()
879 *
880 * inputs - type of link list to look in
881 * - pointer to name string to find
882 * - pointer to user
883 * - pointer to host
884 * output - NULL or pointer to found struct MaskItem
885 * side effects - looks for an exact match on name field
886 */
887 struct MaskItem *
888 find_exact_name_conf(enum maskitem_type type, const struct Client *who, const char *name,
889 const char *user, const char *host)
890 {
891 dlink_node *ptr = NULL;
892 struct MaskItem *conf;
893 dlink_list *list_p = map_to_list(type);
894
895 switch(type)
896 {
897 case CONF_XLINE:
898 case CONF_ULINE:
899 case CONF_NRESV:
900 case CONF_CRESV:
901
902 DLINK_FOREACH(ptr, list_p->head)
903 {
904 conf = ptr->data;
905
906 if (EmptyString(conf->name))
907 continue;
908
909 if (irccmp(conf->name, name) == 0)
910 {
911 if ((user == NULL && (host == NULL)))
912 return conf;
913 if (EmptyString(conf->user) || EmptyString(conf->host))
914 return conf;
915 if (!match(conf->user, user) && !match(conf->host, host))
916 return conf;
917 }
918 }
919 break;
920
921 case CONF_OPER:
922 DLINK_FOREACH(ptr, list_p->head)
923 {
924 conf = ptr->data;
925
926 if (EmptyString(conf->name))
927 continue;
928
929 if (!irccmp(conf->name, name))
930 {
931 if (!who)
932 return conf;
933 if (EmptyString(conf->user) || EmptyString(conf->host))
934 return NULL;
935 if (!match(conf->user, who->username))
936 {
937 switch (conf->htype)
938 {
939 case HM_HOST:
940 if (!match(conf->host, who->host) || !match(conf->host, who->sockhost))
941 if (!conf->class->max_total || conf->class->ref_count < conf->class->max_total)
942 return conf;
943 break;
944 case HM_IPV4:
945 if (who->localClient->aftype == AF_INET)
946 if (match_ipv4(&who->localClient->ip, &conf->addr, conf->bits))
947 if (!conf->class->max_total || conf->class->ref_count < conf->class->max_total)
948 return conf;
949 break;
950 #ifdef IPV6
951 case HM_IPV6:
952 if (who->localClient->aftype == AF_INET6)
953 if (match_ipv6(&who->localClient->ip, &conf->addr, conf->bits))
954 if (!conf->class->max_total || conf->class->ref_count < conf->class->max_total)
955 return conf;
956 break;
957 #endif
958 default:
959 assert(0);
960 }
961 }
962 }
963 }
964
965 break;
966
967 case CONF_SERVER:
968 DLINK_FOREACH(ptr, list_p->head)
969 {
970 conf = ptr->data;
971
972 if (EmptyString(conf->name))
973 continue;
974
975 if (name == NULL)
976 {
977 if (EmptyString(conf->host))
978 continue;
979 if (irccmp(conf->host, host) == 0)
980 return conf;
981 }
982 else if (irccmp(conf->name, name) == 0)
983 return conf;
984 }
985
986 break;
987
988 default:
989 break;
990 }
991
992 return NULL;
993 }
994
995 /* rehash()
996 *
997 * Actual REHASH service routine. Called with sig == 0 if it has been called
998 * as a result of an operator issuing this command, else assume it has been
999 * called as a result of the server receiving a HUP signal.
1000 */
1001 int
1002 rehash(int sig)
1003 {
1004 if (sig)
1005 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1006 "Got signal SIGHUP, reloading configuration file(s)");
1007
1008 restart_resolver();
1009
1010 /* don't close listeners until we know we can go ahead with the rehash */
1011
1012 /* Check to see if we magically got(or lost) IPv6 support */
1013 check_can_use_v6();
1014
1015 read_conf_files(0);
1016
1017 if (ServerInfo.description)
1018 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
1019
1020 load_conf_modules();
1021 check_conf_klines();
1022
1023 return 0;
1024 }
1025
1026 /* set_default_conf()
1027 *
1028 * inputs - NONE
1029 * output - NONE
1030 * side effects - Set default values here.
1031 * This is called **PRIOR** to parsing the
1032 * configuration file. If you want to do some validation
1033 * of values later, put them in validate_conf().
1034 */
1035 static void
1036 set_default_conf(void)
1037 {
1038 /* verify init_class() ran, this should be an unnecessary check
1039 * but its not much work.
1040 */
1041 assert(class_default == class_get_list()->tail->data);
1042
1043 #ifdef HAVE_LIBCRYPTO
1044 ServerInfo.message_digest_algorithm = EVP_sha256();
1045 ServerInfo.rsa_private_key = NULL;
1046 ServerInfo.rsa_private_key_file = NULL;
1047 #endif
1048
1049 /* ServerInfo.name is not rehashable */
1050 /* ServerInfo.name = ServerInfo.name; */
1051 ServerInfo.description = NULL;
1052 ServerInfo.network_name = xstrdup(NETWORK_NAME_DEFAULT);
1053 ServerInfo.network_desc = xstrdup(NETWORK_DESC_DEFAULT);
1054
1055 memset(&ServerInfo.ip, 0, sizeof(ServerInfo.ip));
1056 ServerInfo.specific_ipv4_vhost = 0;
1057 memset(&ServerInfo.ip6, 0, sizeof(ServerInfo.ip6));
1058 ServerInfo.specific_ipv6_vhost = 0;
1059
1060 ServerInfo.max_clients = MAXCLIENTS_MAX;
1061 ServerInfo.max_nick_length = 9;
1062 ServerInfo.max_topic_length = 80;
1063 ServerInfo.hub = 0;
1064 ServerInfo.dns_host.sin_addr.s_addr = 0;
1065 ServerInfo.dns_host.sin_port = 0;
1066
1067 AdminInfo.name = NULL;
1068 AdminInfo.email = NULL;
1069 AdminInfo.description = NULL;
1070
1071 log_del_all();
1072
1073 ConfigLoggingEntry.use_logging = 1;
1074
1075 ConfigChannel.disable_fake_channels = 0;
1076 ConfigChannel.invite_client_count = 10;
1077 ConfigChannel.invite_client_time = 300;
1078 ConfigChannel.knock_client_count = 1;
1079 ConfigChannel.knock_client_time = 300;
1080 ConfigChannel.knock_delay_channel = 60;
1081 ConfigChannel.max_channels = 25;
1082 ConfigChannel.max_bans = 25;
1083 ConfigChannel.default_split_user_count = 0;
1084 ConfigChannel.default_split_server_count = 0;
1085 ConfigChannel.no_join_on_split = 0;
1086 ConfigChannel.no_create_on_split = 0;
1087
1088 ConfigServerHide.flatten_links = 0;
1089 ConfigServerHide.links_delay = 300;
1090 ConfigServerHide.hidden = 0;
1091 ConfigServerHide.hide_servers = 0;
1092 ConfigServerHide.hide_services = 0;
1093 ConfigServerHide.hidden_name = xstrdup(NETWORK_NAME_DEFAULT);
1094 ConfigServerHide.hide_server_ips = 0;
1095 ConfigServerHide.disable_remote_commands = 0;
1096
1097 ConfigFileEntry.away_count = 2;
1098 ConfigFileEntry.away_time = 10;
1099 ConfigFileEntry.service_name = xstrdup(SERVICE_NAME_DEFAULT);
1100 ConfigFileEntry.max_watch = WATCHSIZE_DEFAULT;
1101 ConfigFileEntry.cycle_on_host_change = 1;
1102 ConfigFileEntry.glines = 0;
1103 ConfigFileEntry.gline_time = 12 * 3600;
1104 ConfigFileEntry.gline_request_time = GLINE_REQUEST_EXPIRE_DEFAULT;
1105 ConfigFileEntry.gline_min_cidr = 16;
1106 ConfigFileEntry.gline_min_cidr6 = 48;
1107 ConfigFileEntry.invisible_on_connect = 1;
1108 ConfigFileEntry.tkline_expire_notices = 1;
1109 ConfigFileEntry.hide_spoof_ips = 1;
1110 ConfigFileEntry.ignore_bogus_ts = 0;
1111 ConfigFileEntry.disable_auth = 0;
1112 ConfigFileEntry.kill_chase_time_limit = 90;
1113 ConfigFileEntry.default_floodcount = 8;
1114 ConfigFileEntry.failed_oper_notice = 1;
1115 ConfigFileEntry.dots_in_ident = 0;
1116 ConfigFileEntry.min_nonwildcard = 4;
1117 ConfigFileEntry.min_nonwildcard_simple = 3;
1118 ConfigFileEntry.max_accept = 20;
1119 ConfigFileEntry.anti_nick_flood = 0;
1120 ConfigFileEntry.max_nick_time = 20;
1121 ConfigFileEntry.max_nick_changes = 5;
1122 ConfigFileEntry.anti_spam_exit_message_time = 0;
1123 ConfigFileEntry.ts_warn_delta = TS_WARN_DELTA_DEFAULT;
1124 ConfigFileEntry.ts_max_delta = TS_MAX_DELTA_DEFAULT;
1125 ConfigFileEntry.warn_no_connect_block = 1;
1126 ConfigFileEntry.stats_e_disabled = 0;
1127 ConfigFileEntry.stats_o_oper_only = 0;
1128 ConfigFileEntry.stats_k_oper_only = 1; /* 1 = masked */
1129 ConfigFileEntry.stats_i_oper_only = 1; /* 1 = masked */
1130 ConfigFileEntry.stats_P_oper_only = 0;
1131 ConfigFileEntry.stats_u_oper_only = 0;
1132 ConfigFileEntry.caller_id_wait = 60;
1133 ConfigFileEntry.opers_bypass_callerid = 0;
1134 ConfigFileEntry.pace_wait = 10;
1135 ConfigFileEntry.pace_wait_simple = 1;
1136 ConfigFileEntry.short_motd = 0;
1137 ConfigFileEntry.ping_cookie = 0;
1138 ConfigFileEntry.no_oper_flood = 0;
1139 ConfigFileEntry.true_no_oper_flood = 0;
1140 ConfigFileEntry.oper_pass_resv = 1;
1141 ConfigFileEntry.max_targets = MAX_TARGETS_DEFAULT;
1142 ConfigFileEntry.oper_only_umodes = UMODE_DEBUG;
1143 ConfigFileEntry.oper_umodes = UMODE_BOTS | UMODE_LOCOPS | UMODE_SERVNOTICE | UMODE_WALLOP;
1144 ConfigFileEntry.throttle_count = 1;
1145 ConfigFileEntry.throttle_time = 1;
1146 }
1147
1148 static void
1149 validate_conf(void)
1150 {
1151 if (ConfigFileEntry.ts_warn_delta < TS_WARN_DELTA_MIN)
1152 ConfigFileEntry.ts_warn_delta = TS_WARN_DELTA_DEFAULT;
1153
1154 if (ConfigFileEntry.ts_max_delta < TS_MAX_DELTA_MIN)
1155 ConfigFileEntry.ts_max_delta = TS_MAX_DELTA_DEFAULT;
1156
1157 if (ServerInfo.network_name == NULL)
1158 ServerInfo.network_name = xstrdup(NETWORK_NAME_DEFAULT);
1159
1160 if (ServerInfo.network_desc == NULL)
1161 ServerInfo.network_desc = xstrdup(NETWORK_DESC_DEFAULT);
1162
1163 if (ConfigFileEntry.service_name == NULL)
1164 ConfigFileEntry.service_name = xstrdup(SERVICE_NAME_DEFAULT);
1165
1166 ConfigFileEntry.max_watch = IRCD_MAX(ConfigFileEntry.max_watch, WATCHSIZE_MIN);
1167 }
1168
1169 /* read_conf()
1170 *
1171 * inputs - file descriptor pointing to config file to use
1172 * output - None
1173 * side effects - Read configuration file.
1174 */
1175 static void
1176 read_conf(FILE *file)
1177 {
1178 lineno = 0;
1179
1180 set_default_conf(); /* Set default values prior to conf parsing */
1181 conf_parser_ctx.pass = 1;
1182 yyparse(); /* Pick up the classes first */
1183
1184 rewind(file);
1185
1186 conf_parser_ctx.pass = 2;
1187 yyparse(); /* Load the values from the conf */
1188 validate_conf(); /* Check to make sure some values are still okay. */
1189 /* Some global values are also loaded here. */
1190 class_delete_marked(); /* Delete unused classes that are marked for deletion */
1191 }
1192
1193 /* lookup_confhost()
1194 *
1195 * start DNS lookups of all hostnames in the conf
1196 * line and convert an IP addresses in a.b.c.d number for to IP#s.
1197 */
1198 void
1199 lookup_confhost(struct MaskItem *conf)
1200 {
1201 struct addrinfo hints, *res;
1202
1203 /*
1204 * Do name lookup now on hostnames given and store the
1205 * ip numbers in conf structure.
1206 */
1207 memset(&hints, 0, sizeof(hints));
1208
1209 hints.ai_family = AF_UNSPEC;
1210 hints.ai_socktype = SOCK_STREAM;
1211
1212 /* Get us ready for a bind() and don't bother doing dns lookup */
1213 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
1214
1215 if (getaddrinfo(conf->host, NULL, &hints, &res))
1216 {
1217 conf_dns_lookup(conf);
1218 return;
1219 }
1220
1221 assert(res);
1222
1223 memcpy(&conf->addr, res->ai_addr, res->ai_addrlen);
1224 conf->addr.ss_len = res->ai_addrlen;
1225 conf->addr.ss.ss_family = res->ai_family;
1226
1227 freeaddrinfo(res);
1228 }
1229
1230 /* conf_connect_allowed()
1231 *
1232 * inputs - pointer to inaddr
1233 * - int type ipv4 or ipv6
1234 * output - BANNED or accepted
1235 * side effects - none
1236 */
1237 int
1238 conf_connect_allowed(struct irc_ssaddr *addr, int aftype)
1239 {
1240 struct ip_entry *ip_found = NULL;
1241 struct MaskItem *conf = find_dline_conf(addr, aftype);
1242
1243 /* DLINE exempt also gets you out of static limits/pacing... */
1244 if (conf && (conf->type == CONF_EXEMPT))
1245 return 0;
1246
1247 if (conf)
1248 return BANNED_CLIENT;
1249
1250 ip_found = find_or_add_ip(addr);
1251
1252 if ((CurrentTime - ip_found->last_attempt) < ConfigFileEntry.throttle_time)
1253 {
1254 if (ip_found->connection_count >= ConfigFileEntry.throttle_count)
1255 return TOO_FAST;
1256
1257 ++ip_found->connection_count;
1258 }
1259 else
1260 ip_found->connection_count = 1;
1261
1262 ip_found->last_attempt = CurrentTime;
1263 return 0;
1264 }
1265
1266 /* cleanup_tklines()
1267 *
1268 * inputs - NONE
1269 * output - NONE
1270 * side effects - call function to expire temporary k/d lines
1271 * This is an event started off in ircd.c
1272 */
1273 void
1274 cleanup_tklines(void *notused)
1275 {
1276 hostmask_expire_temporary();
1277 expire_tklines(&xconf_items);
1278 expire_tklines(&nresv_items);
1279 expire_tklines(&cresv_items);
1280 }
1281
1282 /* expire_tklines()
1283 *
1284 * inputs - tkline list pointer
1285 * output - NONE
1286 * side effects - expire tklines
1287 */
1288 static void
1289 expire_tklines(dlink_list *tklist)
1290 {
1291 dlink_node *ptr = NULL, *ptr_next = NULL;
1292 struct MaskItem *conf = NULL;
1293
1294 DLINK_FOREACH_SAFE(ptr, ptr_next, tklist->head)
1295 {
1296 conf = ptr->data;
1297
1298 if (!conf->until || conf->until > CurrentTime)
1299 continue;
1300
1301 if (conf->type == CONF_XLINE)
1302 {
1303 if (ConfigFileEntry.tkline_expire_notices)
1304 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1305 "Temporary X-line for [%s] expired", conf->name);
1306 conf_free(conf);
1307 }
1308 else if (conf->type == CONF_NRESV || conf->type == CONF_CRESV)
1309 {
1310 if (ConfigFileEntry.tkline_expire_notices)
1311 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1312 "Temporary RESV for [%s] expired", conf->name);
1313 conf_free(conf);
1314 }
1315 }
1316 }
1317
1318 /* oper_privs_as_string()
1319 *
1320 * inputs - pointer to client_p
1321 * output - pointer to static string showing oper privs
1322 * side effects - return as string, the oper privs as derived from port
1323 */
1324 static const struct oper_privs
1325 {
1326 const unsigned int flag;
1327 const unsigned char c;
1328 } flag_list[] = {
1329 { OPER_FLAG_ADMIN, 'A' },
1330 { OPER_FLAG_REMOTEBAN, 'B' },
1331 { OPER_FLAG_DIE, 'D' },
1332 { OPER_FLAG_GLINE, 'G' },
1333 { OPER_FLAG_REHASH, 'H' },
1334 { OPER_FLAG_KLINE, 'K' },
1335 { OPER_FLAG_KILL, 'N' },
1336 { OPER_FLAG_KILL_REMOTE, 'O' },
1337 { OPER_FLAG_CONNECT, 'P' },
1338 { OPER_FLAG_CONNECT_REMOTE, 'Q' },
1339 { OPER_FLAG_SQUIT, 'R' },
1340 { OPER_FLAG_SQUIT_REMOTE, 'S' },
1341 { OPER_FLAG_UNKLINE, 'U' },
1342 { OPER_FLAG_XLINE, 'X' },
1343 { 0, '\0' }
1344 };
1345
1346 char *
1347 oper_privs_as_string(const unsigned int port)
1348 {
1349 static char privs_out[IRCD_BUFSIZE];
1350 char *privs_ptr = privs_out;
1351
1352 for (const struct oper_privs *opriv = flag_list; opriv->flag; ++opriv)
1353 {
1354 if (port & opriv->flag)
1355 *privs_ptr++ = opriv->c;
1356 else
1357 *privs_ptr++ = ToLower(opriv->c);
1358 }
1359
1360 *privs_ptr = '\0';
1361
1362 return privs_out;
1363 }
1364
1365 /*
1366 * Input: A client to find the active operator {} name for.
1367 * Output: The nick!user@host{oper} of the oper.
1368 * "oper" is server name for remote opers
1369 * Side effects: None.
1370 */
1371 const char *
1372 get_oper_name(const struct Client *client_p)
1373 {
1374 const dlink_node *cnode = NULL;
1375 /* +5 for !,@,{,} and null */
1376 static char buffer[NICKLEN + USERLEN + HOSTLEN + HOSTLEN + 5];
1377
1378 if (MyConnect(client_p))
1379 {
1380 if ((cnode = client_p->localClient->confs.head))
1381 {
1382 const struct MaskItem *conf = cnode->data;
1383
1384 if (IsConfOperator(conf))
1385 {
1386 snprintf(buffer, sizeof(buffer), "%s!%s@%s{%s}", client_p->name,
1387 client_p->username, client_p->host, conf->name);
1388 return buffer;
1389 }
1390 }
1391
1392 /*
1393 * Probably should assert here for now. If there is an oper out there
1394 * with no operator {} conf attached, it would be good for us to know...
1395 */
1396 assert(0); /* Oper without oper conf! */
1397 }
1398
1399 snprintf(buffer, sizeof(buffer), "%s!%s@%s{%s}", client_p->name,
1400 client_p->username, client_p->host, client_p->servptr->name);
1401 return buffer;
1402 }
1403
1404 /* read_conf_files()
1405 *
1406 * inputs - cold start YES or NO
1407 * output - none
1408 * side effects - read all conf files needed, ircd.conf kline.conf etc.
1409 */
1410 void
1411 read_conf_files(int cold)
1412 {
1413 const char *filename = NULL;
1414 char chanmodes[IRCD_BUFSIZE] = "";
1415 char chanlimit[IRCD_BUFSIZE] = "";
1416
1417 conf_parser_ctx.boot = cold;
1418 filename = ConfigFileEntry.configfile;
1419
1420 /* We need to know the initial filename for the yyerror() to report
1421 FIXME: The full path is in conffilenamebuf first time since we
1422 don't know anything else
1423
1424 - Gozem 2002-07-21
1425 */
1426 strlcpy(conffilebuf, filename, sizeof(conffilebuf));
1427
1428 if ((conf_parser_ctx.conf_file = fopen(filename, "r")) == NULL)
1429 {
1430 if (cold)
1431 {
1432 ilog(LOG_TYPE_IRCD, "Unable to read configuration file '%s': %s",
1433 filename, strerror(errno));
1434 exit(-1);
1435 }
1436 else
1437 {
1438 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1439 "Unable to read configuration file '%s': %s",
1440 filename, strerror(errno));
1441 return;
1442 }
1443 }
1444
1445 if (!cold)
1446 clear_out_old_conf();
1447
1448 read_conf(conf_parser_ctx.conf_file);
1449 fclose(conf_parser_ctx.conf_file);
1450
1451 log_reopen_all();
1452
1453 add_isupport("NICKLEN", NULL, ServerInfo.max_nick_length);
1454 add_isupport("NETWORK", ServerInfo.network_name, -1);
1455
1456 snprintf(chanmodes, sizeof(chanmodes), "beI:%d", ConfigChannel.max_bans);
1457 add_isupport("MAXLIST", chanmodes, -1);
1458 add_isupport("MAXTARGETS", NULL, ConfigFileEntry.max_targets);
1459 add_isupport("CHANTYPES", "#", -1);
1460
1461 snprintf(chanlimit, sizeof(chanlimit), "#:%d",
1462 ConfigChannel.max_channels);
1463 add_isupport("CHANLIMIT", chanlimit, -1);
1464 snprintf(chanmodes, sizeof(chanmodes), "%s", "beI,k,l,cimnprstMORS");
1465 add_isupport("CHANNELLEN", NULL, CHANNELLEN);
1466 add_isupport("TOPICLEN", NULL, ServerInfo.max_topic_length);
1467 add_isupport("CHANMODES", chanmodes, -1);
1468
1469 /*
1470 * message_locale may have changed. rebuild isupport since it relies
1471 * on strlen(form_str(RPL_ISUPPORT))
1472 */
1473 rebuild_isupport_message_line();
1474 }
1475
1476 /* clear_out_old_conf()
1477 *
1478 * inputs - none
1479 * output - none
1480 * side effects - Clear out the old configuration
1481 */
1482 static void
1483 clear_out_old_conf(void)
1484 {
1485 dlink_node *ptr = NULL, *next_ptr = NULL;
1486 struct MaskItem *conf;
1487 dlink_list *free_items [] = {
1488 &server_items, &oconf_items,
1489 &uconf_items, &xconf_items,
1490 &nresv_items, &cluster_items, &service_items, &cresv_items, NULL
1491 };
1492
1493 dlink_list ** iterator = free_items; /* C is dumb */
1494
1495 /* We only need to free anything allocated by yyparse() here.
1496 * Resetting structs, etc, is taken care of by set_default_conf().
1497 */
1498
1499 for (; *iterator != NULL; iterator++)
1500 {
1501 DLINK_FOREACH_SAFE(ptr, next_ptr, (*iterator)->head)
1502 {
1503 conf = ptr->data;
1504
1505 dlinkDelete(&conf->node, map_to_list(conf->type));
1506
1507 /* XXX This is less than pretty */
1508 if (conf->type == CONF_SERVER || conf->type == CONF_OPER)
1509 {
1510 if (!conf->ref_count)
1511 conf_free(conf);
1512 }
1513 else if (conf->type == CONF_XLINE)
1514 {
1515 if (!conf->until)
1516 conf_free(conf);
1517 }
1518 else
1519 conf_free(conf);
1520 }
1521 }
1522
1523 motd_clear();
1524
1525 /*
1526 * don't delete the class table, rather mark all entries
1527 * for deletion. The table is cleaned up by class_delete_marked. - avalon
1528 */
1529 class_mark_for_deletion();
1530
1531 clear_out_address_conf();
1532
1533 /* clean out module paths */
1534 mod_clear_paths();
1535
1536 /* clean out ServerInfo */
1537 MyFree(ServerInfo.description);
1538 ServerInfo.description = NULL;
1539 MyFree(ServerInfo.network_name);
1540 ServerInfo.network_name = NULL;
1541 MyFree(ServerInfo.network_desc);
1542 ServerInfo.network_desc = NULL;
1543 #ifdef HAVE_LIBCRYPTO
1544 if (ServerInfo.rsa_private_key)
1545 {
1546 RSA_free(ServerInfo.rsa_private_key);
1547 ServerInfo.rsa_private_key = NULL;
1548 }
1549
1550 MyFree(ServerInfo.rsa_private_key_file);
1551 ServerInfo.rsa_private_key_file = NULL;
1552 #endif
1553
1554 /* clean out AdminInfo */
1555 MyFree(AdminInfo.name);
1556 AdminInfo.name = NULL;
1557 MyFree(AdminInfo.email);
1558 AdminInfo.email = NULL;
1559 MyFree(AdminInfo.description);
1560 AdminInfo.description = NULL;
1561
1562 /* clean out listeners */
1563 close_listeners();
1564
1565 /* clean out general */
1566 MyFree(ConfigFileEntry.service_name);
1567 ConfigFileEntry.service_name = NULL;
1568 }
1569
1570 /* conf_add_class_to_conf()
1571 *
1572 * inputs - pointer to config item
1573 * output - NONE
1574 * side effects - Add a class pointer to a conf
1575 */
1576 void
1577 conf_add_class_to_conf(struct MaskItem *conf, const char *class_name)
1578 {
1579 if (class_name == NULL)
1580 {
1581 conf->class = class_default;
1582
1583 if (conf->type == CONF_CLIENT)
1584 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1585 "Warning *** Defaulting to default class for %s@%s",
1586 conf->user, conf->host);
1587 else
1588 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1589 "Warning *** Defaulting to default class for %s",
1590 conf->name);
1591 }
1592 else
1593 conf->class = class_find(class_name, 1);
1594
1595 if (conf->class == NULL)
1596 {
1597 if (conf->type == CONF_CLIENT)
1598 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1599 "Warning *** Defaulting to default class for %s@%s",
1600 conf->user, conf->host);
1601 else
1602 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1603 "Warning *** Defaulting to default class for %s",
1604 conf->name);
1605 conf->class = class_default;
1606 }
1607 }
1608
1609 /* yyerror()
1610 *
1611 * inputs - message from parser
1612 * output - NONE
1613 * side effects - message to opers and log file entry is made
1614 */
1615 void
1616 yyerror(const char *msg)
1617 {
1618 char newlinebuf[IRCD_BUFSIZE];
1619
1620 if (conf_parser_ctx.pass != 1)
1621 return;
1622
1623 strip_tabs(newlinebuf, linebuf, sizeof(newlinebuf));
1624 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1625 "\"%s\", line %u: %s: %s",
1626 conffilebuf, lineno + 1, msg, newlinebuf);
1627 ilog(LOG_TYPE_IRCD, "\"%s\", line %u: %s: %s",
1628 conffilebuf, lineno + 1, msg, newlinebuf);
1629 }
1630
1631 void
1632 conf_error_report(const char *msg)
1633 {
1634 char newlinebuf[IRCD_BUFSIZE];
1635
1636 strip_tabs(newlinebuf, linebuf, sizeof(newlinebuf));
1637 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1638 "\"%s\", line %u: %s: %s",
1639 conffilebuf, lineno + 1, msg, newlinebuf);
1640 ilog(LOG_TYPE_IRCD, "\"%s\", line %u: %s: %s",
1641 conffilebuf, lineno + 1, msg, newlinebuf);
1642 }
1643
1644 /*
1645 * valid_tkline()
1646 *
1647 * inputs - pointer to ascii string to check
1648 * - whether the specified time is in seconds or minutes
1649 * output - -1 not enough parameters
1650 * - 0 if not an integer number, else the number
1651 * side effects - none
1652 * Originally written by Dianora (Diane, db@db.net)
1653 */
1654 time_t
1655 valid_tkline(const char *data, const int minutes)
1656 {
1657 const unsigned char *p = (const unsigned char *)data;
1658 unsigned char tmpch = '\0';
1659 time_t result = 0;
1660
1661 while ((tmpch = *p++))
1662 {
1663 if (!IsDigit(tmpch))
1664 return 0;
1665
1666 result *= 10;
1667 result += (tmpch & 0xF);
1668 }
1669
1670 /*
1671 * In the degenerate case where oper does a /quote kline 0 user@host :reason
1672 * i.e. they specifically use 0, I am going to return 1 instead
1673 * as a return value of non-zero is used to flag it as a temporary kline
1674 */
1675 if (result == 0)
1676 result = 1;
1677
1678 /*
1679 * If the incoming time is in seconds convert it to minutes for the purpose
1680 * of this calculation
1681 */
1682 if (!minutes)
1683 result = result / 60;
1684
1685 if (result > MAX_TDKLINE_TIME)
1686 result = MAX_TDKLINE_TIME;
1687
1688 result = result * 60; /* turn it into seconds */
1689
1690 return result;
1691 }
1692
1693 /* valid_wild_card_simple()
1694 *
1695 * inputs - data to check for sufficient non-wildcard characters
1696 * outputs - 1 if valid, else 0
1697 * side effects - none
1698 */
1699 int
1700 valid_wild_card_simple(const char *data)
1701 {
1702 const unsigned char *p = (const unsigned char *)data;
1703 unsigned char tmpch = '\0';
1704 unsigned int nonwild = 0;
1705
1706 while ((tmpch = *p++))
1707 {
1708 if (tmpch == '\\' && *p)
1709 {
1710 ++p;
1711 if (++nonwild >= ConfigFileEntry.min_nonwildcard_simple)
1712 return 1;
1713 }
1714 else if (!IsMWildChar(tmpch))
1715 {
1716 if (++nonwild >= ConfigFileEntry.min_nonwildcard_simple)
1717 return 1;
1718 }
1719 }
1720
1721 return 0;
1722 }
1723
1724 /* valid_wild_card()
1725 *
1726 * input - pointer to client
1727 * - int flag, 0 for no warning oper 1 for warning oper
1728 * - count of following varargs to check
1729 * output - 0 if not valid, 1 if valid
1730 * side effects - NOTICE is given to source_p if warn is 1
1731 */
1732 int
1733 valid_wild_card(struct Client *source_p, int warn, int count, ...)
1734 {
1735 unsigned char tmpch = '\0';
1736 unsigned int nonwild = 0;
1737 va_list args;
1738
1739 /*
1740 * Now we must check the user and host to make sure there
1741 * are at least NONWILDCHARS non-wildcard characters in
1742 * them, otherwise assume they are attempting to kline
1743 * *@* or some variant of that. This code will also catch
1744 * people attempting to kline *@*.tld, as long as NONWILDCHARS
1745 * is greater than 3. In that case, there are only 3 non-wild
1746 * characters (tld), so if NONWILDCHARS is 4, the kline will
1747 * be disallowed.
1748 * -wnder
1749 */
1750
1751 va_start(args, count);
1752
1753 while (count--)
1754 {
1755 const unsigned char *p = va_arg(args, const unsigned char *);
1756 if (p == NULL)
1757 continue;
1758
1759 while ((tmpch = *p++))
1760 {
1761 if (!IsKWildChar(tmpch))
1762 {
1763 /*
1764 * If we find enough non-wild characters, we can
1765 * break - no point in searching further.
1766 */
1767 if (++nonwild >= ConfigFileEntry.min_nonwildcard)
1768 {
1769 va_end(args);
1770 return 1;
1771 }
1772 }
1773 }
1774 }
1775
1776 if (warn)
1777 sendto_one_notice(source_p, &me,
1778 ":Please include at least %u non-wildcard characters with the mask",
1779 ConfigFileEntry.min_nonwildcard);
1780 va_end(args);
1781 return 0;
1782 }
1783
1784 /* XXX should this go into a separate file ? -Dianora */
1785 /* parse_aline
1786 *
1787 * input - pointer to cmd name being used
1788 * - pointer to client using cmd
1789 * - parc parameter count
1790 * - parv[] list of parameters to parse
1791 * - parse_flags bit map of things to test
1792 * - pointer to user or string to parse into
1793 * - pointer to host or NULL to parse into if non NULL
1794 * - pointer to optional tkline time or NULL
1795 * - pointer to target_server to parse into if non NULL
1796 * - pointer to reason to parse into
1797 *
1798 * output - 1 if valid, -1 if not valid
1799 * side effects - A generalised k/d/x etc. line parser,
1800 * "ALINE [time] user@host|string [ON] target :reason"
1801 * will parse returning a parsed user, host if
1802 * h_p pointer is non NULL, string otherwise.
1803 * if tkline_time pointer is non NULL a tk line will be set
1804 * to non zero if found.
1805 * if tkline_time pointer is NULL and tk line is found,
1806 * error is reported.
1807 * if target_server is NULL and an "ON" is found error
1808 * is reported.
1809 * if reason pointer is NULL ignore pointer,
1810 * this allows use of parse_a_line in unkline etc.
1811 *
1812 * - Dianora
1813 */
1814 int
1815 parse_aline(const char *cmd, struct Client *source_p,
1816 int parc, char **parv,
1817 int parse_flags, char **up_p, char **h_p, time_t *tkline_time,
1818 char **target_server, char **reason)
1819 {
1820 int found_tkline_time=0;
1821 static char def_reason[] = CONF_NOREASON;
1822 static char user[USERLEN*4+1];
1823 static char host[HOSTLEN*4+1];
1824
1825 parv++;
1826 parc--;
1827
1828 found_tkline_time = valid_tkline(*parv, TK_MINUTES);
1829
1830 if (found_tkline_time != 0)
1831 {
1832 parv++;
1833 parc--;
1834
1835 if (tkline_time != NULL)
1836 *tkline_time = found_tkline_time;
1837 else
1838 {
1839 sendto_one_notice(source_p, &me, ":temp_line not supported by %s", cmd);
1840 return -1;
1841 }
1842 }
1843
1844 if (parc == 0)
1845 {
1846 sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, cmd);
1847 return -1;
1848 }
1849
1850 if (h_p == NULL)
1851 *up_p = *parv;
1852 else
1853 {
1854 if (find_user_host(source_p, *parv, user, host, parse_flags) == 0)
1855 return -1;
1856
1857 *up_p = user;
1858 *h_p = host;
1859 }
1860
1861 parc--;
1862 parv++;
1863
1864 if (parc != 0)
1865 {
1866 if (irccmp(*parv, "ON") == 0)
1867 {
1868 parc--;
1869 parv++;
1870
1871 if (target_server == NULL)
1872 {
1873 sendto_one_notice(source_p, &me, ":ON server not supported by %s", cmd);
1874 return -1;
1875 }
1876
1877 if (!HasOFlag(source_p, OPER_FLAG_REMOTEBAN))
1878 {
1879 sendto_one_numeric(source_p, &me, ERR_NOPRIVS, "remoteban");
1880 return -1;
1881 }
1882
1883 if (parc == 0 || EmptyString(*parv))
1884 {
1885 sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, cmd);
1886 return -1;
1887 }
1888
1889 *target_server = *parv;
1890 parc--;
1891 parv++;
1892 }
1893 else
1894 {
1895 /* Make sure target_server *is* NULL if no ON server found
1896 * caller probably NULL'd it first, but no harm to do it again -db
1897 */
1898 if (target_server != NULL)
1899 *target_server = NULL;
1900 }
1901 }
1902
1903 if (h_p != NULL)
1904 {
1905 if (strchr(user, '!') != NULL)
1906 {
1907 sendto_one_notice(source_p, &me, ":Invalid character '!' in kline");
1908 return -1;
1909 }
1910
1911 if ((parse_flags & AWILD) && !valid_wild_card(source_p, 1, 2, *up_p, *h_p))
1912 return -1;
1913 }
1914 else
1915 if ((parse_flags & AWILD) && !valid_wild_card(source_p, 1, 1, *up_p))
1916 return -1;
1917
1918 if (reason != NULL)
1919 {
1920 if (parc != 0 && !EmptyString(*parv))
1921 {
1922 *reason = *parv;
1923 if (!valid_comment(source_p, *reason, 1))
1924 return -1;
1925 }
1926 else
1927 *reason = def_reason;
1928 }
1929
1930 return 1;
1931 }
1932
1933 /* find_user_host()
1934 *
1935 * inputs - pointer to client placing kline
1936 * - pointer to user_host_or_nick
1937 * - pointer to user buffer
1938 * - pointer to host buffer
1939 * output - 0 if not ok to kline, 1 to kline i.e. if valid user host
1940 * side effects -
1941 */
1942 static int
1943 find_user_host(struct Client *source_p, char *user_host_or_nick,
1944 char *luser, char *lhost, unsigned int flags)
1945 {
1946 struct Client *target_p = NULL;
1947 char *hostp = NULL;
1948
1949 if (lhost == NULL)
1950 {
1951 strlcpy(luser, user_host_or_nick, USERLEN*4 + 1);
1952 return 1;
1953 }
1954
1955 if ((hostp = strchr(user_host_or_nick, '@')) || *user_host_or_nick == '*')
1956 {
1957 /* Explicit user@host mask given */
1958
1959 if (hostp != NULL) /* I'm a little user@host */
1960 {
1961 *(hostp++) = '\0'; /* short and squat */
1962 if (*user_host_or_nick)
1963 strlcpy(luser, user_host_or_nick, USERLEN*4 + 1); /* here is my user */
1964 else
1965 strcpy(luser, "*");
1966
1967 if (*hostp)
1968 strlcpy(lhost, hostp, HOSTLEN + 1); /* here is my host */
1969 else
1970 strcpy(lhost, "*");
1971 }
1972 else
1973 {
1974 luser[0] = '*'; /* no @ found, assume its *@somehost */
1975 luser[1] = '\0';
1976 strlcpy(lhost, user_host_or_nick, HOSTLEN*4 + 1);
1977 }
1978
1979 return 1;
1980 }
1981 else
1982 {
1983 /* Try to find user@host mask from nick */
1984 /* Okay to use source_p as the first param, because source_p == client_p */
1985 if ((target_p =
1986 find_chasing(source_p, user_host_or_nick)) == NULL)
1987 return 0; /* find_chasing sends ERR_NOSUCHNICK */
1988
1989 if (IsExemptKline(target_p))
1990 {
1991 if (!IsServer(source_p))
1992 sendto_one_notice(source_p, &me, ":%s is E-lined", target_p->name);
1993 return 0;
1994 }
1995
1996 /*
1997 * turn the "user" bit into "*user", blow away '~'
1998 * if found in original user name (non-idented)
1999 */
2000 strlcpy(luser, target_p->username, USERLEN*4 + 1);
2001
2002 if (target_p->username[0] == '~')
2003 luser[0] = '*';
2004
2005 if (target_p->sockhost[0] == '\0' ||
2006 (target_p->sockhost[0] == '0' && target_p->sockhost[1] == '\0'))
2007 strlcpy(lhost, target_p->host, HOSTLEN*4 + 1);
2008 else
2009 strlcpy(lhost, target_p->sockhost, HOSTLEN*4 + 1);
2010 return 1;
2011 }
2012
2013 return 0;
2014 }
2015
2016 /* valid_comment()
2017 *
2018 * inputs - pointer to client
2019 * - pointer to comment
2020 * output - 0 if no valid comment,
2021 * - 1 if valid
2022 * side effects - truncates reason where necessary
2023 */
2024 int
2025 valid_comment(struct Client *source_p, char *comment, int warn)
2026 {
2027 if (strlen(comment) > REASONLEN)
2028 comment[REASONLEN-1] = '\0';
2029
2030 return 1;
2031 }
2032
2033 /* match_conf_password()
2034 *
2035 * inputs - pointer to given password
2036 * - pointer to Conf
2037 * output - 1 or 0 if match
2038 * side effects - none
2039 */
2040 int
2041 match_conf_password(const char *password, const struct MaskItem *conf)
2042 {
2043 const char *encr = NULL;
2044
2045 if (EmptyString(password) || EmptyString(conf->passwd))
2046 return 0;
2047
2048 if (conf->flags & CONF_FLAGS_ENCRYPTED)
2049 encr = crypt(password, conf->passwd);
2050 else
2051 encr = password;
2052
2053 return encr && !strcmp(encr, conf->passwd);
2054 }
2055
2056 /*
2057 * cluster_a_line
2058 *
2059 * inputs - client sending the cluster
2060 * - command name "KLINE" "XLINE" etc.
2061 * - capab -- CAP_KLN etc. from server.h
2062 * - cluster type -- CLUSTER_KLINE etc. from conf.h
2063 * - pattern and args to send along
2064 * output - none
2065 * side effects - Take source_p send the pattern with args given
2066 * along to all servers that match capab and cluster type
2067 */
2068 void
2069 cluster_a_line(struct Client *source_p, const char *command,
2070 int capab, int cluster_type, const char *pattern, ...)
2071 {
2072 va_list args;
2073 char buffer[IRCD_BUFSIZE] = "";
2074 const dlink_node *ptr = NULL;
2075
2076 va_start(args, pattern);
2077 vsnprintf(buffer, sizeof(buffer), pattern, args);
2078 va_end(args);
2079
2080 DLINK_FOREACH(ptr, cluster_items.head)
2081 {
2082 const struct MaskItem *conf = ptr->data;
2083
2084 if (conf->flags & cluster_type)
2085 sendto_match_servs(source_p, conf->name, CAP_CLUSTER|capab,
2086 "%s %s %s", command, conf->name, buffer);
2087 }
2088 }
2089
2090 /*
2091 * split_nuh
2092 *
2093 * inputs - pointer to original mask (modified in place)
2094 * - pointer to pointer where nick should go
2095 * - pointer to pointer where user should go
2096 * - pointer to pointer where host should go
2097 * output - NONE
2098 * side effects - mask is modified in place
2099 * If nick pointer is NULL, ignore writing to it
2100 * this allows us to use this function elsewhere.
2101 *
2102 * mask nick user host
2103 * ---------------------- ------- ------- ------
2104 * Dianora!db@db.net Dianora db db.net
2105 * Dianora Dianora * *
2106 * db.net * * db.net
2107 * OR if nick pointer is NULL
2108 * Dianora - * Dianora
2109 * Dianora! Dianora * *
2110 * Dianora!@ Dianora * *
2111 * Dianora!db Dianora db *
2112 * Dianora!@db.net Dianora * db.net
2113 * db@db.net * db db.net
2114 * !@ * * *
2115 * @ * * *
2116 * ! * * *
2117 */
2118 void
2119 split_nuh(struct split_nuh_item *const iptr)
2120 {
2121 char *p = NULL, *q = NULL;
2122
2123 if (iptr->nickptr)
2124 strlcpy(iptr->nickptr, "*", iptr->nicksize);
2125 if (iptr->userptr)
2126 strlcpy(iptr->userptr, "*", iptr->usersize);
2127 if (iptr->hostptr)
2128 strlcpy(iptr->hostptr, "*", iptr->hostsize);
2129
2130 if ((p = strchr(iptr->nuhmask, '!')))
2131 {
2132 *p = '\0';
2133
2134 if (iptr->nickptr && *iptr->nuhmask)
2135 strlcpy(iptr->nickptr, iptr->nuhmask, iptr->nicksize);
2136
2137 if ((q = strchr(++p, '@')))
2138 {
2139 *q++ = '\0';
2140
2141 if (*p)
2142 strlcpy(iptr->userptr, p, iptr->usersize);
2143
2144 if (*q)
2145 strlcpy(iptr->hostptr, q, iptr->hostsize);
2146 }
2147 else
2148 {
2149 if (*p)
2150 strlcpy(iptr->userptr, p, iptr->usersize);
2151 }
2152 }
2153 else
2154 {
2155 /* No ! found so lets look for a user@host */
2156 if ((p = strchr(iptr->nuhmask, '@')))
2157 {
2158 /* if found a @ */
2159 *p++ = '\0';
2160
2161 if (*iptr->nuhmask)
2162 strlcpy(iptr->userptr, iptr->nuhmask, iptr->usersize);
2163
2164 if (*p)
2165 strlcpy(iptr->hostptr, p, iptr->hostsize);
2166 }
2167 else
2168 {
2169 /* no @ found */
2170 if (!iptr->nickptr || strpbrk(iptr->nuhmask, ".:"))
2171 strlcpy(iptr->hostptr, iptr->nuhmask, iptr->hostsize);
2172 else
2173 strlcpy(iptr->nickptr, iptr->nuhmask, iptr->nicksize);
2174 }
2175 }
2176 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision