freedombone-utils-wifi 21KB

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