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
115
154
client_interface,
116
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
118
180
def checker_completed(self, exitstatus, condition, command):
119
181
if exitstatus == 0:
182
if self.last_checker_failed:
183
self.last_checker_failed = False
184
self.using_timer(False)
120
185
#self.logger(u'Checker for client %s (command "%s")'
121
186
# u' was successful'
122
# % (self.properties[u"name"], command))
187
# % (self.properties[u"Name"], command))
191
if not self.last_checker_failed:
192
self.last_checker_failed = True
193
self.using_timer(True)
124
194
if os.WIFEXITED(condition):
125
195
self.logger(u'Checker for client %s (command "%s")'
126
196
u' failed with exit code %s'
127
% (self.properties[u"name"], command,
197
% (self.properties[u"Name"], command,
128
198
os.WEXITSTATUS(condition)))
130
if os.WIFSIGNALED(condition):
199
elif os.WIFSIGNALED(condition):
131
200
self.logger(u'Checker for client %s (command "%s")'
132
201
u' was killed by signal %s'
133
% (self.properties[u"name"], command,
202
% (self.properties[u"Name"], command,
134
203
os.WTERMSIG(condition)))
136
if os.WCOREDUMP(condition):
204
elif os.WCOREDUMP(condition):
137
205
self.logger(u'Checker for client %s (command "%s")'
139
% (self.properties[u"name"], command))
140
self.logger(u'Checker for client %s completed mysteriously')
207
% (self.properties[u"Name"], command))
209
self.logger(u'Checker for client %s completed'
142
213
def checker_started(self, command):
143
214
#self.logger(u'Client %s started checker "%s"'
144
# % (self.properties[u"name"], unicode(command)))
215
# % (self.properties[u"Name"], unicode(command)))
147
218
def got_secret(self):
219
self.last_checker_failed = False
148
220
self.logger(u'Client %s received its secret'
149
% self.properties[u"name"])
221
% self.properties[u"Name"])
151
223
def need_approval(self, timeout, default):
155
227
message = u'Client %s will get its secret in %s seconds'
156
228
self.logger(message
157
% (self.properties[u"name"], timeout/1000))
229
% (self.properties[u"Name"], timeout/1000))
230
self.using_timer(True)
159
232
def rejected(self, reason):
160
233
self.logger(u'Client %s was rejected; reason: %s'
161
% (self.properties[u"name"], reason))
234
% (self.properties[u"Name"], reason))
163
236
def selectable(self):
164
237
"""Make this a "selectable" widget.
190
263
# Rebuild focus and non-focus widgets using current properties
192
265
# Base part of a client. Name!
193
self._text = (u'%(name)s: '
194
% {u"name": self.properties[u"name"]})
196
if self.properties[u"approved_pending"]:
197
if self.properties[u"approved_by_default"]:
198
self._text += u"Connection established to client. (d)eny?"
200
self._text += u"Seeks approval to send secret. (a)pprove?"
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])
202
self._text += (u'%(enabled)s'
205
if self.properties[u"enabled"]
300
self._text = "%s%s" % (base, message)
207
302
if not urwid.supports_unicode():
208
303
self._text = self._text.encode("ascii", "replace")
209
304
textlist = [(u"normal", self._text)]
284
383
use them as an excuse to shift focus away from this widget.
286
385
def keypress(self, (maxcol, maxrow), key):
287
ret = super(ConstrainedListBox, self).keypress((maxcol, maxrow), key)
386
ret = super(ConstrainedListBox, self).keypress((maxcol,
288
388
if ret in (u"up", u"down"):