File: 05_network

package info (click to toggle)
freedombox-setup 0.10
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 212 kB
  • ctags: 10
  • sloc: sh: 274; makefile: 4
file content (119 lines) | stat: -rwxr-xr-x 3,602 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash

set -e

# Configure networking for all wired and wireless devices.
#
# Creates network-manager connections.

function get-interfaces {
    # XXX: Sorting of interfaces is non-numeric
    WIRED_IFACES=$(nmcli --terse --fields type,device device | grep "^ethernet:" | cut -d: -f2 | sort)
    NO_OF_WIRED_IFACES=$(echo $WIRED_IFACES | wc -w)

    WIRELESS_IFACES=$(nmcli --terse --fields type,device device | grep "^wifi:" | cut -d: -f2 | sort)
    NO_OF_WIRELESS_IFACES=$(echo $WIRELESS_IFACES | wc -w)
}

function configure-regular-interface {
    local interface="$1"
    local zone="$2"
    local connection_name="FreedomBox WAN"

    # Create n-m connection for a regular interface
    nmcli con add con-name "$connection_name" ifname "$interface" type ethernet
    nmcli con modify "$connection_name" connection.autoconnect TRUE
    nmcli con modify "$connection_name" connection.zone "$zone"

    echo "Configured interface '$interface' for '$zone' use as '$connection_name'."
}

function configure-shared-interface {
    local interface="$1"
    local connection_name="FreedomBox LAN $interface"

    # Create n-m connection for eth1
    nmcli con add con-name "$connection_name" ifname "$interface" type ethernet
    nmcli con modify "$connection_name" connection.autoconnect TRUE
    nmcli con modify "$connection_name" connection.zone internal

    # Configure this interface to be shared with other computers.
    #  - Self-assign an address and network
    #  - Start and manage DNS server (dnsmasq)
    #  - Start and manage DHCP server (dnsmasq)
    #  - Register address with mDNS
    #  - Add firewall rules for NATing from this interface
    nmcli con modify "$connection_name" ipv4.method shared

    echo "Configured interface '$interface' for shared use as '$connection_name'."
}

function configure-wireless-interface {
    local interface="$1"
    local connection_name="FreedomBox $interface"
    local ssid="FreedomBox$interface"
    local secret="freedombox123"

    nmcli con add con-name "$connection_name" ifname "$interface" type wifi ssid "$ssid"
    nmcli con modify "$connection_name" connection.autoconnect TRUE
    nmcli con modify "$connection_name" connection.zone internal
    nmcli con modify "$connection_name" ipv4.method shared
    nmcli con modify "$connection_name" wifi.mode ap
    nmcli con modify "$connection_name" wifi-sec.key-mgmt wpa-psk
    nmcli con modify "$connection_name" wifi-sec.psk "$secret"

    echo "Configured interface '$interface' for shared use as '$connection_name'."
}

function multi-wired-setup {
    local first_interface="$1"
    shift
    local remaining_interfaces="$@"

    configure-regular-interface "$first_interface" external

    for interface in $remaining_interfaces
    do
        configure-shared-interface "$interface"
    done
}

function one-wired-setup {
    local interface="$1"

    case $NO_OF_WIRELESS_IFACES in
        "0")
            configure-regular-interface "$interface" internal
            ;;
        *)
            configure-regular-interface "$interface" external
            ;;
    esac
}

function wireless-setup {
    local interfaces="$@"

    for interface in $interfaces
    do
        configure-wireless-interface "$interface"
    done
}

echo "Setting up network configuration..."
get-interfaces

case $NO_OF_WIRED_IFACES in
    "0")
        echo "No wired interfaces detected."
        ;;
    "1")
        one-wired-setup $WIRED_IFACES
        ;;
    *)
        multi-wired-setup $WIRED_IFACES
esac

wireless-setup $WIRELESS_IFACES

echo "Done setting up network configuration."