2024-03-05 19:19:06 -05:00
use std ::{
collections ::{ BTreeMap , HashMap } ,
fs ,
2024-03-24 19:21:05 -07:00
future ::Future ,
2024-03-05 19:19:06 -05:00
path ::PathBuf ,
sync ::{
atomic ::{ self , AtomicBool } ,
2024-03-24 19:21:05 -07:00
Arc ,
2024-03-05 19:19:06 -05:00
} ,
2024-03-24 19:21:05 -07:00
time ::Instant ,
2022-10-09 17:25:06 +02:00
} ;
2022-08-07 19:42:22 +02:00
2024-03-05 19:19:06 -05:00
use argon2 ::Argon2 ;
use base64 ::{ engine ::general_purpose , Engine as _ } ;
pub use data ::Data ;
2024-03-19 19:12:49 -04:00
use hickory_resolver ::TokioAsyncResolver ;
2024-03-05 19:19:06 -05:00
use regex ::RegexSet ;
2021-01-14 21:32:22 -05:00
use ruma ::{
2021-06-30 09:52:01 +02:00
api ::{
2022-02-18 15:33:14 +01:00
client ::sync ::sync_events ,
2021-06-30 09:52:01 +02:00
federation ::discovery ::{ ServerSigningKeys , VerifyKey } ,
} ,
2024-03-05 19:19:06 -05:00
serde ::Base64 ,
DeviceId , OwnedDeviceId , OwnedEventId , OwnedRoomId , OwnedServerName , OwnedServerSigningKeyId , OwnedUserId ,
RoomVersionId , ServerName , UserId ,
2021-06-06 16:58:32 +04:30
} ;
2024-03-05 20:52:16 -05:00
use tokio ::sync ::{ broadcast , watch ::Receiver , Mutex , RwLock , Semaphore } ;
2023-03-18 08:58:20 +01:00
use tracing ::{ error , info } ;
2020-07-23 23:03:24 -04:00
2024-03-24 19:21:05 -07:00
use crate ::{ services , Config , Result } ;
2024-03-05 19:19:06 -05:00
2024-03-24 19:21:05 -07:00
pub mod client ;
2024-03-05 19:19:06 -05:00
mod data ;
2024-03-24 19:21:05 -07:00
pub mod resolver ;
2023-08-01 14:48:50 -10:00
2021-05-20 23:46:52 +02:00
type RateLimitState = ( Instant , u32 ) ; // Time if last failed try, number of failed tries
2021-07-14 12:31:38 +02:00
type SyncHandle = (
2022-02-18 15:33:14 +01:00
Option < String > , // since
Receiver < Option < Result < sync_events ::v3 ::Response > > > , // rx
2021-07-14 12:31:38 +02:00
) ;
2023-12-25 16:28:56 +01:00
pub struct Service < ' a > {
2022-10-08 13:02:52 +02:00
pub db : & 'static dyn Data ,
2024-03-05 19:48:54 -05:00
2022-02-06 20:23:22 +01:00
pub config : Config ,
2020-09-15 16:13:54 +02:00
keypair : Arc < ruma ::signatures ::Ed25519KeyPair > ,
2022-10-09 15:34:36 +02:00
jwt_decoding_key : Option < jsonwebtoken ::DecodingKey > ,
2024-03-24 19:21:05 -07:00
pub resolver : Arc < resolver ::Resolver > ,
pub client : client ::Client ,
2021-11-01 01:58:26 +00:00
pub stable_room_versions : Vec < RoomVersionId > ,
pub unstable_room_versions : Vec < RoomVersionId > ,
2022-10-09 17:25:06 +02:00
pub bad_event_ratelimiter : Arc < RwLock < HashMap < OwnedEventId , RateLimitState > > > ,
2021-07-18 20:43:39 +02:00
pub bad_signature_ratelimiter : Arc < RwLock < HashMap < Vec < String > , RateLimitState > > > ,
2023-09-13 20:54:53 +02:00
pub bad_query_ratelimiter : Arc < RwLock < HashMap < OwnedServerName , RateLimitState > > > ,
2022-10-09 17:25:06 +02:00
pub servername_ratelimiter : Arc < RwLock < HashMap < OwnedServerName , Arc < Semaphore > > > > ,
pub sync_receivers : RwLock < HashMap < ( OwnedUserId , OwnedDeviceId ) , SyncHandle > > ,
2024-03-05 20:52:16 -05:00
pub roomid_mutex_insert : RwLock < HashMap < OwnedRoomId , Arc < Mutex < ( ) > > > > ,
pub roomid_mutex_state : RwLock < HashMap < OwnedRoomId , Arc < Mutex < ( ) > > > > ,
pub roomid_mutex_federation : RwLock < HashMap < OwnedRoomId , Arc < Mutex < ( ) > > > > , // this lock will be held longer
2022-10-09 17:25:06 +02:00
pub roomid_federationhandletime : RwLock < HashMap < OwnedRoomId , ( OwnedEventId , Instant ) > > ,
2022-05-30 12:58:43 +02:00
pub stateres_mutex : Arc < Mutex < ( ) > > ,
2024-01-14 22:39:08 -05:00
pub ( crate ) rotate : RotationHandler ,
2024-03-05 19:48:54 -05:00
2023-03-18 08:58:20 +01:00
pub shutdown : AtomicBool ,
2023-12-25 16:28:56 +01:00
pub argon : Argon2 < ' a > ,
2020-05-03 17:25:31 +02:00
}
2021-07-14 07:07:08 +00:00
/// Handles "rotation" of long-polling requests. "Rotation" in this context is
/// similar to "rotation" of log files and the like.
///
/// This is utilized to have sync workers return early and release read locks on
/// the database.
2024-01-14 22:39:08 -05:00
pub ( crate ) struct RotationHandler ( broadcast ::Sender < ( ) > , ( ) ) ;
2021-07-14 07:07:08 +00:00
impl RotationHandler {
pub fn new ( ) -> Self {
2024-01-14 22:39:08 -05:00
let ( s , _r ) = broadcast ::channel ( 1 ) ;
Self ( s , ( ) )
2021-07-14 07:07:08 +00:00
}
2024-03-05 19:48:54 -05:00
2021-07-14 07:07:08 +00:00
pub fn watch ( & self ) -> impl Future < Output = ( ) > {
let mut r = self . 0. subscribe ( ) ;
2024-03-05 19:48:54 -05:00
2021-07-14 07:07:08 +00:00
async move {
2024-03-23 14:38:15 -04:00
_ = r . recv ( ) . await ;
2021-07-14 07:07:08 +00:00
}
2024-03-05 19:48:54 -05:00
}
2024-03-23 14:38:15 -04:00
pub fn fire ( & self ) { _ = self . 0. send ( ( ) ) ; }
2021-07-14 07:07:08 +00:00
}
2021-07-14 12:31:38 +02:00
impl Default for RotationHandler {
fn default ( ) -> Self { Self ::new ( ) }
}
2023-12-25 16:28:56 +01:00
impl Service < '_ > {
2024-03-23 14:38:15 -04:00
pub fn load ( db : & 'static dyn Data , config : & Config ) -> Result < Self > {
2022-09-07 13:25:51 +02:00
let keypair = db . load_keypair ( ) ;
2024-03-05 19:48:54 -05:00
2020-09-15 21:46:10 +02:00
let keypair = match keypair {
Ok ( k ) = > k ,
Err ( e ) = > {
error! ( " Keypair invalid. Deleting... " ) ;
2022-10-08 13:02:52 +02:00
db . remove_keypair ( ) ? ;
2020-09-15 21:46:10 +02:00
return Err ( e ) ;
} ,
} ;
2024-03-05 19:48:54 -05:00
2024-03-25 17:05:11 -04:00
let jwt_decoding_key = config
. jwt_secret
. as_ref ( )
. map ( | secret | jsonwebtoken ::DecodingKey ::from_secret ( secret . as_bytes ( ) ) ) ;
2024-03-05 19:48:54 -05:00
2024-03-24 19:21:05 -07:00
let resolver = Arc ::new ( resolver ::Resolver ::new ( config ) ) ;
2021-11-01 01:58:26 +00:00
// Supported and stable room versions
2022-02-18 13:41:37 +01:00
let stable_room_versions = vec! [
RoomVersionId ::V6 ,
RoomVersionId ::V7 ,
RoomVersionId ::V8 ,
RoomVersionId ::V9 ,
2022-10-10 15:00:44 +02:00
RoomVersionId ::V10 ,
] ;
2022-10-15 12:16:32 +02:00
// Experimental, partially supported room versions
2023-09-02 12:18:46 -04:00
let unstable_room_versions = vec! [
2023-09-02 13:59:45 -04:00
RoomVersionId ::V2 ,
2023-09-02 12:18:46 -04:00
RoomVersionId ::V3 ,
RoomVersionId ::V4 ,
RoomVersionId ::V5 ,
RoomVersionId ::V11 ,
] ;
2023-12-25 16:28:56 +01:00
// 19456 Kib blocks, iterations = 2, parallelism = 1 for more info https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#argon2id
let argon = Argon2 ::new (
argon2 ::Algorithm ::Argon2id ,
argon2 ::Version ::default ( ) ,
argon2 ::Params ::new ( 19456 , 2 , 1 , None ) . expect ( " valid parameters " ) ,
) ;
2021-11-05 20:47:11 +00:00
let mut s = Self {
2022-09-07 13:25:51 +02:00
db ,
2024-03-16 15:54:58 -07:00
config : config . clone ( ) ,
2020-09-15 21:46:10 +02:00
keypair : Arc ::new ( keypair ) ,
2024-03-24 19:21:05 -07:00
resolver : resolver . clone ( ) ,
client : client ::Client ::new ( config , & resolver ) ,
2022-02-07 12:55:21 +01:00
jwt_decoding_key ,
stable_room_versions ,
2021-01-29 11:20:33 -05:00
unstable_room_versions ,
bad_event_ratelimiter : Arc ::new ( RwLock ::new ( HashMap ::new ( ) ) ) ,
2021-04-15 22:07:27 -03:00
bad_signature_ratelimiter : Arc ::new ( RwLock ::new ( HashMap ::new ( ) ) ) ,
bad_query_ratelimiter : Arc ::new ( RwLock ::new ( HashMap ::new ( ) ) ) ,
servername_ratelimiter : Arc ::new ( RwLock ::new ( HashMap ::new ( ) ) ) ,
2024-02-10 11:22:25 -05:00
roomid_mutex_state : RwLock ::new ( HashMap ::new ( ) ) ,
2021-08-03 11:10:58 +02:00
roomid_mutex_insert : RwLock ::new ( HashMap ::new ( ) ) ,
2022-01-28 12:42:47 -06:00
roomid_mutex_federation : RwLock ::new ( HashMap ::new ( ) ) ,
2021-07-18 20:43:39 +02:00
roomid_federationhandletime : RwLock ::new ( HashMap ::new ( ) ) ,
stateres_mutex : Arc ::new ( Mutex ::new ( ( ) ) ) ,
2023-09-13 20:54:53 +02:00
sync_receivers : RwLock ::new ( HashMap ::new ( ) ) ,
rotate : RotationHandler ::new ( ) ,
2021-07-18 20:43:39 +02:00
shutdown : AtomicBool ::new ( false ) ,
argon ,
2024-03-05 19:48:54 -05:00
} ;
2021-07-18 20:43:39 +02:00
fs ::create_dir_all ( s . get_media_folder ( ) ) ? ;
2024-03-05 19:48:54 -05:00
2024-03-25 17:05:11 -04:00
if ! s
. supported_room_versions ( )
. contains ( & s . config . default_room_version )
{
2021-07-18 20:43:39 +02:00
error! ( config = ? s . config . default_room_version , fallback = ? crate ::config ::default_default_room_version ( ) , " Room version in config isn't supported, falling back to default version " ) ;
2022-05-30 12:58:43 +02:00
s . config . default_room_version = crate ::config ::default_default_room_version ( ) ;
2024-03-05 19:48:54 -05:00
} ;
Ok ( s )
}
2021-07-18 20:43:39 +02:00
/// Returns this server's keypair.
2021-06-08 17:50:06 +04:30
pub fn keypair ( & self ) -> & ruma ::signatures ::Ed25519KeyPair { & self . keypair }
2024-03-05 19:48:54 -05:00
2024-02-10 11:22:25 -05:00
#[ tracing::instrument(skip(self)) ]
pub fn next_count ( & self ) -> Result < u64 > { self . db . next_count ( ) }
2024-03-05 19:48:54 -05:00
2024-02-10 11:22:25 -05:00
#[ tracing::instrument(skip(self)) ]
pub fn current_count ( & self ) -> Result < u64 > { self . db . current_count ( ) }
2024-03-05 19:48:54 -05:00
2024-02-10 11:22:25 -05:00
#[ tracing::instrument(skip(self)) ]
pub fn last_check_for_updates_id ( & self ) -> Result < u64 > { self . db . last_check_for_updates_id ( ) }
2024-03-05 19:48:54 -05:00
2024-02-10 11:22:25 -05:00
#[ tracing::instrument(skip(self)) ]
pub fn update_check_for_updates_id ( & self , id : u64 ) -> Result < ( ) > { self . db . update_check_for_updates_id ( id ) }
2024-03-05 19:48:54 -05:00
2022-01-24 18:42:15 -06:00
pub async fn watch ( & self , user_id : & UserId , device_id : & DeviceId ) -> Result < ( ) > {
2022-01-28 12:42:47 -06:00
self . db . watch ( user_id , device_id ) . await
2024-03-05 19:48:54 -05:00
}
2022-01-28 12:42:47 -06:00
pub fn cleanup ( & self ) -> Result < ( ) > { self . db . cleanup ( ) }
2024-03-05 19:48:54 -05:00
2024-03-06 18:14:30 -05:00
pub fn flush ( & self ) -> Result < ( ) > { self . db . flush ( ) }
2022-01-28 12:42:47 -06:00
pub fn server_name ( & self ) -> & ServerName { self . config . server_name . as_ref ( ) }
2024-03-05 19:48:54 -05:00
2022-01-24 18:42:15 -06:00
pub fn max_request_size ( & self ) -> u32 { self . config . max_request_size }
2024-03-05 19:48:54 -05:00
2022-01-27 10:19:28 -06:00
pub fn max_fetch_prev_events ( & self ) -> u16 { self . config . max_fetch_prev_events }
2024-03-05 19:48:54 -05:00
2023-09-08 14:36:39 +02:00
pub fn allow_registration ( & self ) -> bool { self . config . allow_registration }
2024-03-05 19:48:54 -05:00
2022-01-27 10:19:28 -06:00
pub fn allow_guest_registration ( & self ) -> bool { self . config . allow_guest_registration }
2024-03-05 19:48:54 -05:00
2022-01-28 12:42:47 -06:00
pub fn allow_encryption ( & self ) -> bool { self . config . allow_encryption }
2024-03-05 19:48:54 -05:00
2023-09-10 02:32:37 -04:00
pub fn allow_federation ( & self ) -> bool { self . config . allow_federation }
2024-03-05 19:48:54 -05:00
2022-01-28 12:42:47 -06:00
pub fn allow_public_room_directory_over_federation ( & self ) -> bool {
self . config . allow_public_room_directory_over_federation
2020-05-03 17:25:31 +02:00
}
2024-03-05 19:48:54 -05:00
2023-09-10 02:32:37 -04:00
pub fn allow_public_room_directory_without_auth ( & self ) -> bool {
self . config . allow_public_room_directory_without_auth
2024-03-05 19:48:54 -05:00
}
2021-07-29 08:36:01 +02:00
pub fn allow_device_name_federation ( & self ) -> bool { self . config . allow_device_name_federation }
2024-03-05 19:48:54 -05:00
2020-05-03 17:25:31 +02:00
pub fn allow_room_creation ( & self ) -> bool { self . config . allow_room_creation }
2024-03-05 19:48:54 -05:00
2020-05-03 17:25:31 +02:00
pub fn allow_unstable_room_versions ( & self ) -> bool { self . config . allow_unstable_room_versions }
2024-03-05 19:48:54 -05:00
2020-05-03 17:25:31 +02:00
pub fn default_room_version ( & self ) -> RoomVersionId { self . config . default_room_version . clone ( ) }
2024-03-05 19:48:54 -05:00
2023-07-29 20:01:38 +02:00
pub fn new_user_displayname_suffix ( & self ) -> & String { & self . config . new_user_displayname_suffix }
2024-03-05 19:48:54 -05:00
2023-07-29 20:01:38 +02:00
pub fn allow_check_for_updates ( & self ) -> bool { self . config . allow_check_for_updates }
2024-03-05 19:48:54 -05:00
2023-07-29 20:01:38 +02:00
pub fn trusted_servers ( & self ) -> & [ OwnedServerName ] { & self . config . trusted_servers }
2024-03-05 19:48:54 -05:00
2023-07-29 20:01:38 +02:00
pub fn query_trusted_key_servers_first ( & self ) -> bool { self . config . query_trusted_key_servers_first }
2024-03-05 19:48:54 -05:00
2024-03-24 19:21:05 -07:00
pub fn dns_resolver ( & self ) -> & TokioAsyncResolver { & self . resolver . resolver }
2024-03-26 21:32:10 -04:00
pub fn query_all_nameservers ( & self ) -> bool { self . config . query_all_nameservers }
2024-03-24 19:21:05 -07:00
pub fn actual_destinations ( & self ) -> & Arc < RwLock < resolver ::WellKnownMap > > { & self . resolver . destinations }
2024-03-05 19:48:54 -05:00
2022-10-05 15:33:57 +02:00
pub fn jwt_decoding_key ( & self ) -> Option < & jsonwebtoken ::DecodingKey > { self . jwt_decoding_key . as_ref ( ) }
2024-03-05 19:48:54 -05:00
2021-10-01 15:53:16 +02:00
pub fn turn_password ( & self ) -> & String { & self . config . turn_password }
2024-03-05 19:48:54 -05:00
2022-10-05 15:33:57 +02:00
pub fn turn_ttl ( & self ) -> u64 { self . config . turn_ttl }
2024-03-05 19:48:54 -05:00
2022-10-05 15:33:57 +02:00
pub fn turn_uris ( & self ) -> & [ String ] { & self . config . turn_uris }
2024-03-05 19:48:54 -05:00
2020-07-17 16:00:39 -04:00
pub fn turn_username ( & self ) -> & String { & self . config . turn_username }
2024-03-05 19:48:54 -05:00
2020-07-23 23:03:24 -04:00
pub fn turn_secret ( & self ) -> & String { & self . config . turn_secret }
2024-03-05 19:48:54 -05:00
2024-03-20 11:19:41 -04:00
pub fn auto_join_rooms ( & self ) -> & [ OwnedRoomId ] { & self . config . auto_join_rooms }
2020-12-05 21:03:43 +01:00
pub fn notification_push_path ( & self ) -> & String { & self . config . notification_push_path }
2024-03-05 19:48:54 -05:00
2022-09-09 19:17:29 +02:00
pub fn emergency_password ( & self ) -> & Option < String > { & self . config . emergency_password }
2024-03-05 19:48:54 -05:00
2021-01-01 13:47:53 +01:00
pub fn url_preview_domain_contains_allowlist ( & self ) -> & Vec < String > {
& self . config . url_preview_domain_contains_allowlist
2020-06-06 19:02:31 +02:00
}
2024-03-05 19:48:54 -05:00
2023-12-21 20:44:58 -05:00
pub fn url_preview_domain_explicit_allowlist ( & self ) -> & Vec < String > {
& self . config . url_preview_domain_explicit_allowlist
2024-03-05 19:48:54 -05:00
}
2023-12-21 20:44:58 -05:00
pub fn url_preview_url_contains_allowlist ( & self ) -> & Vec < String > { & self . config . url_preview_url_contains_allowlist }
2024-03-05 19:48:54 -05:00
2024-02-10 13:29:12 -05:00
pub fn url_preview_max_spider_size ( & self ) -> usize { self . config . url_preview_max_spider_size }
2024-03-05 19:48:54 -05:00
2023-12-21 20:44:58 -05:00
pub fn url_preview_check_root_domain ( & self ) -> bool { self . config . url_preview_check_root_domain }
2024-03-05 19:48:54 -05:00
2024-03-18 21:17:05 -04:00
pub fn forbidden_alias_names ( & self ) -> & RegexSet { & self . config . forbidden_alias_names }
2024-03-05 19:48:54 -05:00
2023-12-21 20:44:58 -05:00
pub fn forbidden_usernames ( & self ) -> & RegexSet { & self . config . forbidden_usernames }
2024-03-05 19:48:54 -05:00
2021-01-01 13:47:53 +01:00
pub fn allow_local_presence ( & self ) -> bool { self . config . allow_local_presence }
2024-03-05 19:48:54 -05:00
2021-01-01 13:47:53 +01:00
pub fn allow_incoming_presence ( & self ) -> bool { self . config . allow_incoming_presence }
2024-03-05 19:48:54 -05:00
2021-01-01 13:47:53 +01:00
pub fn allow_outgoing_presence ( & self ) -> bool { self . config . allow_outgoing_presence }
2024-03-05 19:48:54 -05:00
2023-07-10 16:41:00 +02:00
pub fn presence_idle_timeout_s ( & self ) -> u64 { self . config . presence_idle_timeout_s }
2024-03-05 19:48:54 -05:00
2023-07-10 16:41:00 +02:00
pub fn presence_offline_timeout_s ( & self ) -> u64 { self . config . presence_offline_timeout_s }
2024-03-05 19:48:54 -05:00
2024-03-17 12:16:04 -04:00
pub fn allow_incoming_read_receipts ( & self ) -> bool { self . config . allow_incoming_read_receipts }
2023-12-02 21:30:06 -05:00
pub fn rocksdb_log_level ( & self ) -> & String { & self . config . rocksdb_log_level }
2024-03-05 19:48:54 -05:00
2021-01-01 13:47:53 +01:00
pub fn rocksdb_max_log_file_size ( & self ) -> usize { self . config . rocksdb_max_log_file_size }
2024-03-05 19:48:54 -05:00
2021-01-01 13:47:53 +01:00
pub fn rocksdb_log_time_to_roll ( & self ) -> usize { self . config . rocksdb_log_time_to_roll }
2024-03-05 19:48:54 -05:00
2023-09-10 02:32:37 -04:00
pub fn rocksdb_optimize_for_spinning_disks ( & self ) -> bool { self . config . rocksdb_optimize_for_spinning_disks }
2024-03-05 19:48:54 -05:00
2023-09-10 02:32:37 -04:00
pub fn rocksdb_parallelism_threads ( & self ) -> usize { self . config . rocksdb_parallelism_threads }
2024-03-05 19:48:54 -05:00
2024-03-13 12:31:13 -04:00
pub fn rocksdb_compression_algo ( & self ) -> & String { & self . config . rocksdb_compression_algo }
2024-03-13 22:22:07 -04:00
pub fn rocksdb_compression_level ( & self ) -> i32 { self . config . rocksdb_compression_level }
2024-03-13 22:38:30 -04:00
pub fn rocksdb_bottommost_compression_level ( & self ) -> i32 { self . config . rocksdb_bottommost_compression_level }
2023-09-10 15:40:00 -04:00
pub fn prevent_media_downloads_from ( & self ) -> & [ OwnedServerName ] { & self . config . prevent_media_downloads_from }
2024-03-05 19:48:54 -05:00
2021-09-24 07:16:34 +00:00
pub fn ip_range_denylist ( & self ) -> & [ String ] { & self . config . ip_range_denylist }
2024-03-05 19:48:54 -05:00
2021-11-01 01:58:26 +00:00
pub fn block_non_admin_invites ( & self ) -> bool { self . config . block_non_admin_invites }
2024-03-05 19:48:54 -05:00
2021-11-01 01:58:26 +00:00
pub fn supported_room_versions ( & self ) -> Vec < RoomVersionId > {
let mut room_versions : Vec < RoomVersionId > = vec! [ ] ;
2021-11-05 20:47:11 +00:00
room_versions . extend ( self . stable_room_versions . clone ( ) ) ;
if self . allow_unstable_room_versions ( ) {
room_versions . extend ( self . unstable_room_versions . clone ( ) ) ;
2024-03-05 19:48:54 -05:00
} ;
2021-11-01 01:58:26 +00:00
room_versions
}
2024-03-05 19:48:54 -05:00
2024-02-20 23:08:53 -05:00
/// TODO: the key valid until timestamp (`valid_until_ts`) is only honored
/// in room version > 4
2024-03-05 19:48:54 -05:00
///
2021-01-14 21:32:22 -05:00
/// Remove the outdated keys and insert the new ones.
2024-03-05 19:48:54 -05:00
///
2021-01-14 21:32:22 -05:00
/// This doesn't actually check that the keys provided are newer than the
/// old set.
2024-02-20 23:08:53 -05:00
pub fn add_signing_key (
& self , origin : & ServerName , new_keys : ServerSigningKeys ,
2023-07-29 20:01:38 +02:00
) -> Result < BTreeMap < OwnedServerSigningKeyId , VerifyKey > > {
self . db . add_signing_key ( origin , new_keys )
2024-03-05 19:48:54 -05:00
}
2023-07-29 20:01:38 +02:00
/// This returns an empty `Ok(BTreeMap<..>)` when there are no keys found
2022-10-09 17:25:06 +02:00
/// for the server.
pub fn signing_keys_for ( & self , origin : & ServerName ) -> Result < BTreeMap < OwnedServerSigningKeyId , VerifyKey > > {
2024-03-05 19:19:06 -05:00
let mut keys = self . db . signing_keys_for ( origin ) ? ;
2020-12-06 11:05:51 +01:00
if origin = = self . server_name ( ) {
keys . insert (
format! ( " ed25519: {} " , services ( ) . globals . keypair ( ) . version ( ) )
. try_into ( )
. expect ( " found invalid server signing keys in DB " ) ,
VerifyKey {
key : Base64 ::new ( self . keypair . public_key ( ) . to_vec ( ) ) ,
2024-03-05 19:48:54 -05:00
} ,
) ;
2020-12-06 11:05:51 +01:00
}
2021-01-14 21:32:22 -05:00
2022-10-09 15:34:36 +02:00
Ok ( keys )
2021-02-07 17:38:45 +01:00
}
2021-01-14 21:32:22 -05:00
2021-10-01 15:53:16 +02:00
pub fn database_version ( & self ) -> Result < u64 > { self . db . database_version ( ) }
pub fn bump_database_version ( & self , new_version : u64 ) -> Result < ( ) > { self . db . bump_database_version ( new_version ) }
pub fn get_media_folder ( & self ) -> PathBuf {
2023-10-28 23:43:41 -04:00
let mut r = PathBuf ::new ( ) ;
r . push ( self . config . database_path . clone ( ) ) ;
2022-04-07 12:11:55 +00:00
r . push ( " media " ) ;
2024-03-05 19:48:54 -05:00
r
2022-04-07 12:11:55 +00:00
}
2024-03-05 19:48:54 -05:00
2024-02-10 13:29:12 -05:00
/// new SHA256 file name media function, requires "sha256_media" feature
2024-02-11 11:42:55 -05:00
/// flag enabled and database migrated uses SHA256 hash of the base64 key as
2024-02-08 19:11:48 -05:00
/// the file name
2024-03-09 15:53:22 -05:00
#[ cfg(feature = " sha256_media " ) ]
2024-02-08 19:11:48 -05:00
pub fn get_media_file_new ( & self , key : & [ u8 ] ) -> PathBuf {
let mut r = PathBuf ::new ( ) ;
2023-09-08 14:36:39 +02:00
r . push ( self . config . database_path . clone ( ) ) ;
r . push ( " media " ) ;
// Using the hash of the base64 key as the filename
// This is to prevent the total length of the path from exceeding the maximum
2024-01-21 13:28:51 -05:00
// length in most filesystems
2024-03-09 15:53:22 -05:00
r . push ( general_purpose ::URL_SAFE_NO_PAD . encode ( < sha2 ::Sha256 as sha2 ::Digest > ::digest ( key ) ) ) ;
2024-03-05 19:48:54 -05:00
r
}
2023-07-10 16:41:00 +02:00
/// old base64 file name media function
2023-12-02 21:30:06 -05:00
/// This is the old version of `get_media_file` that uses the full base64
/// key as the filename.
2024-01-21 22:59:06 -05:00
pub fn get_media_file ( & self , key : & [ u8 ] ) -> PathBuf {
2021-11-01 01:58:26 +00:00
let mut r = PathBuf ::new ( ) ;
2021-11-05 20:47:11 +00:00
r . push ( self . config . database_path . clone ( ) ) ;
2024-02-07 11:25:26 -05:00
r . push ( " media " ) ;
2021-01-14 21:32:22 -05:00
r . push ( general_purpose ::URL_SAFE_NO_PAD . encode ( key ) ) ;
r
2024-03-05 19:48:54 -05:00
}
2022-10-09 17:25:06 +02:00
pub fn well_known_client ( & self ) -> & Option < String > { & self . config . well_known_client }
2024-03-05 19:48:54 -05:00
2021-05-17 10:25:27 +02:00
pub fn well_known_server ( & self ) -> & Option < String > { & self . config . well_known_server }
2024-03-05 19:48:54 -05:00
2021-06-04 08:06:12 +04:30
pub fn unix_socket_path ( & self ) -> & Option < PathBuf > { & self . config . unix_socket_path }
2024-03-05 19:48:54 -05:00
2023-11-25 02:11:41 -05:00
pub fn shutdown ( & self ) {
self . shutdown . store ( true , atomic ::Ordering ::Relaxed ) ;
// On shutdown
2024-03-05 19:48:54 -05:00
2023-11-25 02:11:41 -05:00
if self . unix_socket_path ( ) . is_some ( ) {
2021-06-06 16:58:32 +04:30
match & self . unix_socket_path ( ) {
2021-06-04 08:06:12 +04:30
Some ( path ) = > {
2024-03-08 09:25:47 -05:00
fs ::remove_file ( path ) . unwrap ( ) ;
2021-06-04 08:06:12 +04:30
} ,
2024-01-07 21:24:55 -05:00
None = > error! (
" Unable to remove socket file at {:?} during shutdown. " ,
2023-07-29 21:57:41 +00:00
& self . unix_socket_path ( )
2023-03-18 08:58:20 +01:00
) ,
} ;
2023-07-29 21:57:41 +00:00
} ;
2024-03-05 19:48:54 -05:00
2023-03-18 08:58:20 +01:00
info! ( target : " shutdown-sync " , " Received shutdown notification, notifying sync helpers... " ) ;
services ( ) . globals . rotate . fire ( ) ;
}
2020-05-03 17:25:31 +02:00
}