summaryrefslogtreecommitdiff
path: root/hscript
diff options
context:
space:
mode:
authorA. Wilcox <AWilcox@Wilcox-Tech.com>2019-10-26 03:38:02 -0500
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2019-10-26 03:38:02 -0500
commit1efbbaf0145dd785b2cefcf1f51703372efed2f3 (patch)
tree08d0052a0c621f04b0bae00a9cbc11da02601ca8 /hscript
parent1cd72d82372a9e89bdf406f58ae8609e344a1bd1 (diff)
downloadhorizon-1efbbaf0145dd785b2cefcf1f51703372efed2f3.tar.gz
horizon-1efbbaf0145dd785b2cefcf1f51703372efed2f3.tar.bz2
horizon-1efbbaf0145dd785b2cefcf1f51703372efed2f3.tar.xz
horizon-1efbbaf0145dd785b2cefcf1f51703372efed2f3.zip
hscript: Implement NetSSID::execute
Diffstat (limited to 'hscript')
-rw-r--r--hscript/network.cc20
-rw-r--r--hscript/script.cc58
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 <algorithm>
#include <arpa/inet.h> /* inet_pton */
#include <cstring> /* memcpy */
+#include <fstream> /* ofstream for Net write */
#ifdef HAS_INSTALL_ENV
# include <linux/wireless.h> /* struct iwreq */
# include <string.h> /* 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 <algorithm>
#include <assert.h>
+#include <cstring> /* strerror */
#include <fstream>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
+#include <sys/stat.h> /* 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");