313
315
"created", "enabled", "fingerprint",
314
316
"host", "interval", "last_checked_ok",
315
317
"last_enabled", "name", "timeout")
317
319
def timeout_milliseconds(self):
318
320
"Return the 'timeout' attribute in milliseconds"
319
321
return _timedelta_to_milliseconds(self.timeout)
321
323
def extended_timeout_milliseconds(self):
322
324
"Return the 'extended_timeout' attribute in milliseconds"
323
return _timedelta_to_milliseconds(self.extended_timeout)
325
return _timedelta_to_milliseconds(self.extended_timeout)
325
327
def interval_milliseconds(self):
326
328
"Return the 'interval' attribute in milliseconds"
327
329
return _timedelta_to_milliseconds(self.interval)
329
331
def approval_delay_milliseconds(self):
330
332
return _timedelta_to_milliseconds(self.approval_delay)
625
631
def _get_all_dbus_properties(self):
626
632
"""Returns a generator of (name, attribute) pairs
628
return ((prop._dbus_name, prop)
634
return ((prop.__get__(self)._dbus_name, prop.__get__(self))
635
for cls in self.__class__.__mro__
629
636
for name, prop in
630
inspect.getmembers(self, self._is_dbus_property))
637
inspect.getmembers(cls, self._is_dbus_property))
632
639
def _get_dbus_property(self, interface_name, property_name):
633
640
"""Returns a bound method if one exists which is a D-Bus
634
641
property with the specified name and interface.
636
for name in (property_name,
637
property_name + "_dbus_property"):
638
prop = getattr(self, name, None)
640
or not self._is_dbus_property(prop)
641
or prop._dbus_name != property_name
642
or (interface_name and prop._dbus_interface
643
and interface_name != prop._dbus_interface)):
643
for cls in self.__class__.__mro__:
644
for name, value in (inspect.getmembers
645
(cls, self._is_dbus_property)):
646
if (value._dbus_name == property_name
647
and value._dbus_interface == interface_name):
648
return value.__get__(self)
646
650
# No such property
647
651
raise DBusPropertyNotFound(self.dbus_object_path + ":"
648
652
+ interface_name + "."
757
761
return dbus.String(dt.isoformat(),
758
762
variant_level=variant_level)
764
class AlternateDBusNamesMetaclass(DBusObjectWithProperties
766
"""Applied to an empty subclass of a D-Bus object, this metaclass
767
will add additional D-Bus attributes matching a certain pattern.
769
def __new__(mcs, name, bases, attr):
770
# Go through all the base classes which could have D-Bus
771
# methods, signals, or properties in them
772
for base in (b for b in bases
773
if issubclass(b, dbus.service.Object)):
774
# Go though all attributes of the base class
775
for attrname, attribute in inspect.getmembers(base):
776
# Ignore non-D-Bus attributes, and D-Bus attributes
777
# with the wrong interface name
778
if (not hasattr(attribute, "_dbus_interface")
779
or not attribute._dbus_interface
780
.startswith("se.recompile.Mandos")):
782
# Create an alternate D-Bus interface name based on
784
alt_interface = (attribute._dbus_interface
785
.replace("se.recompile.Mandos",
786
"se.bsnet.fukt.Mandos"))
787
# Is this a D-Bus signal?
788
if getattr(attribute, "_dbus_is_signal", False):
789
# Extract the original non-method function by
791
nonmethod_func = (dict(
792
zip(attribute.func_code.co_freevars,
793
attribute.__closure__))["func"]
795
# Create a new, but exactly alike, function
796
# object, and decorate it to be a new D-Bus signal
797
# with the alternate D-Bus interface name
798
new_function = (dbus.service.signal
800
attribute._dbus_signature)
802
nonmethod_func.func_code,
803
nonmethod_func.func_globals,
804
nonmethod_func.func_name,
805
nonmethod_func.func_defaults,
806
nonmethod_func.func_closure)))
807
# Define a creator of a function to call both the
808
# old and new functions, so both the old and new
809
# signals gets sent when the function is called
810
def fixscope(func1, func2):
811
"""This function is a scope container to pass
812
func1 and func2 to the "call_both" function
813
outside of its arguments"""
814
def call_both(*args, **kwargs):
815
"""This function will emit two D-Bus
816
signals by calling func1 and func2"""
817
func1(*args, **kwargs)
818
func2(*args, **kwargs)
820
# Create the "call_both" function and add it to
822
attr[attrname] = fixscope(attribute,
824
# Is this a D-Bus method?
825
elif getattr(attribute, "_dbus_is_method", False):
826
# Create a new, but exactly alike, function
827
# object. Decorate it to be a new D-Bus method
828
# with the alternate D-Bus interface name. Add it
830
attr[attrname] = (dbus.service.method
832
attribute._dbus_in_signature,
833
attribute._dbus_out_signature)
835
(attribute.func_code,
836
attribute.func_globals,
838
attribute.func_defaults,
839
attribute.func_closure)))
840
# Is this a D-Bus property?
841
elif getattr(attribute, "_dbus_is_property", False):
842
# Create a new, but exactly alike, function
843
# object, and decorate it to be a new D-Bus
844
# property with the alternate D-Bus interface
845
# name. Add it to the class.
846
attr[attrname] = (dbus_service_property
848
attribute._dbus_signature,
849
attribute._dbus_access,
851
._dbus_get_args_options
854
(attribute.func_code,
855
attribute.func_globals,
857
attribute.func_defaults,
858
attribute.func_closure)))
859
return type.__new__(mcs, name, bases, attr)
760
861
class ClientDBus(Client, DBusObjectWithProperties):
761
862
"""A Client class using D-Bus
787
888
def notifychangeproperty(transform_func,
788
889
dbus_name, type_func=lambda x: x,
789
890
variant_level=1):
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
891
""" Modify a variable so that it's a property which announces
894
transform_fun: Function that takes a value and transforms it
896
dbus_name: D-Bus name of the variable
795
897
type_func: Function that transform the value before sending it
797
variant_level: DBus variant level. default: 1
898
to the D-Bus. Default: no transform
899
variant_level: D-Bus variant level. Default: 1
901
attrname = "_{0}".format(dbus_name)
800
902
def setter(self, value):
801
old_value = real_value[0]
802
real_value[0] = value
803
903
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]),
904
if (not hasattr(self, attrname) or
905
type_func(getattr(self, attrname, None))
906
!= type_func(value)):
907
dbus_value = transform_func(type_func(value),
807
909
self.PropertyChanged(dbus.String(dbus_name),
810
return property(lambda self: real_value[0], setter)
911
setattr(self, attrname, value)
913
return property(lambda self: getattr(self, attrname), setter)
813
916
expires = notifychangeproperty(datetime_to_dbus, "Expires")
814
917
approvals_pending = notifychangeproperty(dbus.Boolean,
815
918
"ApprovalPending",
818
921
last_enabled = notifychangeproperty(datetime_to_dbus,
820
923
checker = notifychangeproperty(dbus.Boolean, "CheckerRunning",
821
type_func = lambda checker: checker is not None)
924
type_func = lambda checker:
822
926
last_checked_ok = notifychangeproperty(datetime_to_dbus,
824
last_approval_request = notifychangeproperty(datetime_to_dbus,
825
"LastApprovalRequest")
928
last_approval_request = notifychangeproperty(
929
datetime_to_dbus, "LastApprovalRequest")
826
930
approved_by_default = notifychangeproperty(dbus.Boolean,
827
931
"ApprovedByDefault")
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)
932
approval_delay = notifychangeproperty(dbus.UInt16,
935
_timedelta_to_milliseconds)
936
approval_duration = notifychangeproperty(
937
dbus.UInt16, "ApprovalDuration",
938
type_func = _timedelta_to_milliseconds)
832
939
host = notifychangeproperty(dbus.String, "Host")
833
940
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)
942
_timedelta_to_milliseconds)
943
extended_timeout = notifychangeproperty(
944
dbus.UInt16, "ExtendedTimeout",
945
type_func = _timedelta_to_milliseconds)
946
interval = notifychangeproperty(dbus.UInt16,
949
_timedelta_to_milliseconds)
839
950
checker_command = notifychangeproperty(dbus.String, "Checker")
841
952
del notifychangeproperty
1185
1299
unicode(self.client_address))
1186
1300
logger.debug("Pipe FD: %d",
1187
1301
self.server.child_pipe.fileno())
1189
1303
session = (gnutls.connection
1190
1304
.ClientSession(self.request,
1191
1305
gnutls.connection
1192
1306
.X509Credentials()))
1194
1308
# Note: gnutls.connection.X509Credentials is really a
1195
1309
# generic GnuTLS certificate credentials object so long as
1196
1310
# no X.509 keys are added to it. Therefore, we can use it
1197
1311
# here despite using OpenPGP certificates.
1199
1313
#priority = ':'.join(("NONE", "+VERS-TLS1.1",
1200
1314
# "+AES-256-CBC", "+SHA1",
1201
1315
# "+COMP-NULL", "+CTYPE-OPENPGP",
1558
1678
"dress: %s", fpr, address)
1559
1679
if self.use_dbus:
1560
1680
# Emit D-Bus signal
1561
mandos_dbus_service.ClientNotFound(fpr, address[0])
1681
mandos_dbus_service.ClientNotFound(fpr,
1562
1683
parent_pipe.send(False)
1565
1686
gobject.io_add_watch(parent_pipe.fileno(),
1566
1687
gobject.IO_IN | gobject.IO_HUP,
1567
1688
functools.partial(self.handle_ipc,
1568
parent_pipe = parent_pipe,
1569
client_object = client))
1570
1693
parent_pipe.send(True)
1571
# remove the old hook in favor of the new above hook on same fileno
1694
# remove the old hook in favor of the new above hook on
1573
1697
if command == 'funcall':
1574
1698
funcname = request[1]
1575
1699
args = request[2]
1576
1700
kwargs = request[3]
1578
parent_pipe.send(('data', getattr(client_object, funcname)(*args, **kwargs)))
1702
parent_pipe.send(('data', getattr(client_object,
1580
1706
if command == 'getattr':
1581
1707
attrname = request[1]
1582
1708
if callable(client_object.__getattribute__(attrname)):
1583
1709
parent_pipe.send(('function',))
1585
parent_pipe.send(('data', client_object.__getattribute__(attrname)))
1711
parent_pipe.send(('data', client_object
1712
.__getattribute__(attrname)))
1587
1714
if command == 'setattr':
1588
1715
attrname = request[1]
1589
1716
value = request[2]
1590
1717
setattr(client_object, attrname, value)