freedombone-utils-wifi 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Wifi functions
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. WIFI_CHANNEL=2
  30. WIFI_INTERFACE=wlan0
  31. WIFI_TYPE='wpa2-psk'
  32. WIFI_SSID=
  33. WIFI_PASSPHRASE=
  34. WIFI_HOTSPOT='no'
  35. WIFI_NETWORKS_FILE=~/${PROJECT_NAME}-wifi.cfg
  36. # repo for atheros AR9271 wifi driver
  37. ATHEROS_WIFI_REPO="https://github.com/qca/open-ath9k-htc-firmware.git"
  38. function default_network_config {
  39. echo '# This file describes the network interfaces available on your system' > /etc/network/interfaces
  40. echo '# and how to activate them. For more information, see interfaces(5).' >> /etc/network/interfaces
  41. echo 'source /etc/network/interfaces.d/*' >> /etc/network/interfaces
  42. }
  43. # Debian stretch has a problem where the formerly predictable wlan0 and eth0
  44. # device names get assigned random names. This is a hacky workaround.
  45. # Also adding net.ifnames=0 to kernel options on bootloader may work.
  46. function enable_predictable_device_names {
  47. test_predictable=$(ls -la /etc/udev/rules.d/80-net-setup-link.rules)
  48. if [[ "$test_predictable" != *"/dev/null" ]]; then
  49. ln -s /dev/null /etc/udev/rules.d/80-net-setup-link.rules
  50. update-initramfs -u
  51. fi
  52. }
  53. function wifi_is_running {
  54. wifi_state=$(wpa_cli status)
  55. if [[ "$wifi_state" == *"COMPLETED"* ]]; then
  56. echo "1"
  57. else
  58. echo "0"
  59. fi
  60. }
  61. function wifi_static_network_interface {
  62. NETWORK_IS_STATIC=0
  63. read_config_param "NETWORK_IS_STATIC"
  64. if [ ${NETWORK_IS_STATIC} -eq 0 ]; then
  65. echo '#this line must always be here' > /etc/network/interfaces.d/static
  66. echo 'iface default inet dhcp' >> /etc/network/interfaces.d/static
  67. else
  68. read_config_param "LOCAL_NETWORK_STATIC_IP_ADDRESS"
  69. read_config_param "ROUTER_IP_ADDRESS"
  70. echo 'iface default inet static' > /etc/network/interfaces.d/static
  71. echo " address ${LOCAL_NETWORK_STATIC_IP_ADDRESS}" >> /etc/network/interfaces.d/static
  72. echo ' netmask 255.255.255.0' >> /etc/network/interfaces.d/static
  73. echo " gateway ${ROUTER_IP_ADDRESS}" >> /etc/network/interfaces.d/static
  74. fi
  75. }
  76. function setup_wifi_atheros {
  77. if [[ $(running_as_root) == "0" ]]; then
  78. return
  79. fi
  80. apt-get -yq install firmware-linux-free
  81. atheros_drivers_file=drivers/firmware-ath9k-htc.deb
  82. if [ ! -f $atheros_drivers_file ]; then
  83. if [ ! -f ~/freedombone/$atheros_drivers_file ]; then
  84. return
  85. else
  86. atheros_drivers_file=~/freedombone/$atheros_drivers_file
  87. fi
  88. else
  89. atheros_drivers_file=$(pwd)/$atheros_drivers_file
  90. fi
  91. dpkg -i $atheros_drivers_file
  92. reset_usb_devices
  93. update_wifi_adaptors
  94. if [ $IFACE ]; then
  95. wpa_action ${IFACE} stop
  96. wpa_cli -i ${IFACE} terminate
  97. #ifconfig $IFACE up
  98. ifup $IFACE
  99. fi
  100. }
  101. function setup_wifi {
  102. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  103. return
  104. fi
  105. if [ ! $WIFI_SSID ]; then
  106. return
  107. fi
  108. if [ ${#WIFI_SSID} -lt 2 ]; then
  109. return
  110. fi
  111. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  112. return
  113. fi
  114. HOTSPOT='no'
  115. if [[ $WIFI_HOTSPOT != 'no' ]]; then
  116. HOTSPOT='yes'
  117. fi
  118. if [ -f $WIFI_NETWORKS_FILE ]; then
  119. ${PROJECT_NAME}-wifi --networks $WIFI_NETWORKS_FILE
  120. mark_complete "${FUNCNAME[0]}"
  121. return
  122. fi
  123. if [[ $WIFI_TYPE != 'none' ]]; then
  124. if [ ! $WIFI_PASSPHRASE ]; then
  125. echo $'No wifi passphrase was given'
  126. return
  127. fi
  128. if [ ${#WIFI_PASSPHRASE} -lt 2 ]; then
  129. echo $'Wifi passphrase was too short'
  130. return
  131. fi
  132. ${PROJECT_NAME}-wifi -s $WIFI_SSID -t $WIFI_TYPE -p $WIFI_PASSPHRASE --hotspot $HOTSPOT --networks $WIFI_NETWORKS_FILE
  133. else
  134. ${PROJECT_NAME}-wifi -s $WIFI_SSID -t $WIFI_TYPE --hotspot $HOTSPOT --networks $WIFI_NETWORKS_FILE
  135. fi
  136. mark_completed "${FUNCNAME[0]}"
  137. }
  138. # ath9k_htc driver
  139. function install_atheros_wifi {
  140. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  141. return
  142. fi
  143. if [ $INSTALLING_ON_BBB != "yes" ]; then
  144. return
  145. fi
  146. if [[ $ENABLE_BATMAN != "yes" ]]; then
  147. return
  148. fi
  149. if [ -d $INSTALL_DIR/open-ath9k-htc-firmware ]; then
  150. return
  151. fi
  152. # have drivers already been installed ?
  153. if [ -f /lib/firmware/htc_9271.fw ]; then
  154. return
  155. fi
  156. apt-get -yq install build-essential cmake git m4 texinfo
  157. if [ ! -d $INSTALL_DIR ]; then
  158. mkdir -p $INSTALL_DIR
  159. fi
  160. cd $INSTALL_DIR
  161. if [ ! -d $INSTALL_DIR/open-ath9k-htc-firmware ]; then
  162. function_check git_clone
  163. git_clone $ATHEROS_WIFI_REPO $INSTALL_DIR/open-ath9k-htc-firmware
  164. if [ ! "$?" = "0" ]; then
  165. rm -rf $INSTALL_DIR/open-ath9k-htc-firmware
  166. exit 74283
  167. fi
  168. fi
  169. cd $INSTALL_DIR/open-ath9k-htc-firmware
  170. git checkout 1.4.0
  171. make toolchain
  172. if [ ! "$?" = "0" ]; then
  173. rm -rf $INSTALL_DIR/open-ath9k-htc-firmware
  174. exit 24820
  175. fi
  176. make firmware
  177. if [ ! "$?" = "0" ]; then
  178. rm -rf $INSTALL_DIR/open-ath9k-htc-firmware
  179. exit 63412
  180. fi
  181. cp target_firmware/*.fw /lib/firmware/
  182. if [ ! "$?" = "0" ]; then
  183. exit 74681
  184. fi
  185. mark_completed "${FUNCNAME[0]}"
  186. }
  187. function update_wifi_adaptors {
  188. IFACE=
  189. IFACE_SECONDARY=
  190. for i in $(seq 10 -1 0); do
  191. ifdown --force wlan${i} 2> /dev/null
  192. done
  193. for i in $(seq 10 -1 0); do
  194. if grep -q "wlan${i}" /proc/net/dev; then
  195. if [ ! $IFACE ]; then
  196. IFACE="wlan${i}"
  197. else
  198. IFACE_SECONDARY="wlan${i}"
  199. return
  200. fi
  201. fi
  202. done
  203. }
  204. function wifi_get_psk {
  205. ssid=$1
  206. passphrase=$2
  207. psk=$(wpa_passphrase "$ssid" "$passphrase" | grep 'psk=' | sed -n 2p | awk -F '=' '{print $2}')
  208. echo $psk
  209. }
  210. function hotspot_off {
  211. if [ ! -f /etc/hostapd/hostapd.conf ]; then
  212. return
  213. fi
  214. systemctl stop hostapd
  215. rm /etc/hostapd/hostapd.conf
  216. if [ -f /etc/network/interfaces_original_static ]; then
  217. cp /etc/network/interfaces_original_static /etc/network/interfaces.d/static
  218. else
  219. if [ -f /etc/network/interfaces.d/static ]; then
  220. rm /etc/network/interfaces.d/static
  221. fi
  222. fi
  223. if [ -f /etc/network/interfaces_original_wifi ]; then
  224. cp /etc/network/interfaces_original_wifi /etc/network/interfaces.d/wifi
  225. else
  226. if [ -f /etc/network/interfaces.d/wifi ]; then
  227. rm /etc/network/interfaces.d/wifi
  228. fi
  229. fi
  230. if [ -f /etc/network/interfaces.d/bridge ]; then
  231. rm /etc/network/interfaces.d/bridge
  232. fi
  233. default_network_config
  234. wpa_action ${WIFI_INTERFACE} stop
  235. wpa_cli -i ${WIFI_INTERFACE} terminate
  236. systemctl restart network-manager
  237. }
  238. function hotspot_on {
  239. if [ ! -f /etc/default/hostapd ]; then
  240. echo $'/etc/default/hostapd was not found'
  241. exit 67241
  242. fi
  243. if [ ${#WIFI_PASSPHRASE} -lt 8 ]; then
  244. echo $'Wifi hotspot passphrase is too short'
  245. exit 25719
  246. fi
  247. sed -i 's|#DAEMON_CONF=.*|DAEMON_CONF="/etc/hostapd/hostapd.conf"|g' /etc/default/hostapd
  248. echo '### Wireless network name ###' > /etc/hostapd/hostapd.conf
  249. echo "interface=$WIFI_INTERFACE" >> /etc/hostapd/hostapd.conf
  250. echo '' >> /etc/hostapd/hostapd.conf
  251. echo '### Set your bridge name ###' >> /etc/hostapd/hostapd.conf
  252. echo 'bridge=br0' >> /etc/hostapd/hostapd.conf
  253. echo '' >> /etc/hostapd/hostapd.conf
  254. echo 'driver=nl80211' >> /etc/hostapd/hostapd.conf
  255. echo "country_code=UK" >> /etc/hostapd/hostapd.conf
  256. echo "ssid=$WIFI_SSID" >> /etc/hostapd/hostapd.conf
  257. echo 'hw_mode=g' >> /etc/hostapd/hostapd.conf
  258. echo 'channel=6' >> /etc/hostapd/hostapd.conf
  259. echo 'wpa=2' >> /etc/hostapd/hostapd.conf
  260. echo "wpa_passphrase=$WIFI_PASSPHRASE" >> /etc/hostapd/hostapd.conf
  261. echo '' >> /etc/hostapd/hostapd.conf
  262. echo '## Key management algorithms ##' >> /etc/hostapd/hostapd.conf
  263. echo 'wpa_key_mgmt=WPA-PSK' >> /etc/hostapd/hostapd.conf
  264. echo '' >> /etc/hostapd/hostapd.conf
  265. echo '## Set cipher suites (encryption algorithms) ##' >> /etc/hostapd/hostapd.conf
  266. echo '## TKIP = Temporal Key Integrity Protocol' >> /etc/hostapd/hostapd.conf
  267. echo '## CCMP = AES in Counter mode with CBC-MAC' >> /etc/hostapd/hostapd.conf
  268. echo 'wpa_pairwise=TKIP' >> /etc/hostapd/hostapd.conf
  269. echo 'rsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf
  270. echo '' >> /etc/hostapd/hostapd.conf
  271. echo '## Shared Key Authentication ##'
  272. echo 'auth_algs=1' >> /etc/hostapd/hostapd.conf
  273. echo '' >> /etc/hostapd/hostapd.conf
  274. echo '## Accept all MAC address ###' >> /etc/hostapd/hostapd.conf
  275. echo 'macaddr_acl=0' >> /etc/hostapd/hostapd.conf
  276. if [ ! -f /etc/network/interfaces_original_static ]; then
  277. if [ -f /etc/network/interfaces.d/static ]; then
  278. cp /etc/network/interfaces.d/static /etc/network/interfaces_original_static
  279. fi
  280. fi
  281. if [ ! -f /etc/network/interfaces_original_wifi ]; then
  282. if [ -f /etc/network/interfaces.d/wifi ]; then
  283. cp /etc/network/interfaces.d/wifi /etc/network/interfaces_original_wifi
  284. fi
  285. fi
  286. default_network_config
  287. echo "allow-hotplug $WIFI_INTERFACE" > /etc/network/interfaces.d/wifi
  288. echo "iface $WIFI_INTERFACE inet manual" >> /etc/network/interfaces.d/wifi
  289. echo 'allow-hotplug eth0' >> /etc/network/interfaces.d/static
  290. echo 'iface eth0 inet manual' >> /etc/network/interfaces.d/static
  291. echo 'iface br0 inet static' >> /etc/network/interfaces.d/bridge
  292. echo " bridge_ports $WIFI_INTERFACE eth0" >> /etc/network/interfaces.d/bridge
  293. systemctl restart network-manager
  294. ifup $WIFI_INTERFACE
  295. systemctl restart hostapd
  296. }
  297. function wifi_store_original_network_settings {
  298. if [ ! -f /etc/network/interfaces_original ]; then
  299. if ! grep -q "# wifi enabled" /etc/network/interfaces; then
  300. cp /etc/network/interfaces /etc/network/interfaces_original
  301. fi
  302. fi
  303. }
  304. function wifi_original_network_settings {
  305. remove_config_param "NETWORK_IS_STATIC"
  306. if [ -f /etc/network/interfaces.d/static ]; then
  307. rm /etc/network/interfaces.d/static
  308. fi
  309. if [ -f /etc/network/interfaces.d/wifi ]; then
  310. rm /etc/network/interfaces.d/wifi
  311. fi
  312. if [ -f /etc/network/interfaces.d/bridge ]; then
  313. rm /etc/network/interfaces.d/bridge
  314. fi
  315. }
  316. function wifi_wpa2_psk {
  317. ssid=$1
  318. passphrase=$2
  319. wifi_store_original_network_settings
  320. echo 'allow-hotplug eth0' > /etc/network/interfaces.d/static
  321. echo 'iface eth0 inet dhcp' >> /etc/network/interfaces.d/static
  322. echo "allow-hotplug ${WIFI_INTERFACE}" > /etc/network/interfaces.d/wifi
  323. echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces.d/wifi
  324. echo " wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces.d/wifi
  325. wifi_static_network_interface
  326. wpa_passphrase "$ssid" "$passphrase" > $WIFI_CONFIG
  327. systemctl restart network-manager
  328. ifup $WIFI_INTERFACE
  329. }
  330. function wifi_none {
  331. ssid=$1
  332. wifi_store_original_network_settings
  333. echo 'allow-hotplug eth0' > /etc/network/interfaces.d/static
  334. echo 'iface eth0 inet dhcp' >> /etc/network/interfaces.d/static
  335. echo "allow-hotplug ${WIFI_INTERFACE}" > /etc/network/interfaces.d/wifi
  336. echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces.d/wifi
  337. echo " wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces.d/wifi
  338. wifi_static_network_interface
  339. echo 'ctrl_interface=/run/wpa_supplicant' > $WIFI_CONFIG
  340. echo 'update_config=1' >> $WIFI_CONFIG
  341. echo 'eapol_version=1' >> $WIFI_CONFIG
  342. echo '' >> $WIFI_CONFIG
  343. echo 'network={' >> $WIFI_CONFIG
  344. if [[ "${ssid}" != $'any' && "${ssid}" != $'all' && "${ssid}" != $'open' ]]; then
  345. echo " ssid=\"${ssid}\"" >> $WIFI_CONFIG
  346. fi
  347. echo ' key_mgmt=NONE' >> $WIFI_CONFIG
  348. echo '}' >> $WIFI_CONFIG
  349. systemctl restart network-manager
  350. ifup $WIFI_INTERFACE
  351. }
  352. function networks_from_file {
  353. if [ ! -f $WIFI_NETWORKS_FILE ]; then
  354. exit 4
  355. fi
  356. if [[ $(config_param_exists "WIFI_INTERFACE") == "0" ]]; then
  357. exit 5
  358. fi
  359. read_config_param "WIFI_INTERFACE"
  360. wifi_store_original_network_settings
  361. echo 'allow-hotplug eth0' > /etc/network/interfaces.d/static
  362. echo 'iface eth0 inet dhcp' >> /etc/network/interfaces.d/static
  363. echo "allow-hotplug ${WIFI_INTERFACE}" >> /etc/network/interfaces.d/wifi
  364. echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces.d/wifi
  365. echo " wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces.d/wifi
  366. wifi_static_network_interface
  367. # remove wpa_supplicant.conf if it exists
  368. if [ -f $WIFI_CONFIG ]; then
  369. rm -f $WIFI_CONFIG
  370. fi
  371. echo 'ctrl_interface=/run/wpa_supplicant' > $WIFI_CONFIG
  372. echo 'update_config=1' >> $WIFI_CONFIG
  373. echo 'eapol_version=1' >> $WIFI_CONFIG
  374. echo '' >> $WIFI_CONFIG
  375. ctr=0
  376. while read -r line
  377. do
  378. if [ ${#line} -gt 1 ]; then
  379. if [[ "$line" != '#'* ]]; then
  380. if [ $ctr -eq 0 ]; then
  381. WIFI_SSID="$line"
  382. fi
  383. if [ $ctr -eq 1 ]; then
  384. WIFI_TYPE="$line"
  385. if [[ $WIFI_TYPE == $'none' || $WIFI_TYPE == $'open' ]]; then
  386. echo 'network={' >> $WIFI_CONFIG
  387. if [[ "${WIFI_SSID}" != $'any' && "${WIFI_SSID}" != $'all' && "${WIFI_SSID}" != $'open' ]]; then
  388. echo " ssid=\"${WIFI_SSID}\"" >> $WIFI_CONFIG
  389. fi
  390. echo ' key_mgmt=NONE' >> $WIFI_CONFIG
  391. echo '}' >> $WIFI_CONFIG
  392. ctr=0
  393. continue
  394. fi
  395. fi
  396. if [ $ctr -eq 2 ]; then
  397. WIFI_PASSPHRASE="$line"
  398. wpa_passphrase "$WIFI_SSID" "$WIFI_PASSPHRASE" >> $WIFI_CONFIG
  399. ctr=0
  400. continue
  401. fi
  402. ctr=$((ctr + 1))
  403. fi
  404. fi
  405. done < $WIFI_NETWORKS_FILE
  406. systemctl restart network-manager
  407. #ifconfig ${WIFI_INTERFACE} up
  408. ifup $WIFI_INTERFACE
  409. }
  410. function wifi_networks_file_header {
  411. echo $'# Add wifi networks as follows:' > $WIFI_NETWORKS_FILE
  412. echo '#' >> $WIFI_NETWORKS_FILE
  413. echo $'# MySSID' >> $WIFI_NETWORKS_FILE
  414. echo $'# wpa2-psk' >> $WIFI_NETWORKS_FILE
  415. echo $'# myWifiPassphrase' >> $WIFI_NETWORKS_FILE
  416. echo '#' >> $WIFI_NETWORKS_FILE
  417. echo $'# AnotherSSID' >> $WIFI_NETWORKS_FILE
  418. echo $'# none' >> $WIFI_NETWORKS_FILE
  419. echo '#' >> $WIFI_NETWORKS_FILE
  420. }
  421. function create_networks_interactive {
  422. remove_config_param "WIFI_INTERFACE"
  423. update_wifi_adaptors
  424. if [ ! $IFACE ]; then
  425. # Don't try to configure wifi if there are no adaptors
  426. return
  427. fi
  428. if [ -f $WIFI_NETWORKS_FILE ]; then
  429. rm $WIFI_NETWORKS_FILE
  430. fi
  431. # By default connect to any open wifi
  432. WIFI_SSID=''
  433. WIFI_TYPE=$'open'
  434. wifi_ctr=0
  435. wifi_networks_done=
  436. wifi_settings_defined=
  437. while [ ! $wifi_networks_done ]
  438. do
  439. data=$(tempfile 2>/dev/null)
  440. trap "rm -f $data" 0 1 2 5 15
  441. wifistr=$"\nTo use this system via wifi (eg. USB dongle) enter the details below, otherwise just select Ok:"
  442. if [ -f /root/.wifi-only ]; then
  443. wifistr=$"\nEnter your wifi login details below."
  444. fi
  445. dialog --backtitle $"Freedombone Configuration" \
  446. --title $"Wifi Settings ${wifi_ctr}" \
  447. --form "$wifistr" 13 65 4 \
  448. $"SSID (can be 'any'):" 1 1 "$WIFI_SSID" 1 24 30 30 \
  449. $"Type (open/wpa2-psk):" 2 1 "$WIFI_TYPE" 2 24 10 10 \
  450. $"Passphrase:" 3 1 "$WIFI_PASSPHRASE" 3 24 50 50 \
  451. 2> $data
  452. sel=$?
  453. case $sel in
  454. 1) return;;
  455. 255) return;;
  456. esac
  457. WIFI_SSID=$(cat $data | sed -n 1p)
  458. WIFI_TYPE=$(cat $data | sed -n 2p)
  459. WIFI_PASSPHRASE=$(cat $data | sed -n 3p)
  460. # If the wifi-only indicator is present then wifi details must
  461. # be specified, otherwise they're optional
  462. if [ ! -f /root/.wifi-only ]; then
  463. # if these fields are empty then there are no more wifi networks
  464. if [ ${#WIFI_SSID} -lt 2 ]; then
  465. wifi_networks_done='yes'
  466. continue
  467. fi
  468. if [ ${#WIFI_TYPE} -lt 2 ]; then
  469. wifi_networks_done='yes'
  470. continue
  471. fi
  472. fi
  473. if [ ! -f $WIFI_NETWORKS_FILE ]; then
  474. wifi_networks_file_header
  475. fi
  476. # update the wifi networks file
  477. echo '' >> $WIFI_NETWORKS_FILE
  478. echo "$WIFI_SSID" >> $WIFI_NETWORKS_FILE
  479. echo "$WIFI_TYPE" >> $WIFI_NETWORKS_FILE
  480. if [ ${#WIFI_PASSPHRASE} -gt 1 ]; then
  481. echo "$WIFI_PASSPHRASE" >> $WIFI_NETWORKS_FILE
  482. fi
  483. if [ ${#WIFI_SSID} -gt 1 ]; then
  484. if [ ${#WIFI_TYPE} -gt 1 ]; then
  485. if [[ "${WIFI_TYPE}" == $'none' || "${WIFI_TYPE}" == $'open' ]]; then
  486. write_config_param "WIFI_INTERFACE" "$WIFI_INTERFACE"
  487. return
  488. else
  489. if [ ${#WIFI_PASSPHRASE} -gt 1 ]; then
  490. write_config_param "WIFI_INTERFACE" "$WIFI_INTERFACE"
  491. return
  492. fi
  493. fi
  494. fi
  495. fi
  496. # clear values
  497. WIFI_SSID=
  498. WIFI_PASSPHRASE=
  499. wifi_ctr=$((wifi_ctr + 1))
  500. done
  501. }
  502. function disable_wifi {
  503. if [[ ${1} == 'yes' || ${1} == 'y' ]]; then
  504. hotspot_off
  505. if [ -f /etc/network/interfaces.d/static ]; then
  506. rm /etc/network/interfaces.d/static
  507. fi
  508. if [ -f /etc/network/interfaces.d/wifi ]; then
  509. rm /etc/network/interfaces.d/wifi
  510. fi
  511. if [ -f /etc/network/interfaces.d/bridge ]; then
  512. rm /etc/network/interfaces.d/bridge
  513. fi
  514. default_network_config
  515. remove_config_param "WIFI_INTERFACE"
  516. wpa_action ${WIFI_INTERFACE} stop
  517. wpa_cli -i ${WIFI_INTERFACE} terminate
  518. systemctl restart network-manager
  519. else
  520. networks_from_file
  521. fi
  522. }
  523. function count_wlan {
  524. # counts the number of wlan devices
  525. ctr=0
  526. for i in $(seq 0 1 10); do
  527. if grep -q "wlan${i}" /proc/net/dev; then
  528. ctr=$((ctr + 1))
  529. fi
  530. done
  531. echo $ctr
  532. }
  533. function wifi_exists {
  534. if grep -q "wlan" /proc/net/dev; then
  535. echo "1"
  536. else
  537. echo "0"
  538. fi
  539. }
  540. function remove_wifi_startup_script {
  541. remove_config_param "WIFI_INTERFACE"
  542. systemd_file=/etc/systemd/system/wifistart.service
  543. if [ -f $systemd_file ]; then
  544. systemctl stop wifistart
  545. systemctl disable wifistart
  546. rm $systemd_file
  547. fi
  548. if [ -f $HOME/${PROJECT_NAME}-wifi.cfg ]; then
  549. rm $HOME/${PROJECT_NAME}-wifi.cfg
  550. fi
  551. }
  552. function create_wifi_startup_script {
  553. systemd_file=/etc/systemd/system/wifistart.service
  554. echo '[Unit]' > $systemd_file
  555. echo 'Description=WifiStartup (Start wifi networking)' >> $systemd_file
  556. echo 'After=syslog.target' >> $systemd_file
  557. echo 'After=network.target' >> $systemd_file
  558. echo 'After=remote-fs.target' >> $systemd_file
  559. echo '' >> $systemd_file
  560. echo '[Service]' >> $systemd_file
  561. echo 'Type=simple' >> $systemd_file
  562. echo 'User=root' >> $systemd_file
  563. echo 'Group=root' >> $systemd_file
  564. echo 'WorkingDirectory=/root' >> $systemd_file
  565. echo "ExecStart=/usr/local/bin/${PROJECT_NAME}-wifi --wait 5 2> /dev/null" >> $systemd_file
  566. echo '' >> $systemd_file
  567. echo '[Install]' >> $systemd_file
  568. echo 'WantedBy=multi-user.target' >> $systemd_file
  569. systemctl enable wifistart
  570. systemctl daemon-reload
  571. }
  572. function remove_wifi_startup_script {
  573. systemd_file=/etc/systemd/system/wifistart.service
  574. if [ -f $systemd_file ]; then
  575. systemctl disable wifistart
  576. systemctl daemon-reload
  577. rm $systemd_file
  578. fi
  579. }
  580. # NOTE: deliberately no exit 0