suricata
util-host-info.c
Go to the documentation of this file.
1 /* Copyright (C) 2014 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 Eric Leblond <eric@regit.org>
22  *
23  * Get information on running host
24  *
25  */
26 
27 #include "suricata-common.h"
28 #include "config.h"
29 #include "util-host-info.h"
30 
31 #ifndef OS_WIN32
32 #include <sys/utsname.h>
33 
34 #define VERSION_REGEX "^([0-9]+)\\.([0-9]+)"
35 
36 int SCKernelVersionIsAtLeast(int major, int minor)
37 {
38  struct utsname kuname;
39  pcre *version_regex;
40  pcre_extra *version_regex_study;
41  const char *eb;
42  int opts = 0;
43  int eo;
44 #define MAX_SUBSTRINGS 3 * 6
45  int ov[MAX_SUBSTRINGS];
46  int ret;
47  int kmajor, kminor;
48  const char **list;
49 
50  /* get local version */
51  if (uname(&kuname) != 0) {
52  SCLogError(SC_ERR_INVALID_VALUE, "Invalid uname return: %s",
53  strerror(errno));
54  return 0;
55  }
56 
57  SCLogDebug("Kernel release is '%s'", kuname.release);
58 
59  version_regex = pcre_compile(VERSION_REGEX, opts, &eb, &eo, NULL);
60  if (version_regex == NULL) {
61  SCLogError(SC_ERR_PCRE_COMPILE, "pcre compile of \"%s\" failed at offset %" PRId32 ": %s", VERSION_REGEX, eo, eb);
62  goto error;
63  }
64 
65  version_regex_study = pcre_study(version_regex, 0, &eb);
66  if (eb != NULL) {
67  SCLogError(SC_ERR_PCRE_STUDY, "pcre study failed: %s", eb);
68  goto error;
69  }
70 
71  ret = pcre_exec(version_regex, version_regex_study, kuname.release,
72  strlen(kuname.release), 0, 0, ov, MAX_SUBSTRINGS);
73 
74  if (ret < 0) {
75  SCLogError(SC_ERR_PCRE_MATCH, "Version did not cut");
76  goto error;
77  }
78 
79  if (ret < 3) {
80  SCLogError(SC_ERR_PCRE_MATCH, "Version major and minor not found (ret %d)", ret);
81  goto error;
82  }
83 
84  pcre_get_substring_list(kuname.release, ov, ret, &list);
85 
86  kmajor = atoi(list[1]);
87  kminor = atoi(list[2]);
88 
89  pcre_free_substring_list(list);
90  pcre_free_study(version_regex_study);
91  pcre_free(version_regex);
92 
93  if (kmajor > major)
94  return 1;
95  if (kmajor == major && kminor >= minor)
96  return 1;
97 error:
98  return 0;
99 }
100 
101 #else /* OS_WIN32 */
102 
103 int SCKernelVersionIsAtLeast(int major, int minor)
104 {
105  SCLogError(SC_ERR_NOT_SUPPORTED, "OS compare is not supported on Windows");
106  return 0;
107 }
108 
109 #endif /* OS_WIN32 */
#define SCLogDebug(...)
Definition: util-debug.h:335
#define VERSION_REGEX
int SCKernelVersionIsAtLeast(int major, int minor)
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
#define MAX_SUBSTRINGS
#define pcre_free_study