Browse Source

Consolidation of wifi functions

Bob Mottram 8 years ago
parent
commit
0c569eff66
3 changed files with 344 additions and 356 deletions
  1. 1
    27
      src/freedombone-mesh-batman
  2. 342
    0
      src/freedombone-utils-wifi
  3. 1
    329
      src/freedombone-wifi

+ 1
- 27
src/freedombone-mesh-batman View File

@@ -32,33 +32,7 @@ PROJECT_NAME='freedombone'
32 32
 COMPLETION_FILE=/root/${PROJECT_NAME}-completed.txt
33 33
 HOTSPOT_PASSPHRASE='mesh'
34 34
 
35
-function count_wlan {
36
-    # counts the number of wlan devices
37
-    ctr=0
38
-
39
-    for i in $(seq 0 1 10); do
40
-        if grep -q "wlan${i}" /proc/net/dev; then
41
-            ctr=$((ctr + 1))
42
-        fi
43
-    done
44
-    echo $ctr
45
-}
46
-
47
-function update_wifi_adaptors {
48
-    IFACE=
49
-    IFACE_SECONDARY=
50
-
51
-    for i in $(seq 10 -1 0); do
52
-        if grep -q "wlan${i}" /proc/net/dev; then
53
-            if [ ! $IFACE ]; then
54
-                IFACE="wlan${i}"
55
-            else
56
-                IFACE_SECONDARY="wlan${i}"
57
-                return
58
-            fi
59
-        fi
60
-    done
61
-}
35
+source /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-wifi
62 36
 
63 37
 if [[ $1 == "start" ]]; then
64 38
     # install avahi

+ 342
- 0
src/freedombone-utils-wifi View File

@@ -131,4 +131,346 @@ function install_atheros_wifi {
131 131
     mark_completed $FUNCNAME
132 132
 }
133 133
 
