freedombone-utils-wifi 21KB

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