2 Commits

Author SHA1 Message Date
Daniel Heule
5aa718e50e Cargo version bump 2026-01-09 10:01:27 +01:00
Daniel Heule
9bd7685dc3 Bugfix for hash generation of non utf8 files 2026-01-09 09:54:52 +01:00
5 changed files with 48 additions and 21 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "rsnmpagent"
version = "0.4.1"
version = "0.4.2"
edition = "2024"
[profile.release]

View File

@@ -8,9 +8,9 @@ use std::path::Path;
pub(crate) fn filesum_filtered(
path: &Path,
oldfile: &mut String,
oldfile: &mut Vec<u8>,
diff_string: &mut String,
re: Option<&regex::Regex>,
re: Option<&regex::bytes::Regex>,
) -> io::Result<(bool, String)> {
// Open file for hashing
match re {
@@ -18,30 +18,37 @@ pub(crate) fn filesum_filtered(
None => trace!("try to open file {:?} for hashing", path),
}
let mut hasher = Sha3_256::new();
let mut filedata = String::with_capacity(2048);
let mut filedata = Vec::with_capacity(2048);
let mut changed = false;
// we read only smal files, so its fast to read the whole file to memory, so we can also du a diff
if let Ok(file_contents) = fs::read_to_string(path) {
if let Ok(file_contents) = fs::read(path) {
if let Some(re) = re {
trace!("Filter lines with regex {:?}", re);
for line in file_contents.lines() {
for line in file_contents.split_inclusive(|&b| b == b'\n') {
if re.is_match(line) {
trace!("line {} skipped by filter regex", line);
trace!("line {} skipped by filter regex", String::from_utf8_lossy(line).trim());
continue;
}
// Update the hasher with the bytes
filedata.push_str(line);
filedata.extend_from_slice(line);
}
} else {
// we do not have a filter regex, so we could simply paste the file to the hasher
trace!("Hash file without filter regex");
filedata = file_contents;
}
hasher.update(format!("{}\n", filedata).as_bytes());
// Debug to find errors
//trace!("Hasher input data {:?}",String::from_utf8_lossy(&filedata));
// Update the hasher with the bytes
hasher.update(&filedata);
if !oldfile.is_empty() && *oldfile != filedata {
diff_string.clear();
for diff in diff::lines(oldfile, &filedata) {
let ofs = String::from_utf8_lossy(oldfile);
let nfs = String::from_utf8_lossy(&filedata);
for diff in diff::lines(&ofs, &nfs) {
match diff {
Result::Left(l) => {
trace!("Diff - {}", l); // Removed line

View File

@@ -1,5 +1,6 @@
use log::{debug, error, info};
use regex::Regex;
use regex::bytes::Regex as BRegex;
pub(crate) fn compile_re(regex: Option<String>, name: &str) -> Option<Regex> {
if let Some(r) = regex {
@@ -19,3 +20,22 @@ pub(crate) fn compile_re(regex: Option<String>, name: &str) -> Option<Regex> {
None
}
}
pub(crate) fn compile_re_bin(regex: Option<String>, name: &str) -> Option<BRegex> {
if let Some(r) = regex {
let re = BRegex::new(&r);
match re {
Ok(r) => {
debug!("Sucessfull compiled {} filter regex: {:?}", name, r);
Some(r)
}
Err(e) => {
error!("Error compiling {} filter regex: {:?}", name, e);
None
}
}
} else {
info!("No filter regex for {} supplied", name);
None
}
}

View File

@@ -19,7 +19,7 @@ pub mod snmp;
use bonding::bonding_status;
use config::DataFunctionsFilesum;
use filesum::filesum_filtered;
use helper::compile_re;
use helper::{compile_re, compile_re_bin};
use multipath::multipath_status;
use processes::Ptypes;
use snmp::{Oid, OidData, SnmpData};
@@ -189,10 +189,10 @@ fn t_filesum(
#[cfg(target_os = "linux")]
{
// allocate some strings for holding file contents between check runs
let mut oldpasswd = String::with_capacity(2048);
let mut oldshadow = String::with_capacity(2048);
let mut oldgroup = String::with_capacity(2048);
let mut oldauthkey = String::with_capacity(2048);
let mut oldpasswd = Vec::with_capacity(2048);
let mut oldshadow = Vec::with_capacity(2048);
let mut oldgroup = Vec::with_capacity(2048);
let mut oldauthkey = Vec::with_capacity(2048);
let mut hash_passwd = String::with_capacity(128);
let mut diff_passwd = String::with_capacity(2048);
let mut hash_shadow = String::with_capacity(128);
@@ -203,10 +203,10 @@ fn t_filesum(
let mut diff_authkey = String::with_capacity(2048);
// allocate Option<Regex> for our regex ...
let re_passwd = compile_re(options.passwd, "passwd");
let re_shadow = compile_re(options.shadow, "shadow");
let re_group = compile_re(options.group, "group");
let re_authkey = compile_re(options.authorized_keys, "authorized_keys");
let re_passwd = compile_re_bin(options.passwd, "passwd");
let re_shadow = compile_re_bin(options.shadow, "shadow");
let re_group = compile_re_bin(options.group, "group");
let re_authkey = compile_re_bin(options.authorized_keys, "authorized_keys");
// prepare variables which we use in the whole function
let oid_filesum_time = Oid::from_str("6.1.0").unwrap();

View File

@@ -49,7 +49,7 @@ impl Oid {
x
}
/* pub fn add_suffix(&self, o: &Oid) -> Oid {
/* pub fn add_suffix(&self, o: &Oid) -> Oid {
let mut x = self.clone();
x.0.extend(o.clone().0);
x
@@ -111,7 +111,7 @@ pub struct OidData {
}
impl OidData {
/*
/*
pub fn new(base_oid: &str) -> Self {
Self {
base: base_oid.parse().expect("Unable to parse Oid"),