1 |
adx |
30 |
/* |
2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
adx |
30 |
* |
4 |
michael |
8279 |
* Copyright (c) 2001-2018 ircd-hybrid development team |
5 |
adx |
30 |
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
michael |
4565 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
adx |
30 |
* USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
michael |
2916 |
/*! \file hostmask.c |
23 |
|
|
* \brief Code to efficiently find IP & hostmask based configs. |
24 |
|
|
* \version $Id$ |
25 |
|
|
*/ |
26 |
|
|
|
27 |
adx |
30 |
#include "stdinc.h" |
28 |
|
|
#include "memory.h" |
29 |
|
|
#include "ircd_defs.h" |
30 |
michael |
1011 |
#include "list.h" |
31 |
michael |
1309 |
#include "conf.h" |
32 |
adx |
30 |
#include "hostmask.h" |
33 |
|
|
#include "send.h" |
34 |
|
|
#include "irc_string.h" |
35 |
michael |
1369 |
#include "ircd.h" |
36 |
adx |
30 |
|
37 |
|
|
|
38 |
|
|
#define DigitParse(ch) do { \ |
39 |
|
|
if (ch >= '0' && ch <= '9') \ |
40 |
|
|
ch = ch - '0'; \ |
41 |
|
|
else if (ch >= 'A' && ch <= 'F') \ |
42 |
|
|
ch = ch - 'A' + 10; \ |
43 |
|
|
else if (ch >= 'a' && ch <= 'f') \ |
44 |
|
|
ch = ch - 'a' + 10; \ |
45 |
michael |
1298 |
} while (0); |
46 |
adx |
30 |
|
47 |
michael |
5460 |
/* Hashtable stuff...now external as it's used in m_stats.c */ |
48 |
|
|
dlink_list atable[ATABLE_SIZE]; |
49 |
|
|
|
50 |
adx |
30 |
/* The mask parser/type determination code... */ |
51 |
|
|
|
52 |
|
|
/* int try_parse_v6_netmask(const char *, struct irc_ssaddr *, int *); |
53 |
|
|
* Input: A possible IPV6 address as a string. |
54 |
|
|
* Output: An integer describing whether it is an IPV6 or hostmask, |
55 |
|
|
* an address(if it is IPV6), a bitlength(if it is IPV6). |
56 |
|
|
* Side effects: None |
57 |
|
|
* Comments: Called from parse_netmask |
58 |
|
|
*/ |
59 |
|
|
static int |
60 |
|
|
try_parse_v6_netmask(const char *text, struct irc_ssaddr *addr, int *b) |
61 |
|
|
{ |
62 |
|
|
char c; |
63 |
|
|
int d[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
64 |
|
|
int dp = 0; |
65 |
|
|
int nyble = 4; |
66 |
|
|
int finsert = -1; |
67 |
|
|
int bits = 128; |
68 |
|
|
int deficit = 0; |
69 |
|
|
short dc[8]; |
70 |
michael |
4874 |
struct sockaddr_in6 *const v6 = (struct sockaddr_in6 *)addr; |
71 |
adx |
30 |
|
72 |
michael |
3469 |
for (const char *p = text; (c = *p); ++p) |
73 |
michael |
2894 |
{ |
74 |
adx |
30 |
if (IsXDigit(c)) |
75 |
|
|
{ |
76 |
|
|
if (nyble == 0) |
77 |
|
|
return HM_HOST; |
78 |
|
|
DigitParse(c); |
79 |
|
|
d[dp] |= c << (4 * --nyble); |
80 |
|
|
} |
81 |
|
|
else if (c == ':') |
82 |
|
|
{ |
83 |
|
|
if (p > text && *(p - 1) == ':') |
84 |
|
|
{ |
85 |
|
|
if (finsert >= 0) |
86 |
|
|
return HM_HOST; |
87 |
|
|
finsert = dp; |
88 |
|
|
} |
89 |
|
|
else |
90 |
|
|
{ |
91 |
michael |
6580 |
/* |
92 |
|
|
* If there were less than 4 hex digits, e.g. :ABC: shift right |
93 |
|
|
* so we don't interpret it as ABC0 -A1kmm |
94 |
|
|
*/ |
95 |
adx |
30 |
d[dp] = d[dp] >> 4 * nyble; |
96 |
|
|
nyble = 4; |
97 |
michael |
6580 |
|
98 |
adx |
30 |
if (++dp >= 8) |
99 |
|
|
return HM_HOST; |
100 |
|
|
} |
101 |
|
|
} |
102 |
|
|
else if (c == '*') |
103 |
|
|
{ |
104 |
|
|
/* * must be last, and * is ambiguous if there is a ::... -A1kmm */ |
105 |
|
|
if (finsert >= 0 || *(p + 1) || dp == 0 || *(p - 1) != ':') |
106 |
|
|
return HM_HOST; |
107 |
|
|
bits = dp * 16; |
108 |
|
|
} |
109 |
|
|
else if (c == '/') |
110 |
|
|
{ |
111 |
|
|
char *after; |
112 |
|
|
|
113 |
|
|
d[dp] = d[dp] >> 4 * nyble; |
114 |
michael |
3215 |
++dp; |
115 |
adx |
30 |
bits = strtoul(p + 1, &after, 10); |
116 |
michael |
1298 |
|
117 |
adx |
30 |
if (bits < 0 || *after) |
118 |
|
|
return HM_HOST; |
119 |
|
|
if (bits > dp * 4 && !(finsert >= 0 && bits <= 128)) |
120 |
|
|
return HM_HOST; |
121 |
|
|
break; |
122 |
|
|
} |
123 |
|
|
else |
124 |
|
|
return HM_HOST; |
125 |
michael |
2894 |
} |
126 |
adx |
30 |
|
127 |
|
|
d[dp] = d[dp] >> 4 * nyble; |
128 |
michael |
1298 |
|
129 |
adx |
30 |
if (c == 0) |
130 |
michael |
3215 |
++dp; |
131 |
adx |
30 |
if (finsert < 0 && bits == 0) |
132 |
|
|
bits = dp * 16; |
133 |
michael |
1298 |
|
134 |
adx |
30 |
/* How many words are missing? -A1kmm */ |
135 |
|
|
deficit = bits / 16 + ((bits % 16) ? 1 : 0) - dp; |
136 |
michael |
1298 |
|
137 |
adx |
30 |
/* Now fill in the gaps(from ::) in the copied table... -A1kmm */ |
138 |
michael |
3215 |
for (dp = 0, nyble = 0; dp < 8; ++dp) |
139 |
adx |
30 |
{ |
140 |
|
|
if (nyble == finsert && deficit) |
141 |
|
|
{ |
142 |
|
|
dc[dp] = 0; |
143 |
|
|
deficit--; |
144 |
|
|
} |
145 |
|
|
else |
146 |
|
|
dc[dp] = d[nyble++]; |
147 |
|
|
} |
148 |
michael |
1298 |
|
149 |
adx |
30 |
/* Set unused bits to 0... -A1kmm */ |
150 |
|
|
if (bits < 128 && (bits % 16 != 0)) |
151 |
|
|
dc[bits / 16] &= ~((1 << (15 - bits % 16)) - 1); |
152 |
michael |
3215 |
for (dp = bits / 16 + (bits % 16 ? 1 : 0); dp < 8; ++dp) |
153 |
adx |
30 |
dc[dp] = 0; |
154 |
michael |
1298 |
|
155 |
adx |
30 |
/* And assign... -A1kmm */ |
156 |
|
|
if (addr) |
157 |
michael |
7791 |
{ |
158 |
|
|
v6->sin6_family = AF_INET6; |
159 |
|
|
|
160 |
michael |
3215 |
for (dp = 0; dp < 8; ++dp) |
161 |
adx |
30 |
/* The cast is a kludge to make netbsd work. */ |
162 |
|
|
((unsigned short *)&v6->sin6_addr)[dp] = htons(dc[dp]); |
163 |
michael |
7791 |
} |
164 |
michael |
1298 |
|
165 |
michael |
3215 |
if (b) |
166 |
adx |
30 |
*b = bits; |
167 |
|
|
return HM_IPV6; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
/* int try_parse_v4_netmask(const char *, struct irc_ssaddr *, int *); |
171 |
|
|
* Input: A possible IPV4 address as a string. |
172 |
|
|
* Output: An integer describing whether it is an IPV4 or hostmask, |
173 |
|
|
* an address(if it is IPV4), a bitlength(if it is IPV4). |
174 |
|
|
* Side effects: None |
175 |
|
|
* Comments: Called from parse_netmask |
176 |
|
|
*/ |
177 |
|
|
static int |
178 |
|
|
try_parse_v4_netmask(const char *text, struct irc_ssaddr *addr, int *b) |
179 |
|
|
{ |
180 |
|
|
const char *digits[4]; |
181 |
|
|
unsigned char addb[4]; |
182 |
|
|
int n = 0, bits = 0; |
183 |
|
|
char c; |
184 |
michael |
4874 |
struct sockaddr_in *const v4 = (struct sockaddr_in *)addr; |
185 |
adx |
30 |
|
186 |
|
|
digits[n++] = text; |
187 |
|
|
|
188 |
michael |
3469 |
for (const char *p = text; (c = *p); ++p) |
189 |
michael |
2894 |
{ |
190 |
adx |
30 |
if (c >= '0' && c <= '9') /* empty */ |
191 |
|
|
; |
192 |
|
|
else if (c == '.') |
193 |
|
|
{ |
194 |
|
|
if (n >= 4) |
195 |
|
|
return HM_HOST; |
196 |
michael |
1298 |
|
197 |
adx |
30 |
digits[n++] = p + 1; |
198 |
|
|
} |
199 |
|
|
else if (c == '*') |
200 |
|
|
{ |
201 |
michael |
5976 |
if (*(p + 1) || n == 1 || *(p - 1) != '.') |
202 |
adx |
30 |
return HM_HOST; |
203 |
michael |
1298 |
|
204 |
adx |
30 |
bits = (n - 1) * 8; |
205 |
|
|
break; |
206 |
|
|
} |
207 |
|
|
else if (c == '/') |
208 |
|
|
{ |
209 |
|
|
char *after; |
210 |
|
|
bits = strtoul(p + 1, &after, 10); |
211 |
michael |
1298 |
|
212 |
michael |
1786 |
if (bits < 0 || *after) |
213 |
adx |
30 |
return HM_HOST; |
214 |
|
|
if (bits > n * 8) |
215 |
|
|
return HM_HOST; |
216 |
michael |
1298 |
|
217 |
adx |
30 |
break; |
218 |
|
|
} |
219 |
|
|
else |
220 |
|
|
return HM_HOST; |
221 |
michael |
2894 |
} |
222 |
adx |
30 |
|
223 |
|
|
if (n < 4 && bits == 0) |
224 |
|
|
bits = n * 8; |
225 |
|
|
if (bits) |
226 |
|
|
while (n < 4) |
227 |
|
|
digits[n++] = "0"; |
228 |
michael |
1298 |
|
229 |
michael |
3215 |
for (n = 0; n < 4; ++n) |
230 |
adx |
30 |
addb[n] = strtoul(digits[n], NULL, 10); |
231 |
michael |
1298 |
|
232 |
adx |
30 |
if (bits == 0) |
233 |
|
|
bits = 32; |
234 |
michael |
1298 |
|
235 |
adx |
30 |
/* Set unused bits to 0... -A1kmm */ |
236 |
|
|
if (bits < 32 && bits % 8) |
237 |
|
|
addb[bits / 8] &= ~((1 << (8 - bits % 8)) - 1); |
238 |
michael |
3215 |
for (n = bits / 8 + (bits % 8 ? 1 : 0); n < 4; ++n) |
239 |
adx |
30 |
addb[n] = 0; |
240 |
|
|
if (addr) |
241 |
michael |
7791 |
{ |
242 |
|
|
v4->sin_family = AF_INET; |
243 |
adx |
30 |
v4->sin_addr.s_addr = |
244 |
|
|
htonl(addb[0] << 24 | addb[1] << 16 | addb[2] << 8 | addb[3]); |
245 |
michael |
7791 |
} |
246 |
|
|
|
247 |
michael |
3215 |
if (b) |
248 |
adx |
30 |
*b = bits; |
249 |
|
|
return HM_IPV4; |
250 |
|
|
} |
251 |
|
|
|
252 |
|
|
/* int parse_netmask(const char *, struct irc_ssaddr *, int *); |
253 |
|
|
* Input: A hostmask, or an IPV4/6 address. |
254 |
|
|
* Output: An integer describing whether it is an IPV4, IPV6 address or a |
255 |
|
|
* hostmask, an address(if it is an IP mask), |
256 |
|
|
* a bitlength(if it is IP mask). |
257 |
|
|
* Side effects: None |
258 |
|
|
*/ |
259 |
|
|
int |
260 |
|
|
parse_netmask(const char *text, struct irc_ssaddr *addr, int *b) |
261 |
|
|
{ |
262 |
michael |
7791 |
if (addr) |
263 |
michael |
8310 |
memset(addr, 0, sizeof(*addr)); |
264 |
michael |
7791 |
|
265 |
michael |
2814 |
if (strchr(text, '.')) |
266 |
|
|
return try_parse_v4_netmask(text, addr, b); |
267 |
michael |
1298 |
if (strchr(text, ':')) |
268 |
adx |
30 |
return try_parse_v6_netmask(text, addr, b); |
269 |
michael |
4415 |
|
270 |
adx |
30 |
return HM_HOST; |
271 |
|
|
} |
272 |
|
|
|
273 |
|
|
/* The address matching stuff... */ |
274 |
|
|
/* int match_ipv6(struct irc_ssaddr *, struct irc_ssaddr *, int) |
275 |
|
|
* Input: An IP address, an IP mask, the number of bits in the mask. |
276 |
|
|
* Output: if match, -1 else 0 |
277 |
|
|
* Side effects: None |
278 |
|
|
*/ |
279 |
|
|
int |
280 |
michael |
1455 |
match_ipv6(const struct irc_ssaddr *addr, const struct irc_ssaddr *mask, int bits) |
281 |
adx |
30 |
{ |
282 |
|
|
int i, m, n = bits / 8; |
283 |
michael |
4977 |
const struct sockaddr_in6 *const v6 = (const struct sockaddr_in6 *)addr; |
284 |
|
|
const struct sockaddr_in6 *const v6mask = (const struct sockaddr_in6 *)mask; |
285 |
adx |
30 |
|
286 |
michael |
3215 |
for (i = 0; i < n; ++i) |
287 |
adx |
30 |
if (v6->sin6_addr.s6_addr[i] != v6mask->sin6_addr.s6_addr[i]) |
288 |
|
|
return 0; |
289 |
michael |
3215 |
|
290 |
adx |
30 |
if ((m = bits % 8) == 0) |
291 |
|
|
return -1; |
292 |
|
|
if ((v6->sin6_addr.s6_addr[n] & ~((1 << (8 - m)) - 1)) == |
293 |
|
|
v6mask->sin6_addr.s6_addr[n]) |
294 |
|
|
return -1; |
295 |
|
|
return 0; |
296 |
|
|
} |
297 |
michael |
1372 |
|
298 |
adx |
30 |
/* int match_ipv4(struct irc_ssaddr *, struct irc_ssaddr *, int) |
299 |
|
|
* Input: An IP address, an IP mask, the number of bits in the mask. |
300 |
|
|
* Output: if match, -1 else 0 |
301 |
|
|
* Side Effects: None |
302 |
|
|
*/ |
303 |
|
|
int |
304 |
michael |
1455 |
match_ipv4(const struct irc_ssaddr *addr, const struct irc_ssaddr *mask, int bits) |
305 |
adx |
30 |
{ |
306 |
michael |
4977 |
const struct sockaddr_in *const v4 = (const struct sockaddr_in *)addr; |
307 |
|
|
const struct sockaddr_in *const v4mask = (const struct sockaddr_in *)mask; |
308 |
michael |
1298 |
|
309 |
adx |
30 |
if ((ntohl(v4->sin_addr.s_addr) & ~((1 << (32 - bits)) - 1)) != |
310 |
|
|
ntohl(v4mask->sin_addr.s_addr)) |
311 |
|
|
return 0; |
312 |
|
|
return -1; |
313 |
|
|
} |
314 |
|
|
|
315 |
|
|
/* unsigned long hash_ipv4(struct irc_ssaddr*) |
316 |
|
|
* Input: An IP address. |
317 |
|
|
* Output: A hash value of the IP address. |
318 |
|
|
* Side effects: None |
319 |
|
|
*/ |
320 |
michael |
1343 |
static uint32_t |
321 |
michael |
2898 |
hash_ipv4(const struct irc_ssaddr *addr, int bits) |
322 |
adx |
30 |
{ |
323 |
michael |
8437 |
if (bits) |
324 |
adx |
30 |
{ |
325 |
michael |
4874 |
const struct sockaddr_in *const v4 = (const struct sockaddr_in *)addr; |
326 |
michael |
1343 |
uint32_t av = ntohl(v4->sin_addr.s_addr) & ~((1 << (32 - bits)) - 1); |
327 |
michael |
1298 |
|
328 |
|
|
return (av ^ (av >> 12) ^ (av >> 24)) & (ATABLE_SIZE - 1); |
329 |
adx |
30 |
} |
330 |
|
|
|
331 |
michael |
1298 |
return 0; |
332 |
adx |
30 |
} |
333 |
|
|
|
334 |
|
|
/* unsigned long hash_ipv6(struct irc_ssaddr*) |
335 |
|
|
* Input: An IP address. |
336 |
|
|
* Output: A hash value of the IP address. |
337 |
|
|
* Side effects: None |
338 |
|
|
*/ |
339 |
michael |
1343 |
static uint32_t |
340 |
michael |
2898 |
hash_ipv6(const struct irc_ssaddr *addr, int bits) |
341 |
adx |
30 |
{ |
342 |
michael |
1343 |
uint32_t v = 0, n; |
343 |
michael |
4874 |
const struct sockaddr_in6 *const v6 = (const struct sockaddr_in6 *)addr; |
344 |
michael |
1298 |
|
345 |
michael |
3215 |
for (n = 0; n < 16; ++n) |
346 |
adx |
30 |
{ |
347 |
|
|
if (bits >= 8) |
348 |
|
|
{ |
349 |
|
|
v ^= v6->sin6_addr.s6_addr[n]; |
350 |
|
|
bits -= 8; |
351 |
|
|
} |
352 |
|
|
else if (bits) |
353 |
|
|
{ |
354 |
|
|
v ^= v6->sin6_addr.s6_addr[n] & ~((1 << (8 - bits)) - 1); |
355 |
|
|
return v & (ATABLE_SIZE - 1); |
356 |
|
|
} |
357 |
|
|
else |
358 |
|
|
return v & (ATABLE_SIZE - 1); |
359 |
|
|
} |
360 |
michael |
4415 |
|
361 |
adx |
30 |
return v & (ATABLE_SIZE - 1); |
362 |
|
|
} |
363 |
|
|
|
364 |
|
|
/* int hash_text(const char *start) |
365 |
|
|
* Input: The start of the text to hash. |
366 |
|
|
* Output: The hash of the string between 1 and (TH_MAX-1) |
367 |
|
|
* Side-effects: None. |
368 |
|
|
*/ |
369 |
michael |
1343 |
static uint32_t |
370 |
adx |
30 |
hash_text(const char *start) |
371 |
|
|
{ |
372 |
michael |
1343 |
uint32_t h = 0; |
373 |
adx |
30 |
|
374 |
michael |
3469 |
for (const char *p = start; *p; ++p) |
375 |
michael |
2331 |
h = (h << 4) - (h + ToLower(*p)); |
376 |
michael |
1298 |
|
377 |
|
|
return h & (ATABLE_SIZE - 1); |
378 |
adx |
30 |
} |
379 |
|
|
|
380 |
|
|
/* unsigned long get_hash_mask(const char *) |
381 |
|
|
* Input: The text to hash. |
382 |
|
|
* Output: The hash of the string right of the first '.' past the last |
383 |
|
|
* wildcard in the string. |
384 |
|
|
* Side-effects: None. |
385 |
|
|
*/ |
386 |
michael |
1343 |
static uint32_t |
387 |
adx |
30 |
get_mask_hash(const char *text) |
388 |
|
|
{ |
389 |
|
|
const char *hp = "", *p; |
390 |
|
|
|
391 |
michael |
3215 |
for (p = text + strlen(text) - 1; p >= text; --p) |
392 |
adx |
723 |
if (IsMWildChar(*p)) |
393 |
adx |
30 |
return hash_text(hp); |
394 |
|
|
else if (*p == '.') |
395 |
|
|
hp = p + 1; |
396 |
|
|
return hash_text(text); |
397 |
|
|
} |
398 |
|
|
|
399 |
michael |
1632 |
/* struct MaskItem *find_conf_by_address(const char *, struct irc_ssaddr *, |
400 |
adx |
30 |
* int type, int fam, const char *username) |
401 |
|
|
* Input: The hostname, the address, the type of mask to find, the address |
402 |
|
|
* family, the username. |
403 |
|
|
* Output: The matching value with the highest precedence. |
404 |
|
|
* Side-effects: None |
405 |
|
|
* Note: Setting bit 0 of the type means that the username is ignored. |
406 |
|
|
* Warning: IsNeedPassword for everything that is not an auth{} entry |
407 |
michael |
1632 |
* should always be true (i.e. conf->flags & CONF_FLAGS_NEED_PASSWORD == 0) |
408 |
adx |
30 |
*/ |
409 |
michael |
1632 |
struct MaskItem * |
410 |
michael |
5850 |
find_conf_by_address(const char *name, const struct irc_ssaddr *addr, unsigned int type, |
411 |
michael |
1371 |
int fam, const char *username, const char *password, int do_match) |
412 |
adx |
30 |
{ |
413 |
michael |
1343 |
unsigned int hprecv = 0; |
414 |
michael |
7914 |
dlink_node *node; |
415 |
michael |
1632 |
struct MaskItem *hprec = NULL; |
416 |
|
|
struct AddressRec *arec = NULL; |
417 |
michael |
1371 |
int (*cmpfunc)(const char *, const char *) = do_match ? match : irccmp; |
418 |
adx |
30 |
|
419 |
|
|
if (addr) |
420 |
|
|
{ |
421 |
|
|
/* Check for IPV6 matches... */ |
422 |
|
|
if (fam == AF_INET6) |
423 |
|
|
{ |
424 |
michael |
7797 |
for (int b = 128; b >= 0; b -= 16) |
425 |
adx |
30 |
{ |
426 |
michael |
4815 |
DLINK_FOREACH(node, atable[hash_ipv6(addr, b)].head) |
427 |
michael |
1367 |
{ |
428 |
michael |
4815 |
arec = node->data; |
429 |
michael |
1367 |
|
430 |
michael |
2995 |
if ((arec->type == type) && |
431 |
adx |
30 |
arec->precedence > hprecv && |
432 |
|
|
arec->masktype == HM_IPV6 && |
433 |
|
|
match_ipv6(addr, &arec->Mask.ipa.addr, |
434 |
|
|
arec->Mask.ipa.bits) && |
435 |
michael |
2995 |
(!username || !cmpfunc(arec->username, username)) && |
436 |
michael |
2916 |
(IsNeedPassword(arec->conf) || arec->conf->passwd == NULL || |
437 |
|
|
match_conf_password(password, arec->conf))) |
438 |
adx |
30 |
{ |
439 |
|
|
hprecv = arec->precedence; |
440 |
michael |
1632 |
hprec = arec->conf; |
441 |
adx |
30 |
} |
442 |
michael |
1367 |
} |
443 |
adx |
30 |
} |
444 |
|
|
} |
445 |
michael |
4415 |
else if (fam == AF_INET) |
446 |
adx |
30 |
{ |
447 |
michael |
7797 |
for (int b = 32; b >= 0; b -= 8) |
448 |
adx |
30 |
{ |
449 |
michael |
4815 |
DLINK_FOREACH(node, atable[hash_ipv4(addr, b)].head) |
450 |
michael |
1367 |
{ |
451 |
michael |
4815 |
arec = node->data; |
452 |
michael |
1367 |
|
453 |
michael |
2995 |
if ((arec->type == type) && |
454 |
adx |
30 |
arec->precedence > hprecv && |
455 |
|
|
arec->masktype == HM_IPV4 && |
456 |
|
|
match_ipv4(addr, &arec->Mask.ipa.addr, |
457 |
|
|
arec->Mask.ipa.bits) && |
458 |
michael |
2995 |
(!username || !cmpfunc(arec->username, username)) && |
459 |
michael |
2916 |
(IsNeedPassword(arec->conf) || arec->conf->passwd == NULL || |
460 |
|
|
match_conf_password(password, arec->conf))) |
461 |
adx |
30 |
{ |
462 |
|
|
hprecv = arec->precedence; |
463 |
michael |
1632 |
hprec = arec->conf; |
464 |
adx |
30 |
} |
465 |
michael |
1367 |
} |
466 |
adx |
30 |
} |
467 |
|
|
} |
468 |
|
|
} |
469 |
|
|
|
470 |
michael |
3215 |
if (name) |
471 |
adx |
30 |
{ |
472 |
|
|
const char *p = name; |
473 |
|
|
|
474 |
|
|
while (1) |
475 |
|
|
{ |
476 |
michael |
4815 |
DLINK_FOREACH(node, atable[hash_text(p)].head) |
477 |
michael |
1367 |
{ |
478 |
michael |
4815 |
arec = node->data; |
479 |
michael |
2995 |
if ((arec->type == type) && |
480 |
adx |
30 |
arec->precedence > hprecv && |
481 |
|
|
(arec->masktype == HM_HOST) && |
482 |
michael |
1652 |
!cmpfunc(arec->Mask.hostname, name) && |
483 |
michael |
2995 |
(!username || !cmpfunc(arec->username, username)) && |
484 |
michael |
1632 |
(IsNeedPassword(arec->conf) || arec->conf->passwd == NULL || |
485 |
|
|
match_conf_password(password, arec->conf))) |
486 |
adx |
30 |
{ |
487 |
|
|
hprecv = arec->precedence; |
488 |
michael |
1632 |
hprec = arec->conf; |
489 |
adx |
30 |
} |
490 |
michael |
1367 |
} |
491 |
michael |
3215 |
|
492 |
|
|
if ((p = strchr(p, '.')) == NULL) |
493 |
adx |
30 |
break; |
494 |
michael |
3215 |
++p; |
495 |
adx |
30 |
} |
496 |
michael |
1367 |
|
497 |
michael |
4815 |
DLINK_FOREACH(node, atable[0].head) |
498 |
michael |
1367 |
{ |
499 |
michael |
4815 |
arec = node->data; |
500 |
michael |
1367 |
|
501 |
michael |
2995 |
if (arec->type == type && |
502 |
adx |
30 |
arec->precedence > hprecv && |
503 |
|
|
arec->masktype == HM_HOST && |
504 |
michael |
1652 |
!cmpfunc(arec->Mask.hostname, name) && |
505 |
michael |
2995 |
(!username || !cmpfunc(arec->username, username)) && |
506 |
michael |
1632 |
(IsNeedPassword(arec->conf) || arec->conf->passwd == NULL || |
507 |
|
|
match_conf_password(password, arec->conf))) |
508 |
adx |
30 |
{ |
509 |
|
|
hprecv = arec->precedence; |
510 |
michael |
1632 |
hprec = arec->conf; |
511 |
adx |
30 |
} |
512 |
michael |
1367 |
} |
513 |
adx |
30 |
} |
514 |
|
|
|
515 |
|
|
return hprec; |
516 |
|
|
} |
517 |
|
|
|
518 |
michael |
1632 |
/* struct MaskItem* find_address_conf(const char*, const char*, |
519 |
adx |
30 |
* struct irc_ssaddr*, int, char *); |
520 |
|
|
* Input: The hostname, username, address, address family. |
521 |
michael |
1632 |
* Output: The applicable MaskItem. |
522 |
adx |
30 |
* Side-effects: None |
523 |
|
|
*/ |
524 |
michael |
1632 |
struct MaskItem * |
525 |
michael |
6429 |
find_address_conf(const char *host, const char *user, const struct irc_ssaddr *ip, |
526 |
michael |
3553 |
int aftype, const char *password) |
527 |
adx |
30 |
{ |
528 |
michael |
1632 |
struct MaskItem *authcnf = NULL, *killcnf = NULL; |
529 |
adx |
30 |
|
530 |
michael |
1426 |
/* Find the best auth{} block... If none, return NULL -A1kmm */ |
531 |
michael |
7797 |
if ((authcnf = find_conf_by_address(host, ip, CONF_CLIENT, aftype, user, password, 1)) == NULL) |
532 |
michael |
1298 |
return NULL; |
533 |
adx |
30 |
|
534 |
michael |
1426 |
/* If they are exempt from K-lines, return the best auth{} block. -A1kmm */ |
535 |
michael |
1632 |
if (IsConfExemptKline(authcnf)) |
536 |
|
|
return authcnf; |
537 |
adx |
30 |
|
538 |
|
|
/* Find the best K-line... -A1kmm */ |
539 |
michael |
1632 |
killcnf = find_conf_by_address(host, ip, CONF_KLINE, aftype, user, NULL, 1); |
540 |
adx |
30 |
|
541 |
michael |
1426 |
/* |
542 |
|
|
* If they are K-lined, return the K-line. Otherwise, return the |
543 |
michael |
5805 |
* auth {} block. -A1kmm |
544 |
michael |
1426 |
*/ |
545 |
michael |
6411 |
if (killcnf) |
546 |
michael |
1632 |
return killcnf; |
547 |
adx |
30 |
|
548 |
michael |
1632 |
return authcnf; |
549 |
adx |
30 |
} |
550 |
|
|
|
551 |
michael |
1632 |
/* struct MaskItem* find_dline_conf(struct irc_ssaddr*, int) |
552 |
adx |
30 |
* |
553 |
|
|
* Input: An address, an address family. |
554 |
|
|
* Output: The best matching D-line or exempt line. |
555 |
|
|
* Side effects: None. |
556 |
|
|
*/ |
557 |
michael |
1632 |
struct MaskItem * |
558 |
michael |
6429 |
find_dline_conf(const struct irc_ssaddr *addr, int aftype) |
559 |
adx |
30 |
{ |
560 |
michael |
1632 |
struct MaskItem *eline; |
561 |
adx |
30 |
|
562 |
michael |
2995 |
eline = find_conf_by_address(NULL, addr, CONF_EXEMPT, aftype, NULL, NULL, 1); |
563 |
michael |
3215 |
if (eline) |
564 |
michael |
1298 |
return eline; |
565 |
|
|
|
566 |
michael |
6411 |
return find_conf_by_address(NULL, addr, CONF_DLINE, aftype, NULL, NULL, 1); |
567 |
adx |
30 |
} |
568 |
|
|
|
569 |
michael |
1632 |
/* void add_conf_by_address(int, struct MaskItem *aconf) |
570 |
michael |
2916 |
* Input: |
571 |
adx |
30 |
* Output: None |
572 |
|
|
* Side-effects: Adds this entry to the hash table. |
573 |
|
|
*/ |
574 |
michael |
2811 |
struct AddressRec * |
575 |
michael |
1632 |
add_conf_by_address(const unsigned int type, struct MaskItem *conf) |
576 |
adx |
30 |
{ |
577 |
michael |
4977 |
const char *const hostname = conf->host; |
578 |
|
|
const char *const username = conf->user; |
579 |
michael |
6868 |
static unsigned int prec_value = UINT_MAX; |
580 |
michael |
1367 |
int bits = 0; |
581 |
adx |
30 |
|
582 |
michael |
3003 |
assert(type && !EmptyString(hostname)); |
583 |
adx |
30 |
|
584 |
michael |
8310 |
struct AddressRec *arec = xcalloc(sizeof(*arec)); |
585 |
michael |
3001 |
arec->masktype = parse_netmask(hostname, &arec->Mask.ipa.addr, &bits); |
586 |
adx |
30 |
arec->Mask.ipa.bits = bits; |
587 |
michael |
1367 |
arec->username = username; |
588 |
michael |
1632 |
arec->conf = conf; |
589 |
michael |
1367 |
arec->precedence = prec_value--; |
590 |
|
|
arec->type = type; |
591 |
michael |
1298 |
|
592 |
michael |
1367 |
switch (arec->masktype) |
593 |
|
|
{ |
594 |
|
|
case HM_IPV4: |
595 |
|
|
/* We have to do this, since we do not re-hash for every bit -A1kmm. */ |
596 |
|
|
bits -= bits % 8; |
597 |
|
|
dlinkAdd(arec, &arec->node, &atable[hash_ipv4(&arec->Mask.ipa.addr, bits)]); |
598 |
|
|
break; |
599 |
|
|
case HM_IPV6: |
600 |
|
|
/* We have to do this, since we do not re-hash for every bit -A1kmm. */ |
601 |
|
|
bits -= bits % 16; |
602 |
|
|
dlinkAdd(arec, &arec->node, &atable[hash_ipv6(&arec->Mask.ipa.addr, bits)]); |
603 |
|
|
break; |
604 |
|
|
default: /* HM_HOST */ |
605 |
michael |
3001 |
arec->Mask.hostname = hostname; |
606 |
|
|
dlinkAdd(arec, &arec->node, &atable[get_mask_hash(hostname)]); |
607 |
michael |
1367 |
break; |
608 |
adx |
30 |
} |
609 |
michael |
2811 |
|
610 |
|
|
return arec; |
611 |
adx |
30 |
} |
612 |
|
|
|
613 |
michael |
1632 |
/* void delete_one_address(const char*, struct MaskItem*) |
614 |
|
|
* Input: An address string, the associated MaskItem. |
615 |
adx |
30 |
* Output: None |
616 |
michael |
1632 |
* Side effects: Deletes an address record. Frees the MaskItem if there |
617 |
adx |
30 |
* is nothing referencing it, sets it as illegal otherwise. |
618 |
|
|
*/ |
619 |
|
|
void |
620 |
michael |
1632 |
delete_one_address_conf(const char *address, struct MaskItem *conf) |
621 |
adx |
30 |
{ |
622 |
michael |
1367 |
int bits = 0; |
623 |
|
|
uint32_t hv = 0; |
624 |
michael |
7914 |
dlink_node *node; |
625 |
adx |
30 |
struct irc_ssaddr addr; |
626 |
michael |
1298 |
|
627 |
michael |
1367 |
switch (parse_netmask(address, &addr, &bits)) |
628 |
|
|
{ |
629 |
|
|
case HM_IPV4: |
630 |
|
|
/* We have to do this, since we do not re-hash for every bit -A1kmm. */ |
631 |
|
|
bits -= bits % 8; |
632 |
|
|
hv = hash_ipv4(&addr, bits); |
633 |
|
|
break; |
634 |
|
|
case HM_IPV6: |
635 |
|
|
/* We have to do this, since we do not re-hash for every bit -A1kmm. */ |
636 |
|
|
bits -= bits % 16; |
637 |
|
|
hv = hash_ipv6(&addr, bits); |
638 |
|
|
break; |
639 |
|
|
default: /* HM_HOST */ |
640 |
|
|
hv = get_mask_hash(address); |
641 |
|
|
break; |
642 |
adx |
30 |
} |
643 |
michael |
1298 |
|
644 |
michael |
4815 |
DLINK_FOREACH(node, atable[hv].head) |
645 |
adx |
30 |
{ |
646 |
michael |
4815 |
struct AddressRec *arec = node->data; |
647 |
michael |
1367 |
|
648 |
michael |
1632 |
if (arec->conf == conf) |
649 |
adx |
30 |
{ |
650 |
michael |
1367 |
dlinkDelete(&arec->node, &atable[hv]); |
651 |
michael |
1298 |
|
652 |
michael |
8437 |
if (conf->ref_count == 0) |
653 |
michael |
1632 |
conf_free(conf); |
654 |
michael |
1298 |
|
655 |
michael |
7032 |
xfree(arec); |
656 |
adx |
30 |
return; |
657 |
|
|
} |
658 |
|
|
} |
659 |
|
|
} |
660 |
|
|
|
661 |
|
|
/* void clear_out_address_conf(void) |
662 |
|
|
* Input: None |
663 |
|
|
* Output: None |
664 |
|
|
* Side effects: Clears out all address records in the hash table, |
665 |
michael |
1632 |
* frees them, and frees the MaskItems if nothing references |
666 |
michael |
2916 |
* them, otherwise sets them as illegal. |
667 |
adx |
30 |
*/ |
668 |
|
|
void |
669 |
|
|
clear_out_address_conf(void) |
670 |
|
|
{ |
671 |
michael |
7914 |
dlink_node *node, *node_next; |
672 |
michael |
2916 |
|
673 |
michael |
3235 |
for (unsigned int i = 0; i < ATABLE_SIZE; ++i) |
674 |
adx |
30 |
{ |
675 |
michael |
4815 |
DLINK_FOREACH_SAFE(node, node_next, atable[i].head) |
676 |
adx |
30 |
{ |
677 |
michael |
4815 |
struct AddressRec *arec = node->data; |
678 |
michael |
1367 |
|
679 |
michael |
1628 |
/* |
680 |
michael |
5846 |
* Destroy the ircd.conf items and keep those that are in the databases |
681 |
michael |
1628 |
*/ |
682 |
michael |
5846 |
if (IsConfDatabase(arec->conf)) |
683 |
michael |
1628 |
continue; |
684 |
michael |
1622 |
|
685 |
michael |
1628 |
dlinkDelete(&arec->node, &atable[i]); |
686 |
michael |
8658 |
arec->conf->active = false; |
687 |
michael |
1298 |
|
688 |
michael |
8437 |
if (arec->conf->ref_count == 0) |
689 |
michael |
1632 |
conf_free(arec->conf); |
690 |
michael |
7032 |
xfree(arec); |
691 |
adx |
30 |
} |
692 |
|
|
} |
693 |
|
|
} |
694 |
|
|
|
695 |
michael |
1369 |
static void |
696 |
michael |
4977 |
hostmask_send_expiration(const struct AddressRec *const arec) |
697 |
michael |
1369 |
{ |
698 |
michael |
6991 |
char ban_type = '?'; |
699 |
michael |
1369 |
|
700 |
michael |
8522 |
if (ConfigGeneral.tkline_expire_notices == 0) |
701 |
michael |
1369 |
return; |
702 |
|
|
|
703 |
|
|
switch (arec->type) |
704 |
|
|
{ |
705 |
|
|
case CONF_KLINE: |
706 |
|
|
ban_type = 'K'; |
707 |
|
|
break; |
708 |
|
|
case CONF_DLINE: |
709 |
|
|
ban_type = 'D'; |
710 |
|
|
break; |
711 |
michael |
1644 |
default: break; |
712 |
michael |
1369 |
} |
713 |
michael |
2916 |
|
714 |
michael |
6318 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
715 |
michael |
1369 |
"Temporary %c-line for [%s@%s] expired", ban_type, |
716 |
michael |
1632 |
(arec->conf->user) ? arec->conf->user : "*", |
717 |
|
|
(arec->conf->host) ? arec->conf->host : "*"); |
718 |
michael |
1369 |
} |
719 |
|
|
|
720 |
|
|
void |
721 |
|
|
hostmask_expire_temporary(void) |
722 |
|
|
{ |
723 |
michael |
7914 |
dlink_node *node, *node_next; |
724 |
michael |
1369 |
|
725 |
michael |
3235 |
for (unsigned int i = 0; i < ATABLE_SIZE; ++i) |
726 |
michael |
1369 |
{ |
727 |
michael |
4815 |
DLINK_FOREACH_SAFE(node, node_next, atable[i].head) |
728 |
michael |
1369 |
{ |
729 |
michael |
4815 |
struct AddressRec *arec = node->data; |
730 |
michael |
1369 |
|
731 |
michael |
8437 |
if (arec->conf->until == 0 || arec->conf->until > CurrentTime) |
732 |
michael |
1369 |
continue; |
733 |
|
|
|
734 |
|
|
switch (arec->type) |
735 |
|
|
{ |
736 |
|
|
case CONF_KLINE: |
737 |
|
|
case CONF_DLINE: |
738 |
|
|
hostmask_send_expiration(arec); |
739 |
|
|
|
740 |
|
|
dlinkDelete(&arec->node, &atable[i]); |
741 |
michael |
1632 |
conf_free(arec->conf); |
742 |
michael |
7032 |
xfree(arec); |
743 |
michael |
1369 |
break; |
744 |
michael |
1644 |
default: break; |
745 |
michael |
1369 |
} |
746 |
|
|
} |
747 |
|
|
} |
748 |
|
|
} |