1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
--
-- (C) 2013-22 - ntop.org
--
local sys_utils = require("sys_utils")
local config = {}
config.APPLY_ON_REBOOT = true
config.DEFAULT_BRIDGE_DEVICE_NAME = "br0"
config.DEFAULT_WIRED_DEVICE_NAME = "eth0"
config.DEFAULT_WIFI_DEVICE_NAME = "wlan0"
config.DEFAULT_COUNTRY_CODE = "IT"
config.DEBUG = false
-- ##############################################
function config.supported()
return ntop.exists("/lib/systemd/system/hostapd.service")
end
-- ##############################################
function config.execCmd(cmd, verbose)
if verbose or config.DEBUG then
traceError(TRACE_NORMAL, TRACE_CONSOLE, "[execCmd] "..cmd)
end
local out = sys_utils.execCmd(cmd)
if verbose or config.DEBUG then
traceError(TRACE_NORMAL, TRACE_CONSOLE, "[execCmd] Output: ".. tostring(out))
end
end
-- ##############################################
function config.readCmd(cmd)
return sys_utils.execShellCmd(cmd)
end
-- ##############################################
function config.getWiFiDeviceName()
local rsp = config.readCmd("cat /proc/net/wireless | grep ':'| cut -d ':' -f 1 | tr -d '[:blank:]'")
local dev = string.gsub(rsp, "\n", "")
if isEmptyString(dev) then
dev = config.DEFAULT_WIFI_DEVICE_NAME
end
return dev
end
-- ##############################################
function config.enableWiFi()
local wifi_dev = config.getWiFiDeviceName()
if wifi_dev == "" then return false end
if not config.APPLY_ON_REBOOT then
config.execCmd("ifconfig "..wifi_dev.." up")
config.execCmd("ifconfig "..wifi_dev.." power auto")
end
config.execCmd("/bin/systemctl unmask hostapd")
config.execCmd("/bin/systemctl enable hostapd")
--config.execCmd("service hostapd enable", true)
config.execCmd("/bin/systemctl enable systemd-networkd")
if not config.APPLY_ON_REBOOT then
config.execCmd("/bin/systemctl restart hostapd")
--config.execCmd("service hostapd restart", true)
end
return true
end
-- ##############################################
function config.disableWiFi()
local wifi_dev = config.getWiFiDeviceName()
if wifi_dev == "" then return false end
if not config.APPLY_ON_REBOOT then
config.execCmd("/bin/systemctl stop hostapd")
--config.execCmd("service hostapd stop", true)
end
config.execCmd("/bin/systemctl disable hostapd")
--config.execCmd("service hostapd disable", true)
if not config.APPLY_ON_REBOOT then
config.execCmd("ifconfig "..wifi_dev.." power off", true)
config.execCmd("ifconfig "..wifi_dev.." down", true)
end
return true
end
-- ##############################################
-- Configure wireless as access point
-- NOTE: password must be 8..63 chars
function config.configureWiFiAccessPoint(nf, ssid, wpa_passphrase, network_conf)
local country_code = config.DEFAULT_COUNTRY_CODE
local bridge_dev = config.DEFAULT_BRIDGE_DEVICE_NAME;
local wired_dev = config.DEFAULT_WIRED_DEVICE_NAME
local wifi_dev = config.getWiFiDeviceName()
if wifi_dev == "" then return false end
local p_len = string.len(wpa_passphrase)
if p_len < 8 or p_len > 63 then
traceError(TRACE_ERROR, TRACE_CONSOLE, "Wrong WPA password length")
return false
end
nf:write("\nauto "..wired_dev.."\n")
nf:write("iface "..wired_dev.." inet manual\n")
nf:write("\nauto "..wifi_dev.."\n")
nf:write("iface "..wifi_dev.." inet manual\n")
nf:write(" wireless-mode Master\n")
local f = sys_utils.openFile("/etc/systemd/network/bridge-"..bridge_dev..".netdev", "w")
if not f then return false end
f:write("[NetDev]\n")
f:write("Name="..bridge_dev.."\n")
f:write("Kind=bridge\n")
f:close()
local f = sys_utils.openFile("/etc/systemd/network/"..bridge_dev.."-member-"..wired_dev..".network", "w")
if not f then return false end
f:write("[Match]\n")
f:write("Name="..wired_dev.."\n")
f:write("[Network]\n")
f:write("Bridge="..bridge_dev.."\n")
f:close()
-- Configure dhcp
config.execCmd("sed -i '/^interface/ d' /etc/dhcpcd.conf")
config.execCmd("sed -i '/^denyinterfaces/ d' /etc/dhcpcd.conf")
local dhcpcd_deny = wifi_dev.." "..wired_dev
if network_conf.mode ~= "dhcp" then
dhcpcd_deny = dhcpcd_deny.." "..bridge_dev
end
config.execCmd("echo 'denyinterfaces "..dhcpcd_deny.."\\n' >> /etc/dhcpcd.conf")
if network_conf.mode == "dhcp" then
config.execCmd("echo 'interface "..bridge_dev.."\\n' >> /etc/dhcpcd.conf")
end
-- Create wpa_supplicant configuration file
local f = sys_utils.openFile("/etc/wpa_supplicant/wpa_supplicant.conf", "w")
if not f then return false end
f:write("ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\n")
f:write("update_config=1\n")
f:write("country="..country_code.."\n")
f:close()
-- Create hostapd configuration file
local f = sys_utils.openFile("/etc/hostapd/hostapd.conf", "w")
if not f then return false end
f:write("country_code="..country_code.."\n")
f:write("interface="..wifi_dev.."\n")
f:write("bridge="..bridge_dev.."\n")
f:write("ssid="..ssid.."\n")
f:write("hw_mode=g\n") -- hw_mode=a to use 5GHz
f:write("channel=11\n")
f:write("ieee80211n=1\n")
f:write("ieee80211ac=1\n")
f:write("macaddr_acl=0\n")
f:write("auth_algs=1\n")
f:write("ignore_broadcast_ssid=0\n")
f:write("wpa=2\n")
f:write("wpa_passphrase="..wpa_passphrase.."\n")
f:write("wpa_key_mgmt=WPA-PSK\n")
f:write("wpa_pairwise=TKIP\n")
f:write("rsn_pairwise=CCMP\n")
f:close()
-- Enable configuration file
config.execCmd("sed -i 's/#DAEMON_CONF/DAEMON_CONF/g' /etc/default/hostapd")
-- Set configuration file path
config.execCmd("sed -i 's/^DAEMON_CONF=.*/DAEMON_CONF=\"\\/etc\\/hostapd\\/hostapd.conf\"/g' /etc/default/hostapd")
-- Change SSID
--config.execCmd("sed -i 's/^ssid=.*/ssid="..ssid.."/g' /etc/hostapd.conf")
-- Set WPA2 / Channel 6
--config.execCmd("sed -i 's/^wpa=.*/wpa=2/g' /etc/hostapd.conf")
--config.execCmd("sed -i 's/^channel=.*/channel=6/g' /etc/hostapd.conf")
-- Change WPA passphrase
--config.execCmd("sed -i 's/^wpa_passphrase=.*/wpa_passphrase="..wpa_passphrase.."/g' /etc/hostapd.conf")
config.enableWiFi()
return true
end
-- ##############################################
return config
|