| 1 |
adx |
30 |
/* |
| 2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
adx |
30 |
* |
| 4 |
michael |
5346 |
* Copyright (c) 2001-2015 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 |
4564 |
* 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 |
|
|
/* The mask parser/type determination code... */ |
| 48 |
|
|
|
| 49 |
|
|
/* int try_parse_v6_netmask(const char *, struct irc_ssaddr *, int *); |
| 50 |
|
|
* Input: A possible IPV6 address as a string. |
| 51 |
|
|
* Output: An integer describing whether it is an IPV6 or hostmask, |
| 52 |
|
|
* an address(if it is IPV6), a bitlength(if it is IPV6). |
| 53 |
|
|
* Side effects: None |
| 54 |
|
|
* Comments: Called from parse_netmask |
| 55 |
|
|
*/ |
| 56 |
michael |
2916 |
/* Fixed so ::/0 (any IPv6 address) is valid |
| 57 |
adx |
30 |
Also a bug in DigitParse above. |
| 58 |
|
|
-Gozem 2002-07-19 gozem@linux.nu |
| 59 |
|
|
*/ |
| 60 |
|
|
static int |
| 61 |
|
|
try_parse_v6_netmask(const char *text, struct irc_ssaddr *addr, int *b) |
| 62 |
|
|
{ |
| 63 |
|
|
char c; |
| 64 |
|
|
int d[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 65 |
|
|
int dp = 0; |
| 66 |
|
|
int nyble = 4; |
| 67 |
|
|
int finsert = -1; |
| 68 |
|
|
int bits = 128; |
| 69 |
|
|
int deficit = 0; |
| 70 |
|
|
short dc[8]; |
| 71 |
michael |
4873 |
struct sockaddr_in6 *const v6 = (struct sockaddr_in6 *)addr; |
| 72 |
adx |
30 |
|
| 73 |
michael |
3470 |
for (const char *p = text; (c = *p); ++p) |
| 74 |
michael |
2894 |
{ |
| 75 |
adx |
30 |
if (IsXDigit(c)) |
| 76 |
|
|
{ |
| 77 |
|
|
if (nyble == 0) |
| 78 |
|
|
return HM_HOST; |
| 79 |
|
|
DigitParse(c); |
| 80 |
|
|
d[dp] |= c << (4 * --nyble); |
| 81 |
|
|
} |
| 82 |
|
|
else if (c == ':') |
| 83 |
|
|
{ |
| 84 |
|
|
if (p > text && *(p - 1) == ':') |
| 85 |
|
|
{ |
| 86 |
|
|
if (finsert >= 0) |
| 87 |
|
|
return HM_HOST; |
| 88 |
|
|
finsert = dp; |
| 89 |
|
|
} |
| 90 |
|
|
else |
| 91 |
|
|
{ |
| 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 |
|
|
d[dp] = d[dp] >> 4 * nyble; |
| 95 |
|
|
nyble = 4; |
| 96 |
|
|
if (++dp >= 8) |
| 97 |
|
|
return HM_HOST; |
| 98 |
|
|
} |
| 99 |
|
|
} |
| 100 |
|
|
else if (c == '*') |
| 101 |
|
|
{ |
| 102 |
|
|
/* * must be last, and * is ambiguous if there is a ::... -A1kmm */ |
| 103 |
|
|
if (finsert >= 0 || *(p + 1) || dp == 0 || *(p - 1) != ':') |
| 104 |
|
|
return HM_HOST; |
| 105 |
|
|
bits = dp * 16; |
| 106 |
|
|
} |
| 107 |
|
|
else if (c == '/') |
| 108 |
|
|
{ |
| 109 |
|
|
char *after; |
| 110 |
|
|
|
| 111 |
|
|
d[dp] = d[dp] >> 4 * nyble; |
| 112 |
michael |
3215 |
++dp; |
| 113 |
adx |
30 |
bits = strtoul(p + 1, &after, 10); |
| 114 |
michael |
1298 |
|
| 115 |
adx |
30 |
if (bits < 0 || *after) |
| 116 |
|
|
return HM_HOST; |
| 117 |
|
|
if (bits > dp * 4 && !(finsert >= 0 && bits <= 128)) |
| 118 |
|
|
return HM_HOST; |
| 119 |
|
|
break; |
| 120 |
|
|
} |
| 121 |
|
|
else |
| 122 |
|
|
return HM_HOST; |
| 123 |
michael |
2894 |
} |
| 124 |
adx |
30 |
|
| 125 |
|
|
d[dp] = d[dp] >> 4 * nyble; |
| 126 |
michael |
1298 |
|
| 127 |
adx |
30 |
if (c == 0) |
| 128 |
michael |
3215 |
++dp; |
| 129 |
adx |
30 |
if (finsert < 0 && bits == 0) |
| 130 |
|
|
bits = dp * 16; |
| 131 |
michael |
1298 |
|
| 132 |
adx |
30 |
/* How many words are missing? -A1kmm */ |
| 133 |
|
|
deficit = bits / 16 + ((bits % 16) ? 1 : 0) - dp; |
| 134 |
michael |
1298 |
|
| 135 |
adx |
30 |
/* Now fill in the gaps(from ::) in the copied table... -A1kmm */ |
| 136 |
michael |
3215 |
for (dp = 0, nyble = 0; dp < 8; ++dp) |
| 137 |
adx |
30 |
{ |
| 138 |
|
|
if (nyble == finsert && deficit) |
| 139 |
|
|
{ |
| 140 |
|
|
dc[dp] = 0; |
| 141 |
|
|
deficit--; |
| 142 |
|
|
} |
| 143 |
|
|
else |
| 144 |
|
|
dc[dp] = d[nyble++]; |
| 145 |
|
|
} |
| 146 |
michael |
1298 |
|
| 147 |
adx |
30 |
/* Set unused bits to 0... -A1kmm */ |
| 148 |
|
|
if (bits < 128 && (bits % 16 != 0)) |
| 149 |
|
|
dc[bits / 16] &= ~((1 << (15 - bits % 16)) - 1); |
| 150 |
michael |
3215 |
for (dp = bits / 16 + (bits % 16 ? 1 : 0); dp < 8; ++dp) |
| 151 |
adx |
30 |
dc[dp] = 0; |
| 152 |
michael |
1298 |
|
| 153 |
adx |
30 |
/* And assign... -A1kmm */ |
| 154 |
|
|
if (addr) |
| 155 |
michael |
3215 |
for (dp = 0; dp < 8; ++dp) |
| 156 |
adx |
30 |
/* The cast is a kludge to make netbsd work. */ |
| 157 |
|
|
((unsigned short *)&v6->sin6_addr)[dp] = htons(dc[dp]); |
| 158 |
michael |
1298 |
|
| 159 |
michael |
3215 |
if (b) |
| 160 |
adx |
30 |
*b = bits; |
| 161 |
|
|
return HM_IPV6; |
| 162 |
|
|
} |
| 163 |
|
|
|
| 164 |
|
|
/* int try_parse_v4_netmask(const char *, struct irc_ssaddr *, int *); |
| 165 |
|
|
* Input: A possible IPV4 address as a string. |
| 166 |
|
|
* Output: An integer describing whether it is an IPV4 or hostmask, |
| 167 |
|
|
* an address(if it is IPV4), a bitlength(if it is IPV4). |
| 168 |
|
|
* Side effects: None |
| 169 |
|
|
* Comments: Called from parse_netmask |
| 170 |
|
|
*/ |
| 171 |
|
|
static int |
| 172 |
|
|
try_parse_v4_netmask(const char *text, struct irc_ssaddr *addr, int *b) |
| 173 |
|
|
{ |
| 174 |
|
|
const char *digits[4]; |
| 175 |
|
|
unsigned char addb[4]; |
| 176 |
|
|
int n = 0, bits = 0; |
| 177 |
|
|
char c; |
| 178 |
michael |
4873 |
struct sockaddr_in *const v4 = (struct sockaddr_in *)addr; |
| 179 |
adx |
30 |
|
| 180 |
|
|
digits[n++] = text; |
| 181 |
|
|
|
| 182 |
michael |
3470 |
for (const char *p = text; (c = *p); ++p) |
| 183 |
michael |
2894 |
{ |
| 184 |
adx |
30 |
if (c >= '0' && c <= '9') /* empty */ |
| 185 |
|
|
; |
| 186 |
|
|
else if (c == '.') |
| 187 |
|
|
{ |
| 188 |
|
|
if (n >= 4) |
| 189 |
|
|
return HM_HOST; |
| 190 |
michael |
1298 |
|
| 191 |
adx |
30 |
digits[n++] = p + 1; |
| 192 |
|
|
} |
| 193 |
|
|
else if (c == '*') |
| 194 |
|
|
{ |
| 195 |
|
|
if (*(p + 1) || n == 0 || *(p - 1) != '.') |
| 196 |
|
|
return HM_HOST; |
| 197 |
michael |
1298 |
|
| 198 |
adx |
30 |
bits = (n - 1) * 8; |
| 199 |
|
|
break; |
| 200 |
|
|
} |
| 201 |
|
|
else if (c == '/') |
| 202 |
|
|
{ |
| 203 |
|
|
char *after; |
| 204 |
|
|
bits = strtoul(p + 1, &after, 10); |
| 205 |
michael |
1298 |
|
| 206 |
michael |
1786 |
if (bits < 0 || *after) |
| 207 |
adx |
30 |
return HM_HOST; |
| 208 |
|
|
if (bits > n * 8) |
| 209 |
|
|
return HM_HOST; |
| 210 |
michael |
1298 |
|
| 211 |
adx |
30 |
break; |
| 212 |
|
|
} |
| 213 |
|
|
else |
| 214 |
|
|
return HM_HOST; |
| 215 |
michael |
2894 |
} |
| 216 |
adx |
30 |
|
| 217 |
|
|
if (n < 4 && bits == 0) |
| 218 |
|
|
bits = n * 8; |
| 219 |
|
|
if (bits) |
| 220 |
|
|
while (n < 4) |
| 221 |
|
|
digits[n++] = "0"; |
| 222 |
michael |
1298 |
|
| 223 |
michael |
3215 |
for (n = 0; n < 4; ++n) |
| 224 |
adx |
30 |
addb[n] = strtoul(digits[n], NULL, 10); |
| 225 |
michael |
1298 |
|
| 226 |
adx |
30 |
if (bits == 0) |
| 227 |
|
|
bits = 32; |
| 228 |
michael |
1298 |
|
| 229 |
adx |
30 |
/* Set unused bits to 0... -A1kmm */ |
| 230 |
|
|
if (bits < 32 && bits % 8) |
| 231 |
|
|
addb[bits / 8] &= ~((1 << (8 - bits % 8)) - 1); |
| 232 |
michael |
3215 |
for (n = bits / 8 + (bits % 8 ? 1 : 0); n < 4; ++n) |
| 233 |
adx |
30 |
addb[n] = 0; |
| 234 |
|
|
if (addr) |
| 235 |
|
|
v4->sin_addr.s_addr = |
| 236 |
|
|
htonl(addb[0] << 24 | addb[1] << 16 | addb[2] << 8 | addb[3]); |
| 237 |
michael |
3215 |
if (b) |
| 238 |
adx |
30 |
*b = bits; |
| 239 |
|
|
return HM_IPV4; |
| 240 |
|
|
} |
| 241 |
|
|
|
| 242 |
|
|
/* int parse_netmask(const char *, struct irc_ssaddr *, int *); |
| 243 |
|
|
* Input: A hostmask, or an IPV4/6 address. |
| 244 |
|
|
* Output: An integer describing whether it is an IPV4, IPV6 address or a |
| 245 |
|
|
* hostmask, an address(if it is an IP mask), |
| 246 |
|
|
* a bitlength(if it is IP mask). |
| 247 |
|
|
* Side effects: None |
| 248 |
|
|
*/ |
| 249 |
|
|
int |
| 250 |
|
|
parse_netmask(const char *text, struct irc_ssaddr *addr, int *b) |
| 251 |
|
|
{ |
| 252 |
michael |
2814 |
if (strchr(text, '.')) |
| 253 |
|
|
return try_parse_v4_netmask(text, addr, b); |
| 254 |
michael |
1298 |
if (strchr(text, ':')) |
| 255 |
adx |
30 |
return try_parse_v6_netmask(text, addr, b); |
| 256 |
michael |
4414 |
|
| 257 |
adx |
30 |
return HM_HOST; |
| 258 |
|
|
} |
| 259 |
|
|
|
| 260 |
|
|
/* The address matching stuff... */ |
| 261 |
|
|
/* int match_ipv6(struct irc_ssaddr *, struct irc_ssaddr *, int) |
| 262 |
|
|
* Input: An IP address, an IP mask, the number of bits in the mask. |
| 263 |
|
|
* Output: if match, -1 else 0 |
| 264 |
|
|
* Side effects: None |
| 265 |
|
|
*/ |
| 266 |
|
|
int |
| 267 |
michael |
1455 |
match_ipv6(const struct irc_ssaddr *addr, const struct irc_ssaddr *mask, int bits) |
| 268 |
adx |
30 |
{ |
| 269 |
|
|
int i, m, n = bits / 8; |
| 270 |
michael |
4976 |
const struct sockaddr_in6 *const v6 = (const struct sockaddr_in6 *)addr; |
| 271 |
|
|
const struct sockaddr_in6 *const v6mask = (const struct sockaddr_in6 *)mask; |
| 272 |
adx |
30 |
|
| 273 |
michael |
3215 |
for (i = 0; i < n; ++i) |
| 274 |
adx |
30 |
if (v6->sin6_addr.s6_addr[i] != v6mask->sin6_addr.s6_addr[i]) |
| 275 |
|
|
return 0; |
| 276 |
michael |
3215 |
|
| 277 |
adx |
30 |
if ((m = bits % 8) == 0) |
| 278 |
|
|
return -1; |
| 279 |
|
|
if ((v6->sin6_addr.s6_addr[n] & ~((1 << (8 - m)) - 1)) == |
| 280 |
|
|
v6mask->sin6_addr.s6_addr[n]) |
| 281 |
|
|
return -1; |
| 282 |
|
|
return 0; |
| 283 |
|
|
} |
| 284 |
michael |
1372 |
|
| 285 |
adx |
30 |
/* int match_ipv4(struct irc_ssaddr *, struct irc_ssaddr *, int) |
| 286 |
|
|
* Input: An IP address, an IP mask, the number of bits in the mask. |
| 287 |
|
|
* Output: if match, -1 else 0 |
| 288 |
|
|
* Side Effects: None |
| 289 |
|
|
*/ |
| 290 |
|
|
int |
| 291 |
michael |
1455 |
match_ipv4(const struct irc_ssaddr *addr, const struct irc_ssaddr *mask, int bits) |
| 292 |
adx |
30 |
{ |
| 293 |
michael |
4976 |
const struct sockaddr_in *const v4 = (const struct sockaddr_in *)addr; |
| 294 |
|
|
const struct sockaddr_in *const v4mask = (const struct sockaddr_in *)mask; |
| 295 |
michael |
1298 |
|
| 296 |
adx |
30 |
if ((ntohl(v4->sin_addr.s_addr) & ~((1 << (32 - bits)) - 1)) != |
| 297 |
|
|
ntohl(v4mask->sin_addr.s_addr)) |
| 298 |
|
|
return 0; |
| 299 |
|
|
return -1; |
| 300 |
|
|
} |
| 301 |
|
|
|
| 302 |
|
|
/* |
| 303 |
|
|
* mask_addr |
| 304 |
|
|
* |
| 305 |
|
|
* inputs - pointer to the ip to mask |
| 306 |
|
|
* - bitlen |
| 307 |
|
|
* output - NONE |
| 308 |
|
|
* side effects - |
| 309 |
|
|
*/ |
| 310 |
|
|
void |
| 311 |
|
|
mask_addr(struct irc_ssaddr *ip, int bits) |
| 312 |
|
|
{ |
| 313 |
|
|
int mask; |
| 314 |
|
|
struct sockaddr_in6 *v6_base_ip; |
| 315 |
|
|
int i, m, n; |
| 316 |
|
|
struct sockaddr_in *v4_base_ip; |
| 317 |
|
|
|
| 318 |
michael |
4414 |
if (ip->ss.ss_family == AF_INET) |
| 319 |
adx |
30 |
{ |
| 320 |
michael |
3769 |
uint32_t tmp = 0; |
| 321 |
michael |
1298 |
v4_base_ip = (struct sockaddr_in *)ip; |
| 322 |
|
|
|
| 323 |
adx |
30 |
mask = ~((1 << (32 - bits)) - 1); |
| 324 |
michael |
3769 |
tmp = ntohl(v4_base_ip->sin_addr.s_addr); |
| 325 |
|
|
v4_base_ip->sin_addr.s_addr = htonl(tmp & mask); |
| 326 |
adx |
30 |
} |
| 327 |
|
|
else |
| 328 |
|
|
{ |
| 329 |
|
|
n = bits / 8; |
| 330 |
|
|
m = bits % 8; |
| 331 |
michael |
1298 |
v6_base_ip = (struct sockaddr_in6 *)ip; |
| 332 |
adx |
30 |
|
| 333 |
michael |
4916 |
mask = ~((1 << (8 - m)) - 1); |
| 334 |
adx |
30 |
v6_base_ip->sin6_addr.s6_addr[n] = v6_base_ip->sin6_addr.s6_addr[n] & mask; |
| 335 |
michael |
1298 |
|
| 336 |
db |
300 |
for (i = n + 1; i < 16; i++) |
| 337 |
|
|
v6_base_ip->sin6_addr.s6_addr[i] = 0; |
| 338 |
adx |
30 |
} |
| 339 |
|
|
} |
| 340 |
|
|
|
| 341 |
michael |
4299 |
/* Hashtable stuff...now external as it's used in m_stats.c */ |
| 342 |
michael |
1367 |
dlink_list atable[ATABLE_SIZE]; |
| 343 |
adx |
30 |
|
| 344 |
|
|
/* unsigned long hash_ipv4(struct irc_ssaddr*) |
| 345 |
|
|
* Input: An IP address. |
| 346 |
|
|
* Output: A hash value of the IP address. |
| 347 |
|
|
* Side effects: None |
| 348 |
|
|
*/ |
| 349 |
michael |
1343 |
static uint32_t |
| 350 |
michael |
2898 |
hash_ipv4(const struct irc_ssaddr *addr, int bits) |
| 351 |
adx |
30 |
{ |
| 352 |
|
|
if (bits != 0) |
| 353 |
|
|
{ |
| 354 |
michael |
4873 |
const struct sockaddr_in *const v4 = (const struct sockaddr_in *)addr; |
| 355 |
michael |
1343 |
uint32_t av = ntohl(v4->sin_addr.s_addr) & ~((1 << (32 - bits)) - 1); |
| 356 |
michael |
1298 |
|
| 357 |
|
|
return (av ^ (av >> 12) ^ (av >> 24)) & (ATABLE_SIZE - 1); |
| 358 |
adx |
30 |
} |
| 359 |
|
|
|
| 360 |
michael |
1298 |
return 0; |
| 361 |
adx |
30 |
} |
| 362 |
|
|
|
| 363 |
|
|
/* unsigned long hash_ipv6(struct irc_ssaddr*) |
| 364 |
|
|
* Input: An IP address. |
| 365 |
|
|
* Output: A hash value of the IP address. |
| 366 |
|
|
* Side effects: None |
| 367 |
|
|
*/ |
| 368 |
michael |
1343 |
static uint32_t |
| 369 |
michael |
2898 |
hash_ipv6(const struct irc_ssaddr *addr, int bits) |
| 370 |
adx |
30 |
{ |
| 371 |
michael |
1343 |
uint32_t v = 0, n; |
| 372 |
michael |
4873 |
const struct sockaddr_in6 *const v6 = (const struct sockaddr_in6 *)addr; |
| 373 |
michael |
1298 |
|
| 374 |
michael |
3215 |
for (n = 0; n < 16; ++n) |
| 375 |
adx |
30 |
{ |
| 376 |
|
|
if (bits >= 8) |
| 377 |
|
|
{ |
| 378 |
|
|
v ^= v6->sin6_addr.s6_addr[n]; |
| 379 |
|
|
bits -= 8; |
| 380 |
|
|
} |
| 381 |
|
|
else if (bits) |
| 382 |
|
|
{ |
| 383 |
|
|
v ^= v6->sin6_addr.s6_addr[n] & ~((1 << (8 - bits)) - 1); |
| 384 |
|
|
return v & (ATABLE_SIZE - 1); |
| 385 |
|
|
} |
| 386 |
|
|
else |
| 387 |
|
|
return v & (ATABLE_SIZE - 1); |
| 388 |
|
|
} |
| 389 |
michael |
4414 |
|
| 390 |
adx |
30 |
return v & (ATABLE_SIZE - 1); |
| 391 |
|
|
} |
| 392 |
|
|
|
| 393 |
|
|
/* int hash_text(const char *start) |
| 394 |
|
|
* Input: The start of the text to hash. |
| 395 |
|
|
* Output: The hash of the string between 1 and (TH_MAX-1) |
| 396 |
|
|
* Side-effects: None. |
| 397 |
|
|
*/ |
| 398 |
michael |
1343 |
static uint32_t |
| 399 |
adx |
30 |
hash_text(const char *start) |
| 400 |
|
|
{ |
| 401 |
michael |
1343 |
uint32_t h = 0; |
| 402 |
adx |
30 |
|
| 403 |
michael |
3470 |
for (const char *p = start; *p; ++p) |
| 404 |
michael |
2331 |
h = (h << 4) - (h + ToLower(*p)); |
| 405 |
michael |
1298 |
|
| 406 |
|
|
return h & (ATABLE_SIZE - 1); |
| 407 |
adx |
30 |
} |
| 408 |
|
|
|
| 409 |
|
|
/* unsigned long get_hash_mask(const char *) |
| 410 |
|
|
* Input: The text to hash. |
| 411 |
|
|
* Output: The hash of the string right of the first '.' past the last |
| 412 |
|
|
* wildcard in the string. |
| 413 |
|
|
* Side-effects: None. |
| 414 |
|
|
*/ |
| 415 |
michael |
1343 |
static uint32_t |
| 416 |
adx |
30 |
get_mask_hash(const char *text) |
| 417 |
|
|
{ |
| 418 |
|
|
const char *hp = "", *p; |
| 419 |
|
|
|
| 420 |
michael |
3215 |
for (p = text + strlen(text) - 1; p >= text; --p) |
| 421 |
adx |
723 |
if (IsMWildChar(*p)) |
| 422 |
adx |
30 |
return hash_text(hp); |
| 423 |
|
|
else if (*p == '.') |
| 424 |
|
|
hp = p + 1; |
| 425 |
|
|
return hash_text(text); |
| 426 |
|
|
} |
| 427 |
|
|
|
| 428 |
michael |
1632 |
/* struct MaskItem *find_conf_by_address(const char *, struct irc_ssaddr *, |
| 429 |
adx |
30 |
* int type, int fam, const char *username) |
| 430 |
|
|
* Input: The hostname, the address, the type of mask to find, the address |
| 431 |
|
|
* family, the username. |
| 432 |
|
|
* Output: The matching value with the highest precedence. |
| 433 |
|
|
* Side-effects: None |
| 434 |
|
|
* Note: Setting bit 0 of the type means that the username is ignored. |
| 435 |
|
|
* Warning: IsNeedPassword for everything that is not an auth{} entry |
| 436 |
michael |
1632 |
* should always be true (i.e. conf->flags & CONF_FLAGS_NEED_PASSWORD == 0) |
| 437 |
adx |
30 |
*/ |
| 438 |
michael |
1632 |
struct MaskItem * |
| 439 |
michael |
1369 |
find_conf_by_address(const char *name, struct irc_ssaddr *addr, unsigned int type, |
| 440 |
michael |
1371 |
int fam, const char *username, const char *password, int do_match) |
| 441 |
adx |
30 |
{ |
| 442 |
michael |
1343 |
unsigned int hprecv = 0; |
| 443 |
michael |
4816 |
dlink_node *node = NULL; |
| 444 |
michael |
1632 |
struct MaskItem *hprec = NULL; |
| 445 |
|
|
struct AddressRec *arec = NULL; |
| 446 |
adx |
30 |
int b; |
| 447 |
michael |
1371 |
int (*cmpfunc)(const char *, const char *) = do_match ? match : irccmp; |
| 448 |
adx |
30 |
|
| 449 |
|
|
if (addr) |
| 450 |
|
|
{ |
| 451 |
|
|
/* Check for IPV6 matches... */ |
| 452 |
|
|
if (fam == AF_INET6) |
| 453 |
|
|
{ |
| 454 |
|
|
for (b = 128; b >= 0; b -= 16) |
| 455 |
|
|
{ |
| 456 |
michael |
4816 |
DLINK_FOREACH(node, atable[hash_ipv6(addr, b)].head) |
| 457 |
michael |
1367 |
{ |
| 458 |
michael |
4816 |
arec = node->data; |
| 459 |
michael |
1367 |
|
| 460 |
michael |
2995 |
if ((arec->type == type) && |
| 461 |
adx |
30 |
arec->precedence > hprecv && |
| 462 |
|
|
arec->masktype == HM_IPV6 && |
| 463 |
|
|
match_ipv6(addr, &arec->Mask.ipa.addr, |
| 464 |
|
|
arec->Mask.ipa.bits) && |
| 465 |
michael |
2995 |
(!username || !cmpfunc(arec->username, username)) && |
| 466 |
michael |
2916 |
(IsNeedPassword(arec->conf) || arec->conf->passwd == NULL || |
| 467 |
|
|
match_conf_password(password, arec->conf))) |
| 468 |
adx |
30 |
{ |
| 469 |
|
|
hprecv = arec->precedence; |
| 470 |
michael |
1632 |
hprec = arec->conf; |
| 471 |
adx |
30 |
} |
| 472 |
michael |
1367 |
} |
| 473 |
adx |
30 |
} |
| 474 |
|
|
} |
| 475 |
michael |
4414 |
else if (fam == AF_INET) |
| 476 |
adx |
30 |
{ |
| 477 |
|
|
for (b = 32; b >= 0; b -= 8) |
| 478 |
|
|
{ |
| 479 |
michael |
4816 |
DLINK_FOREACH(node, atable[hash_ipv4(addr, b)].head) |
| 480 |
michael |
1367 |
{ |
| 481 |
michael |
4816 |
arec = node->data; |
| 482 |
michael |
1367 |
|
| 483 |
michael |
2995 |
if ((arec->type == type) && |
| 484 |
adx |
30 |
arec->precedence > hprecv && |
| 485 |
|
|
arec->masktype == HM_IPV4 && |
| 486 |
|
|
match_ipv4(addr, &arec->Mask.ipa.addr, |
| 487 |
|
|
arec->Mask.ipa.bits) && |
| 488 |
michael |
2995 |
(!username || !cmpfunc(arec->username, username)) && |
| 489 |
michael |
2916 |
(IsNeedPassword(arec->conf) || arec->conf->passwd == NULL || |
| 490 |
|
|
match_conf_password(password, arec->conf))) |
| 491 |
adx |
30 |
{ |
| 492 |
|
|
hprecv = arec->precedence; |
| 493 |
michael |
1632 |
hprec = arec->conf; |
| 494 |
adx |
30 |
} |
| 495 |
michael |
1367 |
} |
| 496 |
adx |
30 |
} |
| 497 |
|
|
} |
| 498 |
|
|
} |
| 499 |
|
|
|
| 500 |
michael |
3215 |
if (name) |
| 501 |
adx |
30 |
{ |
| 502 |
|
|
const char *p = name; |
| 503 |
|
|
|
| 504 |
|
|
while (1) |
| 505 |
|
|
{ |
| 506 |
michael |
4816 |
DLINK_FOREACH(node, atable[hash_text(p)].head) |
| 507 |
michael |
1367 |
{ |
| 508 |
michael |
4816 |
arec = node->data; |
| 509 |
michael |
2995 |
if ((arec->type == type) && |
| 510 |
adx |
30 |
arec->precedence > hprecv && |
| 511 |
|
|
(arec->masktype == HM_HOST) && |
| 512 |
michael |
1652 |
!cmpfunc(arec->Mask.hostname, name) && |
| 513 |
michael |
2995 |
(!username || !cmpfunc(arec->username, username)) && |
| 514 |
michael |
1632 |
(IsNeedPassword(arec->conf) || arec->conf->passwd == NULL || |
| 515 |
|
|
match_conf_password(password, arec->conf))) |
| 516 |
adx |
30 |
{ |
| 517 |
|
|
hprecv = arec->precedence; |
| 518 |
michael |
1632 |
hprec = arec->conf; |
| 519 |
adx |
30 |
} |
| 520 |
michael |
1367 |
} |
| 521 |
michael |
3215 |
|
| 522 |
|
|
if ((p = strchr(p, '.')) == NULL) |
| 523 |
adx |
30 |
break; |
| 524 |
michael |
3215 |
++p; |
| 525 |
adx |
30 |
} |
| 526 |
michael |
1367 |
|
| 527 |
michael |
4816 |
DLINK_FOREACH(node, atable[0].head) |
| 528 |
michael |
1367 |
{ |
| 529 |
michael |
4816 |
arec = node->data; |
| 530 |
michael |
1367 |
|
| 531 |
michael |
2995 |
if (arec->type == type && |
| 532 |
adx |
30 |
arec->precedence > hprecv && |
| 533 |
|
|
arec->masktype == HM_HOST && |
| 534 |
michael |
1652 |
!cmpfunc(arec->Mask.hostname, name) && |
| 535 |
michael |
2995 |
(!username || !cmpfunc(arec->username, username)) && |
| 536 |
michael |
1632 |
(IsNeedPassword(arec->conf) || arec->conf->passwd == NULL || |
| 537 |
|
|
match_conf_password(password, arec->conf))) |
| 538 |
adx |
30 |
{ |
| 539 |
|
|
hprecv = arec->precedence; |
| 540 |
michael |
1632 |
hprec = arec->conf; |
| 541 |
adx |
30 |
} |
| 542 |
michael |
1367 |
} |
| 543 |
adx |
30 |
} |
| 544 |
|
|
|
| 545 |
|
|
return hprec; |
| 546 |
|
|
} |
| 547 |
|
|
|
| 548 |
michael |
1632 |
/* struct MaskItem* find_address_conf(const char*, const char*, |
| 549 |
adx |
30 |
* struct irc_ssaddr*, int, char *); |
| 550 |
|
|
* Input: The hostname, username, address, address family. |
| 551 |
michael |
1632 |
* Output: The applicable MaskItem. |
| 552 |
adx |
30 |
* Side-effects: None |
| 553 |
|
|
*/ |
| 554 |
michael |
1632 |
struct MaskItem * |
| 555 |
michael |
3554 |
find_address_conf(const char *host, const char *user, struct irc_ssaddr *ip, |
| 556 |
|
|
int aftype, const char *password) |
| 557 |
adx |
30 |
{ |
| 558 |
michael |
1632 |
struct MaskItem *authcnf = NULL, *killcnf = NULL; |
| 559 |
adx |
30 |
|
| 560 |
michael |
1426 |
/* Find the best auth{} block... If none, return NULL -A1kmm */ |
| 561 |
michael |
1632 |
if ((authcnf = find_conf_by_address(host, ip, CONF_CLIENT, aftype, user, |
| 562 |
|
|
password, 1)) == NULL) |
| 563 |
michael |
1298 |
return NULL; |
| 564 |
adx |
30 |
|
| 565 |
michael |
1426 |
/* If they are exempt from K-lines, return the best auth{} block. -A1kmm */ |
| 566 |
michael |
1632 |
if (IsConfExemptKline(authcnf)) |
| 567 |
|
|
return authcnf; |
| 568 |
adx |
30 |
|
| 569 |
|
|
/* Find the best K-line... -A1kmm */ |
| 570 |
michael |
1632 |
killcnf = find_conf_by_address(host, ip, CONF_KLINE, aftype, user, NULL, 1); |
| 571 |
adx |
30 |
|
| 572 |
michael |
1426 |
/* |
| 573 |
|
|
* If they are K-lined, return the K-line. Otherwise, return the |
| 574 |
|
|
* auth{} block. -A1kmm |
| 575 |
|
|
*/ |
| 576 |
michael |
3215 |
if (killcnf) |
| 577 |
michael |
1632 |
return killcnf; |
| 578 |
adx |
30 |
|
| 579 |
michael |
1632 |
if (IsConfExemptGline(authcnf)) |
| 580 |
|
|
return authcnf; |
| 581 |
adx |
30 |
|
| 582 |
michael |
1632 |
killcnf = find_conf_by_address(host, ip, CONF_GLINE, aftype, user, NULL, 1); |
| 583 |
michael |
3215 |
if (killcnf) |
| 584 |
michael |
1632 |
return killcnf; |
| 585 |
|
|
|
| 586 |
|
|
return authcnf; |
| 587 |
adx |
30 |
} |
| 588 |
|
|
|
| 589 |
michael |
1632 |
/* struct MaskItem* find_dline_conf(struct irc_ssaddr*, int) |
| 590 |
adx |
30 |
* |
| 591 |
|
|
* Input: An address, an address family. |
| 592 |
|
|
* Output: The best matching D-line or exempt line. |
| 593 |
|
|
* Side effects: None. |
| 594 |
|
|
*/ |
| 595 |
michael |
1632 |
struct MaskItem * |
| 596 |
adx |
30 |
find_dline_conf(struct irc_ssaddr *addr, int aftype) |
| 597 |
|
|
{ |
| 598 |
michael |
1632 |
struct MaskItem *eline; |
| 599 |
adx |
30 |
|
| 600 |
michael |
2995 |
eline = find_conf_by_address(NULL, addr, CONF_EXEMPT, aftype, NULL, NULL, 1); |
| 601 |
michael |
3215 |
if (eline) |
| 602 |
michael |
1298 |
return eline; |
| 603 |
|
|
|
| 604 |
michael |
2995 |
return find_conf_by_address(NULL, addr, CONF_DLINE, aftype, NULL, NULL, 1); |
| 605 |
adx |
30 |
} |
| 606 |
|
|
|
| 607 |
michael |
1632 |
/* void add_conf_by_address(int, struct MaskItem *aconf) |
| 608 |
michael |
2916 |
* Input: |
| 609 |
adx |
30 |
* Output: None |
| 610 |
|
|
* Side-effects: Adds this entry to the hash table. |
| 611 |
|
|
*/ |
| 612 |
michael |
2811 |
struct AddressRec * |
| 613 |
michael |
1632 |
add_conf_by_address(const unsigned int type, struct MaskItem *conf) |
| 614 |
adx |
30 |
{ |
| 615 |
michael |
4976 |
struct AddressRec *arec = NULL; |
| 616 |
|
|
const char *const hostname = conf->host; |
| 617 |
|
|
const char *const username = conf->user; |
| 618 |
michael |
1343 |
static unsigned int prec_value = 0xFFFFFFFF; |
| 619 |
michael |
1367 |
int bits = 0; |
| 620 |
adx |
30 |
|
| 621 |
michael |
3003 |
assert(type && !EmptyString(hostname)); |
| 622 |
adx |
30 |
|
| 623 |
michael |
3505 |
arec = MyCalloc(sizeof(struct AddressRec)); |
| 624 |
michael |
3001 |
arec->masktype = parse_netmask(hostname, &arec->Mask.ipa.addr, &bits); |
| 625 |
adx |
30 |
arec->Mask.ipa.bits = bits; |
| 626 |
michael |
1367 |
arec->username = username; |
| 627 |
michael |
1632 |
arec->conf = conf; |
| 628 |
michael |
1367 |
arec->precedence = prec_value--; |
| 629 |
|
|
arec->type = type; |
| 630 |
michael |
1298 |
|
| 631 |
michael |
1367 |
switch (arec->masktype) |
| 632 |
|
|
{ |
| 633 |
|
|
case HM_IPV4: |
| 634 |
|
|
/* We have to do this, since we do not re-hash for every bit -A1kmm. */ |
| 635 |
|
|
bits -= bits % 8; |
| 636 |
|
|
dlinkAdd(arec, &arec->node, &atable[hash_ipv4(&arec->Mask.ipa.addr, bits)]); |
| 637 |
|
|
break; |
| 638 |
|
|
case HM_IPV6: |
| 639 |
|
|
/* We have to do this, since we do not re-hash for every bit -A1kmm. */ |
| 640 |
|
|
bits -= bits % 16; |
| 641 |
|
|
dlinkAdd(arec, &arec->node, &atable[hash_ipv6(&arec->Mask.ipa.addr, bits)]); |
| 642 |
|
|
break; |
| 643 |
|
|
default: /* HM_HOST */ |
| 644 |
michael |
3001 |
arec->Mask.hostname = hostname; |
| 645 |
|
|
dlinkAdd(arec, &arec->node, &atable[get_mask_hash(hostname)]); |
| 646 |
michael |
1367 |
break; |
| 647 |
adx |
30 |
} |
| 648 |
michael |
2811 |
|
| 649 |
|
|
return arec; |
| 650 |
adx |
30 |
} |
| 651 |
|
|
|
| 652 |
michael |
1632 |
/* void delete_one_address(const char*, struct MaskItem*) |
| 653 |
|
|
* Input: An address string, the associated MaskItem. |
| 654 |
adx |
30 |
* Output: None |
| 655 |
michael |
1632 |
* Side effects: Deletes an address record. Frees the MaskItem if there |
| 656 |
adx |
30 |
* is nothing referencing it, sets it as illegal otherwise. |
| 657 |
|
|
*/ |
| 658 |
|
|
void |
| 659 |
michael |
1632 |
delete_one_address_conf(const char *address, struct MaskItem *conf) |
| 660 |
adx |
30 |
{ |
| 661 |
michael |
1367 |
int bits = 0; |
| 662 |
|
|
uint32_t hv = 0; |
| 663 |
michael |
4816 |
dlink_node *node = NULL; |
| 664 |
adx |
30 |
struct irc_ssaddr addr; |
| 665 |
michael |
1298 |
|
| 666 |
michael |
1367 |
switch (parse_netmask(address, &addr, &bits)) |
| 667 |
|
|
{ |
| 668 |
|
|
case HM_IPV4: |
| 669 |
|
|
/* We have to do this, since we do not re-hash for every bit -A1kmm. */ |
| 670 |
|
|
bits -= bits % 8; |
| 671 |
|
|
hv = hash_ipv4(&addr, bits); |
| 672 |
|
|
break; |
| 673 |
|
|
case HM_IPV6: |
| 674 |
|
|
/* We have to do this, since we do not re-hash for every bit -A1kmm. */ |
| 675 |
|
|
bits -= bits % 16; |
| 676 |
|
|
hv = hash_ipv6(&addr, bits); |
| 677 |
|
|
break; |
| 678 |
|
|
default: /* HM_HOST */ |
| 679 |
|
|
hv = get_mask_hash(address); |
| 680 |
|
|
break; |
| 681 |
adx |
30 |
} |
| 682 |
michael |
1298 |
|
| 683 |
michael |
4816 |
DLINK_FOREACH(node, atable[hv].head) |
| 684 |
adx |
30 |
{ |
| 685 |
michael |
4816 |
struct AddressRec *arec = node->data; |
| 686 |
michael |
1367 |
|
| 687 |
michael |
1632 |
if (arec->conf == conf) |
| 688 |
adx |
30 |
{ |
| 689 |
michael |
1367 |
dlinkDelete(&arec->node, &atable[hv]); |
| 690 |
michael |
1298 |
|
| 691 |
michael |
1644 |
if (!conf->ref_count) |
| 692 |
michael |
1632 |
conf_free(conf); |
| 693 |
michael |
1298 |
|
| 694 |
adx |
30 |
MyFree(arec); |
| 695 |
|
|
return; |
| 696 |
|
|
} |
| 697 |
|
|
} |
| 698 |
|
|
} |
| 699 |
|
|
|
| 700 |
|
|
/* void clear_out_address_conf(void) |
| 701 |
|
|
* Input: None |
| 702 |
|
|
* Output: None |
| 703 |
|
|
* Side effects: Clears out all address records in the hash table, |
| 704 |
michael |
1632 |
* frees them, and frees the MaskItems if nothing references |
| 705 |
michael |
2916 |
* them, otherwise sets them as illegal. |
| 706 |
adx |
30 |
*/ |
| 707 |
|
|
void |
| 708 |
|
|
clear_out_address_conf(void) |
| 709 |
|
|
{ |
| 710 |
michael |
4816 |
dlink_node *node = NULL, *node_next = NULL; |
| 711 |
michael |
2916 |
|
| 712 |
michael |
3235 |
for (unsigned int i = 0; i < ATABLE_SIZE; ++i) |
| 713 |
adx |
30 |
{ |
| 714 |
michael |
4816 |
DLINK_FOREACH_SAFE(node, node_next, atable[i].head) |
| 715 |
adx |
30 |
{ |
| 716 |
michael |
4816 |
struct AddressRec *arec = node->data; |
| 717 |
michael |
1367 |
|
| 718 |
michael |
1628 |
/* |
| 719 |
|
|
* We keep the temporary K-lines and destroy the permanent ones, |
| 720 |
|
|
* just to be confusing :) -A1kmm |
| 721 |
|
|
*/ |
| 722 |
michael |
1649 |
if (arec->conf->until || IsConfDatabase(arec->conf)) |
| 723 |
michael |
1628 |
continue; |
| 724 |
michael |
1622 |
|
| 725 |
michael |
1628 |
dlinkDelete(&arec->node, &atable[i]); |
| 726 |
michael |
4522 |
arec->conf->active = 0; |
| 727 |
michael |
1298 |
|
| 728 |
michael |
1644 |
if (!arec->conf->ref_count) |
| 729 |
michael |
1632 |
conf_free(arec->conf); |
| 730 |
michael |
1628 |
MyFree(arec); |
| 731 |
adx |
30 |
} |
| 732 |
|
|
} |
| 733 |
|
|
} |
| 734 |
|
|
|
| 735 |
michael |
1369 |
static void |
| 736 |
michael |
4976 |
hostmask_send_expiration(const struct AddressRec *const arec) |
| 737 |
michael |
1369 |
{ |
| 738 |
|
|
char ban_type = '\0'; |
| 739 |
|
|
|
| 740 |
michael |
4341 |
if (!ConfigGeneral.tkline_expire_notices) |
| 741 |
michael |
1369 |
return; |
| 742 |
|
|
|
| 743 |
|
|
switch (arec->type) |
| 744 |
|
|
{ |
| 745 |
|
|
case CONF_KLINE: |
| 746 |
|
|
ban_type = 'K'; |
| 747 |
|
|
break; |
| 748 |
|
|
case CONF_DLINE: |
| 749 |
|
|
ban_type = 'D'; |
| 750 |
|
|
break; |
| 751 |
|
|
case CONF_GLINE: |
| 752 |
|
|
ban_type = 'G'; |
| 753 |
|
|
break; |
| 754 |
michael |
1644 |
default: break; |
| 755 |
michael |
1369 |
} |
| 756 |
michael |
2916 |
|
| 757 |
michael |
1618 |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
| 758 |
michael |
1369 |
"Temporary %c-line for [%s@%s] expired", ban_type, |
| 759 |
michael |
1632 |
(arec->conf->user) ? arec->conf->user : "*", |
| 760 |
|
|
(arec->conf->host) ? arec->conf->host : "*"); |
| 761 |
michael |
1369 |
} |
| 762 |
|
|
|
| 763 |
|
|
void |
| 764 |
|
|
hostmask_expire_temporary(void) |
| 765 |
|
|
{ |
| 766 |
michael |
4816 |
dlink_node *node = NULL, *node_next = NULL; |
| 767 |
michael |
1369 |
|
| 768 |
michael |
3235 |
for (unsigned int i = 0; i < ATABLE_SIZE; ++i) |
| 769 |
michael |
1369 |
{ |
| 770 |
michael |
4816 |
DLINK_FOREACH_SAFE(node, node_next, atable[i].head) |
| 771 |
michael |
1369 |
{ |
| 772 |
michael |
4816 |
struct AddressRec *arec = node->data; |
| 773 |
michael |
1369 |
|
| 774 |
michael |
1649 |
if (!arec->conf->until || arec->conf->until > CurrentTime) |
| 775 |
michael |
1369 |
continue; |
| 776 |
|
|
|
| 777 |
|
|
switch (arec->type) |
| 778 |
|
|
{ |
| 779 |
|
|
case CONF_KLINE: |
| 780 |
|
|
case CONF_DLINE: |
| 781 |
|
|
case CONF_GLINE: |
| 782 |
|
|
hostmask_send_expiration(arec); |
| 783 |
|
|
|
| 784 |
|
|
dlinkDelete(&arec->node, &atable[i]); |
| 785 |
michael |
1632 |
conf_free(arec->conf); |
| 786 |
michael |
1369 |
MyFree(arec); |
| 787 |
|
|
break; |
| 788 |
michael |
1644 |
default: break; |
| 789 |
michael |
1369 |
} |
| 790 |
|
|
} |
| 791 |
|
|
} |
| 792 |
|
|
} |