134
+function update_wifi_adaptors {
135
+    IFACE=
136
+    IFACE_SECONDARY=
137
+
138
+    for i in $(seq 10 -1 0); do
139
+        if grep -q "wlan${i}" /proc/net/dev; then
140
+            if [ ! $IFACE ]; then
141
+                IFACE="wlan${i}"
142
+            else
143
+                IFACE_SECONDARY="wlan${i}"
144
+                return
145
+            fi
146
+        fi
147
+    done
148
+}
149
+
150
+function wifi_get_psk {
151
+    ssid=$1
152
+    passphrase=$2
153
+
154
+    psk=$(wpa_passphrase "$ssid" "$passphrase" | grep 'psk=' | sed -n 2p | awk -F '=' '{print $2}')
155
+    echo $psk
156
+}
157
+
158
+function hotspot_off {
159
+    if [ ! -f /etc/hostapd/hostapd.conf ]; then
160
+        return
161
+    fi
162
+    systemctl stop hostapd
163
+
164
+    rm /etc/hostapd/hostapd.conf
165
+
166
+    if [ -f /etc/network/interfaces_original ]; then
167
+        cp /etc/network/interfaces_original /etc/network/interfaces
168
+    else
169
+        echo '# interfaces(5) file used by ifup(8) and ifdown(8)' > /etc/network/interfaces
170
+        echo '# Include files from /etc/network/interfaces.d:' >> /etc/network/interfaces
171
+        echo 'source-directory /etc/network/interfaces.d' >> /etc/network/interfaces
172
+    fi
173
+
174
+    systemctl restart network-manager
175
+    ifdown $WIFI_INTERFACE
176
+}
177
+
178
+function hotspot_on {
179
+    if [ ! -f /etc/default/hostapd ]; then
180
+        echo $'/etc/default/hostapd was not found'
181
+        exit 67241
182
+    fi
183
+    if [ ${#WIFI_PASSPHRASE} -lt 8 ]; then
184
+        echo $'Wifi hotspot passphrase is too short'
185
+        exit 25719
186
+    fi
187
+
188
+    sed -i 's|#DAEMON_CONF=.*|DAEMON_CONF="/etc/hostapd/hostapd.conf"|g' /etc/default/hostapd
189
+
190
+    echo '### Wireless network name ###' > /etc/hostapd/hostapd.conf
191
+    echo "interface=$WIFI_INTERFACE" >> /etc/hostapd/hostapd.conf
192
+    echo '' >> /etc/hostapd/hostapd.conf
193
+    echo '### Set your bridge name ###' >> /etc/hostapd/hostapd.conf
194
+    echo 'bridge=br0' >> /etc/hostapd/hostapd.conf
195
+    echo '' >> /etc/hostapd/hostapd.conf
196
+    echo 'driver=nl80211' >> /etc/hostapd/hostapd.conf
197
+    echo "country_code=UK" >> /etc/hostapd/hostapd.conf
198
+    echo "ssid=$WIFI_SSID" >> /etc/hostapd/hostapd.conf
199
+    echo 'hw_mode=g' >> /etc/hostapd/hostapd.conf
200
+    echo 'channel=6' >> /etc/hostapd/hostapd.conf
201
+    echo 'wpa=2' >> /etc/hostapd/hostapd.conf
202
+    echo "wpa_passphrase=$WIFI_PASSPHRASE" >> /etc/hostapd/hostapd.conf
203
+    echo '' >> /etc/hostapd/hostapd.conf
204
+    echo '## Key management algorithms ##' >> /etc/hostapd/hostapd.conf
205
+    echo 'wpa_key_mgmt=WPA-PSK' >> /etc/hostapd/hostapd.conf
206
+    echo '' >> /etc/hostapd/hostapd.conf
207
+    echo '## Set cipher suites (encryption algorithms) ##' >> /etc/hostapd/hostapd.conf
208
+    echo '## TKIP = Temporal Key Integrity Protocol' >> /etc/hostapd/hostapd.conf
209
+    echo '## CCMP = AES in Counter mode with CBC-MAC' >> /etc/hostapd/hostapd.conf
210
+    echo 'wpa_pairwise=TKIP' >> /etc/hostapd/hostapd.conf
211
+    echo 'rsn_pairwise=CCMP' >> /etc/hostapd/hostapd.conf
212
+    echo '' >> /etc/hostapd/hostapd.conf
213
+    echo '## Shared Key Authentication ##'
214
+    echo 'auth_algs=1' >> /etc/hostapd/hostapd.conf
215
+    echo '' >> /etc/hostapd/hostapd.conf
216
+    echo '## Accept all MAC address ###' >> /etc/hostapd/hostapd.conf
217
+    echo 'macaddr_acl=0' >> /etc/hostapd/hostapd.conf
218
+
219
+    if [ ! -f /etc/network/interfaces_original ]; then
220
+        if ! grep -q "# wifi enabled" /etc/network/interfaces; then
221
+            cp /etc/network/interfaces /etc/network/interfaces_original
222
+        fi
223
+    fi
224
+
225
+    echo '# wifi enabled' > /etc/network/interfaces
226
+    echo 'auto lo br0' >> /etc/network/interfaces
227
+    echo 'iface lo inet loopback' >> /etc/network/interfaces
228
+    echo '' >> /etc/network/interfaces
229
+    echo "# wireless $WIFI_INTERFACE" >> /etc/network/interfaces
230
+    echo "allow-hotplug $WIFI_INTERFACE" >> /etc/network/interfaces
231
+    echo "iface $WIFI_INTERFACE inet manual" >> /etc/network/interfaces
232
+    echo '' >> /etc/network/interfaces
233
+    echo '# eth0 connected to the ISP router' >> /etc/network/interfaces
234
+    echo 'allow-hotplug eth0' >> /etc/network/interfaces
235
+    echo 'iface eth0 inet manual' >> /etc/network/interfaces
236
+    echo '' >> /etc/network/interfaces
237
+    echo '# Setup bridge' >> /etc/network/interfaces
238
+    echo 'iface br0 inet static' >> /etc/network/interfaces
239
+    echo "    bridge_ports $WIFI_INTERFACE eth0" >> /etc/network/interfaces
240
+
241
+    systemctl restart network-manager
242
+    ifup $WIFI_INTERFACE
243
+    systemctl restart hostapd
244
+}
245
+
246
+function wifi_wpa2_psk {
247
+    ssid=$1
248
+    passphrase=$2
249
+
250
+    if [ ! -f /etc/network/interfaces_original ]; then
251
+        if ! grep -q "# wifi enabled" /etc/network/interfaces; then
252
+            cp /etc/network/interfaces /etc/network/interfaces_original
253
+        fi
254
+    fi
255
+
256
+    echo '# wifi enabled' > /etc/network/interfaces
257
+    echo 'auto lo' >> /etc/network/interfaces
258
+    echo 'iface lo inet loopback' >> /etc/network/interfaces
259
+    echo '' >> /etc/network/interfaces
260
+    echo 'allow-hotplug eth0' >> /etc/network/interfaces
261
+    echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
262
+    echo '' >> /etc/network/interfaces
263
+    echo "allow-hotplug ${WIFI_INTERFACE}" >> /etc/network/interfaces
264
+    echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces
265
+    echo "  wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
266
+    echo '' >> /etc/network/interfaces
267
+    echo 'iface default inet dhcp' >> /etc/network/interfaces
268
+
269
+    wpa_passphrase "$ssid" "$passphrase" > $WIFI_CONFIG
270
+
271
+    systemctl restart network-manager
272
+    ifup ${WIFI_INTERFACE}
273
+}
274
+
275
+function wifi_none {
276
+    ssid=$1
277
+
278
+    if [ ! -f /etc/network/interfaces_original ]; then
279
+        if ! grep -q "# wifi enabled" /etc/network/interfaces; then
280
+            cp /etc/network/interfaces /etc/network/interfaces_original
281
+        fi
282
+    fi
283
+
284
+    echo '# wifi enabled' > /etc/network/interfaces
285
+    echo 'auto lo' >> /etc/network/interfaces
286
+    echo 'iface lo inet loopback' >> /etc/network/interfaces
287
+    echo '' >> /etc/network/interfaces
288
+    echo 'allow-hotplug eth0' >> /etc/network/interfaces
289
+    echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
290
+    echo '' >> /etc/network/interfaces
291
+    echo "allow-hotplug ${WIFI_INTERFACE}" >> /etc/network/interfaces
292
+    echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces
293
+    echo "  wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
294
+    echo '' >> /etc/network/interfaces
295
+    echo 'iface default inet dhcp' >> /etc/network/interfaces
296
+
297
+    echo 'network={' > $WIFI_CONFIG
298
+    echo "  ssid=\"${ssid}\"" >> $WIFI_CONFIG
299
+    echo '  key_mgmt=NONE' >> $WIFI_CONFIG
300
+    echo '}' >> $WIFI_CONFIG
301
+
302
+    systemctl restart network-manager
303
+    ifup ${WIFI_INTERFACE}
304
+}
305
+
306
+function networks_from_file {
307
+    if [ ! -f $WIFI_NETWORKS_FILE ]; then
308
+        exit 4
309
+    fi
310
+
311
+    if [ ! -f /etc/network/interfaces_original ]; then
312
+        if ! grep -q "# wifi enabled" /etc/network/interfaces; then
313
+            cp /etc/network/interfaces /etc/network/interfaces_original
314
+        fi
315
+    fi
316
+
317
+    echo '# wifi enabled' > /etc/network/interfaces
318
+    echo 'auto lo' >> /etc/network/interfaces
319
+    echo 'iface lo inet loopback' >> /etc/network/interfaces
320
+    echo '' >> /etc/network/interfaces
321
+    echo 'allow-hotplug eth0' >> /etc/network/interfaces
322
+    echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
323
+    echo '' >> /etc/network/interfaces
324
+    echo "allow-hotplug ${WIFI_INTERFACE}" >> /etc/network/interfaces
325
+    echo "iface ${WIFI_INTERFACE} inet manual" >> /etc/network/interfaces
326
+    echo "  wpa-roam $WIFI_CONFIG" >> /etc/network/interfaces
327
+    echo '' >> /etc/network/interfaces
328
+    echo 'iface default inet dhcp' >> /etc/network/interfaces
329
+
330
+    # remove wpa_supplicant.conf if it exists
331
+    if [ -f $WIFI_CONFIG ]; then
332
+        rm -f $WIFI_CONFIG
333
+    fi
334
+
335
+    ctr=0
336
+    while read -r line
337
+    do
338
+        if [ ${#line} -gt 1 ]; then
339
+            if [[ "$line" != '#'* ]]; then
340
+                if [ $ctr -eq 0 ]; then
341
+                    WIFI_SSID="$line"
342
+                fi
343
+                if [ $ctr -eq 1 ]; then
344
+                    WIFI_TYPE="$line"
345
+                    if [[ $WIFI_TYPE == $'none' || $WIFI_TYPE == $'open' ]]; then
346
+                        echo 'network={' >> $WIFI_CONFIG
347
+                        echo "  ssid=\"${WIFI_SSID}\"" >> $WIFI_CONFIG
348
+                        echo '  key_mgmt=NONE' >> $WIFI_CONFIG
349
+                        echo '}' >> $WIFI_CONFIG
350
+                        ctr=0
351
+                        continue
352
+                    fi
353
+                fi
354
+                if [ $ctr -eq 2 ]; then
355
+                    WIFI_PASSPHRASE="$line"
356
+                    wpa_passphrase "$WIFI_SSID" "$WIFI_PASSPHRASE" >> $WIFI_CONFIG
357
+                    ctr=0
358
+                    continue
359
+                fi
360
+
361
+                ctr=$((ctr + 1))
362
+            fi
363
+        fi
364
+    done < $WIFI_NETWORKS_FILE
365
+
366
+    systemctl restart network-manager
367
+    ifup ${WIFI_INTERFACE}
368
+}
369
+
370
+function create_networks_interactive {
371
+    update_wifi_adaptors
372
+    if [ ! $IFACE ]; then
373
+        # Don't try to configure wifi if there are no adaptors
374
+        return
375
+    fi
376
+
377
+    if [ -f $WIFI_NETWORKS_FILE ]; then
378
+        rm $WIFI_NETWORKS_FILE
379
+    fi
380
+
381
+    echo $'# Add wifi networks as follows:' > $WIFI_NETWORKS_FILE
382
+    echo '#' >> $WIFI_NETWORKS_FILE
383
+    echo $'# MySSID' >> $WIFI_NETWORKS_FILE
384
+    echo $'# wpa2-psk' >> $WIFI_NETWORKS_FILE
385
+    echo $'# myWifiPassphrase' >> $WIFI_NETWORKS_FILE
386
+    echo '#' >> $WIFI_NETWORKS_FILE
387
+    echo $'# AnotherSSID' >> $WIFI_NETWORKS_FILE
388
+    echo $'# none' >> $WIFI_NETWORKS_FILE
389
+    echo '#' >> $WIFI_NETWORKS_FILE
390
+
391
+    wifi_ctr=0
392
+    wifi_networks_done=
393
+    while [ ! $wifi_networks_done ]
394
+    do
395
+        data=$(tempfile 2>/dev/null)
396
+        trap "rm -f $data" 0 1 2 5 15
397
+        dialog --backtitle $"Freedombone Configuration" \
398
+               --title $"Wifi Settings ${wifi_ctr}" \
399
+               --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 \
400
+               $"SSID:" 1 1 "$WIFI_SSID" 1 16 30 30 \
401
+               $"Type:" 2 1 "$WIFI_TYPE" 2 16 10 10 \
402
+               $"Passphrase:" 3 1 "$WIFI_PASSPHRASE" 3 16 30 30 \
403
+               2> $data
404
+        sel=$?
405
+        case $sel in
406
+            1) return;;
407
+            255) return;;
408
+        esac
409
+        WIFI_SSID=$(cat $data | sed -n 1p)
410
+        WIFI_TYPE=$(cat $data | sed -n 2p)
411
+        WIFI_PASSPHRASE=$(cat $data | sed -n 3p)
412
+
413
+        # if these fields are empty then there are no more wifi networks
414
+        if [ ${#WIFI_SSID} -lt 2 ]; then
415
+            wifi_networks_done='yes'
416
+            continue
417
+        fi
418
+        if [ ${#WIFI_TYPE} -lt 2 ]; then
419
+            wifi_networks_done='yes'
420
+            continue
421
+        fi
422
+
423
+        # update the wifi networks file
424
+        echo '' >> $WIFI_NETWORKS_FILE
425
+        echo "$WIFI_SSID" >> $WIFI_NETWORKS_FILE
426
+        echo "$WIFI_TYPE" >> $WIFI_NETWORKS_FILE
427
+        if [ ${#WIFI_PASSPHRASE} -gt 1 ]; then
428
+            echo "$WIFI_PASSPHRASE" >> $WIFI_NETWORKS_FILE
429
+        fi
430
+
431
+        if [ ${#WIFI_SSID} -gt 1 ]; then
432
+            if [ ${#WIFI_TYPE} -gt 1 ]; then
433
+                if [[ "${WIFI_TYPE}" == $'none' || "${WIFI_TYPE}" == $'open' ]]; then
434
+                    return
435
+                else
436
+                    if [ ${#WIFI_PASSPHRASE} -gt 1 ]; then
437
+                        return
438
+                    fi
439
+                fi
440
+            fi
441
+        fi
442
+
443
+        # clear values
444
+        WIFI_SSID=
445
+        WIFI_PASSPHRASE=
446
+
447
+        wifi_ctr=$((wifi_ctr + 1))
448
+    done
449
+}
450
+
451
+function disable_wifi {
452
+    if [[ ${1} == 'yes' || ${1} == 'y' ]]; then
453
+        hotspot_off
454
+        echo '# interfaces(5) file used by ifup(8) and ifdown(8)' > /etc/network/interfaces
455
+        echo '# Include files from /etc/network/interfaces.d:' >> /etc/network/interfaces
456
+        echo 'source-directory /etc/network/interfaces.d' >> /etc/network/interfaces
457
+        systemctl restart network-manager
458
+        ifdown ${WIFI_INTERFACE}
459
+    else
460
+        networks_from_file
461
+    fi
462
+}
463
+
464
+function count_wlan {
465
+    # counts the number of wlan devices
466
+    ctr=0
467
+
468
+    for i in $(seq 0 1 10); do
469
+        if grep -q "wlan${i}" /proc/net/dev; then
470
+            ctr=$((ctr + 1))
471
+        fi
472
+    done
473
+    echo $ctr
474
+}
475
+
134 476
 # NOTE: deliberately no exit 0

+ 1
- 329
src/freedombone-wifi View File

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