Skip to main content

yb/
lib.rs

1// SPDX-FileCopyrightText: 2025 - 2026 Frederic Ruget <fred@atlant.is> (GitHub: @douzebis)
2//
3// SPDX-License-Identifier: MIT
4
5pub mod cli;
6pub mod complete;
7
8use clap::{Parser, Subcommand};
9use clap_complete::engine::ArgValueCompleter;
10
11use crate::complete::complete_serials;
12
13/// Returns the root clap `Command` for `yb`.  Used by `yb-gen-man`.
14pub fn command() -> clap::Command {
15    use clap::CommandFactory as _;
16    Cli::command()
17}
18
19#[derive(Parser, Debug)]
20#[command(name = "yb", about = "Secure blob storage on a YubiKey", version)]
21pub struct Cli {
22    /// YubiKey serial number.
23    #[arg(short = 's', long = "serial", add = ArgValueCompleter::new(complete_serials))]
24    pub serial: Option<u32>,
25
26    /// PC/SC reader name (legacy; prefer --serial).
27    #[arg(short = 'r', long = "reader")]
28    pub reader: Option<String>,
29
30    /// Suppress informational output.
31    #[arg(short = 'q', long = "quiet")]
32    pub quiet: bool,
33
34    /// Read PIN from stdin (one line).
35    #[arg(long = "pin-stdin")]
36    pub pin_stdin: bool,
37
38    /// Enable debug output.
39    #[arg(long = "debug")]
40    pub debug: bool,
41
42    /// Allow insecure default credentials (not recommended).
43    #[arg(long = "allow-defaults")]
44    pub allow_defaults: bool,
45
46    /// YubiKey PIN [deprecated: use YB_PIN, --pin-stdin, or interactive prompt].
47    #[arg(long = "pin", hide = true)]
48    pub pin_deprecated: Option<String>,
49
50    /// Management key [deprecated: use YB_MANAGEMENT_KEY].
51    #[arg(short = 'k', long = "key", hide = true)]
52    pub key_deprecated: Option<String>,
53
54    #[command(subcommand)]
55    pub command: Commands,
56}
57
58#[derive(Subcommand, Debug)]
59pub enum Commands {
60    /// Provision PIV objects and optionally generate an ECDH key.
61    Format(cli::format::FormatArgs),
62    /// Store a blob on the YubiKey.
63    Store(cli::store::StoreArgs),
64    /// Retrieve one or more blobs.
65    Fetch(cli::fetch::FetchArgs),
66    /// List blobs (alias: ls).
67    #[command(alias = "ls")]
68    List(cli::list::ListArgs),
69    /// Remove a blob (alias: rm).
70    #[command(alias = "rm")]
71    Remove(cli::remove::RemoveArgs),
72    /// Filesystem check — dump store metadata.
73    Fsck(cli::fsck::FsckArgs),
74    /// Replace the management key with a random one, kept on the YubiKey
75    /// and unlocked by the PIN (the store is kept).
76    RotateManagementKey(cli::rotate_management_key::RotateManagementKeyArgs),
77    /// List PC/SC readers.
78    ListReaders(cli::list_readers::ListReadersArgs),
79    /// Interactively select a YubiKey and print its serial number.
80    Select(cli::select::SelectArgs),
81    /// Run a destructive end-to-end self-test on real hardware.
82    #[cfg(feature = "self-test")]
83    SelfTest(cli::self_test::SelfTestArgs),
84}