> = LazyLock::new(|| {
- [
- // pam ssh agent auth
- "SSH_AUTH_SOCK",
- // ssh itself provides this when running PAM
- "SSH_AUTH_INFO_0",
- // contains user which ran sudo
- "SUDO_USER",
- ]
- .into_iter()
- .collect()
-});
-
-struct Conversation(P);
-impl Conversation {
- fn prompt_inner(&self, echo: bool, prompt: &CStr) -> Result {
- trace!("do prompt");
- let out = self
- .0
- .prompt_text(echo, &prompt.to_string_lossy(), "PAM prompt request", &[])
- .map_err(|e| {
- trace!("prompt error: {e}");
- ErrorCode::CONV_ERR
- })?;
- CString::new(out).map_err(|_| ErrorCode::CONV_AGAIN)
- }
- fn text_inner(&self, error: bool, msg: &CStr) {
- trace!("do text");
- let msg = msg.to_string_lossy();
- let _ = self.0.display_text(error, &msg, &[]);
- }
-}
-impl ConversationHandler for Conversation {
- fn prompt_echo_on(&mut self, prompt: &CStr) -> Result {
- self.prompt_inner(true, prompt)
- }
-
- fn prompt_echo_off(&mut self, prompt: &CStr) -> Result {
- self.prompt_inner(false, prompt)
- }
-
- fn text_info(&mut self, msg: &CStr) {
- self.text_inner(false, msg)
- }
-
- fn error_msg(&mut self, msg: &CStr) {
- self.text_inner(true, msg)
- }
-
- fn radio_prompt(&mut self, prompt: &CStr) -> Result {
- let prompt = prompt.to_string_lossy();
- let result = self
- .0
- .prompt_radio(&prompt, "PAM prompt request", &[])
- .map_err(|_| ErrorCode::CONV_ERR)?;
- Ok(result)
- }
-}
-
-#[proxy(
- default_service = "org.freedesktop.DBus",
- default_path = "/org/freedesktop/DBus"
-)]
-trait DBus {
- fn get_connection_credentials(&self, body: &str) -> zbus::Result>;
-}
-
-#[interface(name = "lach.PolkitHelper")]
-impl Helper {
- async fn init_conversation(
- &self,
- request: BackendRequest,
- #[zbus(header)] hdr: Header<'_>,
- ) -> fdo::Result<()> {
- let Some(sender) = hdr.sender().map(|v| v.to_owned()) else {
- trace!("missing sender");
- return Err(fdo::Error::AuthFailed("missing sender".to_owned()));
- };
-
- let dbus = DBusProxy::new(&self.connection).await?;
-
- // TOCTOU: sender might be already disconnected, and there might be another
- // user with different user id here, but does it matters?
- let reply = dbus.get_connection_credentials(&sender).await?;
- let connection_uid: u32 = (&reply["UnixUserID"]).try_into().unwrap();
-
- let identity = request.identity.clone();
- let blocking_connection = self.blocking_connection.clone();
- let thread_result: fdo::Result<()> = block_in_place(move || {
- trace!("find user");
- let Some(identity_uid) = identity.uid() else {
- return Err(fdo::Error::AuthFailed("can't process identity".to_owned()));
- };
- let user = User::from_uid(identity_uid)
- .map_err(|_| fdo::Error::AuthFailed("error querying user".to_owned()))?
- .ok_or_else(|| fdo::Error::AuthFailed("uid not found".to_owned()))?;
-
- let responder = DbusPrompterProxyBlocking::new(
- &blocking_connection,
- sender,
- request.prompter_path,
- )?;
- let conversation = Conversation(responder);
- trace!("run context for {}", &user.name);
- let mut ctx = Context::new(
- // TODO: Should another scope be used?
- "login",
- Some(&user.name),
- conversation,
- )
- .map_err(|_| fdo::Error::Failed("pam context init failed".to_owned()))?;
-
- trace!("fill env");
- for (k, v) in request.environment {
- if k.contains('=') || !ALLOWED_ENVIRONMENT.contains(k.as_str()) {
- continue;
- }
- let _ = ctx.putenv(format!("{k}={v}"));
- }
-
- trace!("authenticate");
- ctx.authenticate(Flag::NONE)
- .map_err(|_| fdo::Error::AuthFailed("pam authentication failed".to_owned()))?;
-
- trace!("acct mgmt");
- ctx.acct_mgmt(Flag::NONE)
- .map_err(|_| fdo::Error::AuthFailed("pam acct mgmt failed".to_owned()))?;
-
- Ok(())
- });
-
- thread_result?;
-
- trace!("respond");
- let proxy = zbus_polkit::policykit1::AuthorityProxy::new(&self.connection).await?;
-
- let identity_details = request
- .identity
- .details
- .iter()
- .map(|(k, v)| (k.as_str(), (**v).try_clone().expect("success")))
- .collect::>();
- proxy
- .authentication_agent_response2(
- connection_uid,
- &request.cookie,
- &zbus_polkit::policykit1::Identity {
- identity_kind: &request.identity.kind,
- identity_details: &identity_details,
- },
- )
- .await?;
- Ok(())
- }
-}
-
-const OBJ_PATH: &str = "/lach/PolkitHelper";
-
-#[derive(Parser)]
-struct Opts {
- /// Not recommended: start as a session connection, then use escalation
- /// to respond to polkit requests.
- #[arg(long)]
- session: bool,
-}
-
-#[tokio::main]
-async fn main() -> anyhow::Result<()> {
- tracing_subscriber::fmt::init();
- let opts = Opts::parse();
- let connection = if opts.session {
- Connection::session().await
- } else {
- Connection::system().await
- }
- .context("failed to open connection")?;
-
- let session = opts.session;
- let blocking_connection: anyhow::Result = spawn_blocking(move || {
- Ok(if session {
- blocking::Connection::session()?
- } else {
- blocking::Connection::system()?
- })
- })
- .await?;
- let blocking_connection = blocking_connection.context("failed to open blocking connection")?;
-
- if opts.session {
- setuid(Uid::from_raw(0))
- .context("polkit-backend needs to be suid if run in session mode")?;
- }
-
- connection
- .object_server()
- .at(
- OBJ_PATH,
- Helper {
- connection: connection.clone(),
- blocking_connection,
- },
- )
- .await
- .context("failed listen path")?;
-
- connection
- .request_name("lach.polkit.helper1")
- .await
- .context("failed to request name")?;
-
- pending().await
-}
--- a/remowt/cmds/remowt-agent/src/helper/dbus.rs
+++ /dev/null
@@ -1,81 +0,0 @@
-use std::collections::HashMap;
-use std::marker::PhantomData;
-
-use remowt_polkit_shared::{BackendRequest, Identity};
-use remowt_ui_prompt::dbus::DbusPrompterInterface;
-use remowt_ui_prompt::Prompter;
-use zbus::Connection;
-
-use crate::PolkitHelperProxy;
-
-use super::Helper;
-
-struct TemporaryPrompterInterface {
- connection: Connection,
- path: String,
- _marker: PhantomData,
-}
-impl TemporaryPrompterInterface {
- async fn new(connection: Connection, prompter: P) -> Self {
- let path = format!(
- "/remowt/prompters/{}",
- uuid::Uuid::new_v4().to_string().replace("-", "_")
- );
- let _ = connection
- .object_server()
- .at(path.clone(), DbusPrompterInterface(prompter))
- .await;
- Self {
- connection,
- path,
- _marker: PhantomData,
- }
- }
-}
-impl Drop for TemporaryPrompterInterface {
- fn drop(&mut self) {
- // Removal is async because of async RwLock used inside...
- // We should not care about its reuse
- let connection = self.connection.clone();
- let path = std::mem::take(&mut self.path);
- tokio::spawn(async move {
- let _ = connection
- .object_server()
- .remove::, String>(path)
- .await;
- });
- }
-}
-
-#[derive(Clone)]
-pub struct DbusHelper {
- connection: Connection,
- helper: PolkitHelperProxy<'static>,
-}
-impl DbusHelper {
- pub async fn new(connection: Connection) -> zbus::Result {
- let helper = PolkitHelperProxy::new(&connection).await?;
- Ok(Self { connection, helper })
- }
-}
-impl Helper for DbusHelper {
- async fn help_me(
- &self,
- cookie: &str,
- prompter: P,
- identity: Identity,
- ) -> anyhow::Result<()> {
- let prompter = TemporaryPrompterInterface::new(self.connection.clone(), prompter).await;
- self.helper
- .init_conversation(
- BackendRequest {
- cookie: cookie.to_owned(),
- environment: HashMap::new(),
- prompter_path: prompter.path.clone(),
- identity,
- }, // cookie.to_owned(), HashMap::new(), prompter.path.clone()
- )
- .await?;
- Ok(())
- }
-}
--- a/remowt/cmds/remowt-agent/src/helper/mod.rs
+++ b/remowt/cmds/remowt-agent/src/helper/mod.rs
@@ -2,12 +2,10 @@
use remowt_polkit_shared::Identity;
use remowt_ui_prompt::Prompter;
-mod dbus;
mod protocol;
mod socket;
mod suid;
-pub use dbus::DbusHelper;
pub use socket::SocketHelper;
pub use suid::SuidHelper;
--- a/remowt/cmds/remowt-agent/src/main.rs
+++ b/remowt/cmds/remowt-agent/src/main.rs
@@ -18,7 +18,7 @@
};
use remowt_link_shared::iroh_tunnel::TunnelDialer;
use remowt_link_shared::{editor::EditorEndpointsClient, Address, BifConfig};
-use remowt_polkit_shared::{emphasize, BackendRequest, Identity, PidDisplay};
+use remowt_polkit_shared::{emphasize, Identity, PidDisplay};
use remowt_ui_prompt::bifrost::PromptEndpointsClient;
use remowt_ui_prompt::rofi::RofiPrompter;
use remowt_ui_prompt::{PrependSourcePrompter, Prompter, Source};
@@ -29,7 +29,7 @@
use tracing::{debug, trace};
use zbus::fdo;
use zbus::zvariant::{OwnedValue, Str};
-use zbus::{interface, proxy, Connection};
+use zbus::{interface, Connection};
use zbus_polkit::policykit1::Subject;
use self::helper::{Helper, SocketHelper, SuidHelper};
@@ -199,15 +199,6 @@
}
const OBJ_PATH: &str = "/org/freedesktop/PolicyKit1/AuthenticationAgent";
-
-#[proxy(
- interface = "lach.PolkitHelper",
- default_service = "lach.polkit.helper1",
- default_path = "/lach/PolkitHelper"
-)]
-trait PolkitHelper {
- fn init_conversation(&self, request: BackendRequest) -> zbus::Result<()>;
-}
#[derive(Parser)]
enum Opts {
--- a/remowt/crates/polkit-shared/src/lib.rs
+++ b/remowt/crates/polkit-shared/src/lib.rs
@@ -103,11 +103,3 @@
}
}
}
-
-#[derive(Serialize, Deserialize, Type, PartialEq, Debug)]
-pub struct BackendRequest {
- pub cookie: String,
- pub environment: HashMap,
- pub prompter_path: String,
- pub identity: Identity,
-}
--- a/remowt/crates/remowt-link-shared/Cargo.toml
+++ b/remowt/crates/remowt-link-shared/Cargo.toml
@@ -9,7 +9,7 @@
anyhow.workspace = true
bifrostlink.workspace = true
bytes.workspace = true
-camino = { workspace = true }
+camino = { workspace = true, features = ["serde1"] }
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
thiserror.workspace = true
--- /dev/null
+++ b/remowt/crates/remowt-link-shared/src/gateway.rs
@@ -0,0 +1,100 @@
+use std::path::{Path, PathBuf};
+
+use anyhow::{anyhow, Context as _};
+use bifrostlink::{Rpc, Rtt};
+use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
+use tokio::net::{UnixListener, UnixStream};
+use tracing::{debug, warn};
+use uuid::Uuid;
+
+use crate::port::child_port;
+use crate::{Address, BifConfig};
+
+pub const SOCKET_ENV: &str = "REMOWT_AGENT_SOCKET";
+
+pub const SOCKET_NAME: &str = "agent.sock";
+
+pub fn local_socket() -> anyhow::Result {
+ let dir = std::env::var_os("XDG_RUNTIME_DIR").context("XDG_RUNTIME_DIR not set")?;
+ Ok(PathBuf::from(dir).join("remowt-local"))
+}
+
+pub fn socket_path() -> anyhow::Result {
+ match std::env::var_os(SOCKET_ENV) {
+ Some(p) => Ok(PathBuf::from(p)),
+ None => local_socket(),
+ }
+}
+
+fn peer_tag(addr: &Address) -> u8 {
+ match addr {
+ Address::User => 0,
+ Address::Agent => 1,
+ Address::AgentPrivileged => 2,
+ _ => unreachable!(),
+ }
+}
+fn untag_peer(tag: u8) -> anyhow::Result {
+ Ok(match tag {
+ 0 => Address::User,
+ 1 => Address::Agent,
+ 2 => Address::AgentPrivileged,
+ _ => unreachable!(),
+ })
+}
+
+pub async fn serve(rpc: Rpc, path: &Path) -> anyhow::Result<()> {
+ let _ = tokio::fs::remove_file(path).await;
+ let listener = UnixListener::bind(path)
+ .with_context(|| format!("binding agent gateway at {}", path.display()))?;
+ let tag = peer_tag(&rpc.me());
+ tokio::spawn(async move {
+ loop {
+ let mut stream = match listener.accept().await {
+ Ok((stream, _)) => stream,
+ Err(e) => {
+ warn!("gateway accept failed: {e}");
+ continue;
+ }
+ };
+ let id = Uuid::new_v4().as_u128();
+ let mut hello = [0u8; 17];
+ hello[0] = tag;
+ hello[1..].copy_from_slice(&id.to_be_bytes());
+ if let Err(e) = stream.write_all(&hello).await {
+ warn!("gateway handshake failed: {e}");
+ continue;
+ }
+ debug!("gateway client {id:032x}");
+ let (rx, tx) = stream.into_split();
+ rpc.add_direct(
+ Address::Ephemeral(Uuid::from_u128(id)),
+ child_port(rx, tx),
+ Rtt(0),
+ );
+ }
+ });
+ Ok(())
+}
+
+pub async fn connect(path: &Path) -> anyhow::Result> {
+ let mut stream = UnixStream::connect(path)
+ .await
+ .with_context(|| format!("connecting to agent gateway at {}", path.display()))?;
+
+ let mut hello = [0u8; 17];
+ stream
+ .read_exact(&mut hello)
+ .await
+ .context("reading gateway handshake")?;
+ let peer = untag_peer(hello[0])?;
+ let id = u128::from_be_bytes(hello[1..].try_into().expect("16 bytes"));
+
+ let (rx, tx) = stream.into_split();
+ let rpc = Rpc::::new(Address::Ephemeral(Uuid::from_u128(id)));
+ rpc.add_direct(peer, child_port(rx, tx), Rtt(0));
+ rpc.wait_for_connection_to(Address::User)
+ .await
+ .map_err(|_| anyhow!("no route to the User through the agent"))?;
+ Ok(rpc)
+}
--- a/remowt/crates/remowt-ui-prompt/Cargo.toml
+++ b/remowt/crates/remowt-ui-prompt/Cargo.toml
@@ -2,19 +2,15 @@
name = "remowt-ui-prompt"
description = "Interactive UI prompt endpoint for remowt (D-Bus)"
version.workspace = true
-edition = "2021"
+edition.workspace = true
license.workspace = true
[dependencies]
anyhow.workspace = true
bifrostlink.workspace = true
bifrostlink-macros.workspace = true
+remowt-link-shared.workspace = true
serde.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = ["io-util", "macros", "process", "rt"] }
tracing.workspace = true
-zbus = { workspace = true, optional = true }
-
-[features]
-default = ["dbus"]
-dbus = ["dep:zbus"]
--- a/remowt/crates/remowt-ui-prompt/src/auto.rs
+++ b/remowt/crates/remowt-ui-prompt/src/auto.rs
@@ -1,45 +1,41 @@
-use anyhow::bail;
+use std::path::Path;
+
+use bifrostlink::declarative::RemoteEndpoints as _;
+use remowt_link_shared::{Address, BifConfig, gateway};
use tracing::debug;
-use zbus::fdo::DBusProxy;
-use zbus::names::BusName;
-use crate::dbus::{DbusPrompterProxy, BUS_NAME, PROMPTER_PATH};
+use crate::bifrost::PromptEndpointsClient;
use crate::rofi::RofiPrompter;
use crate::{Prompter, Result, Source};
pub struct AutoPrompter {
- dbus: Option>,
+ remote: Option>,
fallback: RofiPrompter,
}
impl AutoPrompter {
pub async fn new() -> Self {
- let dbus = match Self::try_dbus().await {
- Ok(p) => Some(p),
+ let remote = match gateway::local_socket() {
+ Ok(path) => Self::try_connect(&path).await,
Err(e) => {
- debug!("dbus prompter unavailable, falling back to rofi: {e}");
+ debug!("no local gateway socket, falling back to rofi: {e}");
None
}
};
Self {
- dbus,
+ remote,
fallback: RofiPrompter,
}
}
- async fn try_dbus() -> anyhow::Result> {
- let conn = zbus::Connection::session().await?;
- let dbus = DBusProxy::new(&conn).await?;
- let name = BusName::try_from(BUS_NAME)?;
- if !dbus.name_has_owner(name).await? {
- bail!("{BUS_NAME} not registered on session bus");
+ async fn try_connect(path: &Path) -> Option> {
+ match gateway::connect(path).await {
+ Ok(rpc) => Some(PromptEndpointsClient::wrap(rpc.remote(Address::User))),
+ Err(e) => {
+ debug!("local prompt agent unavailable, falling back to rofi: {e}");
+ None
+ }
}
- let proxy = DbusPrompterProxy::builder(&conn)
- .destination(BUS_NAME)?
- .path(PROMPTER_PATH)?
- .build()
- .await?;
- Ok(proxy)
}
}
@@ -51,8 +47,8 @@
variants: &[&str],
source: &[Source],
) -> Result {
- if let Some(dbus) = &self.dbus {
- return Prompter::prompt_enum(dbus, prompt, description, variants, source).await;
+ if let Some(remote) = &self.remote {
+ return Prompter::prompt_enum(remote, prompt, description, variants, source).await;
}
self.fallback
.prompt_enum(prompt, description, variants, source)
@@ -66,8 +62,8 @@
description: &str,
source: &[Source],
) -> Result {
- if let Some(dbus) = &self.dbus {
- return Prompter::prompt_text(dbus, echo, prompt, description, source).await;
+ if let Some(remote) = &self.remote {
+ return Prompter::prompt_text(remote, echo, prompt, description, source).await;
}
self.fallback
.prompt_text(echo, prompt, description, source)
@@ -75,8 +71,8 @@
}
async fn display_text(&self, error: bool, description: &str, source: &[Source]) -> Result<()> {
- if let Some(dbus) = &self.dbus {
- return Prompter::display_text(dbus, error, description, source).await;
+ if let Some(remote) = &self.remote {
+ return Prompter::display_text(remote, error, description, source).await;
}
self.fallback.display_text(error, description, source).await
}
--- a/remowt/crates/remowt-ui-prompt/src/dbus.rs
+++ /dev/null
@@ -1,143 +0,0 @@
-use zbus::interface;
-use zbus::{fdo, proxy};
-
-use crate::Source;
-use crate::{BlockingPrompter, Result};
-use crate::{Error, Prompter};
-
-pub const BUS_NAME: &str = "lach.RemowtAskpass";
-pub const PROMPTER_PATH: &str = "/lach/Askpass";
-
-pub struct DbusPrompterInterface(pub P);
-
-#[interface(name = "lach.PolkitInputHandler")]
-impl DbusPrompterInterface {
- async fn prompt_enum(
- &self,
- prompt: &str,
- description: &str,
- variants: Vec,
- source: Vec,
- ) -> fdo::Result {
- let variants: Vec<&str> = variants.iter().map(|v| v.as_str()).collect();
- Ok(self
- .0
- .prompt_enum(prompt, description, &variants, &source)
- .await?)
- }
- async fn prompt_text(
- &self,
- echo: bool,
- prompt: &str,
- description: &str,
- source: Vec,
- ) -> fdo::Result {
- Ok(self
- .0
- .prompt_text(echo, prompt, description, &source)
- .await?)
- }
- async fn display_text(
- &self,
- error: bool,
- description: &str,
- source: Vec,
- ) -> fdo::Result<()> {
- Ok(self.0.display_text(error, description, &source).await?)
- }
-}
-
-#[proxy(interface = "lach.PolkitInputHandler")]
-pub trait DbusPrompter {
- async fn prompt_enum(
- &self,
- prompt: &str,
- description: &str,
- variants: &[&str],
- source: &[Source],
- ) -> fdo::Result;
- async fn prompt_text(
- &self,
- echo: bool,
- prompt: &str,
- description: &str,
- source: &[Source],
- ) -> fdo::Result;
- async fn display_text(
- &self,
- error: bool,
- description: &str,
- source: &[Source],
- ) -> fdo::Result<()>;
-}
-
-impl Prompter for DbusPrompterProxy<'_> {
- async fn prompt_enum(
- &self,
- prompt: &str,
- description: &str,
- variants: &[&str],
- source: &[Source],
- ) -> Result {
- Ok(self
- .prompt_enum(prompt, description, variants, source)
- .await?)
- }
-
- async fn prompt_text(
- &self,
- echo: bool,
- prompt: &str,
- description: &str,
- source: &[Source],
- ) -> Result {
- Ok(self.prompt_text(echo, prompt, description, source).await?)
- }
-
- async fn display_text(&self, error: bool, description: &str, source: &[Source]) -> Result<()> {
- Ok(self.display_text(error, description, source).await?)
- }
-}
-impl BlockingPrompter for DbusPrompterProxyBlocking<'_> {
- fn prompt_enum(
- &self,
- prompt: &str,
- description: &str,
- variants: &[&str],
- source: &[Source],
- ) -> Result {
- Ok(self.prompt_enum(prompt, description, variants, source)?)
- }
-
- fn prompt_text(
- &self,
- echo: bool,
- prompt: &str,
- description: &str,
- source: &[Source],
- ) -> Result {
- Ok(self.prompt_text(echo, prompt, description, source)?)
- }
-
- fn display_text(&self, error: bool, description: &str, source: &[Source]) -> Result<()> {
- Ok(self.display_text(error, description, source)?)
- }
-}
-
-impl From for Error {
- fn from(value: fdo::Error) -> Self {
- if matches!(value, fdo::Error::NoReply(_)) {
- return Self::Cancel;
- }
- Self::InputError(format!("{value}"))
- }
-}
-impl From for fdo::Error {
- fn from(value: Error) -> Self {
- match value {
- Error::Cancel => fdo::Error::NoReply("input was cancelled".to_owned()),
- Error::Remote(e) => fdo::Error::NoReply(format!("remote error occured: {e}")),
- Error::InputError(e) => fdo::Error::Failed(e),
- }
- }
-}
--- a/remowt/crates/remowt-ui-prompt/src/lib.rs
+++ b/remowt/crates/remowt-ui-prompt/src/lib.rs
@@ -5,7 +5,6 @@
pub mod auto;
pub mod bifrost;
-pub mod dbus;
pub mod rofi;
#[derive(thiserror::Error, Debug, serde::Serialize, serde::Deserialize)]
@@ -20,7 +19,6 @@
pub type Result = result::Result;
-#[cfg_attr(feature = "dbus", derive(zbus::zvariant::Type))]
#[derive(serde::Serialize, serde::Deserialize, Clone)]
pub struct Source(pub Cow<'static, str>);
impl fmt::Display for Source {