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  PCRE2_UCHAR **list;
48 
49  /* get local version */
50  if (uname(&kuname) != 0) {
51  SCLogError("Invalid uname return: %s", strerror(errno));
52  return 0;
53  }
54 
55  SCLogDebug("Kernel release is '%s'", kuname.release);
56 
57  version_regex =
58  pcre2_compile((PCRE2_SPTR8)VERSION_REGEX, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
59  if (version_regex == NULL) {
60  PCRE2_UCHAR errbuffer[256];
61  pcre2_get_error_message(en, errbuffer, sizeof(errbuffer));
62  SCLogError("pcre2 compile of \"%s\" failed at "
63  "offset %d: %s",
64  VERSION_REGEX, (int)eo, errbuffer);
65  goto error;
66  }
67  version_regex_match = pcre2_match_data_create_from_pattern(version_regex, NULL);
68 
69  ret = pcre2_match(version_regex, (PCRE2_SPTR8)kuname.release, strlen(kuname.release), 0, 0,
70  version_regex_match, NULL);
71 
72  if (ret < 0) {
73  SCLogError("Version did not cut");
74  goto error;
75  }
76 
77  if (ret < 3) {
78  SCLogError("Version major and minor not found (ret %d)", ret);
79  goto error;
80  }
81 
82  pcre2_substring_list_get(version_regex_match, &list, NULL);
83 
84  bool err = false;
85  if (StringParseInt32(&kmajor, 10, 0, (const char *)list[1]) < 0) {
86  SCLogError("Invalid value for kmajor: '%s'", list[1]);
87  err = true;
88  }
89  if (StringParseInt32(&kminor, 10, 0, (const char *)list[2]) < 0) {
90  SCLogError("Invalid value for kminor: '%s'", list[2]);
91  err = true;
92  }
93 
94  pcre2_substring_list_free((PCRE2_SPTR *)list);
95  pcre2_match_data_free(version_regex_match);
96  pcre2_code_free(version_regex);
97 
98  if (err)
99  goto error;
100 
101  if (kmajor > major)
102  return 1;
103  if (kmajor == major && kminor >= minor)
104  return 1;
105 error:
106  return 0;
107 }
108 
109 #else /* OS_WIN32 */
110 
111 int SCKernelVersionIsAtLeast(int major, int minor)
112 {
113  SCLogError("OS compare is not supported on Windows");
114  return 0;
115 }
116 
117 #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