Commit 20a85061 authored by Nick Mathewson's avatar Nick Mathewson 🦞
Browse files

Update tor-dirmgr to use fs-mistrust.

parent 984190b3
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ dirfilter = []
experimental-api = []

[dependencies]
fs-mistrust = { path = "../fs-mistrust", version = "0.1.0" }
tor-basic-utils = { path = "../tor-basic-utils", version = "0.2.0" }
retry-error = { path = "../retry-error", version = "0.1.0" }
tor-checkable = { path = "../tor-checkable", version = "0.2.0" }
+11 −4
Original line number Diff line number Diff line
@@ -189,6 +189,9 @@ pub struct DirMgrConfig {
    /// Cannot be changed on a running Arti client.
    pub cache_path: PathBuf,

    /// Rules for whether to trust the the permissions on the cache_path.
    pub cache_trust: fs_mistrust::Mistrust,

    /// Configuration information about the network.
    pub network: NetworkConfig,

@@ -228,10 +231,13 @@ impl DirMgrConfig {
    /// Note that each time this is called, a new store object will be
    /// created: you probably only want to call this once.
    pub(crate) fn open_store(&self, readonly: bool) -> Result<DynStore> {
        Ok(Box::new(crate::storage::SqliteStore::from_path(
        Ok(Box::new(
            crate::storage::SqliteStore::from_path_and_mistrust(
                &self.cache_path,
                &self.cache_trust,
                readonly,
        )?))
            )?,
        ))
    }

    /// Return a slice of the configured authorities
@@ -251,6 +257,7 @@ impl DirMgrConfig {
    pub(crate) fn update_from_config(&self, new_config: &DirMgrConfig) -> DirMgrConfig {
        DirMgrConfig {
            cache_path: self.cache_path.clone(),
            cache_trust: self.cache_trust.clone(),
            network: NetworkConfig {
                fallback_caches: new_config.network.fallback_caches.clone(),
                authorities: self.network.authorities.clone(),
+11 −0
Original line number Diff line number Diff line
@@ -83,6 +83,9 @@ pub enum Error {
    /// An attempt was made to bootstrap a `DirMgr` created in offline mode.
    #[error("cannot bootstrap offline DirMgr")]
    OfflineMode,
    /// A problem with file permissions on our cache directory.
    #[error("Bad permissions in cache directory")]
    CachePermissions(#[from] fs_mistrust::Error),

    /// Unable to spawn task
    #[error("unable to spawn {spawning}")]
@@ -154,6 +157,7 @@ impl Error {
            // These errors cannot come from a directory cache.
            Error::NoDownloadSupport
            | Error::CacheCorruption(_)
            | Error::CachePermissions(_)
            | Error::SqliteError(_)
            | Error::UnrecognizedSchema
            | Error::BadNetworkConfig(_)
@@ -209,6 +213,13 @@ impl HasKind for Error {
            E::Unwanted(_) => EK::TorProtocolViolation,
            E::NoDownloadSupport => EK::NotImplemented,
            E::CacheCorruption(_) => EK::CacheCorrupted,
            E::CachePermissions(e) => {
                if e.is_bad_permission() {
                    EK::FsPermissions
                } else {
                    EK::CacheAccessFailed
                }
            }
            E::SqliteError(e) => sqlite_error_kind(e),
            E::UnrecognizedSchema => EK::CacheCorrupted,
            E::BadNetworkConfig(_) => EK::InvalidConfig,
+6 −1
Original line number Diff line number Diff line
@@ -995,7 +995,12 @@ mod test {
    fn temp_store() -> (TempDir, Mutex<DynStore>) {
        let tempdir = TempDir::new().unwrap();

        let store = crate::storage::SqliteStore::from_path(tempdir.path(), false).unwrap();
        let store = crate::storage::SqliteStore::from_path_and_mistrust(
            tempdir.path(),
            fs_mistrust::Mistrust::new().dangerously_trust_everyone(),
            false,
        )
        .unwrap();

        (tempdir, Mutex::new(Box::new(store)))
    }
+8 −14
Original line number Diff line number Diff line
@@ -17,8 +17,9 @@ use crate::docmeta::{AuthCertMeta, ConsensusMeta};
use crate::{Error, Result};
use std::cell::RefCell;
use std::collections::HashMap;
use std::fs::File;
use std::str::Utf8Error;
use std::time::SystemTime;
use std::{path::Path, str::Utf8Error};
use time::Duration;

pub(crate) mod sqlite;
@@ -124,18 +125,15 @@ impl InputString {
            }
        }
    }

    /// Construct a new InputString from a file on disk, trying to
    /// memory-map the file if possible.
    pub(crate) fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
        let f = std::fs::File::open(path)?;
    /// DOCDOC
    pub(crate) fn load(file: File) -> Result<Self> {
        #[cfg(feature = "mmap")]
        {
            let mapping = unsafe {
                // I'd rather have a safe option, but that's not possible
                // with mmap, since other processes could in theory replace
                // the contents of the file while we're using it.
                memmap2::Mmap::map(&f)
                memmap2::Mmap::map(&file)
            };
            if let Ok(bytes) = mapping {
                return Ok(InputString::MappedBytes {
@@ -145,7 +143,7 @@ impl InputString {
            }
        }
        use std::io::{BufReader, Read};
        let mut f = BufReader::new(f);
        let mut f = BufReader::new(file);
        let mut result = String::new();
        f.read_to_string(&mut result)?;
        Ok(InputString::Utf8(result))
@@ -308,13 +306,9 @@ mod test {
    fn files() {
        let td = tempdir().unwrap();

        let absent = td.path().join("absent");
        let s = InputString::load(&absent);
        assert!(s.is_err());

        let goodstr = td.path().join("goodstr");
        std::fs::write(&goodstr, "This is a reasonable file.\n").unwrap();
        let s = InputString::load(&goodstr);
        let s = InputString::load(File::open(goodstr).unwrap());
        let s = s.unwrap();
        assert_eq!(s.as_str().unwrap(), "This is a reasonable file.\n");
        assert_eq!(s.as_str().unwrap(), "This is a reasonable file.\n");
@@ -322,7 +316,7 @@ mod test {

        let badutf8 = td.path().join("badutf8");
        std::fs::write(&badutf8, b"Not good \xff UTF-8.\n").unwrap();
        let s = InputString::load(&badutf8);
        let s = InputString::load(File::open(badutf8).unwrap());
        assert!(s.is_err() || s.unwrap().as_str().is_err());
    }

Loading