summaryrefslogtreecommitdiffstats
path: root/src/8021x
blob: ce4fbf22daf96977742e67c212554a6cd4e82343 (plain)
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
# Uses wpa_supplicant to check for association to a network
# wpa_check interface [timeout]
wpa_check()
{
    local timeout=0 INTERFACE="$1" TIMEOUT="${2:-15}"

    while [[ $timeout -lt "$TIMEOUT" ]]; do
        ( # Sometimes wpa_supplicant isn't ready so silence errors for 2s only to avoid hiding real errors
        if [[ $timeout -lt 2 ]]; then
            eval $(wpa_cli -i "$INTERFACE" status 2> /dev/null | fgrep "wpa_state=")
        else
            eval $(wpa_cli -i "$INTERFACE" status | fgrep "wpa_state=")
        fi
        [[ "$wpa_state" = "COMPLETED" ]]
        ) && return 0
        sleep 1
        let timeout++
    done
    echo "$wpa_state"
    # wpa_cli -i "$INTERFACE" terminate >/dev/null 2>&1   # callers sometimes called stop_wpa, which does more but seems redundant
                                                        # termination should either be handled properly here, or by callers
    stop_wpa "$INTERFACE"
    return 1
}

start_wpa()
{
    local INTERFACE="$1" WPA_CONF="$2" WPA_DRIVER="$3"
    shift 3
    local WPA_OPTS="$*"

    wpa_supplicant -B -P "/var/run/wpa_supplicant_${INTERFACE}.pid" -i "$INTERFACE" -D "$WPA_DRIVER" -c "$WPA_CONF" $WPA_OPTS
    sleep 1

    if [[ ! -f "/var/run/wpa_supplicant_${INTERFACE}.pid" ]]; then
        return 1
    fi
}

stop_wpa()
{
    wpa_cli -i "$1" terminate &> /dev/null
    sleep 1         # JP: need this else the file tends to disappear after [[ -f ... ]] but before cat...
                    # see <http://bbs.archlinux.org/viewtopic.php?pid=515667#p515667>
    if [[ -f "/var/run/wpa_supplicant_$1.pid" ]]; then
        kill "$(cat "/var/run/wpa_supplicant_$1.pid")" &>/dev/null &
    fi
}

# Requires already loaded profile
make_wpa_config_file() {
    local interface=$1
    local WPA_CONF="${TMPDIR:-/tmp}/wpa.${interface}" # substitute spaces out

    # make empty tmp dir with correct permissions, rename it
    rm -rf "$WPA_CONF"
    mv -f "$(mktemp -d)" "$WPA_CONF" || return 1
    echo "ctrl_interface=/var/run/wpa_supplicant" >> "$WPA_CONF/wpa.conf"    # we know $WPA_CONF now has no spaces, but it may have other nasty chars, so still needs to be quoted
    echo "ctrl_interface_group=${WPA_GROUP:-wheel}" >> "$WPA_CONF/wpa.conf"
    [[ $WPA_COUNTRY ]] && echo "country=$WPA_COUNTRY" >> "$WPA_CONF/wpa.conf"
    echo "$WPA_CONF/wpa.conf"
}

# Requires already loaded profile
make_wpa_config() {
    case $SECURITY in
    wep|wep-old)
        if [[ ${KEY:0:2} == "s:" ]]; then # TODO: does wpa_supplicant handle this as expected?
            echo "ssid=\"$ESSID\" \nkey_mgmt=NONE \nwep_key0=\"${KEY:2}\" \nwep_tx_keyidx=0"
        else
            echo "ssid=\"$ESSID\" \nkey_mgmt=NONE \nwep_key0=$KEY \nwep_tx_keyidx=0"
        fi
        ;;
    none|none-old)
        echo "ssid=\"$ESSID\" \nkey_mgmt=NONE"
        ;;
    wpa)
        if [[ "${#KEY}" -eq 64 ]]; then
            echo "ssid=\"$ESSID\" \npsk=$KEY"
        else
            echo "ssid=\"$ESSID\" \npsk=\"$KEY\""
        fi
        ;;
    wpa-configsection)
        echo "$CONFIGSECTION"
        ;;
    *)
        return 1
        ;;
    esac
}

# vim: ft=sh ts=4 et sw=4: