ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/conf.c
Revision: 1644
Committed: Tue Nov 6 22:20:16 2012 UTC (12 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 65525 byte(s)
Log Message:
- More config subsystem cleanups

File Contents

# Content
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 struct MaskItem *conf = NULL;
892
893 DLINK_FOREACH_SAFE(ptr, next_ptr, client_p->localClient->confs.head)
894 {
895 conf = ptr->data;
896
897 if (conf->type & type)
898 {
899 dlinkDelete(ptr, &client_p->localClient->confs);
900 free_dlink_node(ptr);
901
902 switch (conf->type)
903 {
904 case CONF_CLIENT:
905 case CONF_OPER:
906 case CONF_SERVER:
907 assert(conf->ref_count > 0);
908 assert(conf->class->ref_count > 0);
909
910 if (conf->type == CONF_CLIENT)
911 remove_from_cidr_check(&client_p->localClient->ip, conf->class);
912 if (--conf->class->ref_count == 0 && conf->class->active == 0)
913 {
914 class_free(conf->class);
915 conf->class = NULL;
916 }
917
918 if (--conf->ref_count == 0 && conf->active == 0)
919 conf_free(conf);
920
921 break;
922 default:
923 break;
924 }
925 }
926 }
927 }
928
929 /* attach_conf()
930 *
931 * inputs - client pointer
932 * - conf pointer
933 * output -
934 * side effects - Associate a specific configuration entry to a *local*
935 * client (this is the one which used in accepting the
936 * connection). Note, that this automatically changes the
937 * attachment if there was an old one...
938 */
939 int
940 attach_conf(struct Client *client_p, struct MaskItem *conf)
941 {
942 if (dlinkFind(&client_p->localClient->confs, conf) != NULL)
943 return 1;
944
945 if (conf->type == CONF_CLIENT)
946 if (cidr_limit_reached(IsConfExemptLimits(conf),
947 &client_p->localClient->ip, conf->class))
948 return TOO_MANY; /* Already at maximum allowed */
949
950 conf->class->ref_count++;
951 conf->ref_count++;
952
953 dlinkAdd(conf, make_dlink_node(), &client_p->localClient->confs);
954
955 return 0;
956 }
957
958 /* attach_connect_block()
959 *
960 * inputs - pointer to server to attach
961 * - name of server
962 * - hostname of server
963 * output - true (1) if both are found, otherwise return false (0)
964 * side effects - find connect block and attach them to connecting client
965 */
966 int
967 attach_connect_block(struct Client *client_p, const char *name,
968 const char *host)
969 {
970 dlink_node *ptr;
971 struct MaskItem *conf = NULL;
972
973 assert(client_p != NULL);
974 assert(host != NULL);
975
976 if (client_p == NULL || host == NULL)
977 return 0;
978
979 DLINK_FOREACH(ptr, server_items.head)
980 {
981 conf = ptr->data;
982
983 if (match(conf->name, name) == 0 || match(conf->host, host) == 0)
984 continue;
985
986 attach_conf(client_p, conf);
987 return -1;
988 }
989
990 return 0;
991 }
992
993 /* find_conf_name()
994 *
995 * inputs - pointer to conf link list to search
996 * - pointer to name to find
997 * - int mask of type of conf to find
998 * output - NULL or pointer to conf found
999 * side effects - find a conf entry which matches the name
1000 * and has the given mask.
1001 */
1002 struct MaskItem *
1003 find_conf_name(dlink_list *list, const char *name, enum maskitem_type type)
1004 {
1005 dlink_node *ptr;
1006 struct MaskItem* conf;
1007
1008 DLINK_FOREACH(ptr, list->head)
1009 {
1010 conf = ptr->data;
1011
1012 if (conf->type == type)
1013 {
1014 if (conf->name && (irccmp(conf->name, name) == 0 ||
1015 match(conf->name, name)))
1016 return conf;
1017 }
1018 }
1019
1020 return NULL;
1021 }
1022
1023 /* map_to_list()
1024 *
1025 * inputs - ConfType conf
1026 * output - pointer to dlink_list to use
1027 * side effects - none
1028 */
1029 static dlink_list *
1030 map_to_list(enum maskitem_type type)
1031 {
1032 switch(type)
1033 {
1034 case CONF_RKLINE:
1035 return(&rkconf_items);
1036 break;
1037 case CONF_RXLINE:
1038 return(&rxconf_items);
1039 break;
1040 case CONF_XLINE:
1041 return(&xconf_items);
1042 break;
1043 case CONF_ULINE:
1044 return(&uconf_items);
1045 break;
1046 case CONF_NRESV:
1047 return(&nresv_items);
1048 break;
1049 case CONF_OPER:
1050 return(&oconf_items);
1051 break;
1052 case CONF_SERVER:
1053 return(&server_items);
1054 break;
1055 case CONF_SERVICE:
1056 return(&service_items);
1057 break;
1058 case CONF_CLUSTER:
1059 return(&cluster_items);
1060 break;
1061 default:
1062 return NULL;
1063 }
1064 }
1065
1066 /* find_matching_name_conf()
1067 *
1068 * inputs - type of link list to look in
1069 * - pointer to name string to find
1070 * - pointer to user
1071 * - pointer to host
1072 * - optional flags to match on as well
1073 * output - NULL or pointer to found struct MaskItem
1074 * side effects - looks for a match on name field
1075 */
1076 struct MaskItem *
1077 find_matching_name_conf(enum maskitem_type type, const char *name, const char *user,
1078 const char *host, unsigned int flags)
1079 {
1080 dlink_node *ptr=NULL;
1081 struct MaskItem *conf=NULL;
1082 dlink_list *list_p = map_to_list(type);
1083
1084 switch (type)
1085 {
1086 #ifdef HAVE_LIBPCRE
1087 case CONF_RXLINE:
1088 DLINK_FOREACH(ptr, list_p->head)
1089 {
1090 conf = ptr->data;
1091 assert(conf->regexpname);
1092
1093 if (!ircd_pcre_exec(conf->regexuser, name))
1094 return conf;
1095 }
1096 break;
1097 #endif
1098 case CONF_SERVICE:
1099 DLINK_FOREACH(ptr, list_p->head)
1100 {
1101 conf = ptr->data;
1102
1103 if (EmptyString(conf->name))
1104 continue;
1105 if ((name != NULL) && !irccmp(name, conf->name))
1106 return conf;
1107 }
1108 break;
1109
1110 case CONF_XLINE:
1111 case CONF_ULINE:
1112 case CONF_NRESV:
1113 DLINK_FOREACH(ptr, list_p->head)
1114 {
1115 conf = ptr->data;
1116
1117 if (EmptyString(conf->name))
1118 continue;
1119 if ((name != NULL) && match_esc(conf->name, name))
1120 {
1121 if ((user == NULL && (host == NULL)))
1122 return conf;
1123 if ((conf->flags & flags) != flags)
1124 continue;
1125 if (EmptyString(conf->user) || EmptyString(conf->host))
1126 return conf;
1127 if (match(conf->user, user) && match(conf->host, host))
1128 return conf;
1129 }
1130 }
1131 break;
1132
1133 case CONF_SERVER:
1134 DLINK_FOREACH(ptr, list_p->head)
1135 {
1136 conf = ptr->data;
1137
1138 if ((name != NULL) && match(name, conf->name))
1139 return conf;
1140 else if ((host != NULL) && match(host, conf->host))
1141 return conf;
1142 }
1143 break;
1144
1145 default:
1146 break;
1147 }
1148 return NULL;
1149 }
1150
1151 /* find_exact_name_conf()
1152 *
1153 * inputs - type of link list to look in
1154 * - pointer to name string to find
1155 * - pointer to user
1156 * - pointer to host
1157 * output - NULL or pointer to found struct MaskItem
1158 * side effects - looks for an exact match on name field
1159 */
1160 struct MaskItem *
1161 find_exact_name_conf(enum maskitem_type type, const struct Client *who, const char *name,
1162 const char *user, const char *host)
1163 {
1164 dlink_node *ptr = NULL;
1165 struct MaskItem *conf;
1166 dlink_list *list_p = map_to_list(type);
1167
1168 switch(type)
1169 {
1170 case CONF_RXLINE:
1171 case CONF_XLINE:
1172 case CONF_ULINE:
1173 case CONF_NRESV:
1174
1175 DLINK_FOREACH(ptr, list_p->head)
1176 {
1177 conf = ptr->data;
1178
1179 if (EmptyString(conf->name))
1180 continue;
1181
1182 if (irccmp(conf->name, name) == 0)
1183 {
1184 if ((user == NULL && (host == NULL)))
1185 return (conf);
1186 if (EmptyString(conf->user) || EmptyString(conf->host))
1187 return (conf);
1188 if (match(conf->user, user) && match(conf->host, host))
1189 return (conf);
1190 }
1191 }
1192 break;
1193
1194 case CONF_OPER:
1195 DLINK_FOREACH(ptr, list_p->head)
1196 {
1197 conf = ptr->data;
1198
1199 if (EmptyString(conf->name))
1200 continue;
1201
1202 if (!irccmp(conf->name, name))
1203 {
1204 if (!who)
1205 return conf;
1206 if (EmptyString(conf->user) || EmptyString(conf->host))
1207 return NULL;
1208 if (match(conf->user, who->username))
1209 {
1210 switch (conf->htype)
1211 {
1212 case HM_HOST:
1213 if (match(conf->host, who->host) || match(conf->host, who->sockhost))
1214 if (!conf->class->max_total || conf->class->ref_count < conf->class->max_total)
1215 return conf;
1216 break;
1217 case HM_IPV4:
1218 if (who->localClient->aftype == AF_INET)
1219 if (match_ipv4(&who->localClient->ip, &conf->addr, conf->bits))
1220 if (!conf->class->max_total || conf->class->ref_count < conf->class->max_total)
1221 return conf;
1222 break;
1223 #ifdef IPV6
1224 case HM_IPV6:
1225 if (who->localClient->aftype == AF_INET6)
1226 if (match_ipv6(&who->localClient->ip, &conf->addr, conf->bits))
1227 if (!conf->class->max_total || conf->class->ref_count < conf->class->max_total)
1228 return conf;
1229 break;
1230 #endif
1231 default:
1232 assert(0);
1233 }
1234 }
1235 }
1236 }
1237
1238 break;
1239
1240 case CONF_SERVER:
1241 DLINK_FOREACH(ptr, list_p->head)
1242 {
1243 conf = ptr->data;
1244
1245 if (EmptyString(conf->name))
1246 continue;
1247
1248 if (name == NULL)
1249 {
1250 if (EmptyString(conf->host))
1251 continue;
1252 if (irccmp(conf->host, host) == 0)
1253 return(conf);
1254 }
1255 else if (irccmp(conf->name, name) == 0)
1256 {
1257 return (conf);
1258 }
1259 }
1260 break;
1261
1262 default:
1263 break;
1264 }
1265 return(NULL);
1266 }
1267
1268 /* rehash()
1269 *
1270 * Actual REHASH service routine. Called with sig == 0 if it has been called
1271 * as a result of an operator issuing this command, else assume it has been
1272 * called as a result of the server receiving a HUP signal.
1273 */
1274 int
1275 rehash(int sig)
1276 {
1277 if (sig != 0)
1278 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1279 "Got signal SIGHUP, reloading ircd.conf file");
1280
1281 restart_resolver();
1282
1283 /* don't close listeners until we know we can go ahead with the rehash */
1284
1285 /* Check to see if we magically got(or lost) IPv6 support */
1286 check_can_use_v6();
1287
1288 read_conf_files(0);
1289
1290 if (ServerInfo.description != NULL)
1291 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
1292
1293 load_conf_modules();
1294
1295 rehashed_klines = 1;
1296 /* XXX */
1297 if (ConfigLoggingEntry.use_logging)
1298 log_close_all();
1299
1300 return(0);
1301 }
1302
1303 /* set_default_conf()
1304 *
1305 * inputs - NONE
1306 * output - NONE
1307 * side effects - Set default values here.
1308 * This is called **PRIOR** to parsing the
1309 * configuration file. If you want to do some validation
1310 * of values later, put them in validate_conf().
1311 */
1312 static void
1313 set_default_conf(void)
1314 {
1315 /* verify init_class() ran, this should be an unnecessary check
1316 * but its not much work.
1317 */
1318 assert(class_default == class_get_list()->tail->data);
1319
1320 #ifdef HAVE_LIBCRYPTO
1321 ServerInfo.rsa_private_key = NULL;
1322 ServerInfo.rsa_private_key_file = NULL;
1323 #endif
1324
1325 /* ServerInfo.name is not rehashable */
1326 /* ServerInfo.name = ServerInfo.name; */
1327 ServerInfo.description = NULL;
1328 DupString(ServerInfo.network_name, NETWORK_NAME_DEFAULT);
1329 DupString(ServerInfo.network_desc, NETWORK_DESC_DEFAULT);
1330
1331 memset(&ServerInfo.ip, 0, sizeof(ServerInfo.ip));
1332 ServerInfo.specific_ipv4_vhost = 0;
1333 memset(&ServerInfo.ip6, 0, sizeof(ServerInfo.ip6));
1334 ServerInfo.specific_ipv6_vhost = 0;
1335
1336 ServerInfo.max_clients = MAXCLIENTS_MAX;
1337
1338 ServerInfo.hub = 0;
1339 ServerInfo.dns_host.sin_addr.s_addr = 0;
1340 ServerInfo.dns_host.sin_port = 0;
1341 AdminInfo.name = NULL;
1342 AdminInfo.email = NULL;
1343 AdminInfo.description = NULL;
1344
1345 log_close_all();
1346
1347 ConfigLoggingEntry.use_logging = 1;
1348
1349 ConfigChannel.disable_fake_channels = 0;
1350 ConfigChannel.restrict_channels = 0;
1351 ConfigChannel.knock_delay = 300;
1352 ConfigChannel.knock_delay_channel = 60;
1353 ConfigChannel.max_chans_per_user = 25;
1354 ConfigChannel.max_chans_per_oper = 50;
1355 ConfigChannel.quiet_on_ban = 1;
1356 ConfigChannel.max_bans = 25;
1357 ConfigChannel.default_split_user_count = 0;
1358 ConfigChannel.default_split_server_count = 0;
1359 ConfigChannel.no_join_on_split = 0;
1360 ConfigChannel.no_create_on_split = 0;
1361
1362 ConfigServerHide.flatten_links = 0;
1363 ConfigServerHide.links_delay = 300;
1364 ConfigServerHide.hidden = 0;
1365 ConfigServerHide.hide_servers = 0;
1366 DupString(ConfigServerHide.hidden_name, NETWORK_NAME_DEFAULT);
1367 ConfigServerHide.hide_server_ips = 0;
1368
1369
1370 DupString(ConfigFileEntry.service_name, SERVICE_NAME_DEFAULT);
1371 ConfigFileEntry.max_watch = WATCHSIZE_DEFAULT;
1372 ConfigFileEntry.glines = 0;
1373 ConfigFileEntry.gline_time = 12 * 3600;
1374 ConfigFileEntry.gline_request_time = GLINE_REQUEST_EXPIRE_DEFAULT;
1375 ConfigFileEntry.gline_min_cidr = 16;
1376 ConfigFileEntry.gline_min_cidr6 = 48;
1377 ConfigFileEntry.invisible_on_connect = 1;
1378 ConfigFileEntry.tkline_expire_notices = 1;
1379 ConfigFileEntry.hide_spoof_ips = 1;
1380 ConfigFileEntry.ignore_bogus_ts = 0;
1381 ConfigFileEntry.disable_auth = 0;
1382 ConfigFileEntry.disable_remote = 0;
1383 ConfigFileEntry.kill_chase_time_limit = 90;
1384 ConfigFileEntry.default_floodcount = 8;
1385 ConfigFileEntry.failed_oper_notice = 1;
1386 ConfigFileEntry.dots_in_ident = 0;
1387 ConfigFileEntry.min_nonwildcard = 4;
1388 ConfigFileEntry.min_nonwildcard_simple = 3;
1389 ConfigFileEntry.max_accept = 20;
1390 ConfigFileEntry.anti_nick_flood = 0;
1391 ConfigFileEntry.max_nick_time = 20;
1392 ConfigFileEntry.max_nick_changes = 5;
1393 ConfigFileEntry.anti_spam_exit_message_time = 0;
1394 ConfigFileEntry.ts_warn_delta = TS_WARN_DELTA_DEFAULT;
1395 ConfigFileEntry.ts_max_delta = TS_MAX_DELTA_DEFAULT;
1396 ConfigFileEntry.warn_no_nline = 1;
1397 ConfigFileEntry.stats_o_oper_only = 0;
1398 ConfigFileEntry.stats_k_oper_only = 1; /* masked */
1399 ConfigFileEntry.stats_i_oper_only = 1; /* masked */
1400 ConfigFileEntry.stats_P_oper_only = 0;
1401 ConfigFileEntry.caller_id_wait = 60;
1402 ConfigFileEntry.opers_bypass_callerid = 0;
1403 ConfigFileEntry.pace_wait = 10;
1404 ConfigFileEntry.pace_wait_simple = 1;
1405 ConfigFileEntry.short_motd = 0;
1406 ConfigFileEntry.ping_cookie = 0;
1407 ConfigFileEntry.no_oper_flood = 0;
1408 ConfigFileEntry.true_no_oper_flood = 0;
1409 ConfigFileEntry.oper_pass_resv = 1;
1410 ConfigFileEntry.max_targets = MAX_TARGETS_DEFAULT;
1411 ConfigFileEntry.oper_only_umodes = UMODE_DEBUG;
1412 ConfigFileEntry.oper_umodes = UMODE_BOTS | UMODE_LOCOPS | UMODE_SERVNOTICE |
1413 UMODE_OPERWALL | UMODE_WALLOP;
1414 ConfigFileEntry.use_egd = 0;
1415 ConfigFileEntry.egdpool_path = NULL;
1416 ConfigFileEntry.throttle_time = 10;
1417 }
1418
1419 static void
1420 validate_conf(void)
1421 {
1422 if (ConfigFileEntry.ts_warn_delta < TS_WARN_DELTA_MIN)
1423 ConfigFileEntry.ts_warn_delta = TS_WARN_DELTA_DEFAULT;
1424
1425 if (ConfigFileEntry.ts_max_delta < TS_MAX_DELTA_MIN)
1426 ConfigFileEntry.ts_max_delta = TS_MAX_DELTA_DEFAULT;
1427
1428 if (ServerInfo.network_name == NULL)
1429 DupString(ServerInfo.network_name,NETWORK_NAME_DEFAULT);
1430
1431 if (ServerInfo.network_desc == NULL)
1432 DupString(ServerInfo.network_desc,NETWORK_DESC_DEFAULT);
1433
1434 if (ConfigFileEntry.service_name == NULL)
1435 DupString(ConfigFileEntry.service_name, SERVICE_NAME_DEFAULT);
1436
1437 ConfigFileEntry.max_watch = IRCD_MAX(ConfigFileEntry.max_watch, WATCHSIZE_MIN);
1438 }
1439
1440 /* read_conf()
1441 *
1442 * inputs - file descriptor pointing to config file to use
1443 * output - None
1444 * side effects - Read configuration file.
1445 */
1446 static void
1447 read_conf(FILE *file)
1448 {
1449 lineno = 0;
1450
1451 set_default_conf(); /* Set default values prior to conf parsing */
1452 conf_parser_ctx.pass = 1;
1453 yyparse(); /* pick up the classes first */
1454
1455 rewind(file);
1456
1457 conf_parser_ctx.pass = 2;
1458 yyparse(); /* Load the values from the conf */
1459 validate_conf(); /* Check to make sure some values are still okay. */
1460 /* Some global values are also loaded here. */
1461 class_delete_marked(); /* Make sure classes are valid */
1462 }
1463
1464 /* lookup_confhost()
1465 *
1466 * start DNS lookups of all hostnames in the conf
1467 * line and convert an IP addresses in a.b.c.d number for to IP#s.
1468 */
1469 static void
1470 lookup_confhost(struct MaskItem *conf)
1471 {
1472 struct addrinfo hints, *res;
1473
1474 if (has_wildcards(conf->host))
1475 {
1476 ilog(LOG_TYPE_IRCD, "Host/server name error: (%s) (%s)",
1477 conf->host, conf->name);
1478 return;
1479 }
1480
1481 /* Do name lookup now on hostnames given and store the
1482 * ip numbers in conf structure.
1483 */
1484 memset(&hints, 0, sizeof(hints));
1485
1486 hints.ai_family = AF_UNSPEC;
1487 hints.ai_socktype = SOCK_STREAM;
1488
1489 /* Get us ready for a bind() and don't bother doing dns lookup */
1490 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
1491
1492 if (getaddrinfo(conf->host, NULL, &hints, &res))
1493 {
1494 conf_dns_lookup(conf);
1495 return;
1496 }
1497
1498 assert(res != NULL);
1499
1500 memcpy(&conf->addr, res->ai_addr, res->ai_addrlen);
1501 conf->addr.ss_len = res->ai_addrlen;
1502 conf->addr.ss.ss_family = res->ai_family;
1503
1504 freeaddrinfo(res);
1505 }
1506
1507 /* conf_connect_allowed()
1508 *
1509 * inputs - pointer to inaddr
1510 * - int type ipv4 or ipv6
1511 * output - BANNED or accepted
1512 * side effects - none
1513 */
1514 int
1515 conf_connect_allowed(struct irc_ssaddr *addr, int aftype)
1516 {
1517 struct ip_entry *ip_found;
1518 struct MaskItem *conf = find_dline_conf(addr, aftype);
1519
1520 /* DLINE exempt also gets you out of static limits/pacing... */
1521 if (conf && (conf->type == CONF_EXEMPT))
1522 return 0;
1523
1524 if (conf != NULL)
1525 return BANNED_CLIENT;
1526
1527 ip_found = find_or_add_ip(addr);
1528
1529 if ((CurrentTime - ip_found->last_attempt) <
1530 ConfigFileEntry.throttle_time)
1531 {
1532 ip_found->last_attempt = CurrentTime;
1533 return TOO_FAST;
1534 }
1535
1536 ip_found->last_attempt = CurrentTime;
1537 return 0;
1538 }
1539
1540 static struct MaskItem *
1541 find_regexp_kline(const char *uhi[])
1542 {
1543 #ifdef HAVE_LIBPCRE
1544 const dlink_node *ptr = NULL;
1545
1546 DLINK_FOREACH(ptr, rkconf_items.head)
1547 {
1548 struct MaskItem *aptr = ptr->data;
1549
1550 assert(aptr->regexuser);
1551 assert(aptr->regexhost);
1552
1553 if (!ircd_pcre_exec(aptr->regexuser, uhi[0]) &&
1554 (!ircd_pcre_exec(aptr->regexhost, uhi[1]) ||
1555 !ircd_pcre_exec(aptr->regexhost, uhi[2])))
1556 return aptr;
1557 }
1558 #endif
1559 return NULL;
1560 }
1561
1562 /* find_kill()
1563 *
1564 * inputs - pointer to client structure
1565 * output - pointer to struct MaskItem if found
1566 * side effects - See if this user is klined already,
1567 * and if so, return struct MaskItem pointer
1568 */
1569 struct MaskItem *
1570 find_kill(struct Client *client_p)
1571 {
1572 struct MaskItem *conf = NULL;
1573 const char *uhi[3];
1574
1575 uhi[0] = client_p->username;
1576 uhi[1] = client_p->host;
1577 uhi[2] = client_p->sockhost;
1578
1579 assert(client_p != NULL);
1580
1581 conf = find_conf_by_address(client_p->host, &client_p->localClient->ip,
1582 CONF_KLINE, client_p->localClient->aftype,
1583 client_p->username, NULL, 1);
1584 if (conf == NULL)
1585 conf = find_regexp_kline(uhi);
1586
1587 return conf;
1588 }
1589
1590 struct MaskItem *
1591 find_gline(struct Client *client_p)
1592 {
1593 struct MaskItem *conf;
1594
1595 assert(client_p != NULL);
1596
1597 conf = find_conf_by_address(client_p->host, &client_p->localClient->ip,
1598 CONF_GLINE, client_p->localClient->aftype,
1599 client_p->username, NULL, 1);
1600 return conf;
1601 }
1602
1603 /* cleanup_tklines()
1604 *
1605 * inputs - NONE
1606 * output - NONE
1607 * side effects - call function to expire temporary k/d lines
1608 * This is an event started off in ircd.c
1609 */
1610 void
1611 cleanup_tklines(void *notused)
1612 {
1613 hostmask_expire_temporary();
1614 expire_tklines(&xconf_items);
1615 expire_tklines(&nresv_items);
1616 expire_tklines(&resv_channel_list);
1617 }
1618
1619 /* expire_tklines()
1620 *
1621 * inputs - tkline list pointer
1622 * output - NONE
1623 * side effects - expire tklines
1624 */
1625 static void
1626 expire_tklines(dlink_list *tklist)
1627 {
1628 dlink_node *ptr;
1629 dlink_node *next_ptr;
1630 struct MaskItem *conf;
1631
1632 DLINK_FOREACH_SAFE(ptr, next_ptr, tklist->head)
1633 {
1634 conf = ptr->data;
1635
1636 if (!conf->hold || conf->hold > CurrentTime)
1637 continue;
1638
1639 if (conf->type == CONF_XLINE)
1640 {
1641 if (ConfigFileEntry.tkline_expire_notices)
1642 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1643 "Temporary X-line for [%s] expired", conf->name);
1644 conf_free(conf);
1645 }
1646 else if (conf->type == CONF_NRESV)
1647 {
1648 if (ConfigFileEntry.tkline_expire_notices)
1649 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1650 "Temporary RESV for [%s] expired", conf->name);
1651 conf_free(conf);
1652 }
1653 else if (conf->type == CONF_CRESV)
1654 {
1655 if (ConfigFileEntry.tkline_expire_notices)
1656 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1657 "Temporary RESV for [%s] expired", conf->name);
1658 delete_channel_resv(conf);
1659 }
1660 }
1661 }
1662
1663 /* oper_privs_as_string()
1664 *
1665 * inputs - pointer to client_p
1666 * output - pointer to static string showing oper privs
1667 * side effects - return as string, the oper privs as derived from port
1668 */
1669 static const struct oper_privs
1670 {
1671 const unsigned int flag;
1672 const unsigned char c;
1673 } flag_list[] = {
1674 { OPER_FLAG_ADMIN, 'A' },
1675 { OPER_FLAG_REMOTEBAN, 'B' },
1676 { OPER_FLAG_DIE, 'D' },
1677 { OPER_FLAG_GLINE, 'G' },
1678 { OPER_FLAG_REHASH, 'H' },
1679 { OPER_FLAG_K, 'K' },
1680 { OPER_FLAG_OPERWALL, 'L' },
1681 { OPER_FLAG_N, 'N' },
1682 { OPER_FLAG_GLOBAL_KILL, 'O' },
1683 { OPER_FLAG_REMOTE, 'R' },
1684 { OPER_FLAG_OPER_SPY, 'S' },
1685 { OPER_FLAG_UNKLINE, 'U' },
1686 { OPER_FLAG_X, 'X' },
1687 { 0, '\0' }
1688 };
1689
1690 char *
1691 oper_privs_as_string(const unsigned int port)
1692 {
1693 static char privs_out[16];
1694 char *privs_ptr = privs_out;
1695 const struct oper_privs *opriv = flag_list;
1696
1697 for (; opriv->flag; ++opriv)
1698 {
1699 if (port & opriv->flag)
1700 *privs_ptr++ = opriv->c;
1701 else
1702 *privs_ptr++ = ToLower(opriv->c);
1703 }
1704
1705 *privs_ptr = '\0';
1706
1707 return privs_out;
1708 }
1709
1710 /*
1711 * Input: A client to find the active oper{} name for.
1712 * Output: The nick!user@host{oper} of the oper.
1713 * "oper" is server name for remote opers
1714 * Side effects: None.
1715 */
1716 const char *
1717 get_oper_name(const struct Client *client_p)
1718 {
1719 dlink_node *cnode = NULL;
1720 /* +5 for !,@,{,} and null */
1721 static char buffer[NICKLEN + USERLEN + HOSTLEN + HOSTLEN + 5];
1722
1723 if (MyConnect(client_p))
1724 {
1725 if ((cnode = client_p->localClient->confs.head))
1726 {
1727 struct MaskItem *conf = cnode->data;
1728
1729 if (IsConfOperator(conf))
1730 {
1731 snprintf(buffer, sizeof(buffer), "%s!%s@%s{%s}", client_p->name,
1732 client_p->username, client_p->host, conf->name);
1733 return buffer;
1734 }
1735 }
1736
1737 /* Probably should assert here for now. If there is an oper out there
1738 * with no oper{} conf attached, it would be good for us to know...
1739 */
1740 assert(0); /* Oper without oper conf! */
1741 }
1742
1743 snprintf(buffer, sizeof(buffer), "%s!%s@%s{%s}", client_p->name,
1744 client_p->username, client_p->host, client_p->servptr->name);
1745 return buffer;
1746 }
1747
1748 /* read_conf_files()
1749 *
1750 * inputs - cold start YES or NO
1751 * output - none
1752 * side effects - read all conf files needed, ircd.conf kline.conf etc.
1753 */
1754 void
1755 read_conf_files(int cold)
1756 {
1757 const char *filename;
1758 char chanmodes[32];
1759 char chanlimit[32];
1760
1761 conf_parser_ctx.boot = cold;
1762 filename = ConfigFileEntry.configfile;
1763
1764 /* We need to know the initial filename for the yyerror() to report
1765 FIXME: The full path is in conffilenamebuf first time since we
1766 dont know anything else
1767
1768 - Gozem 2002-07-21
1769 */
1770 strlcpy(conffilebuf, filename, sizeof(conffilebuf));
1771
1772 if ((conf_parser_ctx.conf_file = fopen(filename, "r")) == NULL)
1773 {
1774 if (cold)
1775 {
1776 ilog(LOG_TYPE_IRCD, "Unable to read configuration file '%s': %s",
1777 filename, strerror(errno));
1778 exit(-1);
1779 }
1780 else
1781 {
1782 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1783 "Unable to read configuration file '%s': %s",
1784 filename, strerror(errno));
1785 return;
1786 }
1787 }
1788
1789 if (!cold)
1790 clear_out_old_conf();
1791
1792 read_conf(conf_parser_ctx.conf_file);
1793 fclose(conf_parser_ctx.conf_file);
1794
1795 add_isupport("NETWORK", ServerInfo.network_name, -1);
1796 snprintf(chanmodes, sizeof(chanmodes), "beI:%d",
1797 ConfigChannel.max_bans);
1798 add_isupport("MAXLIST", chanmodes, -1);
1799 add_isupport("MAXTARGETS", NULL, ConfigFileEntry.max_targets);
1800
1801 add_isupport("CHANTYPES", "#", -1);
1802
1803 snprintf(chanlimit, sizeof(chanlimit), "#:%d",
1804 ConfigChannel.max_chans_per_user);
1805 add_isupport("CHANLIMIT", chanlimit, -1);
1806 snprintf(chanmodes, sizeof(chanmodes), "%s",
1807 "beI,k,l,imnprstORS");
1808 add_isupport("CHANNELLEN", NULL, LOCAL_CHANNELLEN);
1809
1810 add_isupport("EXCEPTS", "e", -1);
1811 add_isupport("INVEX", "I", -1);
1812 add_isupport("CHANMODES", chanmodes, -1);
1813
1814 /*
1815 * message_locale may have changed. rebuild isupport since it relies
1816 * on strlen(form_str(RPL_ISUPPORT))
1817 */
1818 rebuild_isupport_message_line();
1819 }
1820
1821 /* clear_out_old_conf()
1822 *
1823 * inputs - none
1824 * output - none
1825 * side effects - Clear out the old configuration
1826 */
1827 static void
1828 clear_out_old_conf(void)
1829 {
1830 dlink_node *ptr = NULL, *next_ptr = NULL;
1831 struct MaskItem *conf;
1832 dlink_list *free_items [] = {
1833 &server_items, &oconf_items,
1834 &uconf_items, &xconf_items, &rxconf_items, &rkconf_items,
1835 &nresv_items, &cluster_items, &service_items, NULL
1836 };
1837
1838 dlink_list ** iterator = free_items; /* C is dumb */
1839
1840 /* We only need to free anything allocated by yyparse() here.
1841 * Resetting structs, etc, is taken care of by set_default_conf().
1842 */
1843
1844 for (; *iterator != NULL; iterator++)
1845 {
1846 DLINK_FOREACH_SAFE(ptr, next_ptr, (*iterator)->head)
1847 {
1848 conf = ptr->data;
1849
1850 dlinkDelete(&conf->node, map_to_list(conf->type));
1851
1852 /* XXX This is less than pretty */
1853 if (conf->type == CONF_SERVER || conf->type == CONF_OPER)
1854 {
1855 if (!conf->ref_count)
1856 conf_free(conf);
1857 }
1858 else if (conf->type == CONF_XLINE ||
1859 conf->type == CONF_RXLINE ||
1860 conf->type == CONF_RKLINE)
1861 {
1862 if (!conf->hold)
1863 conf_free(conf);
1864 }
1865 else
1866 conf_free(conf);
1867 }
1868 }
1869
1870 /*
1871 * don't delete the class table, rather mark all entries
1872 * for deletion. The table is cleaned up by class_delete_marked. - avalon
1873 */
1874 class_mark_for_deletion();
1875
1876 clear_out_address_conf();
1877
1878 /* clean out module paths */
1879 mod_clear_paths();
1880
1881 /* clean out ServerInfo */
1882 MyFree(ServerInfo.description);
1883 ServerInfo.description = NULL;
1884 MyFree(ServerInfo.network_name);
1885 ServerInfo.network_name = NULL;
1886 MyFree(ServerInfo.network_desc);
1887 ServerInfo.network_desc = NULL;
1888 MyFree(ConfigFileEntry.egdpool_path);
1889 ConfigFileEntry.egdpool_path = NULL;
1890 #ifdef HAVE_LIBCRYPTO
1891 if (ServerInfo.rsa_private_key != NULL)
1892 {
1893 RSA_free(ServerInfo.rsa_private_key);
1894 ServerInfo.rsa_private_key = NULL;
1895 }
1896
1897 MyFree(ServerInfo.rsa_private_key_file);
1898 ServerInfo.rsa_private_key_file = NULL;
1899
1900 if (ServerInfo.server_ctx)
1901 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_NO_SSLv2|
1902 SSL_OP_NO_SSLv3|
1903 SSL_OP_NO_TLSv1);
1904 if (ServerInfo.client_ctx)
1905 SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_NO_SSLv2|
1906 SSL_OP_NO_SSLv3|
1907 SSL_OP_NO_TLSv1);
1908 #endif
1909
1910 /* clean out old resvs from the conf */
1911 clear_conf_resv();
1912
1913 /* clean out AdminInfo */
1914 MyFree(AdminInfo.name);
1915 AdminInfo.name = NULL;
1916 MyFree(AdminInfo.email);
1917 AdminInfo.email = NULL;
1918 MyFree(AdminInfo.description);
1919 AdminInfo.description = NULL;
1920
1921 /* clean out listeners */
1922 close_listeners();
1923
1924 /* clean out general */
1925 MyFree(ConfigFileEntry.service_name);
1926 ConfigFileEntry.service_name = NULL;
1927
1928 delete_isupport("INVEX");
1929 delete_isupport("EXCEPTS");
1930 }
1931
1932 /* conf_add_class_to_conf()
1933 *
1934 * inputs - pointer to config item
1935 * output - NONE
1936 * side effects - Add a class pointer to a conf
1937 */
1938 void
1939 conf_add_class_to_conf(struct MaskItem *conf, const char *class_name)
1940 {
1941 struct ClassItem *class = NULL;
1942
1943 if (class_name == NULL)
1944 {
1945 conf->class = class_default;
1946
1947 if (conf->type == CONF_CLIENT)
1948 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1949 "Warning *** Defaulting to default class for %s@%s",
1950 conf->user, conf->host);
1951 else
1952 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1953 "Warning *** Defaulting to default class for %s",
1954 conf->name);
1955 }
1956 else
1957 conf->class = class_find(class_name, 1);
1958
1959 if (conf->class == NULL)
1960 {
1961 if (conf->type == CONF_CLIENT)
1962 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1963 "Warning *** Defaulting to default class for %s@%s",
1964 conf->user, conf->host);
1965 else
1966 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1967 "Warning *** Defaulting to default class for %s",
1968 conf->name);
1969 conf->class = class_default;
1970 }
1971 }
1972
1973 /* conf_add_server()
1974 *
1975 * inputs - pointer to config item
1976 * - pointer to link count already on this conf
1977 * output - NONE
1978 * side effects - Add a connect block
1979 */
1980 int
1981 conf_add_server(struct MaskItem *conf, const char *class_name)
1982 {
1983 conf_add_class_to_conf(conf, class_name);
1984
1985 if (EmptyString(conf->host) || EmptyString(conf->name))
1986 {
1987 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1988 "Bad connect block");
1989 ilog(LOG_TYPE_IRCD, "Bad connect block");
1990 return -1;
1991 }
1992
1993 if (EmptyString(conf->passwd))
1994 {
1995 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1996 "Bad connect block, name %s",
1997 conf->name);
1998 ilog(LOG_TYPE_IRCD, "Bad connect block, host %s", conf->name);
1999 return -1;
2000 }
2001
2002 lookup_confhost(conf);
2003
2004 return 0;
2005 }
2006
2007 /* yyerror()
2008 *
2009 * inputs - message from parser
2010 * output - NONE
2011 * side effects - message to opers and log file entry is made
2012 */
2013 void
2014 yyerror(const char *msg)
2015 {
2016 char newlinebuf[IRCD_BUFSIZE];
2017
2018 if (conf_parser_ctx.pass != 1)
2019 return;
2020
2021 strip_tabs(newlinebuf, linebuf, sizeof(newlinebuf));
2022 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
2023 "\"%s\", line %u: %s: %s",
2024 conffilebuf, lineno + 1, msg, newlinebuf);
2025 ilog(LOG_TYPE_IRCD, "\"%s\", line %u: %s: %s",
2026 conffilebuf, lineno + 1, msg, newlinebuf);
2027 }
2028
2029 /*
2030 * valid_tkline()
2031 *
2032 * inputs - pointer to ascii string to check
2033 * - whether the specified time is in seconds or minutes
2034 * output - -1 not enough parameters
2035 * - 0 if not an integer number, else the number
2036 * side effects - none
2037 * Originally written by Dianora (Diane, db@db.net)
2038 */
2039 time_t
2040 valid_tkline(const char *p, int minutes)
2041 {
2042 time_t result = 0;
2043
2044 for (; *p; ++p)
2045 {
2046 if (!IsDigit(*p))
2047 return 0;
2048
2049 result *= 10;
2050 result += ((*p) & 0xF);
2051 }
2052
2053 /*
2054 * In the degenerate case where oper does a /quote kline 0 user@host :reason
2055 * i.e. they specifically use 0, I am going to return 1 instead
2056 * as a return value of non-zero is used to flag it as a temporary kline
2057 */
2058 if (result == 0)
2059 result = 1;
2060
2061 /*
2062 * If the incoming time is in seconds convert it to minutes for the purpose
2063 * of this calculation
2064 */
2065 if (!minutes)
2066 result = result / (time_t)60;
2067
2068 if (result > MAX_TDKLINE_TIME)
2069 result = MAX_TDKLINE_TIME;
2070
2071 result = result * (time_t)60; /* turn it into seconds */
2072
2073 return result;
2074 }
2075
2076 /* valid_wild_card()
2077 *
2078 * input - pointer to client
2079 * - int flag, 0 for no warning oper 1 for warning oper
2080 * - count of following varargs to check
2081 * output - 0 if not valid, 1 if valid
2082 * side effects - NOTICE is given to source_p if warn is 1
2083 */
2084 int
2085 valid_wild_card(struct Client *source_p, int warn, int count, ...)
2086 {
2087 char *p;
2088 char tmpch;
2089 int nonwild = 0;
2090 va_list args;
2091
2092 /*
2093 * Now we must check the user and host to make sure there
2094 * are at least NONWILDCHARS non-wildcard characters in
2095 * them, otherwise assume they are attempting to kline
2096 * *@* or some variant of that. This code will also catch
2097 * people attempting to kline *@*.tld, as long as NONWILDCHARS
2098 * is greater than 3. In that case, there are only 3 non-wild
2099 * characters (tld), so if NONWILDCHARS is 4, the kline will
2100 * be disallowed.
2101 * -wnder
2102 */
2103
2104 va_start(args, count);
2105
2106 while (count--)
2107 {
2108 p = va_arg(args, char *);
2109 if (p == NULL)
2110 continue;
2111
2112 while ((tmpch = *p++))
2113 {
2114 if (!IsKWildChar(tmpch))
2115 {
2116 /*
2117 * If we find enough non-wild characters, we can
2118 * break - no point in searching further.
2119 */
2120 if (++nonwild >= ConfigFileEntry.min_nonwildcard)
2121 return 1;
2122 }
2123 }
2124 }
2125
2126 if (warn)
2127 sendto_one(source_p, ":%s NOTICE %s :Please include at least %d non-wildcard characters with the mask",
2128 me.name, source_p->name, ConfigFileEntry.min_nonwildcard);
2129 return 0;
2130 }
2131
2132 /* XXX should this go into a separate file ? -Dianora */
2133 /* parse_aline
2134 *
2135 * input - pointer to cmd name being used
2136 * - pointer to client using cmd
2137 * - parc parameter count
2138 * - parv[] list of parameters to parse
2139 * - parse_flags bit map of things to test
2140 * - pointer to user or string to parse into
2141 * - pointer to host or NULL to parse into if non NULL
2142 * - pointer to optional tkline time or NULL
2143 * - pointer to target_server to parse into if non NULL
2144 * - pointer to reason to parse into
2145 *
2146 * output - 1 if valid, -1 if not valid
2147 * side effects - A generalised k/d/x etc. line parser,
2148 * "ALINE [time] user@host|string [ON] target :reason"
2149 * will parse returning a parsed user, host if
2150 * h_p pointer is non NULL, string otherwise.
2151 * if tkline_time pointer is non NULL a tk line will be set
2152 * to non zero if found.
2153 * if tkline_time pointer is NULL and tk line is found,
2154 * error is reported.
2155 * if target_server is NULL and an "ON" is found error
2156 * is reported.
2157 * if reason pointer is NULL ignore pointer,
2158 * this allows use of parse_a_line in unkline etc.
2159 *
2160 * - Dianora
2161 */
2162 int
2163 parse_aline(const char *cmd, struct Client *source_p,
2164 int parc, char **parv,
2165 int parse_flags, char **up_p, char **h_p, time_t *tkline_time,
2166 char **target_server, char **reason)
2167 {
2168 int found_tkline_time=0;
2169 static char def_reason[] = "No Reason";
2170 static char user[USERLEN*4+1];
2171 static char host[HOSTLEN*4+1];
2172
2173 parv++;
2174 parc--;
2175
2176 found_tkline_time = valid_tkline(*parv, TK_MINUTES);
2177
2178 if (found_tkline_time != 0)
2179 {
2180 parv++;
2181 parc--;
2182
2183 if (tkline_time != NULL)
2184 *tkline_time = found_tkline_time;
2185 else
2186 {
2187 sendto_one(source_p, ":%s NOTICE %s :temp_line not supported by %s",
2188 me.name, source_p->name, cmd);
2189 return -1;
2190 }
2191 }
2192
2193 if (parc == 0)
2194 {
2195 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
2196 me.name, source_p->name, cmd);
2197 return -1;
2198 }
2199
2200 if (h_p == NULL)
2201 *up_p = *parv;
2202 else
2203 {
2204 if (find_user_host(source_p, *parv, user, host, parse_flags) == 0)
2205 return -1;
2206
2207 *up_p = user;
2208 *h_p = host;
2209 }
2210
2211 parc--;
2212 parv++;
2213
2214 if (parc != 0)
2215 {
2216 if (irccmp(*parv, "ON") == 0)
2217 {
2218 parc--;
2219 parv++;
2220
2221 if (target_server == NULL)
2222 {
2223 sendto_one(source_p, ":%s NOTICE %s :ON server not supported by %s",
2224 me.name, source_p->name, cmd);
2225 return -1;
2226 }
2227
2228 if (!HasOFlag(source_p, OPER_FLAG_REMOTEBAN))
2229 {
2230 sendto_one(source_p, form_str(ERR_NOPRIVS),
2231 me.name, source_p->name, "remoteban");
2232 return -1;
2233 }
2234
2235 if (parc == 0 || EmptyString(*parv))
2236 {
2237 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
2238 me.name, source_p->name, cmd);
2239 return -1;
2240 }
2241
2242 *target_server = *parv;
2243 parc--;
2244 parv++;
2245 }
2246 else
2247 {
2248 /* Make sure target_server *is* NULL if no ON server found
2249 * caller probably NULL'd it first, but no harm to do it again -db
2250 */
2251 if (target_server != NULL)
2252 *target_server = NULL;
2253 }
2254 }
2255
2256 if (h_p != NULL)
2257 {
2258 if (strchr(user, '!') != NULL)
2259 {
2260 sendto_one(source_p, ":%s NOTICE %s :Invalid character '!' in kline",
2261 me.name, source_p->name);
2262 return -1;
2263 }
2264
2265 if ((parse_flags & AWILD) && !valid_wild_card(source_p, 1, 2, *up_p, *h_p))
2266 return -1;
2267 }
2268 else
2269 if ((parse_flags & AWILD) && !valid_wild_card(source_p, 1, 1, *up_p))
2270 return -1;
2271
2272 if (reason != NULL)
2273 {
2274 if (parc != 0 && !EmptyString(*parv))
2275 {
2276 *reason = *parv;
2277 if (!valid_comment(source_p, *reason, 1))
2278 return -1;
2279 }
2280 else
2281 *reason = def_reason;
2282 }
2283
2284 return 1;
2285 }
2286
2287 /* find_user_host()
2288 *
2289 * inputs - pointer to client placing kline
2290 * - pointer to user_host_or_nick
2291 * - pointer to user buffer
2292 * - pointer to host buffer
2293 * output - 0 if not ok to kline, 1 to kline i.e. if valid user host
2294 * side effects -
2295 */
2296 static int
2297 find_user_host(struct Client *source_p, char *user_host_or_nick,
2298 char *luser, char *lhost, unsigned int flags)
2299 {
2300 struct Client *target_p = NULL;
2301 char *hostp = NULL;
2302
2303 if (lhost == NULL)
2304 {
2305 strlcpy(luser, user_host_or_nick, USERLEN*4 + 1);
2306 return 1;
2307 }
2308
2309 if ((hostp = strchr(user_host_or_nick, '@')) || *user_host_or_nick == '*')
2310 {
2311 /* Explicit user@host mask given */
2312
2313 if (hostp != NULL) /* I'm a little user@host */
2314 {
2315 *(hostp++) = '\0'; /* short and squat */
2316 if (*user_host_or_nick)
2317 strlcpy(luser, user_host_or_nick, USERLEN*4 + 1); /* here is my user */
2318 else
2319 strcpy(luser, "*");
2320 if (*hostp)
2321 strlcpy(lhost, hostp, HOSTLEN + 1); /* here is my host */
2322 else
2323 strcpy(lhost, "*");
2324 }
2325 else
2326 {
2327 luser[0] = '*'; /* no @ found, assume its *@somehost */
2328 luser[1] = '\0';
2329 strlcpy(lhost, user_host_or_nick, HOSTLEN*4 + 1);
2330 }
2331
2332 return 1;
2333 }
2334 else
2335 {
2336 /* Try to find user@host mask from nick */
2337 /* Okay to use source_p as the first param, because source_p == client_p */
2338 if ((target_p =
2339 find_chasing(source_p, source_p, user_host_or_nick, NULL)) == NULL)
2340 return 0;
2341
2342 if (IsExemptKline(target_p))
2343 {
2344 if (!IsServer(source_p))
2345 sendto_one(source_p,
2346 ":%s NOTICE %s :%s is E-lined",
2347 me.name, source_p->name, target_p->name);
2348 return 0;
2349 }
2350
2351 /*
2352 * turn the "user" bit into "*user", blow away '~'
2353 * if found in original user name (non-idented)
2354 */
2355 strlcpy(luser, target_p->username, USERLEN*4 + 1);
2356
2357 if (target_p->username[0] == '~')
2358 luser[0] = '*';
2359
2360 if (target_p->sockhost[0] == '\0' ||
2361 (target_p->sockhost[0] == '0' && target_p->sockhost[1] == '\0'))
2362 strlcpy(lhost, target_p->host, HOSTLEN*4 + 1);
2363 else
2364 strlcpy(lhost, target_p->sockhost, HOSTLEN*4 + 1);
2365 return 1;
2366 }
2367
2368 return 0;
2369 }
2370
2371 /* valid_comment()
2372 *
2373 * inputs - pointer to client
2374 * - pointer to comment
2375 * output - 0 if no valid comment,
2376 * - 1 if valid
2377 * side effects - truncates reason where necessary
2378 */
2379 int
2380 valid_comment(struct Client *source_p, char *comment, int warn)
2381 {
2382 if (strlen(comment) > REASONLEN)
2383 comment[REASONLEN-1] = '\0';
2384
2385 return 1;
2386 }
2387
2388 /* match_conf_password()
2389 *
2390 * inputs - pointer to given password
2391 * - pointer to Conf
2392 * output - 1 or 0 if match
2393 * side effects - none
2394 */
2395 int
2396 match_conf_password(const char *password, const struct MaskItem *conf)
2397 {
2398 const char *encr = NULL;
2399
2400 if (EmptyString(password) || EmptyString(conf->passwd))
2401 return 0;
2402
2403 if (conf->flags & CONF_FLAGS_ENCRYPTED)
2404 encr = crypt(password, conf->passwd);
2405 else
2406 encr = password;
2407
2408 return !strcmp(encr, conf->passwd);
2409 }
2410
2411 /*
2412 * cluster_a_line
2413 *
2414 * inputs - client sending the cluster
2415 * - command name "KLINE" "XLINE" etc.
2416 * - capab -- CAP_KLN etc. from s_serv.h
2417 * - cluster type -- CLUSTER_KLINE etc. from conf.h
2418 * - pattern and args to send along
2419 * output - none
2420 * side effects - Take source_p send the pattern with args given
2421 * along to all servers that match capab and cluster type
2422 */
2423 void
2424 cluster_a_line(struct Client *source_p, const char *command,
2425 int capab, int cluster_type, const char *pattern, ...)
2426 {
2427 va_list args;
2428 char buffer[IRCD_BUFSIZE];
2429 const dlink_node *ptr = NULL;
2430
2431 va_start(args, pattern);
2432 vsnprintf(buffer, sizeof(buffer), pattern, args);
2433 va_end(args);
2434
2435 DLINK_FOREACH(ptr, cluster_items.head)
2436 {
2437 const struct MaskItem *conf = ptr->data;
2438
2439 if (conf->flags & cluster_type)
2440 sendto_match_servs(source_p, conf->name, CAP_CLUSTER|capab,
2441 "%s %s %s", command, conf->name, buffer);
2442 }
2443 }
2444
2445 /*
2446 * split_nuh
2447 *
2448 * inputs - pointer to original mask (modified in place)
2449 * - pointer to pointer where nick should go
2450 * - pointer to pointer where user should go
2451 * - pointer to pointer where host should go
2452 * output - NONE
2453 * side effects - mask is modified in place
2454 * If nick pointer is NULL, ignore writing to it
2455 * this allows us to use this function elsewhere.
2456 *
2457 * mask nick user host
2458 * ---------------------- ------- ------- ------
2459 * Dianora!db@db.net Dianora db db.net
2460 * Dianora Dianora * *
2461 * db.net * * db.net
2462 * OR if nick pointer is NULL
2463 * Dianora - * Dianora
2464 * Dianora! Dianora * *
2465 * Dianora!@ Dianora * *
2466 * Dianora!db Dianora db *
2467 * Dianora!@db.net Dianora * db.net
2468 * db@db.net * db db.net
2469 * !@ * * *
2470 * @ * * *
2471 * ! * * *
2472 */
2473 void
2474 split_nuh(struct split_nuh_item *const iptr)
2475 {
2476 char *p = NULL, *q = NULL;
2477
2478 if (iptr->nickptr)
2479 strlcpy(iptr->nickptr, "*", iptr->nicksize);
2480 if (iptr->userptr)
2481 strlcpy(iptr->userptr, "*", iptr->usersize);
2482 if (iptr->hostptr)
2483 strlcpy(iptr->hostptr, "*", iptr->hostsize);
2484
2485 if ((p = strchr(iptr->nuhmask, '!')))
2486 {
2487 *p = '\0';
2488
2489 if (iptr->nickptr && *iptr->nuhmask != '\0')
2490 strlcpy(iptr->nickptr, iptr->nuhmask, iptr->nicksize);
2491
2492 if ((q = strchr(++p, '@'))) {
2493 *q++ = '\0';
2494
2495 if (*p != '\0')
2496 strlcpy(iptr->userptr, p, iptr->usersize);
2497
2498 if (*q != '\0')
2499 strlcpy(iptr->hostptr, q, iptr->hostsize);
2500 }
2501 else
2502 {
2503 if (*p != '\0')
2504 strlcpy(iptr->userptr, p, iptr->usersize);
2505 }
2506 }
2507 else
2508 {
2509 /* No ! found so lets look for a user@host */
2510 if ((p = strchr(iptr->nuhmask, '@')))
2511 {
2512 /* if found a @ */
2513 *p++ = '\0';
2514
2515 if (*iptr->nuhmask != '\0')
2516 strlcpy(iptr->userptr, iptr->nuhmask, iptr->usersize);
2517
2518 if (*p != '\0')
2519 strlcpy(iptr->hostptr, p, iptr->hostsize);
2520 }
2521 else
2522 {
2523 /* no @ found */
2524 if (!iptr->nickptr || strpbrk(iptr->nuhmask, ".:"))
2525 strlcpy(iptr->hostptr, iptr->nuhmask, iptr->hostsize);
2526 else
2527 strlcpy(iptr->nickptr, iptr->nuhmask, iptr->nicksize);
2528 }
2529 }
2530 }

Properties

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