77 static void *SCHInfoAllocUserDataOSPolicy(
const char *host_os)
82 SCLogError(
"Invalid enum map inside SCHInfoAddHostOSInfo()");
86 int *user_data =
SCMalloc(
sizeof(
int));
87 if (user_data == NULL) {
88 FatalError(
"Error allocating memory. Exiting");
91 return (
void *)user_data;
99 static void SCHInfoFreeUserDataOSPolicy(
void *data)
105 static const SCRadix4Config ospolicy_radix4_config = { SCHInfoFreeUserDataOSPolicy, NULL };
106 static const SCRadix6Config ospolicy_radix6_config = { SCHInfoFreeUserDataOSPolicy, NULL };
125 char *ip_str_rem = NULL;
126 int *user_data = NULL;
127 bool recursive =
false;
129 if (host_os == NULL || host_os_ip_range == NULL ||
130 strlen(host_os_ip_range) == 0) {
136 if ( (user_data = SCHInfoAllocUserDataOSPolicy(host_os)) == NULL) {
143 if ( (strcasecmp(host_os_ip_range,
"default")) == 0) {
145 host_os_ip_range =
"0.0.0.0/0";
147 host_os_ip_range =
"::/0";
150 if ( (ip_str =
SCStrdup(host_os_ip_range)) == NULL) {
155 if ((ip_str_rem = strchr(ip_str,
',')) != NULL) {
156 ip_str_rem[0] =
'\0';
161 if (strchr(ip_str,
':') == NULL) {
163 &sc_hinfo_tree4, &ospolicy_radix4_config, ip_str, user_data)) {
165 SCLogError(
"duplicate ipv4 address '%s'", ip_str);
167 SCLogError(
"invalid ipv4 address '%s'", ip_str);
169 SCHInfoFreeUserDataOSPolicy(user_data);
175 &sc_hinfo_tree6, &ospolicy_radix6_config, ip_str, user_data)) {
177 SCLogError(
"duplicate ipv6 address '%s'", ip_str);
179 SCLogError(
"invalid ipv6 address '%s'", ip_str);
181 SCHInfoFreeUserDataOSPolicy(user_data);
203 static int SCHInfoGetHostOSFlavour(
const char *
str)
205 void *user_data = NULL;
207 if (
str == NULL || strchr(
str,
'/') != NULL)
210 if (strchr(
str,
':') != NULL) {
211 struct in6_addr addr;
212 if (inet_pton(AF_INET6,
str, &addr) <= 0) {
217 if (user_data == NULL)
220 return *((
int *)user_data);
223 if (inet_pton(AF_INET,
str, &addr) <= 0) {
228 if (user_data == NULL)
231 return *((
int *)user_data);
246 void *user_data = NULL;
248 if (user_data == NULL)
250 return *((
int *)user_data);
263 void *user_data = NULL;
265 if (user_data == NULL)
267 return *((
int *)user_data);
292 if (host->
val != NULL && strchr(host->
val,
':') != NULL)
295 FatalError(
"Failed to add host \"%s\" with policy \"%s\" to host "
309 static void SCHInfoCreateContextBackup(
void)
311 sc_hinfo_tree_backup4 = sc_hinfo_tree4;
313 sc_hinfo_tree_backup6 = sc_hinfo_tree6;
317 static void SCHInfoRestoreContextBackup(
void)
319 sc_hinfo_tree4 = sc_hinfo_tree_backup4;
321 sc_hinfo_tree6 = sc_hinfo_tree_backup6;
329 static int SCHInfoTestInvalidOSFlavour01(
void)
331 SCHInfoCreateContextBackup();
358 SCHInfoRestoreContextBackup();
367 static int SCHInfoTestInvalidIPV4Address02(
void)
369 SCHInfoCreateContextBackup();
396 SCHInfoRestoreContextBackup();
405 static int SCHInfoTestInvalidIPV6Address03(
void)
407 SCHInfoCreateContextBackup();
427 "1921.6311:6241:6422:7352:ABBB:DDDD:EEEE/129",
436 SCHInfoRestoreContextBackup();
447 static int SCHInfoTestValidIPV4Address04(
void)
449 SCHInfoCreateContextBackup();
490 if (SCHInfoGetHostOSFlavour(
"192.168.1.1") !=
494 if (SCHInfoGetHostOSFlavour(
"192.168.1.2") != -1) {
497 if (SCHInfoGetHostOSFlavour(
"192.168.1.100") !=
501 if (SCHInfoGetHostOSFlavour(
"192.192.2.4") != -1) {
504 if (SCHInfoGetHostOSFlavour(
"192.168.2.4") !=
508 if (SCHInfoGetHostOSFlavour(
"192.192.1.5") !=
512 if (SCHInfoGetHostOSFlavour(
"192.168.10.20") !=
516 if (SCHInfoGetHostOSFlavour(
"111.163.151.62") !=
520 if (SCHInfoGetHostOSFlavour(
"11.1.120.210") !=
524 if (SCHInfoGetHostOSFlavour(
"19.18.110.210") !=
528 if (SCHInfoGetHostOSFlavour(
"19.18.120.110") !=
532 if (SCHInfoGetHostOSFlavour(
"191.168.11.128") !=
536 if (SCHInfoGetHostOSFlavour(
"191.168.11.192") !=
540 if (SCHInfoGetHostOSFlavour(
"191.168.11.224") != -1) {
548 SCHInfoRestoreContextBackup();
559 static int SCHInfoTestValidIPV4Address05(
void)
561 SCHInfoCreateContextBackup();
603 if (SCHInfoGetHostOSFlavour(
"192.168.1.1") !=
607 if (SCHInfoGetHostOSFlavour(
"192.168.1.2") != -1) {
610 if (SCHInfoGetHostOSFlavour(
"192.168.1.100") !=
614 if (SCHInfoGetHostOSFlavour(
"192.192.2.4") != -1) {
617 if (SCHInfoGetHostOSFlavour(
"192.168.2.4") !=
621 if (SCHInfoGetHostOSFlavour(
"192.192.1.5") !=
625 if (SCHInfoGetHostOSFlavour(
"192.168.10.20") !=
629 if (SCHInfoGetHostOSFlavour(
"111.163.151.62") !=
633 if (SCHInfoGetHostOSFlavour(
"111.162.208.0") !=
637 if (SCHInfoGetHostOSFlavour(
"111.162.210.1") !=
641 if (SCHInfoGetHostOSFlavour(
"111.162.214.1") !=
645 if (SCHInfoGetHostOSFlavour(
"111.162.0.0") != -1) {
648 if (SCHInfoGetHostOSFlavour(
"111.162.240.112") != -1) {
651 if (SCHInfoGetHostOSFlavour(
"111.162.240.1") !=
655 if (SCHInfoGetHostOSFlavour(
"111.162.214.100") !=
659 if (inet_pton(AF_INET,
"111.162.208.100", &in) < 0) {
666 if (SCHInfoGetHostOSFlavour(
"111.162.194.112") !=
670 if (SCHInfoGetHostOSFlavour(
"111.162.208.200") !=
674 if (inet_pton(AF_INET,
"111.162.208.200", &in) < 0) {
681 if (SCHInfoGetHostOSFlavour(
"111.162.200.201") != -1) {
689 SCHInfoRestoreContextBackup();
700 static int SCHInfoTestValidIPV6Address06(
void)
702 SCHInfoCreateContextBackup();
707 "2351:2512:6211:6246:235A:6242:2352:62AD",
712 "6961:6121:2132:6241:423A:2135:2461:621D",
717 "DD13:613D:F312:62DD:6213:421A:6212:2652",
722 "9891:2131:2151:6426:1342:674D:622F:2342",
727 "3525:2351:4223:6211:2311:2667:6242:2154",
732 "1511:6211:6726:7777:1212:2333:6222:7722",
737 "2666:6222:7222:2335:6223:7722:3425:2362",
742 "8762:2352:6241:7245:EE23:21AD:2312:622C",
747 "6422:EE1A:2621:34AD:2462:432D:642E:E13A",
752 "3521:7622:6241:6242:7277:1234:2352:6234",
757 "2141:6232:6252:2223:7734:2345:6245:6222",
762 "5222:6432:6432:2322:6662:3423:4322:3245",
767 if (SCHInfoGetHostOSFlavour(
"2351:2512:6211:6246:235A:6242:2352:62AD") !=
771 if (SCHInfoGetHostOSFlavour(
"2351:2512:6211:6246:235A:6242:2352:6FFFE") != -1) {
774 if (SCHInfoGetHostOSFlavour(
"DD13:613D:F312:62DD:6213:421A:6212:2652") !=
778 if (SCHInfoGetHostOSFlavour(
"DD13:613D:F312:62DD:6213:421A:6212:2222") != -1) {
781 if (SCHInfoGetHostOSFlavour(
"9891:2131:2151:6426:1342:674D:622F:2342") !=
785 if (SCHInfoGetHostOSFlavour(
"3525:2351:4223:6211:2311:2667:6242:2154") !=
789 if (SCHInfoGetHostOSFlavour(
"1511:6211:6726:7777:1212:2333:6222:7722") !=
793 if (SCHInfoGetHostOSFlavour(
"2666:6222:7222:2335:6223:7722:3425:2362") !=
797 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:21AD:2312:622C") !=
801 if (SCHInfoGetHostOSFlavour(
"6422:EE1A:2621:34AD:2462:432D:642E:E13A") !=
805 if (SCHInfoGetHostOSFlavour(
"3521:7622:6241:6242:7277:1234:2352:6234") !=
809 if (SCHInfoGetHostOSFlavour(
"2141:6232:6252:2223:7734:2345:6245:6222") !=
813 if (SCHInfoGetHostOSFlavour(
"5222:6432:6432:2322:6662:3423:4322:3245") !=
817 if (SCHInfoGetHostOSFlavour(
"5222:6432:6432:2322:6662:3423:4322:DDDD") != -1) {
825 SCHInfoRestoreContextBackup();
836 static int SCHInfoTestValidIPV6Address07(
void)
838 SCHInfoCreateContextBackup();
843 "2351:2512:6211:6246:235A:6242:2352:62AD",
848 "6961:6121:2132:6241:423A:2135:2461:621D",
853 "DD13:613D:F312:62DD:6213:421A:6212:2652",
858 "9891:2131:2151:6426:1342:674D:622F:2342",
863 "3525:2351:4223:6211:2311:2667:6242:2154",
868 "1511:6211:6726:7777:1212:2333:6222:7722",
873 "2666:6222:7222:2335:6223:7722:3425:2362",
878 "8762:2352:6241:7245:EE23:21AD:2312:622C/68",
883 "8762:2352:6241:7245:EE23:21AD:2412:622C",
888 "8762:2352:6241:7245:EE23:21AD:FFFF:622C",
893 "8762:2352:6241:7245:EE23:21AD:2312:62FF",
898 "8762:2352:6241:7245:EE23:21AD:2121:1212",
903 if (SCHInfoGetHostOSFlavour(
"2351:2512:6211:6246:235A:6242:2352:62AD") !=
907 if (SCHInfoGetHostOSFlavour(
"2351:2512:6211:6246:235A:6242:2352:6FFFE") != -1) {
910 if (SCHInfoGetHostOSFlavour(
"DD13:613D:F312:62DD:6213:421A:6212:2652") !=
914 if (SCHInfoGetHostOSFlavour(
"DD13:613D:F312:62DD:6213:421A:6212:2222") != -1) {
917 if (SCHInfoGetHostOSFlavour(
"9891:2131:2151:6426:1342:674D:622F:2342") !=
921 if (SCHInfoGetHostOSFlavour(
"3525:2351:4223:6211:2311:2667:6242:2154") !=
925 if (SCHInfoGetHostOSFlavour(
"1511:6211:6726:7777:1212:2333:6222:7722") !=
929 if (SCHInfoGetHostOSFlavour(
"2666:6222:7222:2335:6223:7722:3425:2362") !=
933 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:21AD:2312:622C") !=
937 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:21AD:2412:622C") !=
941 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:21AD:FFFF:622C") !=
945 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:21AD:2312:62FF") !=
949 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:21AD:2121:1212") !=
953 if (SCHInfoGetHostOSFlavour(
"5222:6432:6432:2322:6662:3423:4322:DDDD") != -1) {
956 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:21AD:2121:1DDD") !=
960 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:FFFF:2121:1DDD") !=
964 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:21AD:2312:622C") !=
968 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE00:0000:0000:0000") !=
972 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:E000:0000:0000:0000") !=
981 SCHInfoRestoreContextBackup();
992 static int SCHInfoTestValidIPV6Address08(
void)
994 SCHInfoCreateContextBackup();
1000 "2351:2512:6211:6246:235A:6242:2352:62AD",
1005 "6961:6121:2132:6241:423A:2135:2461:621D",
1010 "DD13:613D:F312:62DD:6213:421A:6212:2652",
1015 "9891:2131:2151:6426:1342:674D:622F:2342",
1020 "3525:2351:4223:6211:2311:2667:6242:2154",
1025 "1511:6211:6726:7777:1212:2333:6222:7722",
1030 "2666:6222:7222:2335:6223:7722:3425:2362",
1035 "8762:2352:6241:7245:EE23:21AD:2312:622C/68",
1040 "8762:2352:6241:7245:EE23:21AD:2412:622C",
1045 "8762:2352:6241:7245:EE23:21AD:FFFF:622C",
1050 "8762:2352:6241:7245:EE23:21AD:2312:62FF",
1055 "8762:2352:6241:7245:EE23:21AD:2121:1212",
1066 if (SCHInfoGetHostOSFlavour(
"2351:2512:6211:6246:235A:6242:2352:62AD") !=
1070 if (SCHInfoGetHostOSFlavour(
"2351:2512:6211:6246:235A:6242:2352:6FFF") !=
1074 if (SCHInfoGetHostOSFlavour(
"DD13:613D:F312:62DD:6213:421A:6212:2652") !=
1078 if (SCHInfoGetHostOSFlavour(
"DD13:613D:F312:62DD:6213:421A:6212:2222") !=
1082 if (SCHInfoGetHostOSFlavour(
"9891:2131:2151:6426:1342:674D:622F:2342") !=
1086 if (SCHInfoGetHostOSFlavour(
"3525:2351:4223:6211:2311:2667:6242:2154") !=
1090 if (SCHInfoGetHostOSFlavour(
"1511:6211:6726:7777:1212:2333:6222:7722") !=
1094 if (SCHInfoGetHostOSFlavour(
"2666:6222:7222:2335:6223:7722:3425:2362") !=
1098 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:21AD:2312:622C") !=
1102 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:21AD:2412:622C") !=
1106 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:21AD:FFFF:622C") !=
1110 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:21AD:2312:62FF") !=
1114 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:21AD:2121:1212") !=
1118 if (SCHInfoGetHostOSFlavour(
"5222:6432:6432:2322:6662:3423:4322:DDDD") !=
1122 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:21AD:2121:1DDD") !=
1126 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:FFFF:2121:1DDD") !=
1130 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE23:21AD:2312:622C") !=
1134 if (SCHInfoGetHostOSFlavour(
"8762:2352:6241:7245:EE00:0000:0000:0000") !=
1138 if (inet_pton(AF_INET6,
"8762:2352:6241:7245:E000:0000:0000:0000", &in6) < 0) {
1145 if (inet_pton(AF_INET6,
"AD23:2DDA:6D1D:A223:E235:0232:1241:1666", &in6) < 0) {
1152 if (SCHInfoGetHostOSFlavour(
"8.8.8.8") !=
1160 SCHInfoRestoreContextBackup();
1171 static int SCHInfoTestValidIPV4Address09(
void)
1173 SCHInfoCreateContextBackup();
1177 FAIL_IF(SCHInfoGetHostOSFlavour(
"192.168.1.0") !=
1181 FAIL_IF(SCHInfoGetHostOSFlavour(
"192.168.1.0") !=
1184 FAIL_IF(SCHInfoGetHostOSFlavour(
"192.168.50.128") !=
1187 FAIL_IF(SCHInfoGetHostOSFlavour(
"192.168.50.128") !=
1189 FAIL_IF(SCHInfoGetHostOSFlavour(
"192.168.1.100") !=
1193 SCHInfoRestoreContextBackup();
1200 static int SCHInfoTestLoadFromConfig01(
void)
1207 windows: [10.0.0.0/8, 192.168.1.0/24]\n\
1208 linux: [10.0.0.5/32]\n\
1213 SCHInfoCreateContextBackup();
1226 if (SCHInfoGetHostOSFlavour(
"172.168.1.1") !=
OS_POLICY_BSD)
1235 SCHInfoRestoreContextBackup();
1243 static int SCHInfoTestLoadFromConfig02(
void)
1249 one-two: [0.0.0.0/0]\n\
1252 six-seven_eight: [10.0.0.0/8, 192.168.1.0/24]\n\
1253 nine_ten_eleven: [10.0.0.5/32]\n\
1258 SCHInfoCreateContextBackup();
1274 if (strcmp(
"one-two", policy->
name) != 0)
1278 if (strcmp(
"one-two-three", policy->
name) != 0)
1282 if (strcmp(
"four-five", policy->
name) != 0)
1286 if (strcmp(
"six-seven-eight", policy->
name) != 0)
1290 if (strcmp(
"nine-ten-eleven", policy->
name) != 0)
1303 SCHInfoRestoreContextBackup();
1311 static int SCHInfoTestLoadFromConfig03(
void)
1317 bsd-right: [0.0.0.1]\n\
1318 old-linux: [0.0.0.2]\n\
1319 old-solaris: [0.0.0.3]\n\
1320 windows: [0.0.0.4]\n\
1326 SCHInfoCreateContextBackup();
1339 printf(
"Invalid enum map inside\n");
1350 SCHInfoRestoreContextBackup();
1357 static int SCHInfoTestLoadFromConfig04(
void)
1363 bsd_right: [0.0.0.1]\n\
1364 old_linux: [0.0.0.2]\n\
1365 old_solaris: [0.0.0.3]\n\
1366 windows: [0.0.0.4]\n\
1372 SCHInfoCreateContextBackup();
1385 printf(
"Invalid enum map inside\n");
1396 SCHInfoRestoreContextBackup();
1403 static int SCHInfoTestLoadFromConfig05(
void)
1409 bsd_right: [0.0.0.1]\n\
1410 old_linux: [0.0.0.2]\n\
1411 old-solaris: [0.0.0.3]\n\
1412 windows: [0.0.0.4]\n\
1416 SCHInfoCreateContextBackup();
1428 FAIL_IF (SCHInfoGetHostOSFlavour(
"0.0.0.0") != -1);
1429 FAIL_IF (SCHInfoGetHostOSFlavour(
"0.0.0.6") != -1);
1433 SCHInfoRestoreContextBackup();
1444 UtRegisterTest(
"SCHInfoTestInvalidOSFlavour01", SCHInfoTestInvalidOSFlavour01);
1446 SCHInfoTestInvalidIPV4Address02);
1448 SCHInfoTestInvalidIPV6Address03);
1450 SCHInfoTestValidIPV4Address04);
1452 SCHInfoTestValidIPV4Address05);
1454 SCHInfoTestValidIPV6Address06);
1456 SCHInfoTestValidIPV6Address07);
1458 SCHInfoTestValidIPV6Address08);
1460 SCHInfoTestValidIPV4Address09);
1462 UtRegisterTest(
"SCHInfoTestLoadFromConfig01", SCHInfoTestLoadFromConfig01);
1463 UtRegisterTest(
"SCHInfoTestLoadFromConfig02", SCHInfoTestLoadFromConfig02);
1464 UtRegisterTest(
"SCHInfoTestLoadFromConfig03", SCHInfoTestLoadFromConfig03);
1465 UtRegisterTest(
"SCHInfoTestLoadFromConfig04", SCHInfoTestLoadFromConfig04);
1466 UtRegisterTest(
"SCHInfoTestLoadFromConfig05", SCHInfoTestLoadFromConfig05);