761
755
return dbus.String(dt.isoformat(),
762
756
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.
758
class transitional_dbus_metaclass(DBusObjectWithProperties.__metaclass__):
769
759
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)))
760
for attrname, old_dbusobj in inspect.getmembers(bases[0]):
761
new_interface = getattr(old_dbusobj, "_dbus_interface", "").replace("se.bsnet.fukt.", "se.recompile.")
762
if (getattr(old_dbusobj, "_dbus_is_signal", False)
763
and old_dbusobj._dbus_interface.startswith("se.bsnet.fukt.Mandos")):
764
unwrappedfunc = dict(zip(old_dbusobj.func_code.co_freevars,
765
old_dbusobj.__closure__))["func"].cell_contents
766
newfunc = types.FunctionType(unwrappedfunc.func_code,
767
unwrappedfunc.func_globals,
768
unwrappedfunc.func_name,
769
unwrappedfunc.func_defaults,
770
unwrappedfunc.func_closure)
771
new_dbusfunc = dbus.service.signal(
772
new_interface, old_dbusobj._dbus_signature)(newfunc)
773
attr["_transitional_" + attrname] = new_dbusfunc
775
def fixscope(func1, func2):
776
def newcall(*args, **kwargs):
777
func1(*args, **kwargs)
778
func2(*args, **kwargs)
781
attr[attrname] = fixscope(old_dbusobj, new_dbusfunc)
783
elif (getattr(old_dbusobj, "_dbus_is_method", False)
784
and old_dbusobj._dbus_interface.startswith("se.bsnet.fukt.Mandos")):
785
new_dbusfunc = (dbus.service.method
787
old_dbusobj._dbus_in_signature,
788
old_dbusobj._dbus_out_signature)
790
(old_dbusobj.func_code,
791
old_dbusobj.func_globals,
792
old_dbusobj.func_name,
793
old_dbusobj.func_defaults,
794
old_dbusobj.func_closure)))
796
attr[attrname] = new_dbusfunc
797
elif (getattr(old_dbusobj, "_dbus_is_property", False)
798
and old_dbusobj._dbus_interface.startswith("se.bsnet.fukt.Mandos")):
799
new_dbusfunc = (dbus_service_property
801
old_dbusobj._dbus_signature,
802
old_dbusobj._dbus_access,
803
old_dbusobj._dbus_get_args_options["byte_arrays"])
805
(old_dbusobj.func_code,
806
old_dbusobj.func_globals,
807
old_dbusobj.func_name,
808
old_dbusobj.func_defaults,
809
old_dbusobj.func_closure)))
811
attr[attrname] = new_dbusfunc
859
812
return type.__new__(mcs, name, bases, attr)
861
814
class ClientDBus(Client, DBusObjectWithProperties):
888
841
def notifychangeproperty(transform_func,
889
842
dbus_name, type_func=lambda x: x,
890
843
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
844
""" Modify a variable so that its a property that announce its
846
transform_fun: Function that takes a value and transform it to
848
dbus_name: DBus name of the variable
897
849
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
851
variant_level: DBus variant level. default: 1
901
attrname = "_{0}".format(dbus_name)
902
854
def setter(self, value):
855
old_value = real_value[0]
856
real_value[0] = value
903
857
if hasattr(self, "dbus_object_path"):
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),
858
if type_func(old_value) != type_func(real_value[0]):
859
dbus_value = transform_func(type_func(real_value[0]),
909
861
self.PropertyChanged(dbus.String(dbus_name),
911
setattr(self, attrname, value)
913
return property(lambda self: getattr(self, attrname), setter)
864
return property(lambda self: real_value[0], setter)
916
867
expires = notifychangeproperty(datetime_to_dbus, "Expires")
921
872
last_enabled = notifychangeproperty(datetime_to_dbus,
923
874
checker = notifychangeproperty(dbus.Boolean, "CheckerRunning",
924
type_func = lambda checker:
875
type_func = lambda checker: checker is not None)
926
876
last_checked_ok = notifychangeproperty(datetime_to_dbus,
928
last_approval_request = notifychangeproperty(
929
datetime_to_dbus, "LastApprovalRequest")
878
last_approval_request = notifychangeproperty(datetime_to_dbus,
879
"LastApprovalRequest")
930
880
approved_by_default = notifychangeproperty(dbus.Boolean,
931
881
"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)
882
approval_delay = notifychangeproperty(dbus.UInt16, "ApprovalDelay",
883
type_func = _timedelta_to_milliseconds)
884
approval_duration = notifychangeproperty(dbus.UInt16, "ApprovalDuration",
885
type_func = _timedelta_to_milliseconds)
939
886
host = notifychangeproperty(dbus.String, "Host")
940
887
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)
888
type_func = _timedelta_to_milliseconds)
889
extended_timeout = notifychangeproperty(dbus.UInt16, "ExtendedTimeout",
890
type_func = _timedelta_to_milliseconds)
891
interval = notifychangeproperty(dbus.UInt16, "Interval",
892
type_func = _timedelta_to_milliseconds)
950
893
checker_command = notifychangeproperty(dbus.String, "Checker")
952
895
del notifychangeproperty
1684
1616
"dress: %s", fpr, address)
1685
1617
if self.use_dbus:
1686
1618
# Emit D-Bus signal
1687
mandos_dbus_service.ClientNotFound(fpr,
1619
mandos_dbus_service.ClientNotFound(fpr, address[0])
1689
1620
parent_pipe.send(False)
1692
1623
gobject.io_add_watch(parent_pipe.fileno(),
1693
1624
gobject.IO_IN | gobject.IO_HUP,
1694
1625
functools.partial(self.handle_ipc,
1626
parent_pipe = parent_pipe,
1627
client_object = client))
1700
1628
parent_pipe.send(True)
1701
# remove the old hook in favor of the new above hook on
1629
# remove the old hook in favor of the new above hook on same fileno
1704
1631
if command == 'funcall':
1705
1632
funcname = request[1]
1706
1633
args = request[2]
1707
1634
kwargs = request[3]
1709
parent_pipe.send(('data', getattr(client_object,
1636
parent_pipe.send(('data', getattr(client_object, funcname)(*args, **kwargs)))
1713
1638
if command == 'getattr':
1714
1639
attrname = request[1]
1715
1640
if callable(client_object.__getattribute__(attrname)):
1716
1641
parent_pipe.send(('function',))
1718
parent_pipe.send(('data', client_object
1719
.__getattribute__(attrname)))
1643
parent_pipe.send(('data', client_object.__getattribute__(attrname)))
1721
1645
if command == 'setattr':
1722
1646
attrname = request[1]