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  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  bool err = false;
87  if (StringParseInt32(&kmajor, 10, 0, (const char *)list[1]) < 0) {
88  SCLogError(SC_ERR_INVALID_VALUE, "Invalid value for kmajor: '%s'", list[1]);
89  err = true;
90  }
91  if (StringParseInt32(&kminor, 10, 0, (const char *)list[2]) < 0) {
92  SCLogError(SC_ERR_INVALID_VALUE, "Invalid value for kminor: '%s'", list[2]);
93  err = true;
94  }
95 
96  pcre_free_substring_list(list);
97  pcre_free_study(version_regex_study);
98  pcre_free(version_regex);
99 
100  if (err)
101  goto error;
102 
103  if (kmajor > major)
104  return 1;
105  if (kmajor == major && kminor >= minor)
106  return 1;
107 error:
108  return 0;
109 }
110 
111 #else /* OS_WIN32 */
112 
113 int SCKernelVersionIsAtLeast(int major, int minor)
114 {
115  SCLogError(SC_ERR_NOT_SUPPORTED, "OS compare is not supported on Windows");
116  return 0;
117 }
118 
119 #endif /* OS_WIN32 */
util-byte.h
pcre_free_study
#define pcre_free_study
Definition: suricata-common.h:373
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:298
SC_ERR_NOT_SUPPORTED
@ SC_ERR_NOT_SUPPORTED
Definition: util-error.h:257
StringParseInt32
int StringParseInt32(int32_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:613
SCKernelVersionIsAtLeast
int SCKernelVersionIsAtLeast(int major, int minor)
Definition: util-host-info.c:36
VERSION_REGEX
#define VERSION_REGEX
Definition: util-host-info.c:34
SC_ERR_PCRE_MATCH
@ SC_ERR_PCRE_MATCH
Definition: util-error.h:32
SC_ERR_PCRE_STUDY
@ SC_ERR_PCRE_STUDY
Definition: util-error.h:36
util-host-info.h
suricata-common.h
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
MAX_SUBSTRINGS
#define MAX_SUBSTRINGS