auth_helpers.rs: split code into separate file
This commit is contained in:
parent
39a90ca6c5
commit
6c30068ebf
52
src/auth_helpers.rs
Normal file
52
src/auth_helpers.rs
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
use crate::tools;
|
||||||
|
|
||||||
|
use failure::*;
|
||||||
|
|
||||||
|
use openssl::rsa::{Rsa};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
pub fn generate_csrf_key() -> Result<(), Error> {
|
||||||
|
|
||||||
|
let path = PathBuf::from("/etc/proxmox-backup/csrf.key");
|
||||||
|
|
||||||
|
if path.exists() { return Ok(()); }
|
||||||
|
|
||||||
|
let rsa = Rsa::generate(2048).unwrap();
|
||||||
|
|
||||||
|
let pem = rsa.private_key_to_pem()?;
|
||||||
|
|
||||||
|
use nix::sys::stat::Mode;
|
||||||
|
|
||||||
|
tools::file_set_contents(
|
||||||
|
&path, &pem, Some(Mode::from_bits_truncate(0o0640)))?;
|
||||||
|
|
||||||
|
nix::unistd::chown(&path, Some(nix::unistd::ROOT), Some(nix::unistd::Gid::from_raw(33)))?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_auth_key() -> Result<(), Error> {
|
||||||
|
|
||||||
|
let priv_path = PathBuf::from("/etc/proxmox-backup/authkey.key");
|
||||||
|
|
||||||
|
let mut public_path = priv_path.clone();
|
||||||
|
public_path.set_extension("pub");
|
||||||
|
|
||||||
|
if priv_path.exists() && public_path.exists() { return Ok(()); }
|
||||||
|
|
||||||
|
let rsa = Rsa::generate(4096).unwrap();
|
||||||
|
|
||||||
|
let priv_pem = rsa.private_key_to_pem()?;
|
||||||
|
|
||||||
|
use nix::sys::stat::Mode;
|
||||||
|
|
||||||
|
tools::file_set_contents(
|
||||||
|
&priv_path, &priv_pem, Some(Mode::from_bits_truncate(0o0600)))?;
|
||||||
|
|
||||||
|
|
||||||
|
let public_pem = rsa.public_key_to_pem()?;
|
||||||
|
|
||||||
|
tools::file_set_contents(&public_path, &public_pem, None)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
@ -8,62 +8,14 @@ use proxmox_backup::api::router::*;
|
|||||||
use proxmox_backup::api::config::*;
|
use proxmox_backup::api::config::*;
|
||||||
use proxmox_backup::server::rest::*;
|
use proxmox_backup::server::rest::*;
|
||||||
use proxmox_backup::getopts;
|
use proxmox_backup::getopts;
|
||||||
|
use proxmox_backup::auth_helpers::*;
|
||||||
|
|
||||||
use failure::*;
|
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use openssl::rsa::{Rsa};
|
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
use futures::future::Future;
|
use futures::future::Future;
|
||||||
|
|
||||||
use hyper;
|
use hyper;
|
||||||
|
|
||||||
pub fn generate_csrf_key() -> Result<(), Error> {
|
|
||||||
|
|
||||||
let path = PathBuf::from("/etc/proxmox-backup/csrf.key");
|
|
||||||
|
|
||||||
if path.exists() { return Ok(()); }
|
|
||||||
|
|
||||||
let rsa = Rsa::generate(2048).unwrap();
|
|
||||||
|
|
||||||
let pem = rsa.private_key_to_pem()?;
|
|
||||||
|
|
||||||
use nix::sys::stat::Mode;
|
|
||||||
|
|
||||||
tools::file_set_contents(
|
|
||||||
&path, &pem, Some(Mode::from_bits_truncate(0o0640)))?;
|
|
||||||
|
|
||||||
nix::unistd::chown(&path, Some(nix::unistd::ROOT), Some(nix::unistd::Gid::from_raw(33)))?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn generate_auth_key() -> Result<(), Error> {
|
|
||||||
|
|
||||||
let priv_path = PathBuf::from("/etc/proxmox-backup/authkey.key");
|
|
||||||
|
|
||||||
let mut public_path = priv_path.clone();
|
|
||||||
public_path.set_extension("pub");
|
|
||||||
|
|
||||||
if priv_path.exists() && public_path.exists() { return Ok(()); }
|
|
||||||
|
|
||||||
let rsa = Rsa::generate(4096).unwrap();
|
|
||||||
|
|
||||||
let priv_pem = rsa.private_key_to_pem()?;
|
|
||||||
|
|
||||||
use nix::sys::stat::Mode;
|
|
||||||
|
|
||||||
tools::file_set_contents(
|
|
||||||
&priv_path, &priv_pem, Some(Mode::from_bits_truncate(0o0600)))?;
|
|
||||||
|
|
||||||
|
|
||||||
let public_pem = rsa.public_key_to_pem()?;
|
|
||||||
|
|
||||||
tools::file_set_contents(&public_path, &public_pem, None)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
||||||
if let Err(err) = syslog::init(
|
if let Err(err) = syslog::init(
|
||||||
|
@ -49,8 +49,6 @@ pub mod storage {
|
|||||||
pub mod futures;
|
pub mod futures;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod getopts;
|
|
||||||
|
|
||||||
pub mod cli {
|
pub mod cli {
|
||||||
|
|
||||||
pub mod environment;
|
pub mod environment;
|
||||||
@ -65,3 +63,6 @@ pub mod client {
|
|||||||
pub mod http_client;
|
pub mod http_client;
|
||||||
pub mod catar_backup_stream;
|
pub mod catar_backup_stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod getopts;
|
||||||
|
pub mod auth_helpers;
|
||||||
|
Loading…
Reference in New Issue
Block a user