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 "util-host-info.h"
29 #include "util-byte.h"
30 #include "util-debug.h"
31 
32 #ifndef OS_WIN32
33 #include <sys/utsname.h>
34 
35 #define VERSION_REGEX "^([0-9]+)\\.([0-9]+)"
36 
37 int SCKernelVersionIsAtLeast(int major, int minor)
38 {
39  struct utsname kuname;
40  pcre2_code *version_regex;
41  pcre2_match_data *version_regex_match;
42  int en;
43  int opts = 0;
44  PCRE2_SIZE eo;
45  int ret;
46  int kmajor, kminor;
47 
48  /* get local version */
49  if (uname(&kuname) != 0) {
50  SCLogError("Invalid uname return: %s", strerror(errno));
51  return 0;
52  }
53 
54  SCLogDebug("Kernel release is '%s'", kuname.release);
55 
56  version_regex =
57  pcre2_compile((PCRE2_SPTR8)VERSION_REGEX, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
58  if (version_regex == NULL) {
59  PCRE2_UCHAR errbuffer[256];
60  pcre2_get_error_message(en, errbuffer, sizeof(errbuffer));
61  SCLogError("pcre2 compile of \"%s\" failed at "
62  "offset %d: %s",
63  VERSION_REGEX, (int)eo, errbuffer);
64  goto error;
65  }
66  version_regex_match = pcre2_match_data_create_from_pattern(version_regex, NULL);
67 
68  ret = pcre2_match(version_regex, (PCRE2_SPTR8)kuname.release, strlen(kuname.release), 0, 0,
69  version_regex_match, NULL);
70 
71  if (ret < 0) {
72  SCLogError("Version did not cut");
73  goto error;
74  }
75 
76  if (ret < 3) {
77  SCLogError("Version major and minor not found (ret %d)", ret);
78  goto error;
79  }
80 
81  char majorstr[32];
82  size_t pcre2len = sizeof(majorstr);
83  ret = pcre2_substring_copy_bynumber(
84  version_regex_match, 1, (PCRE2_UCHAR8 *)majorstr, &pcre2len);
85  if (ret < 0) {
86  SCLogError("pcre2_substring_copy_bynumber failed");
87  goto error;
88  }
89 
90  char minorstr[32];
91  pcre2len = sizeof(majorstr);
92  ret = pcre2_substring_copy_bynumber(
93  version_regex_match, 2, (PCRE2_UCHAR8 *)minorstr, &pcre2len);
94  if (ret < 0) {
95  SCLogError("pcre2_substring_copy_bynumber failed");
96  goto error;
97  }
98 
99  if (StringParseInt32(&kmajor, 10, 0, (const char *)majorstr) < 0) {
100  SCLogError("Invalid value for kmajor: '%s'", minorstr);
101  goto error;
102  }
103  if (StringParseInt32(&kminor, 10, 0, (const char *)minorstr) < 0) {
104  SCLogError("Invalid value for kminor: '%s'", minorstr);
105  goto error;
106  }
107 
108  pcre2_match_data_free(version_regex_match);
109  pcre2_code_free(version_regex);
110 
111  if (kmajor > major)
112  return 1;
113  if (kmajor == major && kminor >= minor)
114  return 1;
115 error:
116  return 0;
117 }
118 
119 #else /* OS_WIN32 */
120 
121 int SCKernelVersionIsAtLeast(int major, int minor)
122 {
123  SCLogError("OS compare is not supported on Windows");
124  return 0;
125 }
126 
127 #endif /* OS_WIN32 */
util-byte.h
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
SCKernelVersionIsAtLeast
int SCKernelVersionIsAtLeast(int major, int minor)
Definition: util-host-info.c:37
VERSION_REGEX
#define VERSION_REGEX
Definition: util-host-info.c:35
StringParseInt32
int StringParseInt32(int32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:622
util-debug.h
util-host-info.h
suricata-common.h
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261