use crate::{Secret, SecretError};
use std::{fs::File, os::unix::fs::MetadataExt};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ChromiumBrand {
Chrome,
Chromium,
Slack452178,
}
impl ChromiumBrand {
pub(crate) fn application(self) -> &'static str {
match self {
Self::Chrome => "chrome",
Self::Chromium => "chromium",
Self::Slack452178 => "Slack",
}
}
}
pub struct ChromiumKey {
profile: File,
identity: (u32, u64, u64),
brand: ChromiumBrand,
}
impl ChromiumKey {
pub fn new(profile: &File, brand: ChromiumBrand) -> Result<Self, SecretError> {
let facts = profile.metadata().map_err(|_| SecretError::Denied)?;
let owner = rustix::process::getuid().as_raw();
if !facts.is_dir()
|| facts.uid() != owner
|| facts.mode() & 0o022 != 0
|| owner == 0
|| rustix::process::geteuid().as_raw() != owner
{
return Err(SecretError::Denied);
}
Ok(Self {
profile: profile.try_clone().map_err(|_| SecretError::Denied)?,
identity: (owner, facts.dev(), facts.ino()),
brand,
})
}
pub fn read_blocking(&self) -> Result<Option<Secret>, SecretError> {
let facts = self.profile.metadata().map_err(|_| SecretError::Denied)?;
if !facts.is_dir()
|| (facts.uid(), facts.dev(), facts.ino()) != self.identity
|| facts.mode() & 0o022 != 0
|| rustix::process::getuid().as_raw() != self.identity.0
|| rustix::process::geteuid().as_raw() != self.identity.0
{
return Err(SecretError::Denied);
}
crate::linux::read_chromium(self.identity.0, self.brand.application())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn owner_scope_refuses_files_and_elevation_without_contacting_a_keyring() {
tilo_test_env::require_capability!(
NonRoot,
tilo_test_env::non_root_identity(),
"elevation is only refused to an ordinary account"
);
let directory = tempfile::tempdir().unwrap();
let file = File::create(directory.path().join("not-profile")).unwrap();
assert!(ChromiumKey::new(&file, ChromiumBrand::Chrome).is_err());
assert!(
ChromiumKey::new(
&File::open(directory.path()).unwrap(),
ChromiumBrand::Chromium
)
.is_ok()
);
assert_eq!(ChromiumBrand::Chrome.application(), "chrome");
assert_eq!(ChromiumBrand::Chromium.application(), "chromium");
assert_eq!(ChromiumBrand::Slack452178.application(), "Slack");
}
}