2024-07-18 06:37:47 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
2025-02-23 01:17:45 -05:00
|
|
|
use conduwuit::{Result, error, warn};
|
2024-07-18 06:37:47 +00:00
|
|
|
use ruma::{
|
2024-12-15 00:05:47 -05:00
|
|
|
events::{
|
2025-02-23 01:17:45 -05:00
|
|
|
GlobalAccountDataEvent, GlobalAccountDataEventType, push_rules::PushRulesEventContent,
|
2024-12-15 00:05:47 -05:00
|
|
|
},
|
2024-07-18 06:37:47 +00:00
|
|
|
push::Ruleset,
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-05 09:09:38 -04:00
|
|
|
use crate::{
|
|
|
|
|
Dep, account_data, config, globals,
|
|
|
|
|
users::{self, HashedPassword},
|
|
|
|
|
};
|
2024-07-18 06:37:47 +00:00
|
|
|
|
|
|
|
|
pub struct Service {
|
|
|
|
|
services: Services,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Services {
|
|
|
|
|
account_data: Dep<account_data::Service>,
|
2025-03-02 23:16:30 -05:00
|
|
|
config: Dep<config::Service>,
|
2024-07-18 06:37:47 +00:00
|
|
|
globals: Dep<globals::Service>,
|
|
|
|
|
users: Dep<users::Service>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
impl crate::Service for Service {
|
|
|
|
|
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
|
|
|
|
|
Ok(Arc::new(Self {
|
|
|
|
|
services: Services {
|
|
|
|
|
account_data: args.depend::<account_data::Service>("account_data"),
|
2025-03-02 23:16:30 -05:00
|
|
|
config: args.depend::<config::Service>("config"),
|
|
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
globals: args.depend::<globals::Service>("globals"),
|
|
|
|
|
users: args.depend::<users::Service>("users"),
|
|
|
|
|
},
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-15 00:05:47 -05:00
|
|
|
async fn worker(self: Arc<Self>) -> Result {
|
|
|
|
|
self.set_emergency_access().await.inspect_err(|e| {
|
|
|
|
|
error!("Could not set the configured emergency password for the server user: {e}");
|
|
|
|
|
})
|
2024-07-18 06:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Service {
|
2024-12-14 21:58:01 -05:00
|
|
|
/// Sets the emergency password and push rules for the server user account
|
|
|
|
|
/// in case emergency password is set
|
2024-12-15 00:05:47 -05:00
|
|
|
async fn set_emergency_access(&self) -> Result {
|
2024-12-14 21:58:01 -05:00
|
|
|
let server_user = &self.services.globals.server_user;
|
2024-07-18 06:37:47 +00:00
|
|
|
|
2026-05-05 09:09:38 -04:00
|
|
|
self.services.users.set_password(
|
|
|
|
|
server_user,
|
|
|
|
|
self.services
|
|
|
|
|
.config
|
|
|
|
|
.emergency_password
|
|
|
|
|
.as_deref()
|
|
|
|
|
.map(HashedPassword::new)
|
|
|
|
|
.transpose()?,
|
|
|
|
|
);
|
2024-07-18 06:37:47 +00:00
|
|
|
|
2025-03-02 23:16:30 -05:00
|
|
|
let (ruleset, pwd_set) = match self.services.config.emergency_password {
|
2024-12-15 00:05:47 -05:00
|
|
|
| Some(_) => (Ruleset::server_default(server_user), true),
|
|
|
|
|
| None => (Ruleset::new(), false),
|
2024-07-18 06:37:47 +00:00
|
|
|
};
|
|
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
self.services
|
|
|
|
|
.account_data
|
|
|
|
|
.update(
|
|
|
|
|
None,
|
2024-12-14 21:58:01 -05:00
|
|
|
server_user,
|
2024-08-08 17:18:30 +00:00
|
|
|
GlobalAccountDataEventType::PushRules.to_string().into(),
|
2026-04-13 17:31:52 -04:00
|
|
|
&serde_json::to_value(GlobalAccountDataEvent::new(PushRulesEventContent::new(
|
2026-04-07 14:40:10 +01:00
|
|
|
ruleset,
|
|
|
|
|
)))
|
2024-08-08 17:18:30 +00:00
|
|
|
.expect("to json value always works"),
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
2024-07-18 06:37:47 +00:00
|
|
|
|
|
|
|
|
if pwd_set {
|
|
|
|
|
warn!(
|
2024-12-15 00:05:47 -05:00
|
|
|
"The server account emergency password is set! Please unset it as soon as you \
|
|
|
|
|
finish admin account recovery! You will be logged out of the server service \
|
|
|
|
|
account when you finish."
|
2024-07-18 06:37:47 +00:00
|
|
|
);
|
2024-12-15 00:05:47 -05:00
|
|
|
Ok(())
|
2024-07-18 06:37:47 +00:00
|
|
|
} else {
|
|
|
|
|
// logs out any users still in the server service account and removes sessions
|
2024-12-15 00:05:47 -05:00
|
|
|
self.services.users.deactivate_account(server_user).await
|
2024-07-18 06:37:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|