Skip to content

Commit

Permalink
server: Load and dispatch login/default collection
Browse files Browse the repository at this point in the history
This change loads and dispatches the login/default collection/keyring into
the object tree.

This change also adds,
--login command line option to oo7-daemon.
-l, --login option will read a password from stdin, and use it to unlock
the login keyring.
Note: currently -l option will only load the login keyring and not perform
anything related to unlocking the keyring.

Signed-off-by: Dhanuka Warusadura <dhanuka@gnome.org>
  • Loading branch information
warusadura committed Oct 24, 2024
1 parent 0890ef6 commit 914eb84
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ rust-version.workspace = true
version.workspace = true

[dependencies]
clap.workspace = true
oo7 = { workspace = true, features = ["unstable"] }
rpassword = "7.3"
serde.workspace = true
tokio = { workspace = true, features = ["full"] }
tracing = "0.1"
Expand Down
12 changes: 12 additions & 0 deletions server/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ pub enum Error {
Portal(oo7::portal::Error),
// Zbus error
Zbus(zbus::Error),
// IO error
IO(std::io::Error),
// Empty password error
EmptyPassword,
}

impl From<zbus::Error> for Error {
Expand All @@ -20,11 +24,19 @@ impl From<oo7::portal::Error> for Error {
}
}

impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Self::IO(err)
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Portal(err) => write!(f, "Portal error {err}"),
Self::Zbus(err) => write!(f, "Zbus error {err}"),
Self::IO(err) => write!(f, "IO error {err}"),
Self::EmptyPassword => write!(f, "Login password can't be empty"),
}
}
}
28 changes: 27 additions & 1 deletion server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,44 @@ mod service;
mod service_manager;
mod session;

use clap::Parser;
use oo7::portal::Secret;
use service::Service;

use crate::error::Error;

const BINARY_NAME: &str = env!("CARGO_BIN_NAME");

#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(
short = 'l',
long,
default_value_t = false,
help = "Read a password from stdin, and use it to unlock the login keyring."
)]
login: bool,
}

#[tokio::main]
async fn main() -> Result<(), Error> {
tracing_subscriber::fmt::init();
let args = Args::parse();
let mut secret: Option<Secret> = None;

if args.login {
let password = rpassword::prompt_password("Enter the login password: ")?;
if password.is_empty() {
tracing::error!("Login password can't be empty.");
return Err(Error::EmptyPassword);
}
secret = Some(Secret::from(password.into_bytes()));
}

tracing::info!("Starting {}", BINARY_NAME);

Service::run().await?;
Service::run(secret).await?;

std::future::pending::<()>().await;

Expand Down
50 changes: 31 additions & 19 deletions server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ use crate::{
collection::Collection, error::Error, service_manager::ServiceManager, session::Session,
};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Service {
// Properties
collections: Mutex<Vec<OwnedObjectPath>>,
collections: Arc<Mutex<Vec<OwnedObjectPath>>>,
// Other attributes
manager: Arc<Mutex<ServiceManager>>,
#[allow(unused)]
Expand Down Expand Up @@ -128,7 +128,7 @@ impl Service {
}

impl Service {
pub async fn run() -> Result<(), Error> {
pub async fn run(secret: Option<Secret>) -> Result<(), Error> {
let connection = zbus::connection::Builder::session()?
.name(oo7::dbus::api::Service::DESTINATION.as_deref().unwrap())?
.build()
Expand All @@ -140,29 +140,41 @@ impl Service {
connection: connection.clone(),
};

// Load session collection
let session = service.fetch_session().await?;

// Dispatch Service
object_server
.at(oo7::dbus::api::Service::PATH.as_deref().unwrap(), service)
.at(
oo7::dbus::api::Service::PATH.as_deref().unwrap(),
service.clone(),
)
.await?;
// Dispatch session collection
object_server.at(session.path().clone(), session).await?;

Ok(())
}
let mut collections = service.collections.lock().await;

if let Some(secret) = secret {
let collection = Collection::new(
"login",
"default",
Arc::clone(&service.manager),
Arc::new(Keyring::open("login", secret).await?),
);
collections.push(collection.path().clone());
object_server
.at(collection.path().clone(), collection)
.await?;
}

pub async fn fetch_session(&self) -> Result<Collection, Error> {
let secret = Secret::random();
let session = Collection::new(
let collection = Collection::new(
"session",
"session",
Arc::clone(&self.manager),
Arc::new(Keyring::temporary(secret).await?),
Arc::clone(&service.manager),
Arc::new(Keyring::temporary(Secret::random()).await?),
);
self.collections.lock().await.push(session.path().clone());
collections.push(collection.path().clone());
object_server
.at(collection.path().clone(), collection)
.await?;

Ok(session)
drop(collections);

Ok(())
}
}

0 comments on commit 914eb84

Please sign in to comment.