freedombone-wifi 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Wifi configuration tools
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2016 Bob Mottram <bob@robotics.uk.to>
  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. PROJECT_NAME='freedombone'
  30. export TEXTDOMAIN=${PROJECT_NAME}-wifi
  31. export TEXTDOMAINDIR="/usr/share/locale"
  32. CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
  33. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  34. WIFI_INTERFACE=wlan0
  35. wifi_interface_specified=
  36. WIFI_TYPE='wpa2-psk'
  37. WIFI_SSID=
  38. WIFI_PASSPHRASE=
  39. WIFI_HOTSPOT='no'
  40. WIFI_CONFIG=/etc/wpa_supplicant/wpa_supplicant.conf
  41. WIFI_NETWORKS_FILE=~/${PROJECT_NAME}-wifi.cfg
  42. NETWORKS_INTERACTIVE=
  43. WIFI_DISABLE=
  44. IFACE=
  45. IFACE_SECONDARY=
  46. function update_wifi_adaptors {
  47. IFACE=
  48. IFACE_SECONDARY=
  49. for i in $(seq 10 -1 0); do
  50. if grep -q "wlan${i}" /proc/net/dev; then
  51. if [ ! $IFACE ]; then
  52. IFACE="wlan${i}"
  53. else
  54. IFACE_SECONDARY="wlan${i}"
  55. return
  56. fi
  57. fi
  58. done
  59. }
  60. function wifi_get_psk {
  61. ssid=$1
  62. passphrase=$2
  63. psk=$(wpa_passphrase "$ssid" "$passphrase" | grep 'psk=' | sed -n 2p | awk -F '=' '{print $2}')
  64. echo $psk
  65. }
  66. function hotspot_off {
  67. if [ ! -f /etc/hostapd/hostapd.conf ]; then
  68. return
  69. fi
  70. systemctl stop hostapd
  71. rm /etc/hostapd/hostapd.conf
  72. if [ -f /etc/network/interfaces_original ]; then
  73. cp /etc/network/interfaces_original /etc/network/interfaces
  74. else
  75. echo '# interfaces(5) file used by ifup(8) and ifdown(8)' > /etc/network/interfaces
  76. echo '# Include files from /etc/network/interfaces.d:' >> /etc/network/interfaces
  77. echo 'source-directory /etc/network/interfaces.d' >> /etc/network/interfaces
  78. fi
  79. systemctl restart network-manager
  80. ifdown $WIFI_INTERFACE
  81. }
  82. function hotspot_on {
  83. if [ ! -f /etc/default/hostapd ]; then
  84. echo $'/etc/default/hostapd was not found'
  85. exit 67241
  86. fi
  87. if [ ${#WIFI_PASSPHRASE} -lt 8 ]; then
  88. echo $'Wifi hotspot passphrase is too short'
  89. exit 25719
  90. fi
  91. sed -i 's|#DAEMON_CONF=.*|DAEMON_CONF="/etc/hostapd/hostapd.conf"|g' /etc/default/hostapd
  92. echo '### Wireless network name ###' > /etc/hostapd/hostapd.conf
  93. echo "interface=$WIFI_INTERFACE" >> /etc/hostapd/hostapd.conf
  94. echo '' >> /etc/hostapd/hostapd.conf
  95. echo '### Set your bridge name ###' >> /etc/hostapd/hostapd.conf
  96. echo 'bridge=br0' >> /etc/hostapd/hostapd.conf
  97. echo '' >> /etc/hostapd/hostapd.conf
  98. echo 'driver=nl80211' >> /etc/hostapd/hostapd.conf
  99. echo "country_code=UK" >> /etc/hostapd/hostapd.conf
  100. echo "ssid=$WIFI_SSID" >> /etc/hostapd/hostapd.conf
  101. echo 'hw_mode=g' >> /etc/hostapd/hostapd.conf
  102. echo 'channel=6' >> /etc/hostapd/hostapd.conf
  103. echo 'wpa=2' >> /etc/hostapd/hostapd.conf
  104. echo "wpa_passphrase=$WIFI_PASSPHRASE" >> /etc/hostapd/hostapd.conf
  105. echo '' >> /etc/hostapd/hostapd.conf
  106. echo '## Key management algorithms ##' >> /etc/hostapd/hostapd.conf
  107. echo 'wpa_key_mgmt=WPA-PSK' >> /etc/hostapd/hostapd.conf
  108. echo '' >> /etc/hostapd/hostapd.conf
  109. echo '## Set cipher suites (encryption algorithms) ##' >> /etc/hostapd/hostapd.conf
  110. echo '## TKIP = Temporal Key Integrity Protocol' >> /etc/hostapd/hostapd.conf
  111. echo '## CCMP = AES in Counter mode with CBC-MAC' >> /etc/hostapd/hostapd.conf
  112. echo 'wpa_pairwise=TKIP' >> /etc/hostapd/hostapd.conf
  113. echo 'rsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf
  114. echo '' >> /etc/hostapd/hostapd.conf
  115. echo '## Shared Key Authentication ##'
  116. echo 'auth_algs=1' >> /etc/hostapd/hostapd.conf
  117. echo '' >> /etc/hostapd/hostapd.conf
  118. echo '## Accept all MAC address ###' >> /etc/hostapd/hostapd.conf
  119. echo 'macaddr_acl=0' >> /etc/hostapd/hostapd.conf
  120. if [ ! -f /etc/network/interfaces_original ]; then
  121. if ! grep -q "# wifi enabled" /etc/network/interfaces; then
  122. cp /etc/network/interfaces /etc/network/interfaces_original
  123. fi
  124. fi
  125. echo '# wifi enabled' > /etc/network/interfaces
  126. echo 'auto lo br0' >> /etc/network/interfaces
  127. echo 'iface lo inet loopback' >> /etc/network/interfaces
  128. echo '' >> /etc/network/interfaces
  129. echo "# wireless $WIFI_INTERFACE" >> /etc/network/interfaces
  130. echo "allow-hotplug $WIFI_INTERFACE" >> /etc/network/interfaces
  131. echo "iface $WIFI_INTERFACE inet manual" >> /etc/network/interfaces
  132. echo '' >> /etc/network/interfaces
  133. echo '# eth0 connected to the ISP router' >> /etc/network/interfaces
  134. echo 'allow-hotplug eth0' >> /etc/network/interfaces
  135. echo 'iface eth0 inet manual' >> /etc/network/interfaces
  136. echo '' >> /etc/network/interfaces
  137. echo '# Setup bridge' >> /etc/network/interfaces
  138. echo 'iface br0 inet static' >> /etc/network/interfaces
  139. echo " bridge_ports $WIFI_INTERFACE eth0" >> /etc/network/interfaces
  140. systemctl restart network-manager
  141. ifup $WIFI_INTERFACE
  142. systemctl restart hostapd
  143. }
  144. function wifi_wpa2_psk {
  145. ssid=$1
  146. passphrase=$2
  147. if [ ! -f /etc/network/interfaces_original ]; then
  148. if ! grep -q "# wifi enabled" /etc/network/interfaces; then
  149. cp /etc/network/interfaces /etc/network/interfaces_original
  150. fi
  151. fi
  152. echo '# wifi enabled' > /etc/network/interfaces
  153. echo 'auto lo' >> /etc/network/interfaces
  154. echo 'iface lo inet loopback' >> /etc/network/interfaces
  155. echo '' >> /etc/network/interfaces
  156. echo 'allow-hotplug eth0' >> /etc/network/interfaces
  157. echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
  158. echo '' >> /etc/network/interfaces
  159. echo "allow-hotplug ${WIFI_INTERFACE}" >> /etc/network/interfaces
  160. echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces
  161. echo " wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
  162. echo '' >> /etc/network/interfaces
  163. echo 'iface default inet dhcp' >> /etc/network/interfaces
  164. wpa_passphrase "$ssid" "$passphrase" > $WIFI_CONFIG
  165. systemctl restart network-manager
  166. ifup ${WIFI_INTERFACE}
  167. }
  168. function wifi_none {
  169. ssid=$1
  170. if [ ! -f /etc/network/interfaces_original ]; then
  171. if ! grep -q "# wifi enabled" /etc/network/interfaces; then
  172. cp /etc/network/interfaces /etc/network/interfaces_original
  173. fi
  174. fi
  175. echo '# wifi enabled' > /etc/network/interfaces
  176. echo 'auto lo' >> /etc/network/interfaces
  177. echo 'iface lo inet loopback' >> /etc/network/interfaces
  178. echo '' >> /etc/network/interfaces
  179. echo 'allow-hotplug eth0' >> /etc/network/interfaces
  180. echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
  181. echo '' >> /etc/network/interfaces
  182. echo "allow-hotplug ${WIFI_INTERFACE}" >> /etc/network/interfaces
  183. echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces
  184. echo " wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
  185. echo '' >> /etc/network/interfaces
  186. echo 'iface default inet dhcp' >> /etc/network/interfaces
  187. echo 'network={' > $WIFI_CONFIG
  188. echo " ssid=\"${ssid}\"" >> $WIFI_CONFIG
  189. echo ' key_mgmt=NONE' >> $WIFI_CONFIG
  190. echo '}' >> $WIFI_CONFIG
  191. systemctl restart network-manager
  192. ifup ${WIFI_INTERFACE}
  193. }
  194. function networks_from_file {
  195. if [ ! -f $WIFI_NETWORKS_FILE ]; then
  196. exit 4
  197. fi
  198. if [ ! -f /etc/network/interfaces_original ]; then
  199. if ! grep -q "# wifi enabled" /etc/network/interfaces; then
  200. cp /etc/network/interfaces /etc/network/interfaces_original
  201. fi
  202. fi
  203. echo '# wifi enabled' > /etc/network/interfaces
  204. echo 'auto lo' >> /etc/network/interfaces
  205. echo 'iface lo inet loopback' >> /etc/network/interfaces
  206. echo '' >> /etc/network/interfaces
  207. echo 'allow-hotplug eth0' >> /etc/network/interfaces
  208. echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
  209. echo '' >> /etc/network/interfaces
  210. echo "allow-hotplug ${WIFI_INTERFACE}" >> /etc/network/interfaces
  211. echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces
  212. echo " wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
  213. echo '' >> /etc/network/interfaces
  214. echo 'iface default inet dhcp' >> /etc/network/interfaces
  215. # remove wpa_supplicant.conf if it exists
  216. if [ -f $WIFI_CONFIG ]; then
  217. rm -f $WIFI_CONFIG
  218. fi
  219. ctr=0
  220. while read -r line
  221. do
  222. if [ ${#line} -gt 1 ]; then
  223. if [[ "$line" != '#'* ]]; then
  224. if [ $ctr -eq 0 ]; then
  225. WIFI_SSID="$line"
  226. fi
  227. if [ $ctr -eq 1 ]; then
  228. WIFI_TYPE="$line"
  229. if [[ $WIFI_TYPE == $'none' || $WIFI_TYPE == $'open' ]]; then
  230. echo 'network={' >> $WIFI_CONFIG
  231. echo " ssid=\"${WIFI_SSID}\"" >> $WIFI_CONFIG
  232. echo ' key_mgmt=NONE' >> $WIFI_CONFIG
  233. echo '}' >> $WIFI_CONFIG
  234. ctr=0
  235. continue
  236. fi
  237. fi
  238. if [ $ctr -eq 2 ]; then
  239. WIFI_PASSPHRASE="$line"
  240. wpa_passphrase "$WIFI_SSID" "$WIFI_PASSPHRASE" >> $WIFI_CONFIG
  241. ctr=0
  242. continue
  243. fi
  244. ctr=$((ctr + 1))
  245. fi
  246. fi
  247. done < $WIFI_NETWORKS_FILE
  248. systemctl restart network-manager
  249. ifup ${WIFI_INTERFACE}
  250. }
  251. function create_networks_interactive {
  252. update_wifi_adaptors
  253. if [ ! $IFACE ]; then
  254. # Don't try to configure wifi if there are no adaptors
  255. return
  256. fi
  257. if [ -f $WIFI_NETWORKS_FILE ]; then
  258. rm $WIFI_NETWORKS_FILE
  259. fi
  260. echo $'# Add wifi networks as follows:' > $WIFI_NETWORKS_FILE
  261. echo '#' >> $WIFI_NETWORKS_FILE
  262. echo $'# MySSID' >> $WIFI_NETWORKS_FILE
  263. echo $'# wpa2-psk' >> $WIFI_NETWORKS_FILE
  264. echo $'# myWifiPassphrase' >> $WIFI_NETWORKS_FILE
  265. echo '#' >> $WIFI_NETWORKS_FILE
  266. echo $'# AnotherSSID' >> $WIFI_NETWORKS_FILE
  267. echo $'# none' >> $WIFI_NETWORKS_FILE
  268. echo '#' >> $WIFI_NETWORKS_FILE
  269. wifi_ctr=0
  270. wifi_networks_done=
  271. while [ ! $wifi_networks_done ]
  272. do
  273. data=$(tempfile 2>/dev/null)
  274. trap "rm -f $data" 0 1 2 5 15
  275. dialog --backtitle $"Freedombone Configuration" \
  276. --title $"Wifi Settings ${wifi_ctr}" \
  277. --form $"\nIf you wish to use wifi and have a Free Software compatible adapter (eg. Atheros) rather than wired ethernet then enter the details below, otherwise just select Ok:" 15 55 4 \
  278. $"SSID:" 1 1 "$WIFI_SSID" 1 16 30 30 \
  279. $"Type:" 2 1 "$WIFI_TYPE" 2 16 10 10 \
  280. $"Passphrase:" 3 1 "$WIFI_PASSPHRASE" 3 16 30 30 \
  281. 2> $data
  282. sel=$?
  283. case $sel in
  284. 1) return;;
  285. 255) return;;
  286. esac
  287. WIFI_SSID=$(cat $data | sed -n 1p)
  288. WIFI_TYPE=$(cat $data | sed -n 2p)
  289. WIFI_PASSPHRASE=$(cat $data | sed -n 3p)
  290. # if these fields are empty then there are no more wifi networks
  291. if [ ${#WIFI_SSID} -lt 2 ]; then
  292. wifi_networks_done='yes'
  293. continue
  294. fi
  295. if [ ${#WIFI_TYPE} -lt 2 ]; then
  296. wifi_networks_done='yes'
  297. continue
  298. fi
  299. # update the wifi networks file
  300. echo '' >> $WIFI_NETWORKS_FILE
  301. echo "$WIFI_SSID" >> $WIFI_NETWORKS_FILE
  302. echo "$WIFI_TYPE" >> $WIFI_NETWORKS_FILE
  303. if [ ${#WIFI_PASSPHRASE} -gt 1 ]; then
  304. echo "$WIFI_PASSPHRASE" >> $WIFI_NETWORKS_FILE
  305. fi
  306. if [ ${#WIFI_SSID} -gt 1 ]; then
  307. if [ ${#WIFI_TYPE} -gt 1 ]; then
  308. if [[ "${WIFI_TYPE}" == $'none' || "${WIFI_TYPE}" == $'open' ]]; then
  309. return
  310. else
  311. if [ ${#WIFI_PASSPHRASE} -gt 1 ]; then
  312. return
  313. fi
  314. fi
  315. fi
  316. fi
  317. # clear values
  318. WIFI_SSID=
  319. WIFI_PASSPHRASE=
  320. wifi_ctr=$((wifi_ctr + 1))
  321. done
  322. }
  323. function disable_wifi {
  324. if [[ ${1} == 'yes' || ${1} == 'y' ]]; then
  325. hotspot_off
  326. echo '# interfaces(5) file used by ifup(8) and ifdown(8)' > /etc/network/interfaces
  327. echo '# Include files from /etc/network/interfaces.d:' >> /etc/network/interfaces
  328. echo 'source-directory /etc/network/interfaces.d' >> /etc/network/interfaces
  329. systemctl restart network-manager
  330. ifdown ${WIFI_INTERFACE}
  331. else
  332. networks_from_file
  333. fi
  334. }
  335. function show_help {
  336. echo ''
  337. echo $"${PROJECT_NAME}-wifi -i [interface] -t [type] -s [ssid] -p [passphrase]"
  338. echo ''
  339. echo $'Wifi configuration tool'
  340. echo ''
  341. echo $' --help Show help'
  342. echo $' -i --interface [wlan0|wlan1...] Device name'
  343. echo $' -t --type [wpa2-psk|none|open] Security type'
  344. echo $' -s --ssid [id] Set SSID'
  345. echo $' -p --passphrase [text] Set passphrase'
  346. echo $' --hotspot [yes|no] Create a hotspot'
  347. echo $' --networks [filename] File containing wifi networks'
  348. echo $' --createnetworks [filename] Create file containing wifi networks'
  349. echo $' --disable [yes/no] Disable wifi'
  350. echo ''
  351. exit 0
  352. }
  353. while [[ $# > 1 ]]
  354. do
  355. key="$1"
  356. case $key in
  357. --help)
  358. show_help
  359. ;;
  360. -i|--if|--interface)
  361. shift
  362. WIFI_INTERFACE=${1}
  363. wifi_interface_specified=1
  364. ;;
  365. -t|--type)
  366. shift
  367. WIFI_TYPE=${1}
  368. ;;
  369. -s|--ssid)
  370. shift
  371. WIFI_SSID=${1}
  372. ;;
  373. -p|--pass|--passphrase)
  374. shift
  375. WIFI_PASSPHRASE=${1}
  376. ;;
  377. --hotspot)
  378. shift
  379. WIFI_HOTSPOT=${1}
  380. ;;
  381. --networks)
  382. shift
  383. WIFI_NETWORKS_FILE=${1}
  384. ;;
  385. --networksinteractive)
  386. shift
  387. NETWORKS_INTERACTIVE='yes'
  388. WIFI_NETWORKS_FILE=${1}
  389. ;;
  390. --disable)
  391. shift
  392. WIFI_DISABLE=${1}
  393. if [[ $WIFI_DISABLE == $'yes' || $WIFI_DISABLE == $'y' ]]; then
  394. WIFI_DISABLE='yes'
  395. else
  396. WIFI_DISABLE='no'
  397. fi
  398. ;;
  399. *)
  400. # unknown option
  401. ;;
  402. esac
  403. shift
  404. done
  405. if [ $NETWORKS_INTERACTIVE ]; then
  406. create_networks_interactive
  407. exit 0
  408. fi
  409. if [ $WIFI_DISABLE ]; then
  410. disable_wifi $WIFI_DISABLE
  411. exit 0
  412. fi
  413. if [ ! $wifi_interface_specified= ]; then
  414. update_wifi_adaptors
  415. if [ ! $IFACE ]; then
  416. echo $'No wifi adaptors were found'
  417. exit 872356
  418. fi
  419. WIFI_INTERFACE=$IFACE
  420. fi
  421. if [ -f $WIFI_NETWORKS_FILE ]; then
  422. networks_from_file
  423. exit 0
  424. fi
  425. if [ ! $WIFI_SSID ]; then
  426. echo $'No SSID given'
  427. exit 1
  428. fi
  429. if [[ $WIFI_HOTSPOT != 'no' ]]; then
  430. hotspot_on
  431. exit 0
  432. else
  433. hotspot_off
  434. fi
  435. if [[ "$WIFI_TYPE" != 'none' && "$WIFI_TYPE" != 'open' ]]; then
  436. if [ ! $WIFI_PASSPHRASE ]; then
  437. echo $'No wifi passphrase was given'
  438. exit 2
  439. fi
  440. fi
  441. if [[ $WIFI_TYPE == 'wpa2-psk' ]]; then
  442. if [ ! -d /etc/wpa_supplicant ]; then
  443. echo $'wpasupplicant package is not installed'
  444. exit 3
  445. fi
  446. wifi_wpa2_psk "$WIFI_SSID" "$WIFI_PASSPHRASE"
  447. exit 0
  448. fi
  449. if [[ "$WIFI_TYPE" == 'none' || "$WIFI_TYPE" == 'open' ]]; then
  450. wifi_none "$WIFI_SSID"
  451. exit 0
  452. fi
  453. exit 0