ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/conf.c
Revision: 1646
Committed: Wed Nov 7 21:02:43 2012 UTC (11 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 65374 byte(s)
Log Message:
- First pass of conf parser stabilization/cleanup

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

Properties

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