From 74ae69e6d611d1afd611e586cb226e9bee998031 Mon Sep 17 00:00:00 2001 From: Marc 'risson' Schmitt Date: Sun, 1 Dec 2024 16:42:32 +0100 Subject: [PATCH] kadmin: params, db_args: impl Clone, Sync, Send (#62) --- kadmin/src/db_args.rs | 26 +++++++++ kadmin/src/params.rs | 124 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 135 insertions(+), 15 deletions(-) diff --git a/kadmin/src/db_args.rs b/kadmin/src/db_args.rs index adfd913..9fec7d0 100644 --- a/kadmin/src/db_args.rs +++ b/kadmin/src/db_args.rs @@ -28,6 +28,32 @@ pub struct DbArgs { _ptr_vec: Vec<*mut c_char>, } +// Pointees are contained in the struct itself +unsafe impl Send for DbArgs {} +unsafe impl Sync for DbArgs {} + +impl Clone for DbArgs { + fn clone(&self) -> Self { + let mut _origin_args = vec![]; + let mut _ptr_vec = vec![]; + for arg in &self._origin_args { + let c_arg = arg.clone(); + _ptr_vec.push(c_arg.as_ptr().cast_mut()); + _origin_args.push(c_arg); + } + // Null terminated + _ptr_vec.push(null_mut()); + + let db_args = _ptr_vec.as_mut_ptr(); + + DbArgs { + db_args, + _origin_args, + _ptr_vec, + } + } +} + impl DbArgs { /// Construct a new [`DbArgsBuilder`] pub fn builder() -> DbArgsBuilder { diff --git a/kadmin/src/params.rs b/kadmin/src/params.rs index 5f2ee9e..055aba5 100644 --- a/kadmin/src/params.rs +++ b/kadmin/src/params.rs @@ -23,8 +23,97 @@ pub struct Params { /// doesn't become invalid while this struct lives. pub(crate) params: kadm5_config_params, - /// Store [`CString`] that are in `params` - _strings: Vec>, + /// Store [`CString`] that is in `params` + _realm: Option, + /// Store [`CString`] that is in `params` + _admin_server: Option, + /// Store [`CString`] that is in `params` + _dbname: Option, + /// Store [`CString`] that is in `params` + _acl_file: Option, + /// Store [`CString`] that is in `params` + _dict_file: Option, + /// Store [`CString`] that is in `params` + _stash_file: Option, +} + +// Pointees are contained in the struct itself +unsafe impl Send for Params {} +unsafe impl Sync for Params {} + +impl Clone for Params { + fn clone(&self) -> Self { + let _realm = self._realm.clone(); + let _admin_server = self._admin_server.clone(); + let _dbname = self._dbname.clone(); + let _acl_file = self._acl_file.clone(); + let _dict_file = self._dict_file.clone(); + let _stash_file = self._stash_file.clone(); + Self { + params: kadm5_config_params { + mask: self.params.mask, + realm: if let Some(realm) = &_realm { + realm.as_ptr().cast_mut() + } else { + null_mut() + }, + kadmind_port: self.params.kadmind_port, + kpasswd_port: self.params.kpasswd_port, + + admin_server: if let Some(admin_server) = &_admin_server { + admin_server.as_ptr().cast_mut() + } else { + null_mut() + }, + + dbname: if let Some(dbname) = &_dbname { + dbname.as_ptr().cast_mut() + } else { + null_mut() + }, + acl_file: if let Some(acl_file) = &_acl_file { + acl_file.as_ptr().cast_mut() + } else { + null_mut() + }, + dict_file: if let Some(dict_file) = &_dict_file { + dict_file.as_ptr().cast_mut() + } else { + null_mut() + }, + mkey_from_kbd: 0, + stash_file: if let Some(stash_file) = &_stash_file { + stash_file.as_ptr().cast_mut() + } else { + null_mut() + }, + mkey_name: null_mut(), + enctype: 0, + max_life: 0, + max_rlife: 0, + expiration: 0, + flags: 0, + keysalts: null_mut(), + num_keysalts: 0, + kvno: 0, + iprop_enabled: 0, + iprop_ulogsize: 0, + iprop_poll_time: 0, + iprop_logfile: null_mut(), + iprop_port: 0, + iprop_resync_timeout: 0, + kadmind_listen: null_mut(), + kpasswd_listen: null_mut(), + iprop_listen: null_mut(), + }, + _realm, + _admin_server, + _dbname, + _acl_file, + _dict_file, + _stash_file, + } + } } impl Params { @@ -124,16 +213,16 @@ impl ParamsBuilder { /// Construct [`Params`] from the provided options pub fn build(self) -> Result { - let realm = self.realm.map(CString::new).transpose()?; - let admin_server = self.admin_server.map(CString::new).transpose()?; - let dbname = self.dbname.map(CString::new).transpose()?; - let acl_file = self.acl_file.map(CString::new).transpose()?; - let dict_file = self.dict_file.map(CString::new).transpose()?; - let stash_file = self.stash_file.map(CString::new).transpose()?; + let _realm = self.realm.map(CString::new).transpose()?; + let _admin_server = self.admin_server.map(CString::new).transpose()?; + let _dbname = self.dbname.map(CString::new).transpose()?; + let _acl_file = self.acl_file.map(CString::new).transpose()?; + let _dict_file = self.dict_file.map(CString::new).transpose()?; + let _stash_file = self.stash_file.map(CString::new).transpose()?; let params = kadm5_config_params { mask: self.mask, - realm: if let Some(realm) = &realm { + realm: if let Some(realm) = &_realm { realm.as_ptr().cast_mut() } else { null_mut() @@ -141,29 +230,29 @@ impl ParamsBuilder { kadmind_port: self.kadmind_port, kpasswd_port: self.kpasswd_port, - admin_server: if let Some(admin_server) = &admin_server { + admin_server: if let Some(admin_server) = &_admin_server { admin_server.as_ptr().cast_mut() } else { null_mut() }, - dbname: if let Some(dbname) = &dbname { + dbname: if let Some(dbname) = &_dbname { dbname.as_ptr().cast_mut() } else { null_mut() }, - acl_file: if let Some(acl_file) = &acl_file { + acl_file: if let Some(acl_file) = &_acl_file { acl_file.as_ptr().cast_mut() } else { null_mut() }, - dict_file: if let Some(dict_file) = &dict_file { + dict_file: if let Some(dict_file) = &_dict_file { dict_file.as_ptr().cast_mut() } else { null_mut() }, mkey_from_kbd: 0, - stash_file: if let Some(stash_file) = &stash_file { + stash_file: if let Some(stash_file) = &_stash_file { stash_file.as_ptr().cast_mut() } else { null_mut() @@ -190,7 +279,12 @@ impl ParamsBuilder { Ok(Params { params, - _strings: vec![realm, admin_server, dbname, acl_file, dict_file, stash_file], + _realm, + _admin_server, + _dbname, + _acl_file, + _dict_file, + _stash_file, }) } }