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
69
50
self.property_changed,
73
54
self.properties.update(
74
55
self.proxy.GetAll(client_interface,
75
56
dbus_interface = dbus.PROPERTIES_IFACE))
77
#XXX This break good super behaviour!
78
# super(MandosClientPropertyCache, self).__init__(
57
super(MandosClientPropertyCache, self).__init__(
58
proxy_object=proxy_object, *args, **kwargs)
81
60
def property_changed(self, property=None, value=None):
82
61
"""This is called whenever we get a PropertyChanged signal
126
102
client_interface,
127
103
byte_arrays=True)
128
self.proxy.connect_to_signal(u"NeedApproval",
132
104
self.proxy.connect_to_signal(u"Rejected",
134
106
client_interface,
135
107
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()
147
if self.last_checker_failed:
148
self._update_timer_callback_tag = (gobject.timeout_add
152
109
def checker_completed(self, exitstatus, condition, command):
153
110
if exitstatus == 0:
154
if self.last_checker_failed:
155
self.last_checker_failed = False
156
gobject.source_remove(self._update_timer_callback_tag)
157
self._update_timer_callback_tag = None
158
111
self.logger(u'Checker for client %s (command "%s")'
159
112
u' was successful'
160
% (self.properties[u"Name"], command))
113
% (self.properties[u"name"], command))
164
if not self.last_checker_failed:
165
self.last_checker_failed = True
166
self._update_timer_callback_tag = (gobject.timeout_add
169
115
if os.WIFEXITED(condition):
170
116
self.logger(u'Checker for client %s (command "%s")'
171
117
u' failed with exit code %s'
172
% (self.properties[u"Name"], command,
118
% (self.properties[u"name"], command,
173
119
os.WEXITSTATUS(condition)))
174
elif os.WIFSIGNALED(condition):
121
if os.WIFSIGNALED(condition):
175
122
self.logger(u'Checker for client %s (command "%s")'
176
123
u' was killed by signal %s'
177
% (self.properties[u"Name"], command,
124
% (self.properties[u"name"], command,
178
125
os.WTERMSIG(condition)))
179
elif os.WCOREDUMP(condition):
127
if os.WCOREDUMP(condition):
180
128
self.logger(u'Checker for client %s (command "%s")'
182
% (self.properties[u"Name"], command))
184
self.logger(u'Checker for client %s completed'
130
% (self.properties[u"name"], command))
131
self.logger(u'Checker for client %s completed mysteriously')
188
133
def checker_started(self, command):
189
#self.logger(u'Client %s started checker "%s"'
190
# % (self.properties[u"Name"], unicode(command)))
134
self.logger(u'Client %s started checker "%s"'
135
% (self.properties[u"name"], unicode(command)))
193
137
def got_secret(self):
194
self.last_checker_failed = False
195
138
self.logger(u'Client %s received its secret'
196
% self.properties[u"Name"])
198
def need_approval(self, timeout, default):
200
message = u'Client %s needs approval within %s seconds'
202
message = u'Client %s will get its secret in %s seconds'
204
% (self.properties[u"Name"], timeout/1000))
206
def rejected(self, reason):
207
self.logger(u'Client %s was rejected; reason: %s'
208
% (self.properties[u"Name"], reason))
139
% self.properties[u"name"])
142
self.logger(u'Client %s was rejected'
143
% self.properties[u"name"])
210
145
def selectable(self):
211
146
"""Make this a "selectable" widget.
233
168
u"bold-underline-blink":
234
169
u"bold-underline-blink-standout",
237
172
# Rebuild focus and non-focus widgets using current properties
239
# Base part of a client. Name!
240
base = (u'%(name)s: '
241
% {u"name": self.properties[u"Name"]})
242
if not self.properties[u"Enabled"]:
243
message = u"DISABLED"
244
elif self.properties[u"ApprovalPending"]:
245
if self.properties[u"ApprovedByDefault"]:
246
message = u"Connection established to client. (d)eny?"
248
message = u"Seeks approval to send secret. (a)pprove?"
249
elif self.last_checker_failed:
250
timeout = datetime.timedelta(milliseconds
253
last_ok = isoformat_to_datetime(
254
max((self.properties[u"LastCheckedOK"]
255
or self.properties[u"Created"]),
256
self.properties[u"LastEnabled"]))
257
timer = timeout - (datetime.datetime.utcnow() - last_ok)
258
message = (u'A checker has failed! Time until client'
260
% unicode(timer).rsplit(".", 1)[0])
263
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"]
265
179
if not urwid.supports_unicode():
266
180
self._text = self._text.encode("ascii", "replace")
267
181
textlist = [(u"normal", self._text)]
299
205
def keypress(self, (maxcol,), key):
301
207
This overrides the method from urwid.FlowWidget"""
303
self.proxy.Enable(dbus_interface = client_interface)
305
self.proxy.Disable(dbus_interface = client_interface)
307
self.proxy.Approve(dbus.Boolean(True, variant_level=1),
308
dbus_interface = client_interface)
310
self.proxy.Approve(dbus.Boolean(False, variant_level=1),
311
dbus_interface = client_interface)
208
if key == u"e" or key == u"+":
210
elif key == u"d" or key == u"-":
312
212
elif key == u"r" or key == u"_" or key == u"ctrl k":
313
213
self.server_proxy_object.RemoveClient(self.proxy
315
215
elif key == u"s":
316
self.proxy.StartChecker(dbus_interface = client_interface)
216
self.proxy.StartChecker()
317
217
elif key == u"S":
318
self.proxy.StopChecker(dbus_interface = client_interface)
218
self.proxy.StopChecker()
319
219
elif key == u"C":
320
self.proxy.CheckedOK(dbus_interface = client_interface)
220
self.proxy.CheckedOK()
322
222
# elif key == u"p" or key == "=":
323
223
# self.proxy.pause()