]>
Commit | Line | Data |
---|---|---|
949b358b JI |
1 | /* Copyright (C) 2017 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 | use std::os::raw::c_char; | |
1b0b74dc JI |
19 | use std::os::raw::c_void; |
20 | use std::os::raw::c_int; | |
949b358b JI |
21 | use std::ffi::{CString, CStr}; |
22 | use std::ptr; | |
23 | use std::str; | |
24 | ||
949b358b JI |
25 | extern { |
26 | fn ConfGet(key: *const c_char, res: *mut *const c_char) -> i8; | |
1b0b74dc JI |
27 | fn ConfGetChildValue(conf: *const c_void, key: *const c_char, |
28 | vptr: *mut *const c_char) -> i8; | |
29 | fn ConfGetChildValueBool(conf: *const c_void, key: *const c_char, | |
30 | vptr: *mut c_int) -> i8; | |
949b358b JI |
31 | } |
32 | ||
33 | // Return the string value of a configuration value. | |
34 | pub fn conf_get(key: &str) -> Option<&str> { | |
35 | let mut vptr: *const c_char = ptr::null_mut(); | |
36 | ||
37 | unsafe { | |
8db78208 PA |
38 | let s = CString::new(key).unwrap(); |
39 | if ConfGet(s.as_ptr(), &mut vptr) != 1 { | |
85ba2e16 | 40 | SCLogDebug!("Failed to find value for key {}", key); |
949b358b JI |
41 | return None; |
42 | } | |
43 | } | |
44 | ||
45 | if vptr == ptr::null() { | |
46 | return None; | |
47 | } | |
48 | ||
49 | let value = str::from_utf8(unsafe{ | |
50 | CStr::from_ptr(vptr).to_bytes() | |
51 | }).unwrap(); | |
52 | ||
53 | return Some(value); | |
54 | } | |
55 | ||
56 | // Return the value of key as a boolean. A value that is not set is | |
57 | // the same as having it set to false. | |
58 | pub fn conf_get_bool(key: &str) -> bool { | |
59 | match conf_get(key) { | |
60 | Some(val) => { | |
61 | match val { | |
62 | "1" | "yes" | "true" | "on" => { | |
63 | return true; | |
64 | }, | |
65 | _ => {}, | |
66 | } | |
67 | }, | |
68 | None => {}, | |
69 | } | |
70 | ||
71 | return false; | |
72 | } | |
1b0b74dc JI |
73 | |
74 | /// Wrap a Suricata ConfNode and expose some of its methods with a | |
75 | /// Rust friendly interface. | |
76 | pub struct ConfNode { | |
77 | pub conf: *const c_void, | |
78 | } | |
79 | ||
80 | impl ConfNode { | |
81 | ||
aa8871a5 JL |
82 | pub fn wrap(conf: *const c_void) -> Self { |
83 | return Self { | |
1b0b74dc JI |
84 | conf: conf, |
85 | } | |
86 | } | |
87 | ||
88 | pub fn get_child_value(&self, key: &str) -> Option<&str> { | |
89 | let mut vptr: *const c_char = ptr::null_mut(); | |
90 | ||
91 | unsafe { | |
8db78208 | 92 | let s = CString::new(key).unwrap(); |
1b0b74dc | 93 | if ConfGetChildValue(self.conf, |
8db78208 | 94 | s.as_ptr(), |
1b0b74dc JI |
95 | &mut vptr) != 1 { |
96 | return None; | |
97 | } | |
98 | } | |
99 | ||
100 | if vptr == ptr::null() { | |
101 | return None; | |
102 | } | |
103 | ||
104 | let value = str::from_utf8(unsafe{ | |
105 | CStr::from_ptr(vptr).to_bytes() | |
106 | }).unwrap(); | |
107 | ||
108 | return Some(value); | |
109 | } | |
110 | ||
111 | pub fn get_child_bool(&self, key: &str) -> bool { | |
112 | let mut vptr: c_int = 0; | |
113 | ||
114 | unsafe { | |
8db78208 | 115 | let s = CString::new(key).unwrap(); |
1b0b74dc | 116 | if ConfGetChildValueBool(self.conf, |
8db78208 | 117 | s.as_ptr(), |
1b0b74dc JI |
118 | &mut vptr) != 1 { |
119 | return false; | |
120 | } | |
121 | } | |
122 | ||
123 | if vptr == 1 { | |
124 | return true; | |
125 | } | |
126 | return false; | |
127 | } | |
128 | ||
129 | } |