freedombone/src/freedombone-utils-wifi

666 lines
20 KiB
Plaintext
Raw Permalink Normal View History

2016-07-03 17:13:34 +02:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2016-07-03 17:13:34 +02:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2016-07-03 17:13:34 +02:00
#
# Wifi functions
2018-04-08 14:30:21 +02:00
#
2016-07-03 17:13:34 +02:00
# License
# =======
#
# Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
2016-07-03 17:13:34 +02:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
WIFI_CHANNEL=2
WIFI_INTERFACE=wlan0
WIFI_TYPE='wpa2-psk'
WIFI_SSID=
WIFI_PASSPHRASE=
WIFI_HOTSPOT='no'
WIFI_NETWORKS_FILE=~/${PROJECT_NAME}-wifi.cfg
# repo for atheros AR9271 wifi driver
ATHEROS_WIFI_REPO="https://github.com/qca/open-ath9k-htc-firmware.git"
function default_network_config {
echo '# This file describes the network interfaces available on your system' > /etc/network/interfaces
echo '# and how to activate them. For more information, see interfaces(5).' >> /etc/network/interfaces
echo 'source /etc/network/interfaces.d/*' >> /etc/network/interfaces
}
2017-06-19 15:48:51 +02:00
# Debian stretch has a problem where the formerly predictable wlan0 and eth0
# device names get assigned random names. This is a hacky workaround.
2017-06-19 16:27:21 +02:00
# Also adding net.ifnames=0 to kernel options on bootloader may work.
2017-06-19 15:48:51 +02:00
function enable_predictable_device_names {
2017-08-07 15:46:08 +02:00
test_predictable=$(ls -la /etc/udev/rules.d/80-net-setup-link.rules)
if [[ "$test_predictable" != *"/dev/null" ]]; then
ln -s /dev/null /etc/udev/rules.d/80-net-setup-link.rules
update-initramfs -u
fi
2017-06-19 15:48:51 +02:00
}
2016-10-22 00:19:04 +02:00
function wifi_is_running {
wifi_state=$(wpa_cli status)
if [[ "$wifi_state" == *"COMPLETED"* ]]; then
echo "1"
else
echo "0"
fi
}
2016-10-22 22:25:42 +02:00
function wifi_static_network_interface {
NETWORK_IS_STATIC=0
read_config_param "NETWORK_IS_STATIC"
if [ ${NETWORK_IS_STATIC} -eq 0 ]; then
echo '#this line must always be here' > /etc/network/interfaces.d/static
echo 'iface default inet dhcp' >> /etc/network/interfaces.d/static
2016-10-22 22:25:42 +02:00
else
2016-10-23 15:15:48 +02:00
read_config_param "LOCAL_NETWORK_STATIC_IP_ADDRESS"
2016-10-22 22:25:42 +02:00
read_config_param "ROUTER_IP_ADDRESS"
2018-03-02 21:07:54 +01:00
{ echo 'iface default inet static';
echo " address ${LOCAL_NETWORK_STATIC_IP_ADDRESS}";
echo ' netmask 255.255.255.0';
echo " gateway ${ROUTER_IP_ADDRESS}"; } > /etc/network/interfaces.d/static
2016-10-22 22:25:42 +02:00
fi
}
function setup_wifi_atheros {
2016-10-21 14:56:13 +02:00
if [[ $(running_as_root) == "0" ]]; then
return
fi
apt-get -yq install firmware-linux-free
atheros_drivers_file=drivers/firmware-ath9k-htc.deb
if [ ! -f $atheros_drivers_file ]; then
2018-05-02 12:22:36 +02:00
if [ ! -f "$HOME/${PROJECT_NAME}/$atheros_drivers_file" ]; then
return
else
2018-05-02 12:22:36 +02:00
atheros_drivers_file="$HOME/${PROJECT_NAME}/$atheros_drivers_file"
fi
2016-10-21 14:46:40 +02:00
else
atheros_drivers_file=$(pwd)/$atheros_drivers_file
fi
2018-03-02 21:07:54 +01:00
dpkg -i "$atheros_drivers_file"
2016-10-21 15:00:29 +02:00
reset_usb_devices
2018-03-07 16:09:31 +01:00
update_wifi_adaptors 'wlan'
2018-03-02 21:07:54 +01:00
if [ "$IFACE" ]; then
wpa_action "${IFACE}" stop
wpa_cli -i "${IFACE}" terminate
2017-06-19 19:34:50 +02:00
#ifconfig $IFACE up
2018-03-02 21:07:54 +01:00
ifup "$IFACE"
2016-10-21 15:18:05 +02:00
fi
}
2016-07-03 17:13:34 +02:00
function setup_wifi {
2016-10-08 20:32:04 +02:00
if [[ $SYSTEM_TYPE == "mesh"* ]]; then
2016-10-06 14:52:12 +02:00
return
fi
if [ ! $WIFI_SSID ]; then
return
fi
if [ ${#WIFI_SSID} -lt 2 ]; then
return
fi
2016-10-16 20:50:56 +02:00
2018-02-25 13:50:46 +01:00
if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
2016-10-06 14:52:12 +02:00
return
fi
2016-07-03 17:13:34 +02:00
2016-10-06 14:52:12 +02:00
HOTSPOT='no'
if [[ $WIFI_HOTSPOT != 'no' ]]; then
HOTSPOT='yes'
fi
2016-07-03 17:13:34 +02:00
2018-03-02 21:07:54 +01:00
if [ -f "$WIFI_NETWORKS_FILE" ]; then
"${PROJECT_NAME}-wifi" --networks "$WIFI_NETWORKS_FILE"
2018-02-25 13:50:46 +01:00
mark_complete "${FUNCNAME[0]}"
2016-10-06 14:52:12 +02:00
return
fi
2016-07-03 17:13:34 +02:00
2016-10-06 14:52:12 +02:00
if [[ $WIFI_TYPE != 'none' ]]; then
if [ ! $WIFI_PASSPHRASE ]; then
echo $'No wifi passphrase was given'
return
fi
if [ ${#WIFI_PASSPHRASE} -lt 2 ]; then
echo $'Wifi passphrase was too short'
return
fi
2018-03-02 21:07:54 +01:00
"${PROJECT_NAME}-wifi" -s "$WIFI_SSID" -t "$WIFI_TYPE" -p "$WIFI_PASSPHRASE" --hotspot "$HOTSPOT" --networks "$WIFI_NETWORKS_FILE"
2016-10-06 14:52:12 +02:00
else
2018-03-02 21:07:54 +01:00
"${PROJECT_NAME}-wifi" -s "$WIFI_SSID" -t "$WIFI_TYPE" --hotspot "$HOTSPOT" --networks "$WIFI_NETWORKS_FILE"
2016-10-06 14:52:12 +02:00
fi
2018-02-25 13:50:46 +01:00
mark_completed "${FUNCNAME[0]}"
2016-07-03 17:13:34 +02:00
}
# ath9k_htc driver
function install_atheros_wifi {
2018-02-25 13:50:46 +01:00
if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
2016-10-06 14:52:12 +02:00
return
fi
2018-03-02 21:07:54 +01:00
if [ "$INSTALLING_ON_BBB" != "yes" ]; then
return
fi
if [[ $ENABLE_BATMAN != "yes" ]]; then
return
fi
2018-03-02 21:07:54 +01:00
if [ -d "$INSTALL_DIR/open-ath9k-htc-firmware" ]; then
return
fi
# have drivers already been installed ?
if [ -f /lib/firmware/htc_9271.fw ]; then
return
fi
apt-get -yq install build-essential cmake git m4 texinfo
2018-03-02 21:07:54 +01:00
if [ ! -d "$INSTALL_DIR" ]; then
mkdir -p "$INSTALL_DIR"
fi
2018-03-02 21:07:54 +01:00
cd "$INSTALL_DIR" || exit 24678246
if [ ! -d "$INSTALL_DIR/open-ath9k-htc-firmware" ]; then
function_check git_clone
2018-03-02 21:07:54 +01:00
if ! git_clone "$ATHEROS_WIFI_REPO" "$INSTALL_DIR/open-ath9k-htc-firmware"; then
rm -rf "$INSTALL_DIR/open-ath9k-htc-firmware"
exit 74283
fi
fi
2018-03-02 21:07:54 +01:00
cd "$INSTALL_DIR/open-ath9k-htc-firmware" || exit 24682468
git checkout 1.4.0
2018-03-02 21:07:54 +01:00
if ! make toolchain; then
rm -rf "$INSTALL_DIR/open-ath9k-htc-firmware"
exit 24820
fi
2018-03-02 21:07:54 +01:00
if ! make firmware; then
rm -rf "$INSTALL_DIR/open-ath9k-htc-firmware"
exit 63412
fi
2018-03-02 21:07:54 +01:00
if ! cp target_firmware/*.fw /lib/firmware/; then
exit 74681
fi
2018-02-25 13:50:46 +01:00
mark_completed "${FUNCNAME[0]}"
2016-07-03 17:13:34 +02:00
}
2016-10-21 11:24:07 +02:00
function update_wifi_adaptors {
2018-03-07 16:09:31 +01:00
interface_type="$1"
2016-10-21 11:24:07 +02:00
IFACE=
IFACE_SECONDARY=
2018-03-07 16:09:31 +01:00
if [ ! "$interface_type" ]; then
interface_type='wlan'
fi
for i in $(seq 10 -1 0); do
2018-03-07 16:09:31 +01:00
ifdown --force "${interface_type}${i}" 2> /dev/null
done
2016-10-21 11:24:07 +02:00
for i in $(seq 10 -1 0); do
2018-03-07 16:09:31 +01:00
if grep -q "${interface_type}${i}" /proc/net/dev; then
2016-10-21 11:24:07 +02:00
if [ ! $IFACE ]; then
IFACE="wlan${i}"
else
2018-03-07 16:09:31 +01:00
IFACE_SECONDARY="${interface_type}${i}"
2016-10-21 11:24:07 +02:00
return
fi
fi
done
}
function wifi_get_psk {
ssid=$1
passphrase=$2
psk=$(wpa_passphrase "$ssid" "$passphrase" | grep 'psk=' | sed -n 2p | awk -F '=' '{print $2}')
2018-03-02 21:07:54 +01:00
echo "$psk"
2016-10-21 11:24:07 +02:00
}
function hotspot_off {
if [ ! -f /etc/hostapd/hostapd.conf ]; then
return
fi
systemctl stop hostapd
rm /etc/hostapd/hostapd.conf
if [ -f /etc/network/interfaces_original_static ]; then
cp /etc/network/interfaces_original_static /etc/network/interfaces.d/static
else
if [ -f /etc/network/interfaces.d/static ]; then
rm /etc/network/interfaces.d/static
fi
fi
if [ -f /etc/network/interfaces_original_wifi ]; then
cp /etc/network/interfaces_original_wifi /etc/network/interfaces.d/wifi
2016-10-21 11:24:07 +02:00
else
if [ -f /etc/network/interfaces.d/wifi ]; then
rm /etc/network/interfaces.d/wifi
fi
2016-10-21 11:24:07 +02:00
fi
if [ -f /etc/network/interfaces.d/bridge ]; then
rm /etc/network/interfaces.d/bridge
fi
default_network_config
2016-10-21 11:24:07 +02:00
2016-10-21 19:16:00 +02:00
wpa_action ${WIFI_INTERFACE} stop
wpa_cli -i ${WIFI_INTERFACE} terminate
2016-10-21 19:02:56 +02:00
systemctl restart network-manager
2016-10-21 11:24:07 +02:00
}
function hotspot_on {
if [ ! -f /etc/default/hostapd ]; then
echo $'/etc/default/hostapd was not found'
exit 67241
fi
if [ ${#WIFI_PASSPHRASE} -lt 8 ]; then
echo $'Wifi hotspot passphrase is too short'
exit 25719
fi
sed -i 's|#DAEMON_CONF=.*|DAEMON_CONF="/etc/hostapd/hostapd.conf"|g' /etc/default/hostapd
2018-03-02 21:07:54 +01:00
{ echo '### Wireless network name ###';
echo "interface=$WIFI_INTERFACE";
echo '';
echo '### Set your bridge name ###';
echo 'bridge=br0';
echo '';
echo 'driver=nl80211';
echo "country_code=UK";
echo "ssid=$WIFI_SSID";
echo 'hw_mode=g';
echo 'channel=6';
echo 'wpa=2';
echo "wpa_passphrase=$WIFI_PASSPHRASE";
echo '';
echo '## Key management algorithms ##';
echo 'wpa_key_mgmt=WPA-PSK';
echo '';
echo '## Set cipher suites (encryption algorithms) ##';
echo '## TKIP = Temporal Key Integrity Protocol';
echo '## CCMP = AES in Counter mode with CBC-MAC';
echo 'wpa_pairwise=TKIP';
echo 'rsn_pairwise=CCMP';
echo '';
echo '## Shared Key Authentication ##'
echo 'auth_algs=1';
echo '';
echo '## Accept all MAC address ###';
echo 'macaddr_acl=0'; } > /etc/hostapd/hostapd.conf
2016-10-21 11:24:07 +02:00
if [ ! -f /etc/network/interfaces_original_static ]; then
if [ -f /etc/network/interfaces.d/static ]; then
cp /etc/network/interfaces.d/static /etc/network/interfaces_original_static
fi
fi
if [ ! -f /etc/network/interfaces_original_wifi ]; then
if [ -f /etc/network/interfaces.d/wifi ]; then
cp /etc/network/interfaces.d/wifi /etc/network/interfaces_original_wifi
2016-10-21 11:24:07 +02:00
fi
fi
default_network_config
echo "allow-hotplug $WIFI_INTERFACE" > /etc/network/interfaces.d/wifi
echo "iface $WIFI_INTERFACE inet manual" >> /etc/network/interfaces.d/wifi
echo 'allow-hotplug eth0' >> /etc/network/interfaces.d/static
echo 'iface eth0 inet manual' >> /etc/network/interfaces.d/static
echo 'iface br0 inet static' >> /etc/network/interfaces.d/bridge
echo " bridge_ports $WIFI_INTERFACE eth0" >> /etc/network/interfaces.d/bridge
2016-10-21 11:24:07 +02:00
systemctl restart network-manager
2017-06-19 19:34:50 +02:00
ifup $WIFI_INTERFACE
2016-10-21 11:24:07 +02:00
systemctl restart hostapd
}
2016-10-22 22:25:42 +02:00
function wifi_store_original_network_settings {
2016-10-21 11:24:07 +02:00
if [ ! -f /etc/network/interfaces_original ]; then
if ! grep -q "# wifi enabled" /etc/network/interfaces; then
cp /etc/network/interfaces /etc/network/interfaces_original
fi
fi
2016-10-22 22:25:42 +02:00
}
2016-10-23 00:04:53 +02:00
function wifi_original_network_settings {
remove_config_param "NETWORK_IS_STATIC"
if [ -f /etc/network/interfaces.d/static ]; then
rm /etc/network/interfaces.d/static
fi
if [ -f /etc/network/interfaces.d/wifi ]; then
rm /etc/network/interfaces.d/wifi
fi
if [ -f /etc/network/interfaces.d/bridge ]; then
rm /etc/network/interfaces.d/bridge
2016-10-23 00:04:53 +02:00
fi
}
2016-10-22 22:25:42 +02:00
function wifi_wpa2_psk {
ssid=$1
passphrase=$2
wifi_store_original_network_settings
2016-10-21 11:24:07 +02:00
echo 'allow-hotplug eth0' > /etc/network/interfaces.d/static
echo 'iface eth0 inet dhcp' >> /etc/network/interfaces.d/static
echo "allow-hotplug ${WIFI_INTERFACE}" > /etc/network/interfaces.d/wifi
echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces.d/wifi
echo " wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces.d/wifi
2016-10-22 22:25:42 +02:00
wifi_static_network_interface
2016-10-21 11:24:07 +02:00
2018-03-02 21:07:54 +01:00
wpa_passphrase "$ssid" "$passphrase" > "$WIFI_CONFIG"
2016-10-21 11:24:07 +02:00
systemctl restart network-manager
2017-06-19 19:34:50 +02:00
ifup $WIFI_INTERFACE
2016-10-21 11:24:07 +02:00
}
function wifi_none {
ssid=$1
2016-10-22 22:25:42 +02:00
wifi_store_original_network_settings
2016-10-21 11:24:07 +02:00
echo 'allow-hotplug eth0' > /etc/network/interfaces.d/static
echo 'iface eth0 inet dhcp' >> /etc/network/interfaces.d/static
echo "allow-hotplug ${WIFI_INTERFACE}" > /etc/network/interfaces.d/wifi
echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces.d/wifi
echo " wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces.d/wifi
2016-10-22 22:25:42 +02:00
wifi_static_network_interface
2016-10-21 11:24:07 +02:00
2018-03-02 21:07:54 +01:00
{ echo 'ctrl_interface=/run/wpa_supplicant';
echo 'update_config=1';
echo 'eapol_version=1';
echo '';
echo 'network={'; } > "$WIFI_CONFIG"
2016-10-21 20:28:34 +02:00
if [[ "${ssid}" != $'any' && "${ssid}" != $'all' && "${ssid}" != $'open' ]]; then
2018-03-02 21:07:54 +01:00
echo " ssid=\"${ssid}\"" >> "$WIFI_CONFIG"
2016-10-21 20:28:34 +02:00
fi
2018-03-02 21:07:54 +01:00
echo ' key_mgmt=NONE' >> "$WIFI_CONFIG"
echo '}' >> "$WIFI_CONFIG"
2016-10-21 11:24:07 +02:00
systemctl restart network-manager
2017-06-19 19:34:50 +02:00
ifup $WIFI_INTERFACE
2016-10-21 11:24:07 +02:00
}
function networks_from_file {
2018-03-02 21:07:54 +01:00
if [ ! -f "$WIFI_NETWORKS_FILE" ]; then
2016-10-21 11:24:07 +02:00
exit 4
fi
2016-10-21 16:13:48 +02:00
if [[ $(config_param_exists "WIFI_INTERFACE") == "0" ]]; then
exit 5
fi
read_config_param "WIFI_INTERFACE"
2016-10-22 22:25:42 +02:00
wifi_store_original_network_settings
2016-10-21 11:24:07 +02:00
echo 'allow-hotplug eth0' > /etc/network/interfaces.d/static
echo 'iface eth0 inet dhcp' >> /etc/network/interfaces.d/static
2018-03-02 21:07:54 +01:00
{ echo "allow-hotplug ${WIFI_INTERFACE}";
echo "iface ${WIFI_INTERFACE} inet manual";
echo " wpa-roam $WIFI_CONFIG"; } > /etc/network/interfaces.d/wifi
2016-10-22 22:25:42 +02:00
wifi_static_network_interface
2016-10-21 11:24:07 +02:00
# remove wpa_supplicant.conf if it exists
2018-03-02 21:07:54 +01:00
if [ -f "$WIFI_CONFIG" ]; then
rm -f "$WIFI_CONFIG"
2016-10-21 11:24:07 +02:00
fi
2018-03-02 21:07:54 +01:00
{ echo 'ctrl_interface=/run/wpa_supplicant';
echo 'update_config=1';
echo 'eapol_version=1';
echo ''; } > "$WIFI_CONFIG"
2016-10-21 19:23:59 +02:00
2016-10-21 11:24:07 +02:00
ctr=0
while read -r line
do
if [ ${#line} -gt 1 ]; then
if [[ "$line" != '#'* ]]; then
if [ $ctr -eq 0 ]; then
WIFI_SSID="$line"
fi
if [ $ctr -eq 1 ]; then
WIFI_TYPE="$line"
if [[ $WIFI_TYPE == $'none' || $WIFI_TYPE == $'open' ]]; then
2018-03-02 21:07:54 +01:00
echo 'network={' >> "$WIFI_CONFIG"
2016-10-21 19:56:01 +02:00
if [[ "${WIFI_SSID}" != $'any' && "${WIFI_SSID}" != $'all' && "${WIFI_SSID}" != $'open' ]]; then
2018-03-02 21:07:54 +01:00
echo " ssid=\"${WIFI_SSID}\"" >> "$WIFI_CONFIG"
2016-10-21 19:56:01 +02:00
fi
2018-03-02 21:07:54 +01:00
echo ' key_mgmt=NONE' >> "$WIFI_CONFIG"
echo '}' >> "$WIFI_CONFIG"
2016-10-21 11:24:07 +02:00
ctr=0
continue
fi
fi
if [ $ctr -eq 2 ]; then
WIFI_PASSPHRASE="$line"
2018-03-02 21:07:54 +01:00
wpa_passphrase "$WIFI_SSID" "$WIFI_PASSPHRASE" >> "$WIFI_CONFIG"
2016-10-21 11:24:07 +02:00
ctr=0
continue
fi
ctr=$((ctr + 1))
fi
fi
2018-03-02 21:07:54 +01:00
done < "$WIFI_NETWORKS_FILE"
2016-10-21 11:24:07 +02:00
systemctl restart network-manager
2017-06-19 19:34:50 +02:00
#ifconfig ${WIFI_INTERFACE} up
ifup $WIFI_INTERFACE
2016-10-21 11:24:07 +02:00
}
function wifi_networks_file_header {
2018-03-02 21:07:54 +01:00
{ echo $'# Add wifi networks as follows:';
echo '#';
echo $'# MySSID';
echo $'# wpa2-psk';
echo $'# myWifiPassphrase';
echo '#';
echo $'# AnotherSSID';
echo $'# none';
echo '#'; } > "$WIFI_NETWORKS_FILE"
}
2016-10-21 11:24:07 +02:00
function create_networks_interactive {
remove_config_param "WIFI_INTERFACE"
2018-03-07 16:09:31 +01:00
update_wifi_adaptors 'wlan'
2018-03-02 21:07:54 +01:00
if [ ! "$IFACE" ]; then
2016-10-21 11:24:07 +02:00
# Don't try to configure wifi if there are no adaptors
return
fi
2018-03-02 21:07:54 +01:00
if [ -f "$WIFI_NETWORKS_FILE" ]; then
rm "$WIFI_NETWORKS_FILE"
2016-10-21 11:24:07 +02:00
fi
2016-10-21 20:01:47 +02:00
# By default connect to any open wifi
2016-10-29 20:35:00 +02:00
WIFI_SSID=''
2016-10-21 20:01:47 +02:00
WIFI_TYPE=$'open'
2016-10-21 11:24:07 +02:00
wifi_ctr=0
wifi_networks_done=
wifi_settings_defined=
2016-10-21 11:24:07 +02:00
while [ ! $wifi_networks_done ]
do
2018-03-02 21:07:54 +01:00
data=$(mktemp 2>/dev/null)
wifistr=$"\\nTo use this system via wifi (eg. USB dongle) enter the details below, otherwise just select Ok:"
2017-01-05 16:07:46 +01:00
if [ -f /root/.wifi-only ]; then
2018-03-02 21:07:54 +01:00
wifistr=$"\\nEnter your wifi login details below."
2017-01-05 16:07:46 +01:00
fi
2016-10-21 11:24:07 +02:00
dialog --backtitle $"Freedombone Configuration" \
--title $"Wifi Settings ${wifi_ctr}" \
2017-01-05 16:07:46 +01:00
--form "$wifistr" 13 65 4 \
2016-10-29 20:35:00 +02:00
$"SSID (can be 'any'):" 1 1 "$WIFI_SSID" 1 24 30 30 \
2016-10-21 15:29:41 +02:00
$"Type (open/wpa2-psk):" 2 1 "$WIFI_TYPE" 2 24 10 10 \
$"Passphrase:" 3 1 "$WIFI_PASSPHRASE" 3 24 50 50 \
2018-03-02 21:07:54 +01:00
2> "$data"
2016-10-21 11:24:07 +02:00
sel=$?
case $sel in
2018-03-02 21:07:54 +01:00
1) rm -f "$data"
return;;
255) rm -f "$data"
return;;
2016-10-21 11:24:07 +02:00
esac
2018-03-02 21:07:54 +01:00
WIFI_SSID=$(sed -n 1p < "$data")
WIFI_TYPE=$(sed -n 2p < "$data")
WIFI_PASSPHRASE=$(sed -n 3p < "$data")
rm -f "$data"
2016-10-21 11:24:07 +02:00
# If the wifi-only indicator is present then wifi details must
# be specified, otherwise they're optional
if [ ! -f /root/.wifi-only ]; then
# if these fields are empty then there are no more wifi networks
if [ ${#WIFI_SSID} -lt 2 ]; then
wifi_networks_done='yes'
continue
fi
if [ ${#WIFI_TYPE} -lt 2 ]; then
wifi_networks_done='yes'
continue
fi
2016-10-21 11:24:07 +02:00
fi
2018-03-02 21:07:54 +01:00
if [ ! -f "$WIFI_NETWORKS_FILE" ]; then
wifi_networks_file_header
fi
2016-10-21 11:24:07 +02:00
# update the wifi networks file
2018-03-02 21:07:54 +01:00
{ echo '';
echo "$WIFI_SSID";
echo "$WIFI_TYPE"; } >> "$WIFI_NETWORKS_FILE"
2016-10-21 11:24:07 +02:00
if [ ${#WIFI_PASSPHRASE} -gt 1 ]; then
2018-03-02 21:07:54 +01:00
echo "$WIFI_PASSPHRASE" >> "$WIFI_NETWORKS_FILE"
2016-10-21 11:24:07 +02:00
fi
if [ ${#WIFI_SSID} -gt 1 ]; then
if [ ${#WIFI_TYPE} -gt 1 ]; then
if [[ "${WIFI_TYPE}" == $'none' || "${WIFI_TYPE}" == $'open' ]]; then
write_config_param "WIFI_INTERFACE" "$WIFI_INTERFACE"
2016-10-21 11:24:07 +02:00
return
else
if [ ${#WIFI_PASSPHRASE} -gt 1 ]; then
write_config_param "WIFI_INTERFACE" "$WIFI_INTERFACE"
2016-10-21 11:24:07 +02:00
return
fi
fi
fi
fi
# clear values
WIFI_SSID=
WIFI_PASSPHRASE=
wifi_ctr=$((wifi_ctr + 1))
done
}
function disable_wifi {
if [[ ${1} == 'yes' || ${1} == 'y' ]]; then
hotspot_off
if [ -f /etc/network/interfaces.d/static ]; then
rm /etc/network/interfaces.d/static
fi
if [ -f /etc/network/interfaces.d/wifi ]; then
rm /etc/network/interfaces.d/wifi
fi
if [ -f /etc/network/interfaces.d/bridge ]; then
rm /etc/network/interfaces.d/bridge
fi
default_network_config
2016-10-23 00:04:53 +02:00
remove_config_param "WIFI_INTERFACE"
2016-10-21 19:16:00 +02:00
wpa_action ${WIFI_INTERFACE} stop
wpa_cli -i ${WIFI_INTERFACE} terminate
2016-10-23 00:04:53 +02:00
systemctl restart network-manager
2016-10-21 11:24:07 +02:00
else
networks_from_file
fi
}
function count_wlan {
# counts the number of wlan devices
ctr=0
for i in $(seq 0 1 10); do
if grep -q "wlan${i}" /proc/net/dev; then
ctr=$((ctr + 1))
fi
done
echo $ctr
}
2016-11-02 21:42:56 +01:00
function wifi_exists {
if grep -q "wlan" /proc/net/dev; then
echo "1"
else
echo "0"
fi
}
2016-10-30 12:19:32 +01:00
function remove_wifi_startup_script {
remove_config_param "WIFI_INTERFACE"
systemd_file=/etc/systemd/system/wifistart.service
if [ -f $systemd_file ]; then
systemctl stop wifistart
systemctl disable wifistart
rm $systemd_file
fi
2018-03-02 21:07:54 +01:00
if [ -f "$HOME/${PROJECT_NAME}-wifi.cfg" ]; then
rm "$HOME/${PROJECT_NAME}-wifi.cfg"
2016-10-30 12:19:32 +01:00
fi
}
2016-10-22 16:43:15 +02:00
function create_wifi_startup_script {
systemd_file=/etc/systemd/system/wifistart.service
2018-03-02 21:07:54 +01:00
{ echo '[Unit]';
echo 'Description=WifiStartup (Start wifi networking)';
echo 'After=syslog.target';
echo 'After=network.target';
echo 'After=remote-fs.target';
echo '';
echo '[Service]';
echo 'Type=simple';
echo 'User=root';
echo 'Group=root';
echo 'WorkingDirectory=/root';
echo "ExecStart=/usr/local/bin/${PROJECT_NAME}-wifi --wait 5 2> /dev/null";
echo '';
echo '[Install]';
echo 'WantedBy=multi-user.target'; } > $systemd_file
2016-10-22 16:43:15 +02:00
systemctl enable wifistart
systemctl daemon-reload
}
function remove_wifi_startup_script {
systemd_file=/etc/systemd/system/wifistart.service
if [ -f $systemd_file ]; then
systemctl disable wifistart
systemctl daemon-reload
rm $systemd_file
fi
}
2016-07-03 17:13:34 +02:00
# NOTE: deliberately no exit 0