Skip to content

Commit 6d24146

Browse files
committed
Merge branch 'main' into async-interpreter
2 parents cc93800 + cfc22f8 commit 6d24146

File tree

29 files changed

+946
-256
lines changed

29 files changed

+946
-256
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ message ("-- Configuring the Scanner...")
88

99
# VERSION: Always include major, minor and patch level.
1010
project (openvas
11-
VERSION 23.8.5
11+
VERSION 23.9.0
1212
LANGUAGES C)
1313

1414
if (POLICY CMP0005)

charts/openvasd/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ version: 0.1.0
2121
# incremented each time you make changes to the application. Versions are not expected to
2222
# follow Semantic Versioning. They should reflect the version the application is using.
2323
# It is recommended to use it with quotes.
24-
appVersion: "23.8.5"
24+
appVersion: "23.9.0"

rust/Cargo.lock

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ members = [
1212
"nasl-builtin-network",
1313
"nasl-builtin-description",
1414
"nasl-builtin-utils",
15+
"nasl-builtin-regex",
1516
"nasl-builtin-std",
1617
"nasl-syntax",
1718
"nasl-interpreter",

rust/doc/openapi.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,43 @@ paths:
7070
schema:
7171
type: "string"
7272
description: "Header"
73+
/scans:
74+
head:
75+
description: "Get the response header. It contains the API version, feed version and available authentication methods."
76+
operationId: "get_info"
77+
tags:
78+
- "general"
79+
responses:
80+
"204":
81+
headers:
82+
api-version:
83+
description: "Comma separated list of available API versions"
84+
schema:
85+
type: "string"
86+
feed-version:
87+
description: "The version of the VT feed"
88+
schema:
89+
type: "string"
90+
authentication:
91+
description: "Supported authentication methods"
92+
schema:
93+
type: "string"
94+
description: "Authenticated and authorized"
95+
"401":
96+
headers:
97+
api-version:
98+
description: "Comma separated list of available API versions"
99+
schema:
100+
type: "string"
101+
feed-version:
102+
description: "The version of the VT feed"
103+
schema:
104+
type: "string"
105+
authentication:
106+
description: "Supported authentication methods"
107+
schema:
108+
type: "string"
109+
description: "Unauthorized. Required or invalid client certificates"
73110

74111
/health/alive:
75112
get:

rust/nasl-builtin-knowledge-base/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
- set_kb_item
44
- get_kp_item
5-
6-
## Missing
7-
- get_host_kb_index
85
- get_kb_list
9-
- index
106
- replace_kb_item
7+
8+
## Missing
9+
- get_host_kb_index: Do not apply. Redis specific and currently not used in any script

rust/nasl-builtin-knowledge-base/src/lib.rs

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,30 @@ use storage::{Field, Kb, Retrieve};
1111
use nasl_builtin_utils::Context;
1212
use nasl_syntax::NaslValue;
1313

14-
/// NASL function to set a knowledge base
14+
/// NASL function to set a value under name in a knowledge base
15+
/// Only pushes unique values for the given name.
1516
#[nasl_function(named(name, value, expires))]
1617
fn set_kb_item(
17-
c: &Context,
18-
name: &str,
18+
name: NaslValue,
1919
value: NaslValue,
20-
expires: Option<u64>,
20+
expires: Option<NaslValue>,
21+
c: &Context,
2122
) -> Result<NaslValue, FunctionErrorKind> {
22-
let expires = expires.map(|seconds| {
23+
let expires = match expires {
24+
Some(NaslValue::Number(x)) => Some(x),
25+
Some(NaslValue::Exit(0)) => None,
26+
None => None,
27+
Some(x) => {
28+
return Err(FunctionErrorKind::Diagnostic(
29+
format!("expected expires to be a number but is {x}."),
30+
None,
31+
))
32+
}
33+
}
34+
.map(|seconds| {
2335
let start = SystemTime::now();
2436
match start.duration_since(UNIX_EPOCH) {
25-
Ok(x) => x.as_secs() + seconds,
37+
Ok(x) => x.as_secs() + seconds as u64,
2638
Err(_) => 0,
2739
}
2840
});
@@ -41,9 +53,9 @@ fn set_kb_item(
4153

4254
/// NASL function to get a knowledge base
4355
#[nasl_function]
44-
fn get_kb_item(arg: &NaslValue, c: &Context) -> Result<NaslValue, FunctionErrorKind> {
56+
fn get_kb_item(key: &str, c: &Context) -> Result<NaslValue, FunctionErrorKind> {
4557
c.retriever()
46-
.retrieve(c.key(), Retrieve::KB(arg.to_string()))
58+
.retrieve(c.key(), Retrieve::KB(key.to_string()))
4759
.map(|r| {
4860
r.into_iter()
4961
.filter_map(|x| match x {
@@ -56,10 +68,52 @@ fn get_kb_item(arg: &NaslValue, c: &Context) -> Result<NaslValue, FunctionErrorK
5668
.map_err(|e| e.into())
5769
}
5870

71+
/// NASL function to replace a kb list
72+
#[nasl_function(named(name, value, expires))]
73+
fn replace_kb_item(
74+
name: NaslValue,
75+
value: NaslValue,
76+
c: &Context,
77+
) -> Result<NaslValue, FunctionErrorKind> {
78+
c.dispatcher()
79+
.dispatch_replace(
80+
c.key(),
81+
Field::KB(Kb {
82+
key: name.to_string(),
83+
value: value.clone().as_primitive(),
84+
expire: None,
85+
}),
86+
)
87+
.map(|_| NaslValue::Null)
88+
.map_err(|e| e.into())
89+
}
90+
91+
/// NASL function to retrieve an item in a KB.
92+
#[nasl_function(named(name, value, expires))]
93+
fn get_kb_list(key: NaslValue, c: &Context) -> Result<NaslValue, FunctionErrorKind> {
94+
c.retriever()
95+
.retrieve(c.key(), Retrieve::KB(key.to_string()))
96+
.map(|r| {
97+
r.into_iter()
98+
.filter_map(|x| match x {
99+
Field::NVT(_) | Field::NotusAdvisory(_) | Field::Result(_) => None,
100+
Field::KB(kb) => Some(kb.value.into()),
101+
})
102+
.collect::<Vec<_>>()
103+
})
104+
.map(NaslValue::Array)
105+
.map_err(|e| e.into())
106+
}
107+
59108
pub struct KnowledgeBase;
60109

61110
function_set! {
62111
KnowledgeBase,
63112
sync_stateless,
64-
(set_kb_item, get_kb_item)
113+
(
114+
set_kb_item,
115+
get_kb_item,
116+
get_kb_list,
117+
replace_kb_item
118+
)
65119
}

rust/nasl-builtin-knowledge-base/tests/kb.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,23 @@ mod tests {
3131
FunctionErrorKind::MissingPositionalArguments { .. }
3232
);
3333
}
34+
35+
#[test]
36+
fn get_kb_list() {
37+
let mut t = TestBuilder::default();
38+
t.ok(r#"set_kb_item(name: "test", value: 1);"#, NaslValue::Null);
39+
t.ok(r#"set_kb_item(name: "test", value: 2);"#, NaslValue::Null);
40+
t.ok(r#"get_kb_list("test");"#, vec![1, 2]);
41+
}
42+
43+
#[test]
44+
fn replace_kb_item() {
45+
let mut t = TestBuilder::default();
46+
t.ok(r#"set_kb_item(name: "test", value: 1);"#, NaslValue::Null);
47+
t.ok(
48+
r#"replace_kb_item(name: "test", value: 2);"#,
49+
NaslValue::Null,
50+
);
51+
t.ok(r#"get_kb_item("test");"#, 2);
52+
}
3453
}

rust/nasl-builtin-network/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ nasl-function-proc-macro = { path = "../nasl-function-proc-macro" }
1111
nasl-syntax = { path = "../nasl-syntax" }
1212
storage = { path = "../storage" }
1313

14+
dns-lookup = "2.0"
1415
libc = "0.2"
1516
rustls = "0.23.5"
1617
rustls-pemfile = "2.1"

rust/nasl-builtin-network/src/lib.rs

Lines changed: 5 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
//
33
// SPDX-License-Identifier: GPL-2.0-or-later
44

5-
use std::fmt::Display;
5+
use std::{fmt::Display, net::IpAddr};
66

7-
use nasl_builtin_utils::{Context, FunctionErrorKind, Register};
7+
use nasl_builtin_utils::{Context, FunctionErrorKind};
88
use nasl_syntax::NaslValue;
99
use storage::Field;
1010

@@ -14,14 +14,14 @@ pub mod socket;
1414

1515
// 512 Bytes are typically supported by network devices. The ip header maximum size is 60 and a UDP
1616
// header contains 8 bytes, which must be subtracted from the max size for UDP packages.
17-
// TODO: Calculate the MTU dynamically
1817
const MTU: usize = 512 - 60 - 8;
1918

2019
/// Standard port for networking functions
21-
/// @return none
2220
const DEFAULT_PORT: u16 = 33435;
2321

24-
pub fn mtu() -> usize {
22+
// Get the max MTU possible for network communication
23+
// TODO: Calculate the MTU dynamically
24+
pub fn mtu(_: IpAddr) -> usize {
2525
MTU
2626
}
2727

@@ -74,47 +74,6 @@ impl Display for OpenvasEncaps {
7474
}
7575
}
7676

77-
fn get_named_value(r: &Register, name: &str) -> Result<NaslValue, FunctionErrorKind> {
78-
match r.named(name) {
79-
Some(x) => match x {
80-
nasl_builtin_utils::ContextType::Function(_, _) => Err(
81-
FunctionErrorKind::WrongArgument(format!("{name} is a function")),
82-
),
83-
nasl_builtin_utils::ContextType::Value(val) => Ok(val.to_owned()),
84-
},
85-
None => Err(FunctionErrorKind::MissingArguments(vec![name.to_string()])),
86-
}
87-
}
88-
89-
fn get_usize(r: &Register, name: &str) -> Result<usize, FunctionErrorKind> {
90-
match get_named_value(r, name)? {
91-
NaslValue::Number(num) => {
92-
if num < 0 {
93-
return Err(FunctionErrorKind::WrongArgument(format!(
94-
"Argument {name} must be >= 0"
95-
)));
96-
}
97-
Ok(num as usize)
98-
}
99-
_ => Err(FunctionErrorKind::WrongArgument(
100-
"Wrong type for argument, expected a number".to_string(),
101-
)),
102-
}
103-
}
104-
105-
fn get_data(r: &Register) -> Result<Vec<u8>, FunctionErrorKind> {
106-
Ok((get_named_value(r, "data")?).into())
107-
}
108-
109-
fn get_opt_int(r: &Register, name: &str) -> Option<i64> {
110-
get_named_value(r, name)
111-
.map(|val| match val {
112-
NaslValue::Number(len) => Some(len),
113-
_ => None,
114-
})
115-
.unwrap_or_default()
116-
}
117-
11877
pub fn get_kb_item(context: &Context, name: &str) -> Result<Option<NaslValue>, FunctionErrorKind> {
11978
context
12079
.retriever()
@@ -129,30 +88,6 @@ pub fn get_kb_item(context: &Context, name: &str) -> Result<Option<NaslValue>, F
12988
.map_err(|e| e.into())
13089
}
13190

132-
pub fn get_pos_port(r: &Register) -> Result<u16, FunctionErrorKind> {
133-
match r
134-
.positional()
135-
.first()
136-
.ok_or(FunctionErrorKind::MissingPositionalArguments {
137-
expected: 1,
138-
got: 0,
139-
})? {
140-
NaslValue::Number(port) => {
141-
if *port < 0 || *port > 65535 {
142-
return Err(FunctionErrorKind::WrongArgument(format!(
143-
"{} is not a valid port number",
144-
*port
145-
)));
146-
}
147-
Ok(*port as u16)
148-
}
149-
x => Err(FunctionErrorKind::WrongArgument(format!(
150-
"{} is not a valid port number",
151-
x
152-
))),
153-
}
154-
}
155-
15691
pub fn verify_port(port: i64) -> Result<u16, FunctionErrorKind> {
15792
if !(0..=65535).contains(&port) {
15893
return Err(FunctionErrorKind::WrongArgument(format!(

0 commit comments

Comments
 (0)