diff --git a/Cargo.lock b/Cargo.lock index 9614abc..64cf6b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -346,10 +346,12 @@ dependencies = [ "ed25519-dalek", "fs4", "hex", + "num-bigint", "rand_core", "reqwest 0.12.28", "serde", "serde_json", + "sha1", "sha2", "tar", "tempfile", @@ -2078,12 +2080,31 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" +[[package]] +name = "num-bigint" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367" +dependencies = [ + "num-integer", + "num-traits", +] + [[package]] name = "num-conv" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" +[[package]] +name = "num-integer" +version = "0.1.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b" +dependencies = [ + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -3189,6 +3210,17 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "sha1" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a978451301f4db1d02937a4ab3ccce137717b81826e79b7d49ffe3244a13c3b8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "sha2" version = "0.10.9" diff --git a/crates/coa-core/Cargo.toml b/crates/coa-core/Cargo.toml index 7bc709b..ed4bd8c 100644 --- a/crates/coa-core/Cargo.toml +++ b/crates/coa-core/Cargo.toml @@ -19,6 +19,8 @@ base64 = "0.22" zstd = { version = "0.13", features = ["zstdmt"] } reqwest = { version = "0.12", default-features = false, features = ["blocking", "native-tls"] } tar = "0.4" +num-bigint = "0.4" +sha1 = "0.10" tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt", "std"] } [dev-dependencies] diff --git a/crates/coa-core/examples/install_e2e.rs b/crates/coa-core/examples/install_e2e.rs index d291aaa..68089d8 100644 --- a/crates/coa-core/examples/install_e2e.rs +++ b/crates/coa-core/examples/install_e2e.rs @@ -33,7 +33,7 @@ fn main() { &Params { source: Source::Dir(out.clone()), dest: dest.clone(), trusted_key: coa_core::signing::EMBEDDED_PUBLIC_KEY, registry: ®istry, cancel: Cancel::default() }, &|s| eprintln!(" install: {:>3}% {} {}", s.percent, s.step, s.detail.unwrap_or_default()), ) - .expect("install"); + .unwrap_or_else(|e| panic!("install failed: {e:?}")); println!("installed {} at {} in {:?}", installed.version, installed.path, t.elapsed()); assert!(!dest.with_file_name("coa-installtest.installing").exists()); diff --git a/crates/coa-core/examples/ra_account.rs b/crates/coa-core/examples/ra_account.rs index b94e488..db5a784 100644 --- a/crates/coa-core/examples/ra_account.rs +++ b/crates/coa-core/examples/ra_account.rs @@ -1,7 +1,7 @@ //! Create an account through the Manager's RA client on a RUNNING disposable fixture. fn main() { let a: Vec = std::env::args().skip(1).collect(); - assert!(a[0].to_lowercase().contains("fixture"), "refusing: not a fixture folder"); + assert!(a[0].to_lowercase().contains("fixture") || a[0].to_lowercase().contains("installtest"), "refusing: not a disposable folder"); let root = std::path::Path::new(&a[0]); let mut ra = coa_core::ra::Ra::connect(root).expect("connect"); ra.create_account(&a[1], &a[2]).expect("create"); diff --git a/crates/coa-core/src/db.rs b/crates/coa-core/src/db.rs index e9da00f..83d0381 100644 --- a/crates/coa-core/src/db.rs +++ b/crates/coa-core/src/db.rs @@ -247,6 +247,34 @@ impl Db { } } +/// Name of the service account the Manager uses for the server console (RA). Accounts are stored upper-case. +pub const SERVICE_ACCOUNT: &str = "COAMANAGER"; + +impl Db { + /// Create (or reset) the console service account with a fresh random password and administrator rights on + /// all realms, and return that password. The database must be running. + pub fn provision_service_account(&self) -> Result { + let password = hex::encode(&crate::srp6::new_salt()[..8]); // 16 random hex characters + let salt = crate::srp6::new_salt(); + let verifier = crate::srp6::verifier(SERVICE_ACCOUNT, &password, &salt); + let (s, v) = (hex::encode(salt), hex::encode(verifier)); + self.query(&format!( + "INSERT INTO acore_auth.account (username, salt, verifier, email, reg_mail) VALUES ('{SERVICE_ACCOUNT}', UNHEX('{s}'), UNHEX('{v}'), '', '') ON DUPLICATE KEY UPDATE salt=UNHEX('{s}'), verifier=UNHEX('{v}'), failed_logins=0, locked=0; INSERT INTO acore_auth.account_access (id, gmlevel, RealmID, comment) SELECT id, 3, -1, 'CoA Server Manager console' FROM acore_auth.account WHERE username='{SERVICE_ACCOUNT}' ON DUPLICATE KEY UPDATE gmlevel=3;" + ))?; + Ok(password) + } +} + +/// Point `Settings/repack.json` at the service account (other keys are preserved). +pub fn write_console_credentials(root: &Path, password: &str) -> Result<()> { + let path = root.join("Settings/repack.json"); + let mut v: serde_json::Value = fsx::read_json(&path)?; + let obj = v.as_object_mut().ok_or_else(|| Error::Invalid("repack.json is not an object".into()))?; + obj.insert("raUsername".into(), SERVICE_ACCOUNT.into()); + obj.insert("raPassword".into(), password.into()); + fsx::atomic_write_json(&path, &v) +} + pub fn valid_identifier(name: &str) -> bool { ident_ok(name) } diff --git a/crates/coa-core/src/install.rs b/crates/coa-core/src/install.rs index 03eadf1..c7d96bb 100644 --- a/crates/coa-core/src/install.rs +++ b/crates/coa-core/src/install.rs @@ -10,7 +10,7 @@ use std::path::{Path, PathBuf}; use serde::Serialize; -use crate::db::{Account, Db}; +use crate::db::{self, Account, Db}; use crate::download::Cancel; use crate::driver::{self, Verb}; use crate::error::{Error, Result}; @@ -172,6 +172,9 @@ fn bootstrap_database(root: &Path) -> Result<()> { sql.push_str("FLUSH PRIVILEGES;\n"); db.query(&sql)?; fsx::atomic_write_json(&target, &serde_json::json!({ "rootPassword": root_pw, "appPassword": app_pw }))?; + // The server console gets its own account with a random password instead of a shared default. + let console_pw = Db::from_repack(root, Account::Admin)?.provision_service_account()?; // new root password + db::write_console_credentials(root, &console_pw)?; // The new root password must actually work before we throw the old one away. if !Db::from_repack(root, Account::Admin)?.ping() { return Err(Error::Invalid("The new database password did not work.".into())); diff --git a/crates/coa-core/src/lib.rs b/crates/coa-core/src/lib.rs index d319b15..dfb4386 100644 --- a/crates/coa-core/src/lib.rs +++ b/crates/coa-core/src/lib.rs @@ -20,6 +20,7 @@ pub mod ra; pub mod registry; pub mod update; pub mod signing; +pub mod srp6; pub use error::{Error, ErrorCode, Result}; diff --git a/crates/coa-core/src/srp6.rs b/crates/coa-core/src/srp6.rs new file mode 100644 index 0000000..658b465 --- /dev/null +++ b/crates/coa-core/src/srp6.rs @@ -0,0 +1,60 @@ +//! AzerothCore's SRP6 account verifier (salt + verifier), so the Manager can create the service account it +//! needs for the server console without ever knowing a shared default password. + +use num_bigint::BigUint; +use sha1::{Digest, Sha1}; + +const N_HEX: &str = "894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7"; + +fn n() -> BigUint { + BigUint::parse_bytes(N_HEX.as_bytes(), 16).expect("constant") +} + +fn le32(v: &BigUint) -> [u8; 32] { + let mut b = v.to_bytes_le(); + b.resize(32, 0); + b.try_into().expect("32 bytes") +} + +/// Verifier for `username`/`password` under `salt` (both names are case-folded, as the server does). +pub fn verifier(username: &str, password: &str, salt: &[u8; 32]) -> [u8; 32] { + let h1 = Sha1::digest(format!("{}:{}", username.to_uppercase(), password.to_uppercase()).as_bytes()); + let mut h2 = Sha1::new(); + h2.update(salt); + h2.update(h1); + let x = BigUint::from_bytes_le(&h2.finalize()); + le32(&BigUint::from(7u32).modpow(&x, &n())) +} + +/// A fresh random salt. +pub fn new_salt() -> [u8; 32] { + let mut s = [0u8; 32]; + for chunk in s.chunks_mut(16) { + chunk.copy_from_slice(uuid::Uuid::new_v4().as_bytes()); + } + s +} + +#[cfg(test)] +mod tests { + use super::*; + + fn hex32(s: &str) -> [u8; 32] { + hex::decode(s).unwrap().try_into().unwrap() + } + + #[test] + fn matches_a_verifier_created_by_a_real_worldserver() { + // Account created through the server's own `account create` command (RA) on a disposable fixture. + let salt = hex32("54A0DE1E331BB7D2E3964389DB5A443115ED3619BED86B501157DA93C4FC2CF6"); + let expected = hex32("7C39E59C220E8D62CE4CAAB110D4BE8C2381B62E95825B0DC1A4640E57233A46"); + assert_eq!(verifier("ManagerTest1", "hunter22", &salt), expected); + assert_eq!(verifier("MANAGERTEST1", "HUNTER22", &salt), expected, "case-insensitive like the server"); + assert_ne!(verifier("ManagerTest1", "hunter23", &salt), expected); + } + + #[test] + fn salts_are_random() { + assert_ne!(new_salt(), new_salt()); + } +}