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 
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  pcre2_code *version_regex;
40  pcre2_match_data *version_regex_match;
41  int en;
42  int opts = 0;
43  PCRE2_SIZE eo;
44  int ret;
45  int kmajor, kminor;
46  PCRE2_UCHAR **list;
47 
48  /* get local version */
49  if (uname(&kuname) != 0) {
50  SCLogError(SC_ERR_INVALID_VALUE, "Invalid uname return: %s",
51  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));
63  "pcre2 compile of \"%s\" failed at "
64  "offset %d: %s",
65  VERSION_REGEX, (int)eo, errbuffer);
66  goto error;
67  }
68  version_regex_match = pcre2_match_data_create_from_pattern(version_regex, NULL);
69 
70  ret = pcre2_match(version_regex, (PCRE2_SPTR8)kuname.release, strlen(kuname.release), 0, 0,
71  version_regex_match, NULL);
72 
73  if (ret < 0) {
74  SCLogError(SC_ERR_PCRE_MATCH, "Version did not cut");
75  goto error;
76  }
77 
78  if (ret < 3) {
79  SCLogError(SC_ERR_PCRE_MATCH, "Version major and minor not found (ret %d)", ret);
80  goto error;
81  }
82 
83  pcre2_substring_list_get(version_regex_match, &list, NULL);
84 
85  bool err = false;
86  if (StringParseInt32(&kmajor, 10, 0, (const char *)list[1]) < 0) {
87  SCLogError(SC_ERR_INVALID_VALUE, "Invalid value for kmajor: '%s'", list[1]);
88  err = true;
89  }
90  if (StringParseInt32(&kminor, 10, 0, (const char *)list[2]) < 0) {
91  SCLogError(SC_ERR_INVALID_VALUE, "Invalid value for kminor: '%s'", list[2]);
92  err = true;
93  }
94 
95  pcre2_substring_list_free((PCRE2_SPTR *)list);
96  pcre2_match_data_free(version_regex_match);
97  pcre2_code_free(version_regex);
98 
99  if (err)
100  goto error;
101 
102  if (kmajor > major)
103  return 1;
104  if (kmajor == major && kminor >= minor)
105  return 1;
106 error:
107  return 0;
108 }
109 
110 #else /* OS_WIN32 */
111 
112 int SCKernelVersionIsAtLeast(int major, int minor)
113 {
114  SCLogError(SC_ERR_NOT_SUPPORTED, "OS compare is not supported on Windows");
115  return 0;
116 }
117 
118 #endif /* OS_WIN32 */
util-byte.h
SC_ERR_PCRE_COMPILE
@ SC_ERR_PCRE_COMPILE
Definition: util-error.h:35
SC_ERR_INVALID_VALUE
@ SC_ERR_INVALID_VALUE
Definition: util-error.h:160
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:296
SC_ERR_NOT_SUPPORTED
@ SC_ERR_NOT_SUPPORTED
Definition: util-error.h:257
SCKernelVersionIsAtLeast
int SCKernelVersionIsAtLeast(int major, int minor)
Definition: util-host-info.c:36
VERSION_REGEX
#define VERSION_REGEX
Definition: util-host-info.c:34
StringParseInt32
int StringParseInt32(int32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:613
SC_ERR_PCRE_MATCH
@ SC_ERR_PCRE_MATCH
Definition: util-error.h:32
util-host-info.h
suricata-common.h
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:255