diff --git a/rust/nvtcache/src/lib.rs b/rust/nvtcache/src/lib.rs index 019706b29..2a36fb9b5 100644 --- a/rust/nvtcache/src/lib.rs +++ b/rust/nvtcache/src/lib.rs @@ -1,4 +1,9 @@ +/// Module to handle custom errors pub mod dberror; +/// Module to handle Nvt metadata. The Nvt structure is defined here as well +/// as the methods to set and get the struct members. pub mod nvt; +/// Module include objects and methods to upload an Nvt in redis pub mod nvtcache; +/// Module with structures and methods to access redis. pub mod redisconnector; diff --git a/rust/nvtcache/src/nvtcache.rs b/rust/nvtcache/src/nvtcache.rs index eed1140a4..67d3f2c4e 100644 --- a/rust/nvtcache/src/nvtcache.rs +++ b/rust/nvtcache/src/nvtcache.rs @@ -1,24 +1,34 @@ -const NVTICACHE: &str = "nvticache"; -const PLUGIN_PATH: &str = "/home/jnicola/install/var/lib/openvas/plugins/"; - use crate::dberror::Result; use crate::nvt::*; use crate::redisconnector::*; +use std::env; use std::path::Path; pub struct NvtCache { pub cache: RedisCtx, pub init: bool, + cache_key: String, + plugin_path: String, } /// NvtCache implementation. impl NvtCache { /// Initialize and return an NVT Cache Object pub fn init() -> Result { - let rctx = RedisCtx::new()?; + let redis_default_socket = |_| "unix:///run/redis/redis-server.sock".to_string(); + let redis_socket = env::var("REDIS_SOCKET").unwrap_or_else(redis_default_socket); + let rctx = RedisCtx::new(redis_socket.to_string())?; + + let default_plugin_path = |_| "/var/lib/openvas/plugins/".to_string(); + let plugin_path = env::var("PLUGIN_PATH").unwrap_or_else(default_plugin_path); + + let cache_key = "nvticache".to_string(); + Ok(NvtCache { cache: rctx, init: true, + cache_key, + plugin_path, }) } @@ -35,13 +45,13 @@ impl NvtCache { /// Set the key nvtcache pub fn set_version(&mut self, feed_version: &str) -> Result<()> { - let _ = self.cache.redis_set_key(NVTICACHE, feed_version)?; + let _ = self.cache.redis_set_key(&self.cache_key, feed_version)?; Ok(()) } /// Get the key nvtcache, which has the feed version pub fn get_version(&mut self) -> Result { - let version = self.cache.redis_get_key(NVTICACHE)?; + let version = self.cache.redis_get_key(&self.cache_key)?; Ok(version) } @@ -50,7 +60,7 @@ impl NvtCache { /// with the version in the cache /// Return True if it is updated, False if outdated, Error otherwise. pub fn check_feed(&mut self, current: &str) -> Result { - let cached = self.cache.redis_get_key(NVTICACHE)?; + let cached = self.cache.redis_get_key(&self.cache_key)?; if cached == current { return Ok(true); } @@ -79,7 +89,7 @@ impl NvtCache { // If it is in the cache, and are not the same filename // we check if it is still in the filesystem. if !cached_nvt.is_empty() && cached_nvt != filename { - let mut src_path: String = PLUGIN_PATH.to_owned(); + let mut src_path: String = self.plugin_path.to_owned(); src_path.push_str(&cached_nvt); // If still exists, the oid is duplicated diff --git a/rust/nvtcache/src/redisconnector.rs b/rust/nvtcache/src/redisconnector.rs index 21104592d..7e91151d9 100644 --- a/rust/nvtcache/src/redisconnector.rs +++ b/rust/nvtcache/src/redisconnector.rs @@ -2,10 +2,6 @@ use crate::dberror::DbError; use crate::dberror::Result; use crate::nvt::Nvt; use redis::*; -use std::collections::LinkedList; - -const GLOBAL_DBINDEX_NAME: &str = "GVM.__GlobalDBIndex"; -const REDIS_DEFAULT_PATH: &str = "unix:///run/redis/redis-server.sock"; pub enum KbNvtPos { NvtFilenamePos, @@ -31,6 +27,7 @@ pub struct RedisCtx { kb: Connection, //a redis connection db: u32, // the name space maxdb: u32, // max db index + global_db_index: String, } #[derive(Debug, PartialEq)] @@ -52,13 +49,15 @@ impl FromRedisValue for RedisValueHandler { impl RedisCtx { /// Connect to the redis server and return a redis context object - pub fn new() -> Result { - let client = redis::Client::open(REDIS_DEFAULT_PATH)?; + pub fn new(redis_socket: String) -> Result { + let client = redis::Client::open(redis_socket)?; let kb = client.get_connection()?; + let global_db_index = "GVM.__GlobalDBIndex".to_string(); let mut redisctx = RedisCtx { kb, db: 0, maxdb: 0, + global_db_index, }; let _kbi = redisctx.select_database()?; Ok(redisctx) @@ -121,7 +120,7 @@ impl RedisCtx { } fn try_database(&mut self, dbi: u32) -> Result { - let ret = self.kb.hset_nx(GLOBAL_DBINDEX_NAME, dbi, 1)?; + let ret = self.kb.hset_nx(&self.global_db_index, dbi, 1)?; Ok(ret) } @@ -130,7 +129,7 @@ impl RedisCtx { let mut selected_db: u32 = 0; // Start always from 1. Namespace 0 is reserved - //format GLOBAL_DBINDEX_NAME + //format self.global_db_index for i in 1..maxdb { let ret = self.try_database(i)?; if ret == 1 { @@ -153,7 +152,7 @@ impl RedisCtx { let dbi = self.get_namespace()?; // Remove the entry from the hash list self.set_namespace(0)?; - self.kb.hdel(GLOBAL_DBINDEX_NAME, dbi)?; + self.kb.hdel(&self.global_db_index, dbi)?; Ok(()) } @@ -205,7 +204,7 @@ impl RedisCtx { // Get the references let (cves, bids, xrefs) = nvt.get_refs(); - let key_name = ["nvt:".to_owned(), oid].join(""); + let key_name = ["nvt:".to_owned(), oid.to_owned()].join(""); Cmd::new() .arg("RPUSH") .arg(key_name) @@ -226,6 +225,11 @@ impl RedisCtx { .query(&mut self.kb)?; //TODO: Add preferences + //let key_name = ["oid:".to_owned(), oid.to_owned(), "prefs".to_owned()].join(""); + //let prefs = nvt.get_prefs()?; + //for pref in prefs.iter_mut() { + // + //} nvt.destroy();