freedombone-wifi 15KB

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