]> git.sesse.net Git - bcachefs-tools-debian/blob - rust-src/src/key.rs
More rust improvements
[bcachefs-tools-debian] / rust-src / src / key.rs
1 use bch_bindgen::info;
2 use bch_bindgen::bcachefs::bch_sb_handle;
3 use colored::Colorize;
4 use crate::c_str;
5 use anyhow::anyhow;
6
7 #[derive(Clone, Debug)]
8 pub enum KeyLocation {
9     Fail,
10     Wait,
11     Ask,
12 }
13
14 #[derive(Clone, Debug)]
15 pub struct KeyLoc(pub Option<KeyLocation>);
16 impl std::ops::Deref for KeyLoc {
17     type Target = Option<KeyLocation>;
18     fn deref(&self) -> &Self::Target {
19         &self.0
20     }
21 }
22
23 impl std::str::FromStr for KeyLoc {
24     type Err = anyhow::Error;
25     fn from_str(s: &str) -> anyhow::Result<Self> {
26         match s {
27             ""      => Ok(KeyLoc(None)),
28             "fail"  => Ok(KeyLoc(Some(KeyLocation::Fail))),
29             "wait"  => Ok(KeyLoc(Some(KeyLocation::Wait))),
30             "ask"   => Ok(KeyLoc(Some(KeyLocation::Ask))),
31             _       => Err(anyhow!("invalid password option")),
32         }
33     }
34 }
35
36 fn check_for_key(key_name: &std::ffi::CStr) -> anyhow::Result<bool> {
37     use bch_bindgen::keyutils::{self, keyctl_search};
38     let key_name = key_name.to_bytes_with_nul().as_ptr() as *const _;
39     let key_type = c_str!("logon");
40
41     let key_id = unsafe { keyctl_search(keyutils::KEY_SPEC_USER_KEYRING, key_type, key_name, 0) };
42     if key_id > 0 {
43         info!("Key has became available");
44         Ok(true)
45     } else if errno::errno().0 != libc::ENOKEY {
46         Err(crate::ErrnoError(errno::errno()).into())
47     } else {
48         Ok(false)
49     }
50 }
51
52 fn wait_for_key(uuid: &uuid::Uuid) -> anyhow::Result<()> {
53     let key_name = std::ffi::CString::new(format!("bcachefs:{}", uuid)).unwrap();
54     loop {
55         if check_for_key(&key_name)? {
56             break Ok(());
57         }
58
59         std::thread::sleep(std::time::Duration::from_secs(1));
60     }
61 }
62
63 const BCH_KEY_MAGIC: &str = "bch**key";
64 fn ask_for_key(sb: &bch_sb_handle) -> anyhow::Result<()> {
65     use bch_bindgen::bcachefs::{self, bch2_chacha_encrypt_key, bch_encrypted_key, bch_key};
66     use byteorder::{LittleEndian, ReadBytesExt};
67     use std::os::raw::c_char;
68
69     let key_name = std::ffi::CString::new(format!("bcachefs:{}", sb.sb().uuid())).unwrap();
70     if check_for_key(&key_name)? {
71         return Ok(());
72     }
73
74     let bch_key_magic = BCH_KEY_MAGIC.as_bytes().read_u64::<LittleEndian>().unwrap();
75     let crypt = sb.sb().crypt().unwrap();
76     let pass = rpassword::read_password_from_tty(Some("Enter passphrase: "))?;
77     let pass = std::ffi::CString::new(pass.trim_end())?; // bind to keep the CString alive
78     let mut output: bch_key = unsafe {
79         bcachefs::derive_passphrase(
80             crypt as *const _ as *mut _,
81             pass.as_c_str().to_bytes_with_nul().as_ptr() as *const _,
82         )
83     };
84
85     let mut key = crypt.key().clone();
86     let ret = unsafe {
87         bch2_chacha_encrypt_key(
88             &mut output as *mut _,
89             sb.sb().nonce(),
90             &mut key as *mut _ as *mut _,
91             std::mem::size_of::<bch_encrypted_key>() as usize,
92         )
93     };
94     if ret != 0 {
95         Err(anyhow!("chacha decryption failure"))
96     } else if key.magic != bch_key_magic {
97         Err(anyhow!("failed to verify the password"))
98     } else {
99         let key_type = c_str!("logon");
100         let ret = unsafe {
101             bch_bindgen::keyutils::add_key(
102                 key_type,
103                 key_name.as_c_str().to_bytes_with_nul() as *const _ as *const c_char,
104                 &output as *const _ as *const _,
105                 std::mem::size_of::<bch_key>() as usize,
106                 bch_bindgen::keyutils::KEY_SPEC_USER_KEYRING,
107             )
108         };
109         if ret == -1 {
110             Err(anyhow!("failed to add key to keyring: {}", errno::errno()))
111         } else {
112             Ok(())
113         }
114     }
115 }
116
117 pub fn prepare_key(sb: &bch_sb_handle, password: KeyLocation) -> anyhow::Result<()> {
118     info!("checking if key exists for filesystem {}", sb.sb().uuid());
119     match password {
120         KeyLocation::Fail => Err(anyhow!("no key available")),
121         KeyLocation::Wait => Ok(wait_for_key(&sb.sb().uuid())?),
122         KeyLocation::Ask => ask_for_key(sb),
123     }
124 }