2
import cimple.core.devices.discovery
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()
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'])
26
def __service_added(self, discovery, service):
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'):
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'])
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
47
obj.ssh_key_type = dictionary['ssh-key-type']
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']
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)
61
def __service_removed(self, discovery, service):
72
cimple.data.Device.subscribe_to_new_objects(self.__new_device)
73
cimple.data.DeviceAccount.subscribe_to_new_objects(self.__new_account)
75
self.__load_accounts()
77
def __load_devices(self):
78
for device in cimple.data.Device.ClassInstances():
79
self.__new_device(device)
81
def __load_accounts(self):
82
for account in cimple.data.DeviceAccount.ClassInstances():
83
self.__new_account(account)
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, "")
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, "")
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
115
def __device_update(self, device, attribute):
116
#FIXME: manage known_hosts file
118
for entry in self.accounts.values():
119
if entry.device != None and str(entry.device.resUri.uri) == str(device.resUri.uri):
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
130
def __in_authorized_keys(self, account):
131
lines = self.__read_authorized_keys()
133
key = line.split(" ")[1]
134
if key == str(account.ssh_key):
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')
144
def __remove_from_authorized_keys(self, account):
145
lines = self.__read_authorized_keys()
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')
155
def __read_authorized_keys(self):
156
#pylint: disable-msg=C0111
157
handle = open(self.__authorized_keys_location(), 'r')
158
result = handle.readlines()
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'