| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
use clap::Parser; |
| 6 |
use linux_keyutils::{Key, KeyRing, KeyRingIdentifier, KeySerialId}; |
| 7 |
use linux_keyutils::{KeyPermissionsBuilder, Permission}; |
| 8 |
use std::error::Error; |
| 9 |
use zeroize::Zeroizing; |
| 10 |
|
| 11 |
#[derive(Parser, Debug)] |
| 12 |
#[clap(author, version, about, long_about = None)] |
| 13 |
#[command(arg_required_else_help(true))] |
| 14 |
#[command(subcommand_required(true))] |
| 15 |
struct Args { |
| 16 |
#[clap(subcommand)] |
| 17 |
subcommand: Command, |
| 18 |
} |
| 19 |
|
| 20 |
#[derive(clap::Subcommand, Debug, PartialEq)] |
| 21 |
#[command(arg_required_else_help(true))] |
| 22 |
enum Command { |
| 23 |
|
| 24 |
Create { |
| 25 |
#[clap(short, long)] |
| 26 |
description: String, |
| 27 |
|
| 28 |
#[clap(short, long)] |
| 29 |
secret: String, |
| 30 |
}, |
| 31 |
|
| 32 |
Read { |
| 33 |
#[clap(short, long)] |
| 34 |
description: String, |
| 35 |
}, |
| 36 |
|
| 37 |
Chown { |
| 38 |
#[clap(short, long)] |
| 39 |
description: String, |
| 40 |
|
| 41 |
#[clap(short, long)] |
| 42 |
uid: Option<u32>, |
| 43 |
|
| 44 |
#[clap(short, long)] |
| 45 |
gid: Option<u32>, |
| 46 |
}, |
| 47 |
|
| 48 |
Chmod { |
| 49 |
#[clap(short, long)] |
| 50 |
description: String, |
| 51 |
}, |
| 52 |
|
| 53 |
Invalidate { |
| 54 |
#[clap(short, long)] |
| 55 |
description: String, |
| 56 |
}, |
| 57 |
|
| 58 |
Instantiate { |
| 59 |
#[clap(short, long)] |
| 60 |
keyid: Option<i32>, |
| 61 |
|
| 62 |
#[clap(short, long)] |
| 63 |
payload: String, |
| 64 |
|
| 65 |
#[clap(short, long)] |
| 66 |
ring: Option<i32>, |
| 67 |
}, |
| 68 |
} |
| 69 |
|
| 70 |
fn main() -> Result<(), Box<dyn Error>> { |
| 71 |
let args = Args::parse(); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
let ring = KeyRing::from_special_id(KeyRingIdentifier::Session, false)?; |
| 77 |
|
| 78 |
_ = match args.subcommand { |
| 79 |
|
| 80 |
Command::Create { |
| 81 |
description, |
| 82 |
secret, |
| 83 |
} => { |
| 84 |
let key = ring.add_key(&description, &secret)?; |
| 85 |
println!("Created key with ID {:?}", key.get_id()); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
Command::Read { description } => { |
| 90 |
let key = ring.search(&description)?; |
| 91 |
let mut buf = Zeroizing::new([0u8; 2048]); |
| 92 |
let len = key.read(&mut buf)?; |
| 93 |
println!("Secret {:?}", std::str::from_utf8(&buf[..len])?); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
Command::Chown { |
| 98 |
description, |
| 99 |
uid, |
| 100 |
gid, |
| 101 |
} => { |
| 102 |
let key = ring.search(&description)?; |
| 103 |
key.chown(uid, gid)?; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
Command::Chmod { description } => { |
| 108 |
let key = ring.search(&description)?; |
| 109 |
let perms = KeyPermissionsBuilder::builder() |
| 110 |
.user(Permission::ALL) |
| 111 |
.build(); |
| 112 |
key.set_perms(perms)?; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
Command::Invalidate { description } => { |
| 117 |
let key = ring.search(&description)?; |
| 118 |
key.invalidate()?; |
| 119 |
println!("Removed key with ID {:?}", key.get_id()); |
| 120 |
} |
| 121 |
|
| 122 |
Command::Instantiate { |
| 123 |
keyid, |
| 124 |
payload, |
| 125 |
ring, |
| 126 |
} => { |
| 127 |
let key = Key::from_id(KeySerialId::new(keyid.unwrap_or(i32::MAX))); |
| 128 |
key.instantiate(&payload, KeySerialId::new(ring.unwrap_or(i32::MAX)))?; |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
Ok(()) |
| 133 |
} |