40
33
urwid.curses_display.curses.A_UNDERLINE |= (
41
34
urwid.curses_display.curses.A_BLINK)
43
def isoformat_to_datetime(iso):
44
"Parse an ISO 8601 date string to a datetime.datetime()"
47
d, t = iso.split(u"T", 1)
48
year, month, day = d.split(u"-", 2)
49
hour, minute, second = t.split(u":", 2)
50
second, fraction = divmod(float(second), 1)
51
return datetime.datetime(int(year),
56
int(second), # Whole seconds
57
int(fraction*1000000)) # Microseconds
59
36
class MandosClientPropertyCache(object):
60
37
"""This wraps a Mandos Client D-Bus proxy object, caches the
61
38
properties and calls a hook function when any of them are
64
def __init__(self, proxy_object=None, *args, **kwargs):
41
def __init__(self, proxy_object=None, properties=None, *args,
65
43
self.proxy = proxy_object # Mandos Client proxy object
67
self.properties = dict()
45
if properties is None:
46
self.properties = dict()
48
self.properties = properties
68
49
self.proxy.connect_to_signal(u"PropertyChanged",
69
50
self.property_changed,
73
self.properties.update(
74
self.proxy.GetAll(client_interface,
75
dbus_interface = dbus.PROPERTIES_IFACE))
77
#XXX This break good super behaviour!
78
# super(MandosClientPropertyCache, self).__init__(
54
if properties is None:
55
self.properties.update(self.proxy.GetAll(client_interface,
57
dbus.PROPERTIES_IFACE))
58
super(MandosClientPropertyCache, self).__init__(
59
proxy_object=proxy_object,
60
properties=properties, *args, **kwargs)
81
62
def property_changed(self, property=None, value=None):
82
63
"""This is called whenever we get a PropertyChanged signal
126
104
client_interface,
127
105
byte_arrays=True)
128
self.proxy.connect_to_signal(u"NeedApproval",
132
106
self.proxy.connect_to_signal(u"Rejected",
134
108
client_interface,
135
109
byte_arrays=True)
136
last_checked_ok = isoformat_to_datetime(self.properties
138
if last_checked_ok is None:
139
self.last_checker_failed = True
141
self.last_checker_failed = ((datetime.datetime.utcnow()
145
self.properties["interval"]))
146
if self.last_checker_failed:
147
self._update_timer_callback_tag = (gobject.timeout_add
151
111
def checker_completed(self, exitstatus, condition, command):
152
112
if exitstatus == 0:
153
if self.last_checker_failed:
154
self.last_checker_failed = False
155
gobject.source_remove(self._update_timer_callback_tag)
156
self._update_timer_callback_tag = None
157
113
self.logger(u'Checker for client %s (command "%s")'
158
114
u' was successful'
159
115
% (self.properties[u"name"], command))
163
if not self.last_checker_failed:
164
self.last_checker_failed = True
165
self._update_timer_callback_tag = (gobject.timeout_add
168
117
if os.WIFEXITED(condition):
169
118
self.logger(u'Checker for client %s (command "%s")'
170
119
u' failed with exit code %s'
171
120
% (self.properties[u"name"], command,
172
121
os.WEXITSTATUS(condition)))
173
elif os.WIFSIGNALED(condition):
123
if os.WIFSIGNALED(condition):
174
124
self.logger(u'Checker for client %s (command "%s")'
175
125
u' was killed by signal %s'
176
126
% (self.properties[u"name"], command,
177
127
os.WTERMSIG(condition)))
178
elif os.WCOREDUMP(condition):
129
if os.WCOREDUMP(condition):
179
130
self.logger(u'Checker for client %s (command "%s")'
181
132
% (self.properties[u"name"], command))
183
self.logger(u'Checker for client %s completed mysteriously')
133
self.logger(u'Checker for client %s completed mysteriously')
186
135
def checker_started(self, command):
187
#self.logger(u'Client %s started checker "%s"'
188
# % (self.properties[u"name"], unicode(command)))
136
self.logger(u'Client %s started checker "%s"'
137
% (self.properties[u"name"], unicode(command)))
191
139
def got_secret(self):
192
self.last_checker_failed = False
193
140
self.logger(u'Client %s received its secret'
194
141
% self.properties[u"name"])
196
def need_approval(self, timeout, default):
198
message = u'Client %s needs approval within %s seconds'
200
message = u'Client %s will get its secret in %s seconds'
202
% (self.properties[u"name"], timeout/1000))
204
def rejected(self, reason):
205
self.logger(u'Client %s was rejected; reason: %s'
206
% (self.properties[u"name"], reason))
144
self.logger(u'Client %s was rejected'
145
% self.properties[u"name"])
208
147
def selectable(self):
209
148
"""Make this a "selectable" widget.
231
170
u"bold-underline-blink":
232
171
u"bold-underline-blink-standout",
235
174
# Rebuild focus and non-focus widgets using current properties
237
# Base part of a client. Name!
238
base = (u'%(name)s: '
239
% {u"name": self.properties[u"name"]})
240
if not self.properties[u"enabled"]:
241
message = u"DISABLED"
242
elif self.properties[u"approved_pending"]:
243
if self.properties[u"approved_by_default"]:
244
message = u"Connection established to client. (d)eny?"
246
message = u"Seeks approval to send secret. (a)pprove?"
247
elif self.last_checker_failed:
248
timeout = datetime.timedelta(milliseconds
249
= self.properties[u"timeout"])
250
last_ok = isoformat_to_datetime(
251
max((self.properties["last_checked_ok"]
252
or self.properties["created"]),
253
self.properties[u"last_enabled"]))
254
timer = timeout - (datetime.datetime.utcnow() - last_ok)
255
message = (u'A checker has failed! Time until client gets diabled: %s'
256
% unicode(timer).rsplit(".", 1)[0])
259
self._text = "%s%s" % (base, message)
175
self._text = (u'%(name)s: %(enabled)s'
176
% { u"name": self.properties[u"name"],
179
if self.properties[u"enabled"]
261
181
if not urwid.supports_unicode():
262
182
self._text = self._text.encode("ascii", "replace")
263
183
textlist = [(u"normal", self._text)]
295
207
def keypress(self, (maxcol,), key):
297
209
This overrides the method from urwid.FlowWidget"""
299
self.proxy.Enable(dbus_interface = client_interface)
301
self.proxy.Disable(dbus_interface = client_interface)
303
self.proxy.Approve(dbus.Boolean(True, variant_level=1),
304
dbus_interface = client_interface)
306
self.proxy.Approve(dbus.Boolean(False, variant_level=1),
307
dbus_interface = client_interface)
210
if key == u"e" or key == u"+":
212
elif key == u"d" or key == u"-":
308
214
elif key == u"r" or key == u"_" or key == u"ctrl k":
309
215
self.server_proxy_object.RemoveClient(self.proxy
311
217
elif key == u"s":
312
self.proxy.StartChecker(dbus_interface = client_interface)
218
self.proxy.StartChecker()
313
219
elif key == u"S":
314
self.proxy.StopChecker(dbus_interface = client_interface)
220
self.proxy.StopChecker()
315
221
elif key == u"C":
316
self.proxy.CheckedOK(dbus_interface = client_interface)
222
self.proxy.CheckedOK()
318
224
# elif key == u"p" or key == "=":
319
225
# self.proxy.pause()
523
425
self.remove_client(client, path)
525
def add_new_client(self, path):
427
def add_new_client(self, path, properties):
526
428
client_proxy_object = self.bus.get_object(self.busname, path)
527
429
self.add_client(MandosClientWidget(server_proxy_object
528
430
=self.mandos_serv,
530
432
=client_proxy_object,
433
properties=properties,
437
=self.remove_client),
539
440
def add_client(self, client, path=None):