|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// SPDX-FileCopyrightText: 2025 Harald Sitter <sitter@kde.org> |
| 3 | + |
| 4 | +use oo7::{Secret, dbus::ServiceError}; |
| 5 | + |
| 6 | +use serde::Serialize; |
| 7 | +use zbus::{ |
| 8 | + object_server::SignalEmitter, |
| 9 | + zvariant::{self, ObjectPath, OwnedFd, OwnedObjectPath, Type}, |
| 10 | +}; |
| 11 | + |
| 12 | +use crate::{ |
| 13 | + prompt::{Prompt, PromptRole}, |
| 14 | + service::Service, |
| 15 | +}; |
| 16 | +use tokio::{io::AsyncReadExt, sync::OnceCell}; |
| 17 | + |
| 18 | +use std::{env, os::fd::AsFd}; |
| 19 | + |
| 20 | +#[repr(i32)] |
| 21 | +#[derive(Debug, Type, Serialize)] |
| 22 | +pub enum CallbackAction { |
| 23 | + Dismiss = 0, |
| 24 | + Keep = 1, |
| 25 | +} |
| 26 | + |
| 27 | +#[must_use] |
| 28 | +pub async fn in_plasma_environment(connection: &zbus::Connection) -> bool { |
| 29 | + // This isn't going to change, cache it. |
| 30 | + static VALUE: OnceCell<bool> = OnceCell::const_new(); |
| 31 | + *VALUE.get_or_init(|| async { |
| 32 | + match env::var("XDG_CURRENT_DESKTOP") |
| 33 | + .map(|v| v.to_lowercase() == "kde") { |
| 34 | + Ok(_) => (), |
| 35 | + Err(_) => return false, |
| 36 | + }; |
| 37 | + |
| 38 | + let proxy = match zbus::fdo::DBusProxy::new(connection).await { |
| 39 | + Ok(proxy) => proxy, |
| 40 | + Err(_) => return false, |
| 41 | + }; |
| 42 | + let activatable_names = match proxy.list_activatable_names().await { |
| 43 | + Ok(names) => names, |
| 44 | + Err(_) => return false, |
| 45 | + }; |
| 46 | + return activatable_names |
| 47 | + .iter() |
| 48 | + .any(|name| name.as_str() == "org.kde.secretprompter"); |
| 49 | + }).await |
| 50 | +} |
| 51 | + |
| 52 | +#[zbus::proxy( |
| 53 | + default_service = "org.kde.secretprompter", |
| 54 | + interface = "org.kde.secretprompter", |
| 55 | + default_path = "/SecretPrompter", |
| 56 | + gen_blocking = false |
| 57 | +)] |
| 58 | +pub trait PlasmaPrompter { |
| 59 | + fn unlock_collection_prompt( |
| 60 | + &self, |
| 61 | + request: &ObjectPath<'_>, |
| 62 | + window_id: &str, |
| 63 | + activation_token: &str, |
| 64 | + collection_name: &str, |
| 65 | + ) -> Result<(), ServiceError>; |
| 66 | + fn create_collection_prompt( |
| 67 | + &self, |
| 68 | + request: &ObjectPath<'_>, |
| 69 | + window_id: &str, |
| 70 | + activation_token: &str, |
| 71 | + collection_name: &str, |
| 72 | + ) -> Result<(), ServiceError>; |
| 73 | +} |
| 74 | + |
| 75 | +#[derive(Debug, Clone)] |
| 76 | +pub struct PlasmaPrompterCallback { |
| 77 | + service: Service, |
| 78 | + prompt_path: OwnedObjectPath, |
| 79 | + path: OwnedObjectPath, |
| 80 | +} |
| 81 | + |
| 82 | +#[zbus::interface(name = "org.kde.secretprompter.request")] |
| 83 | +impl PlasmaPrompterCallback { |
| 84 | + pub async fn accepted(&self, result_fd: OwnedFd) -> Result<CallbackAction, ServiceError> { |
| 85 | + let prompt_path = &self.prompt_path; |
| 86 | + let Some(prompt) = self.service.prompt(prompt_path).await else { |
| 87 | + return Err(ServiceError::NoSuchObject(format!( |
| 88 | + "Prompt '{prompt_path}' does not exist." |
| 89 | + ))); |
| 90 | + }; |
| 91 | + |
| 92 | + tracing::debug!("User accepted the prompt."); |
| 93 | + |
| 94 | + let secret = { |
| 95 | + let borrowed_fd = result_fd.as_fd(); |
| 96 | + let std_stream = std::os::unix::net::UnixStream::from( |
| 97 | + borrowed_fd |
| 98 | + .try_clone_to_owned() |
| 99 | + .expect("Failed to clone fd"), |
| 100 | + ); |
| 101 | + let mut stream = tokio::net::UnixStream::from_std(std_stream).unwrap(); |
| 102 | + let mut buffer = String::new(); |
| 103 | + stream |
| 104 | + .read_to_string(&mut buffer) |
| 105 | + .await |
| 106 | + .expect("error reading secret"); |
| 107 | + oo7::Secret::from(buffer) |
| 108 | + }; |
| 109 | + |
| 110 | + self.on_reply(&prompt, secret).await |
| 111 | + } |
| 112 | + |
| 113 | + pub async fn rejected(&self) -> Result<CallbackAction, ServiceError> { |
| 114 | + tracing::debug!("User rejected the prompt."); |
| 115 | + self.prompter_dismissed(self.prompt_path.clone()).await?; |
| 116 | + Ok(CallbackAction::Dismiss) // simply dismiss without further action |
| 117 | + } |
| 118 | + |
| 119 | + pub async fn dismissed(&self) -> Result<(), ServiceError> { |
| 120 | + // This is only does check if the prompt is tracked on Service |
| 121 | + let path = &self.prompt_path; |
| 122 | + if let Some(_prompt) = self.service.prompt(path).await { |
| 123 | + self.service |
| 124 | + .object_server() |
| 125 | + .remove::<Prompt, _>(path) |
| 126 | + .await?; |
| 127 | + self.service.remove_prompt(path).await; |
| 128 | + } |
| 129 | + self.service |
| 130 | + .object_server() |
| 131 | + .remove::<Self, _>(&self.path) |
| 132 | + .await?; |
| 133 | + |
| 134 | + Ok(()) |
| 135 | + } |
| 136 | + |
| 137 | + #[zbus(signal)] |
| 138 | + pub async fn retry(signal_emitter: &SignalEmitter<'_>, reason: &str) -> zbus::Result<()>; |
| 139 | + |
| 140 | + #[zbus(signal)] |
| 141 | + pub async fn dismiss(signal_emitter: &SignalEmitter<'_>) -> zbus::Result<()>; |
| 142 | +} |
| 143 | + |
| 144 | +impl PlasmaPrompterCallback { |
| 145 | + pub async fn new( |
| 146 | + service: Service, |
| 147 | + prompt_path: OwnedObjectPath, |
| 148 | + ) -> Result<Self, oo7::crypto::Error> { |
| 149 | + let index = service.prompt_index().await; |
| 150 | + Ok(Self { |
| 151 | + path: OwnedObjectPath::try_from(format!("/org/plasma/keyring/Prompt/p{index}")) |
| 152 | + .unwrap(), |
| 153 | + service, |
| 154 | + prompt_path, |
| 155 | + }) |
| 156 | + } |
| 157 | + |
| 158 | + pub fn path(&self) -> &ObjectPath<'_> { |
| 159 | + &self.path |
| 160 | + } |
| 161 | + |
| 162 | + async fn on_reply( |
| 163 | + &self, |
| 164 | + prompt: &Prompt, |
| 165 | + secret: Secret, |
| 166 | + ) -> Result<CallbackAction, ServiceError> { |
| 167 | + // Handle each role differently based on what validation/preparation is needed |
| 168 | + match prompt.role() { |
| 169 | + PromptRole::Unlock => { |
| 170 | + if prompt.on_unlock_collection(secret).await? { |
| 171 | + Ok(CallbackAction::Dismiss) |
| 172 | + } else { |
| 173 | + let emitter = SignalEmitter::from_parts( |
| 174 | + self.service.connection().clone(), |
| 175 | + self.path().clone(), |
| 176 | + ); |
| 177 | + PlasmaPrompterCallback::retry(&emitter, "The unlock password was incorrect") |
| 178 | + .await?; |
| 179 | + |
| 180 | + Ok(CallbackAction::Keep) // we retry |
| 181 | + } |
| 182 | + } |
| 183 | + PromptRole::CreateCollection => { |
| 184 | + prompt.on_create_collection(secret).await?; |
| 185 | + Ok(CallbackAction::Dismiss) |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + async fn prompter_dismissed(&self, prompt_path: OwnedObjectPath) -> Result<(), ServiceError> { |
| 191 | + let signal_emitter = self.service.signal_emitter(prompt_path)?; |
| 192 | + let result = zvariant::Value::new::<Vec<OwnedObjectPath>>(vec![]) |
| 193 | + .try_into_owned() |
| 194 | + .unwrap(); |
| 195 | + |
| 196 | + tokio::spawn(async move { Prompt::completed(&signal_emitter, true, result).await }); |
| 197 | + Ok(()) |
| 198 | + } |
| 199 | +} |
0 commit comments