suricata
util-host-os-info.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2022 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
22  *
23  * Host info utility functions
24  */
25 
26 #include "suricata-common.h"
27 #include "util-host-os-info.h"
28 #include "util-error.h"
29 #include "util-debug.h"
30 #include "util-ip.h"
31 #include "util-radix4-tree.h"
32 #include "util-radix6-tree.h"
33 #include "util-byte.h"
34 #include "stream-tcp-private.h"
35 #include "stream-tcp-reassemble.h"
36 
37 #include "conf.h"
38 #include "conf-yaml-loader.h"
39 
40 #include "util-enum.h"
41 #include "util-unittest.h"
42 
43 /** Enum map for the various OS flavours */
45  { "none", OS_POLICY_NONE },
46  { "bsd", OS_POLICY_BSD },
47  { "bsd-right", OS_POLICY_BSD_RIGHT },
48  { "old-linux", OS_POLICY_OLD_LINUX },
49  { "linux", OS_POLICY_LINUX },
50  { "old-solaris", OS_POLICY_OLD_SOLARIS },
51  { "solaris", OS_POLICY_SOLARIS },
52  { "hpux10", OS_POLICY_HPUX10 },
53  { "hpux11", OS_POLICY_HPUX11 },
54  { "irix", OS_POLICY_IRIX },
55  { "macos", OS_POLICY_MACOS },
56  { "windows", OS_POLICY_WINDOWS },
57  { "vista", OS_POLICY_VISTA },
58  { "windows2k3", OS_POLICY_WINDOWS2K3 },
59  { NULL, -1 },
60 };
61 
62 /** Radix trees that hold the host OS information */
63 static SCRadix4Tree sc_hinfo_tree4 = SC_RADIX4_TREE_INITIALIZER;
64 static SCRadix6Tree sc_hinfo_tree6 = SC_RADIX6_TREE_INITIALIZER;
65 
66 /**
67  * \brief Allocates the host_os flavour wrapped in user_data variable to be sent
68  * along with the key to the radix tree
69  *
70  * \param host_os Pointer to a character string containing the host_os flavour
71  *
72  * \retval user_data On success, pointer to the user_data that has to be sent
73  * along with the key, to be added to the Radix tree; NULL on
74  * failure
75  * \initonly
76  */
77 static void *SCHInfoAllocUserDataOSPolicy(const char *host_os)
78 {
79  /* the host os flavour that has to be sent as user data */
81  if (v == -1) {
82  SCLogError("Invalid enum map inside SCHInfoAddHostOSInfo()");
83  return NULL;
84  }
85 
86  int *user_data = SCMalloc(sizeof(int));
87  if (user_data == NULL) {
88  FatalError("Error allocating memory. Exiting");
89  }
90  *user_data = v;
91  return (void *)user_data;
92 }
93 
94 /**
95  * \brief Used to free the user data that is allocated by host_os_info API
96  *
97  * \param Pointer to the data that has to be freed
98  */
99 static void SCHInfoFreeUserDataOSPolicy(void *data)
100 {
101  if (data != NULL)
102  SCFree(data);
103 }
104 
105 static const SCRadix4Config ospolicy_radix4_config = { SCHInfoFreeUserDataOSPolicy, NULL };
106 static const SCRadix6Config ospolicy_radix6_config = { SCHInfoFreeUserDataOSPolicy, NULL };
107 
108 /**
109  * \brief Used to add the host-os-info data obtained from the conf
110  *
111  * \param host_os The host_os name/flavour from the conf file
112  * \param host_os_ip_range Pointer to a char string holding the ip/ip_netblock
113  * for the host_os specified in the first argument
114  * \param is_ipv4 Indicates if the ip address to be considered for the
115  * default configuration is IPV4; if not it is IPV6.
116  * Specified using SC_HINFO_IS_IPV6 or SC_HINFO_IS_IPV4
117  *
118  * \retval 0 On successfully adding the host os info to the Radix tree
119  * \retval -1 On failure
120  * \initonly (only specified from config, at the startup)
121  */
122 int SCHInfoAddHostOSInfo(const char *host_os, const char *host_os_ip_range, int is_ipv4)
123 {
124  char *ip_str = NULL;
125  char *ip_str_rem = NULL;
126  int *user_data = NULL;
127  bool recursive = false;
128 
129  if (host_os == NULL || host_os_ip_range == NULL ||
130  strlen(host_os_ip_range) == 0) {
131  SCLogError("Invalid arguments");
132  return -1;
133  }
134 
135  /* the host os flavour that has to be sent as user data */
136  if ( (user_data = SCHInfoAllocUserDataOSPolicy(host_os)) == NULL) {
137  SCLogError("Invalid enum map inside");
138  return -1;
139  }
140 
141  /* if we have a default configuration set the appropriate values for the
142  * netblocks */
143  if ( (strcasecmp(host_os_ip_range, "default")) == 0) {
144  if (is_ipv4)
145  host_os_ip_range = "0.0.0.0/0";
146  else
147  host_os_ip_range = "::/0";
148  }
149 
150  if ( (ip_str = SCStrdup(host_os_ip_range)) == NULL) {
151  FatalError("Error allocating memory");
152  }
153 
154  /* check if we have more addresses in the host_os_ip_range */
155  if ((ip_str_rem = strchr(ip_str, ',')) != NULL) {
156  ip_str_rem[0] = '\0';
157  ip_str_rem++;
158  recursive = true;
159  }
160 
161  if (strchr(ip_str, ':') == NULL) {
163  &sc_hinfo_tree4, &ospolicy_radix4_config, ip_str, user_data)) {
164  if (sc_errno == SC_EEXIST) {
165  SCLogError("duplicate ipv4 address '%s'", ip_str);
166  } else {
167  SCLogError("invalid ipv4 address '%s'", ip_str);
168  }
169  SCHInfoFreeUserDataOSPolicy(user_data);
170  SCFree(ip_str);
171  return -1;
172  }
173  } else {
175  &sc_hinfo_tree6, &ospolicy_radix6_config, ip_str, user_data)) {
176  if (sc_errno == SC_EEXIST) {
177  SCLogError("duplicate ipv6 address '%s'", ip_str);
178  } else {
179  SCLogError("invalid ipv6 address '%s'", ip_str);
180  }
181  SCHInfoFreeUserDataOSPolicy(user_data);
182  SCFree(ip_str);
183  return -1;
184  }
185  }
186 
187  if (recursive) {
188  SCHInfoAddHostOSInfo(host_os, ip_str_rem, is_ipv4);
189  }
190 
191  SCFree(ip_str);
192  return *user_data;
193 }
194 
195 #ifdef UNITTESTS
196 /**
197  * \brief Retrieves the host os flavour, given an ipv4/ipv6 address as a string.
198  *
199  * \param Pointer to a string containing an IP address
200  *
201  * \retval The OS flavour on success; -1 on failure, or on not finding the key
202  */
203 static int SCHInfoGetHostOSFlavour(const char *str)
204 {
205  void *user_data = NULL;
206 
207  if (str == NULL || strchr(str, '/') != NULL)
208  return -1;
209 
210  if (strchr(str, ':') != NULL) {
211  struct in6_addr addr;
212  if (inet_pton(AF_INET6, str, &addr) <= 0) {
213  SCLogError("Invalid IPV6 address");
214  return -1;
215  }
216  (void)SCRadix6TreeFindBestMatch(&sc_hinfo_tree6, (uint8_t *)&addr.s6_addr, &user_data);
217  if (user_data == NULL)
218  return -1;
219  else
220  return *((int *)user_data);
221  } else {
222  struct in_addr addr;
223  if (inet_pton(AF_INET, str, &addr) <= 0) {
224  SCLogError("Invalid IPV4 address");
225  return -1;
226  }
227  (void)SCRadix4TreeFindBestMatch(&sc_hinfo_tree4, (uint8_t *)&addr.s_addr, &user_data);
228  if (user_data == NULL)
229  return -1;
230  else
231  return *((int *)user_data);
232  }
233 }
234 #endif
235 
236 /**
237  * \brief Retrieves the host os flavour, given an ipv4 address in the raw
238  * address format.
239  *
240  * \param Pointer to a raw ipv4 address.
241  *
242  * \retval The OS flavour on success; -1 on failure, or on not finding the key
243  */
244 int SCHInfoGetIPv4HostOSFlavour(uint8_t *addr)
245 {
246  void *user_data = NULL;
247  (void)SCRadix4TreeFindBestMatch(&sc_hinfo_tree4, addr, &user_data);
248  if (user_data == NULL)
249  return -1;
250  return *((int *)user_data);
251 }
252 
253 /**
254  * \brief Retrieves the host os flavour, given an ipv6 address in the raw
255  * address format.
256  *
257  * \param Pointer to a raw ipv6 address.
258  *
259  * \retval The OS flavour on success; -1 on failure, or on not finding the key
260  */
261 int SCHInfoGetIPv6HostOSFlavour(uint8_t *addr)
262 {
263  void *user_data = NULL;
264  (void)SCRadix6TreeFindBestMatch(&sc_hinfo_tree6, addr, &user_data);
265  if (user_data == NULL)
266  return -1;
267  return *((int *)user_data);
268 }
269 
271 {
272  SCRadix4TreeRelease(&sc_hinfo_tree4, &ospolicy_radix4_config);
273  SCRadix6TreeRelease(&sc_hinfo_tree6, &ospolicy_radix6_config);
274 }
275 
276 /**
277  * \brief Load the host os policy information from the configuration.
278  *
279  * \initonly (A mem alloc error should cause an exit failure)
280  */
282 {
283  ConfNode *root = ConfGetNode("host-os-policy");
284  if (root == NULL)
285  return;
286 
287  ConfNode *policy;
288  TAILQ_FOREACH(policy, &root->head, next) {
289  ConfNode *host;
290  TAILQ_FOREACH(host, &policy->head, next) {
291  int is_ipv4 = 1;
292  if (host->val != NULL && strchr(host->val, ':') != NULL)
293  is_ipv4 = 0;
294  if (SCHInfoAddHostOSInfo(policy->name, host->val, is_ipv4) == -1) {
295  FatalError("Failed to add host \"%s\" with policy \"%s\" to host "
296  "info database",
297  host->val, policy->name);
298  }
299  }
300  }
301 }
302 
303 /*------------------------------------Unit_Tests------------------------------*/
304 
305 #ifdef UNITTESTS
306 static SCRadix4Tree sc_hinfo_tree_backup4 = SC_RADIX4_TREE_INITIALIZER;
307 static SCRadix6Tree sc_hinfo_tree_backup6 = SC_RADIX6_TREE_INITIALIZER;
308 
309 static void SCHInfoCreateContextBackup(void)
310 {
311  sc_hinfo_tree_backup4 = sc_hinfo_tree4;
312  sc_hinfo_tree4 = SCRadix4TreeInitialize();
313  sc_hinfo_tree_backup6 = sc_hinfo_tree6;
314  sc_hinfo_tree6 = SCRadix6TreeInitialize();
315 }
316 
317 static void SCHInfoRestoreContextBackup(void)
318 {
319  sc_hinfo_tree4 = sc_hinfo_tree_backup4;
320  sc_hinfo_tree_backup4 = SCRadix4TreeInitialize();
321  sc_hinfo_tree6 = sc_hinfo_tree_backup6;
322  sc_hinfo_tree_backup6 = SCRadix6TreeInitialize();
323 }
324 
325 /**
326  * \test Check if we the IPs with the right OS flavours are added to the host OS
327  * radix tree, and the IPS with invalid flavours returns an error(-1)
328  */
329 static int SCHInfoTestInvalidOSFlavour01(void)
330 {
331  SCHInfoCreateContextBackup();
332 
333  FAIL_IF(SCHInfoAddHostOSInfo("bamboo", "192.168.1.1", SC_HINFO_IS_IPV4) != -1);
334  FAIL_IF(SCHInfoAddHostOSInfo("linux", "192.168.1.2", SC_HINFO_IS_IPV4) !=
336  FAIL_IF(SCHInfoAddHostOSInfo("windows", "192.168.1.3", SC_HINFO_IS_IPV4) !=
338  FAIL_IF(SCHInfoAddHostOSInfo("solaris", "192.168.1.4", SC_HINFO_IS_IPV4) !=
340  FAIL_IF(SCHInfoAddHostOSInfo("hpux10", "192.168.1.5", SC_HINFO_IS_IPV4) !=
342  FAIL_IF(SCHInfoAddHostOSInfo("hpux11", "192.168.1.6", SC_HINFO_IS_IPV4) !=
344  FAIL_IF(SCHInfoAddHostOSInfo("irix", "192.168.1.7", SC_HINFO_IS_IPV4) !=
346  FAIL_IF(SCHInfoAddHostOSInfo("bsd", "192.168.1.8", SC_HINFO_IS_IPV4) !=
348  FAIL_IF(SCHInfoAddHostOSInfo("old_linux", "192.168.1.9", SC_HINFO_IS_IPV4) !=
350  FAIL_IF(SCHInfoAddHostOSInfo("macos", "192.168.1.10", SC_HINFO_IS_IPV4) !=
352  FAIL_IF(SCHInfoAddHostOSInfo("vista", "192.168.1.11", SC_HINFO_IS_IPV4) !=
354  FAIL_IF(SCHInfoAddHostOSInfo("windows2k3", "192.168.1.12", SC_HINFO_IS_IPV4) !=
356 
358  SCHInfoRestoreContextBackup();
359 
360  PASS;
361 }
362 
363 /**
364  * \test Check that invalid ipv4 addresses and ipv4 netblocks are rejected by
365  * the host os info API
366  */
367 static int SCHInfoTestInvalidIPV4Address02(void)
368 {
369  SCHInfoCreateContextBackup();
370 
371  int result = 0;
372 
373  if (SCHInfoAddHostOSInfo("linux", "192.168.1.566", SC_HINFO_IS_IPV4) != -1) {
374  goto end;
375  }
376  if (SCHInfoAddHostOSInfo("linux", "192.168.1", SC_HINFO_IS_IPV4) != -1) {
377  goto end;
378  }
379  if (SCHInfoAddHostOSInfo("linux", "192.", SC_HINFO_IS_IPV4) != -1) {
380  goto end;
381  }
382  if (SCHInfoAddHostOSInfo("linux", "192.168", SC_HINFO_IS_IPV4) != -1) {
383  goto end;
384  }
385  if (SCHInfoAddHostOSInfo("linux", "", SC_HINFO_IS_IPV4) != -1) {
386  goto end;
387  }
388  if (SCHInfoAddHostOSInfo("linux", "192.168.1.1/33", SC_HINFO_IS_IPV4) != -1) {
389  goto end;
390  }
391 
392  result = 1;
393 
394  end:
396  SCHInfoRestoreContextBackup();
397 
398  return result;
399 }
400 
401 /**
402  * \test Check that invalid ipv4 addresses and ipv4 netblocks are rejected by
403  * the host os info API
404  */
405 static int SCHInfoTestInvalidIPV6Address03(void)
406 {
407  SCHInfoCreateContextBackup();
408 
409  int result = 0;
410 
411  if (SCHInfoAddHostOSInfo("linux", "2362:7322", SC_HINFO_IS_IPV6) != -1) {
412  goto end;
413  }
414  if (SCHInfoAddHostOSInfo("linux", "19YW:", SC_HINFO_IS_IPV6) != -1) {
415  goto end;
416  }
417  if (SCHInfoAddHostOSInfo("linux", "1235", SC_HINFO_IS_IPV6) != -1) {
418  goto end;
419  }
420  if (SCHInfoAddHostOSInfo("linux", "1922:236115:", SC_HINFO_IS_IPV6) != -1) {
421  goto end;
422  }
423  if (SCHInfoAddHostOSInfo("linux", "", SC_HINFO_IS_IPV6) != -1) {
424  goto end;
425  }
426  if (SCHInfoAddHostOSInfo("linux",
427  "1921.6311:6241:6422:7352:ABBB:DDDD:EEEE/129",
428  SC_HINFO_IS_IPV6) != -1) {
429  goto end;
430  }
431 
432  result = 1;
433 
434  end:
436  SCHInfoRestoreContextBackup();
437 
438  return result;
439 }
440 
441 /**
442  * \test Check that valid ipv4 addresses are inserted into the host os radix
443  * tree, and the host os api retrieves the right value for the host os
444  * flavour, on supplying as arg an ipv4 addresses that has been added to
445  * the host os radix tree.
446  */
447 static int SCHInfoTestValidIPV4Address04(void)
448 {
449  SCHInfoCreateContextBackup();
450 
451  int result = 0;
452 
453  if (SCHInfoAddHostOSInfo("linux", "192.168.1.1", SC_HINFO_IS_IPV4) == -1) {
454  goto end;
455  }
456  if (SCHInfoAddHostOSInfo("windows", "192.192.1.2", SC_HINFO_IS_IPV4) == -1) {
457  goto end;
458  }
459  if (SCHInfoAddHostOSInfo("solaris", "192.168.1.100", SC_HINFO_IS_IPV4) == -1) {
460  goto end;
461  }
462  if (SCHInfoAddHostOSInfo("hpux10", "192.168.2.4", SC_HINFO_IS_IPV4) == -1) {
463  goto end;
464  }
465  if (SCHInfoAddHostOSInfo("linux", "192.192.1.5", SC_HINFO_IS_IPV4) == -1) {
466  goto end;
467  }
468  if (SCHInfoAddHostOSInfo("vista", "192.168.10.20", SC_HINFO_IS_IPV4) == -1) {
469  goto end;
470  }
471  if (SCHInfoAddHostOSInfo("solaris", "111.163.151.62", SC_HINFO_IS_IPV4) == -1) {
472  goto end;
473  }
474  if (SCHInfoAddHostOSInfo("solaris", "11.1.120.210", SC_HINFO_IS_IPV4) == -1) {
475  goto end;
476  }
477  if (SCHInfoAddHostOSInfo("linux", "19.18.110.210", SC_HINFO_IS_IPV4) == -1) {
478  goto end;
479  }
480  if (SCHInfoAddHostOSInfo("windows", "19.18.120.110", SC_HINFO_IS_IPV4) == -1) {
481  goto end;
482  }
483  if (SCHInfoAddHostOSInfo("hpux11", "191.168.11.128", SC_HINFO_IS_IPV4) == -1) {
484  goto end;
485  }
486  if (SCHInfoAddHostOSInfo("vista", "191.168.11.192", SC_HINFO_IS_IPV4) == -1) {
487  goto end;
488  }
489 
490  if (SCHInfoGetHostOSFlavour("192.168.1.1") !=
492  goto end;
493  }
494  if (SCHInfoGetHostOSFlavour("192.168.1.2") != -1) {
495  goto end;
496  }
497  if (SCHInfoGetHostOSFlavour("192.168.1.100") !=
499  goto end;
500  }
501  if (SCHInfoGetHostOSFlavour("192.192.2.4") != -1) {
502  goto end;
503  }
504  if (SCHInfoGetHostOSFlavour("192.168.2.4") !=
506  goto end;
507  }
508  if (SCHInfoGetHostOSFlavour("192.192.1.5") !=
510  goto end;
511  }
512  if (SCHInfoGetHostOSFlavour("192.168.10.20") !=
514  goto end;
515  }
516  if (SCHInfoGetHostOSFlavour("111.163.151.62") !=
518  goto end;
519  }
520  if (SCHInfoGetHostOSFlavour("11.1.120.210") !=
522  goto end;
523  }
524  if (SCHInfoGetHostOSFlavour("19.18.110.210") !=
526  goto end;
527  }
528  if (SCHInfoGetHostOSFlavour("19.18.120.110") !=
530  goto end;
531  }
532  if (SCHInfoGetHostOSFlavour("191.168.11.128") !=
534  goto end;
535  }
536  if (SCHInfoGetHostOSFlavour("191.168.11.192") !=
538  goto end;
539  }
540  if (SCHInfoGetHostOSFlavour("191.168.11.224") != -1) {
541  goto end;
542  }
543 
544  result = 1;
545 
546  end:
548  SCHInfoRestoreContextBackup();
549 
550  return result;
551 }
552 
553 /**
554  * \test Check that valid ipv4 addresses/netblocks are inserted into the host os
555  * radix tree, and the host os api retrieves the right value for the host
556  * os flavour, on supplying as arg an ipv4 addresses that has been added
557  * to the host os radix tree.
558  */
559 static int SCHInfoTestValidIPV4Address05(void)
560 {
561  SCHInfoCreateContextBackup();
562 
563  struct in_addr in;
564  int result = 0;
565 
566  if (SCHInfoAddHostOSInfo("linux", "192.168.1.1", SC_HINFO_IS_IPV4) == -1) {
567  goto end;
568  }
569  if (SCHInfoAddHostOSInfo("windows", "192.192.1.2", SC_HINFO_IS_IPV4) == -1) {
570  goto end;
571  }
572  if (SCHInfoAddHostOSInfo("solaris", "192.168.1.100", SC_HINFO_IS_IPV4) == -1) {
573  goto end;
574  }
575  if (SCHInfoAddHostOSInfo("hpux10", "192.168.2.4", SC_HINFO_IS_IPV4) == -1) {
576  goto end;
577  }
578  if (SCHInfoAddHostOSInfo("linux", "192.192.1.5", SC_HINFO_IS_IPV4) == -1) {
579  goto end;
580  }
581  if (SCHInfoAddHostOSInfo("vista", "192.168.10.20", SC_HINFO_IS_IPV4) == -1) {
582  goto end;
583  }
584  if (SCHInfoAddHostOSInfo("solaris", "111.163.151.62", SC_HINFO_IS_IPV4) == -1) {
585  goto end;
586  }
587  if (SCHInfoAddHostOSInfo("hpux11", "111.162.208.124/20", SC_HINFO_IS_IPV4) == -1) {
588  goto end;
589  }
590  if (SCHInfoAddHostOSInfo("windows", "111.162.240.1", SC_HINFO_IS_IPV4) == -1) {
591  goto end;
592  }
593  if (SCHInfoAddHostOSInfo("solaris", "111.162.214.100", SC_HINFO_IS_IPV4) == -1) {
594  goto end;
595  }
596  if (SCHInfoAddHostOSInfo("vista", "111.162.208.100", SC_HINFO_IS_IPV4) == -1) {
597  goto end;
598  }
599  if (SCHInfoAddHostOSInfo("linux", "111.162.194.112", SC_HINFO_IS_IPV4) == -1) {
600  goto end;
601  }
602 
603  if (SCHInfoGetHostOSFlavour("192.168.1.1") !=
605  goto end;
606  }
607  if (SCHInfoGetHostOSFlavour("192.168.1.2") != -1) {
608  goto end;
609  }
610  if (SCHInfoGetHostOSFlavour("192.168.1.100") !=
612  goto end;
613  }
614  if (SCHInfoGetHostOSFlavour("192.192.2.4") != -1) {
615  goto end;
616  }
617  if (SCHInfoGetHostOSFlavour("192.168.2.4") !=
619  goto end;
620  }
621  if (SCHInfoGetHostOSFlavour("192.192.1.5") !=
623  goto end;
624  }
625  if (SCHInfoGetHostOSFlavour("192.168.10.20") !=
627  goto end;
628  }
629  if (SCHInfoGetHostOSFlavour("111.163.151.62") !=
631  goto end;
632  }
633  if (SCHInfoGetHostOSFlavour("111.162.208.0") !=
635  goto end;
636  }
637  if (SCHInfoGetHostOSFlavour("111.162.210.1") !=
639  goto end;
640  }
641  if (SCHInfoGetHostOSFlavour("111.162.214.1") !=
643  goto end;
644  }
645  if (SCHInfoGetHostOSFlavour("111.162.0.0") != -1) {
646  goto end;
647  }
648  if (SCHInfoGetHostOSFlavour("111.162.240.112") != -1) {
649  goto end;
650  }
651  if (SCHInfoGetHostOSFlavour("111.162.240.1") !=
653  goto end;
654  }
655  if (SCHInfoGetHostOSFlavour("111.162.214.100") !=
657  goto end;
658  }
659  if (inet_pton(AF_INET, "111.162.208.100", &in) < 0) {
660  goto end;
661  }
662  if (SCHInfoGetIPv4HostOSFlavour((uint8_t *)&in) !=
664  goto end;
665  }
666  if (SCHInfoGetHostOSFlavour("111.162.194.112") !=
668  goto end;
669  }
670  if (SCHInfoGetHostOSFlavour("111.162.208.200") !=
672  goto end;
673  }
674  if (inet_pton(AF_INET, "111.162.208.200", &in) < 0) {
675  goto end;
676  }
677  if (SCHInfoGetIPv4HostOSFlavour((uint8_t *)&in) !=
679  goto end;
680  }
681  if (SCHInfoGetHostOSFlavour("111.162.200.201") != -1) {
682  goto end;
683  }
684 
685  result = 1;
686 
687  end:
689  SCHInfoRestoreContextBackup();
690 
691  return result;
692 }
693 
694 /**
695  * \test Check that valid ipv6 addresses are inserted into the host os radix
696  * tree, and the host os api retrieves the right value for the host os
697  * flavour, on supplying as arg an ipv6 address that has been added to
698  * the host os radix tree.
699  */
700 static int SCHInfoTestValidIPV6Address06(void)
701 {
702  SCHInfoCreateContextBackup();
703 
704  int result = 0;
705 
706  if (SCHInfoAddHostOSInfo("linux",
707  "2351:2512:6211:6246:235A:6242:2352:62AD",
708  SC_HINFO_IS_IPV6) == -1) {
709  goto end;
710  }
711  if (SCHInfoAddHostOSInfo("windows",
712  "6961:6121:2132:6241:423A:2135:2461:621D",
713  SC_HINFO_IS_IPV6) == -1) {
714  goto end;
715  }
716  if (SCHInfoAddHostOSInfo("solaris",
717  "DD13:613D:F312:62DD:6213:421A:6212:2652",
718  SC_HINFO_IS_IPV6) == -1) {
719  goto end;
720  }
721  if (SCHInfoAddHostOSInfo("hpux10",
722  "9891:2131:2151:6426:1342:674D:622F:2342",
723  SC_HINFO_IS_IPV6) == -1) {
724  goto end;
725  }
726  if (SCHInfoAddHostOSInfo("linux",
727  "3525:2351:4223:6211:2311:2667:6242:2154",
728  SC_HINFO_IS_IPV6) == -1) {
729  goto end;
730  }
731  if (SCHInfoAddHostOSInfo("vista",
732  "1511:6211:6726:7777:1212:2333:6222:7722",
733  SC_HINFO_IS_IPV6) == -1) {
734  goto end;
735  }
736  if (SCHInfoAddHostOSInfo("solaris",
737  "2666:6222:7222:2335:6223:7722:3425:2362",
738  SC_HINFO_IS_IPV6) == -1) {
739  goto end;
740  }
741  if (SCHInfoAddHostOSInfo("solaris",
742  "8762:2352:6241:7245:EE23:21AD:2312:622C",
743  SC_HINFO_IS_IPV6) == -1) {
744  goto end;
745  }
746  if (SCHInfoAddHostOSInfo("linux",
747  "6422:EE1A:2621:34AD:2462:432D:642E:E13A",
748  SC_HINFO_IS_IPV6) == -1) {
749  goto end;
750  }
751  if (SCHInfoAddHostOSInfo("windows",
752  "3521:7622:6241:6242:7277:1234:2352:6234",
753  SC_HINFO_IS_IPV6) == -1) {
754  goto end;
755  }
756  if (SCHInfoAddHostOSInfo("hpux11",
757  "2141:6232:6252:2223:7734:2345:6245:6222",
758  SC_HINFO_IS_IPV6) == -1) {
759  goto end;
760  }
761  if (SCHInfoAddHostOSInfo("vista",
762  "5222:6432:6432:2322:6662:3423:4322:3245",
763  SC_HINFO_IS_IPV6) == -1) {
764  goto end;
765  }
766 
767  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:62AD") !=
769  goto end;
770  }
771  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:6FFFE") != -1) {
772  goto end;
773  }
774  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2652") !=
776  goto end;
777  }
778  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2222") != -1) {
779  goto end;
780  }
781  if (SCHInfoGetHostOSFlavour("9891:2131:2151:6426:1342:674D:622F:2342") !=
783  goto end;
784  }
785  if (SCHInfoGetHostOSFlavour("3525:2351:4223:6211:2311:2667:6242:2154") !=
787  goto end;
788  }
789  if (SCHInfoGetHostOSFlavour("1511:6211:6726:7777:1212:2333:6222:7722") !=
791  goto end;
792  }
793  if (SCHInfoGetHostOSFlavour("2666:6222:7222:2335:6223:7722:3425:2362") !=
795  goto end;
796  }
797  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:622C") !=
799  goto end;
800  }
801  if (SCHInfoGetHostOSFlavour("6422:EE1A:2621:34AD:2462:432D:642E:E13A") !=
803  goto end;
804  }
805  if (SCHInfoGetHostOSFlavour("3521:7622:6241:6242:7277:1234:2352:6234") !=
807  goto end;
808  }
809  if (SCHInfoGetHostOSFlavour("2141:6232:6252:2223:7734:2345:6245:6222") !=
811  goto end;
812  }
813  if (SCHInfoGetHostOSFlavour("5222:6432:6432:2322:6662:3423:4322:3245") !=
815  goto end;
816  }
817  if (SCHInfoGetHostOSFlavour("5222:6432:6432:2322:6662:3423:4322:DDDD") != -1) {
818  goto end;
819  }
820 
821  result = 1;
822 
823  end:
825  SCHInfoRestoreContextBackup();
826 
827  return result;
828 }
829 
830 /**
831  * \test Check that valid ipv6 addresses/netblocks are inserted into the host os
832  * radix tree, and the host os api retrieves the right value for the host
833  * os flavour, on supplying as arg an ipv6 address that has been added to
834  * the host os radix tree.
835  */
836 static int SCHInfoTestValidIPV6Address07(void)
837 {
838  SCHInfoCreateContextBackup();
839 
840  int result = 0;
841 
842  if (SCHInfoAddHostOSInfo("linux",
843  "2351:2512:6211:6246:235A:6242:2352:62AD",
844  SC_HINFO_IS_IPV6) == -1) {
845  goto end;
846  }
847  if (SCHInfoAddHostOSInfo("windows",
848  "6961:6121:2132:6241:423A:2135:2461:621D",
849  SC_HINFO_IS_IPV6) == -1) {
850  goto end;
851  }
852  if (SCHInfoAddHostOSInfo("solaris",
853  "DD13:613D:F312:62DD:6213:421A:6212:2652",
854  SC_HINFO_IS_IPV6) == -1) {
855  goto end;
856  }
857  if (SCHInfoAddHostOSInfo("hpux10",
858  "9891:2131:2151:6426:1342:674D:622F:2342",
859  SC_HINFO_IS_IPV6) == -1) {
860  goto end;
861  }
862  if (SCHInfoAddHostOSInfo("linux",
863  "3525:2351:4223:6211:2311:2667:6242:2154",
864  SC_HINFO_IS_IPV6) == -1) {
865  goto end;
866  }
867  if (SCHInfoAddHostOSInfo("vista",
868  "1511:6211:6726:7777:1212:2333:6222:7722",
869  SC_HINFO_IS_IPV6) == -1) {
870  goto end;
871  }
872  if (SCHInfoAddHostOSInfo("solaris",
873  "2666:6222:7222:2335:6223:7722:3425:2362",
874  SC_HINFO_IS_IPV6) == -1) {
875  goto end;
876  }
877  if (SCHInfoAddHostOSInfo("solaris",
878  "8762:2352:6241:7245:EE23:21AD:2312:622C/68",
879  SC_HINFO_IS_IPV6) == -1) {
880  goto end;
881  }
882  if (SCHInfoAddHostOSInfo("linux",
883  "8762:2352:6241:7245:EE23:21AD:2412:622C",
884  SC_HINFO_IS_IPV6) == -1) {
885  goto end;
886  }
887  if (SCHInfoAddHostOSInfo("windows",
888  "8762:2352:6241:7245:EE23:21AD:FFFF:622C",
889  SC_HINFO_IS_IPV6) == -1) {
890  goto end;
891  }
892  if (SCHInfoAddHostOSInfo("hpux11",
893  "8762:2352:6241:7245:EE23:21AD:2312:62FF",
894  SC_HINFO_IS_IPV6) == -1) {
895  goto end;
896  }
897  if (SCHInfoAddHostOSInfo("vista",
898  "8762:2352:6241:7245:EE23:21AD:2121:1212",
899  SC_HINFO_IS_IPV6) == -1) {
900  goto end;
901  }
902 
903  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:62AD") !=
905  goto end;
906  }
907  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:6FFFE") != -1) {
908  goto end;
909  }
910  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2652") !=
912  goto end;
913  }
914  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2222") != -1) {
915  goto end;
916  }
917  if (SCHInfoGetHostOSFlavour("9891:2131:2151:6426:1342:674D:622F:2342") !=
919  goto end;
920  }
921  if (SCHInfoGetHostOSFlavour("3525:2351:4223:6211:2311:2667:6242:2154") !=
923  goto end;
924  }
925  if (SCHInfoGetHostOSFlavour("1511:6211:6726:7777:1212:2333:6222:7722") !=
927  goto end;
928  }
929  if (SCHInfoGetHostOSFlavour("2666:6222:7222:2335:6223:7722:3425:2362") !=
931  goto end;
932  }
933  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:622C") !=
935  goto end;
936  }
937  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2412:622C") !=
939  goto end;
940  }
941  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:FFFF:622C") !=
943  goto end;
944  }
945  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:62FF") !=
947  goto end;
948  }
949  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2121:1212") !=
951  goto end;
952  }
953  if (SCHInfoGetHostOSFlavour("5222:6432:6432:2322:6662:3423:4322:DDDD") != -1) {
954  goto end;
955  }
956  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2121:1DDD") !=
958  goto end;
959  }
960  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:FFFF:2121:1DDD") !=
962  goto end;
963  }
964  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:622C") !=
966  goto end;
967  }
968  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE00:0000:0000:0000") !=
970  goto end;
971  }
972  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:E000:0000:0000:0000") !=
974  goto end;
975  }
976 
977  result = 1;
978 
979  end:
981  SCHInfoRestoreContextBackup();
982 
983  return result;
984 }
985 
986 /**
987  * \test Check that valid ipv6 addresses/netblocks are inserted into the host os
988  * radix tree, and the host os api retrieves the right value for the host
989  * os flavour, on supplying as arg an ipv6 address that has been added to
990  * the host os radix tree.
991  */
992 static int SCHInfoTestValidIPV6Address08(void)
993 {
994  SCHInfoCreateContextBackup();
995 
996  struct in6_addr in6;
997  int result = 0;
998 
999  if (SCHInfoAddHostOSInfo("linux",
1000  "2351:2512:6211:6246:235A:6242:2352:62AD",
1001  SC_HINFO_IS_IPV6) == -1) {
1002  goto end;
1003  }
1004  if (SCHInfoAddHostOSInfo("windows",
1005  "6961:6121:2132:6241:423A:2135:2461:621D",
1006  SC_HINFO_IS_IPV6) == -1) {
1007  goto end;
1008  }
1009  if (SCHInfoAddHostOSInfo("solaris",
1010  "DD13:613D:F312:62DD:6213:421A:6212:2652",
1011  SC_HINFO_IS_IPV6) == -1) {
1012  goto end;
1013  }
1014  if (SCHInfoAddHostOSInfo("hpux10",
1015  "9891:2131:2151:6426:1342:674D:622F:2342",
1016  SC_HINFO_IS_IPV6) == -1) {
1017  goto end;
1018  }
1019  if (SCHInfoAddHostOSInfo("linux",
1020  "3525:2351:4223:6211:2311:2667:6242:2154",
1021  SC_HINFO_IS_IPV6) == -1) {
1022  goto end;
1023  }
1024  if (SCHInfoAddHostOSInfo("vista",
1025  "1511:6211:6726:7777:1212:2333:6222:7722",
1026  SC_HINFO_IS_IPV6) == -1) {
1027  goto end;
1028  }
1029  if (SCHInfoAddHostOSInfo("solaris",
1030  "2666:6222:7222:2335:6223:7722:3425:2362",
1031  SC_HINFO_IS_IPV6) == -1) {
1032  goto end;
1033  }
1034  if (SCHInfoAddHostOSInfo("solaris",
1035  "8762:2352:6241:7245:EE23:21AD:2312:622C/68",
1036  SC_HINFO_IS_IPV6) == -1) {
1037  goto end;
1038  }
1039  if (SCHInfoAddHostOSInfo("linux",
1040  "8762:2352:6241:7245:EE23:21AD:2412:622C",
1041  SC_HINFO_IS_IPV6) == -1) {
1042  goto end;
1043  }
1044  if (SCHInfoAddHostOSInfo("windows",
1045  "8762:2352:6241:7245:EE23:21AD:FFFF:622C",
1046  SC_HINFO_IS_IPV6) == -1) {
1047  goto end;
1048  }
1049  if (SCHInfoAddHostOSInfo("hpux11",
1050  "8762:2352:6241:7245:EE23:21AD:2312:62FF",
1051  SC_HINFO_IS_IPV6) == -1) {
1052  goto end;
1053  }
1054  if (SCHInfoAddHostOSInfo("vista",
1055  "8762:2352:6241:7245:EE23:21AD:2121:1212",
1056  SC_HINFO_IS_IPV6) == -1) {
1057  goto end;
1058  }
1059  if (SCHInfoAddHostOSInfo("vista", "8.8.8.0/24", SC_HINFO_IS_IPV4) == -1) {
1060  goto end;
1061  }
1062  if (SCHInfoAddHostOSInfo("irix", "default", SC_HINFO_IS_IPV6) == -1) {
1063  goto end;
1064  }
1065 
1066  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:62AD") !=
1068  goto end;
1069  }
1070  if (SCHInfoGetHostOSFlavour("2351:2512:6211:6246:235A:6242:2352:6FFF") !=
1072  goto end;
1073  }
1074  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2652") !=
1076  goto end;
1077  }
1078  if (SCHInfoGetHostOSFlavour("DD13:613D:F312:62DD:6213:421A:6212:2222") !=
1080  goto end;
1081  }
1082  if (SCHInfoGetHostOSFlavour("9891:2131:2151:6426:1342:674D:622F:2342") !=
1084  goto end;
1085  }
1086  if (SCHInfoGetHostOSFlavour("3525:2351:4223:6211:2311:2667:6242:2154") !=
1088  goto end;
1089  }
1090  if (SCHInfoGetHostOSFlavour("1511:6211:6726:7777:1212:2333:6222:7722") !=
1092  goto end;
1093  }
1094  if (SCHInfoGetHostOSFlavour("2666:6222:7222:2335:6223:7722:3425:2362") !=
1096  goto end;
1097  }
1098  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:622C") !=
1100  goto end;
1101  }
1102  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2412:622C") !=
1104  goto end;
1105  }
1106  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:FFFF:622C") !=
1108  goto end;
1109  }
1110  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:62FF") !=
1112  goto end;
1113  }
1114  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2121:1212") !=
1116  goto end;
1117  }
1118  if (SCHInfoGetHostOSFlavour("5222:6432:6432:2322:6662:3423:4322:DDDD") !=
1120  goto end;
1121  }
1122  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2121:1DDD") !=
1124  goto end;
1125  }
1126  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:FFFF:2121:1DDD") !=
1128  goto end;
1129  }
1130  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE23:21AD:2312:622C") !=
1132  goto end;
1133  }
1134  if (SCHInfoGetHostOSFlavour("8762:2352:6241:7245:EE00:0000:0000:0000") !=
1136  goto end;
1137  }
1138  if (inet_pton(AF_INET6, "8762:2352:6241:7245:E000:0000:0000:0000", &in6) < 0) {
1139  goto end;
1140  }
1141  if (SCHInfoGetIPv6HostOSFlavour((uint8_t *)&in6) !=
1143  goto end;
1144  }
1145  if (inet_pton(AF_INET6, "AD23:2DDA:6D1D:A223:E235:0232:1241:1666", &in6) < 0) {
1146  goto end;
1147  }
1148  if (SCHInfoGetIPv6HostOSFlavour((uint8_t *)&in6) !=
1150  goto end;
1151  }
1152  if (SCHInfoGetHostOSFlavour("8.8.8.8") !=
1154  goto end;
1155  }
1156  result = 1;
1157 
1158  end:
1160  SCHInfoRestoreContextBackup();
1161 
1162  return result;
1163 }
1164 
1165 /**
1166  * \test Check that valid ipv4 addresses are inserted into the host os radix
1167  * tree, and the host os api retrieves the right value for the host os
1168  * flavour, on supplying as arg an ipv4 addresses that has been added to
1169  * the host os radix tree.
1170  */
1171 static int SCHInfoTestValidIPV4Address09(void)
1172 {
1173  SCHInfoCreateContextBackup();
1174 
1175  FAIL_IF(SCHInfoAddHostOSInfo("linux", "192.168.1.0", SC_HINFO_IS_IPV4) == -1);
1176  FAIL_IF(SCHInfoAddHostOSInfo("windows", "192.192.1.2", SC_HINFO_IS_IPV4) == -1);
1177  FAIL_IF(SCHInfoGetHostOSFlavour("192.168.1.0") !=
1179  FAIL_IF(SCHInfoAddHostOSInfo("solaris", "192.168.1.0/16", SC_HINFO_IS_IPV4) == -1);
1180  FAIL_IF(SCHInfoAddHostOSInfo("macos", "192.168.1.0/20", SC_HINFO_IS_IPV4) == -1);
1181  FAIL_IF(SCHInfoGetHostOSFlavour("192.168.1.0") !=
1183  FAIL_IF(SCHInfoAddHostOSInfo("vista", "192.168.50.128/25", SC_HINFO_IS_IPV4) == -1);
1184  FAIL_IF(SCHInfoGetHostOSFlavour("192.168.50.128") !=
1186  FAIL_IF(SCHInfoAddHostOSInfo("irix", "192.168.50.128", SC_HINFO_IS_IPV4) == -1);
1187  FAIL_IF(SCHInfoGetHostOSFlavour("192.168.50.128") !=
1189  FAIL_IF(SCHInfoGetHostOSFlavour("192.168.1.100") !=
1191 
1193  SCHInfoRestoreContextBackup();
1194  PASS;
1195 }
1196 
1197 /**
1198  * \test Check the loading of host info from a configuration file.
1199  */
1200 static int SCHInfoTestLoadFromConfig01(void)
1201 {
1202  char config[] = "\
1203 %YAML 1.1\n\
1204 ---\n\
1205 host-os-policy:\n\
1206  bsd: [0.0.0.0/0]\n\
1207  windows: [10.0.0.0/8, 192.168.1.0/24]\n\
1208  linux: [10.0.0.5/32]\n\
1209 \n";
1210 
1211  int result = 0;
1212 
1213  SCHInfoCreateContextBackup();
1214 
1216  ConfInit();
1217  ConfYamlLoadString(config, strlen(config));
1218 
1220  if (SCHInfoGetHostOSFlavour("10.0.0.4") != OS_POLICY_WINDOWS)
1221  goto end;
1222  if (SCHInfoGetHostOSFlavour("10.0.0.5") != OS_POLICY_LINUX)
1223  goto end;
1224  if (SCHInfoGetHostOSFlavour("192.168.1.1") != OS_POLICY_WINDOWS)
1225  goto end;
1226  if (SCHInfoGetHostOSFlavour("172.168.1.1") != OS_POLICY_BSD)
1227  goto end;
1228 
1229  result = 1;
1230 
1231  end:
1232  ConfDeInit();
1234 
1235  SCHInfoRestoreContextBackup();
1236 
1237  return result;
1238 }
1239 
1240 /**
1241  * \test Check the loading of host info from a configuration file.
1242  */
1243 static int SCHInfoTestLoadFromConfig02(void)
1244 {
1245  char config[] = "\
1246 %YAML 1.1\n\
1247 ---\n\
1248 host-os-policy:\n\
1249  one-two: [0.0.0.0/0]\n\
1250  one-two-three:\n\
1251  four_five:\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\
1254 \n";
1255 
1256  int result = 0;
1257 
1258  SCHInfoCreateContextBackup();
1259 
1261  ConfInit();
1262  ConfYamlLoadString(config, strlen(config));
1263 
1264  ConfNode *root = ConfGetNode("host-os-policy");
1265  if (root == NULL)
1266  goto end;
1267 
1268  int count = 0;
1269 
1270  ConfNode *policy;
1271  TAILQ_FOREACH(policy, &root->head, next) {
1272  switch (count) {
1273  case 0:
1274  if (strcmp("one-two", policy->name) != 0)
1275  goto end;
1276  break;
1277  case 1:
1278  if (strcmp("one-two-three", policy->name) != 0)
1279  goto end;
1280  break;
1281  case 2:
1282  if (strcmp("four-five", policy->name) != 0)
1283  goto end;
1284  break;
1285  case 3:
1286  if (strcmp("six-seven-eight", policy->name) != 0)
1287  goto end;
1288  break;
1289  case 4:
1290  if (strcmp("nine-ten-eleven", policy->name) != 0)
1291  goto end;
1292  break;
1293  }
1294  count++;
1295  }
1296 
1297  result = 1;
1298 
1299  end:
1300  ConfDeInit();
1302 
1303  SCHInfoRestoreContextBackup();
1304 
1305  return result;
1306 }
1307 
1308 /**
1309  * \test Check the loading of host info from a configuration file.
1310  */
1311 static int SCHInfoTestLoadFromConfig03(void)
1312 {
1313  char config[] = "\
1314 %YAML 1.1\n\
1315 ---\n\
1316 host-os-policy:\n\
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\
1321  vista: [0.0.0.5]\n\
1322 \n";
1323 
1324  int result = 0;
1325 
1326  SCHInfoCreateContextBackup();
1327 
1329  ConfInit();
1330  ConfYamlLoadString(config, strlen(config));
1331 
1332  ConfNode *root = ConfGetNode("host-os-policy");
1333  if (root == NULL)
1334  goto end;
1335 
1336  ConfNode *policy;
1337  TAILQ_FOREACH(policy, &root->head, next) {
1338  if (SCMapEnumNameToValue(policy->name, sc_hinfo_os_policy_map) == -1) {
1339  printf("Invalid enum map inside\n");
1340  goto end;
1341  }
1342  }
1343 
1344  result = 1;
1345 
1346  end:
1347  ConfDeInit();
1349 
1350  SCHInfoRestoreContextBackup();
1351  return result;
1352 }
1353 
1354 /**
1355  * \test Check the loading of host info from a configuration file.
1356  */
1357 static int SCHInfoTestLoadFromConfig04(void)
1358 {
1359  char config[] = "\
1360 %YAML 1.1\n\
1361 ---\n\
1362 host-os-policy:\n\
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\
1367  vista: [0.0.0.5]\n\
1368 \n";
1369 
1370  int result = 0;
1371 
1372  SCHInfoCreateContextBackup();
1373 
1375  ConfInit();
1376  ConfYamlLoadString(config, strlen(config));
1377 
1378  ConfNode *root = ConfGetNode("host-os-policy");
1379  if (root == NULL)
1380  goto end;
1381 
1382  ConfNode *policy;
1383  TAILQ_FOREACH(policy, &root->head, next) {
1384  if (SCMapEnumNameToValue(policy->name, sc_hinfo_os_policy_map) == -1) {
1385  printf("Invalid enum map inside\n");
1386  goto end;
1387  }
1388  }
1389 
1390  result = 1;
1391 
1392  end:
1393  ConfDeInit();
1395 
1396  SCHInfoRestoreContextBackup();
1397  return result;
1398 }
1399 
1400 /**
1401  * \test Check the loading of host info from a configuration file.
1402  */
1403 static int SCHInfoTestLoadFromConfig05(void)
1404 {
1405  char config[] = "\
1406 %YAML 1.1\n\
1407 ---\n\
1408 host-os-policy:\n\
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\
1413  linux: [0.0.0.5]\n\
1414 \n";
1415 
1416  SCHInfoCreateContextBackup();
1417 
1419  ConfInit();
1420  ConfYamlLoadString(config, strlen(config));
1422 
1423  FAIL_IF (SCHInfoGetHostOSFlavour("0.0.0.1") != OS_POLICY_BSD_RIGHT);
1424  FAIL_IF (SCHInfoGetHostOSFlavour("0.0.0.2") != OS_POLICY_OLD_LINUX);
1425  FAIL_IF (SCHInfoGetHostOSFlavour("0.0.0.3") != OS_POLICY_OLD_SOLARIS);
1426  FAIL_IF (SCHInfoGetHostOSFlavour("0.0.0.4") != OS_POLICY_WINDOWS);
1427  FAIL_IF (SCHInfoGetHostOSFlavour("0.0.0.5") != OS_POLICY_LINUX);
1428  FAIL_IF (SCHInfoGetHostOSFlavour("0.0.0.0") != -1);
1429  FAIL_IF (SCHInfoGetHostOSFlavour("0.0.0.6") != -1);
1430 
1431  ConfDeInit();
1433  SCHInfoRestoreContextBackup();
1434  PASS;
1435 }
1436 
1437 #endif /* UNITTESTS */
1438 
1440 {
1441 
1442 #ifdef UNITTESTS
1443 
1444  UtRegisterTest("SCHInfoTestInvalidOSFlavour01", SCHInfoTestInvalidOSFlavour01);
1445  UtRegisterTest("SCHInfoTestInvalidIPV4Address02",
1446  SCHInfoTestInvalidIPV4Address02);
1447  UtRegisterTest("SCHInfoTestInvalidIPV6Address03",
1448  SCHInfoTestInvalidIPV6Address03);
1449  UtRegisterTest("SCHInfoTestValidIPV4Address04",
1450  SCHInfoTestValidIPV4Address04);
1451  UtRegisterTest("SCHInfoTestValidIPV4Address05",
1452  SCHInfoTestValidIPV4Address05);
1453  UtRegisterTest("SCHInfoTestValidIPV6Address06",
1454  SCHInfoTestValidIPV6Address06);
1455  UtRegisterTest("SCHInfoTestValidIPV6Address07",
1456  SCHInfoTestValidIPV6Address07);
1457  UtRegisterTest("SCHInfoTestValidIPV6Address08",
1458  SCHInfoTestValidIPV6Address08);
1459  UtRegisterTest("SCHInfoTestValidIPV4Address09",
1460  SCHInfoTestValidIPV4Address09);
1461 
1462  UtRegisterTest("SCHInfoTestLoadFromConfig01", SCHInfoTestLoadFromConfig01);
1463  UtRegisterTest("SCHInfoTestLoadFromConfig02", SCHInfoTestLoadFromConfig02);
1464  UtRegisterTest("SCHInfoTestLoadFromConfig03", SCHInfoTestLoadFromConfig03);
1465  UtRegisterTest("SCHInfoTestLoadFromConfig04", SCHInfoTestLoadFromConfig04);
1466  UtRegisterTest("SCHInfoTestLoadFromConfig05", SCHInfoTestLoadFromConfig05);
1467 #endif /* UNITTESTS */
1468 }
OS_POLICY_HPUX10
@ OS_POLICY_HPUX10
Definition: stream-tcp-reassemble.h:43
util-byte.h
OS_POLICY_VISTA
@ OS_POLICY_VISTA
Definition: stream-tcp-reassemble.h:48
ConfNode_::val
char * val
Definition: conf.h:34
SCRadix6TreeFindBestMatch
SCRadix6Node * SCRadix6TreeFindBestMatch(const SCRadix6Tree *tree, const uint8_t *key, void **user_data)
Definition: util-radix6-tree.c:173
SCRadix4AddKeyIPV4String
bool SCRadix4AddKeyIPV4String(SCRadix4Tree *tree, const SCRadix4Config *config, const char *str, void *user)
Adds a new IPV4/netblock to the Radix4 tree from a string.
Definition: util-radix4-tree.c:227
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
OS_POLICY_SOLARIS
@ OS_POLICY_SOLARIS
Definition: stream-tcp-reassemble.h:42
SCRadix4TreeInitialize
SCRadix4Tree SCRadix4TreeInitialize(void)
Definition: util-radix4-tree.c:166
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:181
OS_POLICY_LINUX
@ OS_POLICY_LINUX
Definition: stream-tcp-reassemble.h:40
OS_POLICY_MACOS
@ OS_POLICY_MACOS
Definition: stream-tcp-reassemble.h:46
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
OS_POLICY_NONE
@ OS_POLICY_NONE
Definition: stream-tcp-reassemble.h:36
stream-tcp-reassemble.h
sc_hinfo_os_policy_map
SCEnumCharMap sc_hinfo_os_policy_map[]
Definition: util-host-os-info.c:44
util-unittest.h
OS_POLICY_IRIX
@ OS_POLICY_IRIX
Definition: stream-tcp-reassemble.h:45
SCRadix4TreeRelease
void SCRadix4TreeRelease(SCRadix4Tree *tree, const SCRadix4Config *config)
Definition: util-radix4-tree.c:172
SCRadix4Tree_
Structure for the radix tree.
Definition: util-radix4-tree.h:66
util-debug.h
SCHInfoGetIPv6HostOSFlavour
int SCHInfoGetIPv6HostOSFlavour(uint8_t *addr)
Retrieves the host os flavour, given an ipv6 address in the raw address format.
Definition: util-host-os-info.c:261
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
util-error.h
util-ip.h
SCHInfoRegisterTests
void SCHInfoRegisterTests(void)
Definition: util-host-os-info.c:1439
OS_POLICY_BSD_RIGHT
@ OS_POLICY_BSD_RIGHT
Definition: stream-tcp-reassemble.h:38
SC_HINFO_IS_IPV6
#define SC_HINFO_IS_IPV6
Definition: util-host-os-info.h:27
ConfYamlLoadString
int ConfYamlLoadString(const char *string, size_t len)
Load configuration from a YAML string.
Definition: conf-yaml-loader.c:523
conf-yaml-loader.h
stream-tcp-private.h
conf.h
SC_RADIX4_TREE_INITIALIZER
#define SC_RADIX4_TREE_INITIALIZER
Definition: util-radix4-tree.h:78
ConfCreateContextBackup
void ConfCreateContextBackup(void)
Creates a backup of the conf_hash hash_table used by the conf API.
Definition: conf.c:669
SCHInfoAddHostOSInfo
int SCHInfoAddHostOSInfo(const char *host_os, const char *host_os_ip_range, int is_ipv4)
Used to add the host-os-info data obtained from the conf.
Definition: util-host-os-info.c:122
SCHInfoCleanResources
void SCHInfoCleanResources(void)
Definition: util-host-os-info.c:270
util-host-os-info.h
OS_POLICY_WINDOWS
@ OS_POLICY_WINDOWS
Definition: stream-tcp-reassemble.h:47
SCRadix6Config_
Definition: util-radix6-tree.h:69
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
SC_HINFO_IS_IPV4
#define SC_HINFO_IS_IPV4
Definition: util-host-os-info.h:28
SCMapEnumNameToValue
int SCMapEnumNameToValue(const char *enum_name, SCEnumCharMap *table)
Maps a string name to an enum value from the supplied table. Please specify the last element of any m...
Definition: util-enum.c:40
SCRadix4Config_
Definition: util-radix4-tree.h:71
suricata-common.h
OS_POLICY_BSD
@ OS_POLICY_BSD
Definition: stream-tcp-reassemble.h:37
OS_POLICY_OLD_SOLARIS
@ OS_POLICY_OLD_SOLARIS
Definition: stream-tcp-reassemble.h:41
SCEnumCharMap_
Definition: util-enum.h:27
SCHInfoLoadFromConfig
void SCHInfoLoadFromConfig(void)
Load the host os policy information from the configuration.
Definition: util-host-os-info.c:281
ConfNode_::name
char * name
Definition: conf.h:33
util-radix4-tree.h
ConfRestoreContextBackup
void ConfRestoreContextBackup(void)
Restores the backup of the hash_table present in backup_conf_hash back to conf_hash.
Definition: conf.c:679
util-radix6-tree.h
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
FatalError
#define FatalError(...)
Definition: util-debug.h:502
SCRadix4TreeFindBestMatch
SCRadix4Node * SCRadix4TreeFindBestMatch(const SCRadix4Tree *tree, const uint8_t *key, void **user_data)
Definition: util-radix4-tree.c:154
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
ConfInit
void ConfInit(void)
Initialize the configuration system.
Definition: conf.c:120
SC_EEXIST
@ SC_EEXIST
Definition: util-error.h:32
str
#define str(s)
Definition: suricata-common.h:291
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCRadix6Tree_
Structure for the radix tree.
Definition: util-radix6-tree.h:64
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
OS_POLICY_WINDOWS2K3
@ OS_POLICY_WINDOWS2K3
Definition: stream-tcp-reassemble.h:49
sc_errno
thread_local SCError sc_errno
Definition: util-error.c:31
SCRadix6AddKeyIPV6String
bool SCRadix6AddKeyIPV6String(SCRadix6Tree *tree, const SCRadix6Config *config, const char *str, void *user)
Adds a new IPV6/netblock to the Radix6 tree from a string.
Definition: util-radix6-tree.c:262
SCHInfoGetIPv4HostOSFlavour
int SCHInfoGetIPv4HostOSFlavour(uint8_t *addr)
Retrieves the host os flavour, given an ipv4 address in the raw address format.
Definition: util-host-os-info.c:244
SCRadix6TreeInitialize
SCRadix6Tree SCRadix6TreeInitialize(void)
Definition: util-radix6-tree.c:360
ConfDeInit
void ConfDeInit(void)
De-initializes the configuration system.
Definition: conf.c:688
OS_POLICY_OLD_LINUX
@ OS_POLICY_OLD_LINUX
Definition: stream-tcp-reassemble.h:39
SCRadix6TreeRelease
void SCRadix6TreeRelease(SCRadix6Tree *tree, const SCRadix6Config *config)
Definition: util-radix6-tree.c:366
SC_RADIX6_TREE_INITIALIZER
#define SC_RADIX6_TREE_INITIALIZER
Definition: util-radix6-tree.h:76
OS_POLICY_HPUX11
@ OS_POLICY_HPUX11
Definition: stream-tcp-reassemble.h:44
util-enum.h