ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/conf.c
Revision: 7258
Committed: Sat Feb 6 17:29:57 2016 UTC (9 years, 6 months ago) by michael
Content type: text/x-csrc
File size: 48945 byte(s)
Log Message:
- Improve libGeoIP support

File Contents

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

Properties

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