303
297
secret: bytestring; sent verbatim (over TLS) to client
304
298
timeout: datetime.timedelta(); How long from last_checked_ok
305
299
until this client is disabled
306
extended_timeout: extra long timeout when password has been sent
307
300
runtime_expansions: Allowed attributes for runtime expansion.
308
expires: datetime.datetime(); time (UTC) when a client will be
312
303
runtime_expansions = ("approval_delay", "approval_duration",
313
304
"created", "enabled", "fingerprint",
314
305
"host", "interval", "last_checked_ok",
315
306
"last_enabled", "name", "timeout")
309
def _timedelta_to_milliseconds(td):
310
"Convert a datetime.timedelta() to milliseconds"
311
return ((td.days * 24 * 60 * 60 * 1000)
312
+ (td.seconds * 1000)
313
+ (td.microseconds // 1000))
317
315
def timeout_milliseconds(self):
318
316
"Return the 'timeout' attribute in milliseconds"
319
return _timedelta_to_milliseconds(self.timeout)
321
def extended_timeout_milliseconds(self):
322
"Return the 'extended_timeout' attribute in milliseconds"
323
return _timedelta_to_milliseconds(self.extended_timeout)
317
return self._timedelta_to_milliseconds(self.timeout)
325
319
def interval_milliseconds(self):
326
320
"Return the 'interval' attribute in milliseconds"
327
return _timedelta_to_milliseconds(self.interval)
321
return self._timedelta_to_milliseconds(self.interval)
329
323
def approval_delay_milliseconds(self):
330
return _timedelta_to_milliseconds(self.approval_delay)
324
return self._timedelta_to_milliseconds(self.approval_delay)
332
326
def __init__(self, name = None, disable_hook=None, config=None):
333
327
"""Note: the 'checker' key in 'config' sets the
784
764
DBusObjectWithProperties.__init__(self, self.bus,
785
765
self.dbus_object_path)
787
def notifychangeproperty(transform_func,
788
dbus_name, type_func=lambda x: x,
790
""" Modify a variable so that its a property that announce its
792
transform_fun: Function that takes a value and transform it to
794
dbus_name: DBus name of the variable
795
type_func: Function that transform the value before sending it
797
variant_level: DBus variant level. default: 1
800
def setter(self, value):
801
old_value = real_value[0]
802
real_value[0] = value
803
if hasattr(self, "dbus_object_path"):
804
if type_func(old_value) != type_func(real_value[0]):
805
dbus_value = transform_func(type_func(real_value[0]),
807
self.PropertyChanged(dbus.String(dbus_name),
810
return property(lambda self: real_value[0], setter)
813
expires = notifychangeproperty(datetime_to_dbus, "Expires")
814
approvals_pending = notifychangeproperty(dbus.Boolean,
817
enabled = notifychangeproperty(dbus.Boolean, "Enabled")
818
last_enabled = notifychangeproperty(datetime_to_dbus,
820
checker = notifychangeproperty(dbus.Boolean, "CheckerRunning",
821
type_func = lambda checker: checker is not None)
822
last_checked_ok = notifychangeproperty(datetime_to_dbus,
824
last_approval_request = notifychangeproperty(datetime_to_dbus,
825
"LastApprovalRequest")
826
approved_by_default = notifychangeproperty(dbus.Boolean,
828
approval_delay = notifychangeproperty(dbus.UInt16, "ApprovalDelay",
829
type_func = _timedelta_to_milliseconds)
830
approval_duration = notifychangeproperty(dbus.UInt16, "ApprovalDuration",
831
type_func = _timedelta_to_milliseconds)
832
host = notifychangeproperty(dbus.String, "Host")
833
timeout = notifychangeproperty(dbus.UInt16, "Timeout",
834
type_func = _timedelta_to_milliseconds)
835
extended_timeout = notifychangeproperty(dbus.UInt16, "ExtendedTimeout",
836
type_func = _timedelta_to_milliseconds)
837
interval = notifychangeproperty(dbus.UInt16, "Interval",
838
type_func = _timedelta_to_milliseconds)
839
checker_command = notifychangeproperty(dbus.String, "Checker")
841
del notifychangeproperty
767
def _get_approvals_pending(self):
768
return self._approvals_pending
769
def _set_approvals_pending(self, value):
770
old_value = self._approvals_pending
771
self._approvals_pending = value
773
if (hasattr(self, "dbus_object_path")
774
and bval is not bool(old_value)):
775
dbus_bool = dbus.Boolean(bval, variant_level=1)
776
self.PropertyChanged(dbus.String("ApprovalPending"),
779
approvals_pending = property(_get_approvals_pending,
780
_set_approvals_pending)
781
del _get_approvals_pending, _set_approvals_pending
784
def _datetime_to_dbus(dt, variant_level=0):
785
"""Convert a UTC datetime.datetime() to a D-Bus type."""
786
return dbus.String(dt.isoformat(),
787
variant_level=variant_level)
790
oldstate = getattr(self, "enabled", False)
791
r = Client.enable(self)
792
if oldstate != self.enabled:
794
self.PropertyChanged(dbus.String("Enabled"),
795
dbus.Boolean(True, variant_level=1))
796
self.PropertyChanged(
797
dbus.String("LastEnabled"),
798
self._datetime_to_dbus(self.last_enabled,
802
def disable(self, quiet = False):
803
oldstate = getattr(self, "enabled", False)
804
r = Client.disable(self, quiet=quiet)
805
if not quiet and oldstate != self.enabled:
807
self.PropertyChanged(dbus.String("Enabled"),
808
dbus.Boolean(False, variant_level=1))
843
811
def __del__(self, *args, **kwargs):
868
839
return Client.checker_callback(self, pid, condition, command,
842
def checked_ok(self, *args, **kwargs):
843
Client.checked_ok(self, *args, **kwargs)
845
self.PropertyChanged(
846
dbus.String("LastCheckedOK"),
847
(self._datetime_to_dbus(self.last_checked_ok,
850
def need_approval(self, *args, **kwargs):
851
r = Client.need_approval(self, *args, **kwargs)
853
self.PropertyChanged(
854
dbus.String("LastApprovalRequest"),
855
(self._datetime_to_dbus(self.last_approval_request,
871
859
def start_checker(self, *args, **kwargs):
872
860
old_checker = self.checker
873
861
if self.checker is not None:
880
868
and old_checker_pid != self.checker.pid):
881
869
# Emit D-Bus signal
882
870
self.CheckerStarted(self.current_checker_command)
871
self.PropertyChanged(
872
dbus.String("CheckerRunning"),
873
dbus.Boolean(True, variant_level=1))
876
def stop_checker(self, *args, **kwargs):
877
old_checker = getattr(self, "checker", None)
878
r = Client.stop_checker(self, *args, **kwargs)
879
if (old_checker is not None
880
and getattr(self, "checker", None) is None):
881
self.PropertyChanged(dbus.String("CheckerRunning"),
882
dbus.Boolean(False, variant_level=1))
885
885
def _reset_approved(self):
886
886
self._approved = None
995
998
if value is None: # get
996
999
return dbus.UInt64(self.approval_delay_milliseconds())
997
1000
self.approval_delay = datetime.timedelta(0, 0, 0, value)
1002
self.PropertyChanged(dbus.String("ApprovalDelay"),
1003
dbus.UInt64(value, variant_level=1))
999
1005
# ApprovalDuration - property
1000
1006
@dbus_service_property(_interface, signature="t",
1001
1007
access="readwrite")
1002
1008
def ApprovalDuration_dbus_property(self, value=None):
1003
1009
if value is None: # get
1004
return dbus.UInt64(_timedelta_to_milliseconds(
1010
return dbus.UInt64(self._timedelta_to_milliseconds(
1005
1011
self.approval_duration))
1006
1012
self.approval_duration = datetime.timedelta(0, 0, 0, value)
1014
self.PropertyChanged(dbus.String("ApprovalDuration"),
1015
dbus.UInt64(value, variant_level=1))
1008
1017
# Name - property
1009
1018
@dbus_service_property(_interface, signature="s", access="read")
1022
1031
if value is None: # get
1023
1032
return dbus.String(self.host)
1024
1033
self.host = value
1035
self.PropertyChanged(dbus.String("Host"),
1036
dbus.String(value, variant_level=1))
1026
1038
# Created - property
1027
1039
@dbus_service_property(_interface, signature="s", access="read")
1028
1040
def Created_dbus_property(self):
1029
return dbus.String(datetime_to_dbus(self.created))
1041
return dbus.String(self._datetime_to_dbus(self.created))
1031
1043
# LastEnabled - property
1032
1044
@dbus_service_property(_interface, signature="s", access="read")
1033
1045
def LastEnabled_dbus_property(self):
1034
return datetime_to_dbus(self.last_enabled)
1046
if self.last_enabled is None:
1047
return dbus.String("")
1048
return dbus.String(self._datetime_to_dbus(self.last_enabled))
1036
1050
# Enabled - property
1037
1051
@dbus_service_property(_interface, signature="b",
1051
1065
if value is not None:
1052
1066
self.checked_ok()
1054
return datetime_to_dbus(self.last_checked_ok)
1056
# Expires - property
1057
@dbus_service_property(_interface, signature="s", access="read")
1058
def Expires_dbus_property(self):
1059
return datetime_to_dbus(self.expires)
1068
if self.last_checked_ok is None:
1069
return dbus.String("")
1070
return dbus.String(self._datetime_to_dbus(self
1061
1073
# LastApprovalRequest - property
1062
1074
@dbus_service_property(_interface, signature="s", access="read")
1063
1075
def LastApprovalRequest_dbus_property(self):
1064
return datetime_to_dbus(self.last_approval_request)
1076
if self.last_approval_request is None:
1077
return dbus.String("")
1078
return dbus.String(self.
1079
_datetime_to_dbus(self
1080
.last_approval_request))
1066
1082
# Timeout - property
1067
1083
@dbus_service_property(_interface, signature="t",
1086
1104
# The timeout has passed
1089
self.expires = (datetime.datetime.utcnow()
1090
+ datetime.timedelta(milliseconds = time_to_die))
1091
1107
self.disable_initiator_tag = (gobject.timeout_add
1092
1108
(time_to_die, self.disable))
1094
# ExtendedTimeout - property
1095
@dbus_service_property(_interface, signature="t",
1097
def ExtendedTimeout_dbus_property(self, value=None):
1098
if value is None: # get
1099
return dbus.UInt64(self.extended_timeout_milliseconds())
1100
self.extended_timeout = datetime.timedelta(0, 0, 0, value)
1102
1110
# Interval - property
1103
1111
@dbus_service_property(_interface, signature="t",
1104
1112
access="readwrite")