From 36590317aa8982075149ee0a1cf045118e0fc324 Mon Sep 17 00:00:00 2001 From: UsernameSwift Date: Wed, 3 Aug 2022 13:13:27 -0400 Subject: [PATCH 1/3] Added shell chooser --- src/args.rs | 4 +++ src/functions/unakite.rs | 1 + src/functions/users.rs | 61 ++++++++++++++++++++++++++++++++++++++-- src/internal/config.rs | 3 ++ src/main.rs | 2 +- 5 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/args.rs b/src/args.rs index 7e6d01c..b656852 100644 --- a/src/args.rs +++ b/src/args.rs @@ -228,6 +228,10 @@ pub struct NewUserArgs { /// When not providing a password openssl jumps into an interactive masked input mode allowing you to hide your password /// from the terminal history. pub password: String, + + /// The shell to use for the user. The current options are bash, csh, fish, tcsh, and zsh. + /// If a shell is not specified or unknown, it defaults to fish. + pub shell: String, } #[derive(Debug, ArgEnum, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Serialize, Deserialize)] diff --git a/src/functions/unakite.rs b/src/functions/unakite.rs index c4028f4..06e1a6d 100644 --- a/src/functions/unakite.rs +++ b/src/functions/unakite.rs @@ -127,6 +127,7 @@ pub fn setup_unakite(root: &str, oldroot: &str, efi: bool, efidir: &str, bootdev true, "Cp7oN04ZY0PsA", // unakite false, + "/bin/bash", ); exec_eval( exec( diff --git a/src/functions/users.rs b/src/functions/users.rs index d63d445..8bcc7b4 100755 --- a/src/functions/users.rs +++ b/src/functions/users.rs @@ -2,7 +2,8 @@ use crate::internal::exec::*; use crate::internal::*; use std::process::Command; -pub fn new_user(username: &str, hasroot: bool, password: &str, do_hash_pass: bool) { +pub fn new_user(username: &str, hasroot: bool, password: &str, do_hash_pass: bool, shell: &str) { + let mut shell: &str = shell; if do_hash_pass { let hashed_pass = &*hash_pass(password).stdout; let _password = match std::str::from_utf8(hashed_pass) { @@ -10,13 +11,66 @@ pub fn new_user(username: &str, hasroot: bool, password: &str, do_hash_pass: boo Err(e) => panic!("Failed to hash password, invalid UTF-8 sequence {}", e), }; } + if shell == "fish" { + exec_eval( + exec_chroot( + "bash", + vec![ + String::from("pacman -S fish --noconfirm"), + ], + ), + "installed fish", + ); + } + if shell == "zsh" { + exec_eval( + exec_chroot( + "bash", + vec![ + String::from("pacman -S zsh --noconfirm"), + ], + ), + "installed zsh", + ); + } + else if shell == "tcsh" || shell == "csh" { + exec_eval( + exec_chroot( + "bash", + vec![ + String::from("pacman -S tcsh --noconfirm"), + ], + ), + "installed tcsh and csh", + ); + } + else { + exec_eval( + exec_chroot( + "bash", + vec![ + String::from("pacman -S fish --noconfirm"), + ], + ), + "no shell or unknown shell specified, installed fish", + ); + shell = "fish"; + } + let shell_path = match shell { + "bash" => "/bin/bash", + "csh" => "/bin/csh", + "fish" => "/bin/fish", + "tcsh" => "/bin/tcsh", + "zsh" => "/bin/zsh", + &_ => "/bin/fish", + }; exec_eval( exec_chroot( "useradd", vec![ String::from("-m"), String::from("-s"), - String::from("/bin/bash"), + String::from(shell_path), String::from("-p"), String::from(password).replace('\n', ""), String::from(username), @@ -49,7 +103,7 @@ pub fn new_user(username: &str, hasroot: bool, password: &str, do_hash_pass: boo pub fn hash_pass(password: &str) -> std::process::Output { let output = Command::new("openssl") - .args(["passwd", "-1", password]) + .args(["passwd", "-6", password]) .output() .expect("Failed to hash password"); output @@ -67,3 +121,4 @@ pub fn root_pass(root_pass: &str) { "set root password", ); } + diff --git a/src/internal/config.rs b/src/internal/config.rs index 928014f..080383d 100755 --- a/src/internal/config.rs +++ b/src/internal/config.rs @@ -53,6 +53,7 @@ struct Users { name: String, password: String, hasroot: bool, + shell: String, } #[derive(Serialize, Deserialize)] @@ -138,11 +139,13 @@ pub fn read_config(configpath: PathBuf) { log::info!("Creating user : {}", config.users[i].name); log::info!("Setting use password : {}", config.users[i].password); log::info!("Enabling root for user : {}", config.users[i].hasroot); + log::info!("Setting user shell : {}", config.users[i].shell); users::new_user( config.users[i].name.as_str(), config.users[i].hasroot, config.users[i].password.as_str(), false, + config.users[i].shell.as_str(), ); println!("---------"); } diff --git a/src/main.rs b/src/main.rs index 95c13fe..c3d924a 100755 --- a/src/main.rs +++ b/src/main.rs @@ -53,7 +53,7 @@ fn main() { } Command::Users { subcommand } => match subcommand { UsersSubcommand::NewUser(args) => { - users::new_user(&args.username, args.hasroot, &args.password, true); + users::new_user(&args.username, args.hasroot, &args.password, true, &args.shell); } UsersSubcommand::RootPass { password } => { users::root_pass(&password); From ede22122820db0d5fb98bad1c72743240440534b Mon Sep 17 00:00:00 2001 From: usernameswift Date: Wed, 3 Aug 2022 17:06:31 -0400 Subject: [PATCH 2/3] Fixed shell paths --- src/functions/users.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/functions/users.rs b/src/functions/users.rs index 8bcc7b4..4d3b194 100755 --- a/src/functions/users.rs +++ b/src/functions/users.rs @@ -58,11 +58,11 @@ pub fn new_user(username: &str, hasroot: bool, password: &str, do_hash_pass: boo } let shell_path = match shell { "bash" => "/bin/bash", - "csh" => "/bin/csh", - "fish" => "/bin/fish", - "tcsh" => "/bin/tcsh", - "zsh" => "/bin/zsh", - &_ => "/bin/fish", + "csh" => "/usr/bin/csh", + "fish" => "/usr/bin/fish", + "tcsh" => "/usr/bin/tcsh", + "zsh" => "/usr/bin/zsh", + &_ => "/usr/bin/fish", }; exec_eval( exec_chroot( From 2c83afa4fe366eda37c99e9e1e23ba651ea20e1e Mon Sep 17 00:00:00 2001 From: usernameswift Date: Wed, 3 Aug 2022 17:53:30 -0400 Subject: [PATCH 3/3] Reworked the shell choosing --- src/functions/users.rs | 68 +++++++++++++----------------------------- 1 file changed, 21 insertions(+), 47 deletions(-) diff --git a/src/functions/users.rs b/src/functions/users.rs index 4d3b194..cf8c325 100755 --- a/src/functions/users.rs +++ b/src/functions/users.rs @@ -3,7 +3,7 @@ use crate::internal::*; use std::process::Command; pub fn new_user(username: &str, hasroot: bool, password: &str, do_hash_pass: bool, shell: &str) { - let mut shell: &str = shell; + let shell: &str = shell; if do_hash_pass { let hashed_pass = &*hash_pass(password).stdout; let _password = match std::str::from_utf8(hashed_pass) { @@ -11,51 +11,25 @@ pub fn new_user(username: &str, hasroot: bool, password: &str, do_hash_pass: boo Err(e) => panic!("Failed to hash password, invalid UTF-8 sequence {}", e), }; } - if shell == "fish" { - exec_eval( - exec_chroot( - "bash", - vec![ - String::from("pacman -S fish --noconfirm"), - ], - ), - "installed fish", - ); - } - if shell == "zsh" { - exec_eval( - exec_chroot( - "bash", - vec![ - String::from("pacman -S zsh --noconfirm"), - ], - ), - "installed zsh", - ); - } - else if shell == "tcsh" || shell == "csh" { - exec_eval( - exec_chroot( - "bash", - vec![ - String::from("pacman -S tcsh --noconfirm"), - ], - ), - "installed tcsh and csh", - ); - } - else { - exec_eval( - exec_chroot( - "bash", - vec![ - String::from("pacman -S fish --noconfirm"), - ], - ), - "no shell or unknown shell specified, installed fish", - ); - shell = "fish"; - } + let shell_to_install = match shell { + "bash" => "bash", + "csh" => "tcsh", + "fish" => "fish", + "tcsh" => "tcsh", + "zsh" => "zsh", + &_ => "bash", + }; + exec_eval( + exec_chroot( + "bash", + vec![ + String::from("pacman -S "), + String::from(shell_to_install), + String::from("--noconfirm"), + ], + ), + format!("installed {shell:?}").as_str(), + ); let shell_path = match shell { "bash" => "/bin/bash", "csh" => "/usr/bin/csh", @@ -103,7 +77,7 @@ pub fn new_user(username: &str, hasroot: bool, password: &str, do_hash_pass: boo pub fn hash_pass(password: &str) -> std::process::Output { let output = Command::new("openssl") - .args(["passwd", "-6", password]) + .args(["passwd", "-1", password]) .output() .expect("Failed to hash password"); output