cosmic-greeter/daemon/src/main.rs

114 lines
3.6 KiB
Rust
Raw Normal View History

use cosmic_greeter_daemon::UserData;
use std::{env, error::Error, future::pending, io, path::Path};
2025-06-26 15:14:43 -04:00
use zbus::{connection::Builder, DBusError};
2024-02-06 15:03:07 -07:00
//IMPORTANT: this function is critical to the security of this proxy. It must ensure that the
// callback is executed with the permissions of the specified user id. A good test is to see if
// the /etc/shadow file can be read with a non-root user, it should fail with EPERM.
fn run_as_user<F: FnOnce() -> T, T>(user: &pwd::Passwd, f: F) -> Result<T, io::Error> {
2024-02-06 15:48:57 -07:00
// Save root HOME
let root_home_opt = env::var_os("HOME");
// Switch to user HOME
env::set_var("HOME", &user.dir);
// Switch to user UID
2024-02-06 15:03:07 -07:00
if unsafe { libc::seteuid(user.uid) } != 0 {
return Err(io::Error::last_os_error());
}
let t = f();
2024-02-06 15:48:57 -07:00
// Restore root UID
2024-02-06 15:03:07 -07:00
if unsafe { libc::seteuid(0) } != 0 {
panic!("failed to restore root user id")
}
2024-02-06 15:48:57 -07:00
// Restore root HOME
match root_home_opt {
Some(root_home) => env::set_var("HOME", root_home),
None => env::remove_var("HOME"),
}
2024-02-06 15:03:07 -07:00
Ok(t)
}
#[derive(DBusError, Debug)]
2024-05-20 11:01:32 -04:00
#[zbus(prefix = "com.system76.CosmicGreeter")]
2024-02-06 15:03:07 -07:00
enum GreeterError {
2024-05-20 11:01:32 -04:00
#[zbus(error)]
2024-02-06 15:03:07 -07:00
ZBus(zbus::Error),
Ron(String),
RunAsUser(String),
}
struct GreeterProxy;
#[zbus::interface(name = "com.system76.CosmicGreeter")]
2024-02-06 15:03:07 -07:00
impl GreeterProxy {
fn get_user_data(&mut self) -> Result<String, GreeterError> {
// The pwd::Passwd method is unsafe (but not labelled as such) due to using global state (libc pwent functions).
// To prevent issues, this should only be called once in the entire process space at a time
let users: Vec<_> = /* unsafe */ {
pwd::Passwd::iter()
.filter(|user| {
if user.uid < 1000 {
// Skip system accounts
return false;
}
match Path::new(&user.shell).file_name().and_then(|x| x.to_str()) {
// Skip shell ending in false
Some("false") => false,
// Skip shell ending in nologin
Some("nologin") => false,
_ => true,
}
})
.collect()
};
let mut user_datas = Vec::new();
for user in users {
if user.uid < 1000 {
// Skip system accounts
continue;
}
match Path::new(&user.shell).file_name().and_then(|x| x.to_str()) {
// Skip shell ending in false
Some("false") => continue,
// Skip shell ending in nologin
Some("nologin") => continue,
_ => (),
}
let mut user_data = UserData::from(user.clone());
2024-02-06 15:48:57 -07:00
//IMPORTANT: Assume the identity of the user to ensure we don't read user file data as root
run_as_user(&user, || user_data.load_config_as_user())
.map_err(|err| GreeterError::RunAsUser(err.to_string()))?;
2024-02-06 15:03:07 -07:00
2024-02-06 15:48:57 -07:00
user_datas.push(user_data);
2024-02-06 15:03:07 -07:00
}
//TODO: is ron the best choice for passing around background data?
ron::to_string(&user_datas).map_err(|err| GreeterError::Ron(err.to_string()))
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
2024-02-22 20:51:05 -07:00
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init();
2024-02-06 15:03:07 -07:00
2025-06-26 15:14:43 -04:00
let _conn = Builder::system()?
2024-02-06 15:03:07 -07:00
.name("com.system76.CosmicGreeter")?
.serve_at("/com/system76/CosmicGreeter", GreeterProxy)?
.build()
.await?;
pending::<()>().await;
Ok(())
}