diff --git a/.vscode/settings.json b/.vscode/settings.json index 4312bfe05..eb230f7ba 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,3 @@ { - "rust-analyzer.checkOnSave.allTargets": false, - "rust-analyzer.cargo.target":"riscv32imac-unknown-xous-elf" + "rust-analyzer.checkOnSave.allTargets": false } diff --git a/Cargo.lock b/Cargo.lock index e01ce8511..bb773708d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2029,6 +2029,7 @@ dependencies = [ "num-derive", "num-traits", "oqc-test", + "pddb", "rand_core 0.5.1", "rkyv", "root-keys", diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index 0cbedb970..e769534b6 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -71,6 +71,8 @@ pub(crate) enum Opcode { /// warning, the Delete routine has not been well tested DeleteBasis, + CreateDict, + KeyRequest, ReadKey, @@ -90,7 +92,7 @@ pub struct PddbBasisList { pub num: u32, } #[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] -pub enum PddbBasisMgmtCode { +pub enum PddbRequestCode { Create, Open, Close, @@ -104,11 +106,18 @@ pub enum PddbBasisMgmtCode { Uninit, } #[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] -pub struct PddbBasisMgmt { - /// the first 63 that fit in the list -- generally we anticipate not more than a few being open at a time, so this should be enough. +pub struct PddbBasisRequest { pub name: [u8; BASIS_NAME_LEN], - /// total number of basis open. Should be <= 63, but we allow it to be larger to indicate cases where this structure wasn't big enough. - pub code: PddbBasisMgmtCode, + pub code: PddbRequestCode, +} + +#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +pub struct PddbDictRequest { + /// applications shouldn't specify a basis so that the PD mechanism works as intended, but non-sensitive system config keys will want to generally specify the system basis. + pub basis_specified: bool, + pub basis_name: [u8; BASIS_NAME_LEN], + pub dict_name: [u8; DICT_NAME_LEN], + pub code: PddbRequestCode, } /// A structure for requesting a token to access a particular key/value pair diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index 89f7bb5ca..0a1c52c90 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -51,18 +51,18 @@ impl PddbBasisManager { /// returns the latest basis that is opened -- this is where all new values are being sent by default /// if the PDDB is not mounted, returns None pub fn latest_basis(&self) -> Option { - let mgmt = PddbBasisMgmt { + let mgmt = PddbBasisRequest { name: [0u8; BASIS_NAME_LEN], - code: PddbBasisMgmtCode::Uninit, + code: PddbRequestCode::Uninit, }; let mut buf = Buffer::into_buf(mgmt).expect("Couldn't convert to memory structure"); buf.lend_mut(self.conn, Opcode::LatestBasis.to_u32().unwrap()).expect("Couldn't execute ListBasis opcode"); - let ret = buf.to_original::().expect("couldn't restore list structure"); + let ret = buf.to_original::().expect("couldn't restore list structure"); match ret.code { - PddbBasisMgmtCode::NoErr => { + PddbRequestCode::NoErr => { Some(cstr_to_string(&ret.name)) } - PddbBasisMgmtCode::NotMounted => { + PddbRequestCode::NotMounted => { None } _ => { @@ -72,18 +72,18 @@ impl PddbBasisManager { } } pub fn create(&self, basis_name: &str) -> Result<()> { - let mut mgmt = PddbBasisMgmt { + let mut mgmt = PddbBasisRequest { name: [0u8; BASIS_NAME_LEN], - code: PddbBasisMgmtCode::Create, + code: PddbRequestCode::Create, }; for (&src, dst) in basis_name.as_bytes().iter().zip(mgmt.name.iter_mut()) {*dst = src} let mut buf = Buffer::into_buf(mgmt).expect("Couldn't convert to memory structure"); buf.lend_mut(self.conn, Opcode::CreateBasis.to_u32().unwrap()).expect("Couldn't execute CreateBasis opcode"); - let ret = buf.to_original::().expect("couldn't restore mgmt structure"); + let ret = buf.to_original::().expect("couldn't restore mgmt structure"); match ret.code { - PddbBasisMgmtCode::NoErr => Ok(()), - PddbBasisMgmtCode::NoFreeSpace => Err(Error::new(ErrorKind::OutOfMemory, "No free space to create basis")), - PddbBasisMgmtCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error creating basis")), + PddbRequestCode::NoErr => Ok(()), + PddbRequestCode::NoFreeSpace => Err(Error::new(ErrorKind::OutOfMemory, "No free space to create basis")), + PddbRequestCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error creating basis")), _ => { log::error!("Invalid return code"); panic!("Invalid return code"); @@ -91,18 +91,18 @@ impl PddbBasisManager { } } pub fn open(&self, basis_name: &str) -> Result<()> { - let mut mgmt = PddbBasisMgmt { + let mut mgmt = PddbBasisRequest { name: [0u8; BASIS_NAME_LEN], - code: PddbBasisMgmtCode::Open, + code: PddbRequestCode::Open, }; for (&src, dst) in basis_name.as_bytes().iter().zip(mgmt.name.iter_mut()) {*dst = src} let mut buf = Buffer::into_buf(mgmt).expect("Couldn't convert to memory structure"); buf.lend_mut(self.conn, Opcode::OpenBasis.to_u32().unwrap()).expect("Couldn't execute OpenBasis opcode"); - let ret = buf.to_original::().expect("couldn't restore mgmt structure"); + let ret = buf.to_original::().expect("couldn't restore mgmt structure"); match ret.code { - PddbBasisMgmtCode::NoErr => Ok(()), - PddbBasisMgmtCode::AccessDenied => Err(Error::new(ErrorKind::PermissionDenied, "Authentication error")), - PddbBasisMgmtCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error creating basis")), + PddbRequestCode::NoErr => Ok(()), + PddbRequestCode::AccessDenied => Err(Error::new(ErrorKind::PermissionDenied, "Authentication error")), + PddbRequestCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error creating basis")), _ => { log::error!("Invalid return code"); panic!("Invalid return code"); @@ -110,18 +110,18 @@ impl PddbBasisManager { } } pub fn close(&self, basis_name: &str) -> Result<()> { - let mut mgmt = PddbBasisMgmt { + let mut mgmt = PddbBasisRequest { name: [0u8; BASIS_NAME_LEN], - code: PddbBasisMgmtCode::Close, + code: PddbRequestCode::Close, }; for (&src, dst) in basis_name.as_bytes().iter().zip(mgmt.name.iter_mut()) {*dst = src} let mut buf = Buffer::into_buf(mgmt).expect("Couldn't convert to memory structure"); buf.lend_mut(self.conn, Opcode::CloseBasis.to_u32().unwrap()).expect("Couldn't execute CloseBasis opcode"); - let ret = buf.to_original::().expect("couldn't restore mgmt structure"); + let ret = buf.to_original::().expect("couldn't restore mgmt structure"); match ret.code { - PddbBasisMgmtCode::NoErr => Ok(()), - PddbBasisMgmtCode::NotFound => Err(Error::new(ErrorKind::NotFound, "Basis not found")), - PddbBasisMgmtCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error closing basis")), + PddbRequestCode::NoErr => Ok(()), + PddbRequestCode::NotFound => Err(Error::new(ErrorKind::NotFound, "Basis not found")), + PddbRequestCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error closing basis")), _ => { log::error!("Invalid return code"); panic!("Invalid return code"); @@ -129,18 +129,18 @@ impl PddbBasisManager { } } pub fn delete(&self, basis_name: &str) -> Result<()> { - let mut mgmt = PddbBasisMgmt { + let mut mgmt = PddbBasisRequest { name: [0u8; BASIS_NAME_LEN], - code: PddbBasisMgmtCode::Delete, + code: PddbRequestCode::Delete, }; for (&src, dst) in basis_name.as_bytes().iter().zip(mgmt.name.iter_mut()) {*dst = src} let mut buf = Buffer::into_buf(mgmt).expect("Couldn't convert to memory structure"); buf.lend_mut(self.conn, Opcode::DeleteBasis.to_u32().unwrap()).expect("Couldn't execute DeleteBasis opcode"); - let ret = buf.to_original::().expect("couldn't restore mgmt structure"); + let ret = buf.to_original::().expect("couldn't restore mgmt structure"); match ret.code { - PddbBasisMgmtCode::NoErr => Ok(()), - PddbBasisMgmtCode::NotFound => Err(Error::new(ErrorKind::NotFound, "Basis not found")), - PddbBasisMgmtCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error deleting basis")), + PddbRequestCode::NoErr => Ok(()), + PddbRequestCode::NotFound => Err(Error::new(ErrorKind::NotFound, "Basis not found")), + PddbRequestCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error deleting basis")), _ => { log::error!("Invalid return code"); panic!("Invalid return code"); @@ -155,19 +155,20 @@ pub struct Pddb<'a> { callback: Option>, } impl<'a> Pddb<'a> { - // opens a dictionary only if it exists - pub fn open(_dict_name: &str) -> Option { None } // creates a dictionary only if it does not already exist - pub fn create(dict_name: &str) -> Option { + pub fn create(dict_name: &str, basis_name: Option<&str>) -> Option { REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); let xns = xous_names::XousNames::new().unwrap(); let conn = xns.request_connection_blocking(api::SERVER_NAME_PDDB).expect("can't connect to Pddb server"); + /*let mut request = PddbDictRequest { + basis_specified: basis_name.is_some(), + }*/ None } /// returns a key only if it exists - pub fn get(&mut self, _key_name: &str, key_changed_cb: impl FnMut() + 'a) -> Result> { + pub fn get(&mut self, dict_name: &str, key_name: &str, basis_name: Option<&str>, key_changed_cb: impl FnMut() + 'a) -> Result> { self.callback = Some(Box::new(key_changed_cb)); Ok(None) } diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 20937dfc1..68818871c 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -378,7 +378,7 @@ mod tests; use tests::*; use num_traits::*; -use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack, send_message, Message}; +use xous::{send_message, Message}; use xous_ipc::Buffer; use core::cell::RefCell; use std::rc::Rc; @@ -566,20 +566,20 @@ fn xmain() -> ! { } Some(Opcode::LatestBasis) => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; - let mut mgmt = buffer.to_original::().unwrap(); + let mut mgmt = buffer.to_original::().unwrap(); if let Some(name) = basis_cache.basis_latest() { for (&src, dst) in name.as_bytes().iter().zip(mgmt.name.iter_mut()) { *dst = src } - mgmt.code = PddbBasisMgmtCode::NoErr; + mgmt.code = PddbRequestCode::NoErr; } else { - mgmt.code = PddbBasisMgmtCode::NotMounted; + mgmt.code = PddbRequestCode::NotMounted; } buffer.replace(mgmt).unwrap(); } Some(Opcode::CreateBasis) => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; - let mut mgmt = buffer.to_original::().unwrap(); + let mut mgmt = buffer.to_original::().unwrap(); match mgmt.code { - PddbBasisMgmtCode::Create => { + PddbRequestCode::Create => { let name = cstr_to_string(&mgmt.name); let request = BasisRequestPassword { db_name: xous_ipc::String::<{crate::api::BASIS_NAME_LEN}>::from_str(name.as_str()), @@ -590,24 +590,24 @@ fn xmain() -> ! { let ret = buf.to_original::().unwrap(); if let Some(pw) = ret.plaintext_pw { match basis_cache.basis_create(&mut pddb_os, &name, pw.as_str().expect("password was not valid utf-8")) { - Ok(_) => mgmt.code = PddbBasisMgmtCode::NoErr, - _ => mgmt.code = PddbBasisMgmtCode::InternalError, + Ok(_) => mgmt.code = PddbRequestCode::NoErr, + _ => mgmt.code = PddbRequestCode::InternalError, } } else { - mgmt.code = PddbBasisMgmtCode::InternalError; + mgmt.code = PddbRequestCode::InternalError; } } _ => { - mgmt.code = PddbBasisMgmtCode::InternalError; + mgmt.code = PddbRequestCode::InternalError; } } buffer.replace(mgmt).unwrap(); } Some(Opcode::OpenBasis) => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; - let mut mgmt = buffer.to_original::().unwrap(); + let mut mgmt = buffer.to_original::().unwrap(); match mgmt.code { - PddbBasisMgmtCode::Open => { + PddbRequestCode::Open => { let name = cstr_to_string(&mgmt.name); let mut finished = false; while !finished { @@ -625,7 +625,7 @@ fn xmain() -> ! { ) { basis_cache.basis_add(basis); finished = true; - mgmt.code = PddbBasisMgmtCode::NoErr; + mgmt.code = PddbRequestCode::NoErr; } } else { modals.add_list_item(t!("pddb.yes", xous::LANG)).expect("couldn't build radio item list"); @@ -637,7 +637,7 @@ fn xmain() -> ! { // this will cause just another go-around } else if response.as_str() == t!("pddb.no", xous::LANG) { finished = true; - mgmt.code = PddbBasisMgmtCode::AccessDenied; // this will cause a return of AccessDenied + mgmt.code = PddbRequestCode::AccessDenied; // this will cause a return of AccessDenied } else { panic!("Got unexpected return from radiobutton"); } @@ -648,50 +648,53 @@ fn xmain() -> ! { } } _ => { - mgmt.code = PddbBasisMgmtCode::InternalError; + mgmt.code = PddbRequestCode::InternalError; } } buffer.replace(mgmt).unwrap(); } Some(Opcode::CloseBasis) => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; - let mut mgmt = buffer.to_original::().unwrap(); + let mut mgmt = buffer.to_original::().unwrap(); match mgmt.code { - PddbBasisMgmtCode::Close => { + PddbRequestCode::Close => { let name = cstr_to_string(&mgmt.name); match basis_cache.basis_unmount(&mut pddb_os, &name) { - Ok(_) => mgmt.code = PddbBasisMgmtCode::NoErr, + Ok(_) => mgmt.code = PddbRequestCode::NoErr, Err(e) => match e.kind() { - ErrorKind::NotFound => mgmt.code = PddbBasisMgmtCode::NotFound, - _ => mgmt.code = PddbBasisMgmtCode::InternalError, + ErrorKind::NotFound => mgmt.code = PddbRequestCode::NotFound, + _ => mgmt.code = PddbRequestCode::InternalError, } } } _ => { - mgmt.code = PddbBasisMgmtCode::InternalError; + mgmt.code = PddbRequestCode::InternalError; } } buffer.replace(mgmt).unwrap(); } Some(Opcode::DeleteBasis) => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; - let mut mgmt = buffer.to_original::().unwrap(); + let mut mgmt = buffer.to_original::().unwrap(); match mgmt.code { - PddbBasisMgmtCode::Delete => { + PddbRequestCode::Delete => { let name = cstr_to_string(&mgmt.name); match basis_cache.basis_delete(&mut pddb_os, &name) { - Ok(_) => mgmt.code = PddbBasisMgmtCode::NoErr, + Ok(_) => mgmt.code = PddbRequestCode::NoErr, Err(e) => match e.kind() { - ErrorKind::NotFound => mgmt.code = PddbBasisMgmtCode::NotFound, - _ => mgmt.code = PddbBasisMgmtCode::InternalError, + ErrorKind::NotFound => mgmt.code = PddbRequestCode::NotFound, + _ => mgmt.code = PddbRequestCode::InternalError, } } } _ => { - mgmt.code = PddbBasisMgmtCode::InternalError; + mgmt.code = PddbRequestCode::InternalError; } } buffer.replace(mgmt).unwrap(); + } + Some(Opcode::CreateDict) => { + } Some(Opcode::KeyRequest) => { // placeholder