Debian Codegraph @

examples/keyctl.rs

raw symbols SCIP
1 //! Example CLI application that allows you to interact
2 //! with the Linux kernel keyring from user space.
3 //!
4 //! Demo code for the linux_keyutils crate.
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 /// Create a new key
24 Create {
25 #[clap(short, long)]
26 description: String,
27
28 #[clap(short, long)]
29 secret: String,
30 },
31 /// Read the secret from a key
32 Read {
33 #[clap(short, long)]
34 description: String,
35 },
36 /// Change ownership of a key
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 /// Change permissions of a key
48 Chmod {
49 #[clap(short, long)]
50 description: String,
51 },
52 /// Invalidate a key
53 Invalidate {
54 #[clap(short, long)]
55 description: String,
56 },
57 /// Instantiate a partially constructed key
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 // Obtain the default User keyring for the current UID/user
74 // See [KeyRingIdentifier] and `man 2 keyctl` for more information on default
75 // keyrings for processes.
76 let ring = KeyRing::from_special_id(KeyRingIdentifier::Session, false)?;
77
78 _ = match args.subcommand {
79 // Add a new key to the keyring
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 // Search for an existing key by description and read the secret
88 // data from the keyring
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 // Search for an existing key by description and attempt to
96 // change ownership of the key
97 Command::Chown {
98 description,
99 uid,
100 gid,
101 } => {
102 let key = ring.search(&description)?;
103 key.chown(uid, gid)?;
104 }
105 // Search for an existing key by description and attempt to
106 // change permissions of the key
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 // Search for an existing key by description and attempt to
115 // invalidate they key
116 Command::Invalidate { description } => {
117 let key = ring.search(&description)?;
118 key.invalidate()?;
119 println!("Removed key with ID {:?}", key.get_id());
120 }
121 // Instantiate a partially constructed key
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 }