33
40
urwid.curses_display.curses.A_UNDERLINE |= (
34
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
36
59
class MandosClientPropertyCache(object):
37
60
"""This wraps a Mandos Client D-Bus proxy object, caches the
38
61
properties and calls a hook function when any of them are
41
def __init__(self, proxy_object=None, properties=None, *args,
64
def __init__(self, proxy_object=None, *args, **kwargs):
43
65
self.proxy = proxy_object # Mandos Client proxy object
45
if properties is None:
46
self.properties = dict()
48
self.properties = properties
67
self.properties = dict()
49
68
self.proxy.connect_to_signal(u"PropertyChanged",
50
69
self.property_changed,
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)
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__(
62
81
def property_changed(self, property=None, value=None):
63
82
"""This is called whenever we get a PropertyChanged signal
104
146
client_interface,
105
147
byte_arrays=True)
148
self.proxy.connect_to_signal(u"NeedApproval",
106
152
self.proxy.connect_to_signal(u"Rejected",
108
154
client_interface,
109
155
byte_arrays=True)
157
def property_changed(self, property=None, value=None):
158
super(self, MandosClientWidget).property_changed(property,
160
if property == u"ApprovalPending":
161
using_timer(bool(value))
163
def using_timer(self, flag):
164
"""Call this method with True or False when timer should be
165
activated or deactivated.
167
old = self._update_timer_callback_lock
169
self._update_timer_callback_lock += 1
171
self._update_timer_callback_lock -= 1
172
if old == 0 and self._update_timer_callback_lock:
173
self._update_timer_callback_tag = (gobject.timeout_add
176
elif old and self._update_timer_callback_lock == 0:
177
gobject.source_remove(self._update_timer_callback_tag)
178
self._update_timer_callback_tag = None
111
180
def checker_completed(self, exitstatus, condition, command):
112
181
if exitstatus == 0:
113
self.logger(u'Checker for client %s (command "%s")'
115
% (self.properties[u"name"], command))
182
if self.last_checker_failed:
183
self.last_checker_failed = False
184
self.using_timer(False)
185
#self.logger(u'Checker for client %s (command "%s")'
187
# % (self.properties[u"Name"], command))
191
if not self.last_checker_failed:
192
self.last_checker_failed = True
193
self.using_timer(True)
117
194
if os.WIFEXITED(condition):
118
195
self.logger(u'Checker for client %s (command "%s")'
119
196
u' failed with exit code %s'
120
% (self.properties[u"name"], command,
197
% (self.properties[u"Name"], command,
121
198
os.WEXITSTATUS(condition)))
123
if os.WIFSIGNALED(condition):
199
elif os.WIFSIGNALED(condition):
124
200
self.logger(u'Checker for client %s (command "%s")'
125
201
u' was killed by signal %s'
126
% (self.properties[u"name"], command,
202
% (self.properties[u"Name"], command,
127
203
os.WTERMSIG(condition)))
129
if os.WCOREDUMP(condition):
204
elif os.WCOREDUMP(condition):
130
205
self.logger(u'Checker for client %s (command "%s")'
132
% (self.properties[u"name"], command))
133
self.logger(u'Checker for client %s completed mysteriously')
207
% (self.properties[u"Name"], command))
209
self.logger(u'Checker for client %s completed'
135
213
def checker_started(self, command):
136
self.logger(u'Client %s started checker "%s"'
137
% (self.properties[u"name"], unicode(command)))
214
#self.logger(u'Client %s started checker "%s"'
215
# % (self.properties[u"Name"], unicode(command)))
139
218
def got_secret(self):
219
self.last_checker_failed = False
140
220
self.logger(u'Client %s received its secret'
141
% self.properties[u"name"])
144
self.logger(u'Client %s was rejected'
145
% self.properties[u"name"])
221
% self.properties[u"Name"])
223
def need_approval(self, timeout, default):
225
message = u'Client %s needs approval within %s seconds'
227
message = u'Client %s will get its secret in %s seconds'
229
% (self.properties[u"Name"], timeout/1000))
230
self.using_timer(True)
232
def rejected(self, reason):
233
self.logger(u'Client %s was rejected; reason: %s'
234
% (self.properties[u"Name"], reason))
147
236
def selectable(self):
148
237
"""Make this a "selectable" widget.
170
259
u"bold-underline-blink":
171
260
u"bold-underline-blink-standout",
174
263
# Rebuild focus and non-focus widgets using current properties
175
self._text = (u'%(name)s: %(enabled)s'
176
% { u"name": self.properties[u"name"],
179
if self.properties[u"enabled"]
265
# Base part of a client. Name!
266
base = (u'%(name)s: '
267
% {u"name": self.properties[u"Name"]})
268
if not self.properties[u"Enabled"]:
269
message = u"DISABLED"
270
elif self.properties[u"ApprovalPending"]:
271
timeout = datetime.timedelta(milliseconds
274
last_approval_request = isoformat_to_datetime(
275
self.properties[u"LastApprovalRequest"])
276
if last_approval_request is not None:
277
timer = timeout - (datetime.datetime.utcnow()
278
- last_approval_request)
280
timer = datetime.timedelta()
281
if self.properties[u"ApprovedByDefault"]:
282
message = u"Approval in %s. (d)eny?"
284
message = u"Denial in %s. (a)pprove?"
285
message = message % unicode(timer).rsplit(".", 1)[0]
286
elif self.last_checker_failed:
287
timeout = datetime.timedelta(milliseconds
290
last_ok = isoformat_to_datetime(
291
max((self.properties[u"LastCheckedOK"]
292
or self.properties[u"Created"]),
293
self.properties[u"LastEnabled"]))
294
timer = timeout - (datetime.datetime.utcnow() - last_ok)
295
message = (u'A checker has failed! Time until client'
296
u' gets disabled: %s'
297
% unicode(timer).rsplit(".", 1)[0])
300
self._text = "%s%s" % (base, message)
181
302
if not urwid.supports_unicode():
182
303
self._text = self._text.encode("ascii", "replace")
183
304
textlist = [(u"normal", self._text)]
207
336
def keypress(self, (maxcol,), key):
209
338
This overrides the method from urwid.FlowWidget"""
210
if key == u"e" or key == u"+":
212
elif key == u"d" or key == u"-":
340
self.proxy.Enable(dbus_interface = client_interface)
342
self.proxy.Disable(dbus_interface = client_interface)
344
self.proxy.Approve(dbus.Boolean(True, variant_level=1),
345
dbus_interface = client_interface)
347
self.proxy.Approve(dbus.Boolean(False, variant_level=1),
348
dbus_interface = client_interface)
214
349
elif key == u"r" or key == u"_" or key == u"ctrl k":
215
350
self.server_proxy_object.RemoveClient(self.proxy
217
352
elif key == u"s":
218
self.proxy.StartChecker()
353
self.proxy.StartChecker(dbus_interface = client_interface)
219
354
elif key == u"S":
220
self.proxy.StopChecker()
355
self.proxy.StopChecker(dbus_interface = client_interface)
221
356
elif key == u"C":
222
self.proxy.CheckedOK()
357
self.proxy.CheckedOK(dbus_interface = client_interface)
224
359
# elif key == u"p" or key == "=":
225
360
# self.proxy.pause()