1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 2001-2014 ircd-hybrid development team |
5 |
* |
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file resv.c |
23 |
* \brief Functions to reserve(jupe) a nick/channel. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "ircd.h" |
30 |
#include "send.h" |
31 |
#include "client.h" |
32 |
#include "memory.h" |
33 |
#include "numeric.h" |
34 |
#include "resv.h" |
35 |
#include "hash.h" |
36 |
#include "irc_string.h" |
37 |
#include "ircd_defs.h" |
38 |
#include "misc.h" |
39 |
#include "conf.h" |
40 |
#include "conf_db.h" |
41 |
#include "channel.h" |
42 |
#include "hostmask.h" |
43 |
|
44 |
|
45 |
/* create_resv() |
46 |
* |
47 |
* inputs - name of nick to create resv for |
48 |
* - reason for resv |
49 |
* - 1 if from ircd.conf, 0 if from elsewhere |
50 |
* output - pointer to struct ResvNick |
51 |
* side effects - |
52 |
*/ |
53 |
struct MaskItem * |
54 |
create_resv(const char *name, const char *reason, const dlink_list *list) |
55 |
{ |
56 |
dlink_node *node = NULL; |
57 |
struct MaskItem *conf = NULL; |
58 |
enum maskitem_type type; |
59 |
|
60 |
if (name == NULL || reason == NULL) |
61 |
return NULL; |
62 |
|
63 |
if (IsChanPrefix(*name)) |
64 |
type = CONF_CRESV; |
65 |
else |
66 |
type = CONF_NRESV; |
67 |
|
68 |
if (find_exact_name_conf(type, NULL, name, NULL, NULL)) |
69 |
return NULL; |
70 |
|
71 |
conf = conf_make(type); |
72 |
conf->name = xstrdup(name); |
73 |
conf->reason = xstrndup(reason, IRCD_MIN(strlen(reason), REASONLEN)); |
74 |
|
75 |
if (list) |
76 |
{ |
77 |
DLINK_FOREACH(node, list->head) |
78 |
{ |
79 |
char nick[NICKLEN + 1]; |
80 |
char user[USERLEN + 1]; |
81 |
char host[HOSTLEN + 1]; |
82 |
struct split_nuh_item nuh; |
83 |
struct exempt *exptr = NULL; |
84 |
char *s = node->data; |
85 |
|
86 |
if (strlen(s) == 2 && IsAlpha(*(s + 1) && IsAlpha(*(s + 2)))) |
87 |
{ |
88 |
#ifdef HAVE_LIBGEOIP |
89 |
exptr = MyCalloc(sizeof(*exptr)); |
90 |
exptr->name = xstrdup(s); |
91 |
exptr->country_id = GeoIP_id_by_code(s); |
92 |
dlinkAdd(exptr, &exptr->node, &conf->exempt_list); |
93 |
#endif |
94 |
} |
95 |
else |
96 |
{ |
97 |
nuh.nuhmask = s; |
98 |
nuh.nickptr = nick; |
99 |
nuh.userptr = user; |
100 |
nuh.hostptr = host; |
101 |
|
102 |
nuh.nicksize = sizeof(nick); |
103 |
nuh.usersize = sizeof(user); |
104 |
nuh.hostsize = sizeof(host); |
105 |
|
106 |
split_nuh(&nuh); |
107 |
|
108 |
exptr = MyCalloc(sizeof(*exptr)); |
109 |
exptr->name = xstrdup(nick); |
110 |
exptr->user = xstrdup(user); |
111 |
exptr->host = xstrdup(host); |
112 |
exptr->type = parse_netmask(host, &exptr->addr, &exptr->bits); |
113 |
dlinkAdd(exptr, &exptr->node, &conf->exempt_list); |
114 |
} |
115 |
} |
116 |
} |
117 |
|
118 |
return conf; |
119 |
} |
120 |
|
121 |
int |
122 |
resv_find_exempt(const struct Client *who, const struct MaskItem *conf) |
123 |
{ |
124 |
const dlink_node *node = NULL; |
125 |
|
126 |
DLINK_FOREACH(node, conf->exempt_list.head) |
127 |
{ |
128 |
const struct exempt *exptr = node->data; |
129 |
|
130 |
if (exptr->country_id) |
131 |
{ |
132 |
if (exptr->country_id == who->connection->country_id) |
133 |
return 1; |
134 |
} |
135 |
else if (!match(exptr->name, who->name) && !match(exptr->user, who->username)) |
136 |
{ |
137 |
switch (exptr->type) |
138 |
{ |
139 |
case HM_HOST: |
140 |
if (!match(exptr->host, who->host) || !match(exptr->host, who->sockhost)) |
141 |
return 1; |
142 |
break; |
143 |
case HM_IPV4: |
144 |
if (who->connection->aftype == AF_INET) |
145 |
if (match_ipv4(&who->connection->ip, &exptr->addr, exptr->bits)) |
146 |
return 1; |
147 |
break; |
148 |
case HM_IPV6: |
149 |
if (who->connection->aftype == AF_INET6) |
150 |
if (match_ipv6(&who->connection->ip, &exptr->addr, exptr->bits)) |
151 |
return 1; |
152 |
break; |
153 |
default: |
154 |
assert(0); |
155 |
} |
156 |
} |
157 |
} |
158 |
|
159 |
return 0; |
160 |
} |
161 |
|
162 |
/* match_find_resv() |
163 |
* |
164 |
* inputs - pointer to name |
165 |
* output - pointer to a struct ResvChannel |
166 |
* side effects - Finds a reserved channel whose name matches 'name', |
167 |
* if can't find one returns NULL. |
168 |
*/ |
169 |
struct MaskItem * |
170 |
match_find_resv(const char *name) |
171 |
{ |
172 |
dlink_node *node = NULL; |
173 |
|
174 |
if (EmptyString(name)) |
175 |
return NULL; |
176 |
|
177 |
DLINK_FOREACH(node, cresv_items.head) |
178 |
{ |
179 |
struct MaskItem *conf = node->data; |
180 |
|
181 |
if (!match(conf->name, name)) |
182 |
return conf; |
183 |
} |
184 |
|
185 |
return NULL; |
186 |
} |