17
17
# GNU General Public License for more details.
19
19
# You should have received a copy of the GNU General Public License
20
# along with this program. If not, see
21
# <http://www.gnu.org/licenses/>.
20
# along with this program. If not, see <http://www.gnu.org/licenses/>.
23
22
# Contact the authors at <mandos@recompile.se>.
86
83
properties and calls a hook function when any of them are
89
def __init__(self, proxy_object=None, properties=None, **kwargs):
86
def __init__(self, proxy_object=None, *args, **kwargs):
90
87
self.proxy = proxy_object # Mandos Client proxy object
91
self.properties = dict() if properties is None else properties
89
self.properties = dict()
92
90
self.property_changed_match = (
93
91
self.proxy.connect_to_signal("PropertyChanged",
94
self._property_changed,
92
self.property_changed,
98
if properties is None:
99
self.properties.update(
100
self.proxy.GetAll(client_interface,
102
= dbus.PROPERTIES_IFACE))
104
super(MandosClientPropertyCache, self).__init__(**kwargs)
106
def _property_changed(self, property, value):
107
"""Helper which takes positional arguments"""
108
return self.property_changed(property=property, value=value)
96
self.properties.update(
97
self.proxy.GetAll(client_interface,
98
dbus_interface = dbus.PROPERTIES_IFACE))
100
#XXX This breaks good super behaviour
101
# super(MandosClientPropertyCache, self).__init__(
110
104
def property_changed(self, property=None, value=None):
111
105
"""This is called whenever we get a PropertyChanged signal
114
108
# Update properties dict with new value
115
109
self.properties[property] = value
111
def delete(self, *args, **kwargs):
118
112
self.property_changed_match.remove()
113
super(MandosClientPropertyCache, self).__init__(
121
117
class MandosClientWidget(urwid.FlowWidget, MandosClientPropertyCache):
125
121
def __init__(self, server_proxy_object=None, update_hook=None,
126
delete_hook=None, logger=None, **kwargs):
122
delete_hook=None, logger=None, *args, **kwargs):
127
123
# Called on update
128
124
self.update_hook = update_hook
129
125
# Called on delete
134
130
self.logger = logger
136
132
self._update_timer_callback_tag = None
133
self._update_timer_callback_lock = 0
138
135
# The widget shown normally
139
136
self._text_widget = urwid.Text("")
140
137
# The widget shown when we have focus
141
138
self._focus_text_widget = urwid.Text("")
142
super(MandosClientWidget, self).__init__(**kwargs)
139
super(MandosClientWidget, self).__init__(
140
update_hook=update_hook, delete_hook=delete_hook,
144
143
self.opened = False
145
last_checked_ok = isoformat_to_datetime(self.properties
148
if self.properties ["LastCheckerStatus"] != 0:
149
self.using_timer(True)
151
if self.need_approval:
152
self.using_timer(True)
146
154
self.match_objects = (
147
155
self.proxy.connect_to_signal("CheckerCompleted",
148
156
self.checker_completed,
167
175
#self.logger('Created client {0}'
168
176
# .format(self.properties["Name"]))
178
def property_changed(self, property=None, value=None):
179
super(self, MandosClientWidget).property_changed(property,
181
if property == "ApprovalPending":
182
using_timer(bool(value))
183
if property == "LastCheckerStatus":
184
using_timer(value != 0)
185
#self.logger('Checker for client {0} (command "{1}") was '
186
# ' successful'.format(self.properties["Name"],
170
189
def using_timer(self, flag):
171
190
"""Call this method with True or False when timer should be
172
191
activated or deactivated.
174
if flag and self._update_timer_callback_tag is None:
193
old = self._update_timer_callback_lock
195
self._update_timer_callback_lock += 1
197
self._update_timer_callback_lock -= 1
198
if old == 0 and self._update_timer_callback_lock:
175
199
# Will update the shown timer value every second
176
200
self._update_timer_callback_tag = (gobject.timeout_add
178
202
self.update_timer))
179
elif not (flag or self._update_timer_callback_tag is None):
203
elif old and self._update_timer_callback_lock == 0:
180
204
gobject.source_remove(self._update_timer_callback_tag)
181
205
self._update_timer_callback_tag = None
224
248
message = 'Client {0} will get its secret in {1} seconds'
225
249
self.logger(message.format(self.properties["Name"],
251
self.using_timer(True)
228
253
def rejected(self, reason):
229
254
self.logger('Client {0} was rejected; reason: {1}'
255
280
"bold-underline-blink":
256
281
"bold-underline-blink-standout",
259
284
# Rebuild focus and non-focus widgets using current properties
261
286
# Base part of a client. Name!
262
287
base = '{name}: '.format(name=self.properties["Name"])
263
288
if not self.properties["Enabled"]:
264
289
message = "DISABLED"
265
self.using_timer(False)
266
290
elif self.properties["ApprovalPending"]:
267
291
timeout = datetime.timedelta(milliseconds
268
292
= self.properties
270
294
last_approval_request = isoformat_to_datetime(
271
295
self.properties["LastApprovalRequest"])
272
296
if last_approval_request is not None:
273
timer = max(timeout - (datetime.datetime.utcnow()
274
- last_approval_request),
275
datetime.timedelta())
297
timer = timeout - (datetime.datetime.utcnow()
298
- last_approval_request)
277
300
timer = datetime.timedelta()
278
301
if self.properties["ApprovedByDefault"]:
281
304
message = "Denial in {0}. (a)pprove?"
282
305
message = message.format(unicode(timer).rsplit(".", 1)[0])
283
self.using_timer(True)
284
306
elif self.properties["LastCheckerStatus"] != 0:
285
# When checker has failed, show timer until client expires
307
# When checker has failed, print a timer until client expires
286
308
expires = self.properties["Expires"]
287
309
if expires == "":
288
310
timer = datetime.timedelta(0)
290
expires = (datetime.datetime.strptime
291
(expires, '%Y-%m-%dT%H:%M:%S.%f'))
292
timer = max(expires - datetime.datetime.utcnow(),
293
datetime.timedelta())
312
expires = datetime.datetime.strptime(expires,
313
'%Y-%m-%dT%H:%M:%S.%f')
314
timer = expires - datetime.datetime.utcnow()
294
315
message = ('A checker has failed! Time until client'
295
316
' gets disabled: {0}'
296
317
.format(unicode(timer).rsplit(".", 1)[0]))
297
self.using_timer(True)
299
319
message = "enabled"
300
self.using_timer(False)
301
320
self._text = "{0}{1}".format(base, message)
303
322
if not urwid.supports_unicode():
304
323
self._text = self._text.encode("ascii", "replace")
305
324
textlist = [("normal", self._text)]
323
342
return True # Keep calling this
325
def delete(self, **kwargs):
344
def delete(self, *args, **kwargs):
326
345
if self._update_timer_callback_tag is not None:
327
346
gobject.source_remove(self._update_timer_callback_tag)
328
347
self._update_timer_callback_tag = None
331
350
self.match_objects = ()
332
351
if self.delete_hook is not None:
333
352
self.delete_hook(self)
334
return super(MandosClientWidget, self).delete(**kwargs)
353
return super(MandosClientWidget, self).delete(*args, **kwargs)
336
355
def render(self, maxcolrow, focus=False):
337
356
"""Render differently if we have focus.
382
def property_changed(self, property=None, **kwargs):
401
def property_changed(self, property=None, value=None,
383
403
"""Call self.update() if old value is not new value.
384
404
This overrides the method from MandosClientPropertyCache"""
385
405
property_name = unicode(property)
386
406
old_value = self.properties.get(property_name)
387
407
super(MandosClientWidget, self).property_changed(
388
property=property, **kwargs)
408
property=property, value=value, *args, **kwargs)
389
409
if self.properties.get(property_name) != old_value:
395
415
"down" key presses, thus not allowing any containing widgets to
396
416
use them as an excuse to shift focus away from this widget.
398
def keypress(self, *args, **kwargs):
399
ret = super(ConstrainedListBox, self).keypress(*args, **kwargs)
418
def keypress(self, maxcolrow, key):
419
ret = super(ConstrainedListBox, self).keypress(maxcolrow, key)
400
420
if ret in ("up", "down"):