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 #include "util-byte.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  pcre *version_regex;
41  pcre_extra *version_regex_study;
42  const char *eb;
43  int opts = 0;
44  int eo;
45 #define MAX_SUBSTRINGS 3 * 6
46  int ov[MAX_SUBSTRINGS];
47  int ret;
48  int kmajor, kminor;
49  const char **list;
50 
51  /* get local version */
52  if (uname(&kuname) != 0) {
53  SCLogError(SC_ERR_INVALID_VALUE, "Invalid uname return: %s",
54  strerror(errno));
55  return 0;
56  }
57 
58  SCLogDebug("Kernel release is '%s'", kuname.release);
59 
60  version_regex = pcre_compile(VERSION_REGEX, opts, &eb, &eo, NULL);
61  if (version_regex == NULL) {
62  SCLogError(SC_ERR_PCRE_COMPILE, "pcre compile of \"%s\" failed at offset %" PRId32 ": %s", VERSION_REGEX, eo, eb);
63  goto error;
64  }
65 
66  version_regex_study = pcre_study(version_regex, 0, &eb);
67  if (eb != NULL) {
68  SCLogError(SC_ERR_PCRE_STUDY, "pcre study failed: %s", eb);
69  goto error;
70  }
71 
72  ret = pcre_exec(version_regex, version_regex_study, kuname.release,
73  strlen(kuname.release), 0, 0, ov, MAX_SUBSTRINGS);
74 
75  if (ret < 0) {
76  SCLogError(SC_ERR_PCRE_MATCH, "Version did not cut");
77  goto error;
78  }
79 
80  if (ret < 3) {
81  SCLogError(SC_ERR_PCRE_MATCH, "Version major and minor not found (ret %d)", ret);
82  goto error;
83  }
84 
85  pcre_get_substring_list(kuname.release, ov, ret, &list);
86 
87  bool err = false;
88  if (StringParseInt32(&kmajor, 10, 0, (const char *)list[1]) < 0) {
89  SCLogError(SC_ERR_INVALID_VALUE, "Invalid value for kmajor: '%s'", list[1]);
90  err = true;
91  }
92  if (StringParseInt32(&kminor, 10, 0, (const char *)list[2]) < 0) {
93  SCLogError(SC_ERR_INVALID_VALUE, "Invalid value for kminor: '%s'", list[2]);
94  err = true;
95  }
96 
97  pcre_free_substring_list(list);
98  pcre_free_study(version_regex_study);
99  pcre_free(version_regex);
100 
101  if (err)
102  goto error;
103 
104  if (kmajor > major)
105  return 1;
106  if (kmajor == major && kminor >= minor)
107  return 1;
108 error:
109  return 0;
110 }
111 
112 #else /* OS_WIN32 */
113 
114 int SCKernelVersionIsAtLeast(int major, int minor)
115 {
116  SCLogError(SC_ERR_NOT_SUPPORTED, "OS compare is not supported on Windows");
117  return 0;
118 }
119 
120 #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:37
VERSION_REGEX
#define VERSION_REGEX
Definition: util-host-info.c:35
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