-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
137 lines (111 loc) · 4.42 KB
/
Copy pathmod.rs
File metadata and controls
137 lines (111 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (C) 2026 Stephan Naumann
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::os::unix::fs::OpenOptionsExt;
use std::path::PathBuf;
use std::{fs::OpenOptions, io::Write};
use anyhow::{Context, Result};
use tssh_core::sqlite::types::DBKey;
use tssh_core::tpm::HostTemplate;
pub struct DirEnv {
pub data_dir: PathBuf,
pub lib_path: PathBuf,
}
#[derive(Clone)]
pub struct FileEntry {
host: String,
username: String,
port: u16,
accepted_algorithms: String,
pkcs11_provider: String,
identity_file: String,
}
impl FileEntry {
fn as_file_entry(&self) -> String {
format!(
"Host {}\n\tUser {}\n\tPort {}\n\tPKCS11Provider {}\n\tPubkeyAcceptedAlgorithms {}\n\tIdentityFile \"{}\"\n\tIdentitiesOnly yes\n\n",
self.host,
self.username,
self.port,
self.pkcs11_provider,
self.accepted_algorithms,
self.identity_file
)
}
}
impl TryFrom<(DBKey, &str, &str)> for FileEntry{
type Error=anyhow::Error;
fn try_from((db_key, lib_path, file_path): (DBKey, &str, &str)) -> std::prelude::v1::Result<Self, Self::Error> {
let host_template = HostTemplate::try_from(&db_key).context("while parsing host template from db key")?;
let accepted_algorithms = match host_template.template {
tssh_core::tpm::Template::RSA(rsa_template) => match rsa_template.keybits {
tssh_core::tpm::RsaKeyBits::Rsa1024 => "rsa-sha2-256",
tssh_core::tpm::RsaKeyBits::Rsa2048 => "rsa-sha2-256",
tssh_core::tpm::RsaKeyBits::Rsa3072 => "rsa-sha2-256",
tssh_core::tpm::RsaKeyBits::Rsa4096 => "rsa-sha2-512",
},
tssh_core::tpm::Template::ECC(ecc_template) => match ecc_template.curve {
tssh_core::tpm::ECCCurve::NistP256 => "ecdsa-sha2-nistp256",
tssh_core::tpm::ECCCurve::NistP384 => "ecdsa-sha2-nistp384",
tssh_core::tpm::ECCCurve::NistP521 => "ecdsa-sha2-nistp521",
},
};
Ok(Self {
host: db_key.host,
username: db_key.username,
port: db_key.port,
pkcs11_provider: lib_path.to_string(),
identity_file: file_path.to_string(),
accepted_algorithms: accepted_algorithms.to_string(),
})
}
}
const KEY_DIR_NAME: &str = "keys";
const SSH_FILE_NAME: &str = "ssh_file";
pub fn write<T>(i: T, env: &DirEnv) -> Result<()>
where
T: IntoIterator<Item = DBKey>,
{
let tssh_key_dir = env.data_dir.join(KEY_DIR_NAME);
let _ = std::fs::remove_dir_all(&tssh_key_dir); //TODO: better
std::fs::create_dir_all(&tssh_key_dir).context("while creating tssh key directory")?;
let tssh_ssh_file_path = env.data_dir.join(SSH_FILE_NAME);
let mut ssh_file_content = String::new();
for x in i.into_iter() {
let file_path = tssh_key_dir.join(format!("{}.pub", x.pkcs11_id));
let mut file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.mode(0o600)
.open(&file_path)
.context("while creating keyfile")?;
file.write_all(x.pub_key.as_bytes())
.context("while writing to keyfile")?;
ssh_file_content.push_str(
FileEntry::try_from((
x,
env.lib_path.to_string_lossy().to_string().as_str(),
file_path.to_string_lossy().to_string().as_str(),
))?
.as_file_entry()
.as_str(),
);
}
std::fs::write(tssh_ssh_file_path, ssh_file_content.as_bytes()).context("while writing ssh file")
}
pub fn generate_include(env: &DirEnv) -> Result<String> {
let tssh_ssh_file_path = env.data_dir.join(SSH_FILE_NAME);
Ok(format!("Include {}", tssh_ssh_file_path.to_string_lossy()))
}