| 23 |
|
*/ |
| 24 |
|
|
| 25 |
|
#include "config.h" |
| 26 |
– |
#ifdef HAVE_LIBPCRE |
| 27 |
– |
#include <pcre.h> |
| 28 |
– |
#endif |
| 29 |
– |
|
| 26 |
|
#include "stdinc.h" |
| 27 |
|
#include "irc_string.h" |
| 32 |
– |
#include "sprintf_irc.h" |
| 28 |
|
|
| 34 |
– |
#ifndef INADDRSZ |
| 35 |
– |
#define INADDRSZ 4 |
| 36 |
– |
#endif |
| 29 |
|
|
| 30 |
< |
#ifndef IN6ADDRSZ |
| 31 |
< |
#define IN6ADDRSZ 16 |
| 32 |
< |
#endif |
| 30 |
> |
int |
| 31 |
> |
has_wildcards(const char *s) |
| 32 |
> |
{ |
| 33 |
> |
char c; |
| 34 |
|
|
| 35 |
< |
#ifndef INT16SZ |
| 36 |
< |
#define INT16SZ 2 |
| 37 |
< |
#endif |
| 35 |
> |
while ((c = *s++)) |
| 36 |
> |
if (IsMWildChar(c)) |
| 37 |
> |
return 1; |
| 38 |
|
|
| 39 |
+ |
return 0; |
| 40 |
+ |
} |
| 41 |
|
|
| 42 |
|
/* |
| 43 |
|
* myctime - This is like standard ctime()-function, but it zaps away |
| 57 |
|
static char buf[32]; |
| 58 |
|
char *p; |
| 59 |
|
|
| 60 |
< |
strcpy(buf, ctime(&value)); |
| 60 |
> |
strlcpy(buf, ctime(&value), sizeof(buf)); |
| 61 |
|
|
| 62 |
|
if ((p = strchr(buf, '\n')) != NULL) |
| 63 |
|
*p = '\0'; |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
/* |
| 73 |
– |
* clean_string - clean up a string possibly containing garbage |
| 74 |
– |
* |
| 75 |
– |
* *sigh* Before the kiddies find this new and exciting way of |
| 76 |
– |
* annoying opers, lets clean up what is sent to local opers |
| 77 |
– |
* -Dianora |
| 78 |
– |
*/ |
| 79 |
– |
char * |
| 80 |
– |
clean_string(char* dest, const unsigned char* src, size_t len) |
| 81 |
– |
{ |
| 82 |
– |
char* d = dest; |
| 83 |
– |
assert(0 != dest); |
| 84 |
– |
assert(0 != src); |
| 85 |
– |
|
| 86 |
– |
if(dest == NULL || src == NULL) |
| 87 |
– |
return NULL; |
| 88 |
– |
|
| 89 |
– |
len -= 3; /* allow for worst case, '^A\0' */ |
| 90 |
– |
|
| 91 |
– |
while (*src && (len > 0)) |
| 92 |
– |
{ |
| 93 |
– |
if (*src & 0x80) /* if high bit is set */ |
| 94 |
– |
{ |
| 95 |
– |
*d++ = '.'; |
| 96 |
– |
--len; |
| 97 |
– |
} |
| 98 |
– |
else if (!IsPrint(*src)) /* if NOT printable */ |
| 99 |
– |
{ |
| 100 |
– |
*d++ = '^'; |
| 101 |
– |
--len; |
| 102 |
– |
*d++ = 0x40 + *src; /* turn it into a printable */ |
| 103 |
– |
} |
| 104 |
– |
else |
| 105 |
– |
*d++ = *src; |
| 106 |
– |
++src, --len; |
| 107 |
– |
} |
| 108 |
– |
*d = '\0'; |
| 109 |
– |
return dest; |
| 110 |
– |
} |
| 111 |
– |
|
| 112 |
– |
/* |
| 68 |
|
* strip_tabs(dst, src, length) |
| 69 |
|
* |
| 70 |
|
* Copies src to dst, while converting all \t (tabs) into spaces. |
| 120 |
|
*save = pos; |
| 121 |
|
return tmp; |
| 122 |
|
} |
| 168 |
– |
|
| 123 |
|
#endif /* !HAVE_STRTOK_R */ |
| 124 |
|
|
| 171 |
– |
/* |
| 172 |
– |
* From: Thomas Helvey <tomh@inxpress.net> |
| 173 |
– |
*/ |
| 174 |
– |
static const char *IpQuadTab[] = |
| 175 |
– |
{ |
| 176 |
– |
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", |
| 177 |
– |
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", |
| 178 |
– |
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29", |
| 179 |
– |
"30", "31", "32", "33", "34", "35", "36", "37", "38", "39", |
| 180 |
– |
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49", |
| 181 |
– |
"50", "51", "52", "53", "54", "55", "56", "57", "58", "59", |
| 182 |
– |
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69", |
| 183 |
– |
"70", "71", "72", "73", "74", "75", "76", "77", "78", "79", |
| 184 |
– |
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89", |
| 185 |
– |
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99", |
| 186 |
– |
"100", "101", "102", "103", "104", "105", "106", "107", "108", "109", |
| 187 |
– |
"110", "111", "112", "113", "114", "115", "116", "117", "118", "119", |
| 188 |
– |
"120", "121", "122", "123", "124", "125", "126", "127", "128", "129", |
| 189 |
– |
"130", "131", "132", "133", "134", "135", "136", "137", "138", "139", |
| 190 |
– |
"140", "141", "142", "143", "144", "145", "146", "147", "148", "149", |
| 191 |
– |
"150", "151", "152", "153", "154", "155", "156", "157", "158", "159", |
| 192 |
– |
"160", "161", "162", "163", "164", "165", "166", "167", "168", "169", |
| 193 |
– |
"170", "171", "172", "173", "174", "175", "176", "177", "178", "179", |
| 194 |
– |
"180", "181", "182", "183", "184", "185", "186", "187", "188", "189", |
| 195 |
– |
"190", "191", "192", "193", "194", "195", "196", "197", "198", "199", |
| 196 |
– |
"200", "201", "202", "203", "204", "205", "206", "207", "208", "209", |
| 197 |
– |
"210", "211", "212", "213", "214", "215", "216", "217", "218", "219", |
| 198 |
– |
"220", "221", "222", "223", "224", "225", "226", "227", "228", "229", |
| 199 |
– |
"230", "231", "232", "233", "234", "235", "236", "237", "238", "239", |
| 200 |
– |
"240", "241", "242", "243", "244", "245", "246", "247", "248", "249", |
| 201 |
– |
"250", "251", "252", "253", "254", "255" |
| 202 |
– |
}; |
| 203 |
– |
|
| 204 |
– |
/* |
| 205 |
– |
* inetntoa - in_addr to string |
| 206 |
– |
* changed name to remove collision possibility and |
| 207 |
– |
* so behaviour is guaranteed to take a pointer arg. |
| 208 |
– |
* -avalon 23/11/92 |
| 209 |
– |
* inet_ntoa -- returned the dotted notation of a given |
| 210 |
– |
* internet number |
| 211 |
– |
* argv 11/90). |
| 212 |
– |
* inet_ntoa -- its broken on some Ultrix/Dynix too. -avalon |
| 213 |
– |
*/ |
| 214 |
– |
const char * |
| 215 |
– |
inetntoa(const char *in) |
| 216 |
– |
{ |
| 217 |
– |
static char buf[16]; |
| 218 |
– |
char *bufptr = buf; |
| 219 |
– |
const unsigned char *a = (const unsigned char *)in; |
| 220 |
– |
const char *n; |
| 221 |
– |
|
| 222 |
– |
n = IpQuadTab[ *a++ ]; |
| 223 |
– |
while (*n) |
| 224 |
– |
*bufptr++ = *n++; |
| 225 |
– |
*bufptr++ = '.'; |
| 226 |
– |
n = IpQuadTab[ *a++ ]; |
| 227 |
– |
while (*n) |
| 228 |
– |
*bufptr++ = *n++; |
| 229 |
– |
*bufptr++ = '.'; |
| 230 |
– |
n = IpQuadTab[ *a++ ]; |
| 231 |
– |
while (*n) |
| 232 |
– |
*bufptr++ = *n++; |
| 233 |
– |
*bufptr++ = '.'; |
| 234 |
– |
n = IpQuadTab[ *a ]; |
| 235 |
– |
while (*n) |
| 236 |
– |
*bufptr++ = *n++; |
| 237 |
– |
*bufptr = '\0'; |
| 238 |
– |
return buf; |
| 239 |
– |
} |
| 240 |
– |
|
| 125 |
|
/* libio_basename() |
| 126 |
|
* |
| 127 |
|
* input - i.e. "/usr/local/ircd/modules/m_whois.so" |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
/* |
| 261 |
– |
* Copyright (c) 1996-1999 by Internet Software Consortium. |
| 262 |
– |
* |
| 263 |
– |
* Permission to use, copy, modify, and distribute this software for any |
| 264 |
– |
* purpose with or without fee is hereby granted, provided that the above |
| 265 |
– |
* copyright notice and this permission notice appear in all copies. |
| 266 |
– |
* |
| 267 |
– |
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS |
| 268 |
– |
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES |
| 269 |
– |
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE |
| 270 |
– |
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 271 |
– |
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 272 |
– |
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
| 273 |
– |
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 274 |
– |
* SOFTWARE. |
| 275 |
– |
*/ |
| 276 |
– |
|
| 277 |
– |
#define SPRINTF(x) ((size_t)ircsprintf x) |
| 278 |
– |
|
| 279 |
– |
/* |
| 280 |
– |
* WARNING: Don't even consider trying to compile this on a system where |
| 281 |
– |
* sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. |
| 282 |
– |
*/ |
| 283 |
– |
|
| 284 |
– |
static const char *inet_ntop4(const unsigned char *src, char *dst, unsigned int size); |
| 285 |
– |
#ifdef IPV6 |
| 286 |
– |
static const char *inet_ntop6(const unsigned char *src, char *dst, unsigned int size); |
| 287 |
– |
#endif |
| 288 |
– |
|
| 289 |
– |
/* const char * |
| 290 |
– |
* inet_ntop4(src, dst, size) |
| 291 |
– |
* format an IPv4 address |
| 292 |
– |
* return: |
| 293 |
– |
* `dst' (as a const) |
| 294 |
– |
* notes: |
| 295 |
– |
* (1) uses no statics |
| 296 |
– |
* (2) takes a unsigned char* not an in_addr as input |
| 297 |
– |
* author: |
| 298 |
– |
* Paul Vixie, 1996. |
| 299 |
– |
*/ |
| 300 |
– |
static const char * |
| 301 |
– |
inet_ntop4(const unsigned char *src, char *dst, unsigned int size) |
| 302 |
– |
{ |
| 303 |
– |
if (size < 16) |
| 304 |
– |
return NULL; |
| 305 |
– |
|
| 306 |
– |
return strcpy(dst, inetntoa((const char *)src)); |
| 307 |
– |
} |
| 308 |
– |
|
| 309 |
– |
/* const char * |
| 310 |
– |
* inet_ntop6(src, dst, size) |
| 311 |
– |
* convert IPv6 binary address into presentation (printable) format |
| 312 |
– |
* author: |
| 313 |
– |
* Paul Vixie, 1996. |
| 314 |
– |
*/ |
| 315 |
– |
#ifdef IPV6 |
| 316 |
– |
static const char * |
| 317 |
– |
inet_ntop6(const unsigned char *src, char *dst, unsigned int size) |
| 318 |
– |
{ |
| 319 |
– |
/* |
| 320 |
– |
* Note that int32_t and int16_t need only be "at least" large enough |
| 321 |
– |
* to contain a value of the specified size. On some systems, like |
| 322 |
– |
* Crays, there is no such thing as an integer variable with 16 bits. |
| 323 |
– |
* Keep this in mind if you think this function should have been coded |
| 324 |
– |
* to use pointer overlays. All the world's not a VAX. |
| 325 |
– |
*/ |
| 326 |
– |
char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp; |
| 327 |
– |
struct { int base, len; } best = {0,0}, cur = {0,0}; |
| 328 |
– |
unsigned int words[IN6ADDRSZ / INT16SZ]; |
| 329 |
– |
int i; |
| 330 |
– |
|
| 331 |
– |
/* |
| 332 |
– |
* Preprocess: |
| 333 |
– |
* Copy the input (bytewise) array into a wordwise array. |
| 334 |
– |
* Find the longest run of 0x00's in src[] for :: shorthanding. |
| 335 |
– |
*/ |
| 336 |
– |
memset(words, '\0', sizeof words); |
| 337 |
– |
for (i = 0; i < IN6ADDRSZ; i += 2) |
| 338 |
– |
words[i / 2] = (src[i] << 8) | src[i + 1]; |
| 339 |
– |
best.base = -1; |
| 340 |
– |
cur.base = -1; |
| 341 |
– |
for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) { |
| 342 |
– |
if (words[i] == 0) { |
| 343 |
– |
if (cur.base == -1) |
| 344 |
– |
cur.base = i, cur.len = 1; |
| 345 |
– |
else |
| 346 |
– |
cur.len++; |
| 347 |
– |
} else { |
| 348 |
– |
if (cur.base != -1) { |
| 349 |
– |
if (best.base == -1 || cur.len > best.len) |
| 350 |
– |
best = cur; |
| 351 |
– |
cur.base = -1; |
| 352 |
– |
} |
| 353 |
– |
} |
| 354 |
– |
} |
| 355 |
– |
if (cur.base != -1) { |
| 356 |
– |
if (best.base == -1 || cur.len > best.len) |
| 357 |
– |
best = cur; |
| 358 |
– |
} |
| 359 |
– |
if (best.base != -1 && best.len < 2) |
| 360 |
– |
best.base = -1; |
| 361 |
– |
|
| 362 |
– |
/* |
| 363 |
– |
* Format the result. |
| 364 |
– |
*/ |
| 365 |
– |
tp = tmp; |
| 366 |
– |
for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) { |
| 367 |
– |
/* Are we inside the best run of 0x00's? */ |
| 368 |
– |
if (best.base != -1 && i >= best.base && |
| 369 |
– |
i < (best.base + best.len)) { |
| 370 |
– |
if (i == best.base) |
| 371 |
– |
*tp++ = ':'; |
| 372 |
– |
continue; |
| 373 |
– |
} |
| 374 |
– |
/* Are we following an initial run of 0x00s or any real hex? */ |
| 375 |
– |
if (i != 0) |
| 376 |
– |
*tp++ = ':'; |
| 377 |
– |
/* Is this address an encapsulated IPv4? */ |
| 378 |
– |
if (i == 6 && best.base == 0 && |
| 379 |
– |
(best.len == 6 || (best.len == 5 && words[5] == 0xffff))) { |
| 380 |
– |
if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp))) |
| 381 |
– |
return (NULL); |
| 382 |
– |
tp += strlen(tp); |
| 383 |
– |
break; |
| 384 |
– |
} |
| 385 |
– |
tp += SPRINTF((tp, "%x", words[i])); |
| 386 |
– |
} |
| 387 |
– |
/* Was it a trailing run of 0x00's? */ |
| 388 |
– |
if (best.base != -1 && (best.base + best.len) == |
| 389 |
– |
(IN6ADDRSZ / INT16SZ)) |
| 390 |
– |
*tp++ = ':'; |
| 391 |
– |
*tp++ = '\0'; |
| 392 |
– |
|
| 393 |
– |
/* |
| 394 |
– |
* Check for overflow, copy, and we're done. |
| 395 |
– |
*/ |
| 396 |
– |
|
| 397 |
– |
assert (tp - tmp >= 0); |
| 398 |
– |
|
| 399 |
– |
if ((unsigned int)(tp - tmp) > size) { |
| 400 |
– |
return (NULL); |
| 401 |
– |
} |
| 402 |
– |
return strcpy(dst, tmp); |
| 403 |
– |
} |
| 404 |
– |
#endif |
| 405 |
– |
|
| 406 |
– |
/* char * |
| 407 |
– |
* inetntop(af, src, dst, size) |
| 408 |
– |
* convert a network format address to presentation format. |
| 409 |
– |
* return: |
| 410 |
– |
* pointer to presentation format address (`dst'), or NULL (see errno). |
| 411 |
– |
* author: |
| 412 |
– |
* Paul Vixie, 1996. |
| 413 |
– |
*/ |
| 414 |
– |
const char * |
| 415 |
– |
inetntop(int af, const void *src, char *dst, unsigned int size) |
| 416 |
– |
{ |
| 417 |
– |
switch (af) |
| 418 |
– |
{ |
| 419 |
– |
case AF_INET: |
| 420 |
– |
return inet_ntop4(src, dst, size); |
| 421 |
– |
#ifdef IPV6 |
| 422 |
– |
case AF_INET6: |
| 423 |
– |
if (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)src) || |
| 424 |
– |
IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)src)) |
| 425 |
– |
return inet_ntop4((const unsigned char *)&((const struct in6_addr *)src)->s6_addr[12], dst, size); |
| 426 |
– |
else |
| 427 |
– |
return inet_ntop6(src, dst, size); |
| 428 |
– |
#endif |
| 429 |
– |
default: |
| 430 |
– |
return NULL; |
| 431 |
– |
} |
| 432 |
– |
/* NOTREACHED */ |
| 433 |
– |
} |
| 434 |
– |
|
| 435 |
– |
/* |
| 145 |
|
* strlcat and strlcpy were ripped from openssh 2.5.1p2 |
| 146 |
|
* They had the following Copyright info: |
| 147 |
|
* |
| 187 |
|
n = siz - dlen; |
| 188 |
|
|
| 189 |
|
if (n == 0) |
| 190 |
< |
return(dlen + strlen(s)); |
| 190 |
> |
return dlen + strlen(s); |
| 191 |
|
|
| 192 |
|
while (*s != '\0') |
| 193 |
|
{ |
| 235 |
|
return s - src - 1; /* count does not include NUL */ |
| 236 |
|
} |
| 237 |
|
#endif |
| 529 |
– |
|
| 530 |
– |
#ifdef HAVE_LIBPCRE |
| 531 |
– |
void * |
| 532 |
– |
ircd_pcre_compile(const char *pattern, const char **errptr) |
| 533 |
– |
{ |
| 534 |
– |
int erroroffset = 0; |
| 535 |
– |
int options = PCRE_EXTRA; |
| 536 |
– |
|
| 537 |
– |
assert(pattern); |
| 538 |
– |
|
| 539 |
– |
return pcre_compile(pattern, options, errptr, &erroroffset, NULL); |
| 540 |
– |
} |
| 541 |
– |
|
| 542 |
– |
int |
| 543 |
– |
ircd_pcre_exec(const void *code, const char *subject) |
| 544 |
– |
{ |
| 545 |
– |
assert(code && subject); |
| 546 |
– |
|
| 547 |
– |
return pcre_exec(code, NULL, subject, strlen(subject), 0, 0, NULL, 0) < 0; |
| 548 |
– |
} |
| 549 |
– |
#endif |