summaryrefslogtreecommitdiffstats
path: root/src-wireless/netcfg-auto-wireless
blob: a3e3fb93419e2b64ee9eb7de84ddadedc1a229c7 (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
#! /bin/bash
# Originally contributed by Neuro: http://bbs.archlinux.org/viewtopic.php?pid=278148#p278148

. /usr/lib/network/network
. $SUBR_DIR/rfkill
. $SUBR_DIR/8021x
. /etc/conf.d/netcfg

# wifi_auto
#   autoconnect wireless interface
#   $1 - wireless interface
wifi_auto()
{
    local interface="$1"
    report_try "Scanning for networks"
    [[ -f "$IFACE_DIR/$interface" ]] && source "$IFACE_DIR/$interface"

    if [[ -n "$RFKILL" ]]; then
        set_rf_state "$interface" up || exit $?
    fi

    bring_interface up "$interface" # uses iproute methods---is it there any value to providing option to use ifconfig?
    networks=$(list_networks "$interface")

    if [[ -z "$networks" ]]; then
        bring_interface forcedown "$interface"
        exit_fail "- No networks available."
    fi

    # Loop through all the found essid's, then find a matching profile.

    local found_profile

    [ -z "$AUTO_PROFILES" ] && AUTO_PROFILES=$(list_profiles)

    # JP: add ability to use AP instead of ESSID
    while read ap essid; do
        echo $AUTO_PROFILES | while read network; do
            (
                unset CONNECTION INTERFACE AP ESSID
                load_profile "$network"
                case "$CONNECTION" in
                    wireless-old|wireless|wireless-dbus)
                            if [[ "$interface" = "$INTERFACE" ]]; then
                                if [[ "$ap" == "$AP" ]]; then
                                    exit 2
                                elif [[ -z "$found_profile" ]]; then
                                    if [[ "$essid" == "$ESSID" ]]; then
                                        exit 1
                                    fi
                                fi
                            fi
                            ;;
                esac
                exit 0
            )
            case $? in
                2) found_profile="$network"
                    break 2
                    ;;
                1) found_profile="$network"
                    ;;
            esac
        done
    done < "$networks"                  # avoid subshell; list_networks returns name of a tmp file
    rm -f "$networks"               # shouldn't we delete the tmp file?

    if [[ -n "$found_profile" ]]; then
        report_success
        if profile_up "$found_profile"; then
            exit 0
        else
            bring_interface forcedown "$interface"
            exit_fail "Couldn't connect profile $found_profile."
        fi
    else
        bring_interface forcedown "$interface"
        exit_fail "No profiles matched the local networks."
    fi

}

if [[ $(id -u) -ne 0 ]]; then
    exit_stderr "This script needs to be run with root privileges"
fi
if [[ -z "$1" ]]; then
    exit_stderr "Please supply an interface to connect"
fi
wifi_auto "$1"

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