/[svn]/ircd-hybrid/trunk/src/conf.c
ViewVC logotype

Contents of /ircd-hybrid/trunk/src/conf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1647 - (show annotations)
Fri Nov 9 20:11:58 2012 UTC (8 years ago) by michael
File MIME type: text/x-chdr
File size: 64379 byte(s)
- Finish stabilizing/cleanup of conf parser

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

Properties

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

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28