From 007eed6403d77bbfc4164114875ce6253aff7ab4 Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Thu, 7 Nov 2019 19:27:23 -0600 Subject: hscript: Implement Runner.Execute.UserAccounts, add tests --- hscript/script_e.cc | 21 +++++++++- hscript/user.cc | 91 +++++++++++++++++++++++++++++++++++++++----- tests/spec/simulator_spec.rb | 36 ++++++++++++++++++ 3 files changed, 137 insertions(+), 11 deletions(-) diff --git a/hscript/script_e.cc b/hscript/script_e.cc index 24b66ef..85c43d3 100644 --- a/hscript/script_e.cc +++ b/hscript/script_e.cc @@ -481,7 +481,26 @@ bool Script::execute() const { EXECUTE_OR_FAIL("keymap", internal->keymap) } - /* UserAccounts */ + for(auto &acct : internal->accounts) { + output_info("internal", "setting up user account " + acct.first); + + EXECUTE_OR_FAIL("username", acct.second->name) + if(acct.second->alias) { + EXECUTE_OR_FAIL("useralias", acct.second->alias) + } + if(acct.second->passphrase) { + EXECUTE_OR_FAIL("userpw", acct.second->passphrase) + } + if(!acct.second->groups.empty()) { + for(auto &grp : acct.second->groups) { + EXECUTE_OR_FAIL("usergroups", grp) + } + } + if(acct.second->icon) { + EXECUTE_OR_FAIL("usericon", acct.second->icon) + } + } + EXECUTE_OR_FAIL("timezone", internal->tzone) output_step_end("post-metadata"); diff --git a/hscript/user.cc b/hscript/user.cc index f8a46a1..4930677 100644 --- a/hscript/user.cc +++ b/hscript/user.cc @@ -17,6 +17,7 @@ #include #include #include "user.hh" +#include "util.hh" #include "util/net.hh" #include "util/output.hh" @@ -183,8 +184,8 @@ bool RootPassphrase::execute(ScriptOptions options) const { new_shadow << shadow_stream.str(); return true; #else - return false; -#endif /* HAS_INSTALL_ENV */ + return false; /* LCOV_EXCL_LINE */ +#endif /* HAS_INSTALL_ENV */ } @@ -208,8 +209,25 @@ Key *Username::parseFromData(const std::string &data, int lineno, int *errors, return new Username(lineno, data); } -bool Username::execute(ScriptOptions) const { - return false; +bool Username::execute(ScriptOptions opts) const { + output_info("installfile:" + std::to_string(line), + "username: creating account " + _value); + + if(opts.test(Simulate)) { + std::cout << "useradd -c \"Adélie User\" -m -U " << _value + << std::endl; + return true; + } + +#ifdef HAS_INSTALL_ENV + if(run_command("useradd", {"-c", "Adélie User", "-m", "-U", _value}) != 0) + { + output_error("installfile:" + std::to_string(line), + "username: failed to create user account"); + return false; + } +#endif /* HAS_INSTALL_ENV */ + return true; /* LCOV_EXCL_LINE */ } @@ -232,8 +250,24 @@ bool UserAlias::validate(ScriptOptions) const { return true; } -bool UserAlias::execute(ScriptOptions) const { - return false; +bool UserAlias::execute(ScriptOptions opts) const { + output_info("installfile:" + std::to_string(line), + "useralias: setting GECOS name for " + _username); + + if(opts.test(Simulate)) { + std::cout << "usermod -c \"" << _alias << "\" " << _username + << std::endl; + return true; + } + +#ifdef HAS_INSTALL_ENV + if(run_command("usermod", {"-c", _alias, _username}) != 0) { + output_error("installfile:" + std::to_string(line), + "useralias: failed to change GECOS of user " + _username); + return false; + } +#endif /* HAS_INSTALL_ENV */ + return true; /* LCOV_EXCL_LINE */ } @@ -263,8 +297,23 @@ bool UserPassphrase::validate(ScriptOptions) const { return true; } -bool UserPassphrase::execute(ScriptOptions) const { - return false; +bool UserPassphrase::execute(ScriptOptions opts) const { + output_info("installfile:" + std::to_string(line), + "userpw: setting passphrase for " + _username); + + if(opts.test(Simulate)) { + std::cout << "usermod -p '" << _passphrase << "' " << _username + << std::endl; + } + +#ifdef HAS_INSTALL_ENV + if(run_command("usermod", {"-p", _passphrase, _username}) != 0) { + output_error("installfile:" + std::to_string(line), + "userpw: failed to set passphrase for " + _username); + return false; + } +#endif /* HAS_INSTALL_ENV */ + return true; } @@ -345,6 +394,28 @@ bool UserGroups::validate(ScriptOptions) const { return true; } -bool UserGroups::execute(ScriptOptions) const { - return false; +bool UserGroups::execute(ScriptOptions opts) const { + output_info("installfile:" + std::to_string(line), + "usergroups: setting group membership for " + _username); + + std::string groups; + for(auto &grp : _groups) { + groups += grp + ","; + } + /* remove the last comma. */ + groups.pop_back(); + + if(opts.test(Simulate)) { + std::cout << "usermod -aG " << groups << " " << _username << std::endl; + return true; + } + +#ifdef HAS_INSTALL_ENV + if(run_command("usermod", {"-a", "-G", groups, _username}) != 0) { + output_error("installfile:" + std::to_string(line), + "usergroups: failed to add groups to " + _username); + return false; + } +#endif /* HAS_INSTALL_ENV */ + return true; /* LCOV_EXCL_LINE */ } diff --git a/tests/spec/simulator_spec.rb b/tests/spec/simulator_spec.rb index d223c0f..a208be7 100644 --- a/tests/spec/simulator_spec.rb +++ b/tests/spec/simulator_spec.rb @@ -308,4 +308,40 @@ printf '%s\\t%s\\t%s\\t%s\\t0\\t0\\n' /dev/gwyn/source /usr/src auto noatime >> expect(last_command_started.stdout).to include("ln -s /usr/share/zoneinfo/Pacific/Galapagos /target/etc/localtime") end end + context "simulating 'username' execution" do + it "creates the user account" do + use_fixture '0082-username-basic.installfile' + run_simulate + expect(last_command_started.stdout).to include('useradd -c "Adélie User" -m -U chris') + expect(last_command_started.stdout).to include('useradd -c "Adélie User" -m -U kayla') + expect(last_command_started.stdout).to include('useradd -c "Adélie User" -m -U meg') + expect(last_command_started.stdout).to include('useradd -c "Adélie User" -m -U steph') + expect(last_command_started.stdout).to include('useradd -c "Adélie User" -m -U amanda') + end + end + context "simulating 'useralias' execution" do + it "sets aliases on all named accounts" do + use_fixture '0087-useralias-basic.installfile' + run_simulate + expect(last_command_started.stdout).to include('usermod -c "Christopher" chris') + expect(last_command_started.stdout).to include('usermod -c "Kayla" kayla') + expect(last_command_started.stdout).to include('usermod -c "Meaghan" meg') + expect(last_command_started.stdout).to include('usermod -c "Stephanie" steph') + expect(last_command_started.stdout).to include('usermod -c "Amanda Jane" amanda') + end + end + context "simulating 'userpw' execution" do + it "sets the user's passphrase" do + use_fixture '0091-userpw-basic.installfile' + run_simulate + expect(last_command_started.stdout).to include("usermod -p '$6$UZJm/vBmVgyIdMZr$ppKEulz/HY0/e7RcXXujQbcqDXkUYgIqNEVPQJO6.le9kUpz8GvvRezY3ifqUUEwjhSo9tTOMG7lhqjn8gGpH0' awilfox") + end + end + context "simulating 'usergroups' execution" do + it "sets groups" do + use_fixture '0104-usergroups-basic.installfile' + run_simulate + expect(last_command_started.stdout).to include('usermod -aG ') + end + end end -- cgit v1.2.3-60-g2f50