From 04a8005679d62865bec78ef0f977749230da73cd Mon Sep 17 00:00:00 2001 From: selikhov Date: Tue, 29 Sep 2026 18:34:13 +0300 Subject: [PATCH] Add `voelinctl identity import`, `export` and `list` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Import and export have to be reachable before the Profiles screen exists, and a headless command is also how the round trip gets tested against a real client installation without any of its data entering the repository. The listing prints nothing but the nickname, the unique id and the security level: all three are public — servers see them — while everything else in an identity is the private key. `--dry-run` reads and stores nothing. `list` comes along because `export ` is unusable without a way to learn the id. Prove the imported key pair is whole rather than merely parseable: the clients' format carries the public point next to the private scalar and `Identity` keeps only the scalar, so signing a nonce and verifying it with the key derived from that scalar shows the two halves belong together. The same check runs over a real database when `VOELIN_IDENTITY_IMPORT_CHECK` points at one, which keeps that verification repeatable without committing a fixture that could only come from someone's own identities. Co-Authored-By: Claude Opus 5 --- Cargo.lock | 2 + crates/voelin-core/src/identity.rs | 67 +++++++++++++++++++++++ tools/voelinctl/Cargo.toml | 3 ++ tools/voelinctl/src/identity.rs | 86 +++++++++++++++++++++++++++++- tools/voelinctl/src/main.rs | 38 +++++++++++++ 5 files changed, 195 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 13d4017..1275bef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9330,7 +9330,9 @@ dependencies = [ "voelin-gateway-proto", "voelin-model", "voelin-observer", + "voelin-platform", "voelin-query", + "voelin-store", "voelin-stream", ] diff --git a/crates/voelin-core/src/identity.rs b/crates/voelin-core/src/identity.rs index 52318a5..eea4bbf 100644 --- a/crates/voelin-core/src/identity.rs +++ b/crates/voelin-core/src/identity.rs @@ -645,6 +645,73 @@ mod tests { fs::remove_dir_all(dir).unwrap(); } + /// An imported key pair must be whole: the public key derived from the + /// private scalar has to verify what that scalar signs. The clients' + /// format carries the public point alongside the private one and + /// [`Identity`] keeps only the scalar, so this also proves the two + /// halves of the parsed key belong together. + fn signs_and_verifies(identity: &Identity) { + let nonce = b"voelin identity import self-check"; + let signature = identity.key().clone().sign(nonce); + identity.key().to_pub().verify(nonce, &signature).expect("own signature verifies"); + // A different message must not verify under the same signature. + assert!(identity.key().to_pub().verify(b"something else", &signature).is_err()); + } + + #[test] + fn an_imported_key_pair_is_whole() { + let dir = temp_dir("whole"); + let path = dir.join("identity.ini"); + write_export(&path, "tester", &synthetic()).unwrap(); + let found = read(&path).unwrap(); + assert_eq!(found.len(), 1); + signs_and_verifies(&found[0].identity); + fs::remove_dir_all(dir).unwrap(); + } + + /// Checks a real client database when `VOELIN_IDENTITY_IMPORT_CHECK` + /// points at one, printing only public facts: how many identities were + /// found, their nicknames, their unique ids and their levels. Without + /// the variable there is nothing to check and the test passes. + #[test] + fn checks_the_database_the_environment_points_at() { + let Some(path) = std::env::var_os("VOELIN_IDENTITY_IMPORT_CHECK") else { return }; + let path = PathBuf::from(path); + let found = read(&path).expect("read the database"); + println!("{}: {} identities", path.display(), found.len()); + for identity in &found { + let uids = identity.uids(); + // A TeamSpeak unique id is base64 of a SHA-1 or SHA-256 digest. + assert_eq!(uids.ts3.len(), 28, "{}", uids.ts3); + assert_eq!(uids.ts6.len(), 44, "{}", uids.ts6); + for id in [&uids.ts3, &uids.ts6] { + assert!( + id.bytes().all(|b| b.is_ascii_alphanumeric() || b"+/=".contains(&b)), + "{id} is not base64" + ); + } + assert_eq!(uids.ts3, identity.uid()); + // Servers reject anything below 8. + assert!(identity.level() >= 8, "level {}", identity.level()); + signs_and_verifies(&identity.identity); + // The export must come back as the same identity. + let text = export(&identity.nickname, &identity.identity); + let again = text_identities(&text, &path); + assert_eq!(again.len(), 1); + assert_eq!(again[0].uids().ts3, uids.ts3); + assert_eq!(again[0].uids().ts6, uids.ts6); + assert_eq!(again[0].nickname, identity.nickname); + println!( + " {} {} {} level {}", + identity.nickname, + uids.ts3, + uids.ts6, + identity.level() + ); + } + assert!(!found.is_empty()); + } + #[test] fn locations_are_absolute_and_named_settings_db() { for path in locations() { diff --git a/tools/voelinctl/Cargo.toml b/tools/voelinctl/Cargo.toml index df0ab3f..6e9cfc5 100644 --- a/tools/voelinctl/Cargo.toml +++ b/tools/voelinctl/Cargo.toml @@ -27,6 +27,9 @@ tsproto-packets = { path = "../../crates/proto/tsproto-packets" } voelin-model = { path = "../../crates/voelin-model" } voelin-query = { path = "../../crates/voelin-query" } voelin-observer = { path = "../../crates/voelin-observer" } +# The client database (`identity import`/`export`) and where it lives. +voelin-store = { path = "../../crates/voelin-store" } +voelin-platform = { path = "../../crates/voelin-platform", default-features = false } voelin-audio = { path = "../../crates/voelin-audio", default-features = false } # The stream media pipeline (capture, VP8, decoding), without audio devices. voelin-core = { path = "../../crates/voelin-core", default-features = false, features = ["media-desktop"] } diff --git a/tools/voelinctl/src/identity.rs b/tools/voelinctl/src/identity.rs index cceea17..cb3f136 100644 --- a/tools/voelinctl/src/identity.rs +++ b/tools/voelinctl/src/identity.rs @@ -1,15 +1,99 @@ -//! Load, create and store client identities as JSON files. +//! Load, create and store client identities as JSON files, and move them in +//! and out of the client database (`voelin_core::identity`). use std::fs; use std::path::{Path, PathBuf}; use anyhow::{Context, Result}; use tsclientlib::Identity; +use voelin_core::identity::{self as ident, Found, Imported}; +use voelin_store::Store; pub fn default_path() -> PathBuf { dirs::config_dir().unwrap_or_else(|| PathBuf::from(".")).join("voelinctl").join("identity.json") } +/// The client database the desktop app uses. +fn default_store() -> PathBuf { + voelin_platform::paths::data_dir().join("client.db") +} + +fn open_store(path: Option) -> Result { + let path = path.unwrap_or_else(default_store); + Store::open(&path).with_context(|| format!("failed to open {}", path.display())) +} + +/// Identities in the client database, with the id `export` takes. +pub fn list(store: Option) -> Result<()> { + for entry in open_store(store)?.identities()? { + println!("{:>3} {:<20} {} level {}", entry.id, entry.name, entry.uid, entry.level); + } + Ok(()) +} + +/// Import from the official clients. Only public facts are printed: the +/// nickname, the unique id and the security level. +pub fn import(from: Option, dry_run: bool, store: Option) -> Result<()> { + let paths = match from { + Some(path) => vec![path], + None => ident::discover(), + }; + if paths.is_empty() { + anyhow::bail!("no TeamSpeak client data in the usual locations; pass --from "); + } + let mut found: Vec = Vec::new(); + for path in &paths { + match ident::read(path) { + Ok(identities) => found.extend(identities), + // Searching the usual locations turns up files without + // identities; that is not a reason to stop. + Err(e) => eprintln!("{e}"), + } + } + if found.is_empty() { + anyhow::bail!("no identity found in {} file(s)", paths.len()); + } + // `Some(outcome)` once something was stored. + let outcomes: Vec> = if dry_run { + vec![None; found.len()] + } else { + ident::import(&open_store(store)?, &found)?.into_iter().map(Some).collect() + }; + let mut source: Option<&Path> = None; + for (identity, outcome) in found.iter().zip(&outcomes) { + if source != Some(&identity.source) { + println!("{}", identity.source.display()); + source = Some(&identity.source); + } + let status = match outcome { + None => "would import".to_owned(), + Some(Imported::Added(id)) => format!("imported as {id}"), + Some(Imported::Duplicate) => "already in the store".to_owned(), + }; + println!( + " {:<20} {} level {:<3} {status}", + identity.nickname, + identity.uid(), + identity.level() + ); + } + Ok(()) +} + +/// Write a stored identity out in the TeamSpeak 3 `.ini` form. +pub fn export(id: i64, path: &Path, store: Option) -> Result<()> { + let store = open_store(store)?; + let identity = store.identity(id)?; + let name = store + .identities()? + .into_iter() + .find(|entry| entry.id == id) + .map_or_else(|| "voelin".to_owned(), |entry| entry.name); + ident::write_export(path, &name, &identity)?; + println!("wrote {} ({})", path.display(), uid(&identity)); + Ok(()) +} + pub fn load(path: &Path) -> Result { let data = fs::read_to_string(path) .with_context(|| format!("failed to read identity {}", path.display()))?; diff --git a/tools/voelinctl/src/main.rs b/tools/voelinctl/src/main.rs index 8156c45..1d137ed 100644 --- a/tools/voelinctl/src/main.rs +++ b/tools/voelinctl/src/main.rs @@ -86,6 +86,38 @@ enum IdentityCommand { #[arg(long)] identity: Option, }, + /// List the identities in the client database, with the id `export` takes. + List { + /// Client database [default: /voelin/client.db]. + #[arg(long)] + store: Option, + }, + /// Import identities from the official TeamSpeak 3 and 6 clients. Their + /// files are only read, never changed. + Import { + /// A TeamSpeak 3 `.ini` export, or a client's `settings.db`. + #[arg(long, conflicts_with = "auto", required_unless_present = "auto")] + from: Option, + /// Look in the usual locations of both clients instead (see + /// `docs/identity.md`). + #[arg(long)] + auto: bool, + /// List what was found and store nothing. + #[arg(long)] + dry_run: bool, + #[arg(long)] + store: Option, + }, + /// Write an identity from the client database to a TeamSpeak 3 `.ini` + /// export, which the official client imports. + Export { + /// Identity id, from `identity list`. + id: i64, + /// File to write; on Unix it is created readable only by you. + path: PathBuf, + #[arg(long)] + store: Option, + }, } #[derive(Args, Debug)] @@ -259,6 +291,12 @@ fn run_identity(command: IdentityCommand) -> Result<()> { println!("omega: {omega}"); Ok(()) } + IdentityCommand::List { store } => identity::list(store), + // `--auto` only means "no --from"; clap has already required one. + IdentityCommand::Import { from, auto: _, dry_run, store } => { + identity::import(from, dry_run, store) + } + IdentityCommand::Export { id, path, store } => identity::export(id, &path, store), } }