freedombone-utils-wifi 20KB

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