From 960d2ff1cf3078117b9c8c2610901a6036200809 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Thu, 29 Oct 2026 23:52:02 -0700 Subject: [PATCH] Add export-directory: a server's accounts, groups, lists and aliases, without secrets Reads domains, users, groups, mailing lists, tenants and roles from the registry with an administrator's login and writes directory.json with domains.csv, users.csv, groups.csv, lists.csv and aliases.csv. Every read names its properties, so credentials, encryption keys and DKIM keys are never fetched, and only named fields are written even if a server sends more. Ids become addresses and names; --domain keeps the export to some domains with the tenants and roles they use. CSV cells a spreadsheet would run as formulas start with '. Session::registry_account finds the account that advertises the registry, as inbuxa and upstream list it per account. --- CHANGELOG.md | 5 + README.md | 5 +- docs/usage.md | 53 ++- src/cli.rs | 84 ++++ src/export_directory/csv.rs | 88 ++++ src/export_directory/mod.rs | 259 +++++++++++ src/export_directory/model.rs | 783 +++++++++++++++++++++++++++++++++ src/export_files/mod.rs | 4 +- src/jmap/session.rs | 39 ++ src/lib.rs | 1 + src/main.rs | 9 +- tests/mock_export_directory.rs | 273 ++++++++++++ 12 files changed, 1598 insertions(+), 5 deletions(-) create mode 100644 src/export_directory/csv.rs create mode 100644 src/export_directory/mod.rs create mode 100644 src/export_directory/model.rs create mode 100644 tests/mock_export_directory.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cdf381..01b61eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,11 @@ candidates before a series ships (`27.1-rc.1`). The tag is the version with a archive), flags, keywords where the folder keeps them, and arrival times. Like `export`, it matches what is already there, so running it again writes only what changed. +- `export-directory` writes who is on a server: its domains, users, groups, + mailing lists, aliases, quotas, tenants and roles, read from the registry + with an administrator's login, as `directory.json` and CSV files that + another server can be set up from. Credentials, OTP seeds, API keys and + DKIM keys are never asked for. `--domain` keeps it to some domains. ### Changed - `import maildir` reads folder names in modified UTF-7, as Dovecot and diff --git a/README.md b/README.md index af0c45f..6f0c054 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ contacts, identities, Sieve scripts and files. inbuxa-migrate export … alice.sqlite the archive into inbuxa inbuxa-migrate export-files alice.sqlite out the archive as plain files inbuxa-migrate export-imap … alice.sqlite its mail into any IMAP server + inbuxa-migrate export-directory … out a server's accounts, no secrets Between the two sides sits an archive: one SQLite file holding one account. `import` fills it from the old server; `export` writes it into the new one. @@ -39,7 +40,9 @@ advertises the types being written. `export-imap` writes the mail into any IMAP server, and `export-files` is the way out to everything else: it writes the archive as a Maildir++ tree, vCard, iCalendar and Sieve files that other servers and mail, calendar and contacts apps open, with no network and no -store format in the way. The full command reference is in +store format in the way. For a whole server, `export-directory` writes the +list of domains, accounts, groups, lists and aliases to set up again +elsewhere, without passwords. The full command reference is in [docs/usage.md](docs/usage.md). ## Sources diff --git a/docs/usage.md b/docs/usage.md index 30c510c..f61f063 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -13,7 +13,7 @@ and how to install it. ## Commands and global flags ``` -inbuxa-migrate [args...] +inbuxa-migrate [args...] ``` Every command takes a few global flags -- verbosity, worker pool size, retry @@ -375,6 +375,57 @@ the way to finish it is to run again into an empty directory. type writes all of mail, and the same for contacts and calendars. With `--dry-run` it counts what it would write and creates nothing. +## Export a directory + +``` +inbuxa-migrate export-directory \ + --url \ + (--auth-basic [--auth-password ] | --auth-bearer [TOKEN]) \ + [--domain ]... [--dry-run] \ + +``` + +Writes who has an address on a server, so the same accounts can be set up +on another one: domains, users, groups, mailing lists, their aliases and +quotas, and the tenants and roles they belong to. It reads inbuxa's +registry, or Stalwart's, so the login must be an administrator's. Mail and +the rest of each account's data move with `import` and the exports above. + +Nothing secret is written, because nothing secret is asked for: every read +names the fields it wants, and credentials, app passwords, OTP seeds, API +keys, encryption keys and DKIM keys are not among them. Each new server +sets its own passwords and DKIM keys. + +``` +DIR/ + directory.json everything below, exactly, with roles and tenants + domains.csv name, enabled, aliases, catch-all, sub-addressing, tenant + users.csv address, description, roles, groups, aliases, disk quota + groups.csv address, members, aliases, disk quota + lists.csv address, recipients, aliases + aliases.csv each alias with the address it delivers to +``` + +- Accounts, groups and lists are named by address, and roles and tenants by + name, never by the server's ids. A group lists its members, a user the + groups it is in. +- Roles are `user`, `admin` or `default`, or the custom roles by name; + `permissions` holds what an account adds to its roles (`merge`) or has in + their place (`replace`). +- Quotas are by name, in bytes or counts (`maxDiskQuota` and the rest); the + CSV files show `maxDiskQuota` as `disk_quota`. +- In the CSV files, a cell with several values joins them with `;`. The + text comes from the server and these files are made to be opened in a + spreadsheet, so a cell that one would run as a formula starts with `'`. + `directory.json` keeps every value as it is. + +`--domain` keeps the export to the domains named, with what they use: +their tenants and the roles their accounts hold. A group's members on +other domains are still listed by address. `DIR` must be new or empty, and +is made readable by its owner only. `directory.json` is written last, so a +directory without one was interrupted. With `--dry-run` it reads the +registry and counts what it would write, and creates nothing. + ## Inspect ``` diff --git a/src/cli.rs b/src/cli.rs index 032a02d..4ddf320 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -14,6 +14,7 @@ use crate::error::Error; use crate::exchange_ews::oauth::OAuthFlow; use crate::exchange_ews::types::MailboxKind; use crate::exchange_graph::types::{EventBodyFormat, MailboxKind as GraphMailboxKind, Surfaces}; +use crate::export_directory::ExportDirectoryConfig; use crate::export_files::ExportFilesConfig; use crate::inspect::InspectConfig; use crate::jmap::account::AccountSelector; @@ -61,6 +62,11 @@ enum Command { name = "export-imap" )] ExportImap(Box), + #[command( + about = "Write a server's domains, accounts, groups, lists and aliases as JSON and CSV, without secrets", + name = "export-directory" + )] + ExportDirectory(Box), #[command(about = "Show the contents of a local archive (read-only)")] Inspect(Box), } @@ -445,6 +451,64 @@ struct ExportFilesArgs { dir: PathBuf, } +#[derive(Args)] +#[command(group(clap::ArgGroup::new("auth").required(true).args(["auth_basic", "auth_bearer"])))] +struct ExportDirectoryArgs { + #[arg(long, value_name = "URL", help = "Base URL or full JMAP session URL")] + url: String, + + #[arg(long, value_name = "USER", help = "HTTP Basic user (an administrator)")] + auth_basic: Option, + + #[arg( + long, + value_name = "PASS", + help = "Basic password (prefer $INBUXA_MIGRATE_PASSWORD or prompt)" + )] + auth_password: Option, + + #[arg( + long, + value_name = "TOKEN", + num_args = 0..=1, + help = "API key or bearer token (value optional: falls back to $INBUXA_MIGRATE_API_KEY or prompt)" + )] + auth_bearer: Option>, + + #[arg( + long, + value_name = "NAME", + help = "Only this domain (repeatable; default: every domain)" + )] + domain: Vec, + + #[arg(long, help = "Count what would be written; write nothing")] + dry_run: bool, + + #[arg( + long, + value_name = "N", + default_value_t = 5, + help = "Max retry attempts per request on transient failures" + )] + max_retries: u32, + + #[arg( + long, + help = "Accept an invalid TLS certificate from the --url host only; sign-in endpoints are always verified" + )] + allow_invalid_certs: bool, + + #[arg(short = 'v', long, action = ArgAction::Count, conflicts_with = "quiet", help = "Increase log verbosity")] + verbose: u8, + + #[arg(short = 'q', long, help = "Quiet: warnings and errors only")] + quiet: bool, + + #[arg(value_name = "DIR", help = "Directory to write, new or empty")] + dir: PathBuf, +} + #[derive(Args)] struct InspectArgs { #[arg(value_name = "ARCHIVE", help = "Local SQLite archive")] @@ -483,6 +547,7 @@ pub enum Action { ImportExchangeGraph(CommonConfig, GraphImportConfig), Export(CommonConfig, ExportConfig), ExportFiles(ExportFilesConfig), + ExportDirectory(ExportDirectoryConfig), ExportImap(CommonConfig, ExportImapConfig), Inspect(InspectConfig), } @@ -610,6 +675,7 @@ impl Cli { Command::Import { source } => resolve_import(source), Command::Export(args) => resolve_export(*args), Command::ExportFiles(args) => resolve_export_files(*args), + Command::ExportDirectory(args) => resolve_export_directory(*args), Command::ExportImap(args) => resolve_export_imap(*args), Command::Inspect(args) => resolve_inspect(*args), } @@ -1018,6 +1084,24 @@ fn resolve_export_files(args: ExportFilesArgs) -> Result { })) } +fn resolve_export_directory(args: ExportDirectoryArgs) -> Result { + let auth = resolve_auth( + args.auth_basic.as_deref(), + args.auth_password.as_deref(), + args.auth_bearer.as_ref(), + )?; + Ok(Action::ExportDirectory(ExportDirectoryConfig { + url: args.url, + auth, + dir: args.dir, + domains: args.domain, + dry_run: args.dry_run, + max_retries: args.max_retries, + allow_invalid_certs: args.allow_invalid_certs, + logger: Logger::from_flags(args.quiet, args.verbose), + })) +} + fn resolve_inspect(args: InspectArgs) -> Result { let target = match args.target.as_deref() { Some(token) => Some(ObjectType::parse(token)?), diff --git a/src/export_directory/csv.rs b/src/export_directory/csv.rs new file mode 100644 index 0000000..be89a98 --- /dev/null +++ b/src/export_directory/csv.rs @@ -0,0 +1,88 @@ +/* + * SPDX-FileCopyrightText: 2026 Coffey Labs LLC + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + */ + +//! CSV as RFC 4180 writes it: CRLF line ends, and a field quoted when it +//! holds a comma, a quote or a line break. The text comes from the server, +//! and these files are meant to be opened in a spreadsheet, so a field that +//! one would run as a formula is written with a leading `'`. +//! `directory.json` keeps every value exactly. + +use std::fs; +use std::path::Path; + +use super::model::Table; + +pub fn field(value: &str) -> String { + let guarded; + let value = if value.starts_with(['=', '+', '-', '@', '\t', '\r']) { + guarded = format!("'{value}"); + guarded.as_str() + } else { + value + }; + if value.contains([',', '"', '\r', '\n']) { + format!("\"{}\"", value.replace('"', "\"\"")) + } else { + value.to_owned() + } +} + +pub fn render((header, rows): &Table) -> String { + let mut out = String::new(); + let mut line = |cells: &mut dyn Iterator| { + let cells: Vec = cells.map(field).collect(); + out.push_str(&cells.join(",")); + out.push_str("\r\n"); + }; + line(&mut header.iter().copied()); + for row in rows { + line(&mut row.iter().map(String::as_str)); + } + out +} + +pub fn write(path: &Path, table: &Table) -> std::io::Result<()> { + fs::write(path, render(table)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn plain_fields_are_left_alone() { + assert_eq!(field("alice@example.org"), "alice@example.org"); + assert_eq!(field("Alice Example"), "Alice Example"); + assert_eq!(field(""), ""); + } + + #[test] + fn commas_quotes_and_breaks_are_quoted() { + assert_eq!(field("Smith, Jane"), "\"Smith, Jane\""); + assert_eq!(field("say \"hi\""), "\"say \"\"hi\"\"\""); + assert_eq!(field("two\nlines"), "\"two\nlines\""); + } + + #[test] + fn formulas_are_not_left_to_run() { + assert_eq!(field("=HYPERLINK(\"x\")"), "\"'=HYPERLINK(\"\"x\"\")\""); + assert_eq!(field("+1"), "'+1"); + assert_eq!(field("-note"), "'-note"); + assert_eq!(field("@SUM(A1)"), "'@SUM(A1)"); + } + + #[test] + fn a_table_is_crlf_lines() { + let table: Table = ( + vec!["email", "description"], + vec![vec!["a@example.org".to_owned(), "x, y".to_owned()]], + ); + assert_eq!( + render(&table), + "email,description\r\na@example.org,\"x, y\"\r\n" + ); + } +} diff --git a/src/export_directory/mod.rs b/src/export_directory/mod.rs new file mode 100644 index 0000000..f642c83 --- /dev/null +++ b/src/export_directory/mod.rs @@ -0,0 +1,259 @@ +/* + * SPDX-FileCopyrightText: 2026 Coffey Labs LLC + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + */ + +//! `export-directory`: who has an address on a server, without their +//! secrets. Domains, users, groups, mailing lists, aliases, quotas, tenants +//! and roles are read from the registry with an administrator's +//! credentials and written as `directory.json` and CSV files, so the same +//! accounts can be set up on any server. Credentials, OTP seeds, API keys +//! and DKIM keys are never asked for, so they can't be written. + +use std::collections::HashSet; +use std::fs; +use std::path::{Path, PathBuf}; + +use serde_json::{Value, json}; +use time::OffsetDateTime; +use time::format_description::well_known::Rfc3339; + +use crate::error::Error; +use crate::export_files::{check_target, make_private_dir}; +use crate::jmap::error::JmapError; +use crate::jmap::http::{Auth, HttpClient, RetryPolicy}; +use crate::jmap::request::{Request, URN_INBUXA_REGISTRY, check_method_error}; +use crate::jmap::session::{Limits, Session}; +use crate::logging::Logger; +use crate::net::CertOverride; +use crate::sync::Summary; + +pub mod csv; +pub mod model; + +/// The layout this release writes; raised when `directory.json` or a CSV +/// changes in a way a reader has to know about. +pub const FORMAT_VERSION: u32 = 1; + +// Every property is named, so nothing the registry adds later, and none of +// `credentials`, `encryptionAtRest` or the DKIM keys, is ever fetched. +const DOMAIN_PROPERTIES: &[&str] = &[ + "name", + "aliases", + "isEnabled", + "description", + "memberTenantId", + "catchAllAddress", + "subAddressing", +]; +// `@type` tells a user from a group, and is only sent when asked for. +const ACCOUNT_PROPERTIES: &[&str] = &[ + "@type", + "name", + "domainId", + "description", + "memberGroupIds", + "memberTenantId", + "roles", + "permissions", + "quotas", + "aliases", + "externalId", + "locale", + "timeZone", +]; +const LIST_PROPERTIES: &[&str] = &[ + "name", + "domainId", + "description", + "aliases", + "memberTenantId", + "recipients", +]; +const TENANT_PROPERTIES: &[&str] = &["name", "quotas"]; +const ROLE_PROPERTIES: &[&str] = &[ + "description", + "memberTenantId", + "roleIds", + "enabledPermissions", + "disabledPermissions", +]; + +pub struct ExportDirectoryConfig { + pub url: String, + pub auth: Auth, + pub dir: PathBuf, + /// Domains to write, by name; empty for every domain. + pub domains: Vec, + pub dry_run: bool, + pub max_retries: u32, + pub allow_invalid_certs: bool, + pub logger: Logger, +} + +pub fn run(config: ExportDirectoryConfig) -> Result { + check_target(&config.dir)?; + let logger = config.logger; + let client = HttpClient::new( + config.auth.clone(), + RetryPolicy::new(config.max_retries), + CertOverride::for_url(config.allow_invalid_certs, &config.url), + ); + client.set_logger(logger); + let session = Session::discover(&client, &config.url)?; + for mismatch in session.origin_mismatches(&config.url) { + logger.warn(&mismatch.to_string()); + } + let limits = session.core_limits()?; + client.set_limits(&limits); + let (account_id, urn) = session.registry_account().ok_or_else(|| { + Error::Account(format!( + "this login can't read the server's registry ({URN_INBUXA_REGISTRY}); \ + export-directory needs an administrator's credentials" + )) + })?; + let reader = Reader { + client: &client, + api_url: &session.api_url, + account_id, + urn, + limits, + }; + let raw = model::Raw { + domains: reader.all("x:Domain", DOMAIN_PROPERTIES)?, + accounts: reader.all("x:Account", ACCOUNT_PROPERTIES)?, + lists: reader.all("x:MailingList", LIST_PROPERTIES)?, + tenants: reader.optional("x:Tenant", TENANT_PROPERTIES, logger)?, + roles: reader.optional("x:Role", ROLE_PROPERTIES, logger)?, + }; + let directory = model::build(&raw, &config.domains, logger)?; + let summary = directory.summary(); + + let dir = config.dir.as_path(); + if config.dry_run { + logger.warn(&format!( + "dry run: nothing was written; {} would hold what is counted below", + dir.display() + )); + return Ok(summary); + } + make_private_dir(dir)?; + write(&directory, dir, &config.url)?; + Ok(summary) +} + +fn write(directory: &model::Directory, dir: &Path, url: &str) -> Result<(), Error> { + csv::write(&dir.join("domains.csv"), &directory.domains_csv())?; + csv::write(&dir.join("users.csv"), &directory.users_csv())?; + csv::write(&dir.join("groups.csv"), &directory.groups_csv())?; + csv::write(&dir.join("lists.csv"), &directory.lists_csv())?; + csv::write(&dir.join("aliases.csv"), &directory.aliases_csv())?; + let document = json!({ + "format": "inbuxa-migrate-directory", + "formatVersion": FORMAT_VERSION, + "createdBy": format!("inbuxa-migrate {}", crate::version::shown()), + "created": OffsetDateTime::now_utc().format(&Rfc3339).unwrap_or_default(), + "source": { "url": url }, + "domains": directory.domains, + "tenants": directory.tenants, + "roles": directory.roles, + "users": directory.users, + "groups": directory.groups, + "lists": directory.lists, + }); + // Written last, so a directory with a directory.json is a finished one. + let text = serde_json::to_string_pretty(&document).map_err(std::io::Error::other)?; + fs::write(dir.join("directory.json"), text + "\n")?; + Ok(()) +} + +struct Reader<'a> { + client: &'a HttpClient, + api_url: &'a str, + account_id: &'a str, + urn: &'static str, + limits: Limits, +} + +impl Reader<'_> { + /// Every object of one registry type, with only `properties` (and `id`). + fn all(&self, ty: &str, properties: &[&str]) -> Result, JmapError> { + let ids = self.ids(ty)?; + let chunk = usize::try_from(self.limits.max_objects_in_get.max(1)).unwrap_or(500); + let mut out = Vec::with_capacity(ids.len()); + for ids in ids.chunks(chunk) { + let mut req = Request::new(); + req.registry(Some(self.urn)); + req.call( + format!("{ty}/get"), + json!({ "accountId": self.account_id, "ids": ids, "properties": properties }), + "g", + ); + let resp = req.send(self.client, self.api_url)?; + let mr = resp.first()?; + check_method_error(mr)?; + let list = mr + .args + .get("list") + .and_then(Value::as_array) + .ok_or_else(|| JmapError::malformed(format!("{ty}/get response has no list")))?; + out.extend(list.iter().cloned()); + } + Ok(out) + } + + /// As `all`, for a type this login may not be allowed to read, or a + /// server may not have: that is a warning and an empty list. + fn optional( + &self, + ty: &str, + properties: &[&str], + logger: Logger, + ) -> Result, JmapError> { + match self.all(ty, properties) { + Ok(list) => Ok(list), + Err(e @ (JmapError::Method { .. } | JmapError::UnknownMethod)) => { + logger.warn(&format!("{ty} can't be read here ({e}); none are written")); + Ok(Vec::new()) + } + Err(e) => Err(e), + } + } + + /// Ids page by page. A server that returns every id at once and ignores + /// `position` hands back nothing new on the second page, which ends it. + fn ids(&self, ty: &str) -> Result, JmapError> { + let mut ids = Vec::new(); + let mut seen = HashSet::new(); + loop { + let mut req = Request::new(); + req.registry(Some(self.urn)); + req.call( + format!("{ty}/query"), + json!({ "accountId": self.account_id, "position": ids.len() }), + "q", + ); + let resp = req.send(self.client, self.api_url)?; + let mr = resp.first()?; + check_method_error(mr)?; + let page = mr + .args + .get("ids") + .and_then(Value::as_array) + .ok_or_else(|| JmapError::malformed(format!("{ty}/query response has no ids")))?; + let before = ids.len(); + for id in page { + let id = id.as_str().ok_or_else(|| { + JmapError::malformed(format!("{ty}/query id is not a string")) + })?; + if seen.insert(id.to_owned()) { + ids.push(id.to_owned()); + } + } + if ids.len() == before { + return Ok(ids); + } + } + } +} diff --git a/src/export_directory/model.rs b/src/export_directory/model.rs new file mode 100644 index 0000000..a7feb92 --- /dev/null +++ b/src/export_directory/model.rs @@ -0,0 +1,783 @@ +/* + * SPDX-FileCopyrightText: 2026 Coffey Labs LLC + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + */ + +//! Registry objects as a directory any server can be set up from: ids +//! become addresses and names, and only the fields listed here are kept. + +use std::collections::{BTreeSet, HashMap, HashSet}; + +use serde_json::{Map, Value, json}; + +use crate::error::Error; +use crate::logging::Logger; +use crate::sync::{Summary, TypeCounts}; + +/// What the registry returned, one list per type. +#[derive(Default)] +pub struct Raw { + pub domains: Vec, + pub accounts: Vec, + pub lists: Vec, + pub tenants: Vec, + pub roles: Vec, +} + +/// The directory as `directory.json` holds it, each list sorted by name or +/// address. +#[derive(Debug, Default)] +pub struct Directory { + pub domains: Vec, + pub tenants: Vec, + pub roles: Vec, + pub users: Vec, + pub groups: Vec, + pub lists: Vec, + /// Objects left out because they point at something that isn't there. + skipped: HashMap<&'static str, u64>, +} + +fn str_of<'a>(v: &'a Value, key: &str) -> Option<&'a str> { + v.get(key).and_then(Value::as_str) +} + +/// A registry set: `{"a": true, "b": true}` (or a plain array), sorted. +fn set_of(v: &Value, key: &str) -> Vec { + let set: BTreeSet = match v.get(key) { + Some(Value::Object(map)) => map + .iter() + .filter(|(_, on)| on.as_bool() != Some(false)) + .map(|(k, _)| k.clone()) + .collect(), + Some(Value::Array(items)) => items + .iter() + .filter_map(Value::as_str) + .map(str::to_owned) + .collect(), + _ => BTreeSet::new(), + }; + set.into_iter().collect() +} + +/// A registry list: `{"0": {...}, "1": {...}}` (or a plain array), in order. +fn list_of<'a>(v: &'a Value, key: &str) -> Vec<&'a Value> { + match v.get(key) { + Some(Value::Object(map)) => { + let mut items: Vec<(u64, &Value)> = map + .iter() + .map(|(k, item)| (k.parse().unwrap_or(u64::MAX), item)) + .collect(); + items.sort_by_key(|(k, _)| *k); + items.into_iter().map(|(_, item)| item).collect() + } + Some(Value::Array(items)) => items.iter().collect(), + _ => Vec::new(), + } +} + +fn opt(v: Option<&str>) -> Value { + v.map_or(Value::Null, |s| Value::String(s.to_owned())) +} + +/// Quotas are counts and byte sizes by name, `maxDiskQuota` and the rest. +fn quotas_of(v: &Value) -> Value { + let map: Map = v + .get("quotas") + .and_then(Value::as_object) + .map(|q| { + q.iter() + .filter(|(_, n)| n.is_u64()) + .map(|(k, n)| (k.clone(), n.clone())) + .collect() + }) + .unwrap_or_default(); + Value::Object(map) +} + +fn type_of(v: &Value) -> &str { + str_of(v, "@type").unwrap_or_default() +} + +struct Names { + domains: HashMap, + tenants: HashMap, + roles: HashMap, +} + +impl Names { + fn domain(&self, id: Option<&str>) -> Option<&str> { + id.and_then(|id| self.domains.get(id)).map(String::as_str) + } + + fn tenant(&self, v: &Value) -> Value { + opt(str_of(v, "memberTenantId").and_then(|id| self.tenants.get(id).map(String::as_str))) + } + + fn roles(&self, ids: &[String]) -> Vec { + ids.iter() + .filter_map(|id| self.roles.get(id).cloned()) + .collect() + } + + /// `name@domain`; a name that is already an address is kept. + fn address(&self, v: &Value) -> Option { + let name = str_of(v, "name")?; + if name.contains('@') { + return Some(name.to_owned()); + } + Some(format!("{name}@{}", self.domain(str_of(v, "domainId"))?)) + } + + fn aliases(&self, v: &Value) -> Vec { + let mut out: Vec = list_of(v, "aliases") + .into_iter() + .filter_map(|a| { + Some(json!({ + "email": self.address(a)?, + "enabled": a.get("enabled").and_then(Value::as_bool).unwrap_or(true), + "description": opt(str_of(a, "description")), + })) + }) + .collect(); + out.sort_by(|a, b| str_of(a, "email").cmp(&str_of(b, "email"))); + out + } + + /// `"user"`, `"admin"` or `"default"`, or the custom roles by name. + fn role_of(&self, v: &Value) -> Value { + let roles = v.get("roles").unwrap_or(&Value::Null); + match type_of(roles) { + "Custom" => json!({ "custom": self.roles(&set_of(roles, "roleIds")) }), + "" => Value::Null, + other => Value::String(other.to_ascii_lowercase()), + } + } +} + +/// What an account may do beyond its roles: nothing for `Inherit`, else +/// the permissions it adds (`merge`) or has instead (`replace`). +fn permissions_of(v: &Value) -> Value { + let Some(p) = v.get("permissions") else { + return Value::Null; + }; + match type_of(p) { + "Merge" | "Replace" => json!({ + "mode": type_of(p).to_ascii_lowercase(), + "enabled": set_of(p, "enabledPermissions"), + "disabled": set_of(p, "disabledPermissions"), + }), + _ => Value::Null, + } +} + +fn sub_addressing_of(v: &Value) -> Value { + let Some(s) = v.get("subAddressing") else { + return Value::Null; + }; + match type_of(s) { + "Custom" => json!({ "customRule": s.get("customRule").cloned().unwrap_or(Value::Null) }), + "" => Value::Null, + other => Value::String(other.to_ascii_lowercase()), + } +} + +fn id_of(v: &Value) -> Option<&str> { + str_of(v, "id") +} + +pub fn build(raw: &Raw, only: &[String], logger: Logger) -> Result { + let by_id = |list: &[Value], key: &str| -> HashMap { + list.iter() + .filter_map(|v| Some((id_of(v)?.to_owned(), str_of(v, key)?.to_owned()))) + .collect() + }; + let names = Names { + domains: by_id(&raw.domains, "name"), + tenants: by_id(&raw.tenants, "name"), + roles: by_id(&raw.roles, "description"), + }; + + let only: HashSet = only.iter().map(|d| d.to_ascii_lowercase()).collect(); + for wanted in &only { + if !names + .domains + .values() + .any(|d| d.eq_ignore_ascii_case(wanted)) + { + return Err(Error::Usage(format!("the server has no domain {wanted}"))); + } + } + let selected = |v: &Value| -> bool { + only.is_empty() + || names + .domain(str_of(v, "domainId")) + .is_some_and(|d| only.contains(&d.to_ascii_lowercase())) + }; + + let mut dir = Directory::default(); + let mut tenant_ids: HashSet = HashSet::new(); + let mut role_ids: Vec = Vec::new(); + let mut note_refs = |v: &Value| { + if let Some(t) = str_of(v, "memberTenantId") { + tenant_ids.insert(t.to_owned()); + } + if let Some(r) = v.get("roles") { + role_ids.extend(set_of(r, "roleIds")); + } + }; + + for d in &raw.domains { + let Some(name) = str_of(d, "name") else { + continue; + }; + if !only.is_empty() && !only.contains(&name.to_ascii_lowercase()) { + continue; + } + note_refs(d); + dir.domains.push(json!({ + "name": name, + "enabled": d.get("isEnabled").and_then(Value::as_bool).unwrap_or(true), + "description": opt(str_of(d, "description")), + "aliases": set_of(d, "aliases"), + "catchAllAddress": opt(str_of(d, "catchAllAddress")), + "subAddressing": sub_addressing_of(d), + "tenant": names.tenant(d), + })); + } + + // Groups are named by address everywhere, so every account's address is + // known before any is written, whichever domains are selected. + let mut address_of: HashMap = HashMap::new(); + for a in &raw.accounts { + match (id_of(a), names.address(a)) { + (Some(id), Some(email)) => { + address_of.insert(id.to_owned(), email); + } + _ => { + logger.warn(&format!( + "account {} has no domain this login can see; left out", + str_of(a, "name").unwrap_or("(unnamed)") + )); + *dir.skipped.entry(account_kind(a)).or_default() += 1; + } + } + } + let mut members: HashMap<&str, BTreeSet<&str>> = HashMap::new(); + for a in raw.accounts.iter().filter(|a| type_of(a) == "User") { + let Some(email) = id_of(a).and_then(|id| address_of.get(id)) else { + continue; + }; + for group in set_of(a, "memberGroupIds") { + if let Some(group_email) = address_of.get(&group) { + members + .entry(group_email.as_str()) + .or_default() + .insert(email.as_str()); + } + } + } + + for a in &raw.accounts { + let Some(email) = id_of(a).and_then(|id| address_of.get(id)) else { + continue; + }; + if !selected(a) { + continue; + } + note_refs(a); + let mut entry = json!({ + "email": email, + "name": str_of(a, "name"), + "domain": names.domain(str_of(a, "domainId")), + "description": opt(str_of(a, "description")), + "roles": names.role_of(a), + "permissions": permissions_of(a), + "aliases": names.aliases(a), + "quotas": quotas_of(a), + "tenant": names.tenant(a), + "locale": opt(str_of(a, "locale")), + "timeZone": opt(str_of(a, "timeZone")), + "externalId": opt(str_of(a, "externalId")), + }); + match type_of(a) { + "Group" => { + let list: Vec<&str> = members + .get(email.as_str()) + .map(|m| m.iter().copied().collect()) + .unwrap_or_default(); + entry["members"] = json!(list); + dir.groups.push(entry); + } + _ => { + let mut groups: Vec<&String> = set_of(a, "memberGroupIds") + .iter() + .filter_map(|g| address_of.get(g)) + .collect(); + groups.sort(); + entry["groups"] = json!(groups); + dir.users.push(entry); + } + } + } + + for l in &raw.lists { + if !selected(l) { + continue; + } + let Some(email) = names.address(l) else { + *dir.skipped.entry("mailinglist").or_default() += 1; + continue; + }; + note_refs(l); + dir.lists.push(json!({ + "email": email, + "name": str_of(l, "name"), + "domain": names.domain(str_of(l, "domainId")), + "description": opt(str_of(l, "description")), + "recipients": set_of(l, "recipients"), + "aliases": names.aliases(l), + "tenant": names.tenant(l), + })); + } + + // Roles the written accounts hold, and the roles those build on. + let roles_by_id: HashMap<&str, &Value> = raw + .roles + .iter() + .filter_map(|r| Some((id_of(r)?, r))) + .collect(); + let mut kept_roles: HashSet = HashSet::new(); + while let Some(id) = role_ids.pop() { + if !kept_roles.insert(id.clone()) { + continue; + } + if let Some(r) = roles_by_id.get(id.as_str()) { + role_ids.extend(set_of(r, "roleIds")); + } + } + for r in &raw.roles { + let Some(id) = id_of(r) else { + continue; + }; + if !only.is_empty() && !kept_roles.contains(id) { + continue; + } + if let Some(t) = str_of(r, "memberTenantId") { + tenant_ids.insert(t.to_owned()); + } + dir.roles.push(json!({ + "name": str_of(r, "description"), + "tenant": names.tenant(r), + "roles": names.roles(&set_of(r, "roleIds")), + "enabledPermissions": set_of(r, "enabledPermissions"), + "disabledPermissions": set_of(r, "disabledPermissions"), + })); + } + for t in &raw.tenants { + let Some(id) = id_of(t) else { + continue; + }; + if !only.is_empty() && !tenant_ids.contains(id) { + continue; + } + dir.tenants.push(json!({ + "name": str_of(t, "name"), + "quotas": quotas_of(t), + })); + } + + let by = |key: &'static str| { + move |a: &Value, b: &Value| { + let k = |v: &Value| str_of(v, key).map(str::to_lowercase); + k(a).cmp(&k(b)) + } + }; + dir.domains.sort_by(by("name")); + dir.tenants.sort_by(by("name")); + dir.roles.sort_by(by("name")); + dir.users.sort_by(by("email")); + dir.groups.sort_by(by("email")); + dir.lists.sort_by(by("email")); + Ok(dir) +} + +fn account_kind(v: &Value) -> &'static str { + if type_of(v) == "Group" { + "group" + } else { + "user" + } +} + +/// Cells holding more than one value join them with `;`. +fn joined<'a>(values: impl IntoIterator) -> String { + values.into_iter().collect::>().join(";") +} + +fn cell(v: &Value, key: &str) -> String { + match v.get(key) { + Some(Value::String(s)) => s.clone(), + Some(Value::Bool(b)) => b.to_string(), + Some(Value::Number(n)) => n.to_string(), + Some(Value::Array(items)) => joined(items.iter().filter_map(Value::as_str)), + _ => String::new(), + } +} + +fn roles_cell(v: &Value) -> String { + match v.get("roles") { + Some(Value::String(s)) => s.clone(), + Some(r) => cell(r, "custom"), + None => String::new(), + } +} + +fn alias_emails(v: &Value) -> String { + joined( + v.get("aliases") + .and_then(Value::as_array) + .into_iter() + .flatten() + .filter_map(|a| str_of(a, "email")), + ) +} + +fn disk_quota(v: &Value) -> String { + v.get("quotas") + .and_then(|q| q.get("maxDiskQuota")) + .map(Value::to_string) + .unwrap_or_default() +} + +/// One CSV file: its header, then its rows. +pub type Table = (Vec<&'static str>, Vec>); + +impl Directory { + pub fn summary(&self) -> Summary { + let mut summary = Summary::default(); + for (ty, list) in [ + ("domain", &self.domains), + ("tenant", &self.tenants), + ("role", &self.roles), + ("user", &self.users), + ("group", &self.groups), + ("mailinglist", &self.lists), + ] { + summary.per_type.push(( + ty, + TypeCounts { + created: list.len() as u64, + skipped: self.skipped.get(ty).copied().unwrap_or(0), + ..TypeCounts::default() + }, + )); + } + summary + } + + pub fn domains_csv(&self) -> Table { + let header = vec![ + "name", + "enabled", + "description", + "aliases", + "catch_all", + "sub_addressing", + "tenant", + ]; + let rows = self + .domains + .iter() + .map(|d| { + let sub = match d.get("subAddressing") { + Some(Value::String(s)) => s.clone(), + Some(Value::Object(_)) => "custom".to_owned(), + _ => String::new(), + }; + vec![ + cell(d, "name"), + cell(d, "enabled"), + cell(d, "description"), + cell(d, "aliases"), + cell(d, "catchAllAddress"), + sub, + cell(d, "tenant"), + ] + }) + .collect(); + (header, rows) + } + + pub fn users_csv(&self) -> Table { + let header = vec![ + "email", + "name", + "domain", + "description", + "roles", + "groups", + "aliases", + "disk_quota", + "tenant", + "locale", + "time_zone", + "external_id", + ]; + let rows = self + .users + .iter() + .map(|u| { + vec![ + cell(u, "email"), + cell(u, "name"), + cell(u, "domain"), + cell(u, "description"), + roles_cell(u), + cell(u, "groups"), + alias_emails(u), + disk_quota(u), + cell(u, "tenant"), + cell(u, "locale"), + cell(u, "timeZone"), + cell(u, "externalId"), + ] + }) + .collect(); + (header, rows) + } + + pub fn groups_csv(&self) -> Table { + let header = vec![ + "email", + "name", + "domain", + "description", + "members", + "aliases", + "disk_quota", + "tenant", + ]; + let rows = self + .groups + .iter() + .map(|g| { + vec![ + cell(g, "email"), + cell(g, "name"), + cell(g, "domain"), + cell(g, "description"), + cell(g, "members"), + alias_emails(g), + disk_quota(g), + cell(g, "tenant"), + ] + }) + .collect(); + (header, rows) + } + + pub fn lists_csv(&self) -> Table { + let header = vec![ + "email", + "name", + "domain", + "description", + "recipients", + "aliases", + "tenant", + ]; + let rows = self + .lists + .iter() + .map(|l| { + vec![ + cell(l, "email"), + cell(l, "name"), + cell(l, "domain"), + cell(l, "description"), + cell(l, "recipients"), + alias_emails(l), + cell(l, "tenant"), + ] + }) + .collect(); + (header, rows) + } + + /// Every address alias with what it delivers to, one row each. + pub fn aliases_csv(&self) -> Table { + let header = vec!["alias", "target", "kind", "enabled"]; + let mut rows = Vec::new(); + for (kind, list) in [ + ("user", &self.users), + ("group", &self.groups), + ("list", &self.lists), + ] { + for owner in list { + for alias in owner + .get("aliases") + .and_then(Value::as_array) + .into_iter() + .flatten() + { + rows.push(vec![ + cell(alias, "email"), + cell(owner, "email"), + kind.to_owned(), + cell(alias, "enabled"), + ]); + } + } + } + rows.sort(); + (header, rows) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn raw() -> Raw { + Raw { + domains: vec![ + json!({"id": "b", "name": "example.org", "aliases": {"example.net": true}, + "isEnabled": true, "catchAllAddress": "info@example.org", + "subAddressing": {"@type": "Enabled"}, "memberTenantId": "t"}), + json!({"id": "c", "name": "other.test", "aliases": {}, "isEnabled": false, + "subAddressing": {"@type": "Disabled"}}), + ], + accounts: vec![ + json!({"id": "b", "@type": "Group", "name": "sales", "domainId": "b", + "roles": {"@type": "Default"}, "permissions": {"@type": "Inherit"}, + "quotas": {}, "aliases": {}}), + json!({"id": "c", "@type": "User", "name": "alice", "domainId": "b", + "memberGroupIds": {"b": true}, "memberTenantId": "t", + "roles": {"@type": "Custom", "roleIds": {"r": true}}, + "permissions": {"@type": "Merge", "enabledPermissions": {"emailSend": true}, + "disabledPermissions": {}}, + "quotas": {"maxDiskQuota": 1073741824}, + "aliases": {"0": {"enabled": true, "name": "a.example", "domainId": "b"}, + "1": {"enabled": false, "name": "alice", "domainId": "c"}}, + "locale": "en-US", "timeZone": "Europe/Paris"}), + json!({"id": "d", "@type": "User", "name": "dave", "domainId": "c", + "memberGroupIds": {"b": true}, "roles": {"@type": "Admin"}, + "permissions": {"@type": "Inherit"}, "quotas": {}, "aliases": {}}), + json!({"id": "e", "@type": "User", "name": "ghost", "domainId": "zz"}), + ], + lists: vec![json!({"id": "b", "name": "all", "domainId": "b", + "recipients": {"carol@elsewhere.test": true, "alice@example.org": true}, + "aliases": {}})], + tenants: vec![ + json!({"id": "t", "name": "acme", "quotas": {"maxAccounts": 50}}), + json!({"id": "u", "name": "unused", "quotas": {}}), + ], + roles: vec![ + json!({"id": "r", "description": "Helpdesk", "roleIds": {"s": true}, + "enabledPermissions": {"impersonate": true}, "disabledPermissions": {}}), + json!({"id": "s", "description": "Reader", "roleIds": {"r": true}, + "enabledPermissions": {}, "disabledPermissions": {}}), + json!({"id": "x", "description": "Unused", "roleIds": {}}), + ], + } + } + + fn emails(list: &[Value]) -> Vec<&str> { + list.iter().filter_map(|v| str_of(v, "email")).collect() + } + + #[test] + fn ids_become_addresses_and_names() { + let dir = build(&raw(), &[], Logger::new(0)).unwrap(); + assert_eq!(emails(&dir.users), ["alice@example.org", "dave@other.test"]); + let alice = &dir.users[0]; + assert_eq!(alice["groups"], json!(["sales@example.org"])); + assert_eq!(alice["roles"], json!({"custom": ["Helpdesk"]})); + assert_eq!(alice["tenant"], "acme"); + assert_eq!(alice["quotas"], json!({"maxDiskQuota": 1073741824})); + assert_eq!( + alice["permissions"], + json!({"mode": "merge", "enabled": ["emailSend"], "disabled": []}) + ); + assert_eq!( + alice["aliases"], + json!([ + {"email": "a.example@example.org", "enabled": true, "description": null}, + {"email": "alice@other.test", "enabled": false, "description": null}, + ]) + ); + assert_eq!(dir.users[1]["roles"], "admin"); + assert_eq!( + dir.groups[0]["members"], + json!(["alice@example.org", "dave@other.test"]) + ); + assert_eq!( + dir.lists[0]["recipients"], + json!(["alice@example.org", "carol@elsewhere.test"]) + ); + assert_eq!(dir.domains[0]["aliases"], json!(["example.net"])); + assert_eq!(dir.domains[0]["subAddressing"], "enabled"); + assert_eq!(dir.domains[1]["enabled"], false); + assert_eq!(dir.tenants.len(), 2); + assert_eq!(dir.roles.len(), 3); + } + + #[test] + fn an_account_on_an_unknown_domain_is_counted_and_left_out() { + let dir = build(&raw(), &[], Logger::new(0)).unwrap(); + let summary = dir.summary(); + let users = &summary + .per_type + .iter() + .find(|(t, _)| *t == "user") + .unwrap() + .1; + assert_eq!((users.created, users.skipped), (2, 1)); + } + + #[test] + fn one_domain_keeps_only_what_it_uses() { + let dir = build(&raw(), &["Example.org".to_owned()], Logger::new(0)).unwrap(); + assert_eq!(dir.domains.len(), 1); + assert_eq!(emails(&dir.users), ["alice@example.org"]); + // Members elsewhere are still named by address. + assert_eq!( + dir.groups[0]["members"], + json!(["alice@example.org", "dave@other.test"]) + ); + let roles: Vec<&str> = dir.roles.iter().filter_map(|r| str_of(r, "name")).collect(); + assert_eq!(roles, ["Helpdesk", "Reader"]); + let tenants: Vec<&str> = dir + .tenants + .iter() + .filter_map(|t| str_of(t, "name")) + .collect(); + assert_eq!(tenants, ["acme"]); + } + + #[test] + fn an_unknown_domain_is_a_usage_error() { + let err = build(&raw(), &["nope.test".to_owned()], Logger::new(0)).unwrap_err(); + assert!(matches!(err, Error::Usage(_)), "{err:?}"); + } + + #[test] + fn csv_rows_follow_their_headers() { + let dir = build(&raw(), &[], Logger::new(0)).unwrap(); + let (header, rows) = dir.users_csv(); + assert_eq!(rows[0].len(), header.len()); + assert_eq!(rows[0][4], "Helpdesk"); + assert_eq!(rows[0][6], "a.example@example.org;alice@other.test"); + assert_eq!(rows[0][7], "1073741824"); + let (_, aliases) = dir.aliases_csv(); + assert_eq!( + aliases[0], + ["a.example@example.org", "alice@example.org", "user", "true"] + ); + let (_, domains) = dir.domains_csv(); + assert_eq!(domains[0][3], "example.net"); + assert_eq!(domains[0][4], "info@example.org"); + } +} diff --git a/src/export_files/mod.rs b/src/export_files/mod.rs index 0ad2ad1..c40e3f5 100644 --- a/src/export_files/mod.rs +++ b/src/export_files/mod.rs @@ -128,7 +128,7 @@ pub fn run(config: ExportFilesConfig) -> Result { /// Only a new or empty directory: what is written must be exactly the /// archive, with nothing of an earlier run mixed in. -fn check_target(dir: &Path) -> Result<(), Error> { +pub(crate) fn check_target(dir: &Path) -> Result<(), Error> { match fs::read_dir(dir) { Ok(mut entries) => { if entries.next().is_some() { @@ -150,7 +150,7 @@ fn check_target(dir: &Path) -> Result<(), Error> { /// Mail, contacts and calendars are as private as the archive they came /// from. -fn make_private_dir(dir: &Path) -> Result<(), Error> { +pub(crate) fn make_private_dir(dir: &Path) -> Result<(), Error> { fs::create_dir_all(dir)?; #[cfg(unix)] { diff --git a/src/jmap/session.rs b/src/jmap/session.rs index 9a1ee79..324197d 100644 --- a/src/jmap/session.rs +++ b/src/jmap/session.rs @@ -243,6 +243,25 @@ impl Session { .find(|urn| self.capabilities.contains_key(*urn)) } + /// The account to send registry (`x:`) calls to, with the capability + /// to name for them. Servers advertise the registry per account, on the + /// accounts whose credentials may administer it; a session-wide one is + /// taken with the first account when no account lists it. + pub fn registry_account(&self) -> Option<(&str, &'static str)> { + for urn in [URN_INBUXA_REGISTRY, URN_STALWART_REGISTRY] { + if let Some((id, _)) = self + .accounts + .iter() + .find(|(_, a)| a.account_capabilities.contains_key(urn)) + { + return Some((id.as_str(), urn)); + } + } + let urn = self.registry_urn()?; + let (id, _) = self.accounts.first()?; + Some((id.as_str(), urn)) + } + pub fn account(&self, account_id: &str) -> Option<&Account> { self.accounts.get(account_id) } @@ -353,6 +372,26 @@ mod tests { ); } + #[test] + fn registry_account_is_the_account_that_lists_it() { + let mut v: serde_json::Value = serde_json::from_str(raw_session()).unwrap(); + v["accounts"]["a"] = serde_json::json!({ + "name": "admin", + "accountCapabilities": { URN_INBUXA_REGISTRY: {} } + }); + let session: Session = serde_json::from_value(v).unwrap(); + assert_eq!(session.registry_account(), Some(("a", URN_INBUXA_REGISTRY))); + } + + #[test] + fn registry_account_falls_back_to_a_session_capability() { + assert_eq!( + session_with(&[URN_STALWART_REGISTRY]).registry_account(), + Some(("w", URN_STALWART_REGISTRY)) + ); + assert_eq!(session_with(&[]).registry_account(), None); + } + #[test] fn registry_urn_is_none_without_either() { assert_eq!(session_with(&[]).registry_urn(), None); diff --git a/src/lib.rs b/src/lib.rs index d68c320..199866a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ pub mod error; pub mod exchange; pub mod exchange_ews; pub mod exchange_graph; +pub mod export_directory; pub mod export_files; pub mod imap; pub mod inspect; diff --git a/src/main.rs b/src/main.rs index 735945b..90f45d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use clap::Parser; use inbuxa_migrate::cli::{Action, Cli}; use inbuxa_migrate::error::Error; use inbuxa_migrate::sync::{self, RunOutcome, Summary}; -use inbuxa_migrate::{export_files, inspect}; +use inbuxa_migrate::{export_directory, export_files, inspect}; fn main() { let code = run(); @@ -87,6 +87,13 @@ fn run() -> i32 { let logger = config.logger; (RunOutcome::from_result(export_files::run(config)), logger) } + Action::ExportDirectory(config) => { + let logger = config.logger; + ( + RunOutcome::from_result(export_directory::run(config)), + logger, + ) + } Action::ExportImap(common, config) => { let logger = common.logger; ( diff --git a/tests/mock_export_directory.rs b/tests/mock_export_directory.rs new file mode 100644 index 0000000..093763a --- /dev/null +++ b/tests/mock_export_directory.rs @@ -0,0 +1,273 @@ +/* + * SPDX-FileCopyrightText: 2026 Coffey Labs LLC + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + */ + +//! `export-directory` against a registry served by a mock: what is asked +//! for, what is written, and that no secret reaches the files even from a +//! server that sends one unasked. + +use std::collections::BTreeSet; +use std::fs; +use std::path::PathBuf; +use std::sync::{Arc, Mutex}; + +use inbuxa_migrate::export_directory::{self, ExportDirectoryConfig}; +use inbuxa_migrate::jmap::http::Auth; +use inbuxa_migrate::logging::Logger; +use serde_json::{Value, json}; + +const SECRET: &str = "hunter2-never-written"; + +fn objects(ty: &str) -> Vec { + match ty { + "x:Domain" => vec![ + json!({"id": "b", "name": "example.org", "aliases": {"example.net": true}, + "isEnabled": true, "subAddressing": {"@type": "Enabled"}, + "dkimManagement": {"@type": "Automatic"}, "dnsZoneFile": "example.org. IN MX 10 mx."}), + ], + "x:Account" => vec![ + json!({"id": "b", "@type": "Group", "name": "sales", "domainId": "b", + "roles": {"@type": "Default"}, "quotas": {}, "aliases": {}}), + json!({"id": "c", "@type": "User", "name": "alice", "domainId": "b", + "description": "Smith, Alice", "memberGroupIds": {"b": true}, + "roles": {"@type": "User"}, "quotas": {"maxDiskQuota": 5}, + "aliases": {"0": {"enabled": true, "name": "a", "domainId": "b"}}, + "credentials": {"0": {"@type": "Password", "secret": SECRET}}, + "encryptionAtRest": {"@type": "Aes256", "publicKey": SECRET}}), + ], + "x:MailingList" => vec![json!({"id": "b", "name": "all", "domainId": "b", + "description": "=cmd|' /C calc'!A0", "recipients": {"alice@example.org": true}})], + _ => Vec::new(), + } +} + +fn session(base: &str) -> String { + json!({ + "apiUrl": format!("{base}/jmap/api"), + "uploadUrl": format!("{base}/jmap/upload/{{accountId}}/"), + "downloadUrl": format!("{base}/jmap/dl/{{accountId}}/{{blobId}}/{{type}}/{{name}}"), + "capabilities": { + "urn:ietf:params:jmap:core": { + "maxObjectsInGet": 1, "maxObjectsInSet": 500, "maxCallsInRequest": 16, + "maxConcurrentRequests": 4, "maxConcurrentUpload": 4, + "maxSizeRequest": 10000000, "maxSizeUpload": 50000000 + } + }, + "accounts": { + "self": { "name": "admin", "accountCapabilities": {} }, + "adm": { "name": "admin", "accountCapabilities": { "urn:inbuxa:jmap:registry": {} } } + } + }) + .to_string() +} + +/// Each get as (type, the properties it asked for). +type Asked = Arc)>>>; + +/// A registry that answers `/query` and `/get` from `objects`, records the +/// properties each get asked for, and with `honor_properties` off sends +/// every field whatever was asked. +struct Registry { + server: mockito::ServerGuard, + asked: Asked, +} + +fn registry(honor_properties: bool) -> Registry { + let mut server = mockito::Server::new(); + let base = server.url(); + server + .mock("GET", "/jmap/session") + .with_status(200) + .with_body(session(&base)) + .create(); + let asked: Asked = Arc::new(Mutex::new(Vec::new())); + let log = asked.clone(); + server + .mock("POST", "/jmap/api") + .with_status(200) + .with_body_from_request(move |req| { + let body: Value = serde_json::from_slice(req.body().unwrap()).unwrap(); + assert!( + body["using"] + .as_array() + .unwrap() + .contains(&json!("urn:inbuxa:jmap:registry")), + "{body}" + ); + let call = &body["methodCalls"][0]; + let (method, args) = (call[0].as_str().unwrap(), &call[1]); + assert_eq!(args["accountId"], "adm"); + let (ty, verb) = method.split_once('/').unwrap(); + let all = objects(ty); + let response = match verb { + "query" => { + let from = args["position"].as_u64().unwrap_or(0) as usize; + let ids: Vec<&Value> = all.iter().skip(from).map(|o| &o["id"]).collect(); + json!({"accountId": "adm", "ids": ids, "position": from}) + } + "get" => { + let props: Vec = args["properties"] + .as_array() + .unwrap() + .iter() + .map(|p| p.as_str().unwrap().to_owned()) + .collect(); + log.lock().unwrap().push((ty.to_owned(), props.clone())); + let ids = args["ids"].as_array().unwrap(); + let list: Vec = all + .into_iter() + .filter(|o| ids.contains(&o["id"])) + .map(|o| { + if !honor_properties { + return o; + } + let mut kept = serde_json::Map::new(); + for (k, v) in o.as_object().unwrap() { + if k == "id" || props.contains(k) { + kept.insert(k.clone(), v.clone()); + } + } + Value::Object(kept) + }) + .collect(); + json!({"accountId": "adm", "list": list, "notFound": []}) + } + other => panic!("unexpected {other}"), + }; + json!({"methodResponses": [[method, response, call[2]]]}) + .to_string() + .into_bytes() + }) + .create(); + Registry { server, asked } +} + +fn out_dir(name: &str) -> PathBuf { + let dir = + std::env::temp_dir().join(format!("inbuxa-migrate-dir-{name}-{}", std::process::id())); + let _ = fs::remove_dir_all(&dir); + dir +} + +fn config(registry: &Registry, dir: &std::path::Path) -> ExportDirectoryConfig { + ExportDirectoryConfig { + url: format!("{}/jmap/session", registry.server.url()), + auth: Auth::Basic { + user: "admin".into(), + password: "p".into(), + }, + dir: dir.to_path_buf(), + domains: Vec::new(), + dry_run: false, + max_retries: 0, + allow_invalid_certs: false, + logger: Logger::new(0), + } +} + +fn every_file(dir: &std::path::Path) -> String { + let mut text = String::new(); + for entry in fs::read_dir(dir).unwrap() { + text.push_str(&fs::read_to_string(entry.unwrap().path()).unwrap()); + } + text +} + +#[test] +fn the_directory_is_written_without_asking_for_secrets() { + let registry = registry(true); + let dir = out_dir("asked"); + export_directory::run(config(®istry, &dir)).expect("export"); + + let asked = registry.asked.lock().unwrap(); + let types: BTreeSet<&str> = asked.iter().map(|(t, _)| t.as_str()).collect(); + assert_eq!( + types, + BTreeSet::from(["x:Account", "x:Domain", "x:MailingList"]) + ); + // maxObjectsInGet is 1, so the two accounts take two gets. + assert_eq!(asked.iter().filter(|(t, _)| t == "x:Account").count(), 2); + for (_, props) in asked.iter() { + for never in [ + "credentials", + "encryptionAtRest", + "dkimManagement", + "dnsZoneFile", + ] { + assert!(!props.iter().any(|p| p == never), "{props:?}"); + } + } + + let names: BTreeSet = fs::read_dir(&dir) + .unwrap() + .map(|e| e.unwrap().file_name().into_string().unwrap()) + .collect(); + assert_eq!( + names, + BTreeSet::from( + [ + "aliases.csv", + "directory.json", + "domains.csv", + "groups.csv", + "lists.csv", + "users.csv", + ] + .map(str::to_owned) + ) + ); + let doc: Value = + serde_json::from_str(&fs::read_to_string(dir.join("directory.json")).unwrap()).unwrap(); + assert_eq!(doc["format"], "inbuxa-migrate-directory"); + assert_eq!(doc["users"][0]["email"], "alice@example.org"); + assert_eq!(doc["users"][0]["groups"], json!(["sales@example.org"])); + assert_eq!(doc["groups"][0]["members"], json!(["alice@example.org"])); + assert_eq!(doc["lists"][0]["description"], "=cmd|' /C calc'!A0"); + + let users = fs::read_to_string(dir.join("users.csv")).unwrap(); + assert!( + users.contains("alice@example.org,alice,example.org,\"Smith, Alice\",user,sales@example.org,a@example.org,5,"), + "{users}" + ); + let lists = fs::read_to_string(dir.join("lists.csv")).unwrap(); + assert!(lists.contains(",'=cmd|' /C calc'!A0,"), "{lists}"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = fs::metadata(&dir).unwrap().permissions().mode(); + assert_eq!(mode & 0o777, 0o700); + } + fs::remove_dir_all(&dir).unwrap(); +} + +#[test] +fn a_secret_sent_unasked_is_still_not_written() { + let registry = registry(false); + let dir = out_dir("unasked"); + export_directory::run(config(®istry, &dir)).expect("export"); + let text = every_file(&dir); + assert!(text.contains("alice@example.org")); + assert!(!text.contains(SECRET)); + assert!(!text.contains("credentials")); + assert!(!text.contains("IN MX")); + fs::remove_dir_all(&dir).unwrap(); +} + +#[test] +fn a_dry_run_writes_nothing() { + let registry = registry(true); + let dir = out_dir("dry"); + let mut config = config(®istry, &dir); + config.dry_run = true; + let summary = export_directory::run(config).expect("export"); + let users = &summary + .per_type + .iter() + .find(|(t, _)| *t == "user") + .unwrap() + .1; + assert_eq!(users.created, 1); + assert!(!dir.exists()); +} -- 2.54.0