From b855a36d1af28d8c3341c8c564bf38526f512804 Mon Sep 17 00:00:00 2001 From: Michal S Date: Wed, 7 Sep 2022 20:14:31 +0100 Subject: [PATCH] Fix sudoers issue (#24) --- src/functions/users.rs | 2 +- src/internal/files.rs | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/functions/users.rs b/src/functions/users.rs index ae4d63a..963e7a2 100755 --- a/src/functions/users.rs +++ b/src/functions/users.rs @@ -55,7 +55,7 @@ pub fn new_user(username: &str, hasroot: bool, password: &str, do_hash_pass: boo format!("Add user {} to wheel group", username).as_str(), ); files_eval( - files::append_file("/mnt/etc/sudoers", "\n%wheel ALL=(ALL) ALL\n"), + files::sed_file("/mnt/etc/sudoers", "# %wheel ALL=(ALL) ALL", "%wheel ALL=(ALL) ALL"), "Add wheel group to sudoers", ); files_eval( diff --git a/src/internal/files.rs b/src/internal/files.rs index 4167da6..e107d72 100755 --- a/src/internal/files.rs +++ b/src/internal/files.rs @@ -36,6 +36,17 @@ pub fn append_file(path: &str, content: &str) -> std::io::Result<()> { Ok(()) } +pub fn sed_file(path: &str, find: &str, replace: &str) -> std::io::Result<()> { + log::info!("Sed '{}' to '{}' in file {}", find, replace, path); + let mut file = OpenOptions::new().read(true).write(true).open(path)?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + contents = contents.replace(find, replace); + file.set_len(0)?; + file.write_all(contents.as_bytes())?; + Ok(()) +} + pub fn create_directory(path: &str) -> std::io::Result<()> { std::fs::create_dir(path) }