From bb3bade410cbfb4d19b65884b6525be7603dd686 Mon Sep 17 00:00:00 2001 From: James Rayner Date: Mon, 18 May 2009 16:59:34 +1000 Subject: wireless: check if iface is real, stop wpa_supplicant on failures --- src-wireless/wireless-dbus | 26 +++++++++++++++----------- src/8021x | 10 ++++++++++ src/connections/wireless | 28 ++++++++++++++++++++-------- 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src-wireless/wireless-dbus b/src-wireless/wireless-dbus index 0ddd5ab..6f53a6c 100755 --- a/src-wireless/wireless-dbus +++ b/src-wireless/wireless-dbus @@ -40,6 +40,13 @@ def wep_hex2dec(key): return new_key +def fail(msg=None): + if msg: + print " -", msg + kill(int(open("/var/run/wpa_supplicant.pid").read()),SIGTERM) + sys.exit(1) + + def start(profile): # TODO: Add check if it's even a wireless interface # Interface up - probably redundant, should be 'ip' instead. @@ -67,16 +74,14 @@ def start(profile): except KeyError: args.append("-c/etc/wpa_supplicant.conf") elif not profile['SECURITY'] in ['wpa', 'wep', 'none']: - print " - Invalid security chosen" - return False + fail("Invalid security chosen") # Start wpa_supplicant supplicant = subprocess.Popen(args,stderr=subprocess.STDOUT,stdout=subprocess.PIPE) output = supplicant.communicate()[0] if supplicant.returncode not in [255,0]: print output - print " - Could not start wpa_supplicant" - return False + fail("Could not start wpa_supplicant") # Connect to wpa_supplicant bus = dbus.SystemBus() @@ -149,25 +154,24 @@ def start(profile): break if n == timeout: - print " - Association/Authentication failed:", state - return False + fail("Association/Authentication failed:" + state) # Run ethernet and get an ip. try: subprocess.check_call(["/usr/lib/network/connections/ethernet-iproute", "up", sys.argv[2]]) except subprocess.CalledProcessError: - return False - return True + fail() + sys.exit(0) def stop(profile): subprocess.call(["/usr/lib/network/connections/ethernet", "down", sys.argv[2]]) kill(int(open("/var/run/wpa_supplicant.pid").read()),SIGTERM) - return True + sys.exit(0) if __name__ == "__main__": profile = read_config("/etc/network.d/"+sys.argv[2]) if sys.argv[1] == "up": - sys.exit(not start(profile)) + start(profile) elif sys.argv[1] == "down": - sys.exit(not stop(profile)) + stop(profile) diff --git a/src/8021x b/src/8021x index 8dbff29..75030e9 100644 --- a/src/8021x +++ b/src/8021x @@ -34,3 +34,13 @@ start_wpa() return 1 fi } + +stop_wpa() +{ + wpa_cli terminate &> /dev/null + if [[ -f /var/run/wpa_supplicant_$1.pid ]]; then + kill $(cat /var/run/wpa_supplicant_$1.pid) &>/dev/null & + fi +} + +# vim: set ts=4 et sw=4 ft=sh: diff --git a/src/connections/wireless b/src/connections/wireless index 66ccdab..653cb00 100644 --- a/src/connections/wireless +++ b/src/connections/wireless @@ -4,13 +4,19 @@ wireless_up() { load_profile $1 - + . ${SUBR_DIR}/8021x . ${SUBR_DIR}/wireless + # Check if interface exists + if [[ ! -e /sys/class/net/"$INTERFACE" ]]; then + if ! echo "$INTERFACE"|grep ":"; then + err_append "interface $INTERFACE does not exist" + return 1 + fi + fi + # Kill any lingering wpa_supplicants. - if [[ -f /var/run/wpa_supplicant_$INTERFACE.pid ]]; then - kill $(cat /var/run/wpa_supplicant_$INTERFACE.pid) - fi + stop_wpa $INTERFACE # Most drivers (mac80211) need mode set before device is brought up # Drivers generally default to managed, but set this to be sure. @@ -57,7 +63,6 @@ wireless_up() { wep_check $INTERFACE $TIMEOUT||return 1 ;; wpa) - . ${SUBR_DIR}/8021x # Quirk for broken drivers... http://bbs.archlinux.org/viewtopic.php?id=36384 quirk "wpaessid" && eval iwconfig $INTERFACE essid "\"$ESSID\"" @@ -78,14 +83,20 @@ wireless_up() { # Connect! [[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwext" start_wpa $INTERFACE $WPA_CONF $WPA_OPTS || return 1 - wpa_check $INTERFACE $TIMEOUT || return 1 + if ! wpa_check $INTERFACE $TIMEOUT; then + stop_wpa $INTERFACE + return 1 + fi ;; wpa-config) . ${SUBR_DIR}/8021x [[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf" # defaults [[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwext" start_wpa $INTERFACE $WPA_CONF $WPA_OPTS || return 1 - wpa_check $INTERFACE $TIMEOUT || return 1 + if ! wpa_check $INTERFACE $TIMEOUT; then + stop_wpa $INTERFACE + return 1 + fi ;; esac @@ -97,11 +108,12 @@ wireless_up() { wireless_down() { load_profile $1 + . ${SUBR_DIR}/8021x PROFILE=$1 NOETHERNETDOWN=$2 if ! checkyesno $2; then ${CONN_DIR}/ethernet down $1 fi - wpa_cli terminate &> /dev/null + stop_wpa $INTERFACE [[ "$SECURITY" == "wpa" ]] && rm -f "/tmp/wpa.${1// /}" # remove wpa config iwconfig $INTERFACE essid off key off &> /dev/null ifconfig $INTERFACE down -- cgit v1.2.3-24-g4f1b