ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/conf.c
Revision: 4511
Committed: Sun Aug 17 14:50:39 2014 UTC (11 years ago) by michael
Content type: text/x-csrc
File size: 52193 byte(s)
Log Message:
- conf.c:conf_free(): fixed possible infinite loop with /stats c as reported by Adam.
  We now assume that all MaskItem are linked onto some list

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

Properties

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