summaryrefslogtreecommitdiffstats
path: root/src-wireless/wireless-dbus
blob: 2a60fc41fe03bac1347dcb135c37e90063b7aa0b (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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#! /usr/bin/env python
import sys
import dbus
import shlex
import subprocess
from signal import SIGTERM
from os import kill
from time import sleep

# dbus constants.
WPAS_DBUS_SERVICE = "fi.epitest.hostap.WPASupplicant"
WPAS_DBUS_INTERFACE = "fi.epitest.hostap.WPASupplicant"
WPAS_DBUS_OPATH = "/fi/epitest/hostap/WPASupplicant"

WPAS_DBUS_INTERFACES_INTERFACE = "fi.epitest.hostap.WPASupplicant.Interface"
WPAS_DBUS_INTERFACES_OPATH = "/fi/epitest/hostap/WPASupplicant/Interfaces"
WPAS_DBUS_BSSID_INTERFACE = "fi.epitest.hostap.WPASupplicant.BSSID"
WPAS_DBUS_NETWORKS_INTERFACE = "fi.epitest.hostap.WPASupplicant.Network"

def read_config(config):
    cfg = shlex.split(open(config, "r").read())
    options = {}
    for line in cfg:
        (var, delim, value) = line.partition('=')  
        if delim and var.lstrip()[0] != "#":
            options[var] = value
    return options

def start(profile):
    # TODO: Add check if it's even a wireless interface
    # Interface up - probably redundant, should be 'ip' instead.
    #try:
    #    subprocess.check_call(["ifconfig", profile['INTERFACE'], "up"])
    #except subprocess.CalledProcessError:
    #    print " - Could not bring interface up"
    #    return False

    # Manually set any iwconfig options - redundant, use custom wpa_supplicant config instead.
    #if profile.has_key('IWCONFIG'):
    #    subprocess.call(["iwconfig", profile['INTERFACE'], profile['IWCONFIG']])

    # Base arguments
    args=["wpa_supplicant", "-Bu", "-P/var/run/wpa_supplicant.pid"]
    
    try:
        args.append(profile['WPA_OPTS'])
    except KeyError:
        args.append("-Dwext")

    if profile['SECURITY'] == "wpa-config":
        try:
            args.append("-c" + profile["WPA_CONF"])
        except KeyError:
            args.append("-c/etc/wpa_supplicant.conf")
    elif not profile['SECURITY'] in ['wpa', 'wep']:
        print " - Invalid security chosen"
        return False

    # 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

    # Connect to wpa_supplicant
    bus = dbus.SystemBus()
    wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_OPATH)
    wpas = dbus.Interface(wpas_obj, WPAS_DBUS_INTERFACE)
   
    # Add/Get interface path
    try:
        driver=profile["WPA_DRIVER"]
    except KeyError:
        driver="wext"

    try:
        path = wpas.getInterface(profile["INTERFACE"])
    except dbus.exceptions.DBusException:
        path = wpas.addInterface(profile["INTERFACE"], {"driver":dbus.String(driver,variant_level=1)})

    # Get interface object
    if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
    iface = dbus.Interface(if_obj, WPAS_DBUS_INTERFACES_INTERFACE);  
    
    # Connect

    if profile['SECURITY'] in ['wpa','wep']:
        # Add+select the network
        path = iface.addNetwork()
        net_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
        rnet = dbus.Interface(net_obj, WPAS_DBUS_NETWORKS_INTERFACE)
        iface.selectNetwork(rnet)

        if profile['SECURITY'] == "wpa":
            # Set the options
            opts = dbus.Dictionary({"ssid": dbus.ByteArray(profile['ESSID']), "psk": dbus.String(profile['KEY'])}, signature="sv")
        elif profile['SECURITY'] == "wep":
            opts = dbus.Dictionary({"ssid": dbus.ByteArray(profile['ESSID']), "wpa_key0": dbus.String(profile['KEY'])}, signature="sv")

        rnet.set(opts)
       

    # Determine timeout
    try:
        timeout = profile["TIMEOUT"]
    except KeyError:
        timeout = 15

    # Check for association
    n=0
    while n <= timeout:
        n+=1
        sleep(1)
        state = iface.state()
        if state == "COMPLETED":
            break
        
        if n == timeout:
            print " - Association/Authentication failed:", state
            return False

    # 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
  
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 

if __name__ == "__main__":
    profile = read_config("/etc/network.d/"+sys.argv[2])
 
    if sys.argv[1] == "up":
        sys.exit(not start(profile))
    elif sys.argv[1] == "down":
        sys.exit(not stop(profile))