305
314
"host", "interval", "last_checked_ok",
306
315
"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))
315
317
def timeout_milliseconds(self):
316
318
"Return the 'timeout' attribute in milliseconds"
317
return self._timedelta_to_milliseconds(self.timeout)
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)
319
325
def interval_milliseconds(self):
320
326
"Return the 'interval' attribute in milliseconds"
321
return self._timedelta_to_milliseconds(self.interval)
327
return _timedelta_to_milliseconds(self.interval)
323
329
def approval_delay_milliseconds(self):
324
return self._timedelta_to_milliseconds(self.approval_delay)
330
return _timedelta_to_milliseconds(self.approval_delay)
326
332
def __init__(self, name = None, disable_hook=None, config=None):
327
333
"""Note: the 'checker' key in 'config' sets the
764
786
DBusObjectWithProperties.__init__(self, self.bus,
765
787
self.dbus_object_path)
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))
789
def notifychangeproperty(transform_func,
790
dbus_name, type_func=lambda x: x,
792
""" Modify a variable so that its a property that announce its
794
transform_fun: Function that takes a value and transform it to
796
dbus_name: DBus name of the variable
797
type_func: Function that transform the value before sending it
799
variant_level: DBus variant level. default: 1
802
def setter(self, value):
803
old_value = real_value[0]
804
real_value[0] = value
805
if hasattr(self, "dbus_object_path"):
806
if type_func(old_value) != type_func(real_value[0]):
807
dbus_value = transform_func(type_func(real_value[0]),
809
self.PropertyChanged(dbus.String(dbus_name),
812
return property(lambda self: real_value[0], setter)
815
expires = notifychangeproperty(datetime_to_dbus, "Expires")
816
approvals_pending = notifychangeproperty(dbus.Boolean,
819
enabled = notifychangeproperty(dbus.Boolean, "Enabled")
820
last_enabled = notifychangeproperty(datetime_to_dbus,
822
checker = notifychangeproperty(dbus.Boolean, "CheckerRunning",
823
type_func = lambda checker: checker is not None)
824
last_checked_ok = notifychangeproperty(datetime_to_dbus,
826
last_approval_request = notifychangeproperty(datetime_to_dbus,
827
"LastApprovalRequest")
828
approved_by_default = notifychangeproperty(dbus.Boolean,
830
approval_delay = notifychangeproperty(dbus.UInt16, "ApprovalDelay",
831
type_func = _timedelta_to_milliseconds)
832
approval_duration = notifychangeproperty(dbus.UInt16, "ApprovalDuration",
833
type_func = _timedelta_to_milliseconds)
834
host = notifychangeproperty(dbus.String, "Host")
835
timeout = notifychangeproperty(dbus.UInt16, "Timeout",
836
type_func = _timedelta_to_milliseconds)
837
extended_timeout = notifychangeproperty(dbus.UInt16, "ExtendedTimeout",
838
type_func = _timedelta_to_milliseconds)
839
interval = notifychangeproperty(dbus.UInt16, "Interval",
840
type_func = _timedelta_to_milliseconds)
841
checker_command = notifychangeproperty(dbus.String, "Checker")
843
del notifychangeproperty
811
845
def __del__(self, *args, **kwargs):
839
870
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,
859
873
def start_checker(self, *args, **kwargs):
860
874
old_checker = self.checker
861
875
if self.checker is not None:
868
882
and old_checker_pid != self.checker.pid):
869
883
# Emit D-Bus signal
870
884
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
887
def _reset_approved(self):
886
888
self._approved = None
998
997
if value is None: # get
999
998
return dbus.UInt64(self.approval_delay_milliseconds())
1000
999
self.approval_delay = datetime.timedelta(0, 0, 0, value)
1002
self.PropertyChanged(dbus.String("ApprovalDelay"),
1003
dbus.UInt64(value, variant_level=1))
1005
1001
# ApprovalDuration - property
1006
1002
@dbus_service_property(_interface, signature="t",
1007
1003
access="readwrite")
1008
1004
def ApprovalDuration_dbus_property(self, value=None):
1009
1005
if value is None: # get
1010
return dbus.UInt64(self._timedelta_to_milliseconds(
1006
return dbus.UInt64(_timedelta_to_milliseconds(
1011
1007
self.approval_duration))
1012
1008
self.approval_duration = datetime.timedelta(0, 0, 0, value)
1014
self.PropertyChanged(dbus.String("ApprovalDuration"),
1015
dbus.UInt64(value, variant_level=1))
1017
1010
# Name - property
1018
1011
@dbus_service_property(_interface, signature="s", access="read")
1031
1024
if value is None: # get
1032
1025
return dbus.String(self.host)
1033
1026
self.host = value
1035
self.PropertyChanged(dbus.String("Host"),
1036
dbus.String(value, variant_level=1))
1038
1028
# Created - property
1039
1029
@dbus_service_property(_interface, signature="s", access="read")
1040
1030
def Created_dbus_property(self):
1041
return dbus.String(self._datetime_to_dbus(self.created))
1031
return dbus.String(datetime_to_dbus(self.created))
1043
1033
# LastEnabled - property
1044
1034
@dbus_service_property(_interface, signature="s", access="read")
1045
1035
def LastEnabled_dbus_property(self):
1046
if self.last_enabled is None:
1047
return dbus.String("")
1048
return dbus.String(self._datetime_to_dbus(self.last_enabled))
1036
return datetime_to_dbus(self.last_enabled)
1050
1038
# Enabled - property
1051
1039
@dbus_service_property(_interface, signature="b",
1065
1053
if value is not None:
1066
1054
self.checked_ok()
1068
if self.last_checked_ok is None:
1069
return dbus.String("")
1070
return dbus.String(self._datetime_to_dbus(self
1056
return datetime_to_dbus(self.last_checked_ok)
1058
# Expires - property
1059
@dbus_service_property(_interface, signature="s", access="read")
1060
def Expires_dbus_property(self):
1061
return datetime_to_dbus(self.expires)
1073
1063
# LastApprovalRequest - property
1074
1064
@dbus_service_property(_interface, signature="s", access="read")
1075
1065
def LastApprovalRequest_dbus_property(self):
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
return datetime_to_dbus(self.last_approval_request)
1082
1068
# Timeout - property
1083
1069
@dbus_service_property(_interface, signature="t",
1104
1088
# The timeout has passed
1091
self.expires = (datetime.datetime.utcnow()
1092
+ datetime.timedelta(milliseconds = time_to_die))
1107
1093
self.disable_initiator_tag = (gobject.timeout_add
1108
1094
(time_to_die, self.disable))
1096
# ExtendedTimeout - property
1097
@dbus_service_property(_interface, signature="t",
1099
def ExtendedTimeout_dbus_property(self, value=None):
1100
if value is None: # get
1101
return dbus.UInt64(self.extended_timeout_milliseconds())
1102
self.extended_timeout = datetime.timedelta(0, 0, 0, value)
1110
1104
# Interval - property
1111
1105
@dbus_service_property(_interface, signature="t",
1112
1106
access="readwrite")
1200
1187
unicode(self.client_address))
1201
1188
logger.debug("Pipe FD: %d",
1202
1189
self.server.child_pipe.fileno())
1204
1191
session = (gnutls.connection
1205
1192
.ClientSession(self.request,
1206
1193
gnutls.connection
1207
1194
.X509Credentials()))
1209
1196
# Note: gnutls.connection.X509Credentials is really a
1210
1197
# generic GnuTLS certificate credentials object so long as
1211
1198
# no X.509 keys are added to it. Therefore, we can use it
1212
1199
# here despite using OpenPGP certificates.
1214
1201
#priority = ':'.join(("NONE", "+VERS-TLS1.1",
1215
1202
# "+AES-256-CBC", "+SHA1",
1216
1203
# "+COMP-NULL", "+CTYPE-OPENPGP",