| 1 | /* |
| 2 | * Copyright (C) 2000 Gediminas ([email protected]) |
| 3 | * Copyright (C) 2002 Bartomiej Butyn ([email protected]) |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | #include "common/setup_before.h" |
| 20 | #define IPBAN_INTERNAL_ACCESS |
| 21 | #include "ipban.h" |
| 22 | |
| 23 | #include <cctype> |
| 24 | #include <cerrno> |
| 25 | #include <cinttypes> |
| 26 | #include <cstdint> |
| 27 | #include <cstdio> |
| 28 | #include <cstdlib> |
| 29 | #include <cstring> |
| 30 | #include <vector> |
| 31 | |
| 32 | #include "compat/strsep.h" |
| 33 | #include "compat/strcasecmp.h" |
| 34 | |
| 35 | #include "common/list.h" |
| 36 | #include "common/util.h" |
| 37 | #include "common/eventlog.h" |
| 38 | #include "common/xalloc.h" |
| 39 | #include "common/field_sizes.h" |
| 40 | #include "common/xstring.h" |
| 41 | |
| 42 | #include "message.h" |
| 43 | #include "server.h" |
| 44 | #include "prefs.h" |
| 45 | #include "connection.h" |
| 46 | |
| 47 | #include "helpfile.h" |
| 48 | #include "command.h" |
| 49 | #include "i18n.h" |
| 50 | #include "common/setup_after.h" |
| 51 | |
| 52 | namespace pvpgn |
| 53 | { |
| 54 | |
| 55 | namespace bnetd |
| 56 | { |
| 57 | |
| 58 | static int identify_ipban_function(const char * funcstr); |
| 59 | static int ipban_func_del(t_connection * c, char const * cp); |
| 60 | static int ipban_func_list(t_connection * c); |
| 61 | static int ipban_func_check(t_connection * c, char const * cp); |
| 62 | static int ipban_unload_entry(t_ipban_entry * e); |
| 63 | static int ipban_identical_entry(t_ipban_entry * e1, t_ipban_entry * e2); |
| 64 | static t_ipban_entry * ipban_str_to_ipban_entry(char const * cp); |
| 65 | static char * ipban_entry_to_str(t_ipban_entry const * entry); |
| 66 | static unsigned long ipban_str_to_ulong(char const * ipaddr); |
| 67 | static int ipban_could_be_exact_ip_str(char const * str); |
| 68 | static int ipban_could_be_ip_str(char const * ipstr); |
| 69 | static void ipban_usage(t_connection * c); |
| 70 | |
| 71 | static t_list * ipbanlist_head = NULL; |
| 72 | static std::time_t lastchecktime = 0; |
| 73 | |
| 74 | extern int ipbanlist_create(void) |
| 75 | { |
| 76 | ipbanlist_head = list_create(); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | extern int ipbanlist_destroy(void) |
| 82 | { |
| 83 | t_elem * curr; |
| 84 | t_ipban_entry * entry; |
| 85 | |
| 86 | if (ipbanlist_head) |
| 87 | { |
| 88 | LIST_TRAVERSE(ipbanlist_head, curr) |
| 89 | { |
| 90 | entry = (t_ipban_entry*)elem_get_data(curr); |
| 91 | if (!entry) /* should not happen */ |
| 92 | { |
| 93 | eventlog(eventlog_level_error, __FUNCTION__, "ipbanlist contains NULL item"); |
| 94 | continue; |
| 95 | } |
| 96 | if (list_remove_elem(ipbanlist_head, &curr) < 0) |
| 97 | eventlog(eventlog_level_error, __FUNCTION__, "could not remove item from list"); |
| 98 | ipban_unload_entry(entry); |
| 99 | } |
| 100 | if (list_destroy(ipbanlist_head) < 0) |
| 101 | return -1; |
| 102 | ipbanlist_head = NULL; |
| 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | extern int ipbanlist_load(char const * filename) |
| 110 | { |
| 111 | std::FILE * fp; |
| 112 | char * buff; |
| 113 | char * ip; |
| 114 | char * timestr; |
| 115 | unsigned int currline; |
| 116 | unsigned int endtime; |
| 117 | |
| 118 | if (!filename) |
| 119 | { |
| 120 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL filename"); |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | if (!(fp = std::fopen(filename, "r"))) |
| 125 | { |
| 126 | eventlog(eventlog_level_error, __FUNCTION__, "could not open banlist file \"{}\" for reading (std::fopen: {})", filename, std::strerror(errno)); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | for (currline = 1; (buff = file_get_line(fp)); currline++) |
| 131 | { |
| 132 | ip = buff; |
| 133 | |
| 134 | /* eat whitespace in front */ |
| 135 | while (*ip == '\t' || *ip == ' ') ip++; |
| 136 | if (*ip == '\0' || *ip == '#') |
| 137 | { |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | /* eat whitespace in back */ |
| 142 | while (ip[std::strlen(ip) - 1] == ' ' || ip[std::strlen(ip) - 1] == '\t') |
| 143 | ip[std::strlen(ip) - 1] = '\0'; |
| 144 | |
| 145 | if (std::strchr(ip, ' ') || std::strchr(ip, '\t')) |
| 146 | { |
| 147 | timestr = ip; |
| 148 | while (*timestr != ' ' && *timestr != '\t') timestr++; |
| 149 | *timestr = '\0'; |
| 150 | timestr++; |
| 151 | while (*timestr == ' ' || *timestr == '\t') timestr++; |
| 152 | } |
| 153 | else |
| 154 | timestr = NULL; |
| 155 | |
| 156 | if (!timestr) |
| 157 | endtime = 0; |
| 158 | else |
| 159 | if (clockstr_to_seconds(timestr, &endtime) < 0) |
| 160 | { |
| 161 | eventlog(eventlog_level_error, __FUNCTION__, "could not convert to seconds. Banning pernamently."); |
| 162 | endtime = 0; |
| 163 | } |
| 164 | |
| 165 | if (ipbanlist_add(NULL, ip, endtime) != 0) |
| 166 | { |
| 167 | eventlog(eventlog_level_warn, __FUNCTION__, "error in {} at line {}", filename, currline); |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
| 173 | file_get_line(NULL); // clear file_get_line buffer |
| 174 | if (std::fclose(fp) < 0) |
| 175 | eventlog(eventlog_level_error, __FUNCTION__, "could not close banlist file \"{}\" after reading (std::fclose: {})", filename, std::strerror(errno)); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | extern int ipbanlist_save(char const * filename) |
| 182 | { |
| 183 | t_elem const * curr; |
| 184 | t_ipban_entry * entry; |
| 185 | std::FILE * fp; |
| 186 | char * ipstr; |
| 187 | char line[1024]; |
| 188 | |
| 189 | if (!filename) |
| 190 | { |
| 191 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL filename"); |
| 192 | return -1; |
| 193 | } |
| 194 | |
| 195 | if (!(fp = std::fopen(filename, "w"))) |
| 196 | { |
| 197 | eventlog(eventlog_level_error, __FUNCTION__, "could not open banlist file \"{}\" for writing (std::fopen: {})", filename, std::strerror(errno)); |
| 198 | return -1; |
| 199 | } |
| 200 | /* if (ftruncate(fp,0)<0) |
| 201 | { |
| 202 | eventlog(eventlog_level_error,__FUNCTION__,"could not truncate banlist file \"{}\" (ftruncate: {})",filename,std::strerror(errno)); |
| 203 | return -1; |
| 204 | }*/ |
| 205 | |
| 206 | LIST_TRAVERSE_CONST(ipbanlist_head, curr) |
| 207 | { |
| 208 | entry = (t_ipban_entry*)elem_get_data(curr); |
| 209 | if (!entry) |
| 210 | { |
| 211 | eventlog(eventlog_level_error, __FUNCTION__, "ipbanlist contains NULL element"); |
| 212 | continue; |
| 213 | } |
| 214 | if (!(ipstr = ipban_entry_to_str(entry))) |
| 215 | { |
| 216 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL ipstr"); |
| 217 | continue; |
| 218 | } |
| 219 | if (entry->endtime == 0) |
| 220 | std::snprintf(line, sizeof(line), "%s\n", ipstr); |
| 221 | else |
| 222 | std::sprintf(line, "%s %" PRId64 "\n", ipstr, static_cast<std::int64_t>(entry->endtime)); |
| 223 | |
| 224 | if (!(std::fwrite(line, std::strlen(line), 1, fp))) |
| 225 | eventlog(eventlog_level_error, __FUNCTION__, "could not write to banlist file (write: {})", std::strerror(errno)); |
| 226 | xfree(ipstr); |
| 227 | } |
| 228 | |
| 229 | if (std::fclose(fp) < 0) |
| 230 | { |
| 231 | eventlog(eventlog_level_error, __FUNCTION__, "could not close banlist file \"{}\" after writing (std::fclose: {})", filename, std::strerror(errno)); |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | extern int ipbanlist_check(char const * ipaddr) |
| 240 | { |
| 241 | t_elem const * curr; |
| 242 | t_ipban_entry * entry; |
| 243 | char * whole; |
| 244 | char const * ip1; |
| 245 | char const * ip2; |
| 246 | char const * ip3; |
| 247 | char const * ip4; |
| 248 | int counter; |
| 249 | |
| 250 | if (!ipaddr) |
| 251 | { |
| 252 | eventlog(eventlog_level_warn, __FUNCTION__, "got NULL ipaddr"); |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | whole = xstrdup(ipaddr); |
| 257 | |
| 258 | eventlog(eventlog_level_debug, __FUNCTION__, "lastcheck: {}, now: {}, now-lc: {}.", (unsigned)lastchecktime, (unsigned)now, (unsigned)(now - lastchecktime)); |
| 259 | |
| 260 | if (now - lastchecktime >= (signed)prefs_get_ipban_check_int()) /* unsigned; no need to check prefs < 0 */ |
| 261 | { |
| 262 | ipbanlist_unload_expired(); |
| 263 | lastchecktime = now; |
| 264 | } |
| 265 | |
| 266 | ip1 = std::strtok(whole, "."); |
| 267 | ip2 = std::strtok(NULL, "."); |
| 268 | ip3 = std::strtok(NULL, "."); |
| 269 | ip4 = std::strtok(NULL, "."); |
| 270 | |
| 271 | if (!ip1 || !ip2 || !ip3 || !ip4) |
| 272 | { |
| 273 | eventlog(eventlog_level_warn, __FUNCTION__, "got bad IP address \"{}\"", ipaddr); |
| 274 | xfree(whole); |
| 275 | return -1; |
| 276 | } |
| 277 | |
| 278 | eventlog(eventlog_level_debug, __FUNCTION__, "checking {}.{}.{}.{}", ip1, ip2, ip3, ip4); |
| 279 | |
| 280 | counter = 0; |
| 281 | LIST_TRAVERSE_CONST(ipbanlist_head, curr) |
| 282 | { |
| 283 | entry = (t_ipban_entry*)elem_get_data(curr); |
| 284 | if (!entry) |
| 285 | { |
| 286 | eventlog(eventlog_level_error, __FUNCTION__, "ipbanlist contains NULL item"); |
| 287 | xfree(whole); |
| 288 | return -1; |
| 289 | } |
| 290 | counter++; |
| 291 | switch (entry->type) |
| 292 | { |
| 293 | case ipban_type_exact: |
| 294 | if (std::strcmp(entry->info1, ipaddr) == 0) |
| 295 | { |
| 296 | eventlog(eventlog_level_debug, __FUNCTION__, "address {} matched exact {}", ipaddr, entry->info1); |
| 297 | xfree(whole); |
| 298 | return counter; |
| 299 | } |
| 300 | eventlog(eventlog_level_debug, __FUNCTION__, "address {} does not match exact {}", ipaddr, entry->info1); |
| 301 | continue; |
| 302 | |
| 303 | case ipban_type_wildcard: |
| 304 | if (std::strcmp(entry->info1, "*") != 0 && std::strcmp(ip1, entry->info1) != 0) |
| 305 | { |
| 306 | eventlog(eventlog_level_debug, __FUNCTION__, "address {} does not match part 1 of wildcard {}.{}.{}.{}", ipaddr, entry->info1, entry->info2, entry->info3, entry->info4); |
| 307 | continue; |
| 308 | } |
| 309 | if (std::strcmp(entry->info2, "*") != 0 && std::strcmp(ip2, entry->info2) != 0) |
| 310 | { |
| 311 | eventlog(eventlog_level_debug, __FUNCTION__, "address {} does not match part 2 of wildcard {}.{}.{}.{}", ipaddr, entry->info1, entry->info2, entry->info3, entry->info4); |
| 312 | continue; |
| 313 | } |
| 314 | if (std::strcmp(entry->info3, "*") != 0 && std::strcmp(ip3, entry->info3) != 0) |
| 315 | { |
| 316 | eventlog(eventlog_level_debug, __FUNCTION__, "address {} does not match part 3 of wildcard {}.{}.{}.{}", ipaddr, entry->info1, entry->info2, entry->info3, entry->info4); |
| 317 | continue; |
| 318 | } |
| 319 | if (std::strcmp(entry->info4, "*") != 0 && std::strcmp(ip4, entry->info4) != 0) |
| 320 | { |
| 321 | eventlog(eventlog_level_debug, __FUNCTION__, "address {} does not match part 4 of wildcard {}.{}.{}.{}", ipaddr, entry->info1, entry->info2, entry->info3, entry->info4); |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | eventlog(eventlog_level_debug, __FUNCTION__, "address {} matched wildcard {}.{}.{}.{}", ipaddr, entry->info1, entry->info2, entry->info3, entry->info4); |
| 326 | xfree(whole); |
| 327 | return counter; |
| 328 | |
| 329 | case ipban_type_range: |
| 330 | if ((ipban_str_to_ulong(ipaddr) >= ipban_str_to_ulong(entry->info1)) && |
| 331 | (ipban_str_to_ulong(ipaddr) <= ipban_str_to_ulong(entry->info2))) |
| 332 | { |
| 333 | eventlog(eventlog_level_debug, __FUNCTION__, "address {} matched range {}-{}", ipaddr, entry->info1, entry->info2); |
| 334 | xfree(whole); |
| 335 | return counter; |
| 336 | } |
| 337 | eventlog(eventlog_level_debug, __FUNCTION__, "address {} does not match range {}-{}", ipaddr, entry->info1, entry->info2); |
| 338 | continue; |
| 339 | |
| 340 | case ipban_type_netmask: |
| 341 | { |
| 342 | unsigned long lip1; |
| 343 | unsigned long lip2; |
| 344 | unsigned long netmask; |
| 345 | |
| 346 | if (!(lip1 = ipban_str_to_ulong(ipaddr))) |
| 347 | { |
| 348 | xfree(whole); |
| 349 | return -1; |
| 350 | } |
| 351 | |
| 352 | if (!(lip2 = ipban_str_to_ulong(entry->info1))) |
| 353 | { |
| 354 | xfree(whole); |
| 355 | return -1; |
| 356 | } |
| 357 | |
| 358 | if (!(netmask = ipban_str_to_ulong(entry->info2))) |
| 359 | { |
| 360 | xfree(whole); |
| 361 | return -1; |
| 362 | } |
| 363 | |
| 364 | lip1 = lip1 & netmask; |
| 365 | lip2 = lip2 & netmask; |
| 366 | if (lip1 == lip2) |
| 367 | { |
| 368 | eventlog(eventlog_level_debug, __FUNCTION__, "address {} matched netmask {}/{}", ipaddr, entry->info1, entry->info2); |
| 369 | xfree(whole); |
| 370 | return counter; |
| 371 | } |
| 372 | eventlog(eventlog_level_debug, __FUNCTION__, "address {} does not match netmask {}/{}", ipaddr, entry->info1, entry->info2); |
| 373 | continue; |
| 374 | } |
| 375 | |
| 376 | case ipban_type_prefix: |
| 377 | { |
| 378 | unsigned long lip1; |
| 379 | unsigned long lip2; |
| 380 | int prefix; |
| 381 | |
| 382 | if (!(lip1 = ipban_str_to_ulong(ipaddr))) |
| 383 | { |
| 384 | xfree(whole); |
| 385 | return -1; |
| 386 | } |
| 387 | |
| 388 | if (!(lip2 = ipban_str_to_ulong(entry->info1))) |
| 389 | { |
| 390 | xfree(whole); |
| 391 | return -1; |
| 392 | } |
| 393 | |
| 394 | prefix = std::atoi(entry->info2); |
| 395 | |
| 396 | lip1 = lip1 >> (32 - prefix); |
| 397 | lip2 = lip2 >> (32 - prefix); |
| 398 | if (lip1 == lip2) |
| 399 | { |
| 400 | eventlog(eventlog_level_debug, __FUNCTION__, "address {} matched prefix {}/{}", ipaddr, entry->info1, entry->info2); |
| 401 | xfree(whole); |
| 402 | return counter; |
| 403 | } |
| 404 | eventlog(eventlog_level_debug, __FUNCTION__, "address {} does not match prefix {}/{}", ipaddr, entry->info1, entry->info2); |
| 405 | continue; |
| 406 | } |
| 407 | default: /* unknown type */ |
| 408 | eventlog(eventlog_level_warn, __FUNCTION__, "found bad ban type {}", (int)entry->type); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | xfree(whole); |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | extern int ipbanlist_add(t_connection * c, char const * cp, std::time_t endtime) |
| 419 | { |
| 420 | t_ipban_entry * entry; |
| 421 | std::string msgtemp; |
| 422 | |
| 423 | if (!(entry = ipban_str_to_ipban_entry(cp))) |
| 424 | { |
| 425 | if (c) |
| 426 | message_send_text(c, message_type_error, c, localize(c, "Bad IP.")); |
| 427 | eventlog(eventlog_level_error, __FUNCTION__, "could not convert to t_ipban_entry: \"{}\"", cp); |
| 428 | return -1; |
| 429 | } |
| 430 | |
| 431 | entry->endtime = endtime; |
| 432 | list_append_data(ipbanlist_head, entry); |
| 433 | |
| 434 | if (c) |
| 435 | { |
| 436 | |
| 437 | if (endtime == 0) |
| 438 | { |
| 439 | msgtemp = localize(c, "{} banned permamently by {}.", cp, conn_get_username(c)); |
| 440 | eventlog(eventlog_level_info, __FUNCTION__, "{}", msgtemp.c_str()); |
| 441 | message_send_admins(c, message_type_info, msgtemp.c_str()); |
| 442 | msgtemp = localize(c, "{} banned permamently.", cp); |
| 443 | message_send_text(c, message_type_info, c, msgtemp); |
| 444 | } |
| 445 | else |
| 446 | { |
| 447 | msgtemp = localize(c, "{} banned for {} by {}.", cp, seconds_to_timestr(entry->endtime - now), conn_get_username(c)); |
| 448 | eventlog(eventlog_level_info, __FUNCTION__, "{}", msgtemp.c_str()); |
| 449 | message_send_admins(c, message_type_info, msgtemp.c_str()); |
| 450 | msgtemp = localize(c, "{} banned for {}.", cp, seconds_to_timestr(entry->endtime - now)); |
| 451 | message_send_text(c, message_type_info, c, msgtemp); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | |
| 459 | extern int ipbanlist_unload_expired(void) |
| 460 | { |
| 461 | t_elem * curr; |
| 462 | t_ipban_entry * entry; |
| 463 | char removed; |
| 464 | |
| 465 | removed = 0; |
| 466 | LIST_TRAVERSE(ipbanlist_head, curr) |
| 467 | { |
| 468 | entry = (t_ipban_entry*)elem_get_data(curr); |
| 469 | if (!entry) |
| 470 | { |
| 471 | eventlog(eventlog_level_error, __FUNCTION__, "ipbanlist_contains NULL element"); |
| 472 | return -1; |
| 473 | } |
| 474 | if ((entry->endtime - now <= 0) && (entry->endtime != 0)) |
| 475 | { |
| 476 | eventlog(eventlog_level_debug, __FUNCTION__, "removing item: {}", entry->info1); |
| 477 | removed = 1; |
| 478 | if (list_remove_elem(ipbanlist_head, &curr) < 0) |
| 479 | eventlog(eventlog_level_error, __FUNCTION__, "could not remove item"); |
| 480 | else |
| 481 | ipban_unload_entry(entry); |
| 482 | } |
| 483 | } |
| 484 | if (removed == 1) ipbanlist_save(prefs_get_ipbanfile()); |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | extern std::time_t ipbanlist_str_to_time_t(t_connection * c, char const * timestr) |
| 489 | { |
| 490 | |
| 491 | unsigned int bmin; |
| 492 | char minstr[MAX_TIME_STR]; |
| 493 | unsigned int i; |
| 494 | std::string msgtemp; |
| 495 | |
| 496 | for (i = 0; std::isdigit((int)timestr[i]) && i < sizeof(minstr)-1; i++) |
| 497 | minstr[i] = timestr[i]; |
| 498 | minstr[i] = '\0'; |
| 499 | |
| 500 | if (timestr[i] != '\0') |
| 501 | { |
| 502 | if (c) |
| 503 | { |
| 504 | msgtemp = "There was an error in std::time."; |
| 505 | //if (std::strlen(minstr) < 1) |
| 506 | // message_send_text(c, message_type_info, c, localize(c, msgtemp)); |
| 507 | //else |
| 508 | { |
| 509 | msgtemp += " "; |
| 510 | msgtemp += localize(c, "Banning only for: {} minutes.", minstr); |
| 511 | message_send_text(c, message_type_info, c, msgtemp); |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | if (clockstr_to_seconds(minstr, &bmin) < 0) /* it thinks these are seconds but we treat them as minutes */ |
| 517 | { |
| 518 | eventlog(eventlog_level_error, __FUNCTION__, "could not convert to minutes: \"{}\"", timestr); |
| 519 | return -1; |
| 520 | } |
| 521 | if (bmin == 0) |
| 522 | return 0; |
| 523 | else |
| 524 | { |
| 525 | return now + bmin * 60; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | |
| 530 | extern int handle_ipban_command(t_connection * c, char const * text) |
| 531 | { |
| 532 | char const *subcommand, *ipstr, *time; |
| 533 | int result = -1; |
| 534 | |
| 535 | std::vector<std::string> args = split_command(text, 3); |
| 536 | if (args[1].empty()) |
| 537 | { |
| 538 | describe_command(c, args[0].c_str()); |
| 539 | return -1; |
| 540 | } |
| 541 | subcommand = args[1].c_str(); // subcommand |
| 542 | ipstr = args[2].c_str(); // ip address |
| 543 | time = args[3].c_str(); // username |
| 544 | |
| 545 | switch (identify_ipban_function(subcommand)) |
| 546 | { |
| 547 | case IPBAN_FUNC_ADD: |
| 548 | result = ipbanlist_add(c, ipstr, ipbanlist_str_to_time_t(c, time)); |
| 549 | result = ipbanlist_save(prefs_get_ipbanfile()); |
| 550 | break; |
| 551 | case IPBAN_FUNC_DEL: |
| 552 | result = ipban_func_del(c, ipstr); |
| 553 | result = ipbanlist_save(prefs_get_ipbanfile()); |
| 554 | break; |
| 555 | case IPBAN_FUNC_LIST: |
| 556 | result = ipban_func_list(c); |
| 557 | break; |
| 558 | case IPBAN_FUNC_CHECK: |
| 559 | result = ipban_func_check(c, ipstr); |
| 560 | break; |
| 561 | default: |
| 562 | describe_command(c, args[0].c_str()); |
| 563 | } |
| 564 | |
| 565 | return result; |
| 566 | } |
| 567 | |
| 568 | |
| 569 | static int identify_ipban_function(const char * funcstr) |
| 570 | { |
| 571 | if (strcasecmp(funcstr, "add") == 0 || strcasecmp(funcstr, "a") == 0) |
| 572 | return IPBAN_FUNC_ADD; |
| 573 | if (strcasecmp(funcstr, "del") == 0 || strcasecmp(funcstr, "d") == 0) |
| 574 | return IPBAN_FUNC_DEL; |
| 575 | if (strcasecmp(funcstr, "list") == 0 || strcasecmp(funcstr, "l") == 0 || std::strcmp(funcstr, "") == 0) |
| 576 | return IPBAN_FUNC_LIST; |
| 577 | if (strcasecmp(funcstr, "check") == 0 || strcasecmp(funcstr, "c") == 0) |
| 578 | return IPBAN_FUNC_CHECK; |
| 579 | |
| 580 | return IPBAN_FUNC_UNKNOWN; |
| 581 | } |
| 582 | |
| 583 | |
| 584 | static int ipban_func_del(t_connection * c, char const * cp) |
| 585 | { |
| 586 | t_ipban_entry * to_delete; |
| 587 | unsigned int to_delete_nmbr; |
| 588 | t_ipban_entry * entry; |
| 589 | t_elem * curr; |
| 590 | unsigned int counter; |
| 591 | char tstr[MAX_MESSAGE_LEN]; |
| 592 | |
| 593 | counter = 0; |
| 594 | if (std::strchr(cp, '.') || std::strchr(cp, '/') || std::strchr(cp, '*')) |
| 595 | { |
| 596 | if (!(to_delete = ipban_str_to_ipban_entry(cp))) |
| 597 | { |
| 598 | message_send_text(c, message_type_error, c, localize(c, "Illegal IP entry.")); |
| 599 | return -1; |
| 600 | } |
| 601 | LIST_TRAVERSE(ipbanlist_head, curr) |
| 602 | { |
| 603 | entry = (t_ipban_entry*)elem_get_data(curr); |
| 604 | if (!entry) |
| 605 | { |
| 606 | eventlog(eventlog_level_error, __FUNCTION__, "ipbanlist contains NULL item"); |
| 607 | ipban_unload_entry(to_delete); |
| 608 | return -1; |
| 609 | } |
| 610 | if (ipban_identical_entry(to_delete, entry)) |
| 611 | { |
| 612 | counter++; |
| 613 | if (list_remove_elem(ipbanlist_head, &curr) < 0) |
| 614 | eventlog(eventlog_level_error, __FUNCTION__, "could not remove item"); |
| 615 | else |
| 616 | ipban_unload_entry(entry); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | ipban_unload_entry(to_delete); |
| 621 | if (counter == 0) |
| 622 | { |
| 623 | message_send_text(c, message_type_error, c, localize(c, "No matching entry.")); |
| 624 | return -1; |
| 625 | } |
| 626 | else |
| 627 | { |
| 628 | if (counter == 1) |
| 629 | std::sprintf(tstr, "Entry deleted."); |
| 630 | else |
| 631 | std::sprintf(tstr, "Deleted %u entries.", counter); |
| 632 | message_send_text(c, message_type_info, c, tstr); |
| 633 | return 0; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | to_delete_nmbr = std::atoi(cp); |
| 638 | if (to_delete_nmbr <= 0) |
| 639 | { |
| 640 | message_send_text(c, message_type_error, c, localize(c, "Wrong entry number.")); |
| 641 | return -1; |
| 642 | } |
| 643 | LIST_TRAVERSE(ipbanlist_head, curr) |
| 644 | { |
| 645 | if (to_delete_nmbr == ++counter) |
| 646 | { |
| 647 | entry = (t_ipban_entry*)elem_get_data(curr); |
| 648 | if (!entry) |
| 649 | { |
| 650 | eventlog(eventlog_level_error, __FUNCTION__, "ipbanlist contains NULL item"); |
| 651 | return -1; |
| 652 | } |
| 653 | if (list_remove_elem(ipbanlist_head, &curr)<0) |
| 654 | eventlog(eventlog_level_error, __FUNCTION__, "could not remove item"); |
| 655 | else |
| 656 | { |
| 657 | ipban_unload_entry(entry); |
| 658 | message_send_text(c, message_type_info, c, localize(c, "Entry deleted.")); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | if (to_delete_nmbr > counter) |
| 664 | { |
| 665 | std::sprintf(tstr, "There are only %u entries.", counter); |
| 666 | message_send_text(c, message_type_error, c, tstr); |
| 667 | return -1; |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | |
| 674 | static int ipban_func_list(t_connection * c) |
| 675 | { |
| 676 | t_elem const * curr; |
| 677 | t_ipban_entry * entry; |
| 678 | char tstr[MAX_MESSAGE_LEN]; |
| 679 | unsigned int counter; |
| 680 | char timestr[50]; |
| 681 | char * ipstr; |
| 682 | |
| 683 | counter = 0; |
| 684 | message_send_text(c, message_type_info, c, localize(c, "Banned IPs:")); |
| 685 | LIST_TRAVERSE_CONST(ipbanlist_head, curr) |
| 686 | { |
| 687 | entry = (t_ipban_entry*)elem_get_data(curr); |
| 688 | if (!entry) |
| 689 | { |
| 690 | eventlog(eventlog_level_error, __FUNCTION__, "ipbanlist contains NULL item"); |
| 691 | return -1; |
| 692 | } |
| 693 | counter++; |
| 694 | if (entry->endtime == 0) |
| 695 | std::sprintf(timestr, "%s", localize(c, "(perm)").c_str()); |
| 696 | else |
| 697 | std::sprintf(timestr, "(%.48s)", seconds_to_timestr(entry->endtime - now)); |
| 698 | |
| 699 | if (!(ipstr = ipban_entry_to_str(entry))) |
| 700 | { |
| 701 | eventlog(eventlog_level_error, __FUNCTION__, "could not convert entry to string"); |
| 702 | continue; |
| 703 | } |
| 704 | std::sprintf(tstr, "%u: %s %s", counter, ipstr, timestr); |
| 705 | message_send_text(c, message_type_info, c, tstr); |
| 706 | xfree(ipstr); |
| 707 | } |
| 708 | |
| 709 | if (counter == 0) |
| 710 | message_send_text(c, message_type_info, c, localize(c, "none")); |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | |
| 715 | static int ipban_func_check(t_connection * c, char const * cp) |
| 716 | { |
| 717 | int res; |
| 718 | std::string msgtemp; |
| 719 | |
| 720 | res = ipbanlist_check(cp); |
| 721 | switch (res) |
| 722 | { |
| 723 | case 0: |
| 724 | message_send_text(c, message_type_info, c, localize(c, "IP not banned.")); |
| 725 | break; |
| 726 | case -1: |
| 727 | message_send_text(c, message_type_error, c, localize(c, "Error occured.")); |
| 728 | break; |
| 729 | default: |
| 730 | msgtemp = localize(c, "IP banned by rule #{}.", res); |
| 731 | message_send_text(c, message_type_info, c, msgtemp); |
| 732 | } |
| 733 | |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | |
| 738 | static int ipban_identical_entry(t_ipban_entry * e1, t_ipban_entry * e2) |
| 739 | { |
| 740 | if (e1->type != e2->type) |
| 741 | return 0; |
| 742 | |
| 743 | switch (e2->type) |
| 744 | { |
| 745 | case ipban_type_exact: |
| 746 | if (std::strcmp(e1->info1, e2->info1) == 0) |
| 747 | return 1; |
| 748 | break; |
| 749 | case ipban_type_range: |
| 750 | if (std::strcmp(e1->info1, e2->info1) == 0 && |
| 751 | std::strcmp(e1->info2, e2->info2) == 0) |
| 752 | return 1; |
| 753 | break; |
| 754 | case ipban_type_wildcard: |
| 755 | if (std::strcmp(e1->info1, e2->info1) == 0 && |
| 756 | std::strcmp(e1->info2, e2->info2) == 0 && |
| 757 | std::strcmp(e1->info3, e2->info3) == 0 && |
| 758 | std::strcmp(e1->info4, e2->info4) == 0) |
| 759 | return 1; |
| 760 | break; |
| 761 | case ipban_type_netmask: |
| 762 | if (std::strcmp(e1->info1, e2->info1) == 0 && |
| 763 | std::strcmp(e1->info2, e2->info2) == 0) |
| 764 | return 1; |
| 765 | break; |
| 766 | case ipban_type_prefix: |
| 767 | if (std::strcmp(e1->info1, e2->info1) == 0 && |
| 768 | std::strcmp(e1->info2, e2->info2) == 0) |
| 769 | return 1; |
| 770 | break; |
| 771 | default: /* unknown type */ |
| 772 | eventlog(eventlog_level_warn, __FUNCTION__, "found bad ban type {}", (int)e2->type); |
| 773 | } |
| 774 | |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | |
| 779 | static int ipban_unload_entry(t_ipban_entry * e) |
| 780 | { |
| 781 | switch (e->type) |
| 782 | { |
| 783 | case ipban_type_exact: |
| 784 | case ipban_type_wildcard: |
| 785 | if (e->info1) |
| 786 | xfree(e->info1); |
| 787 | break; |
| 788 | case ipban_type_range: |
| 789 | case ipban_type_netmask: |
| 790 | case ipban_type_prefix: |
| 791 | if (e->info1) |
| 792 | xfree(e->info1); |
| 793 | if (e->info2) |
| 794 | xfree(e->info2); |
| 795 | break; |
| 796 | default: /* unknown type */ |
| 797 | eventlog(eventlog_level_warn, __FUNCTION__, "found bad ban type {}", (int)e->type); |
| 798 | return -1; |
| 799 | } |
| 800 | xfree(e); |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | |
| 805 | static t_ipban_entry * ipban_str_to_ipban_entry(char const * ipstr) |
| 806 | { |
| 807 | char * matched; |
| 808 | char * whole; |
| 809 | char * cp; |
| 810 | t_ipban_entry * entry; |
| 811 | |
| 812 | entry = (t_ipban_entry*)xmalloc(sizeof(t_ipban_entry)); |
| 813 | if (!ipstr) |
| 814 | { |
| 815 | eventlog(eventlog_level_error, __FUNCTION__, "got NULL IP"); |
| 816 | xfree(entry); |
| 817 | return NULL; |
| 818 | } |
| 819 | if (ipstr[0] == '\0') |
| 820 | { |
| 821 | eventlog(eventlog_level_warn, __FUNCTION__, "got empty IP string"); |
| 822 | xfree(entry); |
| 823 | return NULL; |
| 824 | } |
| 825 | cp = xstrdup(ipstr); |
| 826 | |
| 827 | if (ipban_could_be_ip_str(cp) == 0) |
| 828 | { |
| 829 | eventlog(eventlog_level_debug, __FUNCTION__, "string: \"{}\" can not be valid IP", cp); |
| 830 | xfree(entry); |
| 831 | return NULL; |
| 832 | } |
| 833 | if ((matched = std::strchr(cp, '-'))) /* range */ |
| 834 | { |
| 835 | entry->type = ipban_type_range; |
| 836 | eventlog(eventlog_level_debug, __FUNCTION__, "entry: {} matched as ipban_type_range", cp); |
| 837 | matched[0] = '\0'; |
| 838 | entry->info1 = xstrdup(cp); /* start of range */ |
| 839 | entry->info2 = xstrdup(&matched[1]); /* end of range */ |
| 840 | entry->info3 = NULL; /* clear unused elements so debugging is nicer */ |
| 841 | entry->info4 = NULL; |
| 842 | } |
| 843 | else |
| 844 | if (std::strchr(cp, '*')) /* wildcard */ |
| 845 | { |
| 846 | entry->type = ipban_type_wildcard; |
| 847 | eventlog(eventlog_level_debug, __FUNCTION__, "entry: {} matched as ipban_type_wildcard", cp); |
| 848 | |
| 849 | /* only xfree() info1! */ |
| 850 | whole = xstrdup(cp); |
| 851 | entry->info1 = std::strtok(whole, "."); |
| 852 | entry->info2 = std::strtok(NULL, "."); |
| 853 | entry->info3 = std::strtok(NULL, "."); |
| 854 | entry->info4 = std::strtok(NULL, "."); |
| 855 | if (!entry->info4) /* not enough dots */ |
| 856 | { |
| 857 | eventlog(eventlog_level_error, __FUNCTION__, "wildcard entry \"{}\" does not contain all four octets", cp); |
| 858 | xfree(entry->info1); |
| 859 | xfree(entry); |
| 860 | xfree(cp); |
| 861 | return NULL; |
| 862 | } |
| 863 | } |
| 864 | else |
| 865 | if ((matched = std::strchr(cp, '/'))) /* netmask or prefix */ |
| 866 | { |
| 867 | if (std::strchr(&matched[1], '.')) |
| 868 | { |
| 869 | entry->type = ipban_type_netmask; |
| 870 | eventlog(eventlog_level_debug, __FUNCTION__, "entry: {} matched as ipban_type_netmask", cp); |
| 871 | } |
| 872 | else |
| 873 | { |
| 874 | entry->type = ipban_type_prefix; |
| 875 | eventlog(eventlog_level_debug, __FUNCTION__, "entry: {} matched as ipban_type_prefix", cp); |
| 876 | } |
| 877 | |
| 878 | matched[0] = '\0'; |
| 879 | entry->info1 = xstrdup(cp); |
| 880 | entry->info2 = xstrdup(&matched[1]); |
| 881 | entry->info3 = NULL; /* clear unused elements so debugging is nicer */ |
| 882 | entry->info4 = NULL; |
| 883 | } |
| 884 | else /* exact */ |
| 885 | { |
| 886 | entry->type = ipban_type_exact; |
| 887 | eventlog(eventlog_level_debug, __FUNCTION__, "entry: {} matched as ipban_type_exact", cp); |
| 888 | |
| 889 | entry->info1 = xstrdup(cp); |
| 890 | entry->info2 = NULL; /* clear unused elements so debugging is nicer */ |
| 891 | entry->info3 = NULL; |
| 892 | entry->info4 = NULL; |
| 893 | } |
| 894 | xfree(cp); |
| 895 | |
| 896 | return entry; |
| 897 | } |
| 898 | |
| 899 | |
| 900 | static char * ipban_entry_to_str(t_ipban_entry const * entry) |
| 901 | { |
| 902 | char tstr[MAX_MESSAGE_LEN]; |
| 903 | char * str; |
| 904 | |
| 905 | switch (entry->type) |
| 906 | { |
| 907 | case ipban_type_exact: |
| 908 | std::sprintf(tstr, "%s", entry->info1); |
| 909 | break; |
| 910 | case ipban_type_wildcard: |
| 911 | std::sprintf(tstr, "%s.%s.%s.%s", entry->info1, entry->info2, entry->info3, entry->info4); |
| 912 | break; |
| 913 | case ipban_type_range: |
| 914 | std::sprintf(tstr, "%s-%s", entry->info1, entry->info2); |
| 915 | break; |
| 916 | case ipban_type_netmask: |
| 917 | case ipban_type_prefix: |
| 918 | std::sprintf(tstr, "%s/%s", entry->info1, entry->info2); |
| 919 | break; |
| 920 | |
| 921 | default: /* unknown type */ |
| 922 | eventlog(eventlog_level_warn, __FUNCTION__, "found bad ban type {}", (int)entry->type); |
| 923 | return NULL; |
| 924 | } |
| 925 | str = xstrdup(tstr); |
| 926 | |
| 927 | return str; |
| 928 | } |
| 929 | |
| 930 | static unsigned long ipban_str_to_ulong(char const * ipaddr) |
| 931 | { |
| 932 | unsigned long lip; |
| 933 | char * ip1; |
| 934 | char * ip2; |
| 935 | char * ip3; |
| 936 | char * ip4; |
| 937 | char * tipaddr; |
| 938 | |
| 939 | tipaddr = xstrdup(ipaddr); |
| 940 | ip1 = std::strtok(tipaddr, "."); |
| 941 | ip2 = std::strtok(NULL, "."); |
| 942 | ip3 = std::strtok(NULL, "."); |
| 943 | ip4 = std::strtok(NULL, "."); |
| 944 | lip = (std::atoi(ip1) << 24) + (std::atoi(ip2) << 16) + (std::atoi(ip3) << 8) + (std::atoi(ip4)); |
| 945 | |
| 946 | xfree(tipaddr); |
| 947 | |
| 948 | return lip; |
| 949 | } |
| 950 | |
| 951 | |
| 952 | static int ipban_could_be_exact_ip_str(char const * str) |
| 953 | { |
| 954 | char * ipstr; |
| 955 | char * s; |
| 956 | char * ttok; |
| 957 | int i; |
| 958 | |
| 959 | ipstr = xstrdup(str); |
| 960 | |
| 961 | s = ipstr; |
| 962 | for (i = 0; i < 4; i++) |
| 963 | { |
| 964 | ttok = (char *)strsep(&ipstr, "."); |
| 965 | if (!ttok || std::strlen(ttok)<1 || std::strlen(ttok)>3) |
| 966 | { |
| 967 | xfree(s); |
| 968 | return 0; |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | xfree(s); |
| 973 | return 1; |
| 974 | } |
| 975 | |
| 976 | |
| 977 | static int ipban_could_be_ip_str(char const * str) |
| 978 | { |
| 979 | char * matched; |
| 980 | char * ipstr; |
| 981 | unsigned int i; |
| 982 | size_t strlen = std::strlen(str); |
| 983 | |
| 984 | if (strlen < 7) |
| 985 | { |
| 986 | eventlog(eventlog_level_error, __FUNCTION__, "string too short"); |
| 987 | return 0; |
| 988 | } |
| 989 | for (i = 0; i < strlen; i++) |
| 990 | if (!std::isdigit((int)str[i]) && str[i] != '.' && str[i] != '*' && str[i] != '/' && str[i] != '-') |
| 991 | { |
| 992 | eventlog(eventlog_level_debug, __FUNCTION__, "illegal character on position {}", i); |
| 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | ipstr = xstrdup(str); |
| 997 | if ((matched = std::strchr(ipstr, '-'))) |
| 998 | { |
| 999 | matched[0] = '\0'; |
| 1000 | if ((ipban_could_be_exact_ip_str(ipstr) == 0) || |
| 1001 | (ipban_could_be_exact_ip_str(&matched[1]) == 0)) |
| 1002 | { |
| 1003 | xfree(ipstr); |
| 1004 | return 0; |
| 1005 | } |
| 1006 | } |
| 1007 | else if ((matched = std::strchr(ipstr, '*'))) |
| 1008 | { |
| 1009 | if (ipban_could_be_exact_ip_str(ipstr) == 0) /* FIXME: 123.123.1*.123 allowed */ |
| 1010 | { |
| 1011 | xfree(ipstr); |
| 1012 | return 0; |
| 1013 | } |
| 1014 | } |
| 1015 | else if ((matched = std::strchr(ipstr, '/'))) |
| 1016 | { |
| 1017 | matched[0] = '\0'; |
| 1018 | if (std::strchr(&matched[1], '.')) |
| 1019 | { |
| 1020 | if ((ipban_could_be_exact_ip_str(ipstr) == 0) || |
| 1021 | (ipban_could_be_exact_ip_str(&matched[1]) == 0)) |
| 1022 | { |
| 1023 | xfree(ipstr); |
| 1024 | return 0; |
| 1025 | } |
| 1026 | } |
| 1027 | else |
| 1028 | { |
| 1029 | if (ipban_could_be_exact_ip_str(ipstr) == 0) |
| 1030 | { |
| 1031 | xfree(ipstr); |
| 1032 | return 0; |
| 1033 | } |
| 1034 | for (i = 1; i<std::strlen(&matched[1]); i++) |
| 1035 | if (!std::isdigit((int)matched[i])) |
| 1036 | { |
| 1037 | xfree(ipstr); |
| 1038 | return 0; |
| 1039 | } |
| 1040 | if (std::atoi(&matched[1])>32) /* can not be less than 0 because IP/-24 is matched as range */ |
| 1041 | { |
| 1042 | xfree(ipstr); |
| 1043 | return 0; |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | else |
| 1048 | { |
| 1049 | if (ipban_could_be_exact_ip_str(ipstr) == 0) |
| 1050 | { |
| 1051 | xfree(ipstr); |
| 1052 | return 0; |
| 1053 | } |
| 1054 | } |
| 1055 | xfree(ipstr); |
| 1056 | |
| 1057 | return 1; |
| 1058 | } |
| 1059 | |
| 1060 | |
| 1061 | } |
| 1062 | |
| 1063 | } |