hyper > ht
Use more exit()
This commit is contained in:
parent
72d91a04b9
commit
39ab75e1c4
2 changed files with 81 additions and 46 deletions
32
Cargo.lock
generated
32
Cargo.lock
generated
|
@ -9,11 +9,33 @@ dependencies = [
|
|||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.93"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "maniver"
|
||||
version = "0.1.0-dev"
|
||||
dependencies = [
|
||||
"regex",
|
||||
"users",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -54,3 +76,13 @@ checksum = "8018d24e04c95ac8790716a5987d0fec4f8b27249ffa0f7d33f1369bdfb88cbd"
|
|||
dependencies = [
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "users"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "24cc0f6d6f267b73e5a2cadf007ba8f9bc39c6a6f9666f8cf25ea809a153b032"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"log",
|
||||
]
|
||||
|
|
95
src/main.rs
95
src/main.rs
|
@ -1,13 +1,13 @@
|
|||
use std::env;
|
||||
use regex::Regex;
|
||||
use std::io::prelude::*;
|
||||
use std::process::{Command, Stdio, Output};
|
||||
use std::fs;
|
||||
use users::get_current_username;
|
||||
use std::io::prelude::*;
|
||||
use std::ffi::OsString;
|
||||
use std::process::{Command, Stdio, Output};
|
||||
use regex::Regex;
|
||||
use users::get_current_username;
|
||||
|
||||
fn exit(error: String) {
|
||||
eprintln!("Error: {}", error);
|
||||
eprintln!("error: {}", error);
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
|
@ -17,23 +17,31 @@ fn main() {
|
|||
|
||||
match get_current_username() {
|
||||
Some(user) => match user {
|
||||
_ if user == superuser => println!("root: right user"),
|
||||
_ => exit("must be run as root".to_string()),
|
||||
_ if user == superuser => parse_command(),
|
||||
_ => exit("Must be run as root.".to_string()),
|
||||
}
|
||||
None => exit("the current user does not exist".to_string()),
|
||||
None => exit("The current user does not exist.".to_string()),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn parse_command() {
|
||||
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
match args[1].as_ref() {
|
||||
"setup-user" => setup_user(args[2].to_string(), args[3].to_string()),
|
||||
"reload-nginx" => reload_nginx(),
|
||||
"reload-tor" => reload_tor(),
|
||||
"restart-gmnisrv" => restart_gmnisrv(),
|
||||
"le-install" => le_install(args[2].to_string()),
|
||||
"export-tor" => export_tor(args[2].to_string(), args[3].to_string()),
|
||||
_ => println!("ERROR: You must specify a subcommand"),
|
||||
match args.get(1) {
|
||||
Some(p) => match p {
|
||||
_ if p == "setup-user" => setup_user(args[2].to_string(), args[3].to_string()),
|
||||
_ if p == "reload-nginx" => reload_nginx(),
|
||||
_ if p == "reload-tor" => reload_tor(),
|
||||
_ if p == "restart-gmnisrv" => restart_gmnisrv(),
|
||||
_ if p == "le-install" => le_install(args[2].to_string()),
|
||||
_ if p == "export-tor" => export_tor(args[2].to_string(), args[3].to_string()),
|
||||
_ => exit("This subcommand doesn't exists.".to_string()),
|
||||
}
|
||||
None => exit("You must specify a subcommand.".to_string()),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn export_tor(username: String, dir: String) {
|
||||
|
@ -43,9 +51,9 @@ fn export_tor(username: String, dir: String) {
|
|||
src_path += &dir.to_string();
|
||||
src_path += &"/hostname".to_string().to_owned();
|
||||
|
||||
let mut dest_path: String = "/srv/hyper/".to_owned();
|
||||
let mut dest_path: String = "/srv/ht/".to_owned();
|
||||
dest_path += &username.to_string();
|
||||
dest_path += &"/hyper/".to_string().to_owned();
|
||||
dest_path += &"/ht/".to_string().to_owned();
|
||||
dest_path += &dir.to_string();
|
||||
dest_path += &"/hostname".to_string().to_owned();
|
||||
|
||||
|
@ -62,10 +70,10 @@ fn export_tor(username: String, dir: String) {
|
|||
|
||||
print_output(output);
|
||||
} else {
|
||||
println!("ERROR: The dirname must be composed only of lowercase letters");
|
||||
exit("The dirname must be composed only of lowercase letters.".to_string());
|
||||
}
|
||||
} else {
|
||||
println!("ERROR: The username must be composed only of lowercase letters");
|
||||
exit("The username must be composed only of lowercase letters.".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -133,13 +141,13 @@ fn setup_user(username: String, password: String) {
|
|||
quota(username4.to_string());
|
||||
|
||||
} else {
|
||||
println!("ERROR: The dirname must be composed only of lowercase letters");
|
||||
exit("The username must be composed only of lowercase letters.".to_string());
|
||||
}
|
||||
} else {
|
||||
println!("ERROR: The password must be shorter than 1024 characters");
|
||||
exit("The password must be shorter than 1024 characters.".to_string());
|
||||
}
|
||||
} else {
|
||||
println!("ERROR: The username must be shorter than 32 characters");
|
||||
exit("The username must be shorter than 32 characters.".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -154,43 +162,38 @@ fn quota(username: String) {
|
|||
print_output(output);
|
||||
}
|
||||
|
||||
// Chown /srv/hyper/username to root:root
|
||||
// Chown /srv/ht/username to root:root
|
||||
fn chroot(username: String) {
|
||||
|
||||
if username.chars().count() < 32 {
|
||||
let mut path = "/srv/ht/".to_string();
|
||||
|
||||
let mut path = "/srv/hyper/".to_string();
|
||||
path += &username;
|
||||
|
||||
path += &username;
|
||||
let output = Command::new("/usr/bin/chown")
|
||||
.arg("root:root")
|
||||
.arg(&path)
|
||||
.output()
|
||||
.expect("Failed to chown /srv/ht/<username> to root:root");
|
||||
print_output(output);
|
||||
|
||||
let output = Command::new("/usr/bin/chown")
|
||||
.arg("root:root")
|
||||
.arg(&path)
|
||||
.output()
|
||||
.expect("Failed to chown /srv/hyper/<username> to root:root");
|
||||
print_output(output);
|
||||
let output = Command::new("/usr/bin/chmod")
|
||||
.arg("755")
|
||||
.arg(path)
|
||||
.output()
|
||||
.expect("Failed to chmod /srv/ht/<username> to 755");
|
||||
print_output(output);
|
||||
|
||||
let output = Command::new("/usr/bin/chmod")
|
||||
.arg("755")
|
||||
.arg(path)
|
||||
.output()
|
||||
.expect("Failed to chmod /srv/hyper/<username> to 755");
|
||||
print_output(output);
|
||||
|
||||
} else {
|
||||
println!("Erreur : l'username doit faire moins de 32 caractères");
|
||||
}
|
||||
}
|
||||
|
||||
// Creates a new user in the group 'hyper', which is available only over SFTP
|
||||
// Creates a new user in the group 'ht', which is available only over SFTP
|
||||
fn newser(username: String) {
|
||||
let output = Command::new("/usr/sbin/useradd")
|
||||
.arg(&username)
|
||||
.arg("--create-home")
|
||||
.arg("--base-dir")
|
||||
.arg("/srv/hyper")
|
||||
.arg("/srv/ht")
|
||||
.arg("--gid")
|
||||
.arg("hyper")
|
||||
.arg("ht")
|
||||
.arg("--shell")
|
||||
.arg("/usr/sbin/nologin")
|
||||
.output()
|
||||
|
|
Reference in a new issue