From f085ab05e62d993fe5323741f410040e2d5ab94f Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Thu, 17 Oct 2019 18:48:36 -0500 Subject: hscript: Implement RootPassphrase::execute --- hscript/user.cc | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file 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 +#include +#include +#include #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; } -- cgit v1.2.3-70-g09d2