/[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 1652 - (show annotations)
Tue Nov 13 20:28:53 2012 UTC (7 years, 7 months ago) by michael
File MIME type: text/x-chdr
File size: 64345 byte(s)
- changed match() polarity. match() now returns 0 on match and 1 on non-match
  This cleans up several places where function pointers of different matching
  functions like irccmp/strcmp/match are passed to other functions.
- added improved collapse() to match.c

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

Properties

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

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