From 82a0cd2ad435e0ec44ca585c0025915402665026 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Mon, 28 Sep 2020 17:40:12 +0200 Subject: [PATCH] proxmox_backup_client key: add new paper-key command --- debian/control.in | 2 +- src/bin/proxmox_backup_client/key.rs | 67 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/debian/control.in b/debian/control.in index 8e2312d3..2e6d82ff 100644 --- a/debian/control.in +++ b/debian/control.in @@ -19,7 +19,7 @@ Description: Proxmox Backup Server daemon with tools and GUI Package: proxmox-backup-client Architecture: any -Depends: ${misc:Depends}, ${shlibs:Depends} +Depends: qrencode ${misc:Depends}, ${shlibs:Depends} Description: Proxmox Backup Client tools This package contains the Proxmox Backup client, which provides a simple command line tool to create and restore backups. diff --git a/src/bin/proxmox_backup_client/key.rs b/src/bin/proxmox_backup_client/key.rs index f60687ed..e87f0483 100644 --- a/src/bin/proxmox_backup_client/key.rs +++ b/src/bin/proxmox_backup_client/key.rs @@ -1,4 +1,6 @@ use std::path::PathBuf; +use std::io::Write; +use std::process::{Stdio, Command}; use anyhow::{bail, format_err, Error}; use serde::{Deserialize, Serialize}; @@ -261,6 +263,66 @@ fn create_master_key() -> Result<(), Error> { Ok(()) } +#[api( + input: { + properties: { + path: { + description: "Key file. Without this the default key's will be used.", + optional: true, + }, + subject: { + description: "Include the specified subject as titel text.", + optional: true, + }, + }, + }, +)] +/// Generate a printable, human readable text file containing the encryption key. +/// +/// This also includes a scanable QR code for fast key restore. +fn paper_key(path: Option, subject: Option) -> Result<(), Error> { + let path = match path { + Some(path) => PathBuf::from(path), + None => { + let path = find_default_encryption_key()? + .ok_or_else(|| { + format_err!("no encryption file provided and no default file found") + })?; + path + } + }; + + let data = file_get_contents(&path)?; + let key_config: KeyConfig = serde_json::from_slice(&data)?; + let key_text = serde_json::to_string_pretty(&key_config)?; + + if let Some(subject) = subject { + println!("Subject: {}\n", subject); + } + + println!("-----BEGIN PROXMOX BACKUP KEY-----"); + println!("{}", key_text); + println!("-----END PROXMOX BACKUP KEY-----"); + + let mut child = Command::new("qrencode") + .args(&["-t", "utf8i", "-lm"]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn()?; + + { + let stdin = child.stdin.as_mut().expect("Failed to open stdin"); + stdin.write_all(key_text.as_bytes()).expect("Failed to write to stdin"); + } + + let output = child.wait_with_output().expect("Failed to read stdout"); + + println!("{}", String::from_utf8_lossy(&output.stdout)); + + Ok(()) +} + + pub fn cli() -> CliCommandMap { let key_create_cmd_def = CliCommand::new(&API_METHOD_CREATE) .arg_param(&["path"]) @@ -275,9 +337,14 @@ pub fn cli() -> CliCommandMap { .arg_param(&["path"]) .completion_cb("path", tools::complete_file_name); + let paper_key_cmd_def = CliCommand::new(&API_METHOD_PAPER_KEY) + .arg_param(&["path"]) + .completion_cb("path", tools::complete_file_name); + CliCommandMap::new() .insert("create", key_create_cmd_def) .insert("create-master-key", key_create_master_key_cmd_def) .insert("import-master-pubkey", key_import_master_pubkey_cmd_def) .insert("change-passphrase", key_change_passphrase_cmd_def) + .insert("paper-key", paper_key_cmd_def) }