diff options
author | Simon Schampijer <simon@schampijer.de> | 2008-11-13 11:52:02 (GMT) |
---|---|---|
committer | Simon Schampijer <simon@schampijer.de> | 2008-11-13 11:52:02 (GMT) |
commit | 5d4dfb6e6cbf27b579d8b6cbc7e33c526e4f684f (patch) | |
tree | 232f1479603d3025310558d07bacadd2d6163e3e /src/jarabe/model | |
parent | 500dfaa886957f550bccc33775a83f2c3b257d65 (diff) |
Add saving and loading of the nm connections and support for WPA
Diffstat (limited to 'src/jarabe/model')
-rw-r--r-- | src/jarabe/model/network.py | 220 |
1 files changed, 213 insertions, 7 deletions
diff --git a/src/jarabe/model/network.py b/src/jarabe/model/network.py index d311174..d793deb 100644 --- a/src/jarabe/model/network.py +++ b/src/jarabe/model/network.py @@ -15,10 +15,13 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import logging +import os import dbus +import ConfigParser from sugar import dispatch +from sugar import env DEVICE_TYPE_802_11_WIRELESS = 2 @@ -33,8 +36,32 @@ DEVICE_STATE_IP_CONFIG = 7 DEVICE_STATE_ACTIVATED = 8 DEVICE_STATE_FAILED = 9 -AP_FLAGS_802_11_NONE = 0 -AP_FLAGS_802_11_PRIVACY = 1 +NM_802_11_AP_FLAGS_NONE = 0x00000000 +NM_802_11_AP_FLAGS_PRIVACY = 0x00000001 + +NM_802_11_AP_SEC_NONE = 0x00000000 +NM_802_11_AP_SEC_PAIR_WEP40 = 0x00000001 +NM_802_11_AP_SEC_PAIR_WEP104 = 0x00000002 +NM_802_11_AP_SEC_PAIR_TKIP = 0x00000004 +NM_802_11_AP_SEC_PAIR_CCMP = 0x00000008 +NM_802_11_AP_SEC_GROUP_WEP40 = 0x00000010 +NM_802_11_AP_SEC_GROUP_WEP104 = 0x00000020 +NM_802_11_AP_SEC_GROUP_TKIP = 0x00000040 +NM_802_11_AP_SEC_GROUP_CCMP = 0x00000080 +NM_802_11_AP_SEC_KEY_MGMT_PSK = 0x00000100 +NM_802_11_AP_SEC_KEY_MGMT_802_1X = 0x00000200 + +NM_802_11_MODE_UNKNOWN = 0 +NM_802_11_MODE_ADHOC = 1 +NM_802_11_MODE_INFRA = 2 + +NM_802_11_DEVICE_CAP_NONE = 0x00000000 +NM_802_11_DEVICE_CAP_CIPHER_WEP40 = 0x00000001 +NM_802_11_DEVICE_CAP_CIPHER_WEP104 = 0x00000002 +NM_802_11_DEVICE_CAP_CIPHER_TKIP = 0x00000004 +NM_802_11_DEVICE_CAP_CIPHER_CCMP = 0x00000008 +NM_802_11_DEVICE_CAP_WPA = 0x00000010 +NM_802_11_DEVICE_CAP_RSN = 0x00000020 SETTINGS_SERVICE = 'org.freedesktop.NetworkManagerUserSettings' @@ -46,6 +73,78 @@ NM_SECRETS_IFACE = 'org.freedesktop.NetworkManagerSettings.Connection.Secrets' _nm_settings = None _conn_counter = 0 +class WirelessSecurity(object): + def __init__(self): + self.key_mgmt = None + self.proto = None + self.group = None + self.pairwise = None + + def get_dict(self): + wireless_security = {} + + if self.key_mgmt is not None: + wireless_security['key-mgmt'] = self.key_mgmt + if self.proto is not None: + wireless_security['proto'] = self.proto + if self.pairwise is not None: + wireless_security['pairwise'] = self.pairwise + if self.group is not None: + wireless_security['group'] = self.group + + return wireless_security + +class Wireless(object): + def __init__(self): + self.ssid = None + + def get_dict(self): + return {'ssid': self.ssid} + +class Connection(object): + def __init__(self): + self.id = None + self.uuid = None + self.type = None + + def get_dict(self): + return {'id': self.id, + 'uuid': self.uuid, + 'type': self.type} + +class Settings(object): + def __init__(self): + self.connection = Connection() + self.wireless = Wireless() + self.wireless_security = None + + def get_dict(self): + settings = {} + settings['connection'] = self.connection.get_dict() + settings['802-11-wireless'] = self.wireless.get_dict() + if self.wireless_security is not None: + settings['802-11-wireless-security'] = \ + self.wireless_security.get_dict() + return settings + +class Secrets(object): + def __init__(self): + self.wep_key = None + self.psk = None + self.auth_alg = None + + def get_dict(self): + secrets = {} + + if self.wep_key is not None: + secrets['wep-key0'] = self.wep_key + if self.psk is not None: + secrets['psk'] = self.psk + if self.auth_alg is not None: + secrets['auth-alg'] = self.auth_alg + + return {'802-11-wireless-security': secrets} + class NMSettings(dbus.service.Object): def __init__(self): bus = dbus.SystemBus() @@ -84,7 +183,7 @@ class SecretsResponse(object): def set_secrets(self, secrets): self._connection.set_secrets(secrets) - self._reply_cb(secrets) + self._reply_cb(secrets.get_dict()) def set_error(self, error): self._error_cb(error) @@ -103,11 +202,62 @@ class NMSettingsConnection(dbus.service.Object): def set_secrets(self, secrets): self._secrets = secrets + self.save() + + def save(self): + profile_path = env.get_profile_path() + config_path = os.path.join(profile_path, 'nm', 'connections.cfg') + + config = ConfigParser.ConfigParser() + try: + try: + if not config.read(config_path): + logging.error('Error reading the nm config file') + return + except ConfigParser.ParsingError, e: + logging.error('Error reading the nm config file: %s' % e) + return + identifier = self._settings.connection.id + + if identifier not in config.sections(): + config.add_section(identifier) + config.set(identifier, 'type', self._settings.connection.type) + config.set(identifier, 'ssid', self._settings.wireless.ssid) + config.set(identifier, 'uuid', self._settings.connection.uuid) + + if self._settings.wireless_security is not None: + if self._settings.wireless_security.key_mgmt is not None: + config.set(identifier, 'key-mgmt', + self._settings.wireless_security.key_mgmt) + if self._settings.wireless_security.proto is not None: + config.set(identifier, 'proto', + self._settings.wireless_security.proto) + if self._settings.wireless_security.pairwise is not None: + config.set(identifier, 'pairwise', + self._settings.wireless_security.pairwise) + if self._settings.wireless_security.group is not None: + config.set(identifier, 'group', + self._settings.wireless_security.group) + if self._secrets is not None: + if self._settings.wireless_security.key_mgmt == 'none': + config.set(identifier, 'key', self._secrets.wep_key) + config.set(identifier, 'auth-alg', self._secrets.auth_alg) + elif self._settings.wireless_security.key_mgmt == 'wpa-psk': + config.set(identifier, 'key', self._secrets.psk) + except ConfigParser.Error, e: + logging.error('Error constructing %s: %s' % (identifier, e)) + else: + f = open(config_path, 'w') + try: + config.write(f) + except ConfigParser.Error, e: + logging.error('Can not write %s error: %s' % (config_path, e)) + f.close() @dbus.service.method(dbus_interface=NM_CONNECTION_IFACE, in_signature='', out_signature='a{sa{sv}}') def GetSettings(self): - return self._settings + return self._settings.get_dict() @dbus.service.method(dbus_interface=NM_SECRETS_IFACE, async_callbacks=('reply', 'error'), @@ -124,7 +274,7 @@ class NMSettingsConnection(dbus.service.Object): except Exception, e: logging.error('Error requesting the secrets via dialog: %s' % e) else: - reply(self._secrets) + reply(self._secrets.get_dict()) def get_settings(): global _nm_settings @@ -133,6 +283,7 @@ def get_settings(): _nm_settings = NMSettings() except dbus.DBusException, e: logging.error('Cannot create the UserSettings service %s.', e) + load_connections() return _nm_settings def find_connection(ssid): @@ -150,8 +301,63 @@ def add_connection(ssid, settings, secrets=None): conn = NMSettingsConnection(path, settings, secrets) _nm_settings.add_connection(ssid, conn) - return conn def load_connections(): - pass + profile_path = env.get_profile_path() + config_path = os.path.join(profile_path, 'nm', 'connections.cfg') + + config = ConfigParser.ConfigParser() + + if not os.path.exists(config_path): + if not os.path.exists(os.path.dirname(config_path)): + os.makedirs(os.path.dirname(config_path), 0755) + f = open(config_path, 'w') + config.write(f) + f.close() + + try: + if not config.read(config_path): + logging.error('Error reading the nm config file') + return + except ConfigParser.ParsingError, e: + logging.error('Error reading the nm config file: %s' % e) + return + + for section in config.sections(): + try: + settings = Settings() + settings.connection.id = section + ssid = config.get(section, 'ssid') + settings.wireless.ssid = dbus.ByteArray(ssid) + uuid = config.get(section, 'uuid') + settings.connection.uuid = uuid + nmtype = config.get(section, 'type') + settings.connection.type = nmtype + + secrets = None + if config.has_option(section, 'key-mgmt'): + secrets = Secrets() + settings.wireless_security = WirelessSecurity() + mgmt = config.get(section, 'key-mgmt') + settings.wireless_security.key_mgmt = mgmt + key = config.get(section, 'key') + if mgmt == 'none': + secrets.wep_key = key + auth_alg = config.get(section, 'auth-alg') + secrets.auth_alg = auth_alg + elif mgmt == 'wpa-psk': + secrets.psk = key + if config.has_option(section, 'proto'): + value = config.get(section, 'proto') + settings.wireless_security.proto = value + if config.has_option(section, 'group'): + value = config.get(section, 'group') + settings.wireless_security.group = value + if config.has_option(section, 'pairwise'): + value = config.get(section, 'pairwise') + settings.wireless_security.pairwise = value + except ConfigParser.Error, e: + logging.error('Error reading section: %s' % e) + else: + add_connection(ssid, settings, secrets) |