summaryrefslogtreecommitdiffstats
path: root/src-wireless/wireless-dbus
blob: f48ed5280aa1d02e81fa674284329e7033265b7a (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#! /usr/bin/env python
import sys
import dbus
import shlex
import subprocess
from signal import SIGTERM
import os
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:
            var = var.lstrip()                                          # JP: allow assignments to be indented, as they can be when sourced
            if var[0] != '#':
                value = value.partition("#")[0].rstrip()                # JP: strip off comments and trailing whitespace
                if value[0] == value[-1] and value[0] in ('"',"'"):     # JP: strip any surrounding quotes
                    value=value[1:-1]
                options[var] = value
    return options


def wep_hex2dec(key):
    if len(key) not in [10, 26]:
        fail("Bad key", report_type="err")

    x=0
    new_key=[]
    while x<len(key):
        new_key.append(int(key[x:x+2],16))
        x+=2

    return new_key

# JP: connect to our hookable reporting functions in /usr/lib/network/globals

def report(report_type, *args):
    report_handler.stdin.write('report_%s %s\n' % (report_type, ' '.join(map(repr,args))))

def fail(msg=None, report_type="fail"):
    if msg:
        report(report_type, msg)
    try:
        pid = open("/run/wpa_supplicant.pid").read()
    except IOError:
        pass
    else:
        os.kill(int(pid),SIGTERM)
    sys.exit(1)


def start(profile, essid):
    # 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

    # Base arguments
    args=["wpa_supplicant", "-Bu", "-P/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', 'none']:
        fail("Invalid security chosen", report_type="err")

    # Start wpa_supplicant
    report('debug', 'wireless_dbus', 'starting wpa_supplicant')
    supplicant = subprocess.Popen(args,stderr=subprocess.STDOUT,stdout=subprocess.PIPE)
    output = supplicant.communicate()[0]
    if supplicant.returncode not in [255,0]:
        print >>sys.stderr, output              # JP: print to stderr
        fail("Could not start wpa_supplicant")

    # Connect to wpa_supplicant
    report('debug', 'wireless_dbus', 'connecting 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);

    # Add and select the network. Networks already specified for wpa-config
    if profile['SECURITY'] in ['wpa','wep','none']:
        report('debug', 'wireless_dbus', 'add and select 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 not essid:
        essid = profile["ESSID"]

    if profile['SECURITY'] == "wpa":
        opts = dbus.Dictionary({"ssid": dbus.ByteArray(essid),
                                "psk": dbus.String(profile['KEY'])},
                               signature="sv")
        report('debug', 'wireless_dbus', 'connect to network with security=wpa')
        rnet.set(opts)
    elif profile['SECURITY'] == "wep":
        key=profile['KEY']
        if key[:2] == "s:": # String key prefixed by "s:"
            keydbus=key[2:]
        else: # Hex key
            key=wep_hex2dec(key)
            keydbus = dbus.ByteArray()
            for l in key:
                keydbus+=chr(l)

        opts = dbus.Dictionary({"ssid": dbus.ByteArray(essid),
                                "key_mgmt": dbus.String("NONE"),
                                "wep_tx_keyidx": dbus.Int32(1),
                                "wep_key0": dbus.ByteArray(keydbus)},
                               signature="sv")
        report('debug', 'wireless_dbus', 'connect to network with security=wep')
        rnet.set(opts)
    elif profile['SECURITY'] == "none":
        opts = dbus.Dictionary({"ssid": dbus.ByteArray(essid)},
                               signature="sv")
        report('debug', 'wireless_dbus', 'connect to network with security=none')
        rnet.set(opts)

    # Determine timeout
    try:
        timeout = int(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:
            fail("Association/Authentication failed:" + state)

    # Run ethernet and get an ip.
    try:
        subprocess.check_call([ETHERNET_IPROUTE, "up", sys.argv[2]])
    except subprocess.CalledProcessError:
        fail()
    sys.exit(0)

def stop(profile):
    ret = subprocess.call([ETHERNET_IPROUTE, "down", sys.argv[2]])
    os.kill(int(open("/run/wpa_supplicant.pid").read()),SIGTERM)
    sys.exit(ret)

if __name__ == "__main__":

    CONN_DIR = os.path.abspath(os.path.dirname(sys.argv[0]))
    ETHERNET_IPROUTE = os.path.join(CONN_DIR,"ethernet-iproute")
    WIRELESS = os.path.join(CONN_DIR,"wireless")

    # setup bash report_* handler
    report_handler_script = 'source %s/../globals; while read cmd args; do eval $cmd "$args"; done' % (CONN_DIR,)
    report_handler = subprocess.Popen(report_handler_script, executable='/bin/bash', stdin=subprocess.PIPE, shell=True)

    try:
        profile_name = sys.argv[2]
        profile = read_config("/etc/network.d/"+profile_name)

        essid = sys.argv[3] if len(sys.argv)>3 else ""   # JP: pass literal ESSID as an argument, so that we can have entry in profile be a regexp
        if sys.argv[1] == "up":
            start(profile, essid)
        elif sys.argv[1] == "down":
            stop(profile)

    finally:
        report_handler.stdin.close()
        report_handler.wait()

# vim: et ts=4