getting started on dict management, but just realized we did strings in a dumb way

we really should have coded a length field in all the name string
stored in the PDDB (derp, old C habits die hard).

let's fix that before we move on.
This commit is contained in:
bunnie 2022-01-06 17:12:30 +08:00
parent d318e31666
commit 233a7cb7d3
5 changed files with 80 additions and 67 deletions

View file

@ -1,4 +1,3 @@
{ {
"rust-analyzer.checkOnSave.allTargets": false, "rust-analyzer.checkOnSave.allTargets": false
"rust-analyzer.cargo.target":"riscv32imac-unknown-xous-elf"
} }

1
Cargo.lock generated
View file

@ -2029,6 +2029,7 @@ dependencies = [
"num-derive", "num-derive",
"num-traits", "num-traits",
"oqc-test", "oqc-test",
"pddb",
"rand_core 0.5.1", "rand_core 0.5.1",
"rkyv", "rkyv",
"root-keys", "root-keys",

View file

@ -71,6 +71,8 @@ pub(crate) enum Opcode {
/// warning, the Delete routine has not been well tested /// warning, the Delete routine has not been well tested
DeleteBasis, DeleteBasis,
CreateDict,
KeyRequest, KeyRequest,
ReadKey, ReadKey,
@ -90,7 +92,7 @@ pub struct PddbBasisList {
pub num: u32, pub num: u32,
} }
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] #[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub enum PddbBasisMgmtCode { pub enum PddbRequestCode {
Create, Create,
Open, Open,
Close, Close,
@ -104,11 +106,18 @@ pub enum PddbBasisMgmtCode {
Uninit, Uninit,
} }
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] #[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub struct PddbBasisMgmt { pub struct PddbBasisRequest {
/// 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 name: [u8; BASIS_NAME_LEN], 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: PddbRequestCode,
pub code: PddbBasisMgmtCode, }
#[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 /// A structure for requesting a token to access a particular key/value pair

View file

@ -51,18 +51,18 @@ impl PddbBasisManager {
/// returns the latest basis that is opened -- this is where all new values are being sent by default /// 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 /// if the PDDB is not mounted, returns None
pub fn latest_basis(&self) -> Option<String> { pub fn latest_basis(&self) -> Option<String> {
let mgmt = PddbBasisMgmt { let mgmt = PddbBasisRequest {
name: [0u8; BASIS_NAME_LEN], 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"); 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"); buf.lend_mut(self.conn, Opcode::LatestBasis.to_u32().unwrap()).expect("Couldn't execute ListBasis opcode");
let ret = buf.to_original::<PddbBasisMgmt, _>().expect("couldn't restore list structure"); let ret = buf.to_original::<PddbBasisRequest, _>().expect("couldn't restore list structure");
match ret.code { match ret.code {
PddbBasisMgmtCode::NoErr => { PddbRequestCode::NoErr => {
Some(cstr_to_string(&ret.name)) Some(cstr_to_string(&ret.name))
} }
PddbBasisMgmtCode::NotMounted => { PddbRequestCode::NotMounted => {
None None
} }
_ => { _ => {
@ -72,18 +72,18 @@ impl PddbBasisManager {
} }
} }
pub fn create(&self, basis_name: &str) -> Result<()> { pub fn create(&self, basis_name: &str) -> Result<()> {
let mut mgmt = PddbBasisMgmt { let mut mgmt = PddbBasisRequest {
name: [0u8; BASIS_NAME_LEN], 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} 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"); 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"); buf.lend_mut(self.conn, Opcode::CreateBasis.to_u32().unwrap()).expect("Couldn't execute CreateBasis opcode");
let ret = buf.to_original::<PddbBasisMgmt, _>().expect("couldn't restore mgmt structure"); let ret = buf.to_original::<PddbBasisRequest, _>().expect("couldn't restore mgmt structure");
match ret.code { match ret.code {
PddbBasisMgmtCode::NoErr => Ok(()), PddbRequestCode::NoErr => Ok(()),
PddbBasisMgmtCode::NoFreeSpace => Err(Error::new(ErrorKind::OutOfMemory, "No free space to create basis")), PddbRequestCode::NoFreeSpace => Err(Error::new(ErrorKind::OutOfMemory, "No free space to create basis")),
PddbBasisMgmtCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error creating basis")), PddbRequestCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error creating basis")),
_ => { _ => {
log::error!("Invalid return code"); log::error!("Invalid return code");
panic!("Invalid return code"); panic!("Invalid return code");
@ -91,18 +91,18 @@ impl PddbBasisManager {
} }
} }
pub fn open(&self, basis_name: &str) -> Result<()> { pub fn open(&self, basis_name: &str) -> Result<()> {
let mut mgmt = PddbBasisMgmt { let mut mgmt = PddbBasisRequest {
name: [0u8; BASIS_NAME_LEN], 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} 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"); 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"); buf.lend_mut(self.conn, Opcode::OpenBasis.to_u32().unwrap()).expect("Couldn't execute OpenBasis opcode");
let ret = buf.to_original::<PddbBasisMgmt, _>().expect("couldn't restore mgmt structure"); let ret = buf.to_original::<PddbBasisRequest, _>().expect("couldn't restore mgmt structure");
match ret.code { match ret.code {
PddbBasisMgmtCode::NoErr => Ok(()), PddbRequestCode::NoErr => Ok(()),
PddbBasisMgmtCode::AccessDenied => Err(Error::new(ErrorKind::PermissionDenied, "Authentication error")), PddbRequestCode::AccessDenied => Err(Error::new(ErrorKind::PermissionDenied, "Authentication error")),
PddbBasisMgmtCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error creating basis")), PddbRequestCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error creating basis")),
_ => { _ => {
log::error!("Invalid return code"); log::error!("Invalid return code");
panic!("Invalid return code"); panic!("Invalid return code");
@ -110,18 +110,18 @@ impl PddbBasisManager {
} }
} }
pub fn close(&self, basis_name: &str) -> Result<()> { pub fn close(&self, basis_name: &str) -> Result<()> {
let mut mgmt = PddbBasisMgmt { let mut mgmt = PddbBasisRequest {
name: [0u8; BASIS_NAME_LEN], 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} 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"); 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"); buf.lend_mut(self.conn, Opcode::CloseBasis.to_u32().unwrap()).expect("Couldn't execute CloseBasis opcode");
let ret = buf.to_original::<PddbBasisMgmt, _>().expect("couldn't restore mgmt structure"); let ret = buf.to_original::<PddbBasisRequest, _>().expect("couldn't restore mgmt structure");
match ret.code { match ret.code {
PddbBasisMgmtCode::NoErr => Ok(()), PddbRequestCode::NoErr => Ok(()),
PddbBasisMgmtCode::NotFound => Err(Error::new(ErrorKind::NotFound, "Basis not found")), PddbRequestCode::NotFound => Err(Error::new(ErrorKind::NotFound, "Basis not found")),
PddbBasisMgmtCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error closing basis")), PddbRequestCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error closing basis")),
_ => { _ => {
log::error!("Invalid return code"); log::error!("Invalid return code");
panic!("Invalid return code"); panic!("Invalid return code");
@ -129,18 +129,18 @@ impl PddbBasisManager {
} }
} }
pub fn delete(&self, basis_name: &str) -> Result<()> { pub fn delete(&self, basis_name: &str) -> Result<()> {
let mut mgmt = PddbBasisMgmt { let mut mgmt = PddbBasisRequest {
name: [0u8; BASIS_NAME_LEN], 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} 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"); 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"); buf.lend_mut(self.conn, Opcode::DeleteBasis.to_u32().unwrap()).expect("Couldn't execute DeleteBasis opcode");
let ret = buf.to_original::<PddbBasisMgmt, _>().expect("couldn't restore mgmt structure"); let ret = buf.to_original::<PddbBasisRequest, _>().expect("couldn't restore mgmt structure");
match ret.code { match ret.code {
PddbBasisMgmtCode::NoErr => Ok(()), PddbRequestCode::NoErr => Ok(()),
PddbBasisMgmtCode::NotFound => Err(Error::new(ErrorKind::NotFound, "Basis not found")), PddbRequestCode::NotFound => Err(Error::new(ErrorKind::NotFound, "Basis not found")),
PddbBasisMgmtCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error deleting basis")), PddbRequestCode::InternalError => Err(Error::new(ErrorKind::Other, "Internal error deleting basis")),
_ => { _ => {
log::error!("Invalid return code"); log::error!("Invalid return code");
panic!("Invalid return code"); panic!("Invalid return code");
@ -155,19 +155,20 @@ pub struct Pddb<'a> {
callback: Option<Box<dyn FnMut() + 'a>>, callback: Option<Box<dyn FnMut() + 'a>>,
} }
impl<'a> Pddb<'a> { impl<'a> Pddb<'a> {
// opens a dictionary only if it exists
pub fn open(_dict_name: &str) -> Option<Self> { None }
// creates a dictionary only if it does not already exist // creates a dictionary only if it does not already exist
pub fn create(dict_name: &str) -> Option<Self> { pub fn create(dict_name: &str, basis_name: Option<&str>) -> Option<Self> {
REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
let xns = xous_names::XousNames::new().unwrap(); 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 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 None
} }
/// returns a key only if it exists /// returns a key only if it exists
pub fn get(&mut self, _key_name: &str, key_changed_cb: impl FnMut() + 'a) -> Result<Option<PddbKey>> { pub fn get(&mut self, dict_name: &str, key_name: &str, basis_name: Option<&str>, key_changed_cb: impl FnMut() + 'a) -> Result<Option<PddbKey>> {
self.callback = Some(Box::new(key_changed_cb)); self.callback = Some(Box::new(key_changed_cb));
Ok(None) Ok(None)
} }

View file

@ -378,7 +378,7 @@ mod tests;
use tests::*; use tests::*;
use num_traits::*; 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 xous_ipc::Buffer;
use core::cell::RefCell; use core::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
@ -566,20 +566,20 @@ fn xmain() -> ! {
} }
Some(Opcode::LatestBasis) => { Some(Opcode::LatestBasis) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut mgmt = buffer.to_original::<PddbBasisMgmt, _>().unwrap(); let mut mgmt = buffer.to_original::<PddbBasisRequest, _>().unwrap();
if let Some(name) = basis_cache.basis_latest() { if let Some(name) = basis_cache.basis_latest() {
for (&src, dst) in name.as_bytes().iter().zip(mgmt.name.iter_mut()) { *dst = src } for (&src, dst) in name.as_bytes().iter().zip(mgmt.name.iter_mut()) { *dst = src }
mgmt.code = PddbBasisMgmtCode::NoErr; mgmt.code = PddbRequestCode::NoErr;
} else { } else {
mgmt.code = PddbBasisMgmtCode::NotMounted; mgmt.code = PddbRequestCode::NotMounted;
} }
buffer.replace(mgmt).unwrap(); buffer.replace(mgmt).unwrap();
} }
Some(Opcode::CreateBasis) => { Some(Opcode::CreateBasis) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut mgmt = buffer.to_original::<PddbBasisMgmt, _>().unwrap(); let mut mgmt = buffer.to_original::<PddbBasisRequest, _>().unwrap();
match mgmt.code { match mgmt.code {
PddbBasisMgmtCode::Create => { PddbRequestCode::Create => {
let name = cstr_to_string(&mgmt.name); let name = cstr_to_string(&mgmt.name);
let request = BasisRequestPassword { let request = BasisRequestPassword {
db_name: xous_ipc::String::<{crate::api::BASIS_NAME_LEN}>::from_str(name.as_str()), 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::<BasisRequestPassword, _>().unwrap(); let ret = buf.to_original::<BasisRequestPassword, _>().unwrap();
if let Some(pw) = ret.plaintext_pw { 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")) { match basis_cache.basis_create(&mut pddb_os, &name, pw.as_str().expect("password was not valid utf-8")) {
Ok(_) => mgmt.code = PddbBasisMgmtCode::NoErr, Ok(_) => mgmt.code = PddbRequestCode::NoErr,
_ => mgmt.code = PddbBasisMgmtCode::InternalError, _ => mgmt.code = PddbRequestCode::InternalError,
} }
} else { } else {
mgmt.code = PddbBasisMgmtCode::InternalError; mgmt.code = PddbRequestCode::InternalError;
} }
} }
_ => { _ => {
mgmt.code = PddbBasisMgmtCode::InternalError; mgmt.code = PddbRequestCode::InternalError;
} }
} }
buffer.replace(mgmt).unwrap(); buffer.replace(mgmt).unwrap();
} }
Some(Opcode::OpenBasis) => { Some(Opcode::OpenBasis) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut mgmt = buffer.to_original::<PddbBasisMgmt, _>().unwrap(); let mut mgmt = buffer.to_original::<PddbBasisRequest, _>().unwrap();
match mgmt.code { match mgmt.code {
PddbBasisMgmtCode::Open => { PddbRequestCode::Open => {
let name = cstr_to_string(&mgmt.name); let name = cstr_to_string(&mgmt.name);
let mut finished = false; let mut finished = false;
while !finished { while !finished {
@ -625,7 +625,7 @@ fn xmain() -> ! {
) { ) {
basis_cache.basis_add(basis); basis_cache.basis_add(basis);
finished = true; finished = true;
mgmt.code = PddbBasisMgmtCode::NoErr; mgmt.code = PddbRequestCode::NoErr;
} }
} else { } else {
modals.add_list_item(t!("pddb.yes", xous::LANG)).expect("couldn't build radio item list"); 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 // this will cause just another go-around
} else if response.as_str() == t!("pddb.no", xous::LANG) { } else if response.as_str() == t!("pddb.no", xous::LANG) {
finished = true; 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 { } else {
panic!("Got unexpected return from radiobutton"); panic!("Got unexpected return from radiobutton");
} }
@ -648,50 +648,53 @@ fn xmain() -> ! {
} }
} }
_ => { _ => {
mgmt.code = PddbBasisMgmtCode::InternalError; mgmt.code = PddbRequestCode::InternalError;
} }
} }
buffer.replace(mgmt).unwrap(); buffer.replace(mgmt).unwrap();
} }
Some(Opcode::CloseBasis) => { Some(Opcode::CloseBasis) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut mgmt = buffer.to_original::<PddbBasisMgmt, _>().unwrap(); let mut mgmt = buffer.to_original::<PddbBasisRequest, _>().unwrap();
match mgmt.code { match mgmt.code {
PddbBasisMgmtCode::Close => { PddbRequestCode::Close => {
let name = cstr_to_string(&mgmt.name); let name = cstr_to_string(&mgmt.name);
match basis_cache.basis_unmount(&mut pddb_os, &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() { Err(e) => match e.kind() {
ErrorKind::NotFound => mgmt.code = PddbBasisMgmtCode::NotFound, ErrorKind::NotFound => mgmt.code = PddbRequestCode::NotFound,
_ => mgmt.code = PddbBasisMgmtCode::InternalError, _ => mgmt.code = PddbRequestCode::InternalError,
} }
} }
} }
_ => { _ => {
mgmt.code = PddbBasisMgmtCode::InternalError; mgmt.code = PddbRequestCode::InternalError;
} }
} }
buffer.replace(mgmt).unwrap(); buffer.replace(mgmt).unwrap();
} }
Some(Opcode::DeleteBasis) => { Some(Opcode::DeleteBasis) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut mgmt = buffer.to_original::<PddbBasisMgmt, _>().unwrap(); let mut mgmt = buffer.to_original::<PddbBasisRequest, _>().unwrap();
match mgmt.code { match mgmt.code {
PddbBasisMgmtCode::Delete => { PddbRequestCode::Delete => {
let name = cstr_to_string(&mgmt.name); let name = cstr_to_string(&mgmt.name);
match basis_cache.basis_delete(&mut pddb_os, &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() { Err(e) => match e.kind() {
ErrorKind::NotFound => mgmt.code = PddbBasisMgmtCode::NotFound, ErrorKind::NotFound => mgmt.code = PddbRequestCode::NotFound,
_ => mgmt.code = PddbBasisMgmtCode::InternalError, _ => mgmt.code = PddbRequestCode::InternalError,
} }
} }
} }
_ => { _ => {
mgmt.code = PddbBasisMgmtCode::InternalError; mgmt.code = PddbRequestCode::InternalError;
} }
} }
buffer.replace(mgmt).unwrap(); buffer.replace(mgmt).unwrap();
}
Some(Opcode::CreateDict) => {
} }
Some(Opcode::KeyRequest) => { Some(Opcode::KeyRequest) => {
// placeholder // placeholder