ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/conf.c
Revision: 1636
Committed: Sun Nov 4 17:09:47 2012 UTC (12 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 66754 byte(s)
Log Message:
- Cleanup configuration subsystem
- Fixed broken CIDR support for /challenge

File Contents

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

Properties

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