xtables.c revision 7a236f4cc685a420c1a782a5db614a93baf37ccf
1/* 2 * (C) 2000-2006 by the netfilter coreteam <coreteam@netfilter.org>: 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 */ 18 19#include <errno.h> 20#include <fcntl.h> 21#include <netdb.h> 22#include <stdarg.h> 23#include <stdbool.h> 24#include <stdio.h> 25#include <stdlib.h> 26#include <string.h> 27#include <unistd.h> 28#include <sys/socket.h> 29#include <sys/stat.h> 30#include <sys/types.h> 31#include <sys/wait.h> 32#include <arpa/inet.h> 33 34#include <xtables.h> 35 36#ifndef NO_SHARED_LIBS 37#include <dlfcn.h> 38#endif 39 40#define NPROTO 255 41 42#ifndef PROC_SYS_MODPROBE 43#define PROC_SYS_MODPROBE "/proc/sys/kernel/modprobe" 44#endif 45 46char *lib_dir; 47 48/* the path to command to load kernel module */ 49const char *modprobe = NULL; 50 51/* Keeping track of external matches and targets: linked lists. */ 52struct xtables_match *xtables_matches; 53struct xtables_target *xtables_targets; 54 55void *fw_calloc(size_t count, size_t size) 56{ 57 void *p; 58 59 if ((p = calloc(count, size)) == NULL) { 60 perror("ip[6]tables: calloc failed"); 61 exit(1); 62 } 63 64 return p; 65} 66 67void *fw_malloc(size_t size) 68{ 69 void *p; 70 71 if ((p = malloc(size)) == NULL) { 72 perror("ip[6]tables: malloc failed"); 73 exit(1); 74 } 75 76 return p; 77} 78 79static char *get_modprobe(void) 80{ 81 int procfile; 82 char *ret; 83 84#define PROCFILE_BUFSIZ 1024 85 procfile = open(PROC_SYS_MODPROBE, O_RDONLY); 86 if (procfile < 0) 87 return NULL; 88 89 ret = (char *) malloc(PROCFILE_BUFSIZ); 90 if (ret) { 91 memset(ret, 0, PROCFILE_BUFSIZ); 92 switch (read(procfile, ret, PROCFILE_BUFSIZ)) { 93 case -1: goto fail; 94 case PROCFILE_BUFSIZ: goto fail; /* Partial read. Wierd */ 95 } 96 if (ret[strlen(ret)-1]=='\n') 97 ret[strlen(ret)-1]=0; 98 close(procfile); 99 return ret; 100 } 101 fail: 102 free(ret); 103 close(procfile); 104 return NULL; 105} 106 107int xtables_insmod(const char *modname, const char *modprobe, int quiet) 108{ 109 char *buf = NULL; 110 char *argv[4]; 111 int status; 112 113 /* If they don't explicitly set it, read out of kernel */ 114 if (!modprobe) { 115 buf = get_modprobe(); 116 if (!buf) 117 return -1; 118 modprobe = buf; 119 } 120 121 switch (fork()) { 122 case 0: 123 argv[0] = (char *)modprobe; 124 argv[1] = (char *)modname; 125 if (quiet) { 126 argv[2] = "-q"; 127 argv[3] = NULL; 128 } else { 129 argv[2] = NULL; 130 argv[3] = NULL; 131 } 132 execv(argv[0], argv); 133 134 /* not usually reached */ 135 exit(1); 136 case -1: 137 return -1; 138 139 default: /* parent */ 140 wait(&status); 141 } 142 143 free(buf); 144 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) 145 return 0; 146 return -1; 147} 148 149int load_xtables_ko(const char *modprobe, int quiet) 150{ 151 static int loaded = 0; 152 static int ret = -1; 153 154 if (!loaded) { 155 ret = xtables_insmod(afinfo.kmod, modprobe, quiet); 156 loaded = (ret == 0); 157 } 158 159 return ret; 160} 161 162int string_to_number_ll(const char *s, unsigned long long min, 163 unsigned long long max, unsigned long long *ret) 164{ 165 unsigned long long number; 166 char *end; 167 168 /* Handle hex, octal, etc. */ 169 errno = 0; 170 number = strtoull(s, &end, 0); 171 if (*end == '\0' && end != s) { 172 /* we parsed a number, let's see if we want this */ 173 if (errno != ERANGE && min <= number && (!max || number <= max)) { 174 *ret = number; 175 return 0; 176 } 177 } 178 return -1; 179} 180 181int string_to_number_l(const char *s, unsigned long min, unsigned long max, 182 unsigned long *ret) 183{ 184 int result; 185 unsigned long long number; 186 187 result = string_to_number_ll(s, min, max, &number); 188 *ret = (unsigned long)number; 189 190 return result; 191} 192 193int string_to_number(const char *s, unsigned int min, unsigned int max, 194 unsigned int *ret) 195{ 196 int result; 197 unsigned long number; 198 199 result = string_to_number_l(s, min, max, &number); 200 *ret = (unsigned int)number; 201 202 return result; 203} 204 205/* 206 * strtonum{,l} - string to number conversion 207 * 208 * If @end is NULL, we assume the caller does not want 209 * a case like "15a", so reject it. 210 */ 211bool strtonuml(const char *s, char **end, unsigned long *value, 212 unsigned long min, unsigned long max) 213{ 214 unsigned long v; 215 char *my_end; 216 217 errno = 0; 218 v = strtoul(s, &my_end, 0); 219 220 if (my_end == s) 221 return false; 222 if (end != NULL) 223 *end = my_end; 224 225 if (errno != ERANGE && min <= v && (max == 0 || v <= max)) { 226 if (value != NULL) 227 *value = v; 228 if (end == NULL) 229 return *my_end == '\0'; 230 return true; 231 } 232 233 return false; 234} 235 236bool strtonum(const char *s, char **end, unsigned int *value, 237 unsigned int min, unsigned int max) 238{ 239 unsigned long v; 240 bool ret; 241 242 ret = strtonuml(s, end, &v, min, max); 243 if (value != NULL) 244 *value = v; 245 return ret; 246} 247 248int service_to_port(const char *name, const char *proto) 249{ 250 struct servent *service; 251 252 if ((service = getservbyname(name, proto)) != NULL) 253 return ntohs((unsigned short) service->s_port); 254 255 return -1; 256} 257 258u_int16_t parse_port(const char *port, const char *proto) 259{ 260 unsigned int portnum; 261 262 if ((string_to_number(port, 0, 65535, &portnum)) != -1 || 263 (portnum = service_to_port(port, proto)) != (unsigned)-1) 264 return (u_int16_t)portnum; 265 266 exit_error(PARAMETER_PROBLEM, 267 "invalid port/service `%s' specified", port); 268} 269 270void parse_interface(const char *arg, char *vianame, unsigned char *mask) 271{ 272 int vialen = strlen(arg); 273 unsigned int i; 274 275 memset(mask, 0, IFNAMSIZ); 276 memset(vianame, 0, IFNAMSIZ); 277 278 if (vialen + 1 > IFNAMSIZ) 279 exit_error(PARAMETER_PROBLEM, 280 "interface name `%s' must be shorter than IFNAMSIZ" 281 " (%i)", arg, IFNAMSIZ-1); 282 283 strcpy(vianame, arg); 284 if ((vialen == 0) || (vialen == 1 && vianame[0] == '+')) 285 memset(mask, 0, IFNAMSIZ); 286 else if (vianame[vialen - 1] == '+') { 287 memset(mask, 0xFF, vialen - 1); 288 memset(mask + vialen - 1, 0, IFNAMSIZ - vialen + 1); 289 /* Don't remove `+' here! -HW */ 290 } else { 291 /* Include nul-terminator in match */ 292 memset(mask, 0xFF, vialen + 1); 293 memset(mask + vialen + 1, 0, IFNAMSIZ - vialen - 1); 294 for (i = 0; vianame[i]; i++) { 295 if (vianame[i] == ':' || 296 vianame[i] == '!' || 297 vianame[i] == '*') { 298 fprintf(stderr, 299 "Warning: weird character in interface" 300 " `%s' (No aliases, :, ! or *).\n", 301 vianame); 302 break; 303 } 304 } 305 } 306} 307 308struct xtables_match *find_match(const char *name, enum xt_tryload tryload, 309 struct xtables_rule_match **matches) 310{ 311#ifndef NO_SHARED_LIBS 312 struct stat sb; 313#endif 314 struct xtables_match *ptr; 315 const char *icmp6 = "icmp6"; 316 317 /* This is ugly as hell. Nonetheless, there is no way of changing 318 * this without hurting backwards compatibility */ 319 if ( (strcmp(name,"icmpv6") == 0) || 320 (strcmp(name,"ipv6-icmp") == 0) || 321 (strcmp(name,"icmp6") == 0) ) 322 name = icmp6; 323 324 for (ptr = xtables_matches; ptr; ptr = ptr->next) { 325 if (strcmp(name, ptr->name) == 0) { 326 struct xtables_match *clone; 327 328 /* First match of this type: */ 329 if (ptr->m == NULL) 330 break; 331 332 /* Second and subsequent clones */ 333 clone = fw_malloc(sizeof(struct xtables_match)); 334 memcpy(clone, ptr, sizeof(struct xtables_match)); 335 clone->mflags = 0; 336 /* This is a clone: */ 337 clone->next = clone; 338 339 ptr = clone; 340 break; 341 } 342 } 343 344#ifndef NO_SHARED_LIBS 345 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) { 346 char path[strlen(lib_dir) + sizeof("/.so") 347 + strlen(afinfo.libprefix) + strlen(name)]; 348 349 sprintf(path, "%s/libxt_%s.so", lib_dir, name); 350 if (dlopen(path, RTLD_NOW) != NULL) 351 /* Found library. If it didn't register itself, 352 maybe they specified target as match. */ 353 ptr = find_match(name, DONT_LOAD, NULL); 354 else if (stat(path, &sb) == 0) 355 fprintf(stderr, "%s: %s\n", path, dlerror()); 356 357 if (ptr == NULL) { 358 sprintf(path, "%s/%s%s.so", lib_dir, afinfo.libprefix, 359 name); 360 if (dlopen(path, RTLD_NOW) != NULL) 361 ptr = find_match(name, DONT_LOAD, NULL); 362 else if (stat(path, &sb) == 0) 363 fprintf(stderr, "%s: %s\n", path, dlerror()); 364 } 365 366 if (ptr == NULL && tryload == LOAD_MUST_SUCCEED) 367 exit_error(PARAMETER_PROBLEM, 368 "Couldn't load match `%s':%s\n", 369 name, dlerror()); 370 } 371#else 372 if (ptr && !ptr->loaded) { 373 if (tryload != DONT_LOAD) 374 ptr->loaded = 1; 375 else 376 ptr = NULL; 377 } 378 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) { 379 exit_error(PARAMETER_PROBLEM, 380 "Couldn't find match `%s'\n", name); 381 } 382#endif 383 384 if (ptr && matches) { 385 struct xtables_rule_match **i; 386 struct xtables_rule_match *newentry; 387 388 newentry = fw_malloc(sizeof(struct xtables_rule_match)); 389 390 for (i = matches; *i; i = &(*i)->next) { 391 if (strcmp(name, (*i)->match->name) == 0) 392 (*i)->completed = 1; 393 } 394 newentry->match = ptr; 395 newentry->completed = 0; 396 newentry->next = NULL; 397 *i = newentry; 398 } 399 400 return ptr; 401} 402 403 404struct xtables_target *find_target(const char *name, enum xt_tryload tryload) 405{ 406#ifndef NO_SHARED_LIBS 407 struct stat sb; 408#endif 409 struct xtables_target *ptr; 410 411 /* Standard target? */ 412 if (strcmp(name, "") == 0 413 || strcmp(name, XTC_LABEL_ACCEPT) == 0 414 || strcmp(name, XTC_LABEL_DROP) == 0 415 || strcmp(name, XTC_LABEL_QUEUE) == 0 416 || strcmp(name, XTC_LABEL_RETURN) == 0) 417 name = "standard"; 418 419 for (ptr = xtables_targets; ptr; ptr = ptr->next) { 420 if (strcmp(name, ptr->name) == 0) 421 break; 422 } 423 424#ifndef NO_SHARED_LIBS 425 if (!ptr && tryload != DONT_LOAD && tryload != DURING_LOAD) { 426 char path[strlen(lib_dir) + sizeof("/.so") 427 + strlen(afinfo.libprefix) + strlen(name)]; 428 429 sprintf(path, "%s/libxt_%s.so", lib_dir, name); 430 if (dlopen(path, RTLD_NOW) != NULL) 431 /* Found library. If it didn't register itself, 432 maybe they specified match as a target. */ 433 ptr = find_target(name, DONT_LOAD); 434 else if (stat(path, &sb) == 0) 435 fprintf(stderr, "%s: %s\n", path, dlerror()); 436 437 if (ptr == NULL) { 438 sprintf(path, "%s/%s%s.so", lib_dir, afinfo.libprefix, 439 name); 440 if (dlopen(path, RTLD_NOW) != NULL) 441 ptr = find_target(name, DONT_LOAD); 442 else if (stat(path, &sb) == 0) 443 fprintf(stderr, "%s: %s\n", path, dlerror()); 444 } 445 if (ptr == NULL && tryload == LOAD_MUST_SUCCEED) 446 exit_error(PARAMETER_PROBLEM, 447 "Couldn't load target `%s':%s\n", 448 name, dlerror()); 449 } 450#else 451 if (ptr && !ptr->loaded) { 452 if (tryload != DONT_LOAD) 453 ptr->loaded = 1; 454 else 455 ptr = NULL; 456 } 457 if(!ptr && (tryload == LOAD_MUST_SUCCEED)) { 458 exit_error(PARAMETER_PROBLEM, 459 "Couldn't find target `%s'\n", name); 460 } 461#endif 462 463 if (ptr) 464 ptr->used = 1; 465 466 return ptr; 467} 468 469static int compatible_revision(const char *name, u_int8_t revision, int opt) 470{ 471 struct xt_get_revision rev; 472 socklen_t s = sizeof(rev); 473 int max_rev, sockfd; 474 475 sockfd = socket(afinfo.family, SOCK_RAW, IPPROTO_RAW); 476 if (sockfd < 0) { 477 if (errno == EPERM) { 478 /* revision 0 is always supported. */ 479 if (revision != 0) 480 fprintf(stderr, "Could not determine whether " 481 "revision %u is supported, " 482 "assuming it is.\n", 483 revision); 484 return 1; 485 } 486 fprintf(stderr, "Could not open socket to kernel: %s\n", 487 strerror(errno)); 488 exit(1); 489 } 490 491 load_xtables_ko(modprobe, 1); 492 493 strcpy(rev.name, name); 494 rev.revision = revision; 495 496 max_rev = getsockopt(sockfd, afinfo.ipproto, opt, &rev, &s); 497 if (max_rev < 0) { 498 /* Definitely don't support this? */ 499 if (errno == ENOENT || errno == EPROTONOSUPPORT) { 500 close(sockfd); 501 return 0; 502 } else if (errno == ENOPROTOOPT) { 503 close(sockfd); 504 /* Assume only revision 0 support (old kernel) */ 505 return (revision == 0); 506 } else { 507 fprintf(stderr, "getsockopt failed strangely: %s\n", 508 strerror(errno)); 509 exit(1); 510 } 511 } 512 close(sockfd); 513 return 1; 514} 515 516 517static int compatible_match_revision(const char *name, u_int8_t revision) 518{ 519 return compatible_revision(name, revision, afinfo.so_rev_match); 520} 521 522static int compatible_target_revision(const char *name, u_int8_t revision) 523{ 524 return compatible_revision(name, revision, afinfo.so_rev_target); 525} 526 527void xtables_register_match(struct xtables_match *me) 528{ 529 struct xtables_match **i, *old; 530 531 if (strcmp(me->version, program_version) != 0) { 532 fprintf(stderr, "%s: match `%s' v%s (I'm v%s).\n", 533 program_name, me->name, me->version, program_version); 534 exit(1); 535 } 536 537 /* Revision field stole a char from name. */ 538 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) { 539 fprintf(stderr, "%s: target `%s' has invalid name\n", 540 program_name, me->name); 541 exit(1); 542 } 543 544 if (me->family >= NPROTO) { 545 fprintf(stderr, 546 "%s: BUG: match %s has invalid protocol family\n", 547 program_name, me->name); 548 exit(1); 549 } 550 551 /* ignore not interested match */ 552 if (me->family != afinfo.family) 553 return; 554 555 old = find_match(me->name, DURING_LOAD, NULL); 556 if (old) { 557 if (old->revision == me->revision) { 558 fprintf(stderr, 559 "%s: match `%s' already registered.\n", 560 program_name, me->name); 561 exit(1); 562 } 563 564 /* Now we have two (or more) options, check compatibility. */ 565 if (compatible_match_revision(old->name, old->revision) 566 && old->revision > me->revision) 567 return; 568 569 /* Replace if compatible. */ 570 if (!compatible_match_revision(me->name, me->revision)) 571 return; 572 573 /* Delete old one. */ 574 for (i = &xtables_matches; *i!=old; i = &(*i)->next); 575 *i = old->next; 576 } 577 578 if (me->size != XT_ALIGN(me->size)) { 579 fprintf(stderr, "%s: match `%s' has invalid size %u.\n", 580 program_name, me->name, (unsigned int)me->size); 581 exit(1); 582 } 583 584 /* Append to list. */ 585 for (i = &xtables_matches; *i; i = &(*i)->next); 586 me->next = NULL; 587 *i = me; 588 589 me->m = NULL; 590 me->mflags = 0; 591} 592 593void xtables_register_target(struct xtables_target *me) 594{ 595 struct xtables_target *old; 596 597 if (strcmp(me->version, program_version) != 0) { 598 fprintf(stderr, "%s: target `%s' v%s (I'm v%s).\n", 599 program_name, me->name, me->version, program_version); 600 exit(1); 601 } 602 603 /* Revision field stole a char from name. */ 604 if (strlen(me->name) >= XT_FUNCTION_MAXNAMELEN-1) { 605 fprintf(stderr, "%s: target `%s' has invalid name\n", 606 program_name, me->name); 607 exit(1); 608 } 609 610 if (me->family >= NPROTO) { 611 fprintf(stderr, 612 "%s: BUG: target %s has invalid protocol family\n", 613 program_name, me->name); 614 exit(1); 615 } 616 617 /* ignore not interested target */ 618 if (me->family != afinfo.family) 619 return; 620 621 old = find_target(me->name, DURING_LOAD); 622 if (old) { 623 struct xtables_target **i; 624 625 if (old->revision == me->revision) { 626 fprintf(stderr, 627 "%s: target `%s' already registered.\n", 628 program_name, me->name); 629 exit(1); 630 } 631 632 /* Now we have two (or more) options, check compatibility. */ 633 if (compatible_target_revision(old->name, old->revision) 634 && old->revision > me->revision) 635 return; 636 637 /* Replace if compatible. */ 638 if (!compatible_target_revision(me->name, me->revision)) 639 return; 640 641 /* Delete old one. */ 642 for (i = &xtables_targets; *i!=old; i = &(*i)->next); 643 *i = old->next; 644 } 645 646 if (me->size != XT_ALIGN(me->size)) { 647 fprintf(stderr, "%s: target `%s' has invalid size %u.\n", 648 program_name, me->name, (unsigned int)me->size); 649 exit(1); 650 } 651 652 /* Prepend to list. */ 653 me->next = xtables_targets; 654 xtables_targets = me; 655 me->t = NULL; 656 me->tflags = 0; 657} 658 659void param_act(unsigned int status, const char *p1, ...) 660{ 661 const char *p2, *p3; 662 va_list args; 663 bool b; 664 665 va_start(args, p1); 666 667 switch (status) { 668 case P_ONLY_ONCE: 669 p2 = va_arg(args, const char *); 670 b = va_arg(args, unsigned int); 671 if (!b) 672 return; 673 exit_error(PARAMETER_PROBLEM, 674 "%s: \"%s\" option may only be specified once", 675 p1, p2); 676 break; 677 case P_NO_INVERT: 678 p2 = va_arg(args, const char *); 679 b = va_arg(args, unsigned int); 680 if (!b) 681 return; 682 exit_error(PARAMETER_PROBLEM, 683 "%s: \"%s\" option cannot be inverted", p1, p2); 684 break; 685 case P_BAD_VALUE: 686 p2 = va_arg(args, const char *); 687 p3 = va_arg(args, const char *); 688 exit_error(PARAMETER_PROBLEM, 689 "%s: Bad value for \"%s\" option: \"%s\"", 690 p1, p2, p3); 691 break; 692 case P_ONE_ACTION: 693 b = va_arg(args, unsigned int); 694 if (!b) 695 return; 696 exit_error(PARAMETER_PROBLEM, 697 "%s: At most one action is possible", p1); 698 break; 699 default: 700 exit_error(status, p1, args); 701 break; 702 } 703 704 va_end(args); 705} 706 707const char *ipaddr_to_numeric(const struct in_addr *addrp) 708{ 709 static char buf[20]; 710 const unsigned char *bytep = (const void *)&addrp->s_addr; 711 712 sprintf(buf, "%u.%u.%u.%u", bytep[0], bytep[1], bytep[2], bytep[3]); 713 return buf; 714} 715 716static const char *ipaddr_to_host(const struct in_addr *addr) 717{ 718 struct hostent *host; 719 720 host = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET); 721 if (host == NULL) 722 return NULL; 723 724 return host->h_name; 725} 726 727static const char *ipaddr_to_network(const struct in_addr *addr) 728{ 729 struct netent *net; 730 731 if ((net = getnetbyaddr(ntohl(addr->s_addr), AF_INET)) != NULL) 732 return net->n_name; 733 734 return NULL; 735} 736 737const char *ipaddr_to_anyname(const struct in_addr *addr) 738{ 739 const char *name; 740 741 if ((name = ipaddr_to_host(addr)) != NULL || 742 (name = ipaddr_to_network(addr)) != NULL) 743 return name; 744 745 return ipaddr_to_numeric(addr); 746} 747 748const char *ipmask_to_numeric(const struct in_addr *mask) 749{ 750 static char buf[20]; 751 uint32_t maskaddr, bits; 752 int i; 753 754 maskaddr = ntohl(mask->s_addr); 755 756 if (maskaddr == 0xFFFFFFFFL) 757 /* we don't want to see "/32" */ 758 return ""; 759 760 i = 32; 761 bits = 0xFFFFFFFEL; 762 while (--i >= 0 && maskaddr != bits) 763 bits <<= 1; 764 if (i >= 0) 765 sprintf(buf, "/%d", i); 766 else 767 /* mask was not a decent combination of 1's and 0's */ 768 sprintf(buf, "/%s", ipaddr_to_numeric(mask)); 769 770 return buf; 771} 772 773static struct in_addr *__numeric_to_ipaddr(const char *dotted, bool is_mask) 774{ 775 static struct in_addr addr; 776 unsigned char *addrp; 777 unsigned int onebyte; 778 char buf[20], *p, *q; 779 int i; 780 781 /* copy dotted string, because we need to modify it */ 782 strncpy(buf, dotted, sizeof(buf) - 1); 783 buf[sizeof(buf) - 1] = '\0'; 784 addrp = (void *)&addr.s_addr; 785 786 p = buf; 787 for (i = 0; i < 3; ++i) { 788 if ((q = strchr(p, '.')) == NULL) { 789 if (is_mask) 790 return NULL; 791 792 /* autocomplete, this is a network address */ 793 if (!strtonum(p, NULL, &onebyte, 0, 255)) 794 return NULL; 795 796 addrp[i] = onebyte; 797 while (i < 3) 798 addrp[++i] = 0; 799 800 return &addr; 801 } 802 803 *q = '\0'; 804 if (!strtonum(p, NULL, &onebyte, 0, 255)) 805 return NULL; 806 807 addrp[i] = onebyte; 808 p = q + 1; 809 } 810 811 /* we have checked 3 bytes, now we check the last one */ 812 if (!strtonum(p, NULL, &onebyte, 0, 255)) 813 return NULL; 814 815 addrp[3] = onebyte; 816 return &addr; 817} 818 819struct in_addr *numeric_to_ipaddr(const char *dotted) 820{ 821 return __numeric_to_ipaddr(dotted, false); 822} 823 824struct in_addr *numeric_to_ipmask(const char *dotted) 825{ 826 return __numeric_to_ipaddr(dotted, true); 827} 828 829static struct in_addr *network_to_ipaddr(const char *name) 830{ 831 static struct in_addr addr; 832 struct netent *net; 833 834 if ((net = getnetbyname(name)) != NULL) { 835 if (net->n_addrtype != AF_INET) 836 return NULL; 837 addr.s_addr = htonl(net->n_net); 838 return &addr; 839 } 840 841 return NULL; 842} 843 844static struct in_addr *host_to_ipaddr(const char *name, unsigned int *naddr) 845{ 846 struct hostent *host; 847 struct in_addr *addr; 848 unsigned int i; 849 850 *naddr = 0; 851 if ((host = gethostbyname(name)) != NULL) { 852 if (host->h_addrtype != AF_INET || 853 host->h_length != sizeof(struct in_addr)) 854 return NULL; 855 856 while (host->h_addr_list[*naddr] != NULL) 857 ++*naddr; 858 addr = fw_calloc(*naddr, sizeof(struct in_addr) * *naddr); 859 for (i = 0; i < *naddr; i++) 860 memcpy(&addr[i], host->h_addr_list[i], 861 sizeof(struct in_addr)); 862 return addr; 863 } 864 865 return NULL; 866} 867 868static struct in_addr * 869ipparse_hostnetwork(const char *name, unsigned int *naddrs) 870{ 871 struct in_addr *addrptmp, *addrp; 872 873 if ((addrptmp = numeric_to_ipaddr(name)) != NULL || 874 (addrptmp = network_to_ipaddr(name)) != NULL) { 875 addrp = fw_malloc(sizeof(struct in_addr)); 876 memcpy(addrp, addrptmp, sizeof(*addrp)); 877 *naddrs = 1; 878 return addrp; 879 } 880 if ((addrptmp = host_to_ipaddr(name, naddrs)) != NULL) 881 return addrptmp; 882 883 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name); 884} 885 886static struct in_addr *parse_ipmask(const char *mask) 887{ 888 static struct in_addr maskaddr; 889 struct in_addr *addrp; 890 unsigned int bits; 891 892 if (mask == NULL) { 893 /* no mask at all defaults to 32 bits */ 894 maskaddr.s_addr = 0xFFFFFFFF; 895 return &maskaddr; 896 } 897 if ((addrp = numeric_to_ipmask(mask)) != NULL) 898 /* dotted_to_addr already returns a network byte order addr */ 899 return addrp; 900 if (string_to_number(mask, 0, 32, &bits) == -1) 901 exit_error(PARAMETER_PROBLEM, 902 "invalid mask `%s' specified", mask); 903 if (bits != 0) { 904 maskaddr.s_addr = htonl(0xFFFFFFFF << (32 - bits)); 905 return &maskaddr; 906 } 907 908 maskaddr.s_addr = 0U; 909 return &maskaddr; 910} 911 912void ipparse_hostnetworkmask(const char *name, struct in_addr **addrpp, 913 struct in_addr *maskp, unsigned int *naddrs) 914{ 915 unsigned int i, j, k, n; 916 struct in_addr *addrp; 917 char buf[256], *p; 918 919 strncpy(buf, name, sizeof(buf) - 1); 920 buf[sizeof(buf) - 1] = '\0'; 921 if ((p = strrchr(buf, '/')) != NULL) { 922 *p = '\0'; 923 addrp = parse_ipmask(p + 1); 924 } else { 925 addrp = parse_ipmask(NULL); 926 } 927 memcpy(maskp, addrp, sizeof(*maskp)); 928 929 /* if a null mask is given, the name is ignored, like in "any/0" */ 930 if (maskp->s_addr == 0U) 931 strcpy(buf, "0.0.0.0"); 932 933 addrp = *addrpp = ipparse_hostnetwork(buf, naddrs); 934 n = *naddrs; 935 for (i = 0, j = 0; i < n; ++i) { 936 addrp[j++].s_addr &= maskp->s_addr; 937 for (k = 0; k < j - 1; ++k) 938 if (addrp[k].s_addr == addrp[j-1].s_addr) { 939 --*naddrs; 940 --j; 941 break; 942 } 943 } 944} 945 946const char *ip6addr_to_numeric(const struct in6_addr *addrp) 947{ 948 /* 0000:0000:0000:0000:0000:000.000.000.000 949 * 0000:0000:0000:0000:0000:0000:0000:0000 */ 950 static char buf[50+1]; 951 return inet_ntop(AF_INET6, addrp, buf, sizeof(buf)); 952} 953 954static const char *ip6addr_to_host(const struct in6_addr *addr) 955{ 956 static char hostname[NI_MAXHOST]; 957 struct sockaddr_in6 saddr; 958 int err; 959 960 memset(&saddr, 0, sizeof(struct sockaddr_in6)); 961 memcpy(&saddr.sin6_addr, addr, sizeof(*addr)); 962 saddr.sin6_family = AF_INET6; 963 964 err = getnameinfo((const void *)&saddr, sizeof(struct sockaddr_in6), 965 hostname, sizeof(hostname) - 1, NULL, 0, 0); 966 if (err != 0) { 967#ifdef DEBUG 968 fprintf(stderr,"IP2Name: %s\n",gai_strerror(err)); 969#endif 970 return NULL; 971 } 972 973#ifdef DEBUG 974 fprintf (stderr, "\naddr2host: %s\n", hostname); 975#endif 976 return hostname; 977} 978 979const char *ip6addr_to_anyname(const struct in6_addr *addr) 980{ 981 const char *name; 982 983 if ((name = ip6addr_to_host(addr)) != NULL) 984 return name; 985 986 return ip6addr_to_numeric(addr); 987} 988 989static int ip6addr_prefix_length(const struct in6_addr *k) 990{ 991 unsigned int bits = 0; 992 uint32_t a, b, c, d; 993 994 a = k->s6_addr32[0]; 995 b = k->s6_addr32[1]; 996 c = k->s6_addr32[2]; 997 d = k->s6_addr32[3]; 998 while (a & 0x80000000U) { 999 ++bits; 1000 a <<= 1; 1001 a |= (b >> 31) & 1; 1002 b <<= 1; 1003 b |= (c >> 31) & 1; 1004 c <<= 1; 1005 c |= (d >> 31) & 1; 1006 d <<= 1; 1007 } 1008 if (a != 0 || b != 0 || c != 0 || d != 0) 1009 return -1; 1010 return bits; 1011} 1012 1013const char *ip6mask_to_numeric(const struct in6_addr *addrp) 1014{ 1015 static char buf[50+2]; 1016 int l = ip6addr_prefix_length(addrp); 1017 1018 if (l == -1) { 1019 strcpy(buf, "/"); 1020 strcat(buf, ip6addr_to_numeric(addrp)); 1021 return buf; 1022 } 1023 sprintf(buf, "/%d", l); 1024 return buf; 1025} 1026 1027struct in6_addr *numeric_to_ip6addr(const char *num) 1028{ 1029 static struct in6_addr ap; 1030 int err; 1031 1032 if ((err = inet_pton(AF_INET6, num, &ap)) == 1) 1033 return ≈ 1034#ifdef DEBUG 1035 fprintf(stderr, "\nnumeric2addr: %d\n", err); 1036#endif 1037 return NULL; 1038} 1039 1040static struct in6_addr * 1041host_to_ip6addr(const char *name, unsigned int *naddr) 1042{ 1043 static struct in6_addr *addr; 1044 struct addrinfo hints; 1045 struct addrinfo *res; 1046 int err; 1047 1048 memset(&hints, 0, sizeof(hints)); 1049 hints.ai_flags = AI_CANONNAME; 1050 hints.ai_family = AF_INET6; 1051 hints.ai_socktype = SOCK_RAW; 1052 hints.ai_protocol = IPPROTO_IPV6; 1053 hints.ai_next = NULL; 1054 1055 *naddr = 0; 1056 if ((err = getaddrinfo(name, NULL, &hints, &res)) != 0) { 1057#ifdef DEBUG 1058 fprintf(stderr,"Name2IP: %s\n",gai_strerror(err)); 1059#endif 1060 return NULL; 1061 } else { 1062 if (res->ai_family != AF_INET6 || 1063 res->ai_addrlen != sizeof(struct sockaddr_in6)) 1064 return NULL; 1065 1066#ifdef DEBUG 1067 fprintf(stderr, "resolved: len=%d %s ", res->ai_addrlen, 1068 ip6addr_to_numeric(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)); 1069#endif 1070 /* Get the first element of the address-chain */ 1071 addr = fw_malloc(sizeof(struct in6_addr)); 1072 memcpy(addr, &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr, 1073 sizeof(struct in6_addr)); 1074 freeaddrinfo(res); 1075 *naddr = 1; 1076 return addr; 1077 } 1078 1079 return NULL; 1080} 1081 1082static struct in6_addr *network_to_ip6addr(const char *name) 1083{ 1084 /* abort();*/ 1085 /* TODO: not implemented yet, but the exception breaks the 1086 * name resolvation */ 1087 return NULL; 1088} 1089 1090static struct in6_addr * 1091ip6parse_hostnetwork(const char *name, unsigned int *naddrs) 1092{ 1093 struct in6_addr *addrp, *addrptmp; 1094 1095 if ((addrptmp = numeric_to_ip6addr(name)) != NULL || 1096 (addrptmp = network_to_ip6addr(name)) != NULL) { 1097 addrp = fw_malloc(sizeof(struct in6_addr)); 1098 memcpy(addrp, addrptmp, sizeof(*addrp)); 1099 *naddrs = 1; 1100 return addrp; 1101 } 1102 if ((addrp = host_to_ip6addr(name, naddrs)) != NULL) 1103 return addrp; 1104 1105 exit_error(PARAMETER_PROBLEM, "host/network `%s' not found", name); 1106} 1107 1108static struct in6_addr *parse_ip6mask(char *mask) 1109{ 1110 static struct in6_addr maskaddr; 1111 struct in6_addr *addrp; 1112 unsigned int bits; 1113 1114 if (mask == NULL) { 1115 /* no mask at all defaults to 128 bits */ 1116 memset(&maskaddr, 0xff, sizeof maskaddr); 1117 return &maskaddr; 1118 } 1119 if ((addrp = numeric_to_ip6addr(mask)) != NULL) 1120 return addrp; 1121 if (string_to_number(mask, 0, 128, &bits) == -1) 1122 exit_error(PARAMETER_PROBLEM, 1123 "invalid mask `%s' specified", mask); 1124 if (bits != 0) { 1125 char *p = (void *)&maskaddr; 1126 memset(p, 0xff, bits / 8); 1127 memset(p + (bits / 8) + 1, 0, (128 - bits) / 8); 1128 p[bits/8] = 0xff << (8 - (bits & 7)); 1129 return &maskaddr; 1130 } 1131 1132 memset(&maskaddr, 0, sizeof(maskaddr)); 1133 return &maskaddr; 1134} 1135 1136void ip6parse_hostnetworkmask(const char *name, struct in6_addr **addrpp, 1137 struct in6_addr *maskp, unsigned int *naddrs) 1138{ 1139 struct in6_addr *addrp; 1140 unsigned int i, j, k, n; 1141 char buf[256], *p; 1142 1143 strncpy(buf, name, sizeof(buf) - 1); 1144 buf[sizeof(buf)-1] = '\0'; 1145 if ((p = strrchr(buf, '/')) != NULL) { 1146 *p = '\0'; 1147 addrp = parse_ip6mask(p + 1); 1148 } else { 1149 addrp = parse_ip6mask(NULL); 1150 } 1151 memcpy(maskp, addrp, sizeof(*maskp)); 1152 1153 /* if a null mask is given, the name is ignored, like in "any/0" */ 1154 if (memcmp(maskp, &in6addr_any, sizeof(in6addr_any)) == 0) 1155 strcpy(buf, "::"); 1156 1157 addrp = *addrpp = ip6parse_hostnetwork(buf, naddrs); 1158 n = *naddrs; 1159 for (i = 0, j = 0; i < n; ++i) { 1160 for (k = 0; k < 4; ++k) 1161 addrp[j].in6_u.u6_addr32[k] &= maskp->in6_u.u6_addr32[k]; 1162 ++j; 1163 for (k = 0; k < j - 1; ++k) 1164 if (IN6_ARE_ADDR_EQUAL(&addrp[k], &addrp[j - 1])) { 1165 --*naddrs; 1166 --j; 1167 break; 1168 } 1169 } 1170} 1171 1172void save_string(const char *value) 1173{ 1174 static const char no_quote_chars[] = "_-0123456789" 1175 "abcdefghijklmnopqrstuvwxyz" 1176 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 1177 static const char escape_chars[] = "\"\\'"; 1178 size_t length; 1179 const char *p; 1180 1181 length = strcspn(value, no_quote_chars); 1182 if (length > 0 && value[length] == 0) { 1183 /* no quoting required */ 1184 fputs(value, stdout); 1185 putchar(' '); 1186 } else { 1187 /* there is at least one dangerous character in the 1188 value, which we have to quote. Write double quotes 1189 around the value and escape special characters with 1190 a backslash */ 1191 putchar('"'); 1192 1193 for (p = strpbrk(value, escape_chars); p != NULL; 1194 p = strpbrk(value, escape_chars)) { 1195 if (p > value) 1196 fwrite(value, 1, p - value, stdout); 1197 putchar('\\'); 1198 putchar(*p); 1199 value = p + 1; 1200 } 1201 1202 /* print the rest and finish the double quoted 1203 string */ 1204 fputs(value, stdout); 1205 printf("\" "); 1206 } 1207} 1208