]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
Add talloc stackframe handling
authorDavid Mulder <dmulder@samba.org>
Fri, 23 Aug 2024 14:02:01 +0000 (08:02 -0600)
committerDavid Mulder <dmulder@samba.org>
Wed, 23 Oct 2024 14:21:34 +0000 (14:21 +0000)
This appeases errors from libsmbconf that no
talloc stackframe was created.

Signed-off-by: David Mulder <dmulder@samba.org>
Reviewed-by: Alexander Bokovoy <ab@samba.org>
rust/Cargo.lock
rust/Cargo.toml
rust/chelps/src/lib.rs
rust/config/additions.h
rust/config/src/lib.rs
rust/dbg/src/lib.rs
rust/himmelblaud/Cargo.toml
rust/himmelblaud/src/main.rs
rust/talloc/Cargo.toml [new file with mode: 0644]
rust/talloc/build.rs [new file with mode: 0644]
rust/talloc/src/lib.rs [new file with mode: 0644]

index 6f6b3ea31cecbb862728cb05765f3a4f018c6788..393707f07e7fecd9fafc3d39f8b60acefba3376f 100644 (file)
@@ -818,6 +818,7 @@ dependencies = [
  "serde",
  "serde_json",
  "sock",
+ "talloc",
  "tdb",
  "tempfile",
  "tokio",
@@ -2138,6 +2139,15 @@ dependencies = [
  "libc",
 ]
 
+[[package]]
+name = "talloc"
+version = "4.21.0"
+dependencies = [
+ "bindgen",
+ "chelps",
+ "config",
+]
+
 [[package]]
 name = "tdb"
 version = "4.21.0"
index 91d0ccbd9c8ea74ed9f4309f4b4d080f34514a47..c32050b9dd00527d40cb63bea09ab7b39bcb0f68 100644 (file)
@@ -9,7 +9,7 @@ resolver = "2"
 members = [
   "chelps", "config", "dbg", "himmelblaud", "idmap",
   "nss", "ntstatus_gen", "pam",
-  "param", "sock", "tdb", "version",
+  "param", "sock", "talloc", "tdb", "version",
 ]
 
 [workspace.dependencies]
index a702dc350673b33b55e2afc051960b40cbb1ba83..79be4f03e5e4551955504243446c87eea1fd6af1 100644 (file)
@@ -50,6 +50,27 @@ pub unsafe fn string_free(input: *mut c_char) {
     }
 }
 
+#[macro_export]
+macro_rules! function {
+    () => {{
+        fn f() {}
+        fn type_name_of<T>(_: T) -> &'static str {
+            std::any::type_name::<T>()
+        }
+        let name = type_name_of(f);
+
+        let base_name = match name.rfind("::") {
+            Some(pos) => &name[..pos],
+            None => name,
+        };
+        let parts: Vec<&str> = base_name
+            .split("::")
+            .filter(|&p| p != "{{closure}}")
+            .collect();
+        parts.join("::")
+    }};
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;
index c9561349028cbf4a535659cc6b3afc0f906a05db..96a49509ac4592a485cee1ebe0ab08bd830209cc 100644 (file)
@@ -2,3 +2,7 @@
 #ifndef USING_SYSTEM_TDB
 #define USING_SYSTEM_TDB 0
 #endif
+
+#ifndef USING_SYSTEM_TALLOC
+#define USING_SYSTEM_TALLOC 0
+#endif
index 2f1d2f84a4388e07782c85b75cd996e20417ace7..a8546eecf65491537a2d66e12e056c401ceda941 100644 (file)
@@ -40,4 +40,13 @@ mod tests {
             USING_SYSTEM_TDB
         );
     }
+
+    #[test]
+    fn test_using_system_talloc() {
+        assert!(
+            USING_SYSTEM_TALLOC == 0 || USING_SYSTEM_TALLOC == 1,
+            "Unexpected value for USING_SYSTEM_TALLOC: {}",
+            USING_SYSTEM_TALLOC
+        );
+    }
 }
index 2104cdb0d1c0e5ff5fb8602a761984812dd29c3a..30b661358115c51013fb278e1192b1f65a53e456 100644 (file)
@@ -79,34 +79,13 @@ macro_rules! debuglevel_set {
     }};
 }
 
-#[macro_export]
-macro_rules! function {
-    () => {{
-        fn f() {}
-        fn type_name_of<T>(_: T) -> &'static str {
-            std::any::type_name::<T>()
-        }
-        let name = type_name_of(f);
-
-        let base_name = match name.rfind("::") {
-            Some(pos) => &name[..pos],
-            None => name,
-        };
-        let parts: Vec<&str> = base_name
-            .split("::")
-            .filter(|&p| p != "{{closure}}")
-            .collect();
-        parts.join("::")
-    }};
-}
-
 #[macro_export]
 macro_rules! DBG_PREFIX {
     ($level:expr $(, $arg:expr)* $(,)?) => {{
         if $level <= $crate::ffi::MAX_DEBUG_LEVEL {
             let location = format!("{}:{}", file!(), line!());
             let location_cstr = chelps::wrap_string(&location);
-            let function = $crate::function!();
+            let function = chelps::function!();
             let function_msg = format!("{}: ", function);
             let function_cstr = chelps::wrap_string(&function);
             let function_msg_cstr = chelps::wrap_string(&function_msg);
index 5806e51af55af578cc263ea1a9e4feaf19d09683..dedde9e1f76e502f3e5a9a4f7c188864ee7399c2 100644 (file)
@@ -23,6 +23,7 @@ futures = "0.3.30"
 serde = "1.0.204"
 idmap = { workspace = true }
 libc = { workspace = true }
+talloc = { version = "4.21.0", path = "../talloc" }
 
 [build-dependencies]
 version = { path = "../version" }
index 42140a9709c71e4d2244590e839c672a0178ca08..bc9f2f4f6a4a97ab01ae460a2bce428f3ef3481f 100644 (file)
@@ -82,6 +82,8 @@ async fn main() -> ExitCode {
     let quit_now = Arc::clone(&stop_now);
     let interrupt_now = Arc::clone(&stop_now);
 
+    let frame = talloc::talloc_stackframe!();
+
     async {
         // Set the command line debug level
         if let Some(debuglevel) = clap_args.get_one::<u16>("debuglevel") {
@@ -97,6 +99,7 @@ async fn main() -> ExitCode {
             Ok(lp) => lp,
             Err(e) => {
                 eprintln!("Failed loading smb.conf: {:?}", e);
+                talloc::TALLOC_FREE!(frame);
                 return ExitCode::FAILURE;
             }
         };
@@ -111,6 +114,7 @@ async fn main() -> ExitCode {
                     "The realm MUST be set in the \
                     smb.conf to start himmelblaud"
                 );
+                talloc::TALLOC_FREE!(frame);
                 return ExitCode::FAILURE;
             }
         };
@@ -125,6 +129,7 @@ async fn main() -> ExitCode {
                     Ok(Some(logfile)) => debug_set_logfile(&logfile),
                     _ => {
                         eprintln!("Failed to determine logfile name");
+                        talloc::TALLOC_FREE!(frame);
                         return ExitCode::FAILURE;
                     }
                 }
@@ -134,14 +139,20 @@ async fn main() -> ExitCode {
         // Determine the unix socket path
         let sock_dir_str = match lp.winbindd_socket_directory() {
             Ok(Some(sock_dir)) => sock_dir,
-            _ => return ExitCode::FAILURE,
+            _ => {
+                talloc::TALLOC_FREE!(frame);
+                return ExitCode::FAILURE;
+            }
         };
         let sock_dir = Path::new(&sock_dir_str);
         let mut sock_path = PathBuf::from(sock_dir);
         sock_path.push("hb_pipe");
         let sock_path = match sock_path.to_str() {
             Some(sock_path) => sock_path,
-            None => return ExitCode::FAILURE,
+            None => {
+                talloc::TALLOC_FREE!(frame);
+                return ExitCode::FAILURE;
+            }
         };
 
         // Initialize the Himmelblau cache
@@ -149,6 +160,7 @@ async fn main() -> ExitCode {
             Ok(Some(private_cache_path)) => private_cache_path,
             _ => {
                 DBG_ERR!("Failed to determine private cache path");
+                talloc::TALLOC_FREE!(frame);
                 return ExitCode::FAILURE;
             }
         };
@@ -159,6 +171,7 @@ async fn main() -> ExitCode {
                 "The private directory '{}' does not exist",
                 private_dir.display()
             );
+            talloc::TALLOC_FREE!(frame);
             return ExitCode::FAILURE;
         }
         let mut pcache = match PrivateCache::new(&private_cache_path) {
@@ -168,6 +181,7 @@ async fn main() -> ExitCode {
                     "Failed to open the himmelblau private cache: {:?}",
                     e
                 );
+                talloc::TALLOC_FREE!(frame);
                 return ExitCode::FAILURE;
             }
         };
@@ -176,11 +190,13 @@ async fn main() -> ExitCode {
             Ok(Some(cache_dir)) => cache_dir,
             _ => {
                 DBG_ERR!("Failed to determine cache directory");
+                talloc::TALLOC_FREE!(frame);
                 return ExitCode::FAILURE;
             }
         };
         if !Path::new(&cache_dir).exists() {
             DBG_ERR!("The cache directory '{}' does not exist", cache_dir);
+            talloc::TALLOC_FREE!(frame);
             return ExitCode::FAILURE;
         }
 
@@ -192,6 +208,7 @@ async fn main() -> ExitCode {
             Ok(cache) => cache,
             Err(e) => {
                 DBG_ERR!("Failed to open the himmelblau user cache: {:?}", e);
+                talloc::TALLOC_FREE!(frame);
                 return ExitCode::FAILURE;
             }
         };
@@ -204,6 +221,7 @@ async fn main() -> ExitCode {
             Ok(cache) => cache,
             Err(e) => {
                 DBG_ERR!("Failed to open the himmelblau uid cache: {:?}", e);
+                talloc::TALLOC_FREE!(frame);
                 return ExitCode::FAILURE;
             }
         };
@@ -216,6 +234,7 @@ async fn main() -> ExitCode {
             Ok(cache) => cache,
             Err(e) => {
                 DBG_ERR!("Failed to open the himmelblau group cache: {:?}", e);
+                talloc::TALLOC_FREE!(frame);
                 return ExitCode::FAILURE;
             }
         };
@@ -225,6 +244,7 @@ async fn main() -> ExitCode {
             Ok(Some(hsm_pin_path)) => hsm_pin_path,
             _ => {
                 DBG_ERR!("Failed loading hsm pin path.");
+                talloc::TALLOC_FREE!(frame);
                 return ExitCode::FAILURE;
             }
         };
@@ -235,6 +255,7 @@ async fn main() -> ExitCode {
                 "The hsm pin directory '{}' does not exist",
                 hsm_pin_dir.display()
             );
+            talloc::TALLOC_FREE!(frame);
             return ExitCode::FAILURE;
         }
         let auth_value =
@@ -242,6 +263,7 @@ async fn main() -> ExitCode {
                 Ok(auth_value) => auth_value,
                 Err(e) => {
                     DBG_ERR!("{:?}", e);
+                    talloc::TALLOC_FREE!(frame);
                     return ExitCode::FAILURE;
                 }
             };
@@ -255,6 +277,7 @@ async fn main() -> ExitCode {
             Ok(lmk) => lmk,
             Err(e) => {
                 DBG_ERR!("{:?}", e);
+                talloc::TALLOC_FREE!(frame);
                 return ExitCode::FAILURE;
             }
         };
@@ -270,6 +293,7 @@ async fn main() -> ExitCode {
                     private_cache_path
                 );
                 DBG_INFO!("The host will forget domain enrollments.");
+                talloc::TALLOC_FREE!(frame);
                 return ExitCode::FAILURE;
             }
         };
@@ -286,6 +310,7 @@ async fn main() -> ExitCode {
             Ok(graph) => graph,
             Err(e) => {
                 DBG_ERR!("Failed initializing the graph: {:?}", e);
+                talloc::TALLOC_FREE!(frame);
                 return ExitCode::FAILURE;
             }
         };
@@ -301,6 +326,7 @@ async fn main() -> ExitCode {
             Ok(client) => client,
             Err(e) => {
                 DBG_ERR!("Failed initializing the broker: {:?}", e);
+                talloc::TALLOC_FREE!(frame);
                 return ExitCode::FAILURE;
             }
         };
@@ -309,6 +335,7 @@ async fn main() -> ExitCode {
             Ok(idmap) => idmap,
             Err(e) => {
                 DBG_ERR!("Failed initializing the idmapper: {:?}", e);
+                talloc::TALLOC_FREE!(frame);
                 return ExitCode::FAILURE;
             }
         };
@@ -317,11 +344,13 @@ async fn main() -> ExitCode {
             Ok(res) => res,
             Err(e) => {
                 DBG_ERR!("Failed fetching idmap range: {:?}", e);
+                talloc::TALLOC_FREE!(frame);
                 return ExitCode::FAILURE;
             }
         };
         if let Err(e) = idmap.add_gen_domain(&realm, &tenant_id, (low, high)) {
             DBG_ERR!("Failed adding the domain idmap range: {:?}", e);
+            talloc::TALLOC_FREE!(frame);
             return ExitCode::FAILURE;
         }
 
@@ -345,6 +374,7 @@ async fn main() -> ExitCode {
             Ok(listener) => listener,
             Err(e) => {
                 DBG_ERR!("Failed setting up the socket listener: {:?}", e);
+                talloc::TALLOC_FREE!(frame);
                 return ExitCode::FAILURE;
             }
         };
@@ -428,6 +458,7 @@ async fn main() -> ExitCode {
             }
         }
 
+        talloc::TALLOC_FREE!(frame);
         ExitCode::SUCCESS
     }
     .await
diff --git a/rust/talloc/Cargo.toml b/rust/talloc/Cargo.toml
new file mode 100644 (file)
index 0000000..a21b3da
--- /dev/null
@@ -0,0 +1,13 @@
+[package]
+name = "talloc"
+edition.workspace = true
+license.workspace = true
+homepage.workspace = true
+version.workspace = true
+
+[build-dependencies]
+bindgen = "0.69.4"
+config.workspace = true
+
+[dependencies]
+chelps.workspace = true
diff --git a/rust/talloc/build.rs b/rust/talloc/build.rs
new file mode 100644 (file)
index 0000000..d14b0b0
--- /dev/null
@@ -0,0 +1,38 @@
+use std::env;
+use std::path::PathBuf;
+
+fn main() {
+    let bindings = bindgen::Builder::default()
+        .blocklist_function("qgcvt")
+        .blocklist_function("qgcvt_r")
+        .blocklist_function("qfcvt")
+        .blocklist_function("qfcvt_r")
+        .blocklist_function("qecvt")
+        .blocklist_function("qecvt_r")
+        .blocklist_function("strtold")
+        .clang_arg("-Dbool=int")
+        .generate_comments(false)
+        .clang_arg("-I../../lib/talloc")
+        .header("../../lib/util/talloc_stack.h")
+        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
+        .generate()
+        .expect("Unable to generate bindings");
+    println!("cargo:rerun-if-changed=../../lib/util/talloc_stack.h");
+
+    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
+    bindings
+        .write_to_file(out_path.join("bindings.rs"))
+        .expect("Couldn't write bindings!");
+
+    let mut src_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
+    src_dir.push("../../bin/default/lib/talloc");
+    if config::USING_SYSTEM_TALLOC == 1 {
+        println!("cargo:rustc-link-lib=talloc");
+    } else {
+        println!("cargo:rustc-link-lib=talloc-private-samba");
+    }
+    println!(
+        "cargo:rustc-link-search=native={}",
+        src_dir.to_str().unwrap()
+    );
+}
diff --git a/rust/talloc/src/lib.rs b/rust/talloc/src/lib.rs
new file mode 100644 (file)
index 0000000..c2eef6f
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+   Unix SMB/CIFS implementation.
+
+   Talloc stackframe functions
+
+   Copyright (C) David Mulder 2024
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+pub mod ffi {
+    #![allow(non_upper_case_globals)]
+    #![allow(non_camel_case_types)]
+    #![allow(non_snake_case)]
+    #![allow(dead_code)]
+    #![allow(clippy::upper_case_acronyms)]
+    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
+}
+
+#[macro_export]
+macro_rules! talloc_stackframe {
+    () => {{
+        let function = chelps::function!();
+        let function_cstr = chelps::wrap_string(&function);
+        unsafe {
+            let ret = $crate::ffi::_talloc_stackframe(function_cstr);
+            chelps::string_free(function_cstr);
+            ret
+        }
+    }};
+}
+
+#[macro_export]
+macro_rules! TALLOC_FREE {
+    ($ctx:ident) => {{
+        if !$ctx.is_null() {
+            let function = chelps::function!();
+            let function_cstr = chelps::wrap_string(&function);
+            unsafe {
+                $crate::ffi::_talloc_free($ctx, function_cstr);
+                chelps::string_free(function_cstr);
+            }
+        }
+    }};
+}