From f8037e665ff4157f977af9180576d585cca062d8 Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Mon, 20 Jun 2016 19:00:34 +0200 Subject: [PATCH] Added openwisp-remove-default-wifi script #19 --- openwisp-config/Makefile | 4 ++ openwisp-config/files/openwisp.agent | 10 ++-- .../sbin/openwisp-remove-default-wifi.lua | 28 +++++++++++ openwisp-config/tests/config/wireless | 21 +++++++++ .../tests/test_remove_default_wifi.lua | 26 ++++++++++ openwisp-config/tests/wifi/wireless | 47 +++++++++++++++++++ runtests | 1 + 7 files changed, 134 insertions(+), 3 deletions(-) create mode 100755 openwisp-config/files/sbin/openwisp-remove-default-wifi.lua create mode 100644 openwisp-config/tests/config/wireless create mode 100644 openwisp-config/tests/test_remove_default_wifi.lua create mode 100644 openwisp-config/tests/wifi/wireless diff --git a/openwisp-config/Makefile b/openwisp-config/Makefile index 965c5e4..214fc25 100644 --- a/openwisp-config/Makefile +++ b/openwisp-config/Makefile @@ -71,6 +71,10 @@ define Package/openwisp-config/install files/sbin/openwisp-restore-unmanaged.lua \ $(1)/usr/sbin/openwisp-restore-unmanaged + $(INSTALL_BIN) \ + files/sbin/openwisp-remove-default-wifi.lua \ + $(1)/usr/sbin/openwisp-remove-default-wifi + $(INSTALL_BIN) \ files/sbin/openwisp-uci-autoname.lua \ $(1)/usr/sbin/openwisp-uci-autoname diff --git a/openwisp-config/files/openwisp.agent b/openwisp-config/files/openwisp.agent index 56980b2..4890a14 100755 --- a/openwisp-config/files/openwisp.agent +++ b/openwisp-config/files/openwisp.agent @@ -298,8 +298,10 @@ call_store_unmanaged() { /usr/sbin/openwisp-store-unmanaged -o="$UNMANAGED" } -# ensures there are no anonymous UCI blocks -call_uci_autoname() { +# 1. removes default wifi-ifaces directive (LEDE or OpenWrt SSID) +# 2. ensures there are no anonymous UCI blocks +fix_uci_config() { + /usr/sbin/openwisp-remove-default-wifi output=$(/usr/sbin/openwisp-uci-autoname) if [ -n "$output" ]; then logger "The following uci configs have been renamed: $output" \ @@ -323,7 +325,9 @@ update_configuration() { return 3 fi - call_uci_autoname + # makes fixes to the default uci config so + # it's more suited for remote control + fix_uci_config # control file to avoid reloading the agent while # configuration is still being applied diff --git a/openwisp-config/files/sbin/openwisp-remove-default-wifi.lua b/openwisp-config/files/sbin/openwisp-remove-default-wifi.lua new file mode 100755 index 0000000..169369b --- /dev/null +++ b/openwisp-config/files/sbin/openwisp-remove-default-wifi.lua @@ -0,0 +1,28 @@ +#!/usr/bin/env lua + +require('uci') + +-- parse arguments +local arg={...} +for key, value in pairs(arg) do + -- test argument + if value == '--test=1' then test = true; end +end + +local standard_prefix = test and '../tests/' or '/etc/' +local standard_path = standard_prefix .. 'config/' +-- standard cursor +local cursor = uci.cursor(standard_path) +local changed = false + +cursor:foreach('wireless', 'wifi-iface', function(section) + if section['.anonymous'] and + section.encryption == 'none' and + section.mode == 'ap' and + (section.ssid == 'LEDE' or section.ssid == 'OpenWrt') + then + cursor:delete('wireless', section['.name']) + changed = true + end +end) +if changed then cursor:commit('wireless') end diff --git a/openwisp-config/tests/config/wireless b/openwisp-config/tests/config/wireless new file mode 100644 index 0000000..b3c8276 --- /dev/null +++ b/openwisp-config/tests/config/wireless @@ -0,0 +1,21 @@ + +config wifi-device 'radio0' + option path 'platform/ar934x_wmac' + option disabled '1' + option country 'IT' + option phy 'phy0' + option hwmode '11g' + option channel '9' + option type 'mac80211' + option htmode 'HT20' + +config wifi-device 'radio1' + option path 'pci0000:00/0000:00:00.0' + option disabled '1' + option country 'IT' + option phy 'phy1' + option hwmode '11a' + option channel '48' + option type 'mac80211' + option htmode 'HT20' + diff --git a/openwisp-config/tests/test_remove_default_wifi.lua b/openwisp-config/tests/test_remove_default_wifi.lua new file mode 100644 index 0000000..ab6e77f --- /dev/null +++ b/openwisp-config/tests/test_remove_default_wifi.lua @@ -0,0 +1,26 @@ +-- manually add lib dir to lua package path +package.path = package.path .. ';../files/lib/?.lua' +require('os') +require('io') +luaunit = require('luaunit') +remove_default_wifi = assert(loadfile("../files/sbin/openwisp-remove-default-wifi.lua")) + +TestRemoveDefaultWifi = { + setUp = function() + os.execute('cp ./wifi/wireless ./config') + end, + tearDown = function() + -- os.execute('rm ./config/wireless') + end +} + +function TestRemoveDefaultWifi.test_default() + remove_default_wifi('--test=1') + local file = io.open('./config/wireless') + luaunit.assertNotNil(file) + local contents = file:read('*all') + luaunit.assertNil(string.find(contents, "option ssid 'LEDE'")) + luaunit.assertNil(string.find(contents, "option ssid 'OpenWrt'")) +end + +os.exit(luaunit.LuaUnit.run()) diff --git a/openwisp-config/tests/wifi/wireless b/openwisp-config/tests/wifi/wireless new file mode 100644 index 0000000..8542b1a --- /dev/null +++ b/openwisp-config/tests/wifi/wireless @@ -0,0 +1,47 @@ +config wifi-device 'radio0' + option path 'platform/ar934x_wmac' + option disabled '1' + option country 'IT' + option phy 'phy0' + option hwmode '11g' + option channel '9' + option type 'mac80211' + option htmode 'HT20' + +config wifi-device 'radio1' + option path 'pci0000:00/0000:00:00.0' + option disabled '1' + option country 'IT' + option phy 'phy1' + option hwmode '11a' + option channel '48' + option type 'mac80211' + option htmode 'HT20' + +config wifi-iface + option ssid 'LEDE' + option encryption 'none' + option device 'radio0' + option mode 'ap' + option network 'lan' + +config wifi-iface + option ssid 'LEDE' + option encryption 'none' + option device 'radio1' + option mode 'ap' + option network 'lan' + +config wifi-iface + option ssid 'OpenWrt' + option encryption 'none' + option device 'radio0' + option mode 'ap' + option network 'lan' + +config wifi-iface + option ssid 'OpenWrt' + option encryption 'none' + option device 'radio1' + option mode 'ap' + option network 'lan' diff --git a/runtests b/runtests index 540a755..383ba1b 100755 --- a/runtests +++ b/runtests @@ -5,3 +5,4 @@ lua test_store_unmanaged.lua -v lua test_restore_unmanaged.lua -v lua test_uci_autoname.lua -v lua test_utils.lua -v +lua test_remove_deafult_wifi.lua -v