1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
|
/*
* Portions of this file are copyrighted by:
* Copyright (c) 2016 VMware, Inc. All rights reserved.
* Use is subject to license terms specified in the COPYING file
* distributed with the Net-SNMP package.
*/
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-features.h>
#include <net-snmp/types.h>
#include <net-snmp/library/snmp_transport.h>
#include <stdio.h>
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#include <sys/types.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <ctype.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <net-snmp/output_api.h>
#include <net-snmp/utilities.h>
#include <net-snmp/library/default_store.h>
#include <net-snmp/library/snmpUDPDomain.h>
#ifdef NETSNMP_TRANSPORT_TLSBASE_DOMAIN
#include <net-snmp/library/snmpTLSBaseDomain.h>
#endif
#ifdef NETSNMP_TRANSPORT_TLSTCP_DOMAIN
#include <net-snmp/library/snmpTLSTCPDomain.h>
#endif
#ifdef NETSNMP_TRANSPORT_STD_DOMAIN
#include <net-snmp/library/snmpSTDDomain.h>
#endif
#ifdef NETSNMP_TRANSPORT_TCP_DOMAIN
#include <net-snmp/library/snmpTCPDomain.h>
#endif
#ifdef NETSNMP_TRANSPORT_DTLSUDP_DOMAIN
#include <net-snmp/library/snmpDTLSUDPDomain.h>
#endif
#ifdef NETSNMP_TRANSPORT_SSH_DOMAIN
#include <net-snmp/library/snmpSSHDomain.h>
#endif
#ifdef NETSNMP_TRANSPORT_ALIAS_DOMAIN
#include <net-snmp/library/snmpAliasDomain.h>
#endif
#ifdef NETSNMP_TRANSPORT_IPX_DOMAIN
#include <net-snmp/library/snmpIPXDomain.h>
#endif
#ifdef NETSNMP_TRANSPORT_UNIX_DOMAIN
#include <net-snmp/library/snmpUnixDomain.h>
#endif
#ifdef NETSNMP_TRANSPORT_AAL5PVC_DOMAIN
#include <net-snmp/library/snmpAAL5PVCDomain.h>
#endif
#ifdef NETSNMP_TRANSPORT_UDPIPV6_DOMAIN
#include <net-snmp/library/snmpUDPIPv6Domain.h>
#endif
#ifdef NETSNMP_TRANSPORT_TCPIPV6_DOMAIN
#include <net-snmp/library/snmpTCPIPv6Domain.h>
#endif
#ifdef NETSNMP_TRANSPORT_UDPSHARED_DOMAIN
#include <net-snmp/library/snmpUDPsharedDomain.h>
#endif
#include <net-snmp/library/snmp_api.h>
#include <net-snmp/library/snmp_service.h>
#include <net-snmp/library/read_config.h>
netsnmp_feature_child_of(transport_all, libnetsnmp);
netsnmp_feature_child_of(tdomain_support, transport_all);
netsnmp_feature_child_of(tdomain_transport_oid, transport_all);
netsnmp_feature_child_of(sockaddr_size, transport_all);
netsnmp_feature_child_of(transport_cache, transport_all);
/*
* Our list of supported transport domains.
*/
static netsnmp_tdomain *domain_list = NULL;
/*
* The standard SNMP domains.
*/
oid netsnmpUDPDomain[] = { 1, 3, 6, 1, 6, 1, 1 };
size_t netsnmpUDPDomain_len = OID_LENGTH(netsnmpUDPDomain);
oid netsnmpCLNSDomain[] = { 1, 3, 6, 1, 6, 1, 2 };
size_t netsnmpCLNSDomain_len = OID_LENGTH(netsnmpCLNSDomain);
oid netsnmpCONSDomain[] = { 1, 3, 6, 1, 6, 1, 3 };
size_t netsnmpCONSDomain_len = OID_LENGTH(netsnmpCONSDomain);
oid netsnmpDDPDomain[] = { 1, 3, 6, 1, 6, 1, 4 };
size_t netsnmpDDPDomain_len = OID_LENGTH(netsnmpDDPDomain);
oid netsnmpIPXDomain[] = { 1, 3, 6, 1, 6, 1, 5 };
size_t netsnmpIPXDomain_len = OID_LENGTH(netsnmpIPXDomain);
static netsnmp_container *_container = NULL;
static void netsnmp_tdomain_dump(void);
#if !defined(NETSNMP_FEATURE_REMOVE_FILTER_SOURCE)
static netsnmp_container * filtered = NULL;
void netsnmp_transport_parse_filter(const char *word, char *cptr);
#endif /* NETSNMP_FEATURE_REMOVE_FILTER_SOURCE */
void
init_snmp_transport(void)
{
netsnmp_ds_register_config(ASN_BOOLEAN,
"snmp", "dontLoadHostConfig",
NETSNMP_DS_LIBRARY_ID,
NETSNMP_DS_LIB_DONT_LOAD_HOST_FILES);
#ifndef NETSNMP_FEATURE_REMOVE_FILTER_SOURCE
register_app_config_handler("sourceFilterType",
netsnmp_transport_parse_filterType,
NULL, "none|acceptlist|blocklist");
register_app_config_handler("sourceFilterAddress",
netsnmp_transport_parse_filter,
netsnmp_transport_filter_cleanup,
"host");
#endif /* NETSNMP_FEATURE_REMOVE_FILTER_SOURCE */
}
void
shutdown_snmp_transport(void)
{
#ifndef NETSNMP_FEATURE_REMOVE_FILTER_SOURCE
netsnmp_transport_filter_cleanup();
#endif
}
/*
* Make a deep copy of an netsnmp_transport.
*/
netsnmp_transport *
netsnmp_transport_copy(const netsnmp_transport *t)
{
netsnmp_transport *n = NULL;
if (t == NULL) {
return NULL;
}
n = SNMP_MALLOC_TYPEDEF(netsnmp_transport);
if (n == NULL) {
return NULL;
}
if (t->domain != NULL) {
n->domain = t->domain;
n->domain_length = t->domain_length;
} else {
n->domain = NULL;
n->domain_length = 0;
}
if (t->local != NULL) {
n->local = netsnmp_memdup(t->local, t->local_length);
if (n->local == NULL) {
netsnmp_transport_free(n);
return NULL;
}
n->local_length = t->local_length;
} else {
n->local = NULL;
n->local_length = 0;
}
if (t->remote != NULL) {
n->remote = netsnmp_memdup(t->remote, t->remote_length);
if (n->remote == NULL) {
netsnmp_transport_free(n);
return NULL;
}
n->remote_length = t->remote_length;
} else {
n->remote = NULL;
n->remote_length = 0;
}
if (t->data != NULL && t->data_length > 0) {
n->data = netsnmp_memdup(t->data, t->data_length);
if (n->data == NULL) {
netsnmp_transport_free(n);
return NULL;
}
n->data_length = t->data_length;
} else {
n->data = NULL;
n->data_length = 0;
}
n->msgMaxSize = t->msgMaxSize;
n->f_accept = t->f_accept;
n->f_recv = t->f_recv;
n->f_send = t->f_send;
n->f_close = t->f_close;
n->f_copy = t->f_copy;
n->f_config = t->f_config;
n->f_fmtaddr = t->f_fmtaddr;
n->sock = t->sock;
n->flags = t->flags;
n->base_transport = netsnmp_transport_copy(t->base_transport);
/* give the transport a chance to do "special things" */
if (t->f_copy)
t->f_copy(t, n);
return n;
}
void
netsnmp_transport_free(netsnmp_transport *t)
{
if (NULL == t)
return;
#ifndef FEATURE_REMOVE_TRANSPORT_CACHE
/** don't free a transport that is currently shared */
if (netsnmp_transport_cache_remove(t) == 1)
return;
#endif
SNMP_FREE(t->local);
SNMP_FREE(t->remote);
SNMP_FREE(t->data);
netsnmp_transport_free(t->base_transport);
SNMP_FREE(t);
}
/*
* netsnmp_transport_peer_string
*
* returns string representation of peer address.
*
* caller is responsible for freeing the allocated string.
*/
char *
netsnmp_transport_peer_string(netsnmp_transport *t, const void *data, int len)
{
char *str;
if (NULL == t)
return NULL;
if (t->f_fmtaddr != NULL)
str = t->f_fmtaddr(t, data, len);
else
str = strdup("<UNKNOWN>");
return str;
}
#if !defined(NETSNMP_FEATURE_REMOVE_FILTER_SOURCE)
static int _transport_filter_init(void)
{
if (filtered)
return 0;
filtered = netsnmp_container_find("transport_filter:cstring");
if (NULL == filtered) {
NETSNMP_LOGONCE((LOG_WARNING,
"couldn't allocate container for transport_filter list\n"));
return -1;
}
filtered->container_name = strdup("transport_filter list");
return 0;
}
int
netsnmp_transport_filter_add(const char *addrtxt)
{
char *tmp;
/*
* create the container, if needed
*/
if (!filtered && _transport_filter_init()) {
snmp_log(LOG_ERR,"netsnmp_transport_filter_add %s failed\n",
addrtxt);
return (-1);
}
tmp = strdup(addrtxt);
if (NULL == tmp) {
snmp_log(LOG_ERR,"netsnmp_transport_filter_add strdup failed\n");
return(-1);
}
return CONTAINER_INSERT(filtered, tmp);
}
int
netsnmp_transport_filter_remove(const char *addrtxt)
{
/*
* create the container, if needed
*/
if (NULL == filtered)
return -1;
return CONTAINER_REMOVE(filtered, addrtxt);
}
/*
* netsnmp_transport_filter_check
*
* returns 1 if the specified address string is in the filter list
*/
int
netsnmp_transport_filter_check(const char *addrtxt)
{
char *addr;
if (NULL == filtered)
return 0;
addr = CONTAINER_FIND(filtered, addrtxt);
return addr ? 1 : 0;
}
void
netsnmp_transport_parse_filterType(const char *word, char *cptr)
{
int type = 42;
if (strcmp(cptr,"acceptlist") == 0)
type = 1;
else if (strcmp(cptr,"whitelist") == 0) {
netsnmp_config_warn("Deprecated configuration term found -- Please use 'acceptlist' instead");
type = 1;
} else if (strcmp(cptr,"blocklist") == 0)
type = -1;
else if (strcmp(cptr,"blacklist") == 0) {
netsnmp_config_warn("Deprecated configuration term found -- Please use 'blocklist' instead");
type = -1;
} else if (strcmp(cptr,"none") == 0)
type = 0;
else
netsnmp_config_error("unknown source filter type: %s", cptr);
if (type != 42) {
DEBUGMSGTL(("transport:filterType", "set to %d\n", type));
netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,
NETSNMP_DS_LIB_FILTER_TYPE, type);
}
}
void
netsnmp_transport_parse_filter(const char *word, char *cptr)
{
if (netsnmp_transport_filter_add(cptr))
netsnmp_config_error("cannot create source filter: %s", cptr);
}
void
netsnmp_transport_filter_cleanup(void)
{
if (NULL == filtered)
return;
CONTAINER_CLEAR(filtered, filtered->free_item, NULL);
CONTAINER_FREE(filtered);
filtered = NULL;
}
#endif /* NETSNMP_FEATURE_REMOVE_FILTER_SOURCE */
#ifndef NETSNMP_FEATURE_REMOVE_SOCKADDR_SIZE
int
netsnmp_sockaddr_size(const struct sockaddr *sa)
{
if (NULL == sa)
return 0;
switch (sa->sa_family) {
case AF_INET:
return sizeof(struct sockaddr_in);
break;
#ifdef NETSNMP_ENABLE_IPV6
case AF_INET6:
return sizeof(struct sockaddr_in6);
break;
#endif
}
return 0;
}
#endif /* NETSNMP_FEATURE_REMOVE_SOCKADDR_SIZE */
int
netsnmp_transport_send(netsnmp_transport *t, const void *packet, int length,
void **opaque, int *olength)
{
int dumpPacket, debugLength;
if ((NULL == t) || (NULL == t->f_send)) {
DEBUGMSGTL(("transport:pkt:send", "NULL transport or send function\n"));
return SNMPERR_GENERR;
}
dumpPacket = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
NETSNMP_DS_LIB_DUMP_PACKET);
debugLength = (SNMPERR_SUCCESS ==
debug_is_token_registered("transport:send"));
if (dumpPacket | debugLength) {
char *str = netsnmp_transport_peer_string(t,
opaque ? *opaque : NULL,
olength ? *olength : 0);
if (debugLength)
DEBUGMSGT_NC(("transport:send","%lu bytes to %s\n",
(unsigned long)length, str));
if (dumpPacket)
snmp_log(LOG_DEBUG, "\nSending %lu bytes to %s\n",
(unsigned long)length, str);
SNMP_FREE(str);
}
if (dumpPacket)
xdump(packet, length, "");
return t->f_send(t, packet, length, opaque, olength);
}
int
netsnmp_transport_recv(netsnmp_transport *t, void *packet, int length,
void **opaque, int *olength)
{
int debugLength;
if ((NULL == t) || (NULL == t->f_recv)) {
DEBUGMSGTL(("transport:recv", "NULL transport or recv function\n"));
return SNMPERR_GENERR;
}
length = t->f_recv(t, packet, length, opaque, olength);
if (length <=0)
return length; /* don't log timeouts/socket closed */
debugLength = (SNMPERR_SUCCESS ==
debug_is_token_registered("transport:recv"));
if (debugLength) {
char *str = netsnmp_transport_peer_string(t,
opaque ? *opaque : NULL,
olength ? *olength : 0);
if (debugLength)
DEBUGMSGT_NC(("transport:recv","%d bytes from %s\n",
length, str));
SNMP_FREE(str);
}
return length;
}
#ifndef NETSNMP_FEATURE_REMOVE_TDOMAIN_SUPPORT
int
netsnmp_tdomain_support(const oid * in_oid,
size_t in_len,
const oid ** out_oid, size_t * out_len)
{
netsnmp_tdomain *d = NULL;
for (d = domain_list; d != NULL; d = d->next) {
if (netsnmp_oid_equals(in_oid, in_len, d->name, d->name_length) == 0) {
if (out_oid != NULL && out_len != NULL) {
*out_oid = d->name;
*out_len = d->name_length;
}
return 1;
}
}
return 0;
}
#endif /* NETSNMP_FEATURE_REMOVE_TDOMAIN_SUPPORT */
void
netsnmp_tdomain_init(void)
{
DEBUGMSGTL(("tdomain", "netsnmp_tdomain_init() called\n"));
/* include the configure generated list of constructor calls */
#include "transports/snmp_transport_inits.h"
netsnmp_tdomain_dump();
}
void
netsnmp_clear_tdomain_list(void)
{
netsnmp_tdomain *list = domain_list, *next = NULL;
DEBUGMSGTL(("tdomain", "clear_tdomain_list() called\n"));
while (list != NULL) {
next = list->next;
SNMP_FREE(list->prefix);
/* attention!! list itself is not in the heap, so we must not free it! */
list = next;
}
domain_list = NULL;
}
static void
netsnmp_tdomain_dump(void)
{
netsnmp_tdomain *d;
int i = 0;
DEBUGMSGTL(("tdomain", "domain_list -> "));
for (d = domain_list; d != NULL; d = d->next) {
DEBUGMSG(("tdomain", "{ "));
DEBUGMSGOID(("tdomain", d->name, d->name_length));
DEBUGMSG(("tdomain", ", \""));
for (i = 0; d->prefix[i] != NULL; i++) {
DEBUGMSG(("tdomain", "%s%s", d->prefix[i],
(d->prefix[i + 1]) ? "/" : ""));
}
DEBUGMSG(("tdomain", "\" } -> "));
}
DEBUGMSG(("tdomain", "[NIL]\n"));
}
int
netsnmp_tdomain_register(netsnmp_tdomain *n)
{
netsnmp_tdomain **prevNext = &domain_list, *d;
if (n != NULL) {
for (d = domain_list; d != NULL; d = d->next) {
if (netsnmp_oid_equals(n->name, n->name_length,
d->name, d->name_length) == 0) {
/*
* Already registered.
*/
return 0;
}
prevNext = &(d->next);
}
n->next = NULL;
*prevNext = n;
return 1;
} else {
return 0;
}
}
netsnmp_feature_child_of(tdomain_unregister, netsnmp_unused);
#ifndef NETSNMP_FEATURE_REMOVE_TDOMAIN_UNREGISTER
int
netsnmp_tdomain_unregister(netsnmp_tdomain *n)
{
netsnmp_tdomain **prevNext = &domain_list, *d;
if (n != NULL) {
for (d = domain_list; d != NULL; d = d->next) {
if (netsnmp_oid_equals(n->name, n->name_length,
d->name, d->name_length) == 0) {
*prevNext = n->next;
SNMP_FREE(n->prefix);
return 1;
}
prevNext = &(d->next);
}
return 0;
} else {
return 0;
}
}
#endif /* NETSNMP_FEATURE_REMOVE_TDOMAIN_UNREGISTER */
static netsnmp_tdomain *
find_tdomain(const char* spec)
{
netsnmp_tdomain *d;
for (d = domain_list; d != NULL; d = d->next) {
int i;
for (i = 0; d->prefix[i] != NULL; i++)
if (strcasecmp(d->prefix[i], spec) == 0) {
DEBUGMSGTL(("tdomain",
"Found domain \"%s\" from specifier \"%s\"\n",
d->prefix[0], spec));
return d;
}
}
DEBUGMSGTL(("tdomain", "Found no domain from specifier \"%s\"\n", spec));
return NULL;
}
static int
netsnmp_is_fqdn(const char *thename)
{
if (!thename)
return 0;
while(*thename) {
if (*thename != '.' && !isupper((unsigned char)*thename) &&
!islower((unsigned char)*thename) &&
!isdigit((unsigned char)*thename) && *thename != '-') {
return 0;
}
thename++;
}
return 1;
}
/*
* Locate the appropriate transport domain and call the create function for
* it.
*/
netsnmp_transport *
netsnmp_tdomain_transport_tspec(netsnmp_tdomain_spec *tspec)
{
const char *application, *str, *default_domain, *default_target, *source;
int local;
netsnmp_tdomain *match = NULL;
const char *addr = NULL;
const char * const *spec = NULL;
int any_found = 0;
char buf[SNMP_MAXPATH];
const char **lspec = NULL;
char *tokenized_domain = NULL;
application = tspec->application;
str = tspec->target;
local = tspec->flags & NETSNMP_TSPEC_LOCAL;
default_domain = tspec->default_domain;
default_target = tspec->default_target;
source = tspec->source;
/** transport_config = tspec->transport_config; not used yet */
DEBUGMSGTL(("tdomain",
"tdomain_transport_spec(\"%s\", \"%s\", %d, \"%s\", \"%s\", \"%s\")\n",
application, str ? str : "[NIL]", local,
default_domain ? default_domain : "[NIL]",
default_target ? default_target : "[NIL]",
source ? source : "[NIL]"));
/* see if we can load a host-name specific set of conf files */
if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
NETSNMP_DS_LIB_DONT_LOAD_HOST_FILES) &&
netsnmp_is_fqdn(str)) {
static int have_added_handler = 0;
char *newhost;
struct config_line *config_handlers;
struct config_files file_names;
char *prev_hostname;
/* register a "transport" specifier */
if (!have_added_handler) {
have_added_handler = 1;
netsnmp_ds_register_config(ASN_OCTET_STR,
"snmp", "transport",
NETSNMP_DS_LIBRARY_ID,
NETSNMP_DS_LIB_HOSTNAME);
}
/* we save on specific setting that we don't allow to change
from one transport creation to the next; ie, we don't want
the "transport" specifier to be a default. It should be a
single invocation use only */
prev_hostname = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
NETSNMP_DS_LIB_HOSTNAME);
if (prev_hostname)
prev_hostname = strdup(prev_hostname);
/* read in the hosts/STRING.conf files */
config_handlers = read_config_get_handlers("snmp");
snprintf(buf, sizeof(buf)-1, "hosts/%s", str);
file_names.fileHeader = buf;
file_names.start = config_handlers;
file_names.next = NULL;
DEBUGMSGTL(("tdomain", "checking for host specific config %s\n",
buf));
read_config_files_of_type(EITHER_CONFIG, &file_names);
if (NULL !=
(newhost = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
NETSNMP_DS_LIB_HOSTNAME))) {
strlcpy(buf, newhost, sizeof(buf));
str = buf;
}
netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID,
NETSNMP_DS_LIB_HOSTNAME,
prev_hostname);
SNMP_FREE(prev_hostname);
}
/* First try - assume that there is a domain in str (domain:target) */
if (str != NULL) {
const char *cp;
if ((cp = strchr(str, ':')) != NULL) {
char* mystring = (char*)malloc(cp + 1 - str);
memcpy(mystring, str, cp - str);
mystring[cp - str] = '\0';
addr = cp + 1;
match = find_tdomain(mystring);
free(mystring);
}
}
/*
* Second try, if there is no domain in str (target), then try the
* default domain
*/
if (match == NULL) {
addr = str;
if (addr && *addr == '/') {
DEBUGMSGTL(("tdomain",
"Address starts with '/', so assume \"unix\" "
"domain\n"));
match = find_tdomain("unix");
} else if (default_domain) {
DEBUGMSGTL(("tdomain",
"Use user specified default domain \"%s\"\n",
default_domain));
if (!strchr(default_domain, ','))
match = find_tdomain(default_domain);
else {
int commas = 0;
const char *cp = default_domain;
char *ptr = NULL;
tokenized_domain = strdup(default_domain);
while (*++cp) if (*cp == ',') commas++;
lspec = calloc(commas+2, sizeof(char *));
commas = 1;
lspec[0] = strtok_r(tokenized_domain, ",", &ptr);
while ((lspec[commas++] = strtok_r(NULL, ",", &ptr)))
;
spec = lspec;
}
} else {
spec = netsnmp_lookup_default_domains(application);
if (spec == NULL) {
DEBUGMSGTL(("tdomain",
"No default domain found, assume \"udp\"\n"));
match = find_tdomain("udp");
} else {
const char * const * r = spec;
DEBUGMSGTL(("tdomain",
"Use application default domains"));
while(*r) {
DEBUGMSG(("tdomain", " \"%s\"", *r));
++r;
}
DEBUGMSG(("tdomain", "\n"));
}
}
}
for(;;) {
if (match) {
netsnmp_transport *t = NULL;
const char* addr2;
any_found = 1;
/*
* Ok, we know what domain to try, lets see what default data
* should be used with it
*/
if (default_target != NULL)
addr2 = default_target;
else
addr2 = netsnmp_lookup_default_target(application,
match->prefix[0]);
DEBUGMSGTL(("tdomain",
"trying domain \"%s\" address \"%s\" "
"default address \"%s\"\n",
match->prefix[0], addr ? addr : "[NIL]",
addr2 ? addr2 : "[NIL]"));
if (match->f_create_from_tspec) {
netsnmp_tdomain_spec tspec_tmp;
memcpy(&tspec_tmp, tspec, sizeof(tspec_tmp));
/** if we didn't have a default target but looked one up,
* copy the spec and use the found default. */
if ((default_target == NULL) && (addr2 != NULL))
tspec_tmp.default_target = addr2;
if (addr != tspec_tmp.target)
tspec_tmp.target = addr;
t = match->f_create_from_tspec(&tspec_tmp);
}
else {
#if 0 /** remove warning until all transports implement tspec */
NETSNMP_LOGONCE((LOG_WARNING,
"transport domain %s uses deprecated f_create function\n",
match->prefix[0]));
#endif
if (match->f_create_from_tstring) {
t = match->f_create_from_tstring(addr, local);
}
else
t = match->f_create_from_tstring_new(addr, local, addr2);
}
if (t) {
if (lspec) {
free(tokenized_domain);
free(lspec);
}
return t;
}
}
addr = str;
if (spec && *spec)
match = find_tdomain(*spec++);
else
break;
}
if (!any_found)
snmp_log(LOG_ERR, "No support for any checked transport domain\n");
if (lspec) {
free(tokenized_domain);
free(lspec);
}
return NULL;
}
netsnmp_transport *
netsnmp_tdomain_transport_full(const char *application,
const char *str, int local,
const char *default_domain,
const char *default_target)
{
netsnmp_tdomain_spec tspec;
memset(&tspec, 0x0, sizeof(tspec));
tspec.application = application;
tspec.target = str;
if (local)
tspec.flags |= NETSNMP_TSPEC_LOCAL;
tspec.default_domain = default_domain;
tspec.default_target = default_target;
tspec.source = NULL;
tspec.transport_config = NULL;
return netsnmp_tdomain_transport_tspec(&tspec);
}
netsnmp_transport *
netsnmp_tdomain_transport(const char *str, int local,
const char *default_domain)
{
netsnmp_tdomain_spec tspec;
memset(&tspec, 0x0, sizeof(tspec));
tspec.application = "snmp";
tspec.target = str;
if (local)
tspec.flags |= NETSNMP_TSPEC_LOCAL;
tspec.default_domain = default_domain;
tspec.default_target = NULL;
tspec.source = NULL;
tspec.transport_config = NULL;
return netsnmp_tdomain_transport_tspec(&tspec);
}
#ifndef NETSNMP_FEATURE_REMOVE_TDOMAIN_TRANSPORT_OID
/*
* The format of @dom and @o follows the TDomain and TAddress textual
* conventions from RFC 2579. For the actual TAddress format definitions,
* see e.g. SnmpUDPAddress in RFC 1906.
*/
netsnmp_transport *
netsnmp_tdomain_transport_oid(const oid * dom,
size_t dom_len,
const u_char * o, size_t o_len, int local)
{
netsnmp_tdomain *d;
int i;
DEBUGMSGTL(("tdomain", "domain \""));
DEBUGMSGOID(("tdomain", dom, dom_len));
DEBUGMSG(("tdomain", "\"\n"));
for (d = domain_list; d != NULL; d = d->next) {
for (i = 0; d->prefix[i] != NULL; i++) {
if (netsnmp_oid_equals(dom, dom_len, d->name, d->name_length) ==
0) {
return d->f_create_from_ostring(o, o_len, local);
}
}
}
snmp_log(LOG_ERR, "No support for requested transport domain\n");
return NULL;
}
#endif /* NETSNMP_FEATURE_REMOVE_TDOMAIN_TRANSPORT_OID */
netsnmp_transport*
netsnmp_transport_open(const char* application, const char* str, int local)
{
return netsnmp_tdomain_transport_full(application, str, local, NULL, NULL);
}
netsnmp_transport*
netsnmp_transport_open_server(const char* application, const char* str)
{
return netsnmp_tdomain_transport_full(application, str, 1, NULL, NULL);
}
netsnmp_transport*
netsnmp_transport_open_client(const char* application, const char* str)
{
return netsnmp_tdomain_transport_full(application, str, 0, NULL, NULL);
}
/** adds a transport to a linked list of transports.
Returns 1 on failure, 0 on success */
int
netsnmp_transport_add_to_list(netsnmp_transport_list **transport_list,
netsnmp_transport *transport)
{
netsnmp_transport_list *newptr =
SNMP_MALLOC_TYPEDEF(netsnmp_transport_list);
if (!newptr)
return 1;
newptr->next = *transport_list;
newptr->transport = transport;
*transport_list = newptr;
return 0;
}
/** removes a transport from a linked list of transports.
Returns 1 on failure, 0 on success */
int
netsnmp_transport_remove_from_list(netsnmp_transport_list **transport_list,
netsnmp_transport *transport)
{
netsnmp_transport_list *ptr = *transport_list, *lastptr = NULL;
while (ptr && ptr->transport != transport) {
lastptr = ptr;
ptr = ptr->next;
}
if (!ptr)
return 1;
if (lastptr)
lastptr->next = ptr->next;
else
*transport_list = ptr->next;
SNMP_FREE(ptr);
return 0;
}
int
netsnmp_transport_config_compare(netsnmp_transport_config *left,
netsnmp_transport_config *right) {
return strcmp(left->key, right->key);
}
netsnmp_transport_config *
netsnmp_transport_create_config(const char *key, const char *value)
{
netsnmp_transport_config *entry =
SNMP_MALLOC_TYPEDEF(netsnmp_transport_config);
entry->key = strdup(key);
entry->value = strdup(value);
return entry;
}
#ifndef FEATURE_REMOVE_TRANSPORT_CACHE
/* *************************************************************************
* transport caching by address family, type and use
*/
typedef struct trans_cache_s {
netsnmp_transport *t;
int af;
int type;
int local;
netsnmp_sockaddr_storage bind_addr;
int count; /* number of times this transport has been returned */
} trans_cache;
static void _tc_free_item(trans_cache *tc, void *context);
static int _tc_compare(trans_cache *lhs, trans_cache *rhs);
/** initialize transport cache */
static int
_tc_init(void)
{
DEBUGMSGTL(("transport:cache:init", "%p\n", _container));
/** prevent double init */
if (NULL != _container)
return 0;
_container = netsnmp_container_find("trans_cache:binary_array");
if (NULL == _container) {
snmp_log(LOG_ERR, "failed to allocate trans_cache container\n");
return 1;
}
_container->container_name = strdup("trans_cache");
_container->free_item = (netsnmp_container_obj_func*) _tc_free_item;
_container->compare = (netsnmp_container_compare*) _tc_compare;
return 0;
}
/*
* container compare function
*
* sort by af, type, local
*/
static int
_tc_compare(trans_cache *lhs, trans_cache *rhs)
{
netsnmp_assert((lhs != NULL) && (rhs != NULL));
DEBUGMSGTL(("9:transport:cache:compare", "%p/%p\n", lhs, rhs));
if (lhs->af < rhs->af)
return -1;
else if (lhs->af > rhs->af)
return 1;
if (lhs->type < rhs->type)
return -1;
else if (lhs->type > rhs->type)
return 1;
if (lhs->local < rhs->local)
return -1;
else if (lhs->local > rhs->local)
return 1;
if (AF_INET == lhs->af) {
struct sockaddr_in *lha = &lhs->bind_addr.sin,
*rha = &rhs->bind_addr.sin;
if (lha->sin_addr.s_addr < rha->sin_addr.s_addr)
return -1;
else if (lha->sin_addr.s_addr > rha->sin_addr.s_addr)
return 1;
if (lha->sin_port < rha->sin_port)
return -1;
else if (lha->sin_port > rha->sin_port)
return 1;
}
#ifdef NETSNMP_ENABLE_IPV6
else if (AF_INET6 == lhs->af) {
struct sockaddr_in6 *lha = &lhs->bind_addr.sin6,
*rha = &rhs->bind_addr.sin6;
int rc = memcmp(lha->sin6_addr.s6_addr, rha->sin6_addr.s6_addr,
sizeof(rha->sin6_addr.s6_addr));
if (rc)
return rc;
if (lha->sin6_port < rha->sin6_port)
return -1;
else if (lha->sin6_port > rha->sin6_port)
return 1;
if (lha->sin6_flowinfo < rha->sin6_flowinfo)
return -1;
else if (lha->sin6_flowinfo > rha->sin6_flowinfo)
return 1;
if (lha->sin6_scope_id < rha->sin6_scope_id)
return -1;
else if (lha->sin6_scope_id > rha->sin6_scope_id)
return 1;
}
#endif
return 0;
}
static void
_tc_free(trans_cache *tc)
{
if (NULL == tc)
return;
DEBUGMSGTL(("transport:cache:free", "%p %d/%d/%d/%p %d\n", tc, tc->af,
tc->type, tc->local, tc->t, tc->count));
netsnmp_transport_free(tc->t);
memset(tc, 0x0, sizeof(*tc));
free(tc);
}
static void
_tc_free_item(trans_cache *tc, void *context)
{
_tc_free(tc);
}
static void
_tc_remove(trans_cache *tc)
{
if (NULL == tc || NULL == _container)
return;
DEBUGMSGTL(("transport:cache:remove", "%p\n", tc));
CONTAINER_REMOVE(_container, tc);
}
static trans_cache *
_tc_create(int af, int type, int local, const netsnmp_sockaddr_storage *addr,
netsnmp_transport *t)
{
trans_cache *tc = SNMP_MALLOC_TYPEDEF(trans_cache);
if (NULL == tc) {
snmp_log(LOG_ERR, "failed to allocate trans_cache\n");
return NULL;
}
DEBUGMSGTL(("transport:cache:create", "%p\n", tc));
tc->af = af;
tc->type = type;
tc->local = local;
tc->t = t;
if (addr)
memcpy(&tc->bind_addr, addr, sizeof(tc->bind_addr));
/** we only understand ipv6 and ipv6 sockaddrs in compare */
if (AF_INET != tc->af && AF_INET6 != tc->af)
NETSNMP_LOGONCE((LOG_WARNING, "transport cache not tested for af %d\n",
tc->af));
return tc;
}
static trans_cache *
_tc_add(int af, int type, int local, const netsnmp_sockaddr_storage *addr,
netsnmp_transport *t)
{
trans_cache *tc;
int rc;
DEBUGMSGTL(("transport:cache:add", "%d/%d/%d/%p\n", af, type, local, t));
if (NULL == _container) {
_tc_init();
if (NULL == _container)
return NULL;
}
tc = _tc_create(af, type, local, addr, t);
if (NULL == tc) {
DEBUGMSGTL(("transport:cache:add",
"could not create transport cache\n"));
return NULL;
}
rc = CONTAINER_INSERT(_container, tc);
if (rc) {
DEBUGMSGTL(("transport:cache:add", "container insert failed\n"));
_tc_free(tc);
return NULL;
}
return tc;
}
trans_cache *
_tc_find(int af, int type, int local, const netsnmp_sockaddr_storage *addr)
{
trans_cache tc, *rtn;
DEBUGMSGTL(("transport:cache:find", "%d/%d/%d\n", af, type, local));
if (NULL == _container)
return NULL;
memset(&tc, 0x00, sizeof(tc));
tc.af = af;
tc.type = type;
tc.local = local;
if (addr)
memcpy(&tc.bind_addr, addr, sizeof(tc.bind_addr));
rtn = CONTAINER_FIND(_container, &tc);
DEBUGMSGTL(("transport:cache:find", "%p\n", rtn));
return rtn;
}
trans_cache *
_tc_find_transport(netsnmp_transport *t)
{
/*
* we shouldn't really have that many transports, so instead of
* using an additional key, just iterate over the whole container.
*/
netsnmp_iterator *itr;
trans_cache *tc;
DEBUGMSGTL(("transport:cache:find_transport", "%p\n", t));
if (NULL == _container)
return NULL;
itr = CONTAINER_ITERATOR(_container);
if (NULL == itr) {
snmp_log(LOG_ERR, "could not get iterator for transport cache\n");
return NULL;
}
tc = ITERATOR_FIRST(itr);
for( ; tc; tc = ITERATOR_NEXT(itr))
if (tc->t == t)
break;
ITERATOR_RELEASE(itr);
DEBUGMSGT(("transport:cache:find_transport","found %p\n", tc));
return tc;
}
int
netsnmp_transport_cache_remove(netsnmp_transport *t)
{
trans_cache *tc;
DEBUGMSGTL(("transport:cache:close", "%p\n", t));
if (NULL == t)
return 0;
/** transport in cache? */
tc = _tc_find_transport(t);
if (NULL == tc) {
DEBUGMSGTL(("transport:cache:close", "%p not found in cache\n", t));
return 0;
}
--tc->count;
/** still in use? */
if (tc->count > 0) {
DEBUGMSGTL(("transport:cache:close", "still %d user(s) of %p\n",
tc->count, t));
return 1;
}
/** unbalanced get/close? */
if (tc->count < 0)
snmp_log(LOG_WARNING, "transport cache get/close mismatch\n");
_tc_remove(tc);
_tc_free(tc); /* also does close */
return 0;
}
/*
* netsnmp_transport_get: get a (possibly duplicate, cached) transport
*/
netsnmp_transport *
netsnmp_transport_cache_get(int af, int type, int local,
const netsnmp_sockaddr_storage *bind_addr)
{
trans_cache *tc;
netsnmp_transport *t;
DEBUGMSGTL(("transport:cache:get", "%d/%d/%d\n", af, type, local));
#define USE_CACHE 1
#if USE_CACHE
/** check for existing transport */
tc = _tc_find(af, type, local, bind_addr);
if (tc) {
DEBUGMSGTL(("transport:cache:get", "using existing transport %p\n",
tc->t));
++tc->count;
return tc->t;
}
#endif
/** get transport */
t = NULL; /* _transport(af, type, 0);*/
if (NULL == t) {
snmp_log(LOG_ERR, "could not get new transport for %d/%d/%d\n", af,
type, local);
return NULL;
}
DEBUGMSGTL(("transport:cache:get", "new transport %p\n", t));
#if USE_CACHE
/** create transport cache for new transport */
tc = _tc_add(af, type, local, bind_addr, t);
if (NULL == tc) {
DEBUGMSGTL(("transport:cache:get", "could not create transport cache\n"));
/*
* hmmm.. this isn't really a critical error, is it? We have a
* transport, just no cache for it. Let's continue on and hope for the
* best.
*/
/** return -1; */
}
tc->count = 1;
#endif
return t;
}
int
netsnmp_transport_cache_save(int af, int type, int local,
const netsnmp_sockaddr_storage *addr,
netsnmp_transport *t)
{
if (NULL == t)
return 1;
if (NULL == _tc_add(af, type, local, addr, t))
return 1;
return 0;
}
#endif /* FEATURE_REMOVE_TRANSPORT_CACHE */
|