lib.rs 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854
  1. // SPDX-License-Identifier: AGPL-3.0-or-later
  2. pub mod accessor;
  3. mod block_path;
  4. pub mod buf_reader;
  5. pub mod collections;
  6. pub mod config_helpers;
  7. /// Code which enables cryptographic operations.
  8. pub mod crypto;
  9. pub mod drop_trigger;
  10. pub mod error;
  11. pub mod log;
  12. mod readcap_dict;
  13. pub mod sectored_buf;
  14. #[cfg(test)]
  15. mod test_helpers;
  16. mod trailered;
  17. use readcap_dict::ReadcapDict;
  18. #[macro_use]
  19. extern crate static_assertions;
  20. #[macro_use]
  21. #[cfg(test)]
  22. extern crate lazy_static;
  23. use ::log::error;
  24. use btserde::{read_from, write_to};
  25. use fuse_backend_rs::abi::fuse_abi::{stat64, Attr};
  26. use positioned_io::{ReadAt, Size, WriteAt};
  27. use serde::{de::DeserializeOwned, Deserialize, Serialize};
  28. use serde_big_array::BigArray;
  29. use std::{
  30. collections::{btree_map, BTreeMap},
  31. convert::{Infallible, TryFrom},
  32. fmt::{self, Display, Formatter},
  33. hash::Hash as Hashable,
  34. io::{self, Read, Seek, SeekFrom, Write},
  35. net::IpAddr,
  36. ops::{Add, Sub},
  37. os::unix::prelude::MetadataExt,
  38. time::{Duration, SystemTime},
  39. };
  40. use strum_macros::{Display, EnumDiscriminants, FromRepr};
  41. use accessor::Accessor;
  42. pub use block_path::{BlockPath, BlockPathError};
  43. use crypto::{
  44. AsymKeyPub, Ciphertext, ConcretePub, Creds, CredsPub, Decrypter, DecrypterExt, EncrypterExt,
  45. HashKind, MerkleStream, SecretStream, Sign, Signature, Signer, SymKey, SymKeyKind, VarHash,
  46. };
  47. use error::{BoxInIoErr, BtErr};
  48. pub use error::{Error, Result};
  49. use trailered::Trailered;
  50. #[derive(Debug)]
  51. pub enum BlockError {
  52. MissingWritecap,
  53. IncorrectSize { expected: usize, actual: usize },
  54. NoBlockKey,
  55. NoBlockPath,
  56. UnknownSize,
  57. ProcRecNotIssued,
  58. ProcRecRevoked,
  59. NoInheritedKey,
  60. }
  61. impl Display for BlockError {
  62. fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
  63. match self {
  64. BlockError::MissingWritecap => write!(f, "missing writecap"),
  65. BlockError::IncorrectSize { expected, actual } => {
  66. write!(f, "incorrect size {actual}, expected {expected}")
  67. }
  68. BlockError::NoBlockKey => write!(f, "no block key is present"),
  69. BlockError::NoBlockPath => write!(f, "no block path was specified"),
  70. BlockError::UnknownSize => write!(f, "size could not be determined"),
  71. BlockError::ProcRecNotIssued => {
  72. write!(f, "a process record was requested by not yet issued")
  73. }
  74. BlockError::ProcRecRevoked => write!(f, "this process record has been revoked"),
  75. BlockError::NoInheritedKey => write!(f, "block metadata has no inherited key"),
  76. }
  77. }
  78. }
  79. impl std::error::Error for BlockError {}
  80. // This assertion ensures that conversions from `usize` to `u64` will not cause truncation. This
  81. // prevents this code from compiling for 128 bit platforms, but that's not really a concern for the
  82. // foreseeable future.
  83. // If this assumption is ever to be removed, you'll need to evaluate every occurrence of `as u64`.
  84. const_assert!(::std::mem::size_of::<usize>() <= ::std::mem::size_of::<u64>());
  85. /// The expected size of the IO blocks for the file system. This is used to calculate
  86. /// [SECTOR_SZ_DEFAULT].
  87. pub const EXPECTED_IO_BLOCK: usize = 4096;
  88. /// The number of IO blocks in each sector.
  89. pub const IO_BLOCKS_PER_SECTOR: usize = 256;
  90. /// The default sector size to use for new blocks.
  91. pub const SECTOR_SZ_DEFAULT: usize = EXPECTED_IO_BLOCK * IO_BLOCKS_PER_SECTOR;
  92. /// `SECTOR_SZ_DEFAULT` converted to a `u64`.
  93. pub const SECTOR_U64_DEFAULT: u64 = SECTOR_SZ_DEFAULT as u64;
  94. pub trait MetaReader: AsRef<BlockMeta> + Size {
  95. fn meta(&self) -> &BlockMeta {
  96. self.as_ref()
  97. }
  98. fn meta_body(&self) -> &BlockMetaBody {
  99. self.meta().body()
  100. }
  101. }
  102. impl<T: AsRef<BlockMeta> + Size + ?Sized> MetaReader for T {}
  103. /// Trait for accessing the metadata associated with a block.
  104. pub trait MetaAccess: AsMut<BlockMeta> + MetaReader {
  105. fn mut_meta(&mut self) -> &mut BlockMeta {
  106. self.as_mut()
  107. }
  108. fn mut_meta_body(&mut self) -> &mut BlockMetaBody {
  109. self.mut_meta().mut_body()
  110. }
  111. }
  112. impl<T: AsMut<BlockMeta> + MetaReader + ?Sized> MetaAccess for T {}
  113. pub trait FlushMeta {
  114. /// Flushes metadata to persistent storage.
  115. fn flush_meta(&mut self) -> Result<()>;
  116. }
  117. impl<T: FlushMeta + ?Sized> FlushMeta for &mut T {
  118. fn flush_meta(&mut self) -> Result<()> {
  119. (*self).flush_meta()
  120. }
  121. }
  122. /// ### THE BLOCK TRAIT
  123. ///
  124. /// Trait for types which provide read and write access to blocks.
  125. pub trait Block: ReadAt + WriteAt + MetaAccess + Sectored + FlushMeta {}
  126. impl<T: ReadAt + WriteAt + MetaAccess + Sectored + FlushMeta + ?Sized> Block for T {}
  127. /// Deserializes an instance of the given type from the given block.
  128. fn read_from_block<T: DeserializeOwned, B: BlockReader + ?Sized>(block: &mut B) -> Result<T> {
  129. block.rewind()?;
  130. let mut block = block;
  131. let dir: T = read_from(&mut block)?;
  132. Ok(dir)
  133. }
  134. pub trait BlockReader: Read + Seek + AsRef<BlockMeta> + Size + Sectored {
  135. fn read_dir(&mut self) -> Result<Directory> {
  136. read_from_block::<Directory, _>(self)
  137. }
  138. fn read_proc_rec(&mut self) -> Result<ProcRec> {
  139. read_from_block::<ProcRec, _>(self)
  140. }
  141. }
  142. impl<T: Read + Seek + AsRef<BlockMeta> + Size + Sectored + ?Sized> BlockReader for T {}
  143. fn write_to_block<T: Serialize, B: BlockAccessor + ?Sized>(block: &mut B, value: &T) -> Result<()> {
  144. block.rewind()?;
  145. let mut sich = block;
  146. write_to(value, &mut sich)?;
  147. sich.flush()?;
  148. Ok(())
  149. }
  150. pub trait BlockAccessor: BlockReader + Write + MetaAccess {
  151. fn write_dir(&mut self, dir: &Directory) -> Result<()> {
  152. write_to_block(self, dir)
  153. }
  154. fn write_proc_rec(&mut self, proc_rec: &ProcRec) -> Result<()> {
  155. write_to_block(self, proc_rec)
  156. }
  157. }
  158. impl<T: Read + Write + Seek + MetaAccess + Sectored + ?Sized> BlockAccessor for T {}
  159. // A trait for streams which only allow reads and writes in fixed sized units called sectors.
  160. pub trait Sectored {
  161. /// Returns the size of the sector for this stream.
  162. fn sector_sz(&self) -> usize;
  163. /// Returns the sector size as a `u64`.
  164. fn sector_sz64(&self) -> u64 {
  165. // This is guaranteed not to truncate thanks to the `const_assert!` above
  166. // `SECTOR_SZ_DEFAULT`.
  167. self.sector_sz() as u64
  168. }
  169. /// Returns `Err(Error::IncorrectSize)` if the given size is not equal to the sector size.
  170. fn assert_sector_sz(&self, actual: usize) -> Result<()> {
  171. let expected = self.sector_sz();
  172. if expected != actual {
  173. Err(bterr!(BlockError::IncorrectSize { expected, actual }))
  174. } else {
  175. Ok(())
  176. }
  177. }
  178. /// Returns `Err(Error::IncorrectSize)` if the given size is less than the sector size.
  179. fn assert_at_least_sector_sz(&self, actual: usize) -> Result<()> {
  180. let expected = self.sector_sz();
  181. if actual < expected {
  182. Err(bterr!(BlockError::IncorrectSize { expected, actual }))
  183. } else {
  184. Ok(())
  185. }
  186. }
  187. /// Returns the offset (in bytes) from the beginning of this stream that the given 0-based
  188. /// sector index corresponds to.
  189. fn offset_at(&self, index: u64) -> u64 {
  190. index * self.sector_sz64()
  191. }
  192. }
  193. impl<T: Sectored + ?Sized> Sectored for &T {
  194. fn sector_sz(&self) -> usize {
  195. (**self).sector_sz()
  196. }
  197. }
  198. impl<T: Sectored + ?Sized> Sectored for &mut T {
  199. fn sector_sz(&self) -> usize {
  200. (**self).sector_sz()
  201. }
  202. }
  203. impl Sectored for ::std::fs::File {
  204. fn sector_sz(&self) -> usize {
  205. self.metadata()
  206. .map(|e| {
  207. let blksize: usize = e.blksize().try_into().bterr().unwrap();
  208. blksize * IO_BLOCKS_PER_SECTOR
  209. })
  210. .unwrap_or(SECTOR_SZ_DEFAULT)
  211. }
  212. }
  213. impl<T: Sectored + Size> Sectored for Cursor<T> {
  214. fn sector_sz(&self) -> usize {
  215. self.cursor.get_ref().sector_sz()
  216. }
  217. }
  218. /// The `Read` trait requires the caller to supply the buffer to be read into. This trait is its
  219. /// dual in the sense that the trait implementor is expected to supply its own buffer, which a
  220. /// `Write` instance is given. This can be used to avoid copying in cases where the trait
  221. /// implementor is already buffering data, so it can give a reference to this buffer to the caller
  222. /// (for example in `SectoredBuf`)
  223. pub trait ReadDual: Read {
  224. fn read_into<W: Write>(&mut self, write: W, count: usize) -> io::Result<usize>;
  225. }
  226. pub trait WriteDual: Write {
  227. fn write_from<R: Read>(&mut self, read: R, count: usize) -> io::Result<usize>;
  228. }
  229. /// Trait for types which can be extended with zero byes.
  230. pub trait ZeroExtendable {
  231. /// Extends this stream with the given number of zero bytes. The position of the stream must
  232. /// be unchanged when this method returns successfully. The state of the stream in the case of
  233. /// an error is undefined.
  234. fn zero_extend(&mut self, num_zeros: u64) -> io::Result<()>;
  235. }
  236. /// Trait for streams which can efficiently and infallibly return their current position.
  237. pub trait Positioned {
  238. /// Returns the position of this stream (byte offset relative to the beginning).
  239. fn pos(&self) -> usize;
  240. }
  241. impl<T: Positioned> Positioned for &T {
  242. fn pos(&self) -> usize {
  243. (**self).pos()
  244. }
  245. }
  246. impl<T: Positioned> Positioned for &mut T {
  247. fn pos(&self) -> usize {
  248. (**self).pos()
  249. }
  250. }
  251. pub trait TrySeek {
  252. /// Attempts to seek to the given offset from the start of the stream.
  253. fn try_seek(&mut self, seek_from: SeekFrom) -> io::Result<()>;
  254. }
  255. pub trait SizeExt: Size {
  256. fn size_or_err(&self) -> Result<u64> {
  257. self.size()?.ok_or_else(|| bterr!(BlockError::UnknownSize))
  258. }
  259. }
  260. impl<T: Size> SizeExt for T {}
  261. /// A version of the `WriteAt` trait, which allows integrity information to be supplied when
  262. /// flushing.
  263. pub trait WriteInteg: WriteAt {
  264. fn flush_integ(&mut self, integrity: &[u8]) -> io::Result<()>;
  265. }
  266. pub trait Decompose<T> {
  267. fn into_inner(self) -> T;
  268. }
  269. pub trait Split<L, R> {
  270. fn split(self) -> (L, R);
  271. fn combine(left: L, right: R) -> Self;
  272. }
  273. pub trait TryCompose<T, U: Decompose<T>> {
  274. type Error;
  275. fn try_compose(self, inner: T) -> std::result::Result<U, Self::Error>;
  276. }
  277. trait Compose<T, U> {
  278. fn compose(self, inner: T) -> U;
  279. }
  280. impl<T, U: Decompose<T>, S: TryCompose<T, U, Error = Infallible>> Compose<T, U> for S {
  281. fn compose(self, inner: T) -> U {
  282. let result = self.try_compose(inner);
  283. // Safety: Infallible has no values, so `result` must be `Ok`.
  284. unsafe { result.unwrap_unchecked() }
  285. }
  286. }
  287. impl AsRef<BlockMeta> for &BlockMeta {
  288. fn as_ref(&self) -> &BlockMeta {
  289. self
  290. }
  291. }
  292. impl AsRef<BlockMeta> for &mut BlockMeta {
  293. fn as_ref(&self) -> &BlockMeta {
  294. self
  295. }
  296. }
  297. impl AsMut<BlockMeta> for &mut BlockMeta {
  298. fn as_mut(&mut self) -> &mut BlockMeta {
  299. self
  300. }
  301. }
  302. #[derive(Debug)]
  303. pub struct Cursor<T: Size> {
  304. cursor: positioned_io::SizeCursor<T>,
  305. }
  306. impl<T: Size> Cursor<T> {
  307. pub fn new(inner: T) -> Cursor<T> {
  308. Self {
  309. cursor: positioned_io::SizeCursor::new(inner),
  310. }
  311. }
  312. pub fn new_pos(inner: T, pos: u64) -> Cursor<T> {
  313. Self {
  314. cursor: positioned_io::SizeCursor::new_pos(inner, pos),
  315. }
  316. }
  317. pub fn get_ref(&self) -> &T {
  318. self.cursor.get_ref()
  319. }
  320. pub fn get_mut(&mut self) -> &mut T {
  321. self.cursor.get_mut()
  322. }
  323. }
  324. impl<T: ReadAt + Size> Read for Cursor<T> {
  325. fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
  326. self.cursor.read(buf)
  327. }
  328. }
  329. impl<T: WriteAt + Size> Write for Cursor<T> {
  330. fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
  331. self.cursor.write(buf)
  332. }
  333. fn flush(&mut self) -> io::Result<()> {
  334. self.cursor.flush()
  335. }
  336. }
  337. impl<T: Size> Seek for Cursor<T> {
  338. fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
  339. self.cursor.seek(pos)
  340. }
  341. }
  342. impl<T: Size> Decompose<T> for Cursor<T> {
  343. fn into_inner(self) -> T {
  344. self.cursor.into_inner()
  345. }
  346. }
  347. impl<U, T: AsRef<U> + Size> AsRef<U> for Cursor<T> {
  348. fn as_ref(&self) -> &U {
  349. self.cursor.get_ref().as_ref()
  350. }
  351. }
  352. impl<U, T: AsMut<U> + Size> AsMut<U> for Cursor<T> {
  353. fn as_mut(&mut self) -> &mut U {
  354. self.cursor.get_mut().as_mut()
  355. }
  356. }
  357. impl<T: Size> Size for Cursor<T> {
  358. fn size(&self) -> io::Result<Option<u64>> {
  359. self.cursor.get_ref().size()
  360. }
  361. }
  362. pub const EMPTY_SLICE: &[u8] = &[0u8; 0];
  363. impl<T: Size> Split<Cursor<&'static [u8]>, T> for Cursor<T> {
  364. fn split(self) -> (Cursor<&'static [u8]>, T) {
  365. let pos = self.cursor.position();
  366. (Cursor::new_pos(EMPTY_SLICE, pos), self.cursor.into_inner())
  367. }
  368. fn combine(left: Cursor<&'static [u8]>, right: T) -> Self {
  369. let pos = left.cursor.position();
  370. Self::new_pos(right, pos)
  371. }
  372. }
  373. /// Extensions to the `Read` trait.
  374. trait ReadExt: Read {
  375. /// Reads repeatedly until one of the following occur:
  376. /// 1. The given buffer is full.
  377. /// 2. A call to `read` returns 0.
  378. /// 3. A call to `read` returns an error.
  379. /// The number of bytes read is returned. If an error is returned, then no bytes were read.
  380. fn fill_buf(&mut self, mut dest: &mut [u8]) -> io::Result<usize> {
  381. let dest_len_start = dest.len();
  382. while !dest.is_empty() {
  383. let byte_ct = match self.read(dest) {
  384. Ok(byte_ct) => byte_ct,
  385. Err(err) => {
  386. if dest_len_start == dest.len() {
  387. return Err(err);
  388. } else {
  389. // We're not allowed to return an error since we've already read from self.
  390. error!("an error occurred in fill_buf: {}", err);
  391. break;
  392. }
  393. }
  394. };
  395. if 0 == byte_ct {
  396. break;
  397. }
  398. dest = &mut dest[byte_ct..];
  399. }
  400. Ok(dest_len_start - dest.len())
  401. }
  402. }
  403. impl<T: Read> ReadExt for T {}
  404. pub trait SeekFromExt {
  405. /// Returns the absolute position (offset from the start) this `SeekFrom` refers to.
  406. /// `curr` is called in the case this `SeekFrom` is `Current`, and is expected to return the
  407. /// current position.
  408. /// `end` is called in the case this `SeekFrom` is `End`, and is expected to return the
  409. /// the position of the end.
  410. fn abs<F, G>(&self, curr: F, end: G) -> Result<u64>
  411. where
  412. F: FnOnce() -> Result<u64>,
  413. G: FnOnce() -> Result<u64>;
  414. /// Like [SeekFromExt::abs] except that an error is always returned when [SeekFrom::End] is
  415. /// given.
  416. fn abs_no_end<F>(&self, curr: F) -> Result<u64>
  417. where
  418. F: FnOnce() -> Result<u64>,
  419. {
  420. self.abs(curr, || Err(bterr!("SeekFrom::End is not supported")))
  421. }
  422. /// Converts a C-style `(whence, offset)` pair into a [SeekFrom] enum value.
  423. /// See the POSIX man page of `lseek` for more details.
  424. fn whence_offset(whence: u32, offset: u64) -> io::Result<SeekFrom> {
  425. let whence = whence as i32;
  426. match whence {
  427. libc::SEEK_SET => Ok(SeekFrom::Start(offset)),
  428. libc::SEEK_CUR => Ok(SeekFrom::Current(offset as i64)),
  429. libc::SEEK_END => Ok(SeekFrom::End(offset as i64)),
  430. _ => Err(io::Error::new(
  431. io::ErrorKind::InvalidInput,
  432. "`whence` was not one of `libc::{SEEK_SET, SEEK_CUR, SEEK_END}`",
  433. )),
  434. }
  435. }
  436. }
  437. impl SeekFromExt for SeekFrom {
  438. fn abs<F, G>(&self, curr: F, end: G) -> Result<u64>
  439. where
  440. F: FnOnce() -> Result<u64>,
  441. G: FnOnce() -> Result<u64>,
  442. {
  443. match self {
  444. SeekFrom::Start(start) => Ok(*start),
  445. SeekFrom::Current(from_curr) => {
  446. let curr = curr()?;
  447. Ok(curr.wrapping_add_signed(*from_curr))
  448. }
  449. SeekFrom::End(from_end) => {
  450. let end = end()?;
  451. Ok(end.wrapping_add_signed(*from_end))
  452. }
  453. }
  454. }
  455. }
  456. /// The type used to identify blocks.
  457. pub type Inode = u64;
  458. /// A unique identifier for a block.
  459. #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Hash)]
  460. pub struct BlockId {
  461. pub generation: u64,
  462. pub inode: Inode,
  463. }
  464. impl BlockId {
  465. pub fn new(generation: u64, inode: Inode) -> BlockId {
  466. BlockId { generation, inode }
  467. }
  468. }
  469. impl Default for BlockId {
  470. fn default() -> Self {
  471. BlockId::new(0, 0)
  472. }
  473. }
  474. /// Metadata that is encrypted.
  475. #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Hash)]
  476. pub struct BlockMetaSecrets {
  477. /// The identifier for the block these secrets are for.
  478. pub block_id: BlockId,
  479. /// Mode of file.
  480. pub mode: u32,
  481. /// Owner UID of file.
  482. pub uid: u32,
  483. /// Owner GID of file.
  484. pub gid: u32,
  485. /// Last access time.
  486. pub atime: Epoch,
  487. /// Last data modification.
  488. pub mtime: Epoch,
  489. /// Last status change.
  490. pub ctime: Epoch,
  491. /// Size of the file in bytes.
  492. pub size: u64,
  493. /// Number of hard links to the file.
  494. pub nlink: u32,
  495. /// The sector size used by the block.
  496. pub sect_sz: u64,
  497. /// User controlled metadata.
  498. pub tags: BTreeMap<String, Vec<u8>>,
  499. }
  500. impl BlockMetaSecrets {
  501. pub fn new() -> BlockMetaSecrets {
  502. Self {
  503. block_id: BlockId::default(),
  504. mode: 0,
  505. uid: 0,
  506. gid: 0,
  507. atime: Epoch::default(),
  508. mtime: Epoch::default(),
  509. ctime: Epoch::default(),
  510. size: 0,
  511. nlink: 0,
  512. sect_sz: SECTOR_U64_DEFAULT,
  513. tags: BTreeMap::new(),
  514. }
  515. }
  516. pub fn attr(&self) -> Result<Attr> {
  517. Ok(Attr {
  518. ino: self.block_id.inode,
  519. size: self.size,
  520. atime: self.atime.value(),
  521. mtime: self.mtime.value(),
  522. ctime: self.ctime.value(),
  523. atimensec: 0,
  524. mtimensec: 0,
  525. ctimensec: 0,
  526. mode: self.mode,
  527. nlink: self.nlink,
  528. uid: self.uid,
  529. gid: self.gid,
  530. rdev: 0,
  531. blksize: self
  532. .sect_sz
  533. .try_into()
  534. .map_err(|_| bterr!("BlockMetaSecrets::sect_sz could not be converted to a u32"))?,
  535. blocks: self.sectors(),
  536. flags: 0,
  537. })
  538. }
  539. pub fn stat(&self) -> Result<stat64> {
  540. self.attr().map(|e| e.into())
  541. }
  542. /// Returns the number of sectors occupied by the block's data.
  543. pub fn sectors(&self) -> u64 {
  544. if self.size % self.sect_sz == 0 {
  545. self.size / self.sect_sz
  546. } else {
  547. self.size / self.sect_sz + 1
  548. }
  549. }
  550. pub fn block_id(&self) -> &BlockId {
  551. &self.block_id
  552. }
  553. pub fn sector_sz(&self) -> u64 {
  554. self.sect_sz
  555. }
  556. }
  557. impl Default for BlockMetaSecrets {
  558. fn default() -> Self {
  559. Self::new()
  560. }
  561. }
  562. impl AsRef<BlockId> for BlockMetaSecrets {
  563. fn as_ref(&self) -> &BlockId {
  564. self.block_id()
  565. }
  566. }
  567. impl TryFrom<&BlockMetaSecrets> for Attr {
  568. type Error = crate::Error;
  569. fn try_from(value: &BlockMetaSecrets) -> Result<Self> {
  570. value.attr()
  571. }
  572. }
  573. /// This struct contains all of the metadata fields associated with a block, except for its
  574. /// signature. Since this struct implements `Serialize`, this allows for convenient signature
  575. /// calculations.
  576. #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
  577. pub struct BlockMetaBody {
  578. /// A copy of the block key encrypted using this block's parent's key. If this is None, then
  579. /// this block is not encrypted.
  580. inherit: Option<Ciphertext<SymKey>>,
  581. readcaps: ReadcapDict,
  582. writecap: Option<Writecap>,
  583. /// A hash which provides integrity for the contents of the block body.
  584. integrity: Option<VarHash>,
  585. /// The public key that corresponds to the private key used to sign these metadata.
  586. signing_key: AsymKeyPub<Sign>,
  587. /// Additional metadata which is subject to confidentiality protection.
  588. secrets: Ciphertext<BlockMetaSecrets>,
  589. #[serde(skip)]
  590. /// The path in the blocktree where this block is located. This is initialized in
  591. /// BlockStream::new.
  592. path: BlockPath,
  593. #[serde(skip)]
  594. /// The cleartext block key.
  595. block_key: Option<SymKey>,
  596. #[serde(skip)]
  597. /// The clear text secret metadata.
  598. secrets_struct: Option<BlockMetaSecrets>,
  599. }
  600. impl BlockMetaBody {
  601. fn new<C: Creds>(creds: C) -> Result<BlockMetaBody> {
  602. let block_key = SymKey::generate(SymKeyKind::default())?;
  603. let secrets = BlockMetaSecrets::default();
  604. let mut body = BlockMetaBody {
  605. path: BlockPath::default(),
  606. inherit: None,
  607. readcaps: ReadcapDict::new()?,
  608. writecap: creds.writecap().map(|e| e.to_owned()),
  609. integrity: None,
  610. signing_key: creds.public_sign().to_owned(),
  611. secrets: block_key.ser_encrypt(&secrets)?,
  612. block_key: Some(block_key),
  613. secrets_struct: Some(secrets),
  614. };
  615. body.add_readcap_for(creds)?;
  616. Ok(body)
  617. }
  618. /// Uses the given symmetric key to decrypt the `inherit` field. If this field is `None`, or if
  619. /// the decryption fails, then an error is returned. If the block key has already been decrypted
  620. /// then this method does nothing.
  621. pub fn unlock_block_key_with_parent_key(&mut self, parent_key: SymKey) -> Result<()> {
  622. if self.block_key.is_some() {
  623. return Ok(());
  624. }
  625. if let Some(ref inherit) = self.inherit {
  626. self.block_key = Some(parent_key.ser_decrypt(inherit)?);
  627. Ok(())
  628. } else {
  629. Err(BlockError::NoInheritedKey.into())
  630. }
  631. }
  632. pub fn access_secrets<T, F: FnOnce(&mut BlockMetaSecrets) -> Result<T>>(
  633. &mut self,
  634. accessor: F,
  635. ) -> Result<T> {
  636. self.decrypt_secrets()?;
  637. let secrets = self.secrets_struct.as_mut().unwrap();
  638. let output = accessor(secrets)?;
  639. self.secrets = self
  640. .block_key
  641. .as_ref()
  642. .ok_or(BlockError::NoBlockKey)?
  643. .ser_encrypt(secrets)?;
  644. Ok(output)
  645. }
  646. pub fn decrypt_secrets(&mut self) -> Result<()> {
  647. if self.secrets_struct.is_none() {
  648. let block_key = self.block_key()?;
  649. self.secrets_struct = Some(block_key.ser_decrypt(&self.secrets)?);
  650. }
  651. Ok(())
  652. }
  653. pub fn secrets(&self) -> Result<&BlockMetaSecrets> {
  654. self.secrets_struct
  655. .as_ref()
  656. .ok_or_else(|| bterr!("secrets have not been decrypted"))
  657. }
  658. pub fn block_key(&self) -> Result<&SymKey> {
  659. self.block_key
  660. .as_ref()
  661. .ok_or_else(|| bterr!(BlockError::NoBlockKey))
  662. }
  663. pub fn mut_block_key(&mut self) -> &mut Option<SymKey> {
  664. &mut self.block_key
  665. }
  666. pub fn block_id(&self) -> Result<&BlockId> {
  667. let secrets = self.secrets()?;
  668. Ok(secrets.block_id())
  669. }
  670. pub fn integrity(&self) -> Option<&[u8]> {
  671. self.integrity.as_ref().map(|hash| hash.as_slice())
  672. }
  673. pub fn use_readcap_for<C: Creds>(&mut self, creds: &C) -> Result<&SymKey> {
  674. let block_key = self.readcaps.get(creds)?;
  675. self.block_key = Some(block_key);
  676. self.block_key()
  677. }
  678. pub fn add_readcap_for<C: CredsPub>(&mut self, creds: C) -> Result<()> {
  679. let block_key = self
  680. .block_key
  681. .as_ref()
  682. .ok_or_else(|| bterr!(BlockError::NoBlockKey))?;
  683. self.readcaps.set(creds, block_key)
  684. }
  685. pub fn path(&self) -> &BlockPath {
  686. &self.path
  687. }
  688. pub fn set_path(&mut self, path: BlockPath) {
  689. self.path = path
  690. }
  691. }
  692. /// Signed metadata associated with a block.
  693. #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
  694. pub struct BlockMeta {
  695. body: BlockMetaBody,
  696. sig: Signature,
  697. }
  698. impl BlockMeta {
  699. pub fn new<C: Creds>(creds: &C) -> Result<BlockMeta> {
  700. let body = BlockMetaBody::new(creds)?;
  701. let sig = Signature::empty(body.signing_key.scheme());
  702. Ok(BlockMeta { body, sig })
  703. }
  704. pub fn body(&self) -> &BlockMetaBody {
  705. self.as_ref()
  706. }
  707. pub fn mut_body(&mut self) -> &mut BlockMetaBody {
  708. self.as_mut()
  709. }
  710. }
  711. impl AsRef<BlockMetaBody> for BlockMeta {
  712. fn as_ref(&self) -> &BlockMetaBody {
  713. &self.body
  714. }
  715. }
  716. impl AsMut<BlockMetaBody> for BlockMeta {
  717. fn as_mut(&mut self) -> &mut BlockMetaBody {
  718. &mut self.body
  719. }
  720. }
  721. pub struct BlockStream<T, C> {
  722. trailered: Trailered<T, BlockMeta>,
  723. meta: BlockMeta,
  724. meta_body_buf: Vec<u8>,
  725. creds: C,
  726. sect_sz: usize,
  727. }
  728. impl<T: ReadAt + Sectored + Size, C: Creds> BlockStream<T, C> {
  729. fn new(
  730. inner: T,
  731. creds: C,
  732. parent_key: Option<SymKey>,
  733. block_path: BlockPath,
  734. ) -> Result<BlockStream<T, C>> {
  735. let (trailered, meta) = Trailered::<_, BlockMeta>::new(inner)?;
  736. let meta = match meta {
  737. Some(mut meta) => {
  738. meta.assert_valid(&block_path)?;
  739. meta.body.path = block_path;
  740. if let Some(parent_key) = parent_key {
  741. meta.body.unlock_block_key_with_parent_key(parent_key)?;
  742. } else {
  743. meta.body.use_readcap_for(&creds)?;
  744. }
  745. // We need to use the writecap and signing_key provided by the current credentials.
  746. meta.body.writecap = Some(
  747. creds
  748. .writecap()
  749. .ok_or(BlockError::MissingWritecap)?
  750. .to_owned(),
  751. );
  752. meta.body.signing_key = creds.public_sign().to_owned();
  753. meta.body.decrypt_secrets()?;
  754. meta
  755. }
  756. None => {
  757. let mut meta = BlockMeta::new(&creds)?;
  758. meta.body.path = block_path;
  759. if let Some(parent_key) = parent_key {
  760. meta.body.inherit = Some(parent_key.ser_encrypt(meta.body.block_key()?)?);
  761. } else {
  762. meta.body.add_readcap_for(&creds)?;
  763. }
  764. meta.body.writecap = Some(
  765. creds
  766. .writecap()
  767. .ok_or(BlockError::MissingWritecap)?
  768. .to_owned(),
  769. );
  770. meta.body.access_secrets(|secrets| {
  771. secrets.sect_sz = trailered.sector_sz64();
  772. Ok(())
  773. })?;
  774. meta
  775. }
  776. };
  777. let sect_sz = meta.body.secrets()?.sector_sz().try_into().bterr()?;
  778. Ok(BlockStream {
  779. trailered,
  780. meta_body_buf: Vec::new(),
  781. creds,
  782. sect_sz,
  783. meta,
  784. })
  785. }
  786. }
  787. impl<T: WriteAt + Size, C: Signer> BlockStream<T, C> {
  788. fn sign_flush_meta(&mut self) -> io::Result<()> {
  789. self.meta_body_buf.clear();
  790. self.meta.sig = self
  791. .creds
  792. .ser_sign_into(&self.meta.body, &mut self.meta_body_buf)?;
  793. self.trailered.flush(&self.meta)
  794. }
  795. }
  796. impl<T: WriteAt + Size, C: Signer + Principaled + Decrypter> WriteAt for BlockStream<T, C> {
  797. fn write_at(&mut self, pos: u64, buf: &[u8]) -> io::Result<usize> {
  798. self.trailered.write_at(pos, buf)
  799. }
  800. fn flush(&mut self) -> io::Result<()> {
  801. self.sign_flush_meta()
  802. }
  803. }
  804. impl<T: WriteAt + Size, C: Signer + Principaled + Decrypter> WriteInteg for BlockStream<T, C> {
  805. fn flush_integ(&mut self, integrity: &[u8]) -> io::Result<()> {
  806. let meta_body = &mut self.meta.body;
  807. let integ = meta_body.integrity.get_or_insert_with(VarHash::default);
  808. integ.as_mut().copy_from_slice(integrity);
  809. self.sign_flush_meta()
  810. }
  811. }
  812. impl<T: ReadAt + Size, C> ReadAt for BlockStream<T, C> {
  813. fn read_at(&self, pos: u64, buf: &mut [u8]) -> io::Result<usize> {
  814. self.trailered.read_at(pos, buf)
  815. }
  816. }
  817. impl<T, C> AsRef<BlockMeta> for BlockStream<T, C> {
  818. fn as_ref(&self) -> &BlockMeta {
  819. &self.meta
  820. }
  821. }
  822. impl<T, C> AsMut<BlockMeta> for BlockStream<T, C> {
  823. fn as_mut(&mut self) -> &mut BlockMeta {
  824. &mut self.meta
  825. }
  826. }
  827. impl<T, C> Sectored for BlockStream<T, C> {
  828. fn sector_sz(&self) -> usize {
  829. self.sect_sz
  830. }
  831. }
  832. impl<T: Size, C> Size for BlockStream<T, C> {
  833. fn size(&self) -> io::Result<Option<u64>> {
  834. self.trailered.size()
  835. }
  836. }
  837. impl<T: WriteAt + Size, C: Signer> FlushMeta for BlockStream<T, C> {
  838. fn flush_meta(&mut self) -> Result<()> {
  839. self.sign_flush_meta().map_err(|err| err.into())
  840. }
  841. }
  842. impl<T, C> Decompose<(T, C)> for BlockStream<T, C> {
  843. fn into_inner(self) -> (T, C) {
  844. (self.trailered.into_inner(), self.creds)
  845. }
  846. }
  847. pub struct BlockOpenOptions<T, C> {
  848. inner: T,
  849. creds: C,
  850. encrypt: bool,
  851. block_path: Option<BlockPath>,
  852. parent_key: Option<SymKey>,
  853. }
  854. impl BlockOpenOptions<(), ()> {
  855. pub fn new() -> BlockOpenOptions<(), ()> {
  856. BlockOpenOptions {
  857. inner: (),
  858. creds: (),
  859. encrypt: true,
  860. block_path: Default::default(),
  861. parent_key: None,
  862. }
  863. }
  864. }
  865. impl<T, C> BlockOpenOptions<T, C> {
  866. pub fn with_inner<U>(self, inner: U) -> BlockOpenOptions<U, C> {
  867. BlockOpenOptions {
  868. inner,
  869. creds: self.creds,
  870. encrypt: self.encrypt,
  871. block_path: self.block_path,
  872. parent_key: self.parent_key,
  873. }
  874. }
  875. pub fn with_creds<D>(self, creds: D) -> BlockOpenOptions<T, D> {
  876. BlockOpenOptions {
  877. inner: self.inner,
  878. creds,
  879. encrypt: self.encrypt,
  880. block_path: self.block_path,
  881. parent_key: self.parent_key,
  882. }
  883. }
  884. pub fn with_encrypt(mut self, encrypt: bool) -> Self {
  885. self.encrypt = encrypt;
  886. self
  887. }
  888. pub fn with_block_path(mut self, block_path: BlockPath) -> Self {
  889. self.block_path = Some(block_path);
  890. self
  891. }
  892. pub fn with_parent_key(mut self, parent_key: Option<SymKey>) -> Self {
  893. self.parent_key = parent_key;
  894. self
  895. }
  896. }
  897. pub type ConcreteBlock<T, C> = MerkleStream<BlockStream<T, C>>;
  898. pub type FileBlock<C> = ConcreteBlock<std::fs::File, C>;
  899. impl<T: ReadAt + WriteAt + Size + Sectored + 'static, C: Creds + 'static> BlockOpenOptions<T, C> {
  900. pub fn open_bare(self) -> Result<ConcreteBlock<T, C>> {
  901. let block_path = self.block_path.ok_or(BlockError::NoBlockPath)?;
  902. let stream = BlockStream::new(self.inner, self.creds, self.parent_key, block_path)?;
  903. let mut stream = MerkleStream::new(stream)?;
  904. stream.assert_root_integrity()?;
  905. Ok(stream)
  906. }
  907. pub fn open(self) -> Result<Accessor<ConcreteBlock<T, C>>> {
  908. let stream = self.open_bare()?;
  909. let stream = Accessor::new(stream)?;
  910. Ok(stream)
  911. }
  912. }
  913. impl Default for BlockOpenOptions<(), ()> {
  914. fn default() -> Self {
  915. Self::new()
  916. }
  917. }
  918. /// An envelopment of a key, which is tagged with the principal who the key is meant for.
  919. #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
  920. pub struct Readcap {
  921. /// The principal this `Readcap` was issued to.
  922. issued_to: Principal,
  923. /// An encipherment of a block key using the public key of the principal.
  924. key: Ciphertext<SymKey>,
  925. }
  926. impl Readcap {
  927. pub fn new(issued_to: VarHash, key: Ciphertext<SymKey>) -> Readcap {
  928. Readcap {
  929. issued_to: Principal(issued_to),
  930. key,
  931. }
  932. }
  933. }
  934. #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
  935. pub struct WritecapBody {
  936. /// The principal this `Writecap` was issued to.
  937. issued_to: Principal,
  938. /// The path where this write caps's validity begins.
  939. path: BlockPath,
  940. /// The point in time after which this write cap is no longer valid.
  941. expires: Epoch,
  942. /// The public key used to sign this write cap.
  943. signing_key: AsymKeyPub<Sign>,
  944. }
  945. /// Verifies that a principal is authorized to write blocks in a tree.
  946. #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
  947. pub struct Writecap {
  948. /// A container for the fields of this writecap which are covered by the signature.
  949. body: WritecapBody,
  950. /// A digital signature which covers all of the fields in the write cap except for next.
  951. signature: Signature,
  952. /// The next write cap in the chain leading back to the root.
  953. next: Option<Box<Writecap>>,
  954. }
  955. impl Writecap {
  956. /// Returns the root key that was used to sign this writecap.
  957. pub fn root_signing_key(&self) -> &AsymKeyPub<Sign> {
  958. let mut writecap = self;
  959. while writecap.next.is_some() {
  960. writecap = writecap.next.as_ref().unwrap();
  961. }
  962. &writecap.body.signing_key
  963. }
  964. pub fn issued_to(&self) -> &Principal {
  965. &self.body.issued_to
  966. }
  967. /// Returns the principal of the root key which was used to sign this writecap.
  968. pub fn root_principal(&self) -> Principal {
  969. self.root_signing_key().principal()
  970. }
  971. /// Returns the path to the root block of the blocktree that the root principal owns.
  972. pub fn root_block_path(&self) -> BlockPath {
  973. BlockPath::new(self.root_principal(), vec![])
  974. }
  975. /// Returns a reference to the path contained in this [Writecap].
  976. pub fn path(&self) -> &BlockPath {
  977. &self.body.path
  978. }
  979. /// Returns the path that the [Principal] this [Writecap] was issued to is allowed to bind.
  980. pub fn bind_path(&self) -> BlockPath {
  981. let mut path = self.body.path.clone();
  982. path.push_component(self.body.issued_to.to_string());
  983. path
  984. }
  985. }
  986. /// Fragments are created from blocks using Erasure Encoding and stored with other nodes in the
  987. /// network to provide availability and redundancy of data.
  988. #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
  989. pub struct Fragment {
  990. /// The path to the block this fragment is from.
  991. path: BlockPath,
  992. /// The serial number of this fragment.
  993. serial: FragmentSerial,
  994. /// The actual data.
  995. body: Vec<u8>,
  996. }
  997. impl Fragment {
  998. /// Create a new fragment with the given fields. If `path_str` cannot be parsed then a failed
  999. /// `Result` is returned containing a `PathError`.
  1000. pub fn new(
  1001. path_str: &str,
  1002. serial_num: u32,
  1003. body: Vec<u8>,
  1004. ) -> std::result::Result<Fragment, BlockPathError> {
  1005. let result = BlockPath::try_from(path_str);
  1006. Ok(Fragment {
  1007. path: result?,
  1008. serial: FragmentSerial(serial_num),
  1009. body,
  1010. })
  1011. }
  1012. }
  1013. #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
  1014. /// Structure for keeping track of server information in a directory.
  1015. pub struct ServerRecord {
  1016. /// The most up-to-date address for this server.
  1017. pub addr: IpAddr,
  1018. /// The public credentials for this server.
  1019. pub pub_creds: ConcretePub,
  1020. }
  1021. #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
  1022. /// Attributes associated with a principal which are used for authorization decisions.
  1023. pub struct AuthzAttrs {
  1024. /// The user ID of the process being authorized.
  1025. pub uid: u32,
  1026. /// The group ID of the process being authorized.
  1027. pub gid: u32,
  1028. /// The group IDs of the supplemental groups in which a process is a member.
  1029. pub supp_gids: Vec<u32>,
  1030. }
  1031. #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
  1032. pub struct IssuedProcRec {
  1033. pub addr: IpAddr,
  1034. pub pub_creds: ConcretePub,
  1035. pub writecap: Writecap,
  1036. pub authz_attrs: AuthzAttrs,
  1037. }
  1038. #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
  1039. /// Structure stored in process blocks for keeping track of process credentials and location.
  1040. pub enum ProcRec {
  1041. Requested {
  1042. addr: IpAddr,
  1043. pub_creds: ConcretePub,
  1044. },
  1045. Valid(IssuedProcRec),
  1046. Revoked(IssuedProcRec),
  1047. }
  1048. impl ProcRec {
  1049. pub fn validate(self) -> Result<IssuedProcRec> {
  1050. match self {
  1051. Self::Requested { .. } => Err(BlockError::ProcRecNotIssued.into()),
  1052. Self::Valid(valid) => Ok(valid),
  1053. Self::Revoked(..) => Err(BlockError::ProcRecRevoked.into()),
  1054. }
  1055. }
  1056. }
  1057. #[derive(Debug, PartialEq, Serialize, Deserialize, EnumDiscriminants, Clone)]
  1058. #[strum_discriminants(derive(FromRepr, Display, Serialize, Deserialize))]
  1059. #[strum_discriminants(name(DirEntryKind))]
  1060. pub enum DirEntry {
  1061. Directory(Inode),
  1062. File(Inode),
  1063. Server(Inode),
  1064. Process(Inode),
  1065. }
  1066. impl DirEntry {
  1067. pub fn inode(&self) -> Inode {
  1068. match self {
  1069. Self::Directory(inode) => *inode,
  1070. Self::File(inode) => *inode,
  1071. Self::Server(inode) => *inode,
  1072. Self::Process(inode) => *inode,
  1073. }
  1074. }
  1075. pub fn kind(&self) -> u8 {
  1076. match self {
  1077. Self::Directory(..) => libc::DT_DIR,
  1078. Self::File(..) => libc::DT_REG,
  1079. Self::Server(..) => libc::DT_UNKNOWN,
  1080. Self::Process(..) => libc::DT_UNKNOWN,
  1081. }
  1082. }
  1083. }
  1084. /// This is the body contained in directory blocks.
  1085. #[derive(Debug, PartialEq, Serialize, Deserialize)]
  1086. pub struct Directory {
  1087. /// This block's descendants.
  1088. entries: BTreeMap<String, DirEntry>,
  1089. }
  1090. impl Directory {
  1091. pub fn new() -> Directory {
  1092. Directory {
  1093. entries: BTreeMap::new(),
  1094. }
  1095. }
  1096. pub fn add_file(&mut self, name: String, inode: Inode) -> Result<()> {
  1097. let entry = match self.entries.entry(name) {
  1098. btree_map::Entry::Occupied(entry) => {
  1099. return Err(bterr!("directory already contains entry '{}'", entry.key()));
  1100. }
  1101. btree_map::Entry::Vacant(entry) => entry,
  1102. };
  1103. entry.insert(DirEntry::File(inode));
  1104. Ok(())
  1105. }
  1106. pub fn num_entries(&self) -> usize {
  1107. self.entries.len()
  1108. }
  1109. pub fn entries(&self) -> impl Iterator<Item = (&str, &DirEntry)> {
  1110. self.entries
  1111. .iter()
  1112. .map(|(name, entry)| (name.as_str(), entry))
  1113. }
  1114. pub fn entry(&self, name: &str) -> Option<&DirEntry> {
  1115. self.entries.get(name)
  1116. }
  1117. pub fn contains_entry(&self, name: &str) -> bool {
  1118. self.entries.contains_key(name)
  1119. }
  1120. pub fn insert_entry(&mut self, name: String, entry: DirEntry) -> Option<DirEntry> {
  1121. self.entries.insert(name, entry)
  1122. }
  1123. pub fn remove_entry(&mut self, name: &str) -> Option<DirEntry> {
  1124. self.entries.remove(name)
  1125. }
  1126. }
  1127. impl Default for Directory {
  1128. fn default() -> Self {
  1129. Self::new()
  1130. }
  1131. }
  1132. /// Keeps track of which principal is storing a fragment.
  1133. #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
  1134. pub struct FragmentRecord {
  1135. /// The fragment serial number this record is for.
  1136. serial: FragmentSerial,
  1137. /// The principal who is storing this fragment.
  1138. stored_by: Principal,
  1139. }
  1140. impl FragmentRecord {
  1141. /// Creates a new `FragmentRecord` whose `serial` and `stored_by` fields are set to
  1142. /// the given values.
  1143. pub fn new(serial: u32, stored_by: VarHash) -> FragmentRecord {
  1144. FragmentRecord {
  1145. serial: FragmentSerial(serial),
  1146. stored_by: Principal(stored_by),
  1147. }
  1148. }
  1149. }
  1150. /// An identifier for a security principal, which is any entity that can be authenticated.
  1151. #[derive(
  1152. Debug, PartialEq, Eq, Serialize, Deserialize, Hashable, Clone, Default, PartialOrd, Ord,
  1153. )]
  1154. pub struct Principal(VarHash);
  1155. impl Principal {
  1156. pub fn kind(&self) -> HashKind {
  1157. HashKind::from(&self.0)
  1158. }
  1159. pub fn as_slice(&self) -> &[u8] {
  1160. self.0.as_slice()
  1161. }
  1162. pub fn as_mut_slice(&mut self) -> &mut [u8] {
  1163. self.0.as_mut_slice()
  1164. }
  1165. }
  1166. impl AsRef<[u8]> for Principal {
  1167. fn as_ref(&self) -> &[u8] {
  1168. self.as_slice()
  1169. }
  1170. }
  1171. impl AsMut<[u8]> for Principal {
  1172. fn as_mut(&mut self) -> &mut [u8] {
  1173. self.as_mut_slice()
  1174. }
  1175. }
  1176. impl TryFrom<&str> for Principal {
  1177. type Error = Error;
  1178. fn try_from(value: &str) -> Result<Self> {
  1179. Ok(Principal(value.try_into()?))
  1180. }
  1181. }
  1182. impl Display for Principal {
  1183. fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
  1184. self.0.fmt(f)
  1185. }
  1186. }
  1187. /// Trait for types which are owned by a `Principal`.
  1188. pub trait Principaled {
  1189. /// Returns the `Principal` that owns `self`, using the given hash algorithm.
  1190. fn principal_of_kind(&self, kind: HashKind) -> Principal;
  1191. /// Returns the `Principal` that owns `self`, using the default hash algorithm.
  1192. fn principal(&self) -> Principal {
  1193. self.principal_of_kind(HashKind::default())
  1194. }
  1195. }
  1196. impl<T: Principaled> Principaled for &T {
  1197. fn principal_of_kind(&self, kind: HashKind) -> Principal {
  1198. (*self).principal_of_kind(kind)
  1199. }
  1200. }
  1201. /// An instant in time represented by the number of seconds since January 1st 1970, 00:00:00 UTC.
  1202. #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
  1203. pub struct Epoch(u64);
  1204. impl Epoch {
  1205. /// Returns the current epoch time.
  1206. pub fn now() -> Epoch {
  1207. let now = SystemTime::now();
  1208. // If the system clock is before the unix epoch, just panic.
  1209. let epoch = now.duration_since(SystemTime::UNIX_EPOCH).unwrap();
  1210. Epoch(epoch.as_secs())
  1211. }
  1212. pub fn from_value(value: u64) -> Self {
  1213. Self(value)
  1214. }
  1215. pub fn value(self) -> u64 {
  1216. self.0
  1217. }
  1218. pub fn to_unix(self) -> libc::time_t {
  1219. self.0 as libc::time_t
  1220. }
  1221. }
  1222. impl Copy for Epoch {}
  1223. impl From<u64> for Epoch {
  1224. fn from(value: u64) -> Self {
  1225. Epoch::from_value(value)
  1226. }
  1227. }
  1228. impl From<Epoch> for u64 {
  1229. fn from(value: Epoch) -> Self {
  1230. value.value()
  1231. }
  1232. }
  1233. impl Add<Duration> for Epoch {
  1234. type Output = Self;
  1235. fn add(self, other: Duration) -> Self {
  1236. Epoch(self.0 + other.as_secs())
  1237. }
  1238. }
  1239. impl Sub<Duration> for Epoch {
  1240. type Output = Self;
  1241. fn sub(self, other: Duration) -> Self {
  1242. Epoch(self.0 - other.as_secs())
  1243. }
  1244. }
  1245. impl Sub<Epoch> for Epoch {
  1246. type Output = Duration;
  1247. fn sub(self, other: Epoch) -> Self::Output {
  1248. Duration::from_secs(self.0 - other.0)
  1249. }
  1250. }
  1251. /// The serial number of a block fragment.
  1252. #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Hashable, Clone)]
  1253. pub struct FragmentSerial(u32);
  1254. /// A struct which may contain a closure. When this struct is dropped, if it contains a closure
  1255. /// then that closure is called. This closure can be removed by calling `disarm`.
  1256. pub struct DropTrigger<F: FnOnce()>(Option<F>);
  1257. impl<F: FnOnce()> DropTrigger<F> {
  1258. pub fn new(trigger: F) -> DropTrigger<F> {
  1259. DropTrigger(Some(trigger))
  1260. }
  1261. pub fn disarm(&mut self) {
  1262. self.0.take();
  1263. }
  1264. }
  1265. impl<F: FnOnce()> Drop for DropTrigger<F> {
  1266. fn drop(&mut self) {
  1267. if let Some(trigger) = self.0.take() {
  1268. trigger()
  1269. }
  1270. }
  1271. }
  1272. #[cfg(test)]
  1273. mod tests {
  1274. use btserde::{from_vec, to_vec};
  1275. use std::{fs::OpenOptions, io::Write, path::PathBuf};
  1276. use tempdir::TempDir;
  1277. use super::*;
  1278. use crate::{
  1279. crypto::{ConcreteCreds, CredsPriv},
  1280. sectored_buf::SectoredBuf,
  1281. test_helpers::{node_creds, root_creds, SectoredCursor, SECTOR_SZ_DEFAULT},
  1282. Cursor as PioCursor,
  1283. };
  1284. /// Tests that the `BlockMetaBody` struct has an updated secrets struct after it is modified
  1285. /// in the `access_secrets` method.
  1286. #[test]
  1287. fn block_meta_body_secrets_updated_after_access() {
  1288. const UID: u32 = 1000;
  1289. let creds = test_helpers::NODE_CREDS.clone();
  1290. let vec = {
  1291. let mut body = BlockMetaBody::new(&creds).expect("failed to create meta body");
  1292. body.access_secrets(|secrets| {
  1293. secrets.uid = UID;
  1294. Ok(())
  1295. })
  1296. .expect("access secrets failed");
  1297. to_vec(&body).expect("to_vec failed")
  1298. };
  1299. let mut body = from_vec::<BlockMetaBody>(&vec).expect("from_vec failed");
  1300. body.use_readcap_for(&creds)
  1301. .expect("unlock_block_key failed");
  1302. let actual_uid = body
  1303. .access_secrets(|secrets| Ok(secrets.uid))
  1304. .expect("access_secrets failed");
  1305. assert_eq!(UID, actual_uid);
  1306. }
  1307. struct InMemTestCase {
  1308. node_creds: ConcreteCreds,
  1309. block_path: BlockPath,
  1310. block_id: BlockId,
  1311. }
  1312. type EncBlock = SectoredBuf<
  1313. SecretStream<PioCursor<MerkleStream<BlockStream<SectoredCursor<Vec<u8>>, ConcreteCreds>>>>,
  1314. >;
  1315. impl InMemTestCase {
  1316. fn new() -> InMemTestCase {
  1317. let components = vec!["nodes".to_string(), "phone".to_string()];
  1318. let node_creds = {
  1319. let mut node_creds = node_creds().clone();
  1320. let writecap = root_creds()
  1321. .issue_writecap(
  1322. node_creds.principal(),
  1323. components.clone(),
  1324. Epoch::now() + Duration::from_secs(3600),
  1325. )
  1326. .expect("failed to issue writecap");
  1327. node_creds.set_writecap(writecap);
  1328. node_creds
  1329. };
  1330. let block_path = BlockPath::new(root_creds().principal(), components);
  1331. let block_id = BlockId::default();
  1332. Self {
  1333. node_creds,
  1334. block_path,
  1335. block_id,
  1336. }
  1337. }
  1338. fn stream(&self, vec: Vec<u8>) -> EncBlock {
  1339. let inner = SectoredCursor::new(vec, SECTOR_SZ_DEFAULT).require_sect_sz(false);
  1340. let mut stream = BlockStream::new(
  1341. inner,
  1342. self.node_creds.clone(),
  1343. None,
  1344. self.block_path.clone(),
  1345. )
  1346. .unwrap();
  1347. stream
  1348. .mut_meta_body()
  1349. .access_secrets(|secrets| {
  1350. secrets.block_id = self.block_id.clone();
  1351. Ok(())
  1352. })
  1353. .unwrap();
  1354. let block_key = stream.meta_body().block_key().unwrap().to_owned();
  1355. let mut stream = MerkleStream::new(stream).unwrap();
  1356. stream.assert_root_integrity().unwrap();
  1357. let stream = PioCursor::new(stream);
  1358. let stream = SecretStream::new(block_key).try_compose(stream).unwrap();
  1359. SectoredBuf::new().try_compose(stream).unwrap()
  1360. }
  1361. fn into_vec(stream: EncBlock) -> Vec<u8> {
  1362. stream
  1363. .into_inner()
  1364. .into_inner()
  1365. .into_inner()
  1366. .into_inner()
  1367. .into_inner()
  1368. .0
  1369. .into_inner()
  1370. }
  1371. }
  1372. #[test]
  1373. fn block_write_read_with_cursor() {
  1374. const EXPECTED: &[u8] = b"Silly sordid sulking sultans.";
  1375. let case = InMemTestCase::new();
  1376. let mut stream = case.stream(Vec::new());
  1377. stream.write_all(EXPECTED).unwrap();
  1378. stream.flush().unwrap();
  1379. let vec = InMemTestCase::into_vec(stream);
  1380. let mut stream = case.stream(vec);
  1381. let mut actual = [0u8; EXPECTED.len()];
  1382. stream.read(&mut actual).unwrap();
  1383. assert_eq!(EXPECTED, actual);
  1384. }
  1385. #[test]
  1386. fn block_write_multiple() {
  1387. const ITER: usize = 16;
  1388. let case = InMemTestCase::new();
  1389. let mut stream = case.stream(Vec::new());
  1390. let expected = vec![1u8; stream.sector_sz()];
  1391. for _ in 0..ITER {
  1392. stream.write(&expected).unwrap();
  1393. }
  1394. stream.flush().unwrap();
  1395. }
  1396. pub struct BlockTestCase {
  1397. temp_dir: TempDir,
  1398. root_path: BlockPath,
  1399. node_path: BlockPath,
  1400. root_creds: ConcreteCreds,
  1401. node_creds: ConcreteCreds,
  1402. }
  1403. impl BlockTestCase {
  1404. fn new() -> BlockTestCase {
  1405. let temp_dir = TempDir::new("block_test").expect("failed to create temp dir");
  1406. let root_creds = test_helpers::ROOT_CREDS.clone();
  1407. let mut node_creds = test_helpers::NODE_CREDS.clone();
  1408. let components = vec!["nodes".to_string(), "phone".to_string()];
  1409. let writecap = root_creds
  1410. .issue_writecap(
  1411. node_creds.principal(),
  1412. components.clone(),
  1413. Epoch::now() + Duration::from_secs(3600),
  1414. )
  1415. .expect("failed to issue writecap");
  1416. node_creds.set_writecap(writecap);
  1417. let case = BlockTestCase {
  1418. temp_dir,
  1419. node_path: BlockPath::new(root_creds.principal(), components),
  1420. root_path: BlockPath::new(root_creds.principal(), vec![]),
  1421. node_creds,
  1422. root_creds,
  1423. };
  1424. std::fs::create_dir_all(case.fs_path(&case.node_path))
  1425. .expect("failed to create node path");
  1426. case
  1427. }
  1428. fn fs_path(&self, path: &crate::BlockPath) -> PathBuf {
  1429. let mut fs_path = self.temp_dir.path().to_owned();
  1430. fs_path.extend(path.components());
  1431. fs_path
  1432. }
  1433. fn open_new(&mut self, path: crate::BlockPath) -> Accessor<impl Block + 'static> {
  1434. let file = OpenOptions::new()
  1435. .create_new(true)
  1436. .read(true)
  1437. .write(true)
  1438. .open(&self.fs_path(&path))
  1439. .expect("failed to open file");
  1440. let mut block = BlockOpenOptions::new()
  1441. .with_inner(file)
  1442. .with_creds(self.node_creds.clone())
  1443. .with_encrypt(true)
  1444. .with_block_path(path)
  1445. .open()
  1446. .expect("failed to open block");
  1447. block
  1448. .mut_meta_body()
  1449. .set_path(self.node_creds.writecap().unwrap().body.path.clone());
  1450. block
  1451. }
  1452. fn open_existing(&mut self, path: crate::BlockPath) -> Accessor<impl Block + 'static> {
  1453. let file = OpenOptions::new()
  1454. .read(true)
  1455. .write(true)
  1456. .open(&self.fs_path(&path))
  1457. .expect("failed to reopen file");
  1458. BlockOpenOptions::new()
  1459. .with_inner(file)
  1460. .with_creds(self.node_creds.clone())
  1461. .with_encrypt(true)
  1462. .with_block_path(path)
  1463. .open()
  1464. .expect("failed to reopen block")
  1465. }
  1466. /// Returns a path in the directory which the node creds have permission to write to.
  1467. fn node_path(&self, file_name: &str) -> BlockPath {
  1468. let mut path = self.node_path.clone();
  1469. path.push_component(file_name.to_owned());
  1470. path
  1471. }
  1472. }
  1473. #[test]
  1474. fn block_can_create_empty() {
  1475. let case = BlockTestCase::new();
  1476. BlockOpenOptions::new()
  1477. .with_inner(SectoredCursor::new(Vec::<u8>::new(), SECTOR_SZ_DEFAULT))
  1478. .with_creds(case.node_creds)
  1479. .with_encrypt(true)
  1480. .with_block_path(case.root_path)
  1481. .open()
  1482. .expect("failed to open block");
  1483. }
  1484. #[test]
  1485. fn block_contents_persisted() {
  1486. const EXPECTED: &[u8] = b"Silly sordid sulking sultans.";
  1487. let mut case = BlockTestCase::new();
  1488. let path = case.node_path("test.blk");
  1489. {
  1490. let mut block = case.open_new(path.clone());
  1491. block.write(EXPECTED).expect("failed to write");
  1492. block.flush().expect("flush failed");
  1493. }
  1494. let mut block = case.open_existing(path);
  1495. let mut actual = [0u8; EXPECTED.len()];
  1496. block.read(&mut actual).expect("read failed");
  1497. assert_eq!(EXPECTED, actual);
  1498. }
  1499. #[test]
  1500. fn block_write_twice() {
  1501. const EXPECTED: &[u8] = b"Cool callous calamitous colonels.";
  1502. const MID: usize = EXPECTED.len() / 2;
  1503. let mut case = BlockTestCase::new();
  1504. let path = case.node_path("test.blk");
  1505. {
  1506. let mut block = case.open_new(path.clone());
  1507. block.write(&EXPECTED[..MID]).expect("first write failed");
  1508. block.flush().expect("first flush failed");
  1509. }
  1510. {
  1511. let mut block = case.open_existing(path.clone());
  1512. block
  1513. .seek(SeekFrom::Start(MID.try_into().unwrap()))
  1514. .expect("seek failed");
  1515. block.write(&EXPECTED[MID..]).expect("second write failed");
  1516. block.flush().expect("second flush failed");
  1517. }
  1518. {
  1519. let mut block = case.open_existing(path);
  1520. let mut actual = [0u8; EXPECTED.len()];
  1521. block.read(&mut actual).expect("read failed");
  1522. assert_eq!(EXPECTED, actual);
  1523. }
  1524. }
  1525. #[test]
  1526. fn block_write_with_different_creds() {
  1527. const EXPECTED: &[u8] = b"Cool callous calamitous colonels.";
  1528. const MID: usize = EXPECTED.len() / 2;
  1529. let mut case = BlockTestCase::new();
  1530. let path = case.node_path("test.blk");
  1531. let app_creds = {
  1532. let mut app_creds = ConcreteCreds::generate().expect("failed to generate app creds");
  1533. let writecap = case
  1534. .root_creds
  1535. .issue_writecap(
  1536. app_creds.principal(),
  1537. path.components().map(|e| e.to_string()).collect(),
  1538. Epoch::now() + Duration::from_secs(60),
  1539. )
  1540. .expect("failed to issue writecap");
  1541. app_creds.set_writecap(writecap);
  1542. app_creds
  1543. };
  1544. {
  1545. let mut block = case.open_new(path.clone());
  1546. block
  1547. .mut_meta_body()
  1548. .add_readcap_for(&app_creds)
  1549. .expect("failed to add readcap");
  1550. block.write(&EXPECTED[..MID]).expect("first write failed");
  1551. block.flush().expect("first flush failed");
  1552. }
  1553. {
  1554. let file = OpenOptions::new()
  1555. .read(true)
  1556. .write(true)
  1557. .open(case.fs_path(&path))
  1558. .expect("failed to reopen file");
  1559. let mut block = BlockOpenOptions::new()
  1560. .with_inner(file)
  1561. // Note that this write is performed using app_creds.
  1562. .with_creds(app_creds)
  1563. .with_encrypt(true)
  1564. .with_block_path(path.clone())
  1565. .open()
  1566. .expect("failed to reopen block");
  1567. block
  1568. .seek(SeekFrom::Start(MID.try_into().unwrap()))
  1569. .expect("seek failed");
  1570. block.write(&EXPECTED[MID..]).expect("second write failed");
  1571. block.flush().expect("second flush failed");
  1572. }
  1573. {
  1574. let file = OpenOptions::new()
  1575. .read(true)
  1576. .write(true)
  1577. .open(case.fs_path(&path))
  1578. .expect("failed to reopen file");
  1579. let mut block = BlockOpenOptions::new()
  1580. .with_inner(file)
  1581. .with_creds(case.node_creds)
  1582. .with_encrypt(true)
  1583. .with_block_path(path)
  1584. .open()
  1585. .expect("failed to reopen block");
  1586. let mut actual = [0u8; EXPECTED.len()];
  1587. block.read(&mut actual).expect("read failed");
  1588. assert_eq!(EXPECTED, actual);
  1589. }
  1590. }
  1591. #[test]
  1592. fn block_try_seek_and_get_buf() {
  1593. const DIVISOR: usize = 8;
  1594. let mut case = BlockTestCase::new();
  1595. let path = case.node_path("blob.dat");
  1596. let mut block = case.open_new(path);
  1597. let sect_sz = block.sector_sz();
  1598. let read_sz = sect_sz / DIVISOR;
  1599. let mut expected = vec![0u8; read_sz];
  1600. for index in 0..(DIVISOR as u8 + 2) {
  1601. expected.fill(index + 1);
  1602. block.write(&expected).unwrap();
  1603. }
  1604. block.rewind().unwrap();
  1605. for index in 0..(DIVISOR as u8 + 2) {
  1606. let offset = (read_sz * index as usize) as u64;
  1607. block.try_seek(SeekFrom::Start(offset)).unwrap();
  1608. let actual = block.get_buf(offset, read_sz as u64).unwrap();
  1609. expected.fill(index + 1);
  1610. assert!(actual == expected);
  1611. }
  1612. }
  1613. /// Tests that the last component of a [Writecap]'s bind path is the string representation of
  1614. /// the [Principal] to which the [Writecap] was issued.
  1615. #[test]
  1616. fn writecap_bind_path_last_component_is_principal() {
  1617. let creds = node_creds();
  1618. let writecap = creds.writecap().unwrap();
  1619. let expected = writecap.issued_to().to_string();
  1620. let bind_path = writecap.bind_path();
  1621. let actual = bind_path.components().last().unwrap();
  1622. assert_eq!(expected, actual);
  1623. }
  1624. }