]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
ftp: Move config file handling to Rust
authorJeff Lucovsky <jlucovsky@oisf.net>
Sun, 2 Feb 2025 16:10:59 +0000 (11:10 -0500)
committerVictor Julien <victor@inliniac.net>
Tue, 25 Feb 2025 14:49:29 +0000 (15:49 +0100)
Issue: 4082

Move the configuration file handling to Rust.

These changes will no longer terminate Suricata when there's an invalid
value for ftp.memcap. Like earlier Suricata releases, an error message
is logged "Invalid value <value> for ftp.memcap" but Suricata will no
longer terminate execution. It will use a default value of "0" instead.

doc/userguide/upgrade.rst
rust/src/ftp/ftp.rs
src/app-layer-ftp.c

index 282c5a0cf1de4b476f1fe4d4feab962e6c97f409..e8a3410c04b46c0f76cdb24cbf93dd29a3431b4b 100644 (file)
@@ -95,6 +95,9 @@ Major changes
   will need to be set before external modules can be loaded. See the
   new default configuration file or :ref:`lua-output-yaml` for more
   details.
+- If the configuration value ``ftp.memcap`` is invalid, Suricata will set it to ``0`` which means
+  no limit will be placed. In previous Suricata  releases, Suricata would terminate execution. A
+  warning message will be displayed `Invalid value <value> for ftp.memcap` when this occurs.
 
 Removals
 ~~~~~~~~
index 2cc45660774237d882b31aa5c3d5552e8bb30e3a..3963a2adfa770a5ef1c409607db1e84c3d9cf4e6 100644 (file)
@@ -19,6 +19,7 @@ use std;
 use std::ffi::CString;
 use std::os::raw::{c_char, c_int, c_void};
 
+use crate::conf::{conf_get, get_memval};
 use crate::ftp::constant::*;
 use lazy_static::lazy_static;
 
@@ -182,6 +183,49 @@ impl FtpTransferCmd {
     }
 }
 
+#[no_mangle]
+pub unsafe extern "C" fn SCFTPGetConfigValues(
+    memcap: *mut u64, max_tx: *mut u32, max_line_len: *mut u32,
+) {
+    if let Some(val) = conf_get("app-layer.protocols.ftp.memcap") {
+        if let Ok(v) = get_memval(val) {
+            *memcap = v;
+            SCLogConfig!("FTP memcap: {}", v);
+        } else {
+            SCLogWarning!(
+                "Invalid value {} for ftp.memcap; defaulting to {}",
+                val,
+                *memcap
+            );
+        }
+    }
+    if let Some(val) = conf_get("app-layer.protocols.ftp.max-tx") {
+        if let Ok(v) = val.parse::<u32>() {
+            *max_tx = v;
+            SCLogConfig!("FTP max tx: {}", v);
+        } else {
+            SCLogWarning!(
+                "Invalid value {} for ftp.max-tx; defaulting to {}",
+                val,
+                *max_tx
+            );
+        }
+    }
+    // This value is often expressed with a unit suffix, e.g., 5kb, hence get_memval
+    if let Some(val) = conf_get("app-layer.protocols.ftp.max-line-length") {
+        if let Ok(v) = get_memval(val) {
+            *max_line_len = v as u32;
+            SCLogConfig!("FTP max line length: {}", v);
+        } else {
+            SCLogWarning!(
+                "Invalid value {} for ftp.max-line-length; defaulting to {}",
+                val,
+                *max_line_len
+            );
+        }
+    }
+}
+
 /// Returns *mut FtpTransferCmd
 #[no_mangle]
 pub unsafe extern "C" fn SCFTPTransferCmdNew() -> *mut FtpTransferCmd {
index 682ee2b42a2ce01f197875fca6c1f915f048c289..29df14e530949dfdd44ea687ae28c6e6e5e30c18 100644 (file)
@@ -58,41 +58,10 @@ static FTPTransaction *FTPGetOldestTx(const FtpState *, FTPTransaction *);
 
 static void FTPParseMemcap(void)
 {
-    const char *conf_val;
-
-    /** set config values for memcap, prealloc and hash_size */
-    if ((ConfGet("app-layer.protocols.ftp.memcap", &conf_val)) == 1)
-    {
-        if (ParseSizeStringU64(conf_val, &ftp_config_memcap) < 0) {
-            SCLogError("Error parsing ftp.memcap "
-                       "from conf file - %s.  Killing engine",
-                    conf_val);
-            exit(EXIT_FAILURE);
-        }
-        SCLogInfo("FTP memcap: %"PRIu64, ftp_config_memcap);
-    } else {
-        /* default to unlimited */
-        ftp_config_memcap = 0;
-    }
+    SCFTPGetConfigValues(&ftp_config_memcap, &ftp_config_maxtx, &ftp_max_line_len);
 
     SC_ATOMIC_INIT(ftp_memuse);
     SC_ATOMIC_INIT(ftp_memcap);
-
-    if ((ConfGet("app-layer.protocols.ftp.max-tx", &conf_val)) == 1) {
-        if (ParseSizeStringU32(conf_val, &ftp_config_maxtx) < 0) {
-            SCLogError("Error parsing ftp.max-tx "
-                       "from conf file - %s.",
-                    conf_val);
-        }
-        SCLogInfo("FTP max tx: %" PRIu32, ftp_config_maxtx);
-    }
-
-    if ((ConfGet("app-layer.protocols.ftp.max-line-length", &conf_val)) == 1) {
-        if (ParseSizeStringU32(conf_val, &ftp_max_line_len) < 0) {
-            SCLogError("Error parsing ftp.max-line-length from conf file - %s.", conf_val);
-        }
-        SCLogConfig("FTP max line length: %" PRIu32, ftp_max_line_len);
-    }
 }
 
 static void FTPIncrMemuse(uint64_t size)