ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/conf.c
Revision: 4319
Committed: Fri Aug 1 16:48:38 2014 UTC (11 years ago) by michael
Content type: text/x-csrc
File size: 57806 byte(s)
Log Message:
- Cleaned up ipcache

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 adx 30 {
598 michael 4319 *number_ips_stored += dlink_list_length(&ip_hash_table[i]);
599     *mem_ips_stored += dlink_list_length(&ip_hash_table[i]) * sizeof(struct ip_entry);
600 adx 30 }
601     }
602    
603     /* garbage_collect_ip_entries()
604     *
605     * input - NONE
606     * output - NONE
607     * side effects - free up all ip entries with no connections
608     */
609     static void
610 michael 4319 ipcache_remove_expired_entries(void *unused)
611 adx 30 {
612 michael 4319 dlink_node *ptr = NULL;
613 adx 30
614 michael 3375 for (unsigned int i = 0; i < IP_HASH_SIZE; ++i)
615 adx 30 {
616 michael 4319 DLINK_FOREACH(ptr, ip_hash_table[i].head)
617 adx 30 {
618 michael 4319 struct ip_entry *iptr = ptr->data;
619 adx 30
620 michael 4319 if (iptr->count == 0 &&
621     (CurrentTime - iptr->last_attempt) >= ConfigFileEntry.throttle_time)
622 adx 30 {
623 michael 4319 dlinkDelete(&iptr->node, &ip_hash_table[i]);
624     mp_pool_release(iptr);
625 adx 30 }
626     }
627     }
628     }
629    
630     /* detach_conf()
631     *
632     * inputs - pointer to client to detach
633     * - type of conf to detach
634     * output - 0 for success, -1 for failure
635     * side effects - Disassociate configuration from the client.
636     * Also removes a class from the list if marked for deleting.
637     */
638 michael 1632 void
639     detach_conf(struct Client *client_p, enum maskitem_type type)
640 adx 30 {
641 michael 3235 dlink_node *ptr = NULL, *ptr_next = NULL;
642 adx 30
643 michael 3235 DLINK_FOREACH_SAFE(ptr, ptr_next, client_p->localClient->confs.head)
644 adx 30 {
645 michael 1645 struct MaskItem *conf = ptr->data;
646 adx 30
647 michael 1645 assert(conf->type & (CONF_CLIENT | CONF_OPER | CONF_SERVER));
648     assert(conf->ref_count > 0);
649     assert(conf->class->ref_count > 0);
650 adx 30
651 michael 1645 if (!(conf->type & type))
652     continue;
653 michael 671
654 michael 1645 dlinkDelete(ptr, &client_p->localClient->confs);
655     free_dlink_node(ptr);
656 michael 1644
657 michael 2278 if (conf->type == CONF_CLIENT)
658 michael 1645 remove_from_cidr_check(&client_p->localClient->ip, conf->class);
659 adx 30
660 michael 1645 if (--conf->class->ref_count == 0 && conf->class->active == 0)
661     {
662     class_free(conf->class);
663     conf->class = NULL;
664 adx 30 }
665 michael 1645
666     if (--conf->ref_count == 0 && conf->active == 0)
667     conf_free(conf);
668 adx 30 }
669     }
670    
671     /* attach_conf()
672     *
673     * inputs - client pointer
674     * - conf pointer
675     * output -
676     * side effects - Associate a specific configuration entry to a *local*
677     * client (this is the one which used in accepting the
678     * connection). Note, that this automatically changes the
679     * attachment if there was an old one...
680     */
681     int
682 michael 1632 attach_conf(struct Client *client_p, struct MaskItem *conf)
683 adx 30 {
684 michael 3235 if (dlinkFind(&client_p->localClient->confs, conf))
685 adx 30 return 1;
686    
687 michael 1632 if (conf->type == CONF_CLIENT)
688     if (cidr_limit_reached(IsConfExemptLimits(conf),
689     &client_p->localClient->ip, conf->class))
690 michael 1394 return TOO_MANY; /* Already at maximum allowed */
691 adx 30
692 michael 1632 conf->class->ref_count++;
693 michael 1644 conf->ref_count++;
694 adx 30
695     dlinkAdd(conf, make_dlink_node(), &client_p->localClient->confs);
696    
697     return 0;
698     }
699    
700     /* attach_connect_block()
701     *
702     * inputs - pointer to server to attach
703     * - name of server
704     * - hostname of server
705     * output - true (1) if both are found, otherwise return false (0)
706     * side effects - find connect block and attach them to connecting client
707     */
708     int
709     attach_connect_block(struct Client *client_p, const char *name,
710     const char *host)
711     {
712     dlink_node *ptr;
713 michael 1632 struct MaskItem *conf = NULL;
714 adx 30
715     assert(client_p != NULL);
716     assert(host != NULL);
717    
718     if (client_p == NULL || host == NULL)
719     return 0;
720    
721     DLINK_FOREACH(ptr, server_items.head)
722     {
723     conf = ptr->data;
724    
725 michael 1652 if (match(conf->name, name) || match(conf->host, host))
726 adx 30 continue;
727    
728     attach_conf(client_p, conf);
729     return -1;
730     }
731    
732     return 0;
733     }
734    
735     /* find_conf_name()
736     *
737     * inputs - pointer to conf link list to search
738     * - pointer to name to find
739     * - int mask of type of conf to find
740     * output - NULL or pointer to conf found
741     * side effects - find a conf entry which matches the name
742     * and has the given mask.
743     */
744 michael 1632 struct MaskItem *
745     find_conf_name(dlink_list *list, const char *name, enum maskitem_type type)
746 adx 30 {
747     dlink_node *ptr;
748 michael 1632 struct MaskItem* conf;
749 adx 30
750     DLINK_FOREACH(ptr, list->head)
751     {
752     conf = ptr->data;
753 michael 2916
754 adx 30 if (conf->type == type)
755     {
756 michael 3375 if (conf->name && (!irccmp(conf->name, name) ||
757 michael 1652 !match(conf->name, name)))
758 adx 30 return conf;
759     }
760     }
761    
762     return NULL;
763     }
764    
765     /* map_to_list()
766     *
767     * inputs - ConfType conf
768     * output - pointer to dlink_list to use
769     * side effects - none
770     */
771     static dlink_list *
772 michael 1632 map_to_list(enum maskitem_type type)
773 adx 30 {
774     switch(type)
775     {
776 michael 1632 case CONF_XLINE:
777 adx 30 return(&xconf_items);
778     break;
779 michael 1632 case CONF_ULINE:
780 adx 30 return(&uconf_items);
781     break;
782 michael 1632 case CONF_NRESV:
783 adx 30 return(&nresv_items);
784     break;
785 michael 1825 case CONF_CRESV:
786 michael 2174 return(&cresv_items);
787 michael 1632 case CONF_OPER:
788 adx 30 return(&oconf_items);
789     break;
790 michael 1632 case CONF_SERVER:
791 adx 30 return(&server_items);
792     break;
793 michael 1632 case CONF_SERVICE:
794 michael 1172 return(&service_items);
795     break;
796 michael 1632 case CONF_CLUSTER:
797 adx 30 return(&cluster_items);
798     break;
799     default:
800     return NULL;
801     }
802     }
803    
804     /* find_matching_name_conf()
805     *
806     * inputs - type of link list to look in
807     * - pointer to name string to find
808     * - pointer to user
809     * - pointer to host
810 michael 1644 * - optional flags to match on as well
811 michael 1632 * output - NULL or pointer to found struct MaskItem
812 adx 30 * side effects - looks for a match on name field
813     */
814 michael 1632 struct MaskItem *
815     find_matching_name_conf(enum maskitem_type type, const char *name, const char *user,
816 michael 1644 const char *host, unsigned int flags)
817 adx 30 {
818     dlink_node *ptr=NULL;
819 michael 1632 struct MaskItem *conf=NULL;
820 adx 30 dlink_list *list_p = map_to_list(type);
821    
822     switch (type)
823     {
824 michael 1632 case CONF_SERVICE:
825 michael 1157 DLINK_FOREACH(ptr, list_p->head)
826     {
827     conf = ptr->data;
828    
829     if (EmptyString(conf->name))
830     continue;
831     if ((name != NULL) && !irccmp(name, conf->name))
832     return conf;
833     }
834     break;
835    
836 michael 1632 case CONF_XLINE:
837     case CONF_ULINE:
838     case CONF_NRESV:
839 michael 1825 case CONF_CRESV:
840 adx 30 DLINK_FOREACH(ptr, list_p->head)
841     {
842     conf = ptr->data;
843    
844     if (EmptyString(conf->name))
845 michael 2182 continue;
846 michael 1653 if ((name != NULL) && !match(conf->name, name))
847 adx 30 {
848 michael 2182 if ((user == NULL && (host == NULL)))
849     return conf;
850     if ((conf->flags & flags) != flags)
851 adx 30 continue;
852 michael 2182 if (EmptyString(conf->user) || EmptyString(conf->host))
853     return conf;
854     if (!match(conf->user, user) && !match(conf->host, host))
855     return conf;
856 adx 30 }
857     }
858     break;
859    
860 michael 1632 case CONF_SERVER:
861 adx 30 DLINK_FOREACH(ptr, list_p->head)
862     {
863     conf = ptr->data;
864    
865 michael 1652 if ((name != NULL) && !match(name, conf->name))
866 adx 30 return conf;
867 michael 1652 else if ((host != NULL) && !match(host, conf->host))
868 adx 30 return conf;
869     }
870     break;
871 michael 2916
872 adx 30 default:
873     break;
874     }
875     return NULL;
876     }
877    
878     /* find_exact_name_conf()
879     *
880     * inputs - type of link list to look in
881     * - pointer to name string to find
882     * - pointer to user
883     * - pointer to host
884 michael 1632 * output - NULL or pointer to found struct MaskItem
885 adx 30 * side effects - looks for an exact match on name field
886     */
887 michael 1632 struct MaskItem *
888     find_exact_name_conf(enum maskitem_type type, const struct Client *who, const char *name,
889 adx 30 const char *user, const char *host)
890     {
891     dlink_node *ptr = NULL;
892 michael 1632 struct MaskItem *conf;
893     dlink_list *list_p = map_to_list(type);
894 adx 30
895     switch(type)
896     {
897 michael 1632 case CONF_XLINE:
898     case CONF_ULINE:
899     case CONF_NRESV:
900 michael 1825 case CONF_CRESV:
901 adx 30
902     DLINK_FOREACH(ptr, list_p->head)
903     {
904     conf = ptr->data;
905 michael 1632
906 adx 30 if (EmptyString(conf->name))
907 michael 2182 continue;
908 michael 2916
909 adx 30 if (irccmp(conf->name, name) == 0)
910     {
911 michael 2182 if ((user == NULL && (host == NULL)))
912     return conf;
913     if (EmptyString(conf->user) || EmptyString(conf->host))
914     return conf;
915     if (!match(conf->user, user) && !match(conf->host, host))
916     return conf;
917 adx 30 }
918     }
919     break;
920    
921 michael 1632 case CONF_OPER:
922 adx 30 DLINK_FOREACH(ptr, list_p->head)
923     {
924     conf = ptr->data;
925 michael 1285
926 adx 30 if (EmptyString(conf->name))
927 michael 1285 continue;
928    
929     if (!irccmp(conf->name, name))
930 adx 30 {
931 michael 1285 if (!who)
932     return conf;
933 michael 1632 if (EmptyString(conf->user) || EmptyString(conf->host))
934 michael 1636 return NULL;
935 michael 1652 if (!match(conf->user, who->username))
936 michael 1285 {
937 michael 1632 switch (conf->htype)
938 michael 1285 {
939     case HM_HOST:
940 michael 1652 if (!match(conf->host, who->host) || !match(conf->host, who->sockhost))
941 michael 1637 if (!conf->class->max_total || conf->class->ref_count < conf->class->max_total)
942     return conf;
943 michael 1285 break;
944     case HM_IPV4:
945     if (who->localClient->aftype == AF_INET)
946 michael 1632 if (match_ipv4(&who->localClient->ip, &conf->addr, conf->bits))
947 michael 1637 if (!conf->class->max_total || conf->class->ref_count < conf->class->max_total)
948     return conf;
949 michael 1285 break;
950     #ifdef IPV6
951     case HM_IPV6:
952     if (who->localClient->aftype == AF_INET6)
953 michael 1632 if (match_ipv6(&who->localClient->ip, &conf->addr, conf->bits))
954 michael 1637 if (!conf->class->max_total || conf->class->ref_count < conf->class->max_total)
955     return conf;
956 michael 1285 break;
957     #endif
958     default:
959     assert(0);
960     }
961     }
962 adx 30 }
963     }
964 michael 1285
965 adx 30 break;
966    
967 michael 1632 case CONF_SERVER:
968 adx 30 DLINK_FOREACH(ptr, list_p->head)
969     {
970     conf = ptr->data;
971 michael 1632
972 adx 30 if (EmptyString(conf->name))
973 michael 2182 continue;
974 michael 2916
975 adx 30 if (name == NULL)
976     {
977 michael 2182 if (EmptyString(conf->host))
978     continue;
979     if (irccmp(conf->host, host) == 0)
980     return conf;
981 adx 30 }
982     else if (irccmp(conf->name, name) == 0)
983 michael 2182 return conf;
984 adx 30 }
985 michael 2182
986 adx 30 break;
987    
988     default:
989     break;
990     }
991 michael 2182
992     return NULL;
993 adx 30 }
994    
995     /* rehash()
996     *
997     * Actual REHASH service routine. Called with sig == 0 if it has been called
998     * as a result of an operator issuing this command, else assume it has been
999     * called as a result of the server receiving a HUP signal.
1000     */
1001     int
1002     rehash(int sig)
1003     {
1004 michael 3235 if (sig)
1005 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1006 michael 2830 "Got signal SIGHUP, reloading configuration file(s)");
1007 adx 30
1008     restart_resolver();
1009 michael 1001
1010 adx 30 /* don't close listeners until we know we can go ahead with the rehash */
1011    
1012     /* Check to see if we magically got(or lost) IPv6 support */
1013     check_can_use_v6();
1014    
1015     read_conf_files(0);
1016    
1017 michael 3235 if (ServerInfo.description)
1018 adx 30 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
1019    
1020     load_conf_modules();
1021 michael 3929 check_conf_klines();
1022 adx 30
1023 michael 1831 return 0;
1024 adx 30 }
1025    
1026     /* set_default_conf()
1027     *
1028     * inputs - NONE
1029     * output - NONE
1030     * side effects - Set default values here.
1031     * This is called **PRIOR** to parsing the
1032     * configuration file. If you want to do some validation
1033     * of values later, put them in validate_conf().
1034     */
1035     static void
1036     set_default_conf(void)
1037     {
1038     /* verify init_class() ran, this should be an unnecessary check
1039     * but its not much work.
1040     */
1041 michael 1644 assert(class_default == class_get_list()->tail->data);
1042 adx 30
1043     #ifdef HAVE_LIBCRYPTO
1044 michael 4114 ServerInfo.message_digest_algorithm = EVP_sha256();
1045 adx 30 ServerInfo.rsa_private_key = NULL;
1046     ServerInfo.rsa_private_key_file = NULL;
1047     #endif
1048    
1049     /* ServerInfo.name is not rehashable */
1050     /* ServerInfo.name = ServerInfo.name; */
1051     ServerInfo.description = NULL;
1052 michael 1646 ServerInfo.network_name = xstrdup(NETWORK_NAME_DEFAULT);
1053     ServerInfo.network_desc = xstrdup(NETWORK_DESC_DEFAULT);
1054 adx 30
1055     memset(&ServerInfo.ip, 0, sizeof(ServerInfo.ip));
1056     ServerInfo.specific_ipv4_vhost = 0;
1057     memset(&ServerInfo.ip6, 0, sizeof(ServerInfo.ip6));
1058     ServerInfo.specific_ipv6_vhost = 0;
1059    
1060     ServerInfo.max_clients = MAXCLIENTS_MAX;
1061 michael 1751 ServerInfo.max_nick_length = 9;
1062     ServerInfo.max_topic_length = 80;
1063 michael 956 ServerInfo.hub = 0;
1064 adx 30 ServerInfo.dns_host.sin_addr.s_addr = 0;
1065     ServerInfo.dns_host.sin_port = 0;
1066 michael 4313
1067 adx 30 AdminInfo.name = NULL;
1068     AdminInfo.email = NULL;
1069     AdminInfo.description = NULL;
1070    
1071 michael 1831 log_del_all();
1072 michael 1247
1073 adx 30 ConfigLoggingEntry.use_logging = 1;
1074    
1075 michael 1243 ConfigChannel.disable_fake_channels = 0;
1076 michael 3860 ConfigChannel.invite_client_count = 10;
1077     ConfigChannel.invite_client_time = 300;
1078     ConfigChannel.knock_client_count = 1;
1079     ConfigChannel.knock_client_time = 300;
1080 adx 30 ConfigChannel.knock_delay_channel = 60;
1081 michael 3933 ConfigChannel.max_channels = 25;
1082 adx 30 ConfigChannel.max_bans = 25;
1083     ConfigChannel.default_split_user_count = 0;
1084     ConfigChannel.default_split_server_count = 0;
1085 michael 1243 ConfigChannel.no_join_on_split = 0;
1086     ConfigChannel.no_create_on_split = 0;
1087 adx 30
1088 michael 1243 ConfigServerHide.flatten_links = 0;
1089 adx 30 ConfigServerHide.links_delay = 300;
1090 michael 1243 ConfigServerHide.hidden = 0;
1091     ConfigServerHide.hide_servers = 0;
1092 michael 1851 ConfigServerHide.hide_services = 0;
1093 michael 1646 ConfigServerHide.hidden_name = xstrdup(NETWORK_NAME_DEFAULT);
1094 michael 1243 ConfigServerHide.hide_server_ips = 0;
1095 michael 2196 ConfigServerHide.disable_remote_commands = 0;
1096 adx 30
1097 michael 4313 ConfigFileEntry.away_count = 2;
1098     ConfigFileEntry.away_time = 10;
1099 michael 1646 ConfigFileEntry.service_name = xstrdup(SERVICE_NAME_DEFAULT);
1100 michael 876 ConfigFileEntry.max_watch = WATCHSIZE_DEFAULT;
1101 michael 2283 ConfigFileEntry.cycle_on_host_change = 1;
1102 michael 1459 ConfigFileEntry.glines = 0;
1103     ConfigFileEntry.gline_time = 12 * 3600;
1104     ConfigFileEntry.gline_request_time = GLINE_REQUEST_EXPIRE_DEFAULT;
1105 adx 30 ConfigFileEntry.gline_min_cidr = 16;
1106     ConfigFileEntry.gline_min_cidr6 = 48;
1107 michael 1243 ConfigFileEntry.invisible_on_connect = 1;
1108     ConfigFileEntry.tkline_expire_notices = 1;
1109     ConfigFileEntry.hide_spoof_ips = 1;
1110     ConfigFileEntry.ignore_bogus_ts = 0;
1111     ConfigFileEntry.disable_auth = 0;
1112 adx 30 ConfigFileEntry.kill_chase_time_limit = 90;
1113 michael 1119 ConfigFileEntry.default_floodcount = 8;
1114 michael 1243 ConfigFileEntry.failed_oper_notice = 1;
1115 michael 1119 ConfigFileEntry.dots_in_ident = 0;
1116 adx 30 ConfigFileEntry.min_nonwildcard = 4;
1117     ConfigFileEntry.min_nonwildcard_simple = 3;
1118     ConfigFileEntry.max_accept = 20;
1119 michael 1243 ConfigFileEntry.anti_nick_flood = 0;
1120 adx 30 ConfigFileEntry.max_nick_time = 20;
1121     ConfigFileEntry.max_nick_changes = 5;
1122 michael 1119 ConfigFileEntry.anti_spam_exit_message_time = 0;
1123 adx 30 ConfigFileEntry.ts_warn_delta = TS_WARN_DELTA_DEFAULT;
1124 michael 1119 ConfigFileEntry.ts_max_delta = TS_MAX_DELTA_DEFAULT;
1125 michael 3473 ConfigFileEntry.warn_no_connect_block = 1;
1126 michael 3780 ConfigFileEntry.stats_e_disabled = 0;
1127 michael 1243 ConfigFileEntry.stats_o_oper_only = 0;
1128 michael 3375 ConfigFileEntry.stats_k_oper_only = 1; /* 1 = masked */
1129     ConfigFileEntry.stats_i_oper_only = 1; /* 1 = masked */
1130 michael 1243 ConfigFileEntry.stats_P_oper_only = 0;
1131 michael 2269 ConfigFileEntry.stats_u_oper_only = 0;
1132 adx 30 ConfigFileEntry.caller_id_wait = 60;
1133 michael 1243 ConfigFileEntry.opers_bypass_callerid = 0;
1134 adx 30 ConfigFileEntry.pace_wait = 10;
1135     ConfigFileEntry.pace_wait_simple = 1;
1136 michael 1243 ConfigFileEntry.short_motd = 0;
1137     ConfigFileEntry.ping_cookie = 0;
1138     ConfigFileEntry.no_oper_flood = 0;
1139     ConfigFileEntry.true_no_oper_flood = 0;
1140     ConfigFileEntry.oper_pass_resv = 1;
1141 adx 30 ConfigFileEntry.max_targets = MAX_TARGETS_DEFAULT;
1142 michael 1119 ConfigFileEntry.oper_only_umodes = UMODE_DEBUG;
1143 michael 3869 ConfigFileEntry.oper_umodes = UMODE_BOTS | UMODE_LOCOPS | UMODE_SERVNOTICE | UMODE_WALLOP;
1144 michael 4068 ConfigFileEntry.throttle_count = 1;
1145     ConfigFileEntry.throttle_time = 1;
1146 adx 30 }
1147    
1148     static void
1149     validate_conf(void)
1150     {
1151     if (ConfigFileEntry.ts_warn_delta < TS_WARN_DELTA_MIN)
1152     ConfigFileEntry.ts_warn_delta = TS_WARN_DELTA_DEFAULT;
1153    
1154     if (ConfigFileEntry.ts_max_delta < TS_MAX_DELTA_MIN)
1155     ConfigFileEntry.ts_max_delta = TS_MAX_DELTA_DEFAULT;
1156    
1157     if (ServerInfo.network_name == NULL)
1158 michael 1646 ServerInfo.network_name = xstrdup(NETWORK_NAME_DEFAULT);
1159 adx 30
1160     if (ServerInfo.network_desc == NULL)
1161 michael 1646 ServerInfo.network_desc = xstrdup(NETWORK_DESC_DEFAULT);
1162 adx 30
1163 michael 1157 if (ConfigFileEntry.service_name == NULL)
1164 michael 1646 ConfigFileEntry.service_name = xstrdup(SERVICE_NAME_DEFAULT);
1165 michael 1157
1166 michael 876 ConfigFileEntry.max_watch = IRCD_MAX(ConfigFileEntry.max_watch, WATCHSIZE_MIN);
1167 adx 30 }
1168    
1169 michael 2916 /* read_conf()
1170 michael 1363 *
1171     * inputs - file descriptor pointing to config file to use
1172     * output - None
1173     * side effects - Read configuration file.
1174     */
1175     static void
1176     read_conf(FILE *file)
1177     {
1178     lineno = 0;
1179    
1180 michael 3375 set_default_conf(); /* Set default values prior to conf parsing */
1181 michael 1363 conf_parser_ctx.pass = 1;
1182 michael 3375 yyparse(); /* Pick up the classes first */
1183 michael 1363
1184     rewind(file);
1185    
1186     conf_parser_ctx.pass = 2;
1187 michael 3375 yyparse(); /* Load the values from the conf */
1188     validate_conf(); /* Check to make sure some values are still okay. */
1189     /* Some global values are also loaded here. */
1190     class_delete_marked(); /* Delete unused classes that are marked for deletion */
1191 michael 1363 }
1192    
1193 adx 30 /* lookup_confhost()
1194     *
1195     * start DNS lookups of all hostnames in the conf
1196     * line and convert an IP addresses in a.b.c.d number for to IP#s.
1197     */
1198 michael 1647 void
1199 michael 1632 lookup_confhost(struct MaskItem *conf)
1200 adx 30 {
1201     struct addrinfo hints, *res;
1202    
1203 michael 3375 /*
1204     * Do name lookup now on hostnames given and store the
1205 adx 30 * ip numbers in conf structure.
1206     */
1207     memset(&hints, 0, sizeof(hints));
1208    
1209     hints.ai_family = AF_UNSPEC;
1210     hints.ai_socktype = SOCK_STREAM;
1211    
1212     /* Get us ready for a bind() and don't bother doing dns lookup */
1213     hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
1214    
1215 michael 1632 if (getaddrinfo(conf->host, NULL, &hints, &res))
1216 adx 30 {
1217 michael 1632 conf_dns_lookup(conf);
1218 adx 30 return;
1219     }
1220    
1221 michael 3235 assert(res);
1222 adx 30
1223 michael 1632 memcpy(&conf->addr, res->ai_addr, res->ai_addrlen);
1224     conf->addr.ss_len = res->ai_addrlen;
1225     conf->addr.ss.ss_family = res->ai_family;
1226    
1227 michael 1123 freeaddrinfo(res);
1228 adx 30 }
1229    
1230     /* conf_connect_allowed()
1231     *
1232     * inputs - pointer to inaddr
1233     * - int type ipv4 or ipv6
1234     * output - BANNED or accepted
1235     * side effects - none
1236     */
1237     int
1238     conf_connect_allowed(struct irc_ssaddr *addr, int aftype)
1239     {
1240 michael 3877 struct ip_entry *ip_found = NULL;
1241 michael 1632 struct MaskItem *conf = find_dline_conf(addr, aftype);
1242 adx 30
1243     /* DLINE exempt also gets you out of static limits/pacing... */
1244 michael 1636 if (conf && (conf->type == CONF_EXEMPT))
1245 adx 30 return 0;
1246    
1247 michael 3235 if (conf)
1248 adx 30 return BANNED_CLIENT;
1249    
1250     ip_found = find_or_add_ip(addr);
1251    
1252 michael 3877 if ((CurrentTime - ip_found->last_attempt) < ConfigFileEntry.throttle_time)
1253 adx 30 {
1254 michael 3877 if (ip_found->connection_count >= ConfigFileEntry.throttle_count)
1255     return TOO_FAST;
1256 michael 4055
1257     ++ip_found->connection_count;
1258 adx 30 }
1259 michael 3877 else
1260     ip_found->connection_count = 1;
1261 adx 30
1262     ip_found->last_attempt = CurrentTime;
1263     return 0;
1264     }
1265    
1266     /* cleanup_tklines()
1267     *
1268     * inputs - NONE
1269     * output - NONE
1270     * side effects - call function to expire temporary k/d lines
1271     * This is an event started off in ircd.c
1272     */
1273     void
1274     cleanup_tklines(void *notused)
1275     {
1276 michael 1369 hostmask_expire_temporary();
1277 michael 2916 expire_tklines(&xconf_items);
1278 michael 1628 expire_tklines(&nresv_items);
1279 michael 2174 expire_tklines(&cresv_items);
1280 adx 30 }
1281    
1282     /* expire_tklines()
1283     *
1284     * inputs - tkline list pointer
1285     * output - NONE
1286     * side effects - expire tklines
1287     */
1288     static void
1289     expire_tklines(dlink_list *tklist)
1290     {
1291 michael 3235 dlink_node *ptr = NULL, *ptr_next = NULL;
1292     struct MaskItem *conf = NULL;
1293 adx 30
1294 michael 3235 DLINK_FOREACH_SAFE(ptr, ptr_next, tklist->head)
1295 adx 30 {
1296     conf = ptr->data;
1297    
1298 michael 1649 if (!conf->until || conf->until > CurrentTime)
1299 michael 1644 continue;
1300    
1301 michael 1632 if (conf->type == CONF_XLINE)
1302 adx 30 {
1303 michael 1644 if (ConfigFileEntry.tkline_expire_notices)
1304     sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1305 michael 1618 "Temporary X-line for [%s] expired", conf->name);
1306 michael 1644 conf_free(conf);
1307 adx 30 }
1308 michael 1825 else if (conf->type == CONF_NRESV || conf->type == CONF_CRESV)
1309 adx 30 {
1310 michael 1644 if (ConfigFileEntry.tkline_expire_notices)
1311     sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1312 adx 30 "Temporary RESV for [%s] expired", conf->name);
1313 michael 1644 conf_free(conf);
1314 adx 30 }
1315     }
1316     }
1317    
1318     /* oper_privs_as_string()
1319     *
1320 michael 58 * inputs - pointer to client_p
1321 adx 30 * output - pointer to static string showing oper privs
1322     * side effects - return as string, the oper privs as derived from port
1323     */
1324 michael 58 static const struct oper_privs
1325     {
1326 michael 1644 const unsigned int flag;
1327 michael 58 const unsigned char c;
1328     } flag_list[] = {
1329 michael 2012 { OPER_FLAG_ADMIN, 'A' },
1330     { OPER_FLAG_REMOTEBAN, 'B' },
1331     { OPER_FLAG_DIE, 'D' },
1332     { OPER_FLAG_GLINE, 'G' },
1333     { OPER_FLAG_REHASH, 'H' },
1334 michael 4019 { OPER_FLAG_KLINE, 'K' },
1335 michael 2012 { OPER_FLAG_KILL, 'N' },
1336     { OPER_FLAG_KILL_REMOTE, 'O' },
1337     { OPER_FLAG_CONNECT, 'P' },
1338     { OPER_FLAG_CONNECT_REMOTE, 'Q' },
1339     { OPER_FLAG_SQUIT, 'R' },
1340     { OPER_FLAG_SQUIT_REMOTE, 'S' },
1341     { OPER_FLAG_UNKLINE, 'U' },
1342 michael 2852 { OPER_FLAG_XLINE, 'X' },
1343 michael 1294 { 0, '\0' }
1344 michael 58 };
1345 adx 30
1346     char *
1347     oper_privs_as_string(const unsigned int port)
1348     {
1349 michael 2694 static char privs_out[IRCD_BUFSIZE];
1350 michael 58 char *privs_ptr = privs_out;
1351 adx 30
1352 michael 3283 for (const struct oper_privs *opriv = flag_list; opriv->flag; ++opriv)
1353 michael 58 {
1354 michael 1644 if (port & opriv->flag)
1355     *privs_ptr++ = opriv->c;
1356 michael 58 else
1357 michael 1644 *privs_ptr++ = ToLower(opriv->c);
1358 michael 58 }
1359    
1360     *privs_ptr = '\0';
1361    
1362 adx 30 return privs_out;
1363     }
1364    
1365     /*
1366 michael 4298 * Input: A client to find the active operator {} name for.
1367 adx 30 * Output: The nick!user@host{oper} of the oper.
1368     * "oper" is server name for remote opers
1369     * Side effects: None.
1370     */
1371 michael 1364 const char *
1372 adx 30 get_oper_name(const struct Client *client_p)
1373     {
1374 michael 3009 const dlink_node *cnode = NULL;
1375 adx 30 /* +5 for !,@,{,} and null */
1376 michael 1147 static char buffer[NICKLEN + USERLEN + HOSTLEN + HOSTLEN + 5];
1377 adx 30
1378     if (MyConnect(client_p))
1379     {
1380 michael 1364 if ((cnode = client_p->localClient->confs.head))
1381 adx 30 {
1382 michael 3009 const struct MaskItem *conf = cnode->data;
1383 adx 30
1384 michael 1632 if (IsConfOperator(conf))
1385 adx 30 {
1386 michael 2182 snprintf(buffer, sizeof(buffer), "%s!%s@%s{%s}", client_p->name,
1387 michael 1147 client_p->username, client_p->host, conf->name);
1388 michael 2182 return buffer;
1389 adx 30 }
1390     }
1391    
1392 michael 4298 /*
1393     * Probably should assert here for now. If there is an oper out there
1394     * with no operator {} conf attached, it would be good for us to know...
1395 adx 30 */
1396 michael 4298 assert(0); /* Oper without oper conf! */
1397 adx 30 }
1398    
1399 michael 1147 snprintf(buffer, sizeof(buffer), "%s!%s@%s{%s}", client_p->name,
1400 michael 2182 client_p->username, client_p->host, client_p->servptr->name);
1401 adx 30 return buffer;
1402     }
1403    
1404     /* read_conf_files()
1405     *
1406     * inputs - cold start YES or NO
1407     * output - none
1408     * side effects - read all conf files needed, ircd.conf kline.conf etc.
1409     */
1410     void
1411     read_conf_files(int cold)
1412     {
1413 michael 3375 const char *filename = NULL;
1414     char chanmodes[IRCD_BUFSIZE] = "";
1415     char chanlimit[IRCD_BUFSIZE] = "";
1416 adx 30
1417 michael 967 conf_parser_ctx.boot = cold;
1418 michael 1622 filename = ConfigFileEntry.configfile;
1419 adx 30
1420     /* We need to know the initial filename for the yyerror() to report
1421     FIXME: The full path is in conffilenamebuf first time since we
1422 michael 4298 don't know anything else
1423 adx 30
1424 michael 2916 - Gozem 2002-07-21
1425 adx 30 */
1426     strlcpy(conffilebuf, filename, sizeof(conffilebuf));
1427    
1428 michael 1325 if ((conf_parser_ctx.conf_file = fopen(filename, "r")) == NULL)
1429 adx 30 {
1430     if (cold)
1431     {
1432 michael 1247 ilog(LOG_TYPE_IRCD, "Unable to read configuration file '%s': %s",
1433 adx 30 filename, strerror(errno));
1434     exit(-1);
1435     }
1436     else
1437     {
1438 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1439 michael 2182 "Unable to read configuration file '%s': %s",
1440     filename, strerror(errno));
1441 adx 30 return;
1442     }
1443     }
1444    
1445     if (!cold)
1446     clear_out_old_conf();
1447    
1448 michael 967 read_conf(conf_parser_ctx.conf_file);
1449 michael 1325 fclose(conf_parser_ctx.conf_file);
1450 adx 30
1451 michael 1831 log_reopen_all();
1452    
1453 michael 1751 add_isupport("NICKLEN", NULL, ServerInfo.max_nick_length);
1454 adx 30 add_isupport("NETWORK", ServerInfo.network_name, -1);
1455 michael 1751
1456     snprintf(chanmodes, sizeof(chanmodes), "beI:%d", ConfigChannel.max_bans);
1457 adx 30 add_isupport("MAXLIST", chanmodes, -1);
1458     add_isupport("MAXTARGETS", NULL, ConfigFileEntry.max_targets);
1459 michael 1474 add_isupport("CHANTYPES", "#", -1);
1460 michael 1147
1461 michael 1474 snprintf(chanlimit, sizeof(chanlimit), "#:%d",
1462 michael 3933 ConfigChannel.max_channels);
1463 adx 30 add_isupport("CHANLIMIT", chanlimit, -1);
1464 michael 4054 snprintf(chanmodes, sizeof(chanmodes), "%s", "beI,k,l,cimnprstMORS");
1465 michael 3421 add_isupport("CHANNELLEN", NULL, CHANNELLEN);
1466 michael 1751 add_isupport("TOPICLEN", NULL, ServerInfo.max_topic_length);
1467 adx 30 add_isupport("CHANMODES", chanmodes, -1);
1468    
1469     /*
1470     * message_locale may have changed. rebuild isupport since it relies
1471     * on strlen(form_str(RPL_ISUPPORT))
1472     */
1473     rebuild_isupport_message_line();
1474     }
1475    
1476     /* clear_out_old_conf()
1477     *
1478     * inputs - none
1479     * output - none
1480     * side effects - Clear out the old configuration
1481     */
1482     static void
1483     clear_out_old_conf(void)
1484     {
1485     dlink_node *ptr = NULL, *next_ptr = NULL;
1486 michael 1632 struct MaskItem *conf;
1487 adx 30 dlink_list *free_items [] = {
1488 michael 1383 &server_items, &oconf_items,
1489 michael 1921 &uconf_items, &xconf_items,
1490 michael 2174 &nresv_items, &cluster_items, &service_items, &cresv_items, NULL
1491 adx 30 };
1492    
1493     dlink_list ** iterator = free_items; /* C is dumb */
1494    
1495     /* We only need to free anything allocated by yyparse() here.
1496     * Resetting structs, etc, is taken care of by set_default_conf().
1497     */
1498 michael 2916
1499 adx 30 for (; *iterator != NULL; iterator++)
1500     {
1501     DLINK_FOREACH_SAFE(ptr, next_ptr, (*iterator)->head)
1502     {
1503     conf = ptr->data;
1504 michael 1632
1505     dlinkDelete(&conf->node, map_to_list(conf->type));
1506    
1507 adx 30 /* XXX This is less than pretty */
1508 michael 1632 if (conf->type == CONF_SERVER || conf->type == CONF_OPER)
1509 adx 30 {
1510 michael 1644 if (!conf->ref_count)
1511 michael 2182 conf_free(conf);
1512 adx 30 }
1513 michael 1921 else if (conf->type == CONF_XLINE)
1514 adx 30 {
1515 michael 1649 if (!conf->until)
1516 michael 1632 conf_free(conf);
1517 adx 30 }
1518     else
1519 michael 1632 conf_free(conf);
1520 adx 30 }
1521     }
1522    
1523 michael 2150 motd_clear();
1524    
1525 michael 671 /*
1526     * don't delete the class table, rather mark all entries
1527 michael 1632 * for deletion. The table is cleaned up by class_delete_marked. - avalon
1528 adx 30 */
1529 michael 1632 class_mark_for_deletion();
1530 michael 671
1531 adx 30 clear_out_address_conf();
1532    
1533     /* clean out module paths */
1534     mod_clear_paths();
1535    
1536     /* clean out ServerInfo */
1537     MyFree(ServerInfo.description);
1538     ServerInfo.description = NULL;
1539     MyFree(ServerInfo.network_name);
1540     ServerInfo.network_name = NULL;
1541     MyFree(ServerInfo.network_desc);
1542     ServerInfo.network_desc = NULL;
1543     #ifdef HAVE_LIBCRYPTO
1544 michael 3235 if (ServerInfo.rsa_private_key)
1545 adx 30 {
1546     RSA_free(ServerInfo.rsa_private_key);
1547     ServerInfo.rsa_private_key = NULL;
1548     }
1549    
1550     MyFree(ServerInfo.rsa_private_key_file);
1551     ServerInfo.rsa_private_key_file = NULL;
1552     #endif
1553    
1554     /* clean out AdminInfo */
1555     MyFree(AdminInfo.name);
1556     AdminInfo.name = NULL;
1557     MyFree(AdminInfo.email);
1558     AdminInfo.email = NULL;
1559     MyFree(AdminInfo.description);
1560     AdminInfo.description = NULL;
1561    
1562     /* clean out listeners */
1563     close_listeners();
1564    
1565     /* clean out general */
1566 michael 1157 MyFree(ConfigFileEntry.service_name);
1567     ConfigFileEntry.service_name = NULL;
1568 adx 30 }
1569    
1570     /* conf_add_class_to_conf()
1571     *
1572     * inputs - pointer to config item
1573     * output - NONE
1574 michael 2916 * side effects - Add a class pointer to a conf
1575 adx 30 */
1576     void
1577 michael 1632 conf_add_class_to_conf(struct MaskItem *conf, const char *class_name)
1578 adx 30 {
1579 michael 2916 if (class_name == NULL)
1580 adx 30 {
1581 michael 1632 conf->class = class_default;
1582 michael 671
1583 michael 1632 if (conf->type == CONF_CLIENT)
1584 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1585 michael 2182 "Warning *** Defaulting to default class for %s@%s",
1586     conf->user, conf->host);
1587 adx 30 else
1588 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1589 michael 2182 "Warning *** Defaulting to default class for %s",
1590     conf->name);
1591 adx 30 }
1592     else
1593 michael 1632 conf->class = class_find(class_name, 1);
1594 adx 30
1595 michael 1632 if (conf->class == NULL)
1596 adx 30 {
1597 michael 1632 if (conf->type == CONF_CLIENT)
1598 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1599 michael 2182 "Warning *** Defaulting to default class for %s@%s",
1600     conf->user, conf->host);
1601 adx 30 else
1602 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1603 michael 2182 "Warning *** Defaulting to default class for %s",
1604     conf->name);
1605 michael 1632 conf->class = class_default;
1606 adx 30 }
1607     }
1608    
1609     /* yyerror()
1610     *
1611     * inputs - message from parser
1612     * output - NONE
1613     * side effects - message to opers and log file entry is made
1614     */
1615     void
1616     yyerror(const char *msg)
1617     {
1618     char newlinebuf[IRCD_BUFSIZE];
1619    
1620 michael 967 if (conf_parser_ctx.pass != 1)
1621 adx 30 return;
1622    
1623     strip_tabs(newlinebuf, linebuf, sizeof(newlinebuf));
1624 michael 1751 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1625 michael 1618 "\"%s\", line %u: %s: %s",
1626 adx 30 conffilebuf, lineno + 1, msg, newlinebuf);
1627 michael 1247 ilog(LOG_TYPE_IRCD, "\"%s\", line %u: %s: %s",
1628 adx 30 conffilebuf, lineno + 1, msg, newlinebuf);
1629     }
1630    
1631 michael 1751 void
1632     conf_error_report(const char *msg)
1633     {
1634     char newlinebuf[IRCD_BUFSIZE];
1635    
1636     strip_tabs(newlinebuf, linebuf, sizeof(newlinebuf));
1637     sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1638     "\"%s\", line %u: %s: %s",
1639     conffilebuf, lineno + 1, msg, newlinebuf);
1640     ilog(LOG_TYPE_IRCD, "\"%s\", line %u: %s: %s",
1641     conffilebuf, lineno + 1, msg, newlinebuf);
1642     }
1643    
1644 adx 30 /*
1645     * valid_tkline()
1646 michael 2916 *
1647 adx 30 * inputs - pointer to ascii string to check
1648     * - whether the specified time is in seconds or minutes
1649     * output - -1 not enough parameters
1650     * - 0 if not an integer number, else the number
1651     * side effects - none
1652     * Originally written by Dianora (Diane, db@db.net)
1653     */
1654     time_t
1655 michael 2313 valid_tkline(const char *data, const int minutes)
1656 adx 30 {
1657 michael 2313 const unsigned char *p = (const unsigned char *)data;
1658     unsigned char tmpch = '\0';
1659 adx 30 time_t result = 0;
1660    
1661 michael 2313 while ((tmpch = *p++))
1662 adx 30 {
1663 michael 2313 if (!IsDigit(tmpch))
1664 michael 1121 return 0;
1665 michael 1120
1666     result *= 10;
1667 michael 2313 result += (tmpch & 0xF);
1668 adx 30 }
1669    
1670 michael 1120 /*
1671 michael 2916 * In the degenerate case where oper does a /quote kline 0 user@host :reason
1672 adx 30 * i.e. they specifically use 0, I am going to return 1 instead
1673     * as a return value of non-zero is used to flag it as a temporary kline
1674     */
1675     if (result == 0)
1676     result = 1;
1677    
1678 michael 2916 /*
1679 adx 30 * If the incoming time is in seconds convert it to minutes for the purpose
1680     * of this calculation
1681     */
1682     if (!minutes)
1683 michael 2916 result = result / 60;
1684 adx 30
1685     if (result > MAX_TDKLINE_TIME)
1686     result = MAX_TDKLINE_TIME;
1687    
1688 michael 2313 result = result * 60; /* turn it into seconds */
1689 adx 30
1690     return result;
1691     }
1692    
1693 michael 2130 /* valid_wild_card_simple()
1694     *
1695     * inputs - data to check for sufficient non-wildcard characters
1696     * outputs - 1 if valid, else 0
1697     * side effects - none
1698     */
1699     int
1700     valid_wild_card_simple(const char *data)
1701     {
1702     const unsigned char *p = (const unsigned char *)data;
1703     unsigned char tmpch = '\0';
1704 michael 3870 unsigned int nonwild = 0;
1705 michael 2130
1706     while ((tmpch = *p++))
1707     {
1708 michael 4265 if (tmpch == '\\' && *p)
1709 michael 2130 {
1710     ++p;
1711     if (++nonwild >= ConfigFileEntry.min_nonwildcard_simple)
1712     return 1;
1713     }
1714     else if (!IsMWildChar(tmpch))
1715     {
1716     if (++nonwild >= ConfigFileEntry.min_nonwildcard_simple)
1717     return 1;
1718     }
1719     }
1720    
1721     return 0;
1722     }
1723    
1724 adx 30 /* valid_wild_card()
1725     *
1726     * input - pointer to client
1727     * - int flag, 0 for no warning oper 1 for warning oper
1728     * - count of following varargs to check
1729     * output - 0 if not valid, 1 if valid
1730     * side effects - NOTICE is given to source_p if warn is 1
1731     */
1732     int
1733     valid_wild_card(struct Client *source_p, int warn, int count, ...)
1734     {
1735 michael 3931 unsigned char tmpch = '\0';
1736 michael 3870 unsigned int nonwild = 0;
1737 adx 30 va_list args;
1738    
1739     /*
1740     * Now we must check the user and host to make sure there
1741     * are at least NONWILDCHARS non-wildcard characters in
1742     * them, otherwise assume they are attempting to kline
1743     * *@* or some variant of that. This code will also catch
1744     * people attempting to kline *@*.tld, as long as NONWILDCHARS
1745     * is greater than 3. In that case, there are only 3 non-wild
1746     * characters (tld), so if NONWILDCHARS is 4, the kline will
1747     * be disallowed.
1748     * -wnder
1749     */
1750    
1751     va_start(args, count);
1752    
1753     while (count--)
1754     {
1755 michael 3931 const unsigned char *p = va_arg(args, const unsigned char *);
1756 adx 30 if (p == NULL)
1757     continue;
1758    
1759     while ((tmpch = *p++))
1760     {
1761     if (!IsKWildChar(tmpch))
1762     {
1763     /*
1764     * If we find enough non-wild characters, we can
1765     * break - no point in searching further.
1766     */
1767     if (++nonwild >= ConfigFileEntry.min_nonwildcard)
1768 michael 2659 {
1769     va_end(args);
1770 adx 30 return 1;
1771 michael 2659 }
1772 adx 30 }
1773     }
1774     }
1775    
1776     if (warn)
1777 michael 3110 sendto_one_notice(source_p, &me,
1778 michael 4058 ":Please include at least %u non-wildcard characters with the mask",
1779 michael 3110 ConfigFileEntry.min_nonwildcard);
1780 michael 2659 va_end(args);
1781 adx 30 return 0;
1782     }
1783    
1784     /* XXX should this go into a separate file ? -Dianora */
1785     /* parse_aline
1786     *
1787     * input - pointer to cmd name being used
1788     * - pointer to client using cmd
1789     * - parc parameter count
1790     * - parv[] list of parameters to parse
1791     * - parse_flags bit map of things to test
1792     * - pointer to user or string to parse into
1793     * - pointer to host or NULL to parse into if non NULL
1794 michael 2916 * - pointer to optional tkline time or NULL
1795 adx 30 * - pointer to target_server to parse into if non NULL
1796     * - pointer to reason to parse into
1797     *
1798     * output - 1 if valid, -1 if not valid
1799     * side effects - A generalised k/d/x etc. line parser,
1800     * "ALINE [time] user@host|string [ON] target :reason"
1801     * will parse returning a parsed user, host if
1802     * h_p pointer is non NULL, string otherwise.
1803     * if tkline_time pointer is non NULL a tk line will be set
1804     * to non zero if found.
1805     * if tkline_time pointer is NULL and tk line is found,
1806     * error is reported.
1807     * if target_server is NULL and an "ON" is found error
1808     * is reported.
1809     * if reason pointer is NULL ignore pointer,
1810 db 936 * this allows use of parse_a_line in unkline etc.
1811 adx 30 *
1812     * - Dianora
1813     */
1814     int
1815     parse_aline(const char *cmd, struct Client *source_p,
1816 michael 2182 int parc, char **parv,
1817     int parse_flags, char **up_p, char **h_p, time_t *tkline_time,
1818     char **target_server, char **reason)
1819 adx 30 {
1820     int found_tkline_time=0;
1821 michael 3499 static char def_reason[] = CONF_NOREASON;
1822 adx 30 static char user[USERLEN*4+1];
1823     static char host[HOSTLEN*4+1];
1824    
1825     parv++;
1826     parc--;
1827    
1828     found_tkline_time = valid_tkline(*parv, TK_MINUTES);
1829    
1830     if (found_tkline_time != 0)
1831     {
1832     parv++;
1833     parc--;
1834    
1835     if (tkline_time != NULL)
1836     *tkline_time = found_tkline_time;
1837     else
1838     {
1839 michael 3110 sendto_one_notice(source_p, &me, ":temp_line not supported by %s", cmd);
1840 adx 30 return -1;
1841     }
1842     }
1843    
1844     if (parc == 0)
1845     {
1846 michael 3109 sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, cmd);
1847 adx 30 return -1;
1848     }
1849    
1850     if (h_p == NULL)
1851     *up_p = *parv;
1852     else
1853     {
1854     if (find_user_host(source_p, *parv, user, host, parse_flags) == 0)
1855     return -1;
1856    
1857     *up_p = user;
1858     *h_p = host;
1859     }
1860 michael 2916
1861 adx 30 parc--;
1862     parv++;
1863    
1864     if (parc != 0)
1865     {
1866     if (irccmp(*parv, "ON") == 0)
1867     {
1868     parc--;
1869     parv++;
1870    
1871     if (target_server == NULL)
1872     {
1873 michael 3110 sendto_one_notice(source_p, &me, ":ON server not supported by %s", cmd);
1874 michael 2182 return -1;
1875 adx 30 }
1876    
1877 michael 1219 if (!HasOFlag(source_p, OPER_FLAG_REMOTEBAN))
1878 adx 30 {
1879 michael 3109 sendto_one_numeric(source_p, &me, ERR_NOPRIVS, "remoteban");
1880 adx 30 return -1;
1881     }
1882    
1883     if (parc == 0 || EmptyString(*parv))
1884     {
1885 michael 3109 sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, cmd);
1886 michael 2182 return -1;
1887 adx 30 }
1888    
1889     *target_server = *parv;
1890     parc--;
1891     parv++;
1892     }
1893     else
1894     {
1895     /* Make sure target_server *is* NULL if no ON server found
1896     * caller probably NULL'd it first, but no harm to do it again -db
1897     */
1898     if (target_server != NULL)
1899 michael 2182 *target_server = NULL;
1900 adx 30 }
1901     }
1902    
1903     if (h_p != NULL)
1904     {
1905     if (strchr(user, '!') != NULL)
1906     {
1907 michael 3110 sendto_one_notice(source_p, &me, ":Invalid character '!' in kline");
1908 adx 30 return -1;
1909     }
1910    
1911 michael 1243 if ((parse_flags & AWILD) && !valid_wild_card(source_p, 1, 2, *up_p, *h_p))
1912 adx 30 return -1;
1913     }
1914     else
1915 michael 1243 if ((parse_flags & AWILD) && !valid_wild_card(source_p, 1, 1, *up_p))
1916 adx 30 return -1;
1917    
1918     if (reason != NULL)
1919     {
1920 michael 867 if (parc != 0 && !EmptyString(*parv))
1921 adx 30 {
1922     *reason = *parv;
1923 michael 1243 if (!valid_comment(source_p, *reason, 1))
1924 michael 2182 return -1;
1925 adx 30 }
1926     else
1927     *reason = def_reason;
1928     }
1929    
1930     return 1;
1931     }
1932    
1933     /* find_user_host()
1934     *
1935     * inputs - pointer to client placing kline
1936     * - pointer to user_host_or_nick
1937     * - pointer to user buffer
1938     * - pointer to host buffer
1939     * output - 0 if not ok to kline, 1 to kline i.e. if valid user host
1940     * side effects -
1941     */
1942     static int
1943     find_user_host(struct Client *source_p, char *user_host_or_nick,
1944     char *luser, char *lhost, unsigned int flags)
1945     {
1946     struct Client *target_p = NULL;
1947     char *hostp = NULL;
1948    
1949     if (lhost == NULL)
1950     {
1951     strlcpy(luser, user_host_or_nick, USERLEN*4 + 1);
1952     return 1;
1953     }
1954    
1955     if ((hostp = strchr(user_host_or_nick, '@')) || *user_host_or_nick == '*')
1956     {
1957     /* Explicit user@host mask given */
1958    
1959 michael 593 if (hostp != NULL) /* I'm a little user@host */
1960 adx 30 {
1961     *(hostp++) = '\0'; /* short and squat */
1962     if (*user_host_or_nick)
1963 michael 2182 strlcpy(luser, user_host_or_nick, USERLEN*4 + 1); /* here is my user */
1964 adx 30 else
1965 michael 2182 strcpy(luser, "*");
1966    
1967 adx 30 if (*hostp)
1968 michael 2182 strlcpy(lhost, hostp, HOSTLEN + 1); /* here is my host */
1969 adx 30 else
1970 michael 2182 strcpy(lhost, "*");
1971 adx 30 }
1972     else
1973     {
1974     luser[0] = '*'; /* no @ found, assume its *@somehost */
1975 michael 2182 luser[1] = '\0';
1976 adx 30 strlcpy(lhost, user_host_or_nick, HOSTLEN*4 + 1);
1977     }
1978 michael 2916
1979 adx 30 return 1;
1980     }
1981 michael 1518 else
1982 adx 30 {
1983     /* Try to find user@host mask from nick */
1984     /* Okay to use source_p as the first param, because source_p == client_p */
1985 michael 2916 if ((target_p =
1986 michael 3192 find_chasing(source_p, user_host_or_nick)) == NULL)
1987 michael 3737 return 0; /* find_chasing sends ERR_NOSUCHNICK */
1988 adx 30
1989     if (IsExemptKline(target_p))
1990     {
1991     if (!IsServer(source_p))
1992 michael 3110 sendto_one_notice(source_p, &me, ":%s is E-lined", target_p->name);
1993 adx 30 return 0;
1994     }
1995    
1996     /*
1997     * turn the "user" bit into "*user", blow away '~'
1998     * if found in original user name (non-idented)
1999     */
2000     strlcpy(luser, target_p->username, USERLEN*4 + 1);
2001    
2002     if (target_p->username[0] == '~')
2003     luser[0] = '*';
2004    
2005     if (target_p->sockhost[0] == '\0' ||
2006     (target_p->sockhost[0] == '0' && target_p->sockhost[1] == '\0'))
2007     strlcpy(lhost, target_p->host, HOSTLEN*4 + 1);
2008     else
2009     strlcpy(lhost, target_p->sockhost, HOSTLEN*4 + 1);
2010     return 1;
2011     }
2012    
2013     return 0;
2014     }
2015    
2016     /* valid_comment()
2017     *
2018     * inputs - pointer to client
2019     * - pointer to comment
2020     * output - 0 if no valid comment,
2021     * - 1 if valid
2022     * side effects - truncates reason where necessary
2023     */
2024     int
2025     valid_comment(struct Client *source_p, char *comment, int warn)
2026     {
2027     if (strlen(comment) > REASONLEN)
2028     comment[REASONLEN-1] = '\0';
2029    
2030     return 1;
2031     }
2032    
2033     /* match_conf_password()
2034     *
2035     * inputs - pointer to given password
2036     * - pointer to Conf
2037     * output - 1 or 0 if match
2038     * side effects - none
2039     */
2040     int
2041 michael 1632 match_conf_password(const char *password, const struct MaskItem *conf)
2042 adx 30 {
2043     const char *encr = NULL;
2044    
2045 michael 1632 if (EmptyString(password) || EmptyString(conf->passwd))
2046 adx 30 return 0;
2047    
2048 michael 1632 if (conf->flags & CONF_FLAGS_ENCRYPTED)
2049     encr = crypt(password, conf->passwd);
2050 adx 30 else
2051     encr = password;
2052    
2053 michael 3263 return encr && !strcmp(encr, conf->passwd);
2054 adx 30 }
2055    
2056     /*
2057     * cluster_a_line
2058     *
2059     * inputs - client sending the cluster
2060     * - command name "KLINE" "XLINE" etc.
2061 michael 3347 * - capab -- CAP_KLN etc. from server.h
2062 michael 1309 * - cluster type -- CLUSTER_KLINE etc. from conf.h
2063 adx 30 * - pattern and args to send along
2064     * output - none
2065     * side effects - Take source_p send the pattern with args given
2066     * along to all servers that match capab and cluster type
2067     */
2068     void
2069     cluster_a_line(struct Client *source_p, const char *command,
2070 michael 593 int capab, int cluster_type, const char *pattern, ...)
2071 adx 30 {
2072     va_list args;
2073 michael 3375 char buffer[IRCD_BUFSIZE] = "";
2074 michael 593 const dlink_node *ptr = NULL;
2075 adx 30
2076     va_start(args, pattern);
2077     vsnprintf(buffer, sizeof(buffer), pattern, args);
2078     va_end(args);
2079    
2080     DLINK_FOREACH(ptr, cluster_items.head)
2081     {
2082 michael 1632 const struct MaskItem *conf = ptr->data;
2083 adx 30
2084     if (conf->flags & cluster_type)
2085     sendto_match_servs(source_p, conf->name, CAP_CLUSTER|capab,
2086 michael 2182 "%s %s %s", command, conf->name, buffer);
2087 adx 30 }
2088     }
2089    
2090     /*
2091     * split_nuh
2092     *
2093     * inputs - pointer to original mask (modified in place)
2094     * - pointer to pointer where nick should go
2095     * - pointer to pointer where user should go
2096     * - pointer to pointer where host should go
2097     * output - NONE
2098     * side effects - mask is modified in place
2099     * If nick pointer is NULL, ignore writing to it
2100     * this allows us to use this function elsewhere.
2101     *
2102     * mask nick user host
2103     * ---------------------- ------- ------- ------
2104     * Dianora!db@db.net Dianora db db.net
2105     * Dianora Dianora * *
2106     * db.net * * db.net
2107     * OR if nick pointer is NULL
2108     * Dianora - * Dianora
2109     * Dianora! Dianora * *
2110     * Dianora!@ Dianora * *
2111     * Dianora!db Dianora db *
2112     * Dianora!@db.net Dianora * db.net
2113     * db@db.net * db db.net
2114     * !@ * * *
2115     * @ * * *
2116     * ! * * *
2117     */
2118     void
2119 michael 593 split_nuh(struct split_nuh_item *const iptr)
2120 adx 30 {
2121     char *p = NULL, *q = NULL;
2122    
2123 michael 593 if (iptr->nickptr)
2124     strlcpy(iptr->nickptr, "*", iptr->nicksize);
2125     if (iptr->userptr)
2126     strlcpy(iptr->userptr, "*", iptr->usersize);
2127     if (iptr->hostptr)
2128     strlcpy(iptr->hostptr, "*", iptr->hostsize);
2129    
2130     if ((p = strchr(iptr->nuhmask, '!')))
2131 adx 30 {
2132     *p = '\0';
2133    
2134 michael 3375 if (iptr->nickptr && *iptr->nuhmask)
2135 michael 593 strlcpy(iptr->nickptr, iptr->nuhmask, iptr->nicksize);
2136 adx 30
2137 michael 2525 if ((q = strchr(++p, '@')))
2138     {
2139 michael 593 *q++ = '\0';
2140    
2141 michael 3375 if (*p)
2142 michael 593 strlcpy(iptr->userptr, p, iptr->usersize);
2143 adx 30
2144 michael 3375 if (*q)
2145 michael 593 strlcpy(iptr->hostptr, q, iptr->hostsize);
2146 adx 30 }
2147     else
2148     {
2149 michael 3375 if (*p)
2150 michael 593 strlcpy(iptr->userptr, p, iptr->usersize);
2151 adx 30 }
2152     }
2153 michael 593 else
2154 adx 30 {
2155 michael 593 /* No ! found so lets look for a user@host */
2156     if ((p = strchr(iptr->nuhmask, '@')))
2157 adx 30 {
2158 michael 593 /* if found a @ */
2159     *p++ = '\0';
2160 adx 30
2161 michael 3375 if (*iptr->nuhmask)
2162 michael 593 strlcpy(iptr->userptr, iptr->nuhmask, iptr->usersize);
2163 adx 30
2164 michael 3375 if (*p)
2165 michael 593 strlcpy(iptr->hostptr, p, iptr->hostsize);
2166 adx 30 }
2167 michael 593 else
2168 adx 30 {
2169 michael 593 /* no @ found */
2170     if (!iptr->nickptr || strpbrk(iptr->nuhmask, ".:"))
2171     strlcpy(iptr->hostptr, iptr->nuhmask, iptr->hostsize);
2172 adx 30 else
2173 michael 593 strlcpy(iptr->nickptr, iptr->nuhmask, iptr->nicksize);
2174 adx 30 }
2175     }
2176     }

Properties

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