Compiler errors:
Checking signatory v0.1.0 (/mnt/a-00-net/git/inferencium/signatory)
error[E0425]: cannot find value `cert_serial` in this scope
--> src/generate.rs:56:25
|
56 | cert_serial_file_check(cert_serial);
| ^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find value `cert_serial` in this scope
--> src/generate.rs:58:29
|
58 | cert_serial_file_read_init(cert_serial);
| ^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find value `pub_key` in this scope
--> src/generate.rs:69:14
|
69 | for user in pub_key.iter() {
| ^^^^^^^ not found in this scope
error[E0425]: cannot find value `ca_priv_key` in this scope
--> src/generate.rs:75:10
|
75 | "s", ca_priv_key,
| ^^^^^^^^^^^ not found in this scope
error[E0425]: cannot find value `principle` in this scope
--> src/generate.rs:79:10
|
79 | "n", principle,
| ^^^^^^^^^ not found in this scope
error[E0425]: cannot find value `validity` in this scope
--> src/generate.rs:84:10
|
84 | "v", validity,
| ^^^^^^^^ not found in this scope
error[E0425]: cannot find value `pub_key` in this scope
--> src/generate.rs:92:5
|
92 | pub_key,
| ^^^^^^^ not found in this scope
error[E0603]: function `config_read` is private
--> src/main.rs:14:10
|
14 | config::config_read();
| ^^^^^^^^^^^ private function
|
note: the function `config_read` is defined here
--> src/config.rs:19:1
|
19 | fn config_read() {
| ^^^^^^^^^^^^^^^^
error: missing type for `static mut` item
--> src/config.rs:24:23
|
24 | static mut config_dir = "/.config/signatory";
| ^ help: provide a type for the static variable: `: &str`
error[E0121]: the placeholder `_` is not allowed within types on item signatures for static variables
--> src/config.rs:24:23
|
24 | static mut config_dir = "/.config/signatory";
| ^ not allowed in type signatures
|
help: replace this with a fully-specified type
|
24 | static mut config_dir&str = "/.config/signatory";
| ++++
error[E0308]: mismatched types
--> src/config.rs:37:2
|
37 | config_read()
| ^^^^^^^^^^^^^ expected `Config`, found `()`
Some errors have detailed explanations: E0121, E0308, E0425, E0603.
For more information about an error, try `rustc --explain E0121`.
error: could not compile `signatory` (bin "signatory") due to 12 previous errors
I am aware that some of the arguments have not yet been implemented in the TOML configuration-file, thus not being in the structs. The for-loop has also not been fully implemented, yet. The other errors, regarding existing variables within config.rs being accessed by generate.rs, are what are new to me.
main.rs:
mod config;
mod generate;
fn main() {
config::config_read();
generate::generate();
}
config.rs:
use std::fs;
use std::sync::LazyLock;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct Config {
key: Key,
}
#[derive(Debug, Deserialize)]
struct Key {
ca_priv_key: String,
user_dir: String,
cert_serial: String,
}
fn config_read() {
// Get value of "$HOME" environment variable.
let home_env = std::env::var("HOME").unwrap();
// Append the configuration directory to the "$HOME" environment-variable value.
static mut config_dir = "/.config/signatory";
// Append the configuration-file filename to the configuration-file directory.
let config_file = [home_env.clone(), config_dir.to_string(), "/config.toml".to_string()].join("");
let config_file_parse = fs::read_to_string(&config_file).unwrap();
let config: Config = toml::from_str(&config_file_parse).unwrap();
let ca_priv_key = config.key.ca_priv_key;
let user_dir = config.key.user_dir;
let cert_serial = config.key.cert_serial;
}
pub static CONFIG: LazyLock<Config> = LazyLock::new (|| {
config_read()
});
generate.rs:
use std::fs;
use std::fs::OpenOptions;
use std::io;
use std::process::Command;
#[path = "config.rs"]
mod config;
// Function containing the logic for checking whether the certificate-serial-file exists. If it exists, open it. If it
// does not exist, create it, then open it.
fn cert_serial_file_check(path: &str) -> io::Result<std::fs::File> {
OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(path)
}
// Function for reading the contents of the certificate-serial-file. If it contains a string, return that string. If it
// does not contain a value, zero-initialise the string.
fn cert_serial_file_read_init(path: &str) -> String {
let content = fs::read_to_string(path).unwrap_or_default();
if content.trim().is_empty() {
println!("Zero-initialising certificate-serial-file...");
fs::write(path, "0")
.unwrap();
"0".to_string()
} else {
content
}
}
// Function containing the logic for the serial-number incrementation after each OpenSSH-certificate generation.
fn increment(n: &mut u32) {
*n += 1
}
// Function containing the logic for the OpenSSH-certificate generation.
pub fn generate() {
let config = &*crate::config::CONFIG;
let mut serial_read: String;
let mut serial: u32;
cert_serial_file_check(cert_serial);
cert_serial_file_read_init(cert_serial);
// Convert the string read from the certificate-serial-file to an integer to allow it to be incremented as a number.
let mut serial: u32 = match serial_read.trim().parse() {
Ok(num) => num,
Err(err) => panic!("ERROR: Failed to convert serial-number string to integer!"),
};
// Iterate through the defined user OpenSSH public-keys and generate respective OpenSSH certificates for them.
// The certificate-serial file is read before each certificate generation, in order to use the value as the serial
// number for the next certificate.
for user in pub_key.iter() {
// Begin generation of OpenSSH certificates via looping through defined user OpenSSH public-keys.
println!("Generating OpenSSH certificate for {}...", user);
Command::new("ssh-keygen")
.args([
// OpenSSH certificate-authority private-key to use for signing the user's OpenSSH public-key.
"s", ca_priv_key,
// User's OpenSSH public-key to signed.
"i", user,
// OpenSSH principles to be granted access to on remote systems.
"n", principle,
// OpenSSH-certificate validity dates.
"v", validity,
// Clear all default OpenSSH permissions for the OpenSSH certificate.
"O", "clear",
// Permit pty access via the OpenSSH certificate.
"O", "permit-pty",
// Set the OpenSSH-certificate serial number to the number contained in the certificate-serial file.
"z", serial,
// Path to the user's OpenSSH public-key.
pub_key,
])
.output()
.expect("Failed to generate OpenSSH certificate for user!");
increment(&mut serial);
}
}