freedombone-utils-wifi 21KB

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