ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/conf.c
Revision: 4324
Committed: Fri Aug 1 16:59:35 2014 UTC (12 years, 2 months ago) by michael
Content type: text/x-csrc
File size: 57810 byte(s)
Log Message:
- conf.c: optimized count_ip_hash()

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

Properties

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