Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 147 additions & 6 deletions src/drives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ use crate::config::Config;
use crate::mountpoints;
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub enum Encryption {
Unencrypted,
Locked,
Unlocked(Cleartext),
}

#[derive(Debug, Clone)]
pub struct Cleartext {
pub object_path: String,
pub devices: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct Block {
pub object_path: String,
Expand All @@ -12,6 +25,15 @@ pub struct Block {
pub fstype: String,
pub mounted: bool,
pub size: String,
pub encryption: Encryption,
}

impl Block {
fn has_device(&self, dev: &str) -> bool {
self.dev == dev
|| matches!(&self.encryption, Encryption::Unlocked(cleartext)
if cleartext.devices.iter().any(|name| name == dev))
}
}

#[derive(Debug, Clone)]
Expand All @@ -23,6 +45,34 @@ pub struct Drive {
pub blocks: Vec<Block>,
}

fn device_name(bytes: &[u8]) -> String {
String::from_utf8_lossy(bytes)
.chars()
.filter(|c| c != &'\0')
.collect()
}

async fn encryption_of(
client: &udisks2::Client,
object: &udisks2::Object,
) -> udisks2::Result<Encryption> {
let Ok(encrypted) = object.encrypted().await else {
return Ok(Encryption::Unencrypted);
};
let cleartext_path = encrypted.cleartext_device().await?.to_string();
if cleartext_path == "/" {
return Ok(Encryption::Locked);
}
let cleartext = client.object(cleartext_path.clone())?.block().await?;
Ok(Encryption::Unlocked(Cleartext {
object_path: cleartext_path,
devices: vec![
device_name(&cleartext.device().await?),
device_name(&cleartext.preferred_device().await?),
],
}))
}

pub async fn collect_drives_from_udisk() -> udisks2::Result<Vec<Drive>> {
let mut drives: Vec<Drive> = Vec::new();
let client = udisks2::Client::new().await?;
Expand Down Expand Up @@ -58,6 +108,9 @@ pub async fn collect_drives_from_udisk() -> udisks2::Result<Vec<Drive>> {
drives.push(drv);
}
} else if let Ok(blk) = i.block().await {
if blk.crypto_backing_device().await?.as_str() != "/" {
continue;
}
let drv_path = blk.drive().await?.to_string();
let size = if let Ok(size) = blk.size().await {
client.size_for_display(size, true, false)
Expand All @@ -67,15 +120,13 @@ pub async fn collect_drives_from_udisk() -> udisks2::Result<Vec<Drive>> {
let block = Block {
object_path: path,
drive_path: drv_path.clone(),
dev: String::from_utf8_lossy(&blk.device().await?)
.chars()
.filter(|c| c != &'\0')
.collect::<String>(),
dev: device_name(&blk.device().await?),
label: blk.id_label().await?,
mount: None,
fstype: blk.id_type().await?,
mounted: false,
size,
encryption: encryption_of(&client, &i).await?,
};

if let Some(d) = drives.iter_mut().find(|i| i.object_path == drv_path) {
Expand Down Expand Up @@ -110,8 +161,8 @@ pub async fn collect_all(config: &Config) -> udisks2::Result<Vec<Drive>> {
for i in mounts {
let block = drives
.iter_mut()
.find(|d| d.blocks.iter().any(|b| b.dev == i.dev))
.and_then(|d| d.blocks.iter_mut().find(|b| b.dev == i.dev));
.find(|d| d.blocks.iter().any(|b| b.has_device(&i.dev)))
.and_then(|d| d.blocks.iter_mut().find(|b| b.has_device(&i.dev)));
if let Some(block) = block {
block.mount = i.path;
block.mounted = i.mounted;
Expand All @@ -125,6 +176,7 @@ pub async fn collect_all(config: &Config) -> udisks2::Result<Vec<Drive>> {
fstype: i.fs,
mounted: i.mounted,
size: String::new(),
encryption: Encryption::Unencrypted,
});
}
}
Expand All @@ -140,9 +192,32 @@ pub async fn collect_all(config: &Config) -> udisks2::Result<Vec<Drive>> {

pub async fn mount(block: &Block) -> udisks2::Result<()> {
let client = udisks2::Client::new().await?;
let filesystem_path = match &block.encryption {
Encryption::Unlocked(cleartext) => cleartext.object_path.clone(),
Encryption::Unencrypted | Encryption::Locked => block.object_path.clone(),
};

client
.object(filesystem_path)?
.filesystem()
.await?
.mount(HashMap::new())
.await?;

Ok(())
}

pub async fn unlock_and_mount(block: &Block, passphrase: &str) -> udisks2::Result<()> {
let client = udisks2::Client::new().await?;

let cleartext_path = client
.object(block.object_path.clone())?
.encrypted()
.await?
.unlock(passphrase, HashMap::new())
.await?;
client
.object(cleartext_path)?
.filesystem()
.await?
.mount(HashMap::new())
Expand All @@ -154,6 +229,25 @@ pub async fn mount(block: &Block) -> udisks2::Result<()> {
pub async fn unmount(block: &Block) -> udisks2::Result<()> {
let client = udisks2::Client::new().await?;

if let Encryption::Unlocked(cleartext) = &block.encryption {
if let Ok(filesystem) = client
.object(cleartext.object_path.clone())?
.filesystem()
.await
{
if !filesystem.mount_points().await?.is_empty() {
filesystem.unmount(HashMap::new()).await?;
}
}
client
.object(block.object_path.clone())?
.encrypted()
.await?
.lock(HashMap::new())
.await?;
return Ok(());
}

client
.object(block.object_path.clone())?
.filesystem()
Expand All @@ -176,3 +270,50 @@ pub async fn eject(block: &Block) -> udisks2::Result<()> {

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

fn luks_block(encryption: Encryption) -> Block {
Block {
object_path: "/org/freedesktop/UDisks2/block_devices/sdc2".to_owned(),
drive_path: "/org/freedesktop/UDisks2/drives/disk".to_owned(),
dev: "/dev/sdc2".to_owned(),
label: String::new(),
mount: None,
fstype: "crypto_LUKS".to_owned(),
mounted: false,
size: String::new(),
encryption,
}
}

fn unlocked() -> Encryption {
Encryption::Unlocked(Cleartext {
object_path: "/org/freedesktop/UDisks2/block_devices/dm_2d0".to_owned(),
devices: vec!["/dev/dm-0".to_owned(), "/dev/mapper/luks-1234".to_owned()],
})
}

// An unlocked LUKS partition shows as one row. /proc/self/mounts names its
// filesystem /dev/mapper/<name> (mount tools) or /dev/dm-N, never the LUKS
// partition, so the row must match both names to show its mount point.
#[test]
fn unlocked_row_matches_cleartext_device_names() {
let block = luks_block(unlocked());
assert!(block.has_device("/dev/mapper/luks-1234"));
assert!(block.has_device("/dev/dm-0"));
assert!(block.has_device("/dev/sdc2"));
assert!(!block.has_device("/dev/mapper/other"));
}

// A locked row must not take the mount of a cleartext device: the row of
// a locked partition always shows "not mounted".
#[test]
fn locked_row_matches_only_its_own_device() {
let block = luks_block(Encryption::Locked);
assert!(block.has_device("/dev/sdc2"));
assert!(!block.has_device("/dev/mapper/luks-1234"));
}
}
Loading