diff options
author | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-17 18:48:36 -0500 |
---|---|---|
committer | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-17 18:48:36 -0500 |
commit | f085ab05e62d993fe5323741f410040e2d5ab94f (patch) | |
tree | 849cd53fbd2962580995759126572356d01610b7 | |
parent | 44d7399b48afac4c91a57c625087f9ef3625bcba (diff) | |
download | horizon-f085ab05e62d993fe5323741f410040e2d5ab94f.tar.gz horizon-f085ab05e62d993fe5323741f410040e2d5ab94f.tar.bz2 horizon-f085ab05e62d993fe5323741f410040e2d5ab94f.tar.xz horizon-f085ab05e62d993fe5323741f410040e2d5ab94f.zip |
hscript: Implement RootPassphrase::execute
-rw-r--r-- | hscript/user.cc | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/hscript/user.cc b/hscript/user.cc index 2b89167..21db88a 100644 --- a/hscript/user.cc +++ b/hscript/user.cc @@ -10,6 +10,10 @@ * SPDX-License-Identifier: AGPL-3.0-only */ +#include <assert.h> +#include <cstring> +#include <fstream> +#include <time.h> #include "user.hh" #include "util/output.hh" @@ -32,6 +36,48 @@ bool RootPassphrase::validate(ScriptOptions) const { return true; } -bool RootPassphrase::execute(ScriptOptions) const { - return false; +bool RootPassphrase::execute(ScriptOptions options) const { + const std::string root_line = "root:" + this->_value + ":" + + std::to_string(time(nullptr) / 86400) + ":0:::::"; + + if(options.test(Simulate)) { + std::cout << "(printf '" << root_line << "\\" + << "n'; cat /target/etc/shadow | sed '1d') > /tmp/shadow" + << std::endl + << "mv /tmp/shadow /target/etc/shadow" << std::endl + << "chown root:shadow /target/etc/shadow" << std::endl + << "chmod 640 /target/etc/shadow" << std::endl; + return true; + } + + std::ifstream old_shadow("/target/etc/shadow"); + if(!old_shadow) { + output_error("installfile:" + std::to_string(this->lineno()), + "rootpw: cannot open existing shadow file"); + return false; + } + + std::stringstream shadow_stream; + char shadow_line[200]; + /* Discard root. */ + old_shadow.getline(shadow_line, sizeof(shadow_line)); + assert(strncmp(shadow_line, "root", 4) == 0); + + /* Insert the new root line... */ + shadow_stream << root_line << std::endl; + /* ...and copy the rest of the old shadow file. */ + while(old_shadow.getline(shadow_line, sizeof(shadow_line))) { + shadow_stream << shadow_line << std::endl; + } + + old_shadow.close(); + + std::ofstream new_shadow("/target/etc/shadow", std::ios_base::trunc); + if(!new_shadow) { + output_error("installfile:" + std::to_string(this->lineno()), + "rootpw: cannot replace target shadow file"); + return false; + } + new_shadow << shadow_stream.str(); + return true; } |