freedombone-utils-wifi 20KB

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