40
37
urwid.curses_display.curses.A_UNDERLINE |= (
41
38
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
40
class MandosClientPropertyCache(object):
60
41
"""This wraps a Mandos Client D-Bus proxy object, caches the
61
42
properties and calls a hook function when any of them are
146
102
client_interface,
147
103
byte_arrays=True)
148
self.proxy.connect_to_signal(u"NeedApproval",
152
104
self.proxy.connect_to_signal(u"Rejected",
154
106
client_interface,
155
107
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
180
109
def checker_completed(self, exitstatus, condition, command):
181
110
if exitstatus == 0:
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))
111
self.logger(u'Checker for client %s (command "%s")'
113
% (self.properties[u"name"], command))
191
if not self.last_checker_failed:
192
self.last_checker_failed = True
193
self.using_timer(True)
194
115
if os.WIFEXITED(condition):
195
116
self.logger(u'Checker for client %s (command "%s")'
196
117
u' failed with exit code %s'
197
% (self.properties[u"Name"], command,
118
% (self.properties[u"name"], command,
198
119
os.WEXITSTATUS(condition)))
199
elif os.WIFSIGNALED(condition):
121
if os.WIFSIGNALED(condition):
200
122
self.logger(u'Checker for client %s (command "%s")'
201
123
u' was killed by signal %s'
202
% (self.properties[u"Name"], command,
124
% (self.properties[u"name"], command,
203
125
os.WTERMSIG(condition)))
204
elif os.WCOREDUMP(condition):
127
if os.WCOREDUMP(condition):
205
128
self.logger(u'Checker for client %s (command "%s")'
207
% (self.properties[u"Name"], command))
209
self.logger(u'Checker for client %s completed'
130
% (self.properties[u"name"], command))
131
self.logger(u'Checker for client %s completed mysteriously')
213
133
def checker_started(self, command):
214
#self.logger(u'Client %s started checker "%s"'
215
# % (self.properties[u"Name"], unicode(command)))
134
self.logger(u'Client %s started checker "%s"'
135
% (self.properties[u"name"], unicode(command)))
218
137
def got_secret(self):
219
self.last_checker_failed = False
220
138
self.logger(u'Client %s received its secret'
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))
139
% self.properties[u"name"])
142
self.logger(u'Client %s was rejected'
143
% self.properties[u"name"])
236
145
def selectable(self):
237
146
"""Make this a "selectable" widget.
259
168
u"bold-underline-blink":
260
169
u"bold-underline-blink-standout",
263
172
# Rebuild focus and non-focus widgets using current properties
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)
173
self._text = (u'%(name)s: %(enabled)s'
174
% { u"name": self.properties[u"name"],
177
if self.properties[u"enabled"]
302
179
if not urwid.supports_unicode():
303
180
self._text = self._text.encode("ascii", "replace")
304
181
textlist = [(u"normal", self._text)]
336
205
def keypress(self, (maxcol,), key):
338
207
This overrides the method from urwid.FlowWidget"""
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)
208
if key == u"e" or key == u"+":
210
elif key == u"d" or key == u"-":
349
212
elif key == u"r" or key == u"_" or key == u"ctrl k":
350
213
self.server_proxy_object.RemoveClient(self.proxy
352
215
elif key == u"s":
353
self.proxy.StartChecker(dbus_interface = client_interface)
216
self.proxy.StartChecker()
354
217
elif key == u"S":
355
self.proxy.StopChecker(dbus_interface = client_interface)
218
self.proxy.StopChecker()
356
219
elif key == u"C":
357
self.proxy.CheckedOK(dbus_interface = client_interface)
220
self.proxy.CheckedOK()
359
222
# elif key == u"p" or key == "=":
360
223
# self.proxy.pause()