summaryrefslogtreecommitdiffstats
path: root/src/Gui/ConfBackend.py
blob: 024a10330ab9acc03c6ff2a687df5daf384b00c1 (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
from abrt_utils import _

#FIXME: add some backend factory

try:
    import gnomekeyring as gkey
except ImportError, e:
    gkey = None

# Exceptions
class ConfBackendInitError(Exception):
    def __init__(self, msg):
        Exception.__init__(self)
        self.what = msg

    def __str__(self):
        return self.what

class ConfBackendSaveError(Exception):
    def __init__(self, msg):
        Exception.__init__(self)
        self.what = msg

    def __str__(self):
        return self.what


class ConfBackend(object):
    def __init__(self):
        pass

    def save(self, name, settings):
        """ Default save method has to be implemented in derived class """
        raise NotImplementedError

    def load(self, name):
        """ Default load method has to be implemented in derived class """
        raise NotImplementedError


# We use Gnome keyring in the following way:
# we store passwords for each plugin in a key named "abrt:<plugin_name>".
# The value of the key becomes the value of "Password" setting.
# Other settings (if plugin has them) are stored as attributes of this key.
#
# Example: Key "abrt:Bugzilla" with bugzilla password as value, and with attributes:
#
# AbrtPluginInfo: Bugzilla
# NoSSLVerify: yes
# Login: user@host.com
# BugzillaURL: https://host.with.bz.com/
#
# The attribute "AbrtPluginInfo" is special, it is used for retrieving
# the key via keyring API find_items_sync() function.


class ConfBackendGnomeKeyring(ConfBackend):
    def __init__(self):
        ConfBackend.__init__(self)
        if not gkey.is_available():
            raise ConfBackendInitError(_("Can't connect to Gnome Keyring daemon"))
        try:
            self.default_key_ring = gkey.get_default_keyring_sync()
        except:
            # could happen if keyring daemon is running, but we run gui under
            # user who is not the owner of the running session - using su
            raise ConfBackendInitError(_("Can't get default keyring"))

    def save(self, name, settings):
        settings_tmp = settings.copy()
        settings_tmp["AbrtPluginInfo"] = name
        password = ""

        item_list = []
        try:
            item_list = gkey.find_items_sync(gkey.ITEM_GENERIC_SECRET, {"AbrtPluginInfo":str(name)})
        except gkey.NoMatchError:
            # nothing found
            pass
        except gkey.DeniedError:
            raise ConfBackendSaveError(_("Access to gnome-keyring has been denied, plugins settings won't be saved."))

        # delete all items containg "AbrtPluginInfo":<plugin_name>, so we always have only 1 item per plugin
        for item in item_list:
            gkey.item_delete_sync(self.default_key_ring, item.item_id)

        if "Password" in settings_tmp:
            password = settings_tmp["Password"]
            del settings_tmp["Password"]
        try:
            gkey.item_create_sync(self.default_key_ring,
                                        gkey.ITEM_GENERIC_SECRET,
                                        "abrt:%s" % name,
                                        settings_tmp,
                                        password,
                                        True)
        except gkey.DeniedError, e:
            raise ConfBackendSaveError(_("Access to gnome-keyring has been denied, plugins settings won't be saved."))

    def load(self, name):
        item_list = None
        try:
            item_list = gkey.find_items_sync(gkey.ITEM_GENERIC_SECRET, {"AbrtPluginInfo":str(name)})
        except gkey.NoMatchError:
            # nothing found
            pass

        if item_list:
            retval = item_list[0].attributes.copy()
            retval["Password"] = item_list[0].secret
            return retval
        else:
            return {}
            #for i in item_list:
            #    for attr in i.attributes:
            #        print attr, i.attributes[attr]