From 1015f967271a70c9675db72915fe37f827114458 Mon Sep 17 00:00:00 2001 From: Amy Date: Fri, 21 Jan 2022 20:52:03 +0100 Subject: [PATCH] user management, desktop and network backend mostly finished --- src/functions/desktops.rs | 112 +++++++++++++++++++++++++------------ src/functions/mod.rs | 8 +-- src/functions/network.rs | 46 +++++++++++---- src/functions/partition.rs | 14 ++--- src/functions/users.rs | 100 ++++++++++++++++++++++++++++----- src/internal/files.rs | 66 ++++++++++++++++++++++ src/internal/install.rs | 8 ++- src/internal/mod.rs | 7 +-- src/internal/strings.rs | 27 +-------- src/main.rs | 16 ++++-- 10 files changed, 297 insertions(+), 107 deletions(-) create mode 100644 src/internal/files.rs diff --git a/src/functions/desktops.rs b/src/functions/desktops.rs index cb9c147..df8380f 100755 --- a/src/functions/desktops.rs +++ b/src/functions/desktops.rs @@ -1,35 +1,77 @@ -use crate::internal::*; - -pub fn choose_pkgs(desktop_setup: &str) { - println!("Installing {}", desktop_setup); - match desktop_setup { - "onyx" => { - install(vec!("onyx", "lightdm", "lightdm-gtk-greeter", "lightdm-gtk-greeter-settings")); - } - "gnome" => { - install(vec!("gnome","gnome-tweaks","chrome-gnome-shell","gdm")); - }, - "kde" => { - install(vec!("kde", "plasma", "plasma-wayland-session", "kde-applications", "sddm")); - }, - "budgie" => { - install(vec!("budgie-desktop", "gnome", "lightdm", "lightdm-gtk-greeter", "lightdm-gtk-greeter-settings")); - }, - "cinnamon" => { - install(vec!("cinnamon", "lightdm", "lightdm-gtk-greeter", "lightdm-gtk-greeter-settings", "metacity", "gnome-shell")); - }, - "mate" => { - install(vec!("mate", "lightdm", "lightdm-gtk-greeter", "lightdm-gtk-greeter-settings", "mate-extra")); - }, - "xfce" => { - install(vec!("xfce4", "lightdm", "lightdm-gtk-greeter", "lightdm-gtk-greeter-settings", "xfce4-goodies")); - }, - "enlightenment" => { - install(vec!("enlightenment", "lightdm", "lightdm-gtk-greeter", "lightdm-gtk-greeter-settings", "terminology")); - }, - - _ => { - crash("Unknown desktop setup".to_string(), 1); - } - } -} \ No newline at end of file +use crate::internal::*; + +pub fn choose_pkgs(desktop_setup: &str) { + println!("Installing {}", desktop_setup); + match desktop_setup { + "onyx" => { + install(vec![ + "onyx", + "lightdm", + "lightdm-gtk-greeter", + "lightdm-gtk-greeter-settings", + ]); + } + "gnome" => { + install(vec!["gnome", "gnome-tweaks", "chrome-gnome-shell", "gdm"]); + } + "kde" => { + install(vec![ + "kde", + "plasma", + "plasma-wayland-session", + "kde-applications", + "sddm", + ]); + } + "budgie" => { + install(vec![ + "budgie-desktop", + "gnome", + "lightdm", + "lightdm-gtk-greeter", + "lightdm-gtk-greeter-settings", + ]); + } + "cinnamon" => { + install(vec![ + "cinnamon", + "lightdm", + "lightdm-gtk-greeter", + "lightdm-gtk-greeter-settings", + "metacity", + "gnome-shell", + ]); + } + "mate" => { + install(vec![ + "mate", + "lightdm", + "lightdm-gtk-greeter", + "lightdm-gtk-greeter-settings", + "mate-extra", + ]); + } + "xfce" => { + install(vec![ + "xfce4", + "lightdm", + "lightdm-gtk-greeter", + "lightdm-gtk-greeter-settings", + "xfce4-goodies", + ]); + } + "enlightenment" => { + install(vec![ + "enlightenment", + "lightdm", + "lightdm-gtk-greeter", + "lightdm-gtk-greeter-settings", + "terminology", + ]); + } + + _ => { + crash("Unknown desktop setup".to_string(), 1); + } + } +} diff --git a/src/functions/mod.rs b/src/functions/mod.rs index e613d09..871009c 100755 --- a/src/functions/mod.rs +++ b/src/functions/mod.rs @@ -1,4 +1,4 @@ -pub mod partition; -pub mod network; -pub mod users; -pub mod desktops; \ No newline at end of file +pub mod desktops; +pub mod network; +pub mod partition; +pub mod users; diff --git a/src/functions/network.rs b/src/functions/network.rs index 8c471b9..69aaf3b 100755 --- a/src/functions/network.rs +++ b/src/functions/network.rs @@ -1,11 +1,35 @@ -pub fn set_hostname(hostname: &str) { - println!("Setting hostname to {}", hostname); -} - -pub fn enable_ipv6(ipv6: bool) { - if ipv6 { - println!("enabling ipv6"); - } else { - println!("disabling ipv6"); - } -} \ No newline at end of file +use crate::internal::*; + +pub fn set_hostname(hostname: &str) { + println!("Setting hostname to {}", hostname); + files::create_file("/etc/hostname"); + let return_val = files::append_file("/etc/hostname", hostname); + match return_val { + Ok(_) => { + info(format!("Set hostname to {}", hostname)); + } + Err(e) => { + crash( + format!("Failed to set hostname to {}, Error: {}", hostname, e), + 1, + ); + } + } +} + +pub fn enable_ipv6(ipv6: bool) { + if ipv6 { + println!("Enabling IPv6"); + let return_val = files::append_file("/etc/hosts", "::1 localhost"); + match return_val { + Ok(_) => { + info("Enabled IPv6".to_string()); + } + Err(e) => { + crash(format!("Failed to enable IPv6, Error: {}", e), 1); + } + } + } else { + println!("Not enabling ipv6"); + } +} diff --git a/src/functions/partition.rs b/src/functions/partition.rs index 451c2e2..7894051 100755 --- a/src/functions/partition.rs +++ b/src/functions/partition.rs @@ -1,7 +1,7 @@ -pub fn partition(device: &str, mode: &str) { - if mode == "manual" { - println!("Manual partitioning"); - } else { - println!("automatically partitioning {}", device); - } -} \ No newline at end of file +pub fn partition(device: &str, mode: &str) { + if mode == "manual" { + println!("Manual partitioning"); + } else { + println!("automatically partitioning {}", device); + } +} diff --git a/src/functions/users.rs b/src/functions/users.rs index ed0f48a..0bf3582 100755 --- a/src/functions/users.rs +++ b/src/functions/users.rs @@ -1,13 +1,87 @@ -pub fn new_user(username: &str, hasroot: bool, password: &str) { - println!("Creating new user '{}'", username); - if hasroot { - println!("User '{}' will have root privileges", username); - } else { - println!("User '{}' will not have root privileges", username); - } - println!("Setting password for user '{}' to '{}'", username, password); -} - -pub fn root_pass(root_pass: &str) { - println!("Setting root password to '{}'", root_pass); -} \ No newline at end of file +use crate::internal::*; +use std::process::Command; + +pub fn new_user(username: &str, hasroot: bool, password: &str) { + let return_val = Command::new("useradd") + .arg("-m") + .arg("-s") + .arg("/bin/bash") + .arg(username) + .output(); + match return_val { + Ok(_) => { + info(format!("Created user {}", username)); + } + Err(e) => { + crash( + format!("Failed to create user {}, Error: {}", username, e), + 1, + ); + } + } + if hasroot { + let return_val = Command::new("usermod") + .arg("-a") + .arg("-G") + .arg("wheel") + .arg(username) + .output(); + match return_val { + Ok(_) => { + info(format!("Added user {} to group wheel", username)); + } + Err(e) => { + crash(format!("Failed to add user {}, Error: {}", username, e), 1); + } + } + } + let return_val = Command::new("arch-chroot") + .arg("/mnt") + .arg("usermod") + .arg("--password") + .arg("$(echo") + .arg(format!("${{{}}}", password)) + .arg("|") + .arg("openssl") + .arg("passwd") + .arg("-1") + .arg("-stdin)") + .arg(username) + .output(); + match return_val { + Ok(_) => { + info(format!("Set password for user {}", username)); + } + Err(e) => { + crash( + format!("Failed to set password for user {}, Error: {}", username, e), + 1, + ); + } + } +} + +pub fn root_pass(root_pass: &str) { + println!("Setting root password to '{}'", root_pass); + let return_val = Command::new("arch-chroot") + .arg("/mnt") + .arg("usermod") + .arg("--password") + .arg("$(echo") + .arg(format!("${{{}}}", root_pass)) + .arg("|") + .arg("openssl") + .arg("passwd") + .arg("-1") + .arg("-stdin)") + .arg("root") + .output(); + match return_val { + Ok(_) => { + info("Set root password".to_string()); + } + Err(e) => { + crash(format!("Failed to set root password, Error: {}", e), 1); + } + } +} diff --git a/src/internal/files.rs b/src/internal/files.rs new file mode 100644 index 0000000..e8c6f76 --- /dev/null +++ b/src/internal/files.rs @@ -0,0 +1,66 @@ +use crate::internal::*; +use std::fs::{File, OpenOptions}; +use std::io::prelude::*; + +pub fn create_file(path: &str) { + let return_val = File::create(path); + match return_val { + Ok(_file) => { + info(format!("Created file {}", path)); + } + Err(e) => { + crash(format!("Failed to create file {}, Error: {}", path, e), 1); + } + } +} + +pub fn append_file(path: &str, content: &str) -> std::io::Result<()> { + info(format!("Appending {} to file {}", content, path)); + let mut file = OpenOptions::new().append(true).open(path)?; + file.write(content.as_bytes())?; + Ok(()) +} + +pub fn delete_file(path: &str) { + let return_val = std::fs::remove_file(path); + match return_val { + Ok(_) => { + info(format!("Deleted file {}", path)); + } + Err(e) => { + crash(format!("Failed to delete file {}, Error: {}", path, e), 1); + } + } +} + +pub fn create_directory(path: &str) { + let return_val = std::fs::create_dir(path); + match return_val { + Ok(_) => { + info(format!("Created directory {}", path)); + } + Err(e) => { + crash( + format!("Failed to create directory {}, Error: {}", path, e), + 1, + ); + } + } +} + +pub fn overwrite_file(path: &str, content: &str) { + delete_file(path); + create_file(path); + let return_val = append_file(path, content); + match return_val { + Ok(_) => { + info(format!("Overwrote file {}", path)); + } + Err(e) => { + crash( + format!("Failed to overwrite file {}, Error: {}", path, e), + 1, + ); + } + } +} diff --git a/src/internal/install.rs b/src/internal/install.rs index ed226a0..f0cd969 100644 --- a/src/internal/install.rs +++ b/src/internal/install.rs @@ -1,5 +1,9 @@ use std::process::Command; pub fn install(pkgs: Vec<&str>) { - Command::new("pacman").arg("-S").args(pkgs).output().expect("Failed to install packages"); -} \ No newline at end of file + Command::new("pacstrap") + .arg("/mnt") + .args(pkgs) + .output() + .expect("Failed to install packages"); +} diff --git a/src/internal/mod.rs b/src/internal/mod.rs index 9f7b2bd..a32ed5a 100644 --- a/src/internal/mod.rs +++ b/src/internal/mod.rs @@ -1,3 +1,4 @@ +pub mod files; pub mod install; pub mod strings; @@ -17,10 +18,6 @@ pub fn log(a: String) { strings::log(a); } -pub fn prompt(a: String, b: bool) -> bool { - strings ::prompt(a, b) -} - #[macro_export] macro_rules! uwu { ($x:expr) => {{ @@ -34,4 +31,4 @@ macro_rules! uwu { let uwu = uwu.replace("NA", "NYA"); uwu }}; -} \ No newline at end of file +} diff --git a/src/internal/strings.rs b/src/internal/strings.rs index f0bab70..f55e068 100644 --- a/src/internal/strings.rs +++ b/src/internal/strings.rs @@ -1,9 +1,9 @@ -use std::{env, io}; +use crate::uwu; use std::io::Write; use std::process::exit; use std::str::FromStr; use std::time::UNIX_EPOCH; -use crate::uwu; +use std::{env, io}; pub fn info(a: String) { let a = if env::var("JADE_UWU").unwrap_or_else(|_| "".to_string()) == "true" { uwu!(&a) @@ -38,26 +38,3 @@ pub fn log(a: String) { a ); } -pub fn prompt(a: String, b: bool) -> bool { - let default = ["[Y/n]", "[y/N]"]; - let i = if b { 0 } else { 1 }; - let a = if env::var("JADE_UWU").unwrap_or_else(|_| "".to_string()) == "true" { - uwu!(&a) - } else { - a - }; - print!( - "\x1b[2;22;35m?\x1b[0m \x1b[1;37m{}\x1b[0m \x1b[2;22;37m{}\x1b[0m: ", - a, default[i] - ); - let mut yn: String = String::new(); - io::stdout().flush().ok(); - let _ = std::io::stdin().read_line(&mut yn); - if yn.trim().to_lowercase() == "n" || yn.trim().to_lowercase() == "no" { - false - } else if yn.trim().to_lowercase() == "y" || yn.trim().to_lowercase() == "yes" { - true - } else { - b - } -} diff --git a/src/main.rs b/src/main.rs index 9979078..43195ed 100755 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ mod functions; mod internal; use crate::functions::*; -use clap::{App, Arg, SubCommand}; +use clap::{App, Arg, SubCommand}; fn main() { let app = App::new("jade") @@ -98,11 +98,13 @@ fn main() { .help("The desktop setup to use") .required(true), ), - ).get_matches(); - + ).get_matches(); if let Some(app) = app.subcommand_matches("partition") { - partition::partition(app.value_of("device").unwrap(), app.value_of("mode").unwrap()); + partition::partition( + app.value_of("device").unwrap(), + app.value_of("mode").unwrap(), + ); } else if let Some(app) = app.subcommand_matches("locale") { let kbrlayout = app.value_of("keyboard").unwrap(); let timezn = app.value_of("timezone").unwrap(); @@ -115,7 +117,11 @@ fn main() { network::set_hostname(app.value_of("hostname").unwrap()) } else if let Some(app) = app.subcommand_matches("users") { if let Some(app) = app.subcommand_matches("newUser") { - users::new_user(app.value_of("username").unwrap(), app.value_of("hasroot").unwrap().parse::().unwrap(), app.value_of("password").unwrap()); + users::new_user( + app.value_of("username").unwrap(), + app.value_of("hasroot").unwrap().parse::().unwrap(), + app.value_of("password").unwrap(), + ); } else if let Some(app) = app.subcommand_matches("rootPass") { let rootpass = app.value_of("rootPass").unwrap(); println!("{}", rootpass);