40
40
urwid.curses_display.curses.A_UNDERLINE |= (
41
41
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
43
59
class MandosClientPropertyCache(object):
44
60
"""This wraps a Mandos Client D-Bus proxy object, caches the
45
61
properties and calls a hook function when any of them are
57
73
self.properties.update(
58
74
self.proxy.GetAll(client_interface,
59
75
dbus_interface = dbus.PROPERTIES_IFACE))
60
super(MandosClientPropertyCache, self).__init__(
61
proxy_object=proxy_object, *args, **kwargs)
77
#XXX This break good super behaviour!
78
# super(MandosClientPropertyCache, self).__init__(
63
81
def property_changed(self, property=None, value=None):
64
82
"""This is called whenever we get a PropertyChanged signal
113
134
client_interface,
114
135
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
116
151
def checker_completed(self, exitstatus, condition, command):
117
152
if exitstatus == 0:
118
#self.logger(u'Checker for client %s (command "%s")'
120
# % (self.properties[u"name"], command))
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
self.logger(u'Checker for client %s (command "%s")'
159
% (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
122
168
if os.WIFEXITED(condition):
123
169
self.logger(u'Checker for client %s (command "%s")'
124
170
u' failed with exit code %s'
125
171
% (self.properties[u"name"], command,
126
172
os.WEXITSTATUS(condition)))
128
if os.WIFSIGNALED(condition):
173
elif os.WIFSIGNALED(condition):
129
174
self.logger(u'Checker for client %s (command "%s")'
130
175
u' was killed by signal %s'
131
176
% (self.properties[u"name"], command,
132
177
os.WTERMSIG(condition)))
134
if os.WCOREDUMP(condition):
178
elif os.WCOREDUMP(condition):
135
179
self.logger(u'Checker for client %s (command "%s")'
137
181
% (self.properties[u"name"], command))
138
self.logger(u'Checker for client %s completed mysteriously')
183
self.logger(u'Checker for client %s completed mysteriously')
140
186
def checker_started(self, command):
141
187
#self.logger(u'Client %s started checker "%s"'
184
231
u"bold-underline-blink":
185
232
u"bold-underline-blink-standout",
188
235
# Rebuild focus and non-focus widgets using current properties
189
self._text = (u'%(name)s: %(enabled)s'
190
% { u"name": self.properties[u"name"],
193
if self.properties[u"enabled"]
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)
195
261
if not urwid.supports_unicode():
196
262
self._text = self._text.encode("ascii", "replace")
197
263
textlist = [(u"normal", self._text)]
208
274
if self.update_hook is not None:
209
275
self.update_hook()
277
def update_timer(self):
280
return True # Keep calling this
211
282
def delete(self):
283
if self._update_timer_callback_tag is not None:
284
gobject.source_remove(self._update_timer_callback_tag)
285
self._update_timer_callback_tag = None
212
286
if self.delete_hook is not None:
213
287
self.delete_hook(self)
221
295
def keypress(self, (maxcol,), key):
223
297
This overrides the method from urwid.FlowWidget"""
224
if key == u"e" or key == u"+":
226
elif key == u"d" or key == u"-":
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)
228
308
elif key == u"r" or key == u"_" or key == u"ctrl k":
229
309
self.server_proxy_object.RemoveClient(self.proxy
231
311
elif key == u"s":
232
self.proxy.StartChecker()
312
self.proxy.StartChecker(dbus_interface = client_interface)
233
313
elif key == u"S":
234
self.proxy.StopChecker()
314
self.proxy.StopChecker(dbus_interface = client_interface)
235
315
elif key == u"C":
236
self.proxy.CheckedOK()
316
self.proxy.CheckedOK(dbus_interface = client_interface)
238
318
# elif key == u"p" or key == "=":
239
319
# self.proxy.pause()