From 1efbbaf0145dd785b2cefcf1f51703372efed2f3 Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Sat, 26 Oct 2019 03:38:02 -0500 Subject: hscript: Implement NetSSID::execute --- hscript/network.cc | 20 ++++++++++++++++++- hscript/script.cc | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/hscript/network.cc b/hscript/network.cc index 14e33b4..e375385 100644 --- a/hscript/network.cc +++ b/hscript/network.cc @@ -13,6 +13,7 @@ #include #include /* inet_pton */ #include /* memcpy */ +#include /* ofstream for Net write */ #ifdef HAS_INSTALL_ENV # include /* struct iwreq */ # include /* strerror */ @@ -395,5 +396,22 @@ bool NetSSID::validate(ScriptOptions options) const { } bool NetSSID::execute(ScriptOptions) const { - return false; + std::ofstream conf("/tmp/horizon/wpa_supplicant.conf", + std::ios_base::app); + if(!conf) { + output_error("installfile:" + std::to_string(this->lineno()), + "netssid: failed to write configuration"); + return false; + } + + conf << std::endl; + conf << "network={" << std::endl; + conf << "\tssid=\"" << this->ssid() << "\"" << std::endl; + if(this->type() != SecurityType::None) { + conf << "\tpsk=\"" << this->passphrase() << "\"" << std::endl; + } + conf << "\tpriority=5" << std::endl; + conf << "}" << std::endl; + + return !conf.fail(); } diff --git a/hscript/script.cc b/hscript/script.cc index 38d7a7c..d58efb7 100644 --- a/hscript/script.cc +++ b/hscript/script.cc @@ -12,11 +12,13 @@ #include #include +#include /* strerror */ #include #include #include #include #include +#include /* mkdir */ #include "script.hh" #include "disk.hh" @@ -879,6 +881,14 @@ bool Script::validate() const { bool Script::execute() const { bool success; + if(mkdir("/tmp/horizon", S_IRUSR | S_IWUSR | S_IXUSR) != 0) { + if(errno != EEXIST) { + output_error("internal", "could not create temporary directory", + ::strerror(errno)); + return false; + } + } + /* REQ: Runner.Execute.Verify */ output_step_start("validate"); success = this->validate(); @@ -960,12 +970,52 @@ bool Script::execute() const { /**************** NETWORK ****************/ output_step_start("net"); - for(auto &ssid : this->internal->ssids) { - if(!ssid->execute(opts)) { - EXECUTE_FAILURE("net"); - /* "Soft" error. Not fatal. */ + + if(!this->internal->ssids.empty()) { + std::ofstream wpao("/tmp/horizon/wpa_supplicant.conf", + std::ios_base::trunc); + if(wpao) { + wpao << "# Enable the control interface for wpa_cli and wpa_gui" + << std::endl + << "ctrl_interface=/var/run/wpa_supplicant" << std::endl + << "ctrl_interface_group=wheel" << std::endl + << "update_config=1" << std::endl; + } else { + output_error("internal", + "cannot write wireless networking configuration"); + } + + for(auto &ssid : this->internal->ssids) { + if(!ssid->execute(opts)) { + EXECUTE_FAILURE("net"); + /* "Soft" error. Not fatal. */ + } + } + + std::ifstream wpai("/tmp/horizon/wpa_supplicant.conf"); + if(wpai) { + if(opts.test(Simulate)) { + std::cout << "cat >/target/etc/wpa_supplicant/wpa_supplicant.conf " + << "<<- WPA_EOF" << std::endl; + std::cout << wpai.rdbuf(); + std::cout << std::endl << "WPA_EOF" << std::endl; + } else { + std::ofstream target_wpa("/target/etc/wpa_supplicant/" + "wpa_supplicant.conf", + std::ios_base::trunc); + if(!target_wpa) { + output_error("internal", "cannot save wireless networking " + "configuration to target"); + } else { + target_wpa << wpai.rdbuf(); + } + } + } else { + output_error("internal", + "cannot read wireless networking configuration"); } } + for(auto &addr : this->internal->addresses) { if(!addr->execute(opts)) { EXECUTE_FAILURE("net"); -- cgit v1.2.3-70-g09d2