diff --git a/Cargo.toml b/Cargo.toml index e8a722802..7a8c3ec53 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -90,3 +90,6 @@ webrtc = "0.13.0" # keep in lockstep with [dependencies] webrtc (rustc 1.75 pin, [target.'cfg(target_os = "linux")'.dependencies] users = { version = "0.11" } x11 = "2.21" + +[target.'cfg(target_os = "windows")'.dependencies] +windows = { version = "0.61", features = ["Win32_Storage_FileSystem", "Win32_System_IO", "Win32_Security"] } diff --git a/src/config.rs b/src/config.rs index 6fe100455..efeb4b8c7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -19,7 +19,9 @@ use serde_json; use sodiumoxide::base64; use sodiumoxide::crypto::sign; +mod file_storage; mod permanent_password; +pub use file_storage::FileStorage; pub use permanent_password::{ compute_permanent_password_h1, decode_permanent_password_h1_from_storage, @@ -249,6 +251,13 @@ pub struct Config2 { #[serde(default, deserialize_with = "deserialize_string")] trusted_devices: String, + #[serde( + default, + deserialize_with = "deserialize_string", + skip_serializing_if = "String::is_empty" + )] + trusted_devices_v2: String, + #[serde(default)] socks: Option, @@ -512,8 +521,17 @@ impl Config2 { } fn store(&self) { + if let Err(error) = self.try_store() { + log::error!("Failed to store config2: {error}"); + } + } + + fn try_store(&self) -> crate::ResultType<()> { + let storage = FileStorage::lock(&Self::file())?; let mut config = self.clone(); - let stored = Config::load_::("2"); + let stored: Config2 = storage.read_toml()?; + // Ordinary settings and stale process snapshots must never restore grants. + config.trusted_devices_v2 = stored.trusted_devices_v2; if let Some(mut socks) = config.socks { let stored_password = stored .socks @@ -526,7 +544,26 @@ impl Config2 { } config.unlock_pin = keep_encrypted_storage_if_plaintext_unchanged(&config.unlock_pin, &stored.unlock_pin); - Config::store_(&config, "2"); + storage.write_toml(&config) + } + + pub fn get_trusted_devices_v2() -> crate::ResultType { + let config: Self = FileStorage::lock(&Self::file())?.read_toml()?; + Ok(config.trusted_devices_v2) + } + + pub fn set_trusted_devices_v2(value: String) -> crate::ResultType<()> { + let storage = FileStorage::lock(&Self::file())?; + let mut config: Self = storage.read_toml()?; + config.trusted_devices_v2 = value; + storage.write_toml(&config) + } + + pub fn without_trusted_devices_v2(self) -> Self { + Self { + trusted_devices_v2: String::new(), + ..self + } } pub fn get() -> Config2 { diff --git a/src/config/file_storage.rs b/src/config/file_storage.rs new file mode 100644 index 000000000..fdf05b896 --- /dev/null +++ b/src/config/file_storage.rs @@ -0,0 +1,162 @@ +use crate::{anyhow::Context, ResultType}; +use serde::{de::DeserializeOwned, Serialize}; +use std::{ + fs::{self, File, OpenOptions}, + io::{ErrorKind, Write}, + path::{Path, PathBuf}, +}; + +#[cfg(unix)] +const PRIVATE_DIRECTORY_MODE: u32 = 0o700; +#[cfg(unix)] +const PRIVATE_FILE_MODE: u32 = 0o600; + +pub struct FileStorage { + path: PathBuf, + _lock: File, +} + +impl FileStorage { + pub fn lock(path: &Path) -> ResultType { + let parent = path + .parent() + .ok_or_else(|| crate::anyhow::anyhow!("Missing credential directory"))?; + let mut directories = fs::DirBuilder::new(); + directories.recursive(true); + #[cfg(unix)] + { + use std::os::unix::fs::DirBuilderExt; + directories.mode(PRIVATE_DIRECTORY_MODE); + } + directories + .create(parent) + .context("Create trusted-device credential directory")?; + let lock = lock_file(&path.with_extension("lock"))?; + Ok(Self { + path: path.to_owned(), + _lock: lock, + }) + } + + pub fn read(&self) -> ResultType { + match File::open(&self.path) { + Ok(file) => serde_json::from_reader(file).context("Read trusted-device credentials"), + Err(error) if error.kind() == ErrorKind::NotFound => Ok(T::default()), + Err(error) => Err(error).context("Open trusted-device credentials"), + } + } + + pub fn read_toml(&self) -> ResultType { + match fs::read_to_string(&self.path) { + Ok(value) => crate::toml::from_str(&value) + .map_err(|_| crate::anyhow::anyhow!("Invalid configuration file")), + Err(error) if error.kind() == ErrorKind::NotFound => Ok(T::default()), + Err(error) => Err(error).context("Open configuration file"), + } + } + + pub fn write_toml(&self, value: &T) -> ResultType<()> { + let value = crate::toml::to_string(value).context("Encode configuration update")?; + self.write_with(|file| Ok(file.write_all(value.as_bytes())?)) + } + + pub fn write(&self, value: &T) -> ResultType<()> { + self.write_with(|file| { + serde_json::to_writer(file, value).context("Encode trusted-device credential update") + }) + } + + fn write_with(&self, write: impl FnOnce(&mut File) -> ResultType<()>) -> ResultType<()> { + let temporary = self + .path + .with_extension(format!("{}.tmp", crate::uuid::Uuid::new_v4())); + let mut options = OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options.mode(PRIVATE_FILE_MODE); + } + let mut file = options + .open(&temporary) + .context("Create trusted-device credential update")?; + write(&mut file)?; + file.sync_all() + .context("Persist trusted-device credential update")?; + #[cfg(not(windows))] + fs::rename(&temporary, &self.path).context("Commit trusted-device credential update")?; + #[cfg(windows)] + replace_file(&temporary, &self.path)?; + #[cfg(unix)] + if let Some(parent) = self.path.parent() { + File::open(parent)? + .sync_all() + .context("Persist trusted-device credential directory")?; + } + Ok(()) + } +} + +fn lock_file(path: &Path) -> ResultType { + let mut options = OpenOptions::new(); + options.read(true).write(true).create(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options + .mode(PRIVATE_FILE_MODE) + .custom_flags(crate::libc::O_NOFOLLOW | crate::libc::O_CLOEXEC); + } + #[cfg(windows)] + { + use std::os::windows::fs::OpenOptionsExt; + use windows::Win32::Storage::FileSystem::{FILE_SHARE_READ, FILE_SHARE_WRITE}; + options.share_mode(FILE_SHARE_READ.0 | FILE_SHARE_WRITE.0); + } + let file = options + .open(path) + .context("Lock trusted-device credentials")?; + #[cfg(unix)] + { + use std::os::fd::AsRawFd; + let result = unsafe { crate::libc::flock(file.as_raw_fd(), crate::libc::LOCK_EX) }; + if result != 0 { + return Err(std::io::Error::last_os_error()).context("Lock trusted-device credentials"); + } + } + #[cfg(windows)] + lock_windows_file(&file)?; + Ok(file) +} + +#[cfg(windows)] +fn lock_windows_file(file: &File) -> ResultType<()> { + use std::os::windows::io::AsRawHandle; + use windows::Win32::{Foundation::HANDLE, Storage::FileSystem, System::IO::OVERLAPPED}; + const LOCK_BYTES: u32 = 1; + // This is a synchronous handle: wait for the lock; closing the handle releases it. + unsafe { + FileSystem::LockFileEx( + HANDLE(file.as_raw_handle()), + FileSystem::LOCKFILE_EXCLUSIVE_LOCK, + None, + LOCK_BYTES, + 0, + &mut OVERLAPPED::default(), + ) + } + .context("Lock trusted-device credentials") +} + +#[cfg(windows)] +fn replace_file(source: &Path, destination: &Path) -> ResultType<()> { + use windows::{core::HSTRING, Win32::Storage::FileSystem}; + unsafe { + FileSystem::MoveFileExW( + &HSTRING::from(source), + &HSTRING::from(destination), + FileSystem::MOVEFILE_REPLACE_EXISTING | FileSystem::MOVEFILE_WRITE_THROUGH, + ) + } + .context("Commit trusted-device credential update") +}