37
40
urwid.curses_display.curses.A_UNDERLINE |= (
38
41
urwid.curses_display.curses.A_BLINK)
40
def isoformat_to_datetime(iso):
41
"Parse an ISO 8601 date string to a datetime.datetime()"
44
d, t = iso.split(u"T", 1)
45
year, month, day = d.split(u"-", 2)
46
hour, minute, second = t.split(u":", 2)
47
second, fraction = divmod(float(second), 1)
48
return datetime.datetime(int(year),
53
int(second), # Whole seconds
54
int(fraction*1000000)) # Microseconds
56
43
class MandosClientPropertyCache(object):
57
44
"""This wraps a Mandos Client D-Bus proxy object, caches the
58
45
properties and calls a hook function when any of them are
70
57
self.properties.update(
71
58
self.proxy.GetAll(client_interface,
72
59
dbus_interface = dbus.PROPERTIES_IFACE))
73
super(MandosClientPropertyCache, self).__init__(
74
proxy_object=proxy_object, *args, **kwargs)
61
#XXX This break good super behaviour!
62
# super(MandosClientPropertyCache, self).__init__(
76
65
def property_changed(self, property=None, value=None):
77
66
"""This is called whenever we get a PropertyChanged signal
121
107
client_interface,
122
108
byte_arrays=True)
109
self.proxy.connect_to_signal(u"NeedApproval",
123
113
self.proxy.connect_to_signal(u"Rejected",
125
115
client_interface,
126
116
byte_arrays=True)
127
last_checked_ok = isoformat_to_datetime(self.properties
129
if last_checked_ok is None:
130
self.last_checker_failed = True
132
self.last_checker_failed = ((datetime.datetime.utcnow()
136
self.properties["interval"]))
137
if self.last_checker_failed:
138
self._update_timer_callback_tag = (gobject.timeout_add
142
118
def checker_completed(self, exitstatus, condition, command):
143
119
if exitstatus == 0:
144
if self.last_checker_failed:
145
self.last_checker_failed = False
146
gobject.source_remove(self._update_timer_callback_tag)
147
self._update_timer_callback_tag = None
148
self.logger(u'Checker for client %s (command "%s")'
150
% (self.properties[u"name"], command))
120
#self.logger(u'Checker for client %s (command "%s")'
122
# % (self.properties[u"name"], command))
154
if not self.last_checker_failed:
155
self.last_checker_failed = True
156
self._update_timer_callback_tag = (gobject.timeout_add
159
124
if os.WIFEXITED(condition):
160
125
self.logger(u'Checker for client %s (command "%s")'
161
126
u' failed with exit code %s'
162
127
% (self.properties[u"name"], command,
163
128
os.WEXITSTATUS(condition)))
164
elif os.WIFSIGNALED(condition):
130
if os.WIFSIGNALED(condition):
165
131
self.logger(u'Checker for client %s (command "%s")'
166
132
u' was killed by signal %s'
167
133
% (self.properties[u"name"], command,
168
134
os.WTERMSIG(condition)))
169
elif os.WCOREDUMP(condition):
136
if os.WCOREDUMP(condition):
170
137
self.logger(u'Checker for client %s (command "%s")'
172
139
% (self.properties[u"name"], command))
174
self.logger(u'Checker for client %s completed mysteriously')
140
self.logger(u'Checker for client %s completed mysteriously')
177
142
def checker_started(self, command):
178
self.logger(u'Client %s started checker "%s"'
179
% (self.properties[u"name"], unicode(command)))
143
#self.logger(u'Client %s started checker "%s"'
144
# % (self.properties[u"name"], unicode(command)))
181
147
def got_secret(self):
182
148
self.logger(u'Client %s received its secret'
183
149
% self.properties[u"name"])
186
self.logger(u'Client %s was rejected'
187
% self.properties[u"name"])
151
def need_approval(self, timeout, default):
153
message = u'Client %s needs approval within %s seconds'
155
message = u'Client %s will get its secret in %s seconds'
157
% (self.properties[u"name"], timeout/1000))
159
def rejected(self, reason):
160
self.logger(u'Client %s was rejected; reason: %s'
161
% (self.properties[u"name"], reason))
189
163
def selectable(self):
190
164
"""Make this a "selectable" widget.
212
186
u"bold-underline-blink":
213
187
u"bold-underline-blink-standout",
216
190
# Rebuild focus and non-focus widgets using current properties
217
self._text = (u'%(name)s: %(enabled)s%(timer)s'
218
% { u"name": self.properties[u"name"],
221
if self.properties[u"enabled"]
223
u"timer": (unicode(datetime.timedelta
229
- isoformat_to_datetime
230
(max((self.properties
235
self.properties[u"last_enabled"]))))
236
if (self.last_checker_failed
192
# 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?"
202
self._text += (u'%(enabled)s'
205
if self.properties[u"enabled"]
240
207
if not urwid.supports_unicode():
241
208
self._text = self._text.encode("ascii", "replace")
242
209
textlist = [(u"normal", self._text)]
274
233
def keypress(self, (maxcol,), key):
276
235
This overrides the method from urwid.FlowWidget"""
277
if key == u"e" or key == u"+":
279
elif key == u"d" or key == u"-":
237
self.proxy.Enable(dbus_interface = client_interface)
239
self.proxy.Disable(dbus_interface = client_interface)
241
self.proxy.Approve(dbus.Boolean(True, variant_level=1),
242
dbus_interface = client_interface)
244
self.proxy.Approve(dbus.Boolean(False, variant_level=1),
245
dbus_interface = client_interface)
281
246
elif key == u"r" or key == u"_" or key == u"ctrl k":
282
247
self.server_proxy_object.RemoveClient(self.proxy
284
249
elif key == u"s":
285
self.proxy.StartChecker()
250
self.proxy.StartChecker(dbus_interface = client_interface)
286
251
elif key == u"S":
287
self.proxy.StopChecker()
252
self.proxy.StopChecker(dbus_interface = client_interface)
288
253
elif key == u"C":
289
self.proxy.CheckedOK()
254
self.proxy.CheckedOK(dbus_interface = client_interface)
291
256
# elif key == u"p" or key == "=":
292
257
# self.proxy.pause()