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)
464
459
if timeout is None:
465
460
timeout = self.timeout
466
461
self.last_checked_ok = datetime.datetime.utcnow()
467
if self.disable_initiator_tag is not None:
468
gobject.source_remove(self.disable_initiator_tag)
469
if getattr(self, "enabled", False):
470
self.disable_initiator_tag = (gobject.timeout_add
471
(_timedelta_to_milliseconds
472
(timeout), self.disable))
473
self.expires = datetime.datetime.utcnow() + timeout
462
gobject.source_remove(self.disable_initiator_tag)
463
self.expires = datetime.datetime.utcnow() + timeout
464
self.disable_initiator_tag = (gobject.timeout_add
465
(_timedelta_to_milliseconds(timeout),
475
468
def need_approval(self):
476
469
self.last_approval_request = datetime.datetime.utcnow()
633
625
def _get_all_dbus_properties(self):
634
626
"""Returns a generator of (name, attribute) pairs
636
return ((prop.__get__(self)._dbus_name, prop.__get__(self))
637
for cls in self.__class__.__mro__
628
return ((prop._dbus_name, prop)
638
629
for name, prop in
639
inspect.getmembers(cls, self._is_dbus_property))
630
inspect.getmembers(self, self._is_dbus_property))
641
632
def _get_dbus_property(self, interface_name, property_name):
642
633
"""Returns a bound method if one exists which is a D-Bus
643
634
property with the specified name and interface.
645
for cls in self.__class__.__mro__:
646
for name, value in (inspect.getmembers
647
(cls, self._is_dbus_property)):
648
if (value._dbus_name == property_name
649
and value._dbus_interface == interface_name):
650
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)):
652
646
# No such property
653
647
raise DBusPropertyNotFound(self.dbus_object_path + ":"
654
648
+ interface_name + "."
763
757
return dbus.String(dt.isoformat(),
764
758
variant_level=variant_level)
766
class AlternateDBusNamesMetaclass(DBusObjectWithProperties
768
"""Applied to an empty subclass of a D-Bus object, this metaclass
769
will add additional D-Bus attributes matching a certain pattern.
771
def __new__(mcs, name, bases, attr):
772
# Go through all the base classes which could have D-Bus
773
# methods, signals, or properties in them
774
for base in (b for b in bases
775
if issubclass(b, dbus.service.Object)):
776
# Go though all attributes of the base class
777
for attrname, attribute in inspect.getmembers(base):
778
# Ignore non-D-Bus attributes, and D-Bus attributes
779
# with the wrong interface name
780
if (not hasattr(attribute, "_dbus_interface")
781
or not attribute._dbus_interface
782
.startswith("se.recompile.Mandos")):
784
# Create an alternate D-Bus interface name based on
786
alt_interface = (attribute._dbus_interface
787
.replace("se.recompile.Mandos",
788
"se.bsnet.fukt.Mandos"))
789
# Is this a D-Bus signal?
790
if getattr(attribute, "_dbus_is_signal", False):
791
# Extract the original non-method function by
793
nonmethod_func = (dict(
794
zip(attribute.func_code.co_freevars,
795
attribute.__closure__))["func"]
797
# Create a new, but exactly alike, function
798
# object, and decorate it to be a new D-Bus signal
799
# with the alternate D-Bus interface name
800
new_function = (dbus.service.signal
802
attribute._dbus_signature)
804
nonmethod_func.func_code,
805
nonmethod_func.func_globals,
806
nonmethod_func.func_name,
807
nonmethod_func.func_defaults,
808
nonmethod_func.func_closure)))
809
# Define a creator of a function to call both the
810
# old and new functions, so both the old and new
811
# signals gets sent when the function is called
812
def fixscope(func1, func2):
813
"""This function is a scope container to pass
814
func1 and func2 to the "call_both" function
815
outside of its arguments"""
816
def call_both(*args, **kwargs):
817
"""This function will emit two D-Bus
818
signals by calling func1 and func2"""
819
func1(*args, **kwargs)
820
func2(*args, **kwargs)
822
# Create the "call_both" function and add it to
824
attr[attrname] = fixscope(attribute,
826
# Is this a D-Bus method?
827
elif getattr(attribute, "_dbus_is_method", False):
828
# Create a new, but exactly alike, function
829
# object. Decorate it to be a new D-Bus method
830
# with the alternate D-Bus interface name. Add it
832
attr[attrname] = (dbus.service.method
834
attribute._dbus_in_signature,
835
attribute._dbus_out_signature)
837
(attribute.func_code,
838
attribute.func_globals,
840
attribute.func_defaults,
841
attribute.func_closure)))
842
# Is this a D-Bus property?
843
elif getattr(attribute, "_dbus_is_property", False):
844
# Create a new, but exactly alike, function
845
# object, and decorate it to be a new D-Bus
846
# property with the alternate D-Bus interface
847
# name. Add it to the class.
848
attr[attrname] = (dbus_service_property
850
attribute._dbus_signature,
851
attribute._dbus_access,
853
._dbus_get_args_options
856
(attribute.func_code,
857
attribute.func_globals,
859
attribute.func_defaults,
860
attribute.func_closure)))
861
return type.__new__(mcs, name, bases, attr)
863
760
class ClientDBus(Client, DBusObjectWithProperties):
864
761
"""A Client class using D-Bus
890
787
def notifychangeproperty(transform_func,
891
788
dbus_name, type_func=lambda x: x,
892
789
variant_level=1):
893
""" Modify a variable so that it's a property which announces
896
transform_fun: Function that takes a value and a variant_level
897
and transforms it to a D-Bus type.
898
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
899
795
type_func: Function that transform the value before sending it
900
to the D-Bus. Default: no transform
901
variant_level: D-Bus variant level. Default: 1
797
variant_level: DBus variant level. default: 1
903
attrname = "_{0}".format(dbus_name)
904
800
def setter(self, value):
801
old_value = real_value[0]
802
real_value[0] = value
905
803
if hasattr(self, "dbus_object_path"):
906
if (not hasattr(self, attrname) or
907
type_func(getattr(self, attrname, None))
908
!= type_func(value)):
909
dbus_value = transform_func(type_func(value),
804
if type_func(old_value) != type_func(real_value[0]):
805
dbus_value = transform_func(type_func(real_value[0]),
912
807
self.PropertyChanged(dbus.String(dbus_name),
914
setattr(self, attrname, value)
916
return property(lambda self: getattr(self, attrname), setter)
810
return property(lambda self: real_value[0], setter)
919
813
expires = notifychangeproperty(datetime_to_dbus, "Expires")
920
814
approvals_pending = notifychangeproperty(dbus.Boolean,
921
815
"ApprovalPending",
924
818
last_enabled = notifychangeproperty(datetime_to_dbus,
926
820
checker = notifychangeproperty(dbus.Boolean, "CheckerRunning",
927
type_func = lambda checker:
821
type_func = lambda checker: checker is not None)
929
822
last_checked_ok = notifychangeproperty(datetime_to_dbus,
931
last_approval_request = notifychangeproperty(
932
datetime_to_dbus, "LastApprovalRequest")
824
last_approval_request = notifychangeproperty(datetime_to_dbus,
825
"LastApprovalRequest")
933
826
approved_by_default = notifychangeproperty(dbus.Boolean,
934
827
"ApprovedByDefault")
935
approval_delay = notifychangeproperty(dbus.UInt16,
938
_timedelta_to_milliseconds)
939
approval_duration = notifychangeproperty(
940
dbus.UInt16, "ApprovalDuration",
941
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)
942
832
host = notifychangeproperty(dbus.String, "Host")
943
833
timeout = notifychangeproperty(dbus.UInt16, "Timeout",
945
_timedelta_to_milliseconds)
946
extended_timeout = notifychangeproperty(
947
dbus.UInt16, "ExtendedTimeout",
948
type_func = _timedelta_to_milliseconds)
949
interval = notifychangeproperty(dbus.UInt16,
952
_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)
953
839
checker_command = notifychangeproperty(dbus.String, "Checker")
955
841
del notifychangeproperty
1301
1185
unicode(self.client_address))
1302
1186
logger.debug("Pipe FD: %d",
1303
1187
self.server.child_pipe.fileno())
1305
1189
session = (gnutls.connection
1306
1190
.ClientSession(self.request,
1307
1191
gnutls.connection
1308
1192
.X509Credentials()))
1310
1194
# Note: gnutls.connection.X509Credentials is really a
1311
1195
# generic GnuTLS certificate credentials object so long as
1312
1196
# no X.509 keys are added to it. Therefore, we can use it
1313
1197
# here despite using OpenPGP certificates.
1315
1199
#priority = ':'.join(("NONE", "+VERS-TLS1.1",
1316
1200
# "+AES-256-CBC", "+SHA1",
1317
1201
# "+COMP-NULL", "+CTYPE-OPENPGP",
1534
1413
This function creates a new pipe in self.pipe
1536
1415
parent_pipe, self.child_pipe = multiprocessing.Pipe()
1538
proc = MultiprocessingMixIn.process_request(self, request,
1417
super(MultiprocessingMixInWithPipe,
1418
self).process_request(request, client_address)
1540
1419
self.child_pipe.close()
1541
self.add_pipe(parent_pipe, proc)
1543
def add_pipe(self, parent_pipe, proc):
1420
self.add_pipe(parent_pipe)
1422
def add_pipe(self, parent_pipe):
1544
1423
"""Dummy function; override as necessary"""
1545
1424
raise NotImplementedError
1548
1426
class IPv6_TCPServer(MultiprocessingMixInWithPipe,
1549
1427
socketserver.TCPServer, object):
1550
1428
"""IPv6-capable TCP server. Accepts 'None' as address and/or port
1634
1512
def server_activate(self):
1635
1513
if self.enabled:
1636
1514
return socketserver.TCPServer.server_activate(self)
1638
1515
def enable(self):
1639
1516
self.enabled = True
1641
def add_pipe(self, parent_pipe, proc):
1517
def add_pipe(self, parent_pipe):
1642
1518
# Call "handle_ipc" for both data and EOF events
1643
1519
gobject.io_add_watch(parent_pipe.fileno(),
1644
1520
gobject.IO_IN | gobject.IO_HUP,
1645
1521
functools.partial(self.handle_ipc,
1522
parent_pipe = parent_pipe))
1650
1524
def handle_ipc(self, source, condition, parent_pipe=None,
1651
proc = None, client_object=None):
1525
client_object=None):
1652
1526
condition_names = {
1653
1527
gobject.IO_IN: "IN", # There is data to read.
1654
1528
gobject.IO_OUT: "OUT", # Data can be written (without
1686
1558
"dress: %s", fpr, address)
1687
1559
if self.use_dbus:
1688
1560
# Emit D-Bus signal
1689
mandos_dbus_service.ClientNotFound(fpr,
1561
mandos_dbus_service.ClientNotFound(fpr, address[0])
1691
1562
parent_pipe.send(False)
1694
1565
gobject.io_add_watch(parent_pipe.fileno(),
1695
1566
gobject.IO_IN | gobject.IO_HUP,
1696
1567
functools.partial(self.handle_ipc,
1568
parent_pipe = parent_pipe,
1569
client_object = client))
1702
1570
parent_pipe.send(True)
1703
# 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
1706
1573
if command == 'funcall':
1707
1574
funcname = request[1]
1708
1575
args = request[2]
1709
1576
kwargs = request[3]
1711
parent_pipe.send(('data', getattr(client_object,
1578
parent_pipe.send(('data', getattr(client_object, funcname)(*args, **kwargs)))
1715
1580
if command == 'getattr':
1716
1581
attrname = request[1]
1717
1582
if callable(client_object.__getattribute__(attrname)):
1718
1583
parent_pipe.send(('function',))
1720
parent_pipe.send(('data', client_object
1721
.__getattribute__(attrname)))
1585
parent_pipe.send(('data', client_object.__getattribute__(attrname)))
1723
1587
if command == 'setattr':
1724
1588
attrname = request[1]
1725
1589
value = request[2]
1726
1590
setattr(client_object, attrname, value)