~tdelaet/cimple/old-codebase

« back to all changes in this revision

Viewing changes to src/cimple/core/discovery/trust.py

  • Committer: Thomas Delaet
  • Date: 2010-01-07 14:14:32 UTC
  • Revision ID: thomas@cole-20100107141432-yhker27v3pmn62uo
first phase of rewrite with focus on storage

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import avahi
2
 
import cimple.core.devices.discovery
3
 
import cimple.data
4
 
import RDF
5
 
import os
6
 
import functools
7
 
import notify.all
8
 
 
9
 
class DeviceManager:
10
 
 
11
 
        def __init__(self):
12
 
                self.trusted_accounts = TrustManager()
13
 
                self.discovery = cimple.core.devices.discovery.DeviceDiscovery()
14
 
                self.discovery.connect("service_added", self.__service_added)
15
 
                self.discovery.connect("service_removed", self.__service_removed)
16
 
                self.account_online = notify.all.Signal()
17
 
                self.account_offline = notify.all.Signal()
18
 
                self.device_online = notify.all.Signal()
19
 
                self.device_offline = notify.all.Signal()
20
 
 
21
 
        def __same_user(self, dictionary):
22
 
                full_name = cimple.core.config.name()
23
 
                login = cimple.core.config.login_name()
24
 
                return (full_name == dictionary['name'] or login == dictionary['name'] or login == dictionary['login'])
25
 
 
26
 
        def __service_added(self, discovery, service):
27
 
                dictionary = {}
28
 
                for txt in service.txt:
29
 
                        txt = avahi.byte_array_to_string(txt)
30
 
                        splitted = txt.split("=", 1)
31
 
                        dictionary[splitted[0]] = splitted[1]
32
 
                if dictionary.has_key('type'):
33
 
                        obj = None
34
 
                        uri = "urn:cimple:" + str(service.name)
35
 
                        if dictionary['type'] == 'user' and self.__same_user(dictionary):
36
 
                                obj = cimple.data.DeviceAccount.create(uri)
37
 
                        elif dictionary['type'] == 'device':
38
 
                                obj = cimple.data.Device.create(uri)
39
 
                        if type(obj) != type(None):
40
 
                                obj.name = str(dictionary['name'])
41
 
                                key_part = 0
42
 
                                key = ""
43
 
                                while dictionary.has_key("ssh-key-part-" + str(key_part)):
44
 
                                        key = key + dictionary['ssh-key-part-' + str(key_part)]
45
 
                                        key_part = key_part + 1
46
 
                                if key != "":
47
 
                                        obj.ssh_key_type = dictionary['ssh-key-type']
48
 
                                        obj.ssh_key = key
49
 
                                if dictionary['type'] == 'user':
50
 
                                        obj.login = dictionary['login']
51
 
                                        if dictionary.has_key('key'):
52
 
                                                obj.key = dictionary['key']
53
 
                                                obj.key_type = dictionary['key-type']
54
 
                                        obj.device = "urn:cimple:" + dictionary['device']
55
 
                                obj.commit()
56
 
                                if dictionary['type'] == 'user' and service.aprotocol == avahi.PROTO_INET:
57
 
                                        self.account_online(obj, service.address, service.port)
58
 
                                if dictionary['type'] == 'device' and service.aprotocol == avahi.PROTO_INET:
59
 
                                        self.device_online(obj, service.address, service.port)
60
 
        
61
 
        def __service_removed(self, discovery, service):
62
 
                pass
63
 
 
64
 
class TrustManager:
65
 
        """
66
 
        Trust Manager
67
 
        """
68
 
 
69
 
        def __init__(self):
70
 
                self.devices = {}
71
 
                self.accounts = {}
72
 
                cimple.data.Device.subscribe_to_new_objects(self.__new_device)
73
 
                cimple.data.DeviceAccount.subscribe_to_new_objects(self.__new_account)
74
 
                self.__load_devices()
75
 
                self.__load_accounts()
76
 
                
77
 
        def __load_devices(self):
78
 
                for device in cimple.data.Device.ClassInstances():
79
 
                        self.__new_device(device)
80
 
        
81
 
        def __load_accounts(self):
82
 
                for account in cimple.data.DeviceAccount.ClassInstances():
83
 
                        self.__new_account(account)
84
 
        
85
 
        def __new_device(self, device):
86
 
                if not self.devices.has_key(str(device.resUri.uri)):
87
 
                        self.devices[str(device.resUri.uri)] = device
88
 
                        device.subscribe_to_updates(functools.partial(self.__device_update, device))
89
 
                        self.__device_update(device, "")
90
 
                        
91
 
        def __new_account(self, account):
92
 
                if not self.accounts.has_key(str(account.resUri.uri)):
93
 
                        self.accounts[str(account.resUri.uri)] = account
94
 
                        account.subscribe_to_updates(functools.partial(self.__account_update, account))
95
 
                        self.__account_update(account, "")
96
 
 
97
 
        def __account_update(self, account, attribute):
98
 
                if account.trusted != None:
99
 
                        if account.trusted == 'True':
100
 
                                if not self.__in_authorized_keys(account):
101
 
                                        self.__add_to_authorized_keys(account)
102
 
                        if account.trusted == 'False':
103
 
                                if self.__in_authorized_keys(account):
104
 
                                        self.__remove_from_authorized_keys(account)
105
 
                if account.device != None and self.devices.has_key(str(account.device.resUri.uri)):
106
 
                        device = self.devices[str(account.device.resUri.uri)]
107
 
                        if account.trusted == None and device.trusted != None:
108
 
                                account.trusted = device.trusted
109
 
                        elif account.trusted != None and device.trusted == None:
110
 
                                device.trusted = account.trusted
111
 
                        elif account.trusted != None and device.trusted != None:
112
 
                                if attribute == 'trusted' or attribute == '':
113
 
                                        device.trusted = account.trusted
114
 
 
115
 
        def __device_update(self, device, attribute):
116
 
                #FIXME: manage known_hosts file
117
 
                account = None
118
 
                for entry in self.accounts.values():
119
 
                        if entry.device != None and str(entry.device.resUri.uri) == str(device.resUri.uri):
120
 
                                account = entry
121
 
                if account != None:
122
 
                        if device.trusted != None and account.trusted == None:
123
 
                                account.trusted = device.trusted
124
 
                        elif device.trusted == None and account.trusted != None:
125
 
                                device.trusted = account.trusted
126
 
                        elif device.trusted != None and account.trusted != None:
127
 
                                if attribute == 'trusted' or attribute == '':
128
 
                                        account.trusted = device.trusted
129
 
 
130
 
        def __in_authorized_keys(self, account):
131
 
                lines = self.__read_authorized_keys()
132
 
                for line in lines:
133
 
                        key = line.split(" ")[1]
134
 
                        if key == str(account.ssh_key):
135
 
                                return True
136
 
                return False
137
 
        
138
 
        def __add_to_authorized_keys(self, account):
139
 
                line = " ".join([account.ssh_key_type, account.ssh_key, str(account.resUri.uri) + "\n"])
140
 
                handle = open(self.__authorized_keys_location(), 'a')
141
 
                handle.write(line)
142
 
                handle.close()
143
 
        
144
 
        def __remove_from_authorized_keys(self, account):
145
 
                lines = self.__read_authorized_keys()
146
 
                new_lines = []
147
 
                for line in lines:
148
 
                        key = line.split(" ")[1]
149
 
                        if key != str(account.ssh_key):
150
 
                                new_lines.append(line)
151
 
                handle = open(self.__authorized_keys_location(), 'w')
152
 
                handle.write(lines)
153
 
                handle.close()          
154
 
        
155
 
        def __read_authorized_keys(self):
156
 
                #pylint: disable-msg=C0111
157
 
                handle = open(self.__authorized_keys_location(), 'r')
158
 
                result = handle.readlines()
159
 
                handle.close()
160
 
                return result
161
 
        
162
 
        def __authorized_keys_location(self):
163
 
                #pylint: disable-msg=C0111,R0201
164
 
                return os.path.join(os.environ['HOME'], '.ssh', 'authorized_keys')
 
 
b'\\ No newline at end of file'