315
313
"created", "enabled", "fingerprint",
316
314
"host", "interval", "last_checked_ok",
317
315
"last_enabled", "name", "timeout")
319
317
def timeout_milliseconds(self):
320
318
"Return the 'timeout' attribute in milliseconds"
321
319
return _timedelta_to_milliseconds(self.timeout)
323
321
def extended_timeout_milliseconds(self):
324
322
"Return the 'extended_timeout' attribute in milliseconds"
325
return _timedelta_to_milliseconds(self.extended_timeout)
323
return _timedelta_to_milliseconds(self.extended_timeout)
327
325
def interval_milliseconds(self):
328
326
"Return the 'interval' attribute in milliseconds"
329
327
return _timedelta_to_milliseconds(self.interval)
331
329
def approval_delay_milliseconds(self):
332
330
return _timedelta_to_milliseconds(self.approval_delay)
631
625
def _get_all_dbus_properties(self):
632
626
"""Returns a generator of (name, attribute) pairs
634
return ((prop.__get__(self)._dbus_name, prop.__get__(self))
635
for cls in self.__class__.__mro__
628
return ((prop._dbus_name, prop)
636
629
for name, prop in
637
inspect.getmembers(cls, self._is_dbus_property))
630
inspect.getmembers(self, self._is_dbus_property))
639
632
def _get_dbus_property(self, interface_name, property_name):
640
633
"""Returns a bound method if one exists which is a D-Bus
641
634
property with the specified name and 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)
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)):
650
646
# No such property
651
647
raise DBusPropertyNotFound(self.dbus_object_path + ":"
652
648
+ interface_name + "."
761
757
return dbus.String(dt.isoformat(),
762
758
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)
861
760
class ClientDBus(Client, DBusObjectWithProperties):
862
761
"""A Client class using D-Bus
888
787
def notifychangeproperty(transform_func,
889
788
dbus_name, type_func=lambda x: x,
890
789
variant_level=1):
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
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
897
795
type_func: Function that transform the value before sending it
898
to the D-Bus. Default: no transform
899
variant_level: D-Bus variant level. Default: 1
797
variant_level: DBus variant level. default: 1
901
799
real_value = [None,]
902
800
def setter(self, value):
921
818
last_enabled = notifychangeproperty(datetime_to_dbus,
923
820
checker = notifychangeproperty(dbus.Boolean, "CheckerRunning",
924
type_func = lambda checker:
821
type_func = lambda checker: checker is not None)
926
822
last_checked_ok = notifychangeproperty(datetime_to_dbus,
928
last_approval_request = notifychangeproperty(
929
datetime_to_dbus, "LastApprovalRequest")
824
last_approval_request = notifychangeproperty(datetime_to_dbus,
825
"LastApprovalRequest")
930
826
approved_by_default = notifychangeproperty(dbus.Boolean,
931
827
"ApprovedByDefault")
932
approval_delay = notifychangeproperty(dbus.UInt16,
935
_timedelta_to_milliseconds)
936
approval_duration = notifychangeproperty(
937
dbus.UInt16, "ApprovalDuration",
938
type_func = _timedelta_to_milliseconds)
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)
939
832
host = notifychangeproperty(dbus.String, "Host")
940
833
timeout = notifychangeproperty(dbus.UInt16, "Timeout",
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)
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)
950
839
checker_command = notifychangeproperty(dbus.String, "Checker")
952
841
del notifychangeproperty
1299
1185
unicode(self.client_address))
1300
1186
logger.debug("Pipe FD: %d",
1301
1187
self.server.child_pipe.fileno())
1303
1189
session = (gnutls.connection
1304
1190
.ClientSession(self.request,
1305
1191
gnutls.connection
1306
1192
.X509Credentials()))
1308
1194
# Note: gnutls.connection.X509Credentials is really a
1309
1195
# generic GnuTLS certificate credentials object so long as
1310
1196
# no X.509 keys are added to it. Therefore, we can use it
1311
1197
# here despite using OpenPGP certificates.
1313
1199
#priority = ':'.join(("NONE", "+VERS-TLS1.1",
1314
1200
# "+AES-256-CBC", "+SHA1",
1315
1201
# "+COMP-NULL", "+CTYPE-OPENPGP",
1678
1558
"dress: %s", fpr, address)
1679
1559
if self.use_dbus:
1680
1560
# Emit D-Bus signal
1681
mandos_dbus_service.ClientNotFound(fpr,
1561
mandos_dbus_service.ClientNotFound(fpr, address[0])
1683
1562
parent_pipe.send(False)
1686
1565
gobject.io_add_watch(parent_pipe.fileno(),
1687
1566
gobject.IO_IN | gobject.IO_HUP,
1688
1567
functools.partial(self.handle_ipc,
1568
parent_pipe = parent_pipe,
1569
client_object = client))
1693
1570
parent_pipe.send(True)
1694
# remove the old hook in favor of the new above hook on
1571
# remove the old hook in favor of the new above hook on same fileno
1697
1573
if command == 'funcall':
1698
1574
funcname = request[1]
1699
1575
args = request[2]
1700
1576
kwargs = request[3]
1702
parent_pipe.send(('data', getattr(client_object,
1578
parent_pipe.send(('data', getattr(client_object, funcname)(*args, **kwargs)))
1706
1580
if command == 'getattr':
1707
1581
attrname = request[1]
1708
1582
if callable(client_object.__getattribute__(attrname)):
1709
1583
parent_pipe.send(('function',))
1711
parent_pipe.send(('data', client_object
1712
.__getattribute__(attrname)))
1585
parent_pipe.send(('data', client_object.__getattribute__(attrname)))
1714
1587
if command == 'setattr':
1715
1588
attrname = request[1]
1716
1589
value = request[2]
1717
1590
setattr(client_object, attrname, value)