2007-04-10 04:47:37 +02:00
|
|
|
"""User settings/configuration loading"""
|
2007-06-24 14:57:57 +02:00
|
|
|
# Copyright (C) 2006-2007, Red Hat, Inc.
|
2006-10-15 01:24:45 +02:00
|
|
|
#
|
2007-06-24 14:57:57 +02:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2 of the License, or (at your option) any later version.
|
2006-10-15 01:24:45 +02:00
|
|
|
#
|
2007-06-24 14:57:57 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2006-10-15 01:24:45 +02:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2007-06-24 14:57:57 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2006-10-15 01:24:45 +02:00
|
|
|
#
|
2007-06-24 14:57:57 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
# Boston, MA 02111-1307, USA.
|
2006-10-15 01:24:45 +02:00
|
|
|
|
2006-05-12 08:34:20 +02:00
|
|
|
import os
|
2007-02-25 23:53:10 +01:00
|
|
|
import logging
|
2006-10-16 13:34:43 +02:00
|
|
|
from ConfigParser import ConfigParser
|
2006-08-12 23:35:52 +02:00
|
|
|
|
|
|
|
from sugar import env
|
2007-02-26 01:24:48 +01:00
|
|
|
from sugar import util
|
2007-02-23 13:09:33 +01:00
|
|
|
from sugar.graphics.xocolor import XoColor
|
2006-10-16 13:34:43 +02:00
|
|
|
|
2007-09-17 13:53:10 +02:00
|
|
|
DEFAULT_JABBER_SERVER = 'olpc.collabora.co.uk'
|
2007-09-24 18:00:36 +02:00
|
|
|
DEFAULT_VOLUME = 81
|
2007-09-17 13:53:10 +02:00
|
|
|
|
|
|
|
_profile = None
|
|
|
|
|
|
|
|
def _set_key(cp, section, key, value):
|
|
|
|
if not cp.has_section(section):
|
|
|
|
cp.add_section(section)
|
|
|
|
cp.set(section, key, value)
|
|
|
|
|
|
|
|
class Profile(object):
|
2007-04-10 04:47:37 +02:00
|
|
|
"""Local user's current options/profile information
|
|
|
|
|
|
|
|
User settings are stored in an INI-style configuration
|
|
|
|
file. This object uses the ConfigParser module to load
|
2007-07-26 21:25:13 +02:00
|
|
|
the settings. (We only very rarely set keys, so we don't
|
|
|
|
keep the ConfigParser around between calls.)
|
2007-04-10 04:47:37 +02:00
|
|
|
|
|
|
|
The profile is also responsible for loading the user's
|
|
|
|
public and private ssh keys from disk.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
|
|
|
|
name -- child's name
|
|
|
|
color -- XoColor for the child's icon
|
|
|
|
server -- school server with which the child is
|
|
|
|
associated
|
|
|
|
server_registered -- whether the child has registered
|
|
|
|
with the school server or not
|
2007-07-26 21:25:13 +02:00
|
|
|
backup1 -- temporary backup info key for Trial-2
|
2007-04-10 04:47:37 +02:00
|
|
|
|
|
|
|
pubkey -- public ssh key
|
|
|
|
privkey_hash -- SHA has of the child's public key
|
|
|
|
"""
|
2007-09-17 13:53:10 +02:00
|
|
|
def __init__(self, path):
|
2007-09-17 16:28:21 +02:00
|
|
|
self.nick_name = None
|
2006-12-04 20:12:24 +01:00
|
|
|
self.color = None
|
2007-09-17 13:53:10 +02:00
|
|
|
self.jabber_server = DEFAULT_JABBER_SERVER
|
|
|
|
self.jabber_registered = False
|
2007-07-26 21:25:13 +02:00
|
|
|
self.backup1 = None
|
2008-01-09 15:35:23 +01:00
|
|
|
self.sound_volume = DEFAULT_VOLUME
|
2007-07-30 15:34:02 +02:00
|
|
|
|
2008-01-09 15:35:23 +01:00
|
|
|
self._pubkey = None
|
|
|
|
self._privkey_hash = None
|
2007-09-17 13:53:10 +02:00
|
|
|
self._config_path = path
|
2006-10-17 14:31:04 +02:00
|
|
|
|
2007-07-30 15:34:02 +02:00
|
|
|
self._load_config()
|
2006-10-17 14:31:04 +02:00
|
|
|
|
2007-09-17 13:53:10 +02:00
|
|
|
def is_valid(self):
|
2007-09-17 16:28:21 +02:00
|
|
|
return self.nick_name is not None and \
|
2007-09-17 13:53:10 +02:00
|
|
|
self.color is not None and \
|
|
|
|
self.pubkey is not None and \
|
|
|
|
self.privkey_hash is not None
|
|
|
|
|
2007-10-12 19:07:02 +02:00
|
|
|
def is_registered(self):
|
|
|
|
return self.backup1 is not None
|
|
|
|
|
2007-09-17 13:53:10 +02:00
|
|
|
def save(self):
|
|
|
|
cp = ConfigParser()
|
2008-01-09 15:35:23 +01:00
|
|
|
cp.read([self._config_path])
|
2007-09-17 13:53:10 +02:00
|
|
|
|
2007-09-17 16:28:21 +02:00
|
|
|
if self.nick_name:
|
2007-10-30 17:10:02 +01:00
|
|
|
_set_key(cp, 'Buddy', 'NickName', self.nick_name.encode('utf8'))
|
2007-09-17 13:53:10 +02:00
|
|
|
if self.color:
|
|
|
|
_set_key(cp, 'Buddy', 'Color', self.color.to_string())
|
|
|
|
if self.backup1:
|
|
|
|
_set_key(cp, 'Server', 'Backup1', self.backup1)
|
|
|
|
if self.jabber_server:
|
|
|
|
_set_key(cp, 'Jabber', 'Server', self.jabber_server)
|
|
|
|
|
|
|
|
_set_key(cp, 'Jabber', 'Registered', self.jabber_registered)
|
|
|
|
|
2007-09-24 18:00:36 +02:00
|
|
|
_set_key(cp, 'Sound', 'Volume', self.sound_volume)
|
|
|
|
|
2007-09-17 13:53:10 +02:00
|
|
|
f = open(self._config_path, 'w')
|
|
|
|
cp.write(f)
|
|
|
|
f.close()
|
|
|
|
|
2007-07-30 15:34:02 +02:00
|
|
|
def _load_config(self):
|
|
|
|
cp = ConfigParser()
|
2008-01-09 15:35:23 +01:00
|
|
|
cp.read([self._config_path])
|
2007-07-30 15:34:02 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
if cp.has_option('Buddy', 'NickName'):
|
2007-05-07 06:18:42 +02:00
|
|
|
name = cp.get('Buddy', 'NickName')
|
|
|
|
# decode nickname from ascii-safe chars to unicode
|
2007-09-17 16:28:21 +02:00
|
|
|
self.nick_name = name.decode("utf-8")
|
2006-12-04 20:12:24 +01:00
|
|
|
if cp.has_option('Buddy', 'Color'):
|
2007-02-23 13:09:33 +01:00
|
|
|
self.color = XoColor(cp.get('Buddy', 'Color'))
|
2007-09-17 13:53:10 +02:00
|
|
|
if cp.has_option('Jabber', 'Server'):
|
|
|
|
self.jabber_server = cp.get('Jabber', 'Server')
|
|
|
|
if cp.has_option('Jabber', 'Registered'):
|
|
|
|
registered = cp.get('Jabber', 'Registered')
|
2007-02-27 20:22:38 +01:00
|
|
|
if registered.lower() == "true":
|
2007-09-17 13:53:10 +02:00
|
|
|
self.jabber_registered = True
|
2007-07-26 21:25:13 +02:00
|
|
|
if cp.has_option('Server', 'Backup1'):
|
|
|
|
self.backup1 = cp.get('Server', 'Backup1')
|
2007-09-24 18:00:36 +02:00
|
|
|
if cp.has_option('Sound', 'Volume'):
|
|
|
|
self.sound_volume = float(cp.get('Sound', 'Volume'))
|
2007-02-25 23:53:10 +01:00
|
|
|
|
2007-07-30 15:34:02 +02:00
|
|
|
del cp
|
|
|
|
|
2007-02-25 23:53:10 +01:00
|
|
|
def _load_pubkey(self):
|
|
|
|
key_path = os.path.join(env.get_profile_path(), 'owner.key.pub')
|
|
|
|
try:
|
|
|
|
f = open(key_path, "r")
|
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
|
|
|
except IOError, e:
|
|
|
|
logging.error("Error reading public key: %s" % e)
|
2008-01-09 15:35:23 +01:00
|
|
|
return None
|
2007-02-25 23:53:10 +01:00
|
|
|
|
|
|
|
magic = "ssh-dss "
|
|
|
|
for l in lines:
|
|
|
|
l = l.strip()
|
|
|
|
if not l.startswith(magic):
|
|
|
|
continue
|
2008-01-09 15:35:23 +01:00
|
|
|
return l[len(magic):]
|
|
|
|
else:
|
2007-02-25 23:53:10 +01:00
|
|
|
logging.error("Error parsing public key.")
|
2008-01-09 15:35:23 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
def _get_pubkey(self):
|
|
|
|
# load on-demand.
|
|
|
|
if not self._pubkey:
|
|
|
|
self._pubkey = self._load_pubkey()
|
|
|
|
return self._pubkey
|
2007-02-25 23:53:10 +01:00
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
def _hash_private_key(self):
|
|
|
|
key_path = os.path.join(env.get_profile_path(), 'owner.key')
|
|
|
|
try:
|
|
|
|
f = open(key_path, "r")
|
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
|
|
|
except IOError, e:
|
|
|
|
logging.error("Error reading private key: %s" % e)
|
2008-01-09 15:35:23 +01:00
|
|
|
return None
|
2007-02-26 01:24:48 +01:00
|
|
|
|
|
|
|
key = ""
|
|
|
|
for l in lines:
|
|
|
|
l = l.strip()
|
|
|
|
if l.startswith("-----BEGIN DSA PRIVATE KEY-----"):
|
|
|
|
continue
|
|
|
|
if l.startswith("-----END DSA PRIVATE KEY-----"):
|
|
|
|
continue
|
|
|
|
key += l
|
|
|
|
if not len(key):
|
|
|
|
logging.error("Error parsing public key.")
|
2008-01-09 15:35:23 +01:00
|
|
|
return None
|
2007-02-26 01:24:48 +01:00
|
|
|
|
|
|
|
# hash it
|
|
|
|
key_hash = util._sha_data(key)
|
2008-01-09 15:35:23 +01:00
|
|
|
return util.printable_hash(key_hash)
|
|
|
|
|
|
|
|
def _get_privkey_hash(self):
|
|
|
|
# load on-demand.
|
|
|
|
if not self._privkey_hash:
|
|
|
|
self._privkey_hash = self._hash_private_key()
|
|
|
|
return self._privkey_hash
|
|
|
|
|
|
|
|
privkey_hash = property(_get_privkey_hash)
|
|
|
|
pubkey = property(_get_pubkey)
|
2007-02-25 23:53:10 +01:00
|
|
|
|
2007-09-17 13:53:10 +02:00
|
|
|
def get_profile():
|
|
|
|
global _profile
|
2007-07-26 21:25:13 +02:00
|
|
|
|
2007-09-17 13:53:10 +02:00
|
|
|
if not _profile:
|
|
|
|
path = os.path.join(env.get_profile_path(), 'config')
|
|
|
|
_profile = Profile(path)
|
2007-07-26 21:25:13 +02:00
|
|
|
|
2007-09-17 13:53:10 +02:00
|
|
|
return _profile
|
2007-07-30 15:34:02 +02:00
|
|
|
|
2007-09-17 13:53:10 +02:00
|
|
|
# Convenience methods for frequently used properties
|
2007-07-30 15:34:02 +02:00
|
|
|
|
2007-09-17 16:28:21 +02:00
|
|
|
def get_nick_name():
|
|
|
|
return get_profile().nick_name
|
2006-10-17 14:31:04 +02:00
|
|
|
|
|
|
|
def get_color():
|
2007-09-17 13:53:10 +02:00
|
|
|
return get_profile().color
|
2007-09-17 16:35:23 +02:00
|
|
|
|
|
|
|
def get_pubkey():
|
|
|
|
return get_profile().pubkey
|