ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/conf.c
Revision: 4523
Committed: Tue Aug 19 09:43:38 2014 UTC (11 years ago) by michael
Content type: text/x-csrc
File size: 52058 byte(s)
Log Message:
- Fixed double dlinkDelete() in conf_free()
- Fixed memory leak with server/oper/auth configuration items

File Contents

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

Properties

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