From d801b48df0f78e44cf756ee69670d53789fd0964 Mon Sep 17 00:00:00 2001 From: Toni Peter Date: Thu, 7 Nov 2024 12:22:58 +0100 Subject: [PATCH] Rename FunctionErrorKind -> NaslFnError for now. --- .../src/nasl/builtin/cryptographic/aes_cbc.rs | 14 +- .../src/nasl/builtin/cryptographic/aes_ccm.rs | 52 ++---- .../nasl/builtin/cryptographic/aes_cmac.rs | 4 +- .../src/nasl/builtin/cryptographic/aes_ctr.rs | 14 +- .../src/nasl/builtin/cryptographic/aes_gcm.rs | 46 ++---- .../nasl/builtin/cryptographic/aes_gmac.rs | 2 +- rust/src/nasl/builtin/cryptographic/des.rs | 2 +- rust/src/nasl/builtin/cryptographic/hash.rs | 16 +- rust/src/nasl/builtin/cryptographic/hmac.rs | 18 +- .../builtin/cryptographic/tests/aes_gcm.rs | 2 +- rust/src/nasl/builtin/description/mod.rs | 8 +- rust/src/nasl/builtin/error.rs | 11 +- rust/src/nasl/builtin/host/mod.rs | 17 +- rust/src/nasl/builtin/http/mod.rs | 33 ++-- rust/src/nasl/builtin/isotime/mod.rs | 4 +- rust/src/nasl/builtin/knowledge_base/mod.rs | 10 +- rust/src/nasl/builtin/network/mod.rs | 2 +- rust/src/nasl/builtin/network/network.rs | 8 +- rust/src/nasl/builtin/network/socket.rs | 14 +- rust/src/nasl/builtin/raw_ip/frame_forgery.rs | 48 +++--- .../src/nasl/builtin/raw_ip/packet_forgery.rs | 156 +++++++----------- rust/src/nasl/builtin/raw_ip/raw_ip_utils.rs | 8 +- .../builtin/raw_ip/tests/packet_forgery.rs | 12 +- rust/src/nasl/builtin/regex/mod.rs | 8 +- rust/src/nasl/builtin/report_functions/mod.rs | 8 +- rust/src/nasl/builtin/ssh/mod.rs | 4 +- rust/src/nasl/builtin/ssh/utils.rs | 8 +- rust/src/nasl/builtin/string/mod.rs | 10 +- rust/src/nasl/interpreter/error.rs | 10 +- rust/src/nasl/mod.rs | 3 +- rust/src/nasl/test_utils.rs | 10 +- rust/src/nasl/utils/error.rs | 63 ++++--- .../nasl/utils/function/from_nasl_value.rs | 22 +-- rust/src/nasl/utils/function/maybe.rs | 4 +- rust/src/nasl/utils/function/positionals.rs | 10 +- .../src/nasl/utils/function/to_nasl_result.rs | 12 +- rust/src/nasl/utils/function/types.rs | 2 +- rust/src/nasl/utils/function/utils.rs | 16 +- rust/src/nasl/utils/mod.rs | 4 +- 39 files changed, 299 insertions(+), 396 deletions(-) diff --git a/rust/src/nasl/builtin/cryptographic/aes_cbc.rs b/rust/src/nasl/builtin/cryptographic/aes_cbc.rs index 610dc08fe..036870624 100644 --- a/rust/src/nasl/builtin/cryptographic/aes_cbc.rs +++ b/rust/src/nasl/builtin/cryptographic/aes_cbc.rs @@ -17,7 +17,7 @@ use crate::nasl::prelude::*; use super::{get_data, get_iv, get_key, get_len, Crypt}; /// Base function for en- and decrypting Cipher Block Chaining (CBC) mode -fn cbc(register: &Register, crypt: Crypt) -> Result +fn cbc(register: &Register, crypt: Crypt) -> Result where D: BlockCipher + BlockEncrypt + BlockDecrypt + KeyInit, { @@ -71,7 +71,7 @@ where /// Currently the data is filled with zeroes. Therefore the length of the encrypted data must be /// known for decryption. If no length is given, the last block is decrypted as a whole. /// - The iv must have a length of 16 bytes -fn aes128_cbc_encrypt(register: &Register, _: &Context) -> Result { +fn aes128_cbc_encrypt(register: &Register, _: &Context) -> Result { cbc::(register, Crypt::Encrypt) } @@ -83,7 +83,7 @@ fn aes128_cbc_encrypt(register: &Register, _: &Context) -> Result Result { +fn aes128_cbc_decrypt(register: &Register, _: &Context) -> Result { cbc::(register, Crypt::Decrypt) } @@ -94,7 +94,7 @@ fn aes128_cbc_decrypt(register: &Register, _: &Context) -> Result Result { +fn aes192_cbc_encrypt(register: &Register, _: &Context) -> Result { cbc::(register, Crypt::Encrypt) } @@ -106,7 +106,7 @@ fn aes192_cbc_encrypt(register: &Register, _: &Context) -> Result Result { +fn aes192_cbc_decrypt(register: &Register, _: &Context) -> Result { cbc::(register, Crypt::Decrypt) } @@ -117,7 +117,7 @@ fn aes192_cbc_decrypt(register: &Register, _: &Context) -> Result Result { +fn aes256_cbc_encrypt(register: &Register, _: &Context) -> Result { cbc::(register, Crypt::Encrypt) } @@ -129,7 +129,7 @@ fn aes256_cbc_encrypt(register: &Register, _: &Context) -> Result Result { +fn aes256_cbc_decrypt(register: &Register, _: &Context) -> Result { cbc::(register, Crypt::Decrypt) } diff --git a/rust/src/nasl/builtin/cryptographic/aes_ccm.rs b/rust/src/nasl/builtin/cryptographic/aes_ccm.rs index 9785ba1ed..85714f9ee 100644 --- a/rust/src/nasl/builtin/cryptographic/aes_ccm.rs +++ b/rust/src/nasl/builtin/cryptographic/aes_ccm.rs @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception -use crate::nasl::utils::error::FunctionErrorKind; +use crate::nasl::utils::error::NaslFnError; use aes::cipher::{BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser}; use aes::{Aes128, Aes192, Aes256}; use ccm::{ @@ -41,7 +41,7 @@ where } /// Base function for ccm en- and decryption. Sets the tag length to 16. -fn ccm(register: &Register, crypt: Crypt, auth: bool) -> Result +fn ccm(register: &Register, crypt: Crypt, auth: bool) -> Result where D: BlockCipher + BlockSizeUser + BlockEncrypt + BlockDecrypt + KeyInit, { @@ -71,7 +71,7 @@ where /// - The length of the key should be 16 bytes long /// - The iv must have a length of 7-13 bytes /// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16 -fn aes128_ccm_encrypt(register: &Register, _: &Context) -> Result { +fn aes128_ccm_encrypt(register: &Register, _: &Context) -> Result { ccm::(register, Crypt::Encrypt, false) } @@ -82,10 +82,7 @@ fn aes128_ccm_encrypt(register: &Register, _: &Context) -> Result Result { +fn aes128_ccm_encrypt_auth(register: &Register, _: &Context) -> Result { ccm::(register, Crypt::Encrypt, true) } @@ -96,7 +93,7 @@ fn aes128_ccm_encrypt_auth( /// - The length of the key should be 16 bytes long /// - The iv must have a length of 7-13 bytes /// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16 -fn aes128_ccm_decrypt(register: &Register, _: &Context) -> Result { +fn aes128_ccm_decrypt(register: &Register, _: &Context) -> Result { ccm::(register, Crypt::Decrypt, false) } @@ -107,10 +104,7 @@ fn aes128_ccm_decrypt(register: &Register, _: &Context) -> Result Result { +fn aes128_ccm_decrypt_auth(register: &Register, _: &Context) -> Result { ccm::(register, Crypt::Decrypt, true) } @@ -121,7 +115,7 @@ fn aes128_ccm_decrypt_auth( /// - The length of the key should be 16 bytes long /// - The iv must have a length of 7-13 bytes /// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16 -fn aes192_ccm_encrypt(register: &Register, _: &Context) -> Result { +fn aes192_ccm_encrypt(register: &Register, _: &Context) -> Result { ccm::(register, Crypt::Encrypt, false) } @@ -132,10 +126,7 @@ fn aes192_ccm_encrypt(register: &Register, _: &Context) -> Result Result { +fn aes192_ccm_encrypt_auth(register: &Register, _: &Context) -> Result { ccm::(register, Crypt::Encrypt, true) } @@ -146,7 +137,7 @@ fn aes192_ccm_encrypt_auth( /// - The length of the key should be 16 bytes long /// - The iv must have a length of 7-13 bytes /// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16 -fn aes192_ccm_decrypt(register: &Register, _: &Context) -> Result { +fn aes192_ccm_decrypt(register: &Register, _: &Context) -> Result { ccm::(register, Crypt::Decrypt, false) } @@ -157,10 +148,7 @@ fn aes192_ccm_decrypt(register: &Register, _: &Context) -> Result Result { +fn aes192_ccm_decrypt_auth(register: &Register, _: &Context) -> Result { ccm::(register, Crypt::Decrypt, true) } @@ -171,7 +159,7 @@ fn aes192_ccm_decrypt_auth( /// - The length of the key should be 16 bytes long /// - The iv must have a length of 7-13 bytes /// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16 -fn aes256_ccm_encrypt(register: &Register, _: &Context) -> Result { +fn aes256_ccm_encrypt(register: &Register, _: &Context) -> Result { ccm::(register, Crypt::Encrypt, false) } @@ -182,10 +170,7 @@ fn aes256_ccm_encrypt(register: &Register, _: &Context) -> Result Result { +fn aes256_ccm_encrypt_auth(register: &Register, _: &Context) -> Result { ccm::(register, Crypt::Encrypt, true) } @@ -196,7 +181,7 @@ fn aes256_ccm_encrypt_auth( /// - The length of the key should be 16 bytes long /// - The iv must have a length of 7-13 bytes /// - The tag_size default is 16, it can be set to either 4, 6, 8, 10, 12, 14 or 16 -fn aes256_ccm_decrypt(register: &Register, _: &Context) -> Result { +fn aes256_ccm_decrypt(register: &Register, _: &Context) -> Result { ccm::(register, Crypt::Decrypt, false) } @@ -207,16 +192,13 @@ fn aes256_ccm_decrypt(register: &Register, _: &Context) -> Result Result { +fn aes256_ccm_decrypt_auth(register: &Register, _: &Context) -> Result { ccm::(register, Crypt::Decrypt, true) } macro_rules! ccm_call_typed { ($(($t1s: expr, $t1: ty) => $(($t2s: expr, $t2: ty)),*);*) => { - fn ccm_typed(tag_size: usize, iv_size: usize, crypt: Crypt, key: &[u8], nonce: &[u8], data: &[u8], aad: &[u8]) -> Result, aError>, FunctionErrorKind> + fn ccm_typed(tag_size: usize, iv_size: usize, crypt: Crypt, key: &[u8], nonce: &[u8], data: &[u8], aad: &[u8]) -> Result, aError>, NaslFnError> where D: BlockCipher + BlockSizeUser + BlockEncrypt + BlockDecrypt + KeyInit { match tag_size { @@ -228,11 +210,11 @@ macro_rules! ccm_call_typed { Ok(ccm_crypt::(crypt, key, nonce, data, aad)) } ),* - other => Err(FunctionErrorKind::wrong_unnamed_argument("iv must be between 7 and 13", other.to_string().as_str())) + other => Err(NaslFnError::wrong_unnamed_argument("iv must be between 7 and 13", other.to_string().as_str())) } } ),* - other => Err(FunctionErrorKind::wrong_unnamed_argument("tag_size must be 4, 6, 8, 10, 12, 14 or 16", other.to_string().as_str())) + other => Err(NaslFnError::wrong_unnamed_argument("tag_size must be 4, 6, 8, 10, 12, 14 or 16", other.to_string().as_str())) } } } diff --git a/rust/src/nasl/builtin/cryptographic/aes_cmac.rs b/rust/src/nasl/builtin/cryptographic/aes_cmac.rs index 9e01cd544..4ff304bbb 100644 --- a/rust/src/nasl/builtin/cryptographic/aes_cmac.rs +++ b/rust/src/nasl/builtin/cryptographic/aes_cmac.rs @@ -3,7 +3,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception use crate::nasl::syntax::NaslValue; -use crate::nasl::utils::{Context, FunctionErrorKind, Register}; +use crate::nasl::utils::{Context, NaslFnError, Register}; use aes::Aes128; use cmac::{Cmac, Mac}; @@ -16,7 +16,7 @@ use super::{get_data, get_key, CryptographicError}; /// This function expects 2 named arguments key and data either in a string or data type. /// It is important to notice, that internally the CMAC algorithm is used and not, as the name /// suggests, CBC-MAC. -fn aes_cmac(register: &Register, _: &Context) -> Result { +fn aes_cmac(register: &Register, _: &Context) -> Result { let key = get_key(register)?; let data = get_data(register)?; diff --git a/rust/src/nasl/builtin/cryptographic/aes_ctr.rs b/rust/src/nasl/builtin/cryptographic/aes_ctr.rs index 7d46c7495..36f6e6adc 100644 --- a/rust/src/nasl/builtin/cryptographic/aes_ctr.rs +++ b/rust/src/nasl/builtin/cryptographic/aes_ctr.rs @@ -15,7 +15,7 @@ use crate::nasl::prelude::*; use super::{get_data, get_iv, get_key, get_len, Crypt}; -fn ctr(register: &Register, crypt: Crypt) -> Result +fn ctr(register: &Register, crypt: Crypt) -> Result where D: BlockSizeUser + aes::cipher::KeyInit @@ -56,7 +56,7 @@ where /// Currently the data is filled with zeroes. Therefore the length of the encrypted data must be /// known for decryption. If no length is given, the last block is decrypted as a whole. /// - The iv must have a length of 16 bytes. It is used as the initial counter. -fn aes128_ctr_encrypt(register: &Register, _: &Context) -> Result { +fn aes128_ctr_encrypt(register: &Register, _: &Context) -> Result { ctr::(register, Crypt::Encrypt) } @@ -68,7 +68,7 @@ fn aes128_ctr_encrypt(register: &Register, _: &Context) -> Result Result { +fn aes128_ctr_decrypt(register: &Register, _: &Context) -> Result { ctr::(register, Crypt::Decrypt) } @@ -79,7 +79,7 @@ fn aes128_ctr_decrypt(register: &Register, _: &Context) -> Result Result { +fn aes192_ctr_encrypt(register: &Register, _: &Context) -> Result { ctr::(register, Crypt::Encrypt) } @@ -91,7 +91,7 @@ fn aes192_ctr_encrypt(register: &Register, _: &Context) -> Result Result { +fn aes192_ctr_decrypt(register: &Register, _: &Context) -> Result { ctr::(register, Crypt::Decrypt) } @@ -102,7 +102,7 @@ fn aes192_ctr_decrypt(register: &Register, _: &Context) -> Result Result { +fn aes256_ctr_encrypt(register: &Register, _: &Context) -> Result { ctr::(register, Crypt::Encrypt) } @@ -114,7 +114,7 @@ fn aes256_ctr_encrypt(register: &Register, _: &Context) -> Result Result { +fn aes256_ctr_decrypt(register: &Register, _: &Context) -> Result { ctr::(register, Crypt::Decrypt) } diff --git a/rust/src/nasl/builtin/cryptographic/aes_gcm.rs b/rust/src/nasl/builtin/cryptographic/aes_gcm.rs index 9d8fbf436..95456d589 100644 --- a/rust/src/nasl/builtin/cryptographic/aes_gcm.rs +++ b/rust/src/nasl/builtin/cryptographic/aes_gcm.rs @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception -// FunctionErrorKind::GeneralError +// NaslFnError::GeneralError use crate::nasl::prelude::*; use aes::{ cipher::{BlockCipher, BlockDecrypt, BlockEncrypt, BlockSizeUser, KeyInit}, @@ -16,7 +16,7 @@ use digest::typenum::{U12, U16}; use super::{get_aad, get_data, get_iv, get_key, get_len, Crypt, CryptographicError}; -fn gcm(register: &Register, crypt: Crypt, auth: bool) -> Result +fn gcm(register: &Register, crypt: Crypt, auth: bool) -> Result where D: BlockSizeUser + aes::cipher::KeyInit @@ -76,7 +76,7 @@ where /// - The iv must have a length of 16 bytes. It is used as the initial counter. /// - The result contains the ciphertext and the calculated tag in a single data type. /// - The tag has a size of 16 Bytes. -fn aes128_gcm_encrypt(register: &Register, _: &Context) -> Result { +fn aes128_gcm_encrypt(register: &Register, _: &Context) -> Result { gcm::(register, Crypt::Encrypt, false) } @@ -89,10 +89,7 @@ fn aes128_gcm_encrypt(register: &Register, _: &Context) -> Result Result { +fn aes128_gcm_encrypt_auth(register: &Register, _: &Context) -> Result { gcm::(register, Crypt::Encrypt, true) } @@ -105,7 +102,7 @@ fn aes128_gcm_encrypt_auth( /// known for decryption. If no length is given, the last block is decrypted as a whole. /// - The iv must have a length of 16 bytes. It is used as the initial counter. /// - The tag is needed as a postfix in the given data in order to decrypt successfully. -fn aes128_gcm_decrypt(register: &Register, _: &Context) -> Result { +fn aes128_gcm_decrypt(register: &Register, _: &Context) -> Result { gcm::(register, Crypt::Decrypt, false) } @@ -118,10 +115,7 @@ fn aes128_gcm_decrypt(register: &Register, _: &Context) -> Result Result { +fn aes128_gcm_decrypt_auth(register: &Register, _: &Context) -> Result { gcm::(register, Crypt::Decrypt, true) } @@ -134,7 +128,7 @@ fn aes128_gcm_decrypt_auth( /// - The iv must have a length of 16 bytes. It is used as the initial counter. /// - The result contains the ciphertext and the calculated tag in a single data type. /// - The tag has a size of 16 Bytes. -fn aes192_gcm_encrypt(register: &Register, _: &Context) -> Result { +fn aes192_gcm_encrypt(register: &Register, _: &Context) -> Result { gcm::(register, Crypt::Encrypt, false) } @@ -147,10 +141,7 @@ fn aes192_gcm_encrypt(register: &Register, _: &Context) -> Result Result { +fn aes192_gcm_encrypt_auth(register: &Register, _: &Context) -> Result { gcm::(register, Crypt::Encrypt, true) } @@ -163,7 +154,7 @@ fn aes192_gcm_encrypt_auth( /// known for decryption. If no length is given, the last block is decrypted as a whole. /// - The iv must have a length of 16 bytes. It is used as the initial counter. /// - The tag is needed as a postfix in the given data in order to decrypt successfully. -fn aes192_gcm_decrypt(register: &Register, _: &Context) -> Result { +fn aes192_gcm_decrypt(register: &Register, _: &Context) -> Result { gcm::(register, Crypt::Decrypt, false) } @@ -176,10 +167,7 @@ fn aes192_gcm_decrypt(register: &Register, _: &Context) -> Result Result { +fn aes192_gcm_decrypt_auth(register: &Register, _: &Context) -> Result { gcm::(register, Crypt::Decrypt, true) } @@ -192,7 +180,7 @@ fn aes192_gcm_decrypt_auth( /// - The iv must have a length of 16 bytes. It is used as the initial counter. /// - The result contains the ciphertext and the calculated tag in a single data type. /// - The tag has a size of 16 Bytes. -fn aes256_gcm_encrypt(register: &Register, _: &Context) -> Result { +fn aes256_gcm_encrypt(register: &Register, _: &Context) -> Result { gcm::(register, Crypt::Encrypt, false) } @@ -205,10 +193,7 @@ fn aes256_gcm_encrypt(register: &Register, _: &Context) -> Result Result { +fn aes256_gcm_encrypt_auth(register: &Register, _: &Context) -> Result { gcm::(register, Crypt::Encrypt, true) } @@ -221,7 +206,7 @@ fn aes256_gcm_encrypt_auth( /// known for decryption. If no length is given, the last block is decrypted as a whole. /// - The iv must have a length of 16 bytes. It is used as the initial counter. /// - The tag is needed as a postfix in the given data in order to decrypt successfully. -fn aes256_gcm_decrypt(register: &Register, _: &Context) -> Result { +fn aes256_gcm_decrypt(register: &Register, _: &Context) -> Result { gcm::(register, Crypt::Decrypt, false) } @@ -234,10 +219,7 @@ fn aes256_gcm_decrypt(register: &Register, _: &Context) -> Result Result { +fn aes256_gcm_decrypt_auth(register: &Register, _: &Context) -> Result { gcm::(register, Crypt::Decrypt, true) } diff --git a/rust/src/nasl/builtin/cryptographic/aes_gmac.rs b/rust/src/nasl/builtin/cryptographic/aes_gmac.rs index 03147c1f8..4b586aad5 100644 --- a/rust/src/nasl/builtin/cryptographic/aes_gmac.rs +++ b/rust/src/nasl/builtin/cryptographic/aes_gmac.rs @@ -10,7 +10,7 @@ use crate::nasl::prelude::*; /// /// This function expects 3 named arguments key, data and iv either in a string or data type. #[cfg(feature = "nasl-c-lib")] -fn aes_gmac(register: &Register, _: &Context) -> Result { +fn aes_gmac(register: &Register, _: &Context) -> Result { use super::{get_data, get_iv, get_key, CryptographicError}; use nasl_c_lib::cryptographic::mac::aes_gmac; diff --git a/rust/src/nasl/builtin/cryptographic/des.rs b/rust/src/nasl/builtin/cryptographic/des.rs index 7e1f26020..4ec0df44a 100644 --- a/rust/src/nasl/builtin/cryptographic/des.rs +++ b/rust/src/nasl/builtin/cryptographic/des.rs @@ -7,7 +7,7 @@ use aes::cipher::BlockEncrypt; use ccm::KeyInit; use des::cipher::generic_array::GenericArray; -fn encrypt_des(register: &Register, _: &Context) -> Result { +fn encrypt_des(register: &Register, _: &Context) -> Result { let positional = register.positional(); if positional.len() != 2 { return Err(ArgumentError::MissingPositionals { diff --git a/rust/src/nasl/builtin/cryptographic/hash.rs b/rust/src/nasl/builtin/cryptographic/hash.rs index 5be8a996a..9ecb9c9d0 100644 --- a/rust/src/nasl/builtin/cryptographic/hash.rs +++ b/rust/src/nasl/builtin/cryptographic/hash.rs @@ -13,7 +13,7 @@ use sha2::{Sha256, Sha512}; use crate::nasl::prelude::*; use crate::nasl::utils::function::StringOrData; -fn nasl_hash(data: Option) -> Result +fn nasl_hash(data: Option) -> Result where D::OutputSize: std::ops::Add, ::Output: digest::generic_array::ArrayLength, @@ -29,43 +29,43 @@ where /// NASL function to get MD2 hash #[nasl_function] -pub fn hash_md2(data: Option) -> Result { +pub fn hash_md2(data: Option) -> Result { nasl_hash::(data) } /// NASL function to get MD4 hash #[nasl_function] -pub fn hash_md4(data: Option) -> Result { +pub fn hash_md4(data: Option) -> Result { nasl_hash::(data) } /// NASL function to get MD5 hash #[nasl_function] -pub fn hash_md5(data: Option) -> Result { +pub fn hash_md5(data: Option) -> Result { nasl_hash::(data) } /// NASL function to get SHA1 hash #[nasl_function] -pub fn hash_sha1(data: Option) -> Result { +pub fn hash_sha1(data: Option) -> Result { nasl_hash::(data) } /// NASL function to get SHA256 hash #[nasl_function] -pub fn hash_sha256(data: Option) -> Result { +pub fn hash_sha256(data: Option) -> Result { nasl_hash::(data) } /// NASL function to get SHA512 hash #[nasl_function] -pub fn hash_sha512(data: Option) -> Result { +pub fn hash_sha512(data: Option) -> Result { nasl_hash::(data) } /// NASL function to get RIPemd160 hash #[nasl_function] -pub fn hash_ripemd160(data: Option) -> Result { +pub fn hash_ripemd160(data: Option) -> Result { nasl_hash::(data) } diff --git a/rust/src/nasl/builtin/cryptographic/hmac.rs b/rust/src/nasl/builtin/cryptographic/hmac.rs index aefbc05ec..de037b7b5 100644 --- a/rust/src/nasl/builtin/cryptographic/hmac.rs +++ b/rust/src/nasl/builtin/cryptographic/hmac.rs @@ -19,7 +19,7 @@ use sha2::{Sha256, Sha384, Sha512}; use crate::nasl::prelude::*; -fn hmac(key: &str, data: &str) -> Result +fn hmac(key: &str, data: &str) -> Result where D: CoreProxy, D::Core: HashMarker @@ -34,7 +34,7 @@ where let mut hmac = match Hmac::::new_from_slice(key.as_bytes()) { Ok(x) => x, Err(InvalidLength) => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "valid size key", "invalid size key", )) @@ -48,43 +48,43 @@ where /// NASL function to get HMAC MD2 string #[nasl_function(named(key, data))] -pub fn hmac_md2(key: &str, data: &str) -> Result { +pub fn hmac_md2(key: &str, data: &str) -> Result { hmac::(key, data) } /// NASL function to get HMAC MD5 string #[nasl_function(named(key, data))] -pub fn hmac_md5(key: &str, data: &str) -> Result { +pub fn hmac_md5(key: &str, data: &str) -> Result { hmac::(key, data) } /// NASL function to get HMAC RIPEMD160 string #[nasl_function(named(key, data))] -pub fn hmac_ripemd160(key: &str, data: &str) -> Result { +pub fn hmac_ripemd160(key: &str, data: &str) -> Result { hmac::(key, data) } /// NASL function to get HMAC SHA1 string #[nasl_function(named(key, data))] -pub fn hmac_sha1(key: &str, data: &str) -> Result { +pub fn hmac_sha1(key: &str, data: &str) -> Result { hmac::(key, data) } /// NASL function to get HMAC SHA256 string #[nasl_function(named(key, data))] -pub fn hmac_sha256(key: &str, data: &str) -> Result { +pub fn hmac_sha256(key: &str, data: &str) -> Result { hmac::(key, data) } /// NASL function to get HMAC SHA384 string #[nasl_function(named(key, data))] -pub fn hmac_sha384(key: &str, data: &str) -> Result { +pub fn hmac_sha384(key: &str, data: &str) -> Result { hmac::(key, data) } /// NASL function to get HMAC SHA512 string #[nasl_function(named(key, data))] -pub fn hmac_sha512(key: &str, data: &str) -> Result { +pub fn hmac_sha512(key: &str, data: &str) -> Result { hmac::(key, data) } diff --git a/rust/src/nasl/builtin/cryptographic/tests/aes_gcm.rs b/rust/src/nasl/builtin/cryptographic/tests/aes_gcm.rs index 28b663ef8..0e9bed486 100644 --- a/rust/src/nasl/builtin/cryptographic/tests/aes_gcm.rs +++ b/rust/src/nasl/builtin/cryptographic/tests/aes_gcm.rs @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception -// FunctionErrorKind::GeneralError +// NaslFnError::GeneralError use super::helper::decode_hex; use crate::nasl::test_prelude::*; diff --git a/rust/src/nasl/builtin/description/mod.rs b/rust/src/nasl/builtin/description/mod.rs index 66f80cac3..ce4b048a0 100644 --- a/rust/src/nasl/builtin/description/mod.rs +++ b/rust/src/nasl/builtin/description/mod.rs @@ -25,7 +25,7 @@ use crate::nasl::utils::get_named_parameter; ///} /// ```` /// The first parameter is the name of the function as well as the &str lookup key. -/// Afterwards a method that transform `&[&NaslValue]` to `Result` must be defined. +/// Afterwards a method that transform `&[&NaslValue]` to `Result` must be defined. /// /// Parameter are separated from the definition by a `=>`. /// @@ -59,7 +59,7 @@ macro_rules! make_storage_function { pub fn $name( registrat: &Register, ctxconfigs: &Context, - ) -> Result { + ) -> Result { let mut variables = vec![]; $( let positional = registrat.positional(); @@ -106,7 +106,7 @@ macro_rules! make_storage_function { }; } -type Transform = Result, FunctionErrorKind>; +type Transform = Result, NaslFnError>; fn as_timeout_field(_: &ContextKey, arguments: &[&NaslValue]) -> Transform { Ok(vec![NVTField::Preference(NvtPreference { @@ -120,7 +120,7 @@ fn as_timeout_field(_: &ContextKey, arguments: &[&NaslValue]) -> Transform { fn as_category_field(_: &ContextKey, arguments: &[&NaslValue]) -> Transform { match arguments[0] { NaslValue::AttackCategory(cat) => Ok(vec![NVTField::Category(*cat)]), - a => Err(FunctionErrorKind::wrong_unnamed_argument( + a => Err(NaslFnError::wrong_unnamed_argument( "AttackCategory", &a.to_string(), )), diff --git a/rust/src/nasl/builtin/error.rs b/rust/src/nasl/builtin/error.rs index 3a607b353..ddaa6a0d1 100644 --- a/rust/src/nasl/builtin/error.rs +++ b/rust/src/nasl/builtin/error.rs @@ -1,6 +1,7 @@ use thiserror::Error; use crate::nasl::prelude::*; +use crate::nasl::utils::error::NaslFnErrorKind; use super::cryptographic::CryptographicError; use super::http::HttpError; @@ -45,18 +46,18 @@ macro_rules! builtin_error_variant ( } } - impl From<$err> for FunctionErrorKind { + impl From<$err> for NaslFnError { fn from(value: $err) -> Self { - FEK::Builtin(BuiltinError::$variant(value).into()).into() + NaslFnErrorKind::Builtin(BuiltinError::$variant(value).into()).into() } } - impl TryFrom for $err { + impl TryFrom for $err { type Error = (); - fn try_from(value: FunctionErrorKind) -> Result { + fn try_from(value: NaslFnError) -> Result { match value.kind { - FEK::Builtin( + NaslFnErrorKind::Builtin( BuiltinError::$variant(e) ) => Ok(e), _ => Err(()), diff --git a/rust/src/nasl/builtin/host/mod.rs b/rust/src/nasl/builtin/host/mod.rs index 8a941ea79..eea3f5db4 100644 --- a/rust/src/nasl/builtin/host/mod.rs +++ b/rust/src/nasl/builtin/host/mod.rs @@ -8,7 +8,7 @@ mod tests; use std::{net::IpAddr, str::FromStr}; use crate::function_set; -use crate::nasl::utils::{error::FunctionErrorKind, lookup_keys::TARGET}; +use crate::nasl::utils::{error::NaslFnError, lookup_keys::TARGET}; use crate::nasl::syntax::NaslValue; use crate::nasl::utils::{Context, ContextType, Register}; @@ -17,7 +17,7 @@ use crate::nasl::utils::{Context, ContextType, Register}; /// /// It does lookup TARGET and when not found falls back to 127.0.0.1 to resolve. /// If the TARGET is not a IP address than we assume that it already is a fqdn or a hostname and will return that instead. -fn resolve_hostname(register: &Register) -> Result { +fn resolve_hostname(register: &Register) -> Result { use std::net::ToSocketAddrs; let default_ip = "127.0.0.1"; @@ -41,7 +41,7 @@ fn resolve_hostname(register: &Register) -> Result { /// /// As of now (2023-01-20) there is no vhost handling. /// Therefore this function does load the registered TARGET and if it is an IP Address resolves it via DNS instead. -fn get_host_names(register: &Register, _: &Context) -> Result { +fn get_host_names(register: &Register, _: &Context) -> Result { resolve_hostname(register).map(|x| NaslValue::Array(vec![NaslValue::String(x)])) } @@ -49,12 +49,12 @@ fn get_host_names(register: &Register, _: &Context) -> Result Result { +fn get_host_name(register: &Register, _: &Context) -> Result { resolve_hostname(register).map(NaslValue::String) } /// Return the target's IP address as IpAddr. -pub fn get_host_ip(context: &Context) -> Result { +pub fn get_host_ip(context: &Context) -> Result { let default_ip = "127.0.0.1"; let r_sock_addr = match context.target() { x if !x.is_empty() => IpAddr::from_str(x), @@ -63,7 +63,7 @@ pub fn get_host_ip(context: &Context) -> Result { match r_sock_addr { Ok(x) => Ok(x), - Err(e) => Err(FunctionErrorKind::wrong_unnamed_argument( + Err(e) => Err(NaslFnError::wrong_unnamed_argument( "IP address", e.to_string().as_str(), )), @@ -71,10 +71,7 @@ pub fn get_host_ip(context: &Context) -> Result { } /// Return the target's IP address or 127.0.0.1 if not set. -fn nasl_get_host_ip( - _register: &Register, - context: &Context, -) -> Result { +fn nasl_get_host_ip(_register: &Register, context: &Context) -> Result { let ip = get_host_ip(context)?; Ok(NaslValue::String(ip.to_string())) } diff --git a/rust/src/nasl/builtin/http/mod.rs b/rust/src/nasl/builtin/http/mod.rs index 1348938fd..fa1cd183b 100644 --- a/rust/src/nasl/builtin/http/mod.rs +++ b/rust/src/nasl/builtin/http/mod.rs @@ -38,9 +38,9 @@ pub struct NaslHttp { async fn lock_handles( handles: &Arc>>, -) -> Result>, FunctionErrorKind> { +) -> Result>, NaslFnError> { // we actually need to panic as a lock error is fatal - // alternatively we need to add a poison error on FunctionErrorKind + // alternatively we need to add a poison error on NaslFnError Ok(Arc::as_ref(handles).lock().await) } @@ -204,7 +204,7 @@ impl NaslHttp { register: &Register, ctx: &Context<'a>, method: Method, - ) -> Result { + ) -> Result { let handle_id = match register.named("handle") { Some(ContextType::Value(NaslValue::Number(x))) => *x as i32, _ => return Err(ArgumentError::WrongArgument("Invalid handle ID".to_string()).into()), @@ -220,7 +220,7 @@ impl NaslHttp { let item: String = register .named("item") .map(|x| x.to_string()) - .ok_or(FunctionErrorKind::missing_argument("item"))?; + .ok_or(NaslFnError::missing_argument("item"))?; let schema: String = match register.named("schema") { Some(x) => { @@ -281,7 +281,7 @@ impl NaslHttp { &self, register: &Register, ctx: &Context<'a>, - ) -> Result { + ) -> Result { self.http2_req(register, ctx, Method::GET).await } @@ -290,7 +290,7 @@ impl NaslHttp { &self, register: &Register, ctx: &Context<'a>, - ) -> Result { + ) -> Result { self.http2_req(register, ctx, Method::POST).await } @@ -299,7 +299,7 @@ impl NaslHttp { &self, register: &Register, ctx: &Context<'a>, - ) -> Result { + ) -> Result { self.http2_req(register, ctx, Method::PUT).await } @@ -308,7 +308,7 @@ impl NaslHttp { &self, register: &Register, ctx: &Context<'a>, - ) -> Result { + ) -> Result { self.http2_req(register, ctx, Method::HEAD).await } @@ -317,7 +317,7 @@ impl NaslHttp { &self, register: &Register, ctx: &Context<'a>, - ) -> Result { + ) -> Result { self.http2_req(register, ctx, Method::DELETE).await } @@ -328,7 +328,7 @@ impl NaslHttp { /// On success the function returns a and integer with the handle /// identifier. Null on error. #[nasl_function] - async fn handle(&self) -> Result { + async fn handle(&self) -> Result { let mut handles = lock_handles(&self.handles).await?; let handle_id = next_handle_id(&handles); let h = Handle { @@ -348,7 +348,7 @@ impl NaslHttp { /// The function returns an integer. /// O on success, -1 on error. #[nasl_function(named(handle))] - async fn close_handle(&self, handle: i32) -> Result { + async fn close_handle(&self, handle: i32) -> Result { let mut handles = lock_handles(&self.handles).await?; match handles .iter_mut() @@ -359,10 +359,7 @@ impl NaslHttp { handles.remove(i); Ok(NaslValue::Number(0)) } - _ => { - Err(FunctionErrorKind::from(HttpError::HandleIdNotFound(handle)) - .with(ReturnValue(-1))) - } + _ => Err(NaslFnError::from(HttpError::HandleIdNotFound(handle)).with(ReturnValue(-1))), } } @@ -376,7 +373,7 @@ impl NaslHttp { &self, register: &Register, _: &Context<'_>, - ) -> Result { + ) -> Result { let handle_id = match register.named("handle") { Some(ContextType::Value(NaslValue::Number(x))) => *x as i32, _ => { @@ -405,10 +402,10 @@ impl NaslHttp { &self, register: &Register, _: &Context<'_>, - ) -> Result { + ) -> Result { let header_item = match register.named("header_item") { Some(ContextType::Value(NaslValue::String(x))) => x, - _ => return Err(FunctionErrorKind::missing_argument("No command passed")), + _ => return Err(NaslFnError::missing_argument("No command passed")), }; let (key, val) = header_item.split_once(": ").expect("Missing header_item"); diff --git a/rust/src/nasl/builtin/isotime/mod.rs b/rust/src/nasl/builtin/isotime/mod.rs index 4b830423f..1da23e4a3 100644 --- a/rust/src/nasl/builtin/isotime/mod.rs +++ b/rust/src/nasl/builtin/isotime/mod.rs @@ -105,12 +105,12 @@ fn isotime_now() -> String { } #[nasl_function] -fn isotime_print(time: &str) -> Result { +fn isotime_print(time: &str) -> Result { Ok(parse_time(time)?.format("%Y-%m-%d %H:%M:%S").to_string()) } #[nasl_function] -fn isotime_scan(time: &str) -> Result { +fn isotime_scan(time: &str) -> Result { let time = parse_time(time)?; Ok(time.format("%Y%m%dT%H%M%S").to_string()) diff --git a/rust/src/nasl/builtin/knowledge_base/mod.rs b/rust/src/nasl/builtin/knowledge_base/mod.rs index a9ec656df..815e1eb5b 100644 --- a/rust/src/nasl/builtin/knowledge_base/mod.rs +++ b/rust/src/nasl/builtin/knowledge_base/mod.rs @@ -9,7 +9,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; use crate::function_set; use crate::nasl::syntax::NaslValue; -use crate::nasl::utils::error::FunctionErrorKind; +use crate::nasl::utils::error::NaslFnError; use crate::nasl::utils::Context; use crate::storage::{Field, Kb, Retrieve}; use nasl_function_proc_macro::nasl_function; @@ -27,7 +27,7 @@ fn set_kb_item( name: NaslValue, value: NaslValue, expires: Option, -) -> Result { +) -> Result { let expires = match expires { Some(NaslValue::Number(x)) => Some(x), Some(NaslValue::Exit(0)) => None, @@ -58,7 +58,7 @@ fn set_kb_item( /// NASL function to get a knowledge base #[nasl_function] -fn get_kb_item(c: &Context, key: &str) -> Result { +fn get_kb_item(c: &Context, key: &str) -> Result { c.retriever() .retrieve(c.key(), Retrieve::KB(key.to_string())) .map(|r| { @@ -79,7 +79,7 @@ fn replace_kb_item( c: &Context, name: NaslValue, value: NaslValue, -) -> Result { +) -> Result { c.dispatcher() .dispatch_replace( c.key(), @@ -95,7 +95,7 @@ fn replace_kb_item( /// NASL function to retrieve an item in a KB. #[nasl_function] -fn get_kb_list(c: &Context, key: NaslValue) -> Result { +fn get_kb_list(c: &Context, key: NaslValue) -> Result { c.retriever() .retrieve(c.key(), Retrieve::KB(key.to_string())) .map(|r| { diff --git a/rust/src/nasl/builtin/network/mod.rs b/rust/src/nasl/builtin/network/mod.rs index 3a09e6e42..b7d7c814f 100644 --- a/rust/src/nasl/builtin/network/mod.rs +++ b/rust/src/nasl/builtin/network/mod.rs @@ -74,7 +74,7 @@ impl Display for OpenvasEncaps { } } -pub fn get_kb_item(context: &Context, name: &str) -> Result, FunctionErrorKind> { +pub fn get_kb_item(context: &Context, name: &str) -> Result, NaslFnError> { context .retriever() .retrieve(context.key(), Retrieve::KB(name.to_string())) diff --git a/rust/src/nasl/builtin/network/network.rs b/rust/src/nasl/builtin/network/network.rs index 33f3915bf..99686a9fa 100644 --- a/rust/src/nasl/builtin/network/network.rs +++ b/rust/src/nasl/builtin/network/network.rs @@ -11,7 +11,7 @@ use super::{ verify_port, DEFAULT_PORT, }; use crate::function_set; -use crate::nasl::utils::{Context, FunctionErrorKind}; +use crate::nasl::utils::{Context, NaslFnError}; use crate::storage::{types::Primitive, Field, Kb}; use nasl_function_proc_macro::nasl_function; @@ -132,11 +132,7 @@ fn islocalnet(context: &Context) -> Result { /// Declares an open port on the target host #[nasl_function(named(port, proto))] -fn scanner_add_port( - context: &Context, - port: i64, - proto: Option<&str>, -) -> Result<(), FunctionErrorKind> { +fn scanner_add_port(context: &Context, port: i64, proto: Option<&str>) -> Result<(), NaslFnError> { let port = verify_port(port)?; let protocol = proto.unwrap_or("tcp"); diff --git a/rust/src/nasl/builtin/network/socket.rs b/rust/src/nasl/builtin/network/socket.rs index cf81a770c..b3830ec4e 100644 --- a/rust/src/nasl/builtin/network/socket.rs +++ b/rust/src/nasl/builtin/network/socket.rs @@ -297,7 +297,7 @@ impl NaslSockets { data: &[u8], flags: Option, len: Option, - ) -> Result { + ) -> Result { let len = if let Some(len) = len { if len < 1 || len > data.len() { data.len() @@ -381,7 +381,7 @@ impl NaslSockets { len: usize, min: Option, timeout: Option, - ) -> Result { + ) -> Result { let min = min .map(|min| if min < 0 { len } else { min as usize }) .unwrap_or(len); @@ -473,7 +473,7 @@ impl NaslSockets { /// - Secret/kdc_port /// - Secret/kdc_use_tcp #[nasl_function] - fn open_sock_kdc(&self, context: &Context) -> Result { + fn open_sock_kdc(&self, context: &Context) -> Result { let hostname = match get_kb_item(context, "Secret/kdc_hostname")? { Some(x) => Ok(x.to_string()), None => Err(SocketError::Diagnostic( @@ -556,7 +556,7 @@ impl NaslSockets { bufsz: Option, // TODO: Extract information from custom priority string // priority: Option<&str>, - ) -> Result { + ) -> Result { // Get port let port = verify_port(port)?; let transport = transport.unwrap_or(-1); @@ -657,7 +657,7 @@ impl NaslSockets { bufsz: Option, timeout: Duration, tls_config: Option, - ) -> Result { + ) -> Result { let addr = ipstr2ipaddr(addr)?; let mut retry = super::get_kb_item(context, "timeout_retry")? .map(|val| match val { @@ -717,7 +717,7 @@ impl NaslSockets { bufsz: Option, timeout: Duration, hostname: &str, - ) -> Result { + ) -> Result { let cert_path = get_kb_item(context, "SSL/cert")? .ok_or(SocketError::Diagnostic( "unable to open TLS connection: kes 'SSL/cert' is missing".to_string(), @@ -796,7 +796,7 @@ impl NaslSockets { /// Open a UDP socket to the target host #[nasl_function] - fn open_sock_udp(&self, context: &Context, port: i64) -> Result { + fn open_sock_udp(&self, context: &Context, port: i64) -> Result { let port = verify_port(port)?; let addr = ipstr2ipaddr(context.target())?; diff --git a/rust/src/nasl/builtin/raw_ip/frame_forgery.rs b/rust/src/nasl/builtin/raw_ip/frame_forgery.rs index 0f27aff94..ee96d1a82 100644 --- a/rust/src/nasl/builtin/raw_ip/frame_forgery.rs +++ b/rust/src/nasl/builtin/raw_ip/frame_forgery.rs @@ -106,11 +106,11 @@ impl From<&Frame> for Vec { } impl TryFrom<&[u8]> for Frame { - type Error = FunctionErrorKind; + type Error = NaslFnError; fn try_from(f: &[u8]) -> Result { if f.len() < 14 { - Err(FunctionErrorKind::missing_argument("valid ip address")) + Err(NaslFnError::missing_argument("valid ip address")) } else { let mut frame = Frame::new(); frame.set_dsthaddr(MacAddr(f[0], f[1], f[2], f[3], f[4], f[5])); @@ -273,19 +273,18 @@ fn convert_vec_into_mac_address(v: &[u8]) -> Option { } } -fn validate_mac_address(v: Option<&ContextType>) -> Result { +fn validate_mac_address(v: Option<&ContextType>) -> Result { let mac_addr = match v { Some(ContextType::Value(NaslValue::String(x))) => MacAddr::from_str(x).ok(), Some(ContextType::Value(NaslValue::Data(x))) => convert_vec_into_mac_address(x), _ => None, }; - mac_addr.ok_or_else(|| { - FunctionErrorKind::wrong_unnamed_argument("mac address", "invalid mac address") - }) + mac_addr + .ok_or_else(|| NaslFnError::wrong_unnamed_argument("mac address", "invalid mac address")) } /// Return the MAC address, given the interface name -fn get_local_mac_address(name: &str) -> Result { +fn get_local_mac_address(name: &str) -> Result { interfaces() .into_iter() .find(|x| x.name == *name) @@ -295,7 +294,7 @@ fn get_local_mac_address(name: &str) -> Result { /// Return a frame given a capture device and a filter. It returns an empty frame in case /// there was no response or anything was filtered. -fn recv_frame(cap: &mut Capture, filter: &str) -> Result { +fn recv_frame(cap: &mut Capture, filter: &str) -> Result { let f = Frame::new(); let p = match cap.filter(filter, true) { @@ -315,7 +314,7 @@ fn send_frame( pcap_active: &bool, filter: Option<&String>, timeout: i32, -) -> Result, FunctionErrorKind> { +) -> Result, NaslFnError> { let mut capture_dev = match Capture::from_device(iface.clone()) { Ok(c) => match c.promisc(true).timeout(timeout).open() { Ok(mut capture) => match capture.sendpacket(frame) { @@ -348,15 +347,12 @@ fn send_frame( /// /// It takes the following argument: /// - cap_timeout: time to wait for answer in seconds, 5 by default -fn nasl_send_arp_request( - register: &Register, - context: &Context, -) -> Result { +fn nasl_send_arp_request(register: &Register, context: &Context) -> Result { let timeout = match register.named("pcap_timeout") { Some(ContextType::Value(NaslValue::Number(x))) => *x as i32 * 1000i32, // to milliseconds None => DEFAULT_TIMEOUT, _ => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "Integer", "Invalid timeout value", )) @@ -366,7 +362,7 @@ fn nasl_send_arp_request( let target_ip = get_host_ip(context)?; if target_ip.is_ipv6() { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "IPv4", "IPv6 does not support ARP protocol.", )); @@ -378,7 +374,7 @@ fn nasl_send_arp_request( let src_ip = match Ipv4Addr::from_str(&local_ip.to_string()) { Ok(x) => x, Err(_) => { - return Err(FunctionErrorKind::missing_argument( + return Err(NaslFnError::missing_argument( "Not possible to parse the src IP address.", )) } @@ -387,7 +383,7 @@ fn nasl_send_arp_request( let dst_ip = match Ipv4Addr::from_str(&target_ip.to_string()) { Ok(x) => x, Err(_) => { - return Err(FunctionErrorKind::missing_argument( + return Err(NaslFnError::missing_argument( "Not possible to parse the dst IP address.", )) } @@ -407,7 +403,7 @@ fn nasl_send_arp_request( fn nasl_get_local_mac_address_from_ip( register: &Register, _: &Context, -) -> Result { +) -> Result { let positional = register.positional(); if positional.is_empty() { return Err(ArgumentError::MissingPositionals { @@ -436,7 +432,7 @@ fn nasl_get_local_mac_address_from_ip( /// - ether_proto: is an int containing the ethernet type (normally given as hexadecimal). /// It is optional and its default value is 0x0800. A list of Types can be e.g. looked up here. /// - payload: is any data, which is then attached as payload to the frame. -fn nasl_forge_frame(register: &Register, _: &Context) -> Result { +fn nasl_forge_frame(register: &Register, _: &Context) -> Result { let src_haddr = validate_mac_address(register.named("src_haddr"))?; let dst_haddr = validate_mac_address(register.named("dst_haddr"))?; let ether_proto = match register.named("ether_proto") { @@ -464,11 +460,11 @@ fn nasl_forge_frame(register: &Register, _: &Context) -> Result Result { +fn nasl_send_frame(register: &Register, context: &Context) -> Result { let frame = match register.named("frame") { Some(ContextType::Value(NaslValue::Data(x))) => x, _ => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "Data", "Invalid data type", )) @@ -479,7 +475,7 @@ fn nasl_send_frame(register: &Register, context: &Context) -> Result x, None => &true, _ => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "Boolean", "Invalid pcap_active value", )) @@ -490,7 +486,7 @@ fn nasl_send_frame(register: &Register, context: &Context) -> Result Some(x), None => None, _ => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "String", "Invalid pcap_filter value", )) @@ -501,7 +497,7 @@ fn nasl_send_frame(register: &Register, context: &Context) -> Result *x as i32 * 1000i32, // to milliseconds None => DEFAULT_TIMEOUT, _ => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "Integer", "Invalid timeout value", )) @@ -523,11 +519,11 @@ fn nasl_send_frame(register: &Register, context: &Context) -> Result Result { +fn nasl_dump_frame(register: &Register, _: &Context) -> Result { let frame: Frame = match register.named("frame") { Some(ContextType::Value(NaslValue::Data(x))) => (x as &[u8]).try_into()?, _ => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "Data", "Invalid data type", )) diff --git a/rust/src/nasl/builtin/raw_ip/packet_forgery.rs b/rust/src/nasl/builtin/raw_ip/packet_forgery.rs index 496a28527..30d8d88dd 100644 --- a/rust/src/nasl/builtin/raw_ip/packet_forgery.rs +++ b/rust/src/nasl/builtin/raw_ip/packet_forgery.rs @@ -48,7 +48,7 @@ pub enum PacketForgeryError { SendPacket(std::io::ErrorKind), } -fn error(s: String) -> FunctionErrorKind { +fn error(s: String) -> NaslFnError { PacketForgeryError::Custom(s).into() } @@ -103,7 +103,7 @@ fn safe_copy_from_slice( o_buf: &[u8], o_init: usize, o_fin: usize, -) -> Result<(), FunctionErrorKind> { +) -> Result<(), NaslFnError> { let o_range = o_fin - o_init; let d_range = d_fin - d_init; if d_buf.len() < d_range @@ -136,7 +136,7 @@ fn safe_copy_from_slice( /// - ip_v is: the IP version. 4 by default. /// /// Returns the IP datagram or NULL on error. -fn forge_ip_packet(register: &Register, configs: &Context) -> Result { +fn forge_ip_packet(register: &Register, configs: &Context) -> Result { let dst_addr = get_host_ip(configs)?; if dst_addr.is_ipv6() { @@ -275,14 +275,11 @@ fn forge_ip_packet(register: &Register, configs: &Context) -> Result Result { +fn set_ip_elements(register: &Register, _configs: &Context) -> Result { let mut buf = match register.named("ip") { Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), _ => { - return Err(FunctionErrorKind::missing_argument("ip")); + return Err(NaslFnError::missing_argument("ip")); } }; @@ -367,11 +364,11 @@ fn set_ip_elements( /// - ip_sum /// - ip_src /// - ip_dst -fn get_ip_element(register: &Register, _configs: &Context) -> Result { +fn get_ip_element(register: &Register, _configs: &Context) -> Result { let buf = match register.named("ip") { Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), _ => { - return Err(FunctionErrorKind::missing_argument("ip")); + return Err(NaslFnError::missing_argument("ip")); } }; @@ -393,12 +390,12 @@ fn get_ip_element(register: &Register, _configs: &Context) -> Result Ok(NaslValue::String(pkt.get_destination().to_string())), _ => Err(ArgumentError::WrongArgument("Invalid element".to_string()).into()), }, - _ => Err(FunctionErrorKind::missing_argument("element")), + _ => Err(NaslFnError::missing_argument("element")), } } /// Receive a list of IP packets and print them in a readable format in the screen. -fn dump_ip_packet(register: &Register, _: &Context) -> Result { +fn dump_ip_packet(register: &Register, _: &Context) -> Result { let positional = register.positional(); if positional.is_empty() { return Err(ArgumentError::MissingPositionals { @@ -453,34 +450,31 @@ fn dump_protocol(pkt: &Ipv4Packet) -> String { /// - code: is the identifier of the option to add /// - length: is the length of the option data /// - value: is the option data -fn insert_ip_options( - register: &Register, - _configs: &Context, -) -> Result { +fn insert_ip_options(register: &Register, _configs: &Context) -> Result { let buf = match register.named("ip") { Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), _ => { - return Err(FunctionErrorKind::missing_argument("ip")); + return Err(NaslFnError::missing_argument("ip")); } }; let code = match register.named("code") { Some(ContextType::Value(NaslValue::Number(x))) => *x, _ => { - return Err(FunctionErrorKind::missing_argument("code")); + return Err(NaslFnError::missing_argument("code")); } }; let length = match register.named("length") { Some(ContextType::Value(NaslValue::Number(x))) => *x as usize, _ => { - return Err(FunctionErrorKind::missing_argument("length")); + return Err(NaslFnError::missing_argument("length")); } }; let value = match register.named("value") { Some(ContextType::Value(NaslValue::String(x))) => x.as_bytes(), Some(ContextType::Value(NaslValue::Data(x))) => x, _ => { - return Err(FunctionErrorKind::missing_argument("value")); + return Err(NaslFnError::missing_argument("value")); } }; @@ -549,15 +543,12 @@ fn insert_ip_options( /// - update_ip_len: is a flag (TRUE by default). If set, NASL will recompute the size field of the IP datagram. /// /// The modified IP datagram or NULL on error. -fn forge_tcp_packet( - register: &Register, - _configs: &Context, -) -> Result { +fn forge_tcp_packet(register: &Register, _configs: &Context) -> Result { let mut ip_buf = match register.named("ip") { Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), _ => { // Missingarguments - return Err(FunctionErrorKind::missing_argument("ip")); + return Err(NaslFnError::missing_argument("ip")); } }; let original_ip_len = ip_buf.len(); @@ -667,14 +658,11 @@ fn forge_tcp_packet( /// - data /// /// Returns an TCP element from a IP datagram. -fn get_tcp_element( - register: &Register, - _configs: &Context, -) -> Result { +fn get_tcp_element(register: &Register, _configs: &Context) -> Result { let buf = match register.named("tcp") { Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), _ => { - return Err(FunctionErrorKind::missing_argument("tcp")); + return Err(NaslFnError::missing_argument("tcp")); } }; @@ -698,7 +686,7 @@ fn get_tcp_element( "th_data" => Ok(NaslValue::Data(tcp.payload().to_vec())), _ => Err(ArgumentError::WrongArgument("element".to_string()).into()), }, - _ => Err(FunctionErrorKind::missing_argument("element")), + _ => Err(NaslFnError::missing_argument("element")), } } @@ -713,11 +701,11 @@ fn get_tcp_element( /// - 8: TCPOPT_TIMESTAMP, 8 bytes value for timestamp and echo timestamp, 4 bytes each one. /// /// The returned option depends on the given *option* parameter. It is either an int for option 2, 3 and 4 or an array containing the two values for option 8. -fn get_tcp_option(register: &Register, _configs: &Context) -> Result { +fn get_tcp_option(register: &Register, _configs: &Context) -> Result { let buf = match register.named("tcp") { Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), _ => { - return Err(FunctionErrorKind::missing_argument("tcp")); + return Err(NaslFnError::missing_argument("tcp")); } }; @@ -768,7 +756,7 @@ fn get_tcp_option(register: &Register, _configs: &Context) -> Result Ok(NaslValue::Array(timestamps)), _ => Err(ArgumentError::WrongArgument("Invalid option".to_string()).into()), }, - _ => Err(FunctionErrorKind::missing_argument("option")), + _ => Err(NaslFnError::missing_argument("option")), } } @@ -787,14 +775,11 @@ fn get_tcp_option(register: &Register, _configs: &Context) -> Result Result { +fn set_tcp_elements(register: &Register, _configs: &Context) -> Result { let buf = match register.named("tcp") { Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), _ => { - return Err(FunctionErrorKind::missing_argument("tcp")); + return Err(NaslFnError::missing_argument("tcp")); } }; @@ -926,10 +911,7 @@ fn set_tcp_elements( /// - 3: TCPOPT_WINDOW, with values between 0 and 14 /// - 4: TCPOPT_SACK_PERMITTED, no value required. /// - 8: TCPOPT_TIMESTAMP, 8 bytes value for timestamp and echo timestamp, 4 bytes each one. -fn insert_tcp_options( - register: &Register, - _configs: &Context, -) -> Result { +fn insert_tcp_options(register: &Register, _configs: &Context) -> Result { let buf = match register.named("tcp") { Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), _ => { @@ -1108,7 +1090,7 @@ fn insert_tcp_options( } /// Receive a list of IPv4 datagrams and print their TCP part in a readable format in the screen. -fn dump_tcp_packet(register: &Register, _: &Context) -> Result { +fn dump_tcp_packet(register: &Register, _: &Context) -> Result { let positional = register.positional(); if positional.is_empty() { return Err(error( @@ -1210,13 +1192,10 @@ fn format_flags(pkt: &TcpPacket) -> String { /// - update_ip_len: is a flag (TRUE by default). If set, NASL will recompute the size field of the IP datagram. /// /// Returns the modified IP datagram or NULL on error. -fn forge_udp_packet( - register: &Register, - _configs: &Context, -) -> Result { +fn forge_udp_packet(register: &Register, _configs: &Context) -> Result { let mut ip_buf = match register.named("ip") { Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), - _ => return Err(FunctionErrorKind::missing_argument("ip")), + _ => return Err(NaslFnError::missing_argument("ip")), }; let original_ip_len = ip_buf.len(); @@ -1291,14 +1270,11 @@ fn forge_udp_packet( /// - uh_sport: is the source port. NASL will convert it into network order if necessary. 0 by default. /// - uh_sum: is the UDP checksum. Although it is not compulsory, the right value is computed by default. /// - uh_ulen: is the data length. By default it is set to the length the data argument plus the size of the UDP header. -fn set_udp_elements( - register: &Register, - _configs: &Context, -) -> Result { +fn set_udp_elements(register: &Register, _configs: &Context) -> Result { let buf = match register.named("udp") { Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), _ => { - return Err(FunctionErrorKind::missing_argument("udp")); + return Err(NaslFnError::missing_argument("udp")); } }; @@ -1395,7 +1371,7 @@ fn set_udp_elements( } /// Receive a list of IPv4 datagrams and print their UDP part in a readable format in the screen. -fn dump_udp_packet(register: &Register, _: &Context) -> Result { +fn dump_udp_packet(register: &Register, _: &Context) -> Result { let positional = register.positional(); if positional.is_empty() { return Err(error( @@ -1447,14 +1423,11 @@ fn dump_udp_packet(register: &Register, _: &Context) -> Result Result { +fn get_udp_element(register: &Register, _configs: &Context) -> Result { let buf = match register.named("udp") { Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), _ => { - return Err(FunctionErrorKind::missing_argument("udp")); + return Err(NaslFnError::missing_argument("udp")); } }; @@ -1485,14 +1458,11 @@ fn get_udp_element( /// - *icmp_seq*: ICMP sequence number. /// - *icmp_type*: ICMP type. 0 by default. /// - *update_ip_len*: If this flag is set, NASL will recompute the size field of the IP datagram. Default: True. -fn forge_icmp_packet( - register: &Register, - _configs: &Context, -) -> Result { +fn forge_icmp_packet(register: &Register, _configs: &Context) -> Result { let mut ip_buf = match register.named("ip") { Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), _ => { - return Err(FunctionErrorKind::missing_argument("icmp")); + return Err(NaslFnError::missing_argument("icmp")); } }; let original_ip_len = ip_buf.len(); @@ -1579,14 +1549,11 @@ fn forge_icmp_packet( /// - icmp_seq /// - icmp_chsum /// - icmp_data -fn get_icmp_element( - register: &Register, - _configs: &Context, -) -> Result { +fn get_icmp_element(register: &Register, _configs: &Context) -> Result { let buf = match register.named("icmp") { Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), _ => { - return Err(FunctionErrorKind::missing_argument("icmp")); + return Err(NaslFnError::missing_argument("icmp")); } }; @@ -1633,15 +1600,15 @@ fn get_icmp_element( } _ => Ok(NaslValue::Null), }, - _ => Err(FunctionErrorKind::missing_argument("element")), + _ => Err(NaslFnError::missing_argument("element")), } } /// Receive a list of IPv4 ICMP packets and print them in a readable format in the screen. -fn dump_icmp_packet(register: &Register, _: &Context) -> Result { +fn dump_icmp_packet(register: &Register, _: &Context) -> Result { let positional = register.positional(); if positional.is_empty() { - return Err(FunctionErrorKind::missing_argument("icmp")); + return Err(NaslFnError::missing_argument("icmp")); } for icmp_pkt in positional.iter() { @@ -1764,14 +1731,11 @@ pub mod igmp { /// - group: IGMP group /// - type: IGMP type. 0 by default. /// - update_ip_len: If this flag is set, NASL will recompute the size field of the IP datagram. Default: True. -fn forge_igmp_packet( - register: &Register, - _configs: &Context, -) -> Result { +fn forge_igmp_packet(register: &Register, _configs: &Context) -> Result { let mut ip_buf = match register.named("ip") { Some(ContextType::Value(NaslValue::Data(d))) => d.clone(), _ => { - return Err(FunctionErrorKind::missing_argument("igmp")); + return Err(NaslFnError::missing_argument("igmp")); } }; let original_ip_len = ip_buf.len(); @@ -1844,7 +1808,7 @@ fn forge_igmp_packet( Ok(NaslValue::Data(ip_buf)) } -fn new_raw_socket() -> Result { +fn new_raw_socket() -> Result { match Socket::new_raw( Domain::IPV4, socket2::Type::RAW, @@ -1859,7 +1823,7 @@ fn new_raw_socket() -> Result { /// /// Its argument is: /// - port: port for the ping -fn nasl_tcp_ping(register: &Register, configs: &Context) -> Result { +fn nasl_tcp_ping(register: &Register, configs: &Context) -> Result { let rnd_tcp_port = || -> u16 { (random_impl().unwrap_or(0) % 65535 + 1024) as u16 }; let sports_ori: Vec = vec![ @@ -1893,7 +1857,7 @@ fn nasl_tcp_ping(register: &Register, configs: &Context) -> Result *x, None => 0, //TODO: implement plug_get_host_open_port() _ => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "Number", "Invalid length value", )) @@ -1995,15 +1959,12 @@ fn nasl_tcp_ping(register: &Register, configs: &Context) -> Result Result { +fn nasl_send_packet(register: &Register, configs: &Context) -> Result { let use_pcap = match register.named("pcap_active") { Some(ContextType::Value(NaslValue::Boolean(x))) => *x, None => true, _ => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "Boolean", "Invalid pcap_active value", )) @@ -2014,7 +1975,7 @@ fn nasl_send_packet( Some(ContextType::Value(NaslValue::String(x))) => x.to_string(), None => String::new(), _ => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "String", "Invalid pcap_filter value", )) @@ -2025,7 +1986,7 @@ fn nasl_send_packet( Some(ContextType::Value(NaslValue::Number(x))) => *x as i32 * 1000i32, // to milliseconds None => DEFAULT_TIMEOUT, _ => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "Integer", "Invalid timeout value", )) @@ -2036,7 +1997,7 @@ fn nasl_send_packet( Some(ContextType::Value(NaslValue::Boolean(x))) => *x, None => false, _ => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "Boolean", "Invalid allow_broadcast value", )) @@ -2058,7 +2019,7 @@ fn nasl_send_packet( Some(ContextType::Value(NaslValue::Number(x))) => *x, None => 0, _ => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "Number", "Invalid length value", )) @@ -2082,7 +2043,7 @@ fn nasl_send_packet( let packet_raw = match pkt { NaslValue::Data(data) => data as &[u8], _ => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "Data", "Invalid packet", )) @@ -2142,7 +2103,7 @@ fn nasl_send_packet( /// - interface: network interface name, by default NASL will try to find the best one /// - pcap_filter: BPF filter, by default it listens to everything /// - timeout: timeout in seconds, 5 by default -fn nasl_pcap_next(register: &Register, configs: &Context) -> Result { +fn nasl_pcap_next(register: &Register, configs: &Context) -> Result { nasl_send_capture(register, configs) } @@ -2151,15 +2112,12 @@ fn nasl_pcap_next(register: &Register, configs: &Context) -> Result Result { +fn nasl_send_capture(register: &Register, configs: &Context) -> Result { let interface = match register.named("interface") { Some(ContextType::Value(NaslValue::String(x))) => x.to_string(), None => String::new(), _ => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "String", "Invalid interface value", )) @@ -2170,7 +2128,7 @@ fn nasl_send_capture( Some(ContextType::Value(NaslValue::String(x))) => x.to_string(), None => String::new(), _ => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "String", "Invalid pcap_filter value", )) @@ -2181,7 +2139,7 @@ fn nasl_send_capture( Some(ContextType::Value(NaslValue::Number(x))) => *x as i32 * 1000i32, // to milliseconds None => DEFAULT_TIMEOUT, _ => { - return Err(FunctionErrorKind::wrong_unnamed_argument( + return Err(NaslFnError::wrong_unnamed_argument( "Integer", "Invalid timeout value", )) diff --git a/rust/src/nasl/builtin/raw_ip/raw_ip_utils.rs b/rust/src/nasl/builtin/raw_ip/raw_ip_utils.rs index e3396b77a..2582846c1 100644 --- a/rust/src/nasl/builtin/raw_ip/raw_ip_utils.rs +++ b/rust/src/nasl/builtin/raw_ip/raw_ip_utils.rs @@ -13,10 +13,10 @@ use pcap::{Address, Device}; use super::RawIpError; /// Convert a string in a IpAddr -pub fn ipstr2ipaddr(ip_addr: &str) -> Result { +pub fn ipstr2ipaddr(ip_addr: &str) -> Result { match IpAddr::from_str(ip_addr) { Ok(ip) => Ok(ip), - Err(_) => Err(FunctionErrorKind::from(ArgumentError::WrongArgument( + Err(_) => Err(NaslFnError::from(ArgumentError::WrongArgument( "Invalid IP address".to_string(), )) .with(ReturnValue(NaslValue::Null))), @@ -40,7 +40,7 @@ pub fn islocalhost(addr: IpAddr) -> bool { } /// Get the interface from the local ip -pub fn get_interface_by_local_ip(local_address: IpAddr) -> Result { +pub fn get_interface_by_local_ip(local_address: IpAddr) -> Result { // This fake IP is used for matching (and return false) // during the search of the interface in case an interface // doesn't have an associated address. @@ -80,7 +80,7 @@ pub fn bind_local_socket(dst: &SocketAddr) -> Result { } /// Return the source IP address given the destination IP address -pub fn get_source_ip(dst: IpAddr, port: u16) -> Result { +pub fn get_source_ip(dst: IpAddr, port: u16) -> Result { let socket = SocketAddr::new(dst, port); let sd = format!("{}:{}", dst, port); let local_socket = bind_local_socket(&socket)?; diff --git a/rust/src/nasl/builtin/raw_ip/tests/packet_forgery.rs b/rust/src/nasl/builtin/raw_ip/tests/packet_forgery.rs index fb446c261..39896a93f 100644 --- a/rust/src/nasl/builtin/raw_ip/tests/packet_forgery.rs +++ b/rust/src/nasl/builtin/raw_ip/tests/packet_forgery.rs @@ -8,7 +8,7 @@ mod tests { use nasl_builtin_raw_ip::RawIp; use nasl_builtin_std::ContextFactory; - use crate::nasl::utils::{error::FunctionErrorKind, Executor}; + use crate::nasl::utils::{error::NaslFnError, Executor}; use crate::nasl::interpreter::test_utils::TestBuilder; use crate::nasl::syntax::NaslValue; @@ -20,7 +20,7 @@ mod tests { o_buf: &[u8], o_init: usize, o_fin: usize, - ) -> Result<(), FunctionErrorKind> { + ) -> Result<(), NaslFnError> { let o_range = o_fin - o_init; let d_range = d_fin - d_init; if d_buf.len() < d_range @@ -29,7 +29,7 @@ mod tests { || d_buf.len() < d_fin || o_buf.len() < o_fin { - return Err(FunctionErrorKind::Diagnostic( + return Err(NaslFnError::Diagnostic( "Error copying from slice. Index out of range".to_string(), Some(NaslValue::Null), )); @@ -270,7 +270,7 @@ mod tests { // different range size between origin and destination assert_eq!( safe_copy_from_slice(&mut a, 0, 2, &b, 0, b.len()), - Err(FunctionErrorKind::Diagnostic( + Err(NaslFnError::Diagnostic( "Error copying from slice. Index out of range".to_string(), Some(NaslValue::Null) )) @@ -279,7 +279,7 @@ mod tests { // different range size between origin and destination assert_eq!( safe_copy_from_slice(&mut a, 0, alen, &b, 0, 2), - Err(FunctionErrorKind::Diagnostic( + Err(NaslFnError::Diagnostic( "Error copying from slice. Index out of range".to_string(), Some(NaslValue::Null) )) @@ -288,7 +288,7 @@ mod tests { // out of index in the destination range assert_eq!( safe_copy_from_slice(&mut a, 1, alen + 1, &b, 0, b.len()), - Err(FunctionErrorKind::Diagnostic( + Err(NaslFnError::Diagnostic( "Error copying from slice. Index out of range".to_string(), Some(NaslValue::Null) )) diff --git a/rust/src/nasl/builtin/regex/mod.rs b/rust/src/nasl/builtin/regex/mod.rs index adb2e8af7..4353e6ecf 100644 --- a/rust/src/nasl/builtin/regex/mod.rs +++ b/rust/src/nasl/builtin/regex/mod.rs @@ -52,7 +52,7 @@ fn ereg( icase: Option, rnul: Option, multiline: Option, -) -> Result { +) -> Result { let icase = icase.unwrap_or(false); let rnul = rnul.unwrap_or(true); let multiline = multiline.unwrap_or(false); @@ -79,7 +79,7 @@ fn ereg_replace( replace: NaslValue, icase: Option, rnul: Option, -) -> Result { +) -> Result { let icase = icase.unwrap_or(false); let rnul = rnul.unwrap_or(true); @@ -107,7 +107,7 @@ fn egrep( pattern: NaslValue, icase: Option, rnul: Option, -) -> Result { +) -> Result { let icase = icase.unwrap_or(false); let rnul = rnul.unwrap_or(true); @@ -141,7 +141,7 @@ fn eregmatch( find_all: Option, icase: Option, rnul: Option, -) -> Result { +) -> Result { let icase = icase.unwrap_or(false); let rnul = rnul.unwrap_or(true); let find_all = find_all.unwrap_or(false); diff --git a/rust/src/nasl/builtin/report_functions/mod.rs b/rust/src/nasl/builtin/report_functions/mod.rs index 652d1fcd9..d375e6bee 100644 --- a/rust/src/nasl/builtin/report_functions/mod.rs +++ b/rust/src/nasl/builtin/report_functions/mod.rs @@ -26,7 +26,7 @@ impl Reporting { typus: ResultType, register: &Register, context: &Context, - ) -> Result { + ) -> Result { let data = register.named("data").map(|x| x.to_string()); let port = register .named("port") @@ -74,7 +74,7 @@ impl Reporting { &self, register: &Register, context: &Context, - ) -> Result { + ) -> Result { self.store_result(ResultType::Log, register, context) } @@ -89,7 +89,7 @@ impl Reporting { &self, register: &Register, context: &Context, - ) -> Result { + ) -> Result { self.store_result(ResultType::Alarm, register, context) } @@ -104,7 +104,7 @@ impl Reporting { &self, register: &Register, context: &Context, - ) -> Result { + ) -> Result { self.store_result(ResultType::Error, register, context) } } diff --git a/rust/src/nasl/builtin/ssh/mod.rs b/rust/src/nasl/builtin/ssh/mod.rs index 53219137b..e2eaa7ff1 100644 --- a/rust/src/nasl/builtin/ssh/mod.rs +++ b/rust/src/nasl/builtin/ssh/mod.rs @@ -42,7 +42,7 @@ mod libssh_uses { #[cfg(feature = "nasl-builtin-libssh")] pub use libssh_uses::*; -type Result = std::result::Result; +type Result = std::result::Result; const DEFAULT_SSH_PORT: u16 = 22; @@ -519,7 +519,7 @@ impl Ssh { } AuthStatus::Success => break, status => { - return Err(FunctionErrorKind::from( + return Err(NaslFnError::from( SshErrorKind::UnexpectedAuthenticationStatus(format!("{:?}", status)) .with(session_id), ) diff --git a/rust/src/nasl/builtin/ssh/utils.rs b/rust/src/nasl/builtin/ssh/utils.rs index 3a0a89d4d..8076c5c84 100644 --- a/rust/src/nasl/builtin/ssh/utils.rs +++ b/rust/src/nasl/builtin/ssh/utils.rs @@ -12,7 +12,7 @@ impl<'a, T> FromNaslValue<'a> for CommaSeparated where T: for<'b> FromNaslValue<'b>, { - fn from_nasl_value(value: &'a NaslValue) -> Result { + fn from_nasl_value(value: &'a NaslValue) -> Result { let s = StringOrData::from_nasl_value(value)?; Ok(Self( s.0.split(",") @@ -21,13 +21,13 @@ where let nasl_val = NaslValue::String(substr.to_string()); T::from_nasl_value(&nasl_val) }) - .collect::, FunctionErrorKind>>()?, + .collect::, NaslFnError>>()?, )) } } impl<'a> FromNaslValue<'a> for key::Name { - fn from_nasl_value(value: &'a NaslValue) -> Result { + fn from_nasl_value(value: &'a NaslValue) -> Result { let s = String::from_nasl_value(value)?; key::Name::try_from(&*s).map_err(|_| { ArgumentError::WrongArgument(format!("Expected a valid SSH key type, found '{}'", s)) @@ -37,7 +37,7 @@ impl<'a> FromNaslValue<'a> for key::Name { } impl<'a> FromNaslValue<'a> for cipher::Name { - fn from_nasl_value(value: &'a NaslValue) -> Result { + fn from_nasl_value(value: &'a NaslValue) -> Result { let s = String::from_nasl_value(value)?; cipher::Name::try_from(&*s).map_err(|_| { ArgumentError::WrongArgument(format!("Expected a valid SSH cipher type, found '{}'", s)) diff --git a/rust/src/nasl/builtin/string/mod.rs b/rust/src/nasl/builtin/string/mod.rs index 64b822030..707cb3323 100644 --- a/rust/src/nasl/builtin/string/mod.rs +++ b/rust/src/nasl/builtin/string/mod.rs @@ -10,7 +10,7 @@ mod tests; use crate::nasl::{ utils::{ function::{bytes_to_str, CheckedPositionals, Maybe, StringOrData}, - FunctionErrorKind, + NaslFnError, }, ArgumentError, }; @@ -290,7 +290,7 @@ fn stridx(haystack: NaslValue, needle: NaslValue, offset: Option) -> i64 /// NASL function to display any number of NASL values /// /// Internally the string function is used to concatenate the given parameters -fn display(register: &Register, configs: &Context) -> Result { +fn display(register: &Register, configs: &Context) -> Result { println!("{}", &string(register, configs)?); Ok(NaslValue::Null) } @@ -368,11 +368,7 @@ fn insstr( /// `pattern` contains the pattern to search for. /// The optional argument `icase` toggles case sensitivity. Default: false (case sensitive). If true, search is case insensitive. #[nasl_function(named(string, pattern, icase))] -fn match_( - string: NaslValue, - pattern: NaslValue, - icase: Option, -) -> Result { +fn match_(string: NaslValue, pattern: NaslValue, icase: Option) -> Result { let options = MatchOptions { case_sensitive: !icase.unwrap_or(false), require_literal_separator: false, diff --git a/rust/src/nasl/interpreter/error.rs b/rust/src/nasl/interpreter/error.rs index 7279f4fb4..0038e0cd0 100644 --- a/rust/src/nasl/interpreter/error.rs +++ b/rust/src/nasl/interpreter/error.rs @@ -6,8 +6,8 @@ use std::io; use crate::nasl::syntax::LoadError; use crate::nasl::syntax::{Statement, SyntaxError, TokenCategory}; -use crate::nasl::utils::error::FunctionErrorKind; -use crate::nasl::{InternalError, FEK}; +use crate::nasl::utils::error::{NaslFnError, NaslFnErrorKind}; +use crate::nasl::InternalError; use crate::storage::StorageError; use thiserror::Error; @@ -19,12 +19,12 @@ pub struct FunctionError { pub function: String, /// Kind of error #[source] - pub kind: FunctionErrorKind, + pub kind: NaslFnError, } impl FunctionError { /// Creates a new FunctionError - pub fn new(function: &str, kind: FunctionErrorKind) -> Self { + pub fn new(function: &str, kind: NaslFnError) -> Self { Self { function: function.to_owned(), kind, @@ -231,7 +231,7 @@ impl From for InterpretError { impl From for InterpretError { fn from(fe: FunctionError) -> Self { match fe.kind.kind { - FEK::Internal(InternalError::Storage(e)) => { + NaslFnErrorKind::Internal(InternalError::Storage(e)) => { Self::new(InterpretErrorKind::StorageError(e), None) } _ => Self::new(InterpretErrorKind::FunctionCallError(fe), None), diff --git a/rust/src/nasl/mod.rs b/rust/src/nasl/mod.rs index d5c229028..8ae53f49e 100644 --- a/rust/src/nasl/mod.rs +++ b/rust/src/nasl/mod.rs @@ -15,7 +15,6 @@ pub mod prelude { pub use super::syntax::NaslValue; pub use super::utils::error::ReturnValue; pub use super::utils::error::WithErrorInfo; - pub use super::utils::error::FEK; pub use super::utils::function::CheckedPositionals; pub use super::utils::function::FromNaslValue; pub use super::utils::function::Positionals; @@ -23,8 +22,8 @@ pub mod prelude { pub use super::utils::ArgumentError; pub use super::utils::Context; pub use super::utils::ContextType; - pub use super::utils::FunctionErrorKind; pub use super::utils::InternalError; + pub use super::utils::NaslFnError; pub use super::utils::NaslResult; pub use super::utils::Register; pub use crate::function_set; diff --git a/rust/src/nasl/test_utils.rs b/rust/src/nasl/test_utils.rs index 69736ed46..797d7a8b4 100644 --- a/rust/src/nasl/test_utils.rs +++ b/rust/src/nasl/test_utils.rs @@ -290,7 +290,7 @@ where fn check_result( &self, - result: &Result, + result: &Result, reference: &TracedTestResult, line_count: usize, ) { @@ -301,7 +301,7 @@ where "Mismatch at {}.\nIn code \"{}\":\nExpected: {:?}\nFound: {:?}", reference.location, self.lines[line_count], - Ok::<_, FunctionErrorKind>(reference_result), + Ok::<_, NaslFnError>(reference_result), result, ); } @@ -322,7 +322,7 @@ where fn compare_result( &self, - result: &Result, + result: &Result, reference: &TestResult, ) -> bool { match reference { @@ -395,11 +395,11 @@ macro_rules! check_err_matches { |e| { if let Err(e) = e { // Convert with try_into to allow using - // the variants of `FunctionErrorKind` directly without + // the variants of `NaslFnError` directly without // having to wrap them in the outer enum. let converted = e.try_into(); // This is only irrefutable for the - // FunctionErrorKind -> FunctionErrorKind conversion but not for others. + // NaslFnError -> NaslFnError conversion but not for others. #[allow(irrefutable_let_patterns)] if let Ok(e) = converted { matches!(e, $pat) diff --git a/rust/src/nasl/utils/error.rs b/rust/src/nasl/utils/error.rs index aeef08a51..f79c074b8 100644 --- a/rust/src/nasl/utils/error.rs +++ b/rust/src/nasl/utils/error.rs @@ -12,57 +12,56 @@ use crate::storage::StorageError; #[derive(Debug, Clone, Error)] #[error("{kind}")] -pub struct FunctionErrorKind { +pub struct NaslFnError { #[source] - pub kind: FEK, + pub kind: NaslFnErrorKind, return_value: Option, } -impl FunctionErrorKind { +impl NaslFnError { pub fn return_value(&self) -> &Option { &self.return_value } } -impl From for FunctionErrorKind { - fn from(value: FEK) -> Self { - FunctionErrorKind { +impl From for NaslFnError { + fn from(value: NaslFnErrorKind) -> Self { + NaslFnError { kind: value, return_value: None, } } } -impl From for FunctionErrorKind { +impl From for NaslFnError { fn from(value: ArgumentError) -> Self { - FunctionErrorKind { - kind: FEK::Argument(value), + NaslFnError { + kind: NaslFnErrorKind::Argument(value), return_value: None, } } } -impl From for FunctionErrorKind { +impl From for NaslFnError { fn from(value: BuiltinError) -> Self { - FunctionErrorKind { - kind: FEK::Builtin(value), + NaslFnError { + kind: NaslFnErrorKind::Builtin(value), return_value: None, } } } -impl From for FunctionErrorKind { +impl From for NaslFnError { fn from(value: InternalError) -> Self { - FunctionErrorKind { - kind: FEK::Internal(value), + NaslFnError { + kind: NaslFnErrorKind::Internal(value), return_value: None, } } } #[derive(Debug, Clone, Error)] -/// Descriptive kind of error that can occur while calling a function -pub enum FEK { +pub enum NaslFnErrorKind { #[error("{0}")] Argument(ArgumentError), #[error("{0}")] @@ -97,47 +96,47 @@ pub trait WithErrorInfo { pub struct ReturnValue(pub T); -impl> WithErrorInfo> for FunctionErrorKind { +impl> WithErrorInfo> for NaslFnError { fn with(mut self, val: ReturnValue) -> Self { self.return_value = Some(val.0.into()); self } } -impl From for FunctionErrorKind { +impl From for NaslFnError { fn from(value: StorageError) -> Self { - FEK::Internal(InternalError::Storage(value)).into() + NaslFnErrorKind::Internal(InternalError::Storage(value)).into() } } -impl TryFrom for ArgumentError { +impl TryFrom for ArgumentError { type Error = (); - fn try_from(value: FunctionErrorKind) -> Result { + fn try_from(value: NaslFnError) -> Result { match value.kind { - FEK::Argument(e) => Ok(e), + NaslFnErrorKind::Argument(e) => Ok(e), _ => Err(()), } } } -impl TryFrom for InternalError { +impl TryFrom for InternalError { type Error = (); - fn try_from(value: FunctionErrorKind) -> Result { + fn try_from(value: NaslFnError) -> Result { match value.kind { - FEK::Internal(e) => Ok(e), + NaslFnErrorKind::Internal(e) => Ok(e), _ => Err(()), } } } -impl TryFrom for BuiltinError { +impl TryFrom for BuiltinError { type Error = (); - fn try_from(value: FunctionErrorKind) -> Result { + fn try_from(value: NaslFnError) -> Result { match value.kind { - FEK::Builtin(e) => Ok(e), + NaslFnErrorKind::Builtin(e) => Ok(e), _ => Err(()), } } @@ -152,12 +151,12 @@ impl ArgumentError { } } -impl FunctionErrorKind { +impl NaslFnError { /// Helper function to quickly construct a `WrongArgument` variant /// containing the name of the argument, the expected value and /// the actual value. pub fn wrong_unnamed_argument(expected: &str, got: &str) -> Self { - FEK::Argument(ArgumentError::WrongArgument(format!( + NaslFnErrorKind::Argument(ArgumentError::WrongArgument(format!( "Expected {expected} but {got}" ))) .into() @@ -166,6 +165,6 @@ impl FunctionErrorKind { /// Helper function to quickly construct a `MissingArguments` variant /// for a single missing argument. pub fn missing_argument(val: &str) -> Self { - FEK::Argument(ArgumentError::MissingNamed(vec![val.to_string()])).into() + NaslFnErrorKind::Argument(ArgumentError::MissingNamed(vec![val.to_string()])).into() } } diff --git a/rust/src/nasl/utils/function/from_nasl_value.rs b/rust/src/nasl/utils/function/from_nasl_value.rs index eecf9b68c..b5593641e 100644 --- a/rust/src/nasl/utils/function/from_nasl_value.rs +++ b/rust/src/nasl/utils/function/from_nasl_value.rs @@ -6,23 +6,23 @@ use crate::nasl::prelude::*; /// The conversion may fail. pub trait FromNaslValue<'a>: Sized { /// Perform the conversion - fn from_nasl_value(value: &'a NaslValue) -> Result; + fn from_nasl_value(value: &'a NaslValue) -> Result; } impl<'a> FromNaslValue<'a> for NaslValue { - fn from_nasl_value(value: &'a NaslValue) -> Result { + fn from_nasl_value(value: &'a NaslValue) -> Result { Ok(value.clone()) } } impl<'a> FromNaslValue<'a> for &'a NaslValue { - fn from_nasl_value(value: &'a NaslValue) -> Result { + fn from_nasl_value(value: &'a NaslValue) -> Result { Ok(value) } } impl<'a> FromNaslValue<'a> for String { - fn from_nasl_value(value: &NaslValue) -> Result { + fn from_nasl_value(value: &NaslValue) -> Result { match value { NaslValue::String(string) => Ok(string.to_string()), _ => Err(ArgumentError::WrongArgument("Expected string.".to_string()).into()), @@ -31,7 +31,7 @@ impl<'a> FromNaslValue<'a> for String { } impl<'a> FromNaslValue<'a> for &'a str { - fn from_nasl_value(value: &'a NaslValue) -> Result { + fn from_nasl_value(value: &'a NaslValue) -> Result { match value { NaslValue::String(string) => Ok(string), _ => Err(ArgumentError::WrongArgument("Expected string.".to_string()).into()), @@ -40,7 +40,7 @@ impl<'a> FromNaslValue<'a> for &'a str { } impl<'a> FromNaslValue<'a> for &'a [u8] { - fn from_nasl_value(value: &'a NaslValue) -> Result { + fn from_nasl_value(value: &'a NaslValue) -> Result { match value { NaslValue::Data(bytes) => Ok(bytes), _ => Err(ArgumentError::WrongArgument("Expected byte data.".to_string()).into()), @@ -49,19 +49,19 @@ impl<'a> FromNaslValue<'a> for &'a [u8] { } impl<'a, T: FromNaslValue<'a>> FromNaslValue<'a> for Vec { - fn from_nasl_value(value: &'a NaslValue) -> Result { + fn from_nasl_value(value: &'a NaslValue) -> Result { match value { NaslValue::Array(vals) => Ok(vals .iter() .map(T::from_nasl_value) - .collect::, FunctionErrorKind>>()?), + .collect::, NaslFnError>>()?), _ => Err(ArgumentError::WrongArgument("Expected an array..".to_string()).into()), } } } impl<'a, T: FromNaslValue<'a>> FromNaslValue<'a> for HashMap { - fn from_nasl_value(value: &'a NaslValue) -> Result { + fn from_nasl_value(value: &'a NaslValue) -> Result { match value { NaslValue::Dict(map) => Ok(map .iter() @@ -73,7 +73,7 @@ impl<'a, T: FromNaslValue<'a>> FromNaslValue<'a> for HashMap { } impl<'a> FromNaslValue<'a> for bool { - fn from_nasl_value(value: &'a NaslValue) -> Result { + fn from_nasl_value(value: &'a NaslValue) -> Result { match value { NaslValue::Boolean(b) => Ok(*b), NaslValue::Number(n) => Ok(*n != 0), @@ -85,7 +85,7 @@ impl<'a> FromNaslValue<'a> for bool { macro_rules! impl_from_nasl_value_for_numeric_type { ($ty: ty) => { impl<'a> FromNaslValue<'a> for $ty { - fn from_nasl_value(value: &NaslValue) -> Result { + fn from_nasl_value(value: &NaslValue) -> Result { match value { NaslValue::Number(num) => Ok(<$ty>::try_from(*num).map_err(|_| { ArgumentError::WrongArgument("Expected positive number.".into()) diff --git a/rust/src/nasl/utils/function/maybe.rs b/rust/src/nasl/utils/function/maybe.rs index 28085e718..02b79d986 100644 --- a/rust/src/nasl/utils/function/maybe.rs +++ b/rust/src/nasl/utils/function/maybe.rs @@ -1,6 +1,6 @@ use crate::nasl::syntax::NaslValue; -use crate::nasl::FunctionErrorKind; +use crate::nasl::NaslFnError; use super::FromNaslValue; @@ -12,7 +12,7 @@ use super::FromNaslValue; pub struct Maybe(Option); impl<'a, T: FromNaslValue<'a>> FromNaslValue<'a> for Maybe { - fn from_nasl_value(value: &'a NaslValue) -> Result { + fn from_nasl_value(value: &'a NaslValue) -> Result { Ok(Self(T::from_nasl_value(value).ok())) } } diff --git a/rust/src/nasl/utils/function/positionals.rs b/rust/src/nasl/utils/function/positionals.rs index bd29d26f5..b9b5e249b 100644 --- a/rust/src/nasl/utils/function/positionals.rs +++ b/rust/src/nasl/utils/function/positionals.rs @@ -1,6 +1,6 @@ use std::{marker::PhantomData, ops::Index}; -use crate::nasl::{FunctionErrorKind, Register}; +use crate::nasl::{NaslFnError, Register}; use super::FromNaslValue; @@ -22,9 +22,9 @@ impl<'a, T: FromNaslValue<'a>> Positionals<'a, T> { } /// Returns an iterator over the positional arguments. - /// The item type is Result, since + /// The item type is Result, since /// the conversion to T can still fail. - pub fn iter(&self) -> impl Iterator> + 'a { + pub fn iter(&self) -> impl Iterator> + 'a { self.register .positional() .iter() @@ -45,12 +45,12 @@ pub struct CheckedPositionals { impl<'a, T: FromNaslValue<'a>> CheckedPositionals { /// Create a new `CheckedPositionals` from the register. - pub fn new(register: &'a Register) -> Result { + pub fn new(register: &'a Register) -> Result { let data = register .positional() .iter() .map(T::from_nasl_value) - .collect::, FunctionErrorKind>>()?; + .collect::, NaslFnError>>()?; Ok(Self { data, _marker: PhantomData, diff --git a/rust/src/nasl/utils/function/to_nasl_result.rs b/rust/src/nasl/utils/function/to_nasl_result.rs index ab48aa7eb..456fd0290 100644 --- a/rust/src/nasl/utils/function/to_nasl_result.rs +++ b/rust/src/nasl/utils/function/to_nasl_result.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use crate::nasl::syntax::NaslValue; -use crate::nasl::{FunctionErrorKind, NaslResult}; +use crate::nasl::{NaslFnError, NaslResult}; /// A type that can be converted to a NaslResult. /// The conversion is fallible to make it possible to convert from other Result @@ -26,7 +26,7 @@ impl ToNaslResult for Option { } } -impl> ToNaslResult for Result { +impl> ToNaslResult for Result { fn to_nasl_result(self) -> NaslResult { self.map_err(|e| e.into()).and_then(|x| x.to_nasl_result()) } @@ -67,7 +67,7 @@ impl ToNaslResult for Vec<&str> { Ok(NaslValue::Array( self.into_iter() .map(|s| s.to_nasl_result()) - .collect::, FunctionErrorKind>>()?, + .collect::, NaslFnError>>()?, )) } } @@ -77,7 +77,7 @@ impl ToNaslResult for Vec { Ok(NaslValue::Array( self.into_iter() .map(|s| s.to_nasl_result()) - .collect::, FunctionErrorKind>>()?, + .collect::, NaslFnError>>()?, )) } } @@ -93,7 +93,7 @@ impl ToNaslResult for HashMap { Ok(NaslValue::Dict( self.into_iter() .map(|(key, s)| s.to_nasl_result().map(|res| (key, res))) - .collect::, FunctionErrorKind>>()?, + .collect::, NaslFnError>>()?, )) } } @@ -116,7 +116,7 @@ macro_rules! impl_to_nasl_result_for_numeric_type { impl_to_nasl_result_for_numeric_type!($ty, skip_vec_impl); impl ToNaslResult for Vec<$ty> { fn to_nasl_result(self) -> NaslResult { - let collected: Result, FunctionErrorKind> = + let collected: Result, NaslFnError> = self.into_iter().map(|x| x.to_nasl_result()).collect(); Ok(NaslValue::Array(collected?)) } diff --git a/rust/src/nasl/utils/function/types.rs b/rust/src/nasl/utils/function/types.rs index 38179334d..19c03e1ac 100644 --- a/rust/src/nasl/utils/function/types.rs +++ b/rust/src/nasl/utils/function/types.rs @@ -5,7 +5,7 @@ use crate::nasl::prelude::*; pub struct StringOrData(pub String); impl<'a> FromNaslValue<'a> for StringOrData { - fn from_nasl_value(value: &'a NaslValue) -> Result { + fn from_nasl_value(value: &'a NaslValue) -> Result { match value { NaslValue::String(string) => Ok(Self(string.clone())), NaslValue::Data(buffer) => Ok(Self(bytes_to_str(buffer))), diff --git a/rust/src/nasl/utils/function/utils.rs b/rust/src/nasl/utils/function/utils.rs index 14990eccc..d1c6483c8 100644 --- a/rust/src/nasl/utils/function/utils.rs +++ b/rust/src/nasl/utils/function/utils.rs @@ -9,7 +9,7 @@ use crate::nasl::prelude::*; pub fn get_optional_positional_arg<'a, T: FromNaslValue<'a>>( register: &'a Register, position: usize, -) -> Result, FunctionErrorKind> { +) -> Result, NaslFnError> { register .positional() .get(position) @@ -23,7 +23,7 @@ pub fn get_positional_arg<'a, T: FromNaslValue<'a>>( register: &'a Register, position: usize, num_required_positional_args: usize, -) -> Result { +) -> Result { let positional = register.positional(); let arg = positional.get(position).ok_or_else(|| { let num_given = positional.len(); @@ -53,7 +53,7 @@ fn context_type_as_nasl_value<'a>( pub fn get_optional_named_arg<'a, T: FromNaslValue<'a>>( register: &'a Register, name: &'a str, -) -> Result, FunctionErrorKind> { +) -> Result, NaslFnError> { register .named(name) .map(|arg| context_type_as_nasl_value(arg, name)) @@ -67,7 +67,7 @@ pub fn get_optional_named_arg<'a, T: FromNaslValue<'a>>( pub fn get_named_arg<'a, T: FromNaslValue<'a>>( register: &'a Register, name: &'a str, -) -> Result { +) -> Result { let arg = register .named(name) .ok_or_else(|| ArgumentError::MissingNamed(vec![name.to_string()]))?; @@ -80,7 +80,7 @@ pub fn get_optional_maybe_named_arg<'a, T: FromNaslValue<'a>>( register: &'a Register, name: &'a str, position: usize, -) -> Result, FunctionErrorKind> { +) -> Result, NaslFnError> { let via_position = get_optional_positional_arg::(register, position)?; if let Some(via_position) = via_position { Ok(Some(via_position)) @@ -95,7 +95,7 @@ pub fn get_maybe_named_arg<'a, T: FromNaslValue<'a>>( register: &'a Register, name: &'a str, position: usize, -) -> Result { +) -> Result { let via_position = get_optional_positional_arg(register, position)?; if let Some(via_position) = via_position { Ok(via_position) @@ -114,7 +114,7 @@ fn check_named_args( _nasl_fn_name: &str, named: &[&str], maybe_named: &[&str], -) -> Result { +) -> Result { let mut num_maybe_named = 0; for arg_name in register.iter_named_args().unwrap() { if arg_name == FC_ANON_ARGS || named.contains(&arg_name) { @@ -142,7 +142,7 @@ pub fn check_args( named: &[&str], maybe_named: &[&str], max_num_expected_positional: Option, -) -> Result<(), FunctionErrorKind> { +) -> Result<(), NaslFnError> { let num_maybe_named_given = check_named_args(register, _nasl_fn_name, named, maybe_named)?; let num_positional_given = register.positional().len(); if let Some(max_num_expected_positional) = max_num_expected_positional { diff --git a/rust/src/nasl/utils/mod.rs b/rust/src/nasl/utils/mod.rs index cf8699b9e..811c373c1 100644 --- a/rust/src/nasl/utils/mod.rs +++ b/rust/src/nasl/utils/mod.rs @@ -13,13 +13,13 @@ use std::collections::HashMap; pub use context::{Context, ContextType, Register}; pub use error::ArgumentError; -pub use error::FunctionErrorKind; pub use error::InternalError; +pub use error::NaslFnError; pub use executor::{Executor, IntoFunctionSet, StoredFunctionSet}; /// The result of a function call. -pub type NaslResult = Result; +pub type NaslResult = Result; /// Resolves positional arguments from the register. pub fn resolve_positional_arguments(register: &Register) -> Vec {