freedombone-utils-wifi 21KB

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