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)
459
464
if timeout is None:
460
465
timeout = self.timeout
461
466
self.last_checked_ok = datetime.datetime.utcnow()
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),
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
468
475
def need_approval(self):
469
476
self.last_approval_request = datetime.datetime.utcnow()
625
633
def _get_all_dbus_properties(self):
626
634
"""Returns a generator of (name, attribute) pairs
628
return ((prop._dbus_name, prop)
636
return ((prop.__get__(self)._dbus_name, prop.__get__(self))
637
for cls in self.__class__.__mro__
629
638
for name, prop in
630
inspect.getmembers(self, self._is_dbus_property))
639
inspect.getmembers(cls, self._is_dbus_property))
632
641
def _get_dbus_property(self, interface_name, property_name):
633
642
"""Returns a bound method if one exists which is a D-Bus
634
643
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)):
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)
646
652
# No such property
647
653
raise DBusPropertyNotFound(self.dbus_object_path + ":"
648
654
+ interface_name + "."
757
763
return dbus.String(dt.isoformat(),
758
764
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)
760
863
class ClientDBus(Client, DBusObjectWithProperties):
761
864
"""A Client class using D-Bus
787
890
def notifychangeproperty(transform_func,
788
891
dbus_name, type_func=lambda x: x,
789
892
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
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
795
899
type_func: Function that transform the value before sending it
797
variant_level: DBus variant level. default: 1
900
to the D-Bus. Default: no transform
901
variant_level: D-Bus variant level. Default: 1
903
attrname = "_{0}".format(dbus_name)
800
904
def setter(self, value):
801
old_value = real_value[0]
802
real_value[0] = value
803
905
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]),
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),
807
912
self.PropertyChanged(dbus.String(dbus_name),
810
return property(lambda self: real_value[0], setter)
914
setattr(self, attrname, value)
916
return property(lambda self: getattr(self, attrname), setter)
813
919
expires = notifychangeproperty(datetime_to_dbus, "Expires")
814
920
approvals_pending = notifychangeproperty(dbus.Boolean,
815
921
"ApprovalPending",
818
924
last_enabled = notifychangeproperty(datetime_to_dbus,
820
926
checker = notifychangeproperty(dbus.Boolean, "CheckerRunning",
821
type_func = lambda checker: checker is not None)
927
type_func = lambda checker:
822
929
last_checked_ok = notifychangeproperty(datetime_to_dbus,
824
last_approval_request = notifychangeproperty(datetime_to_dbus,
825
"LastApprovalRequest")
931
last_approval_request = notifychangeproperty(
932
datetime_to_dbus, "LastApprovalRequest")
826
933
approved_by_default = notifychangeproperty(dbus.Boolean,
827
934
"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)
935
approval_delay = notifychangeproperty(dbus.UInt16,
938
_timedelta_to_milliseconds)
939
approval_duration = notifychangeproperty(
940
dbus.UInt16, "ApprovalDuration",
941
type_func = _timedelta_to_milliseconds)
832
942
host = notifychangeproperty(dbus.String, "Host")
833
943
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)
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)
839
953
checker_command = notifychangeproperty(dbus.String, "Checker")
841
955
del notifychangeproperty
1185
1301
unicode(self.client_address))
1186
1302
logger.debug("Pipe FD: %d",
1187
1303
self.server.child_pipe.fileno())
1189
1305
session = (gnutls.connection
1190
1306
.ClientSession(self.request,
1191
1307
gnutls.connection
1192
1308
.X509Credentials()))
1194
1310
# Note: gnutls.connection.X509Credentials is really a
1195
1311
# generic GnuTLS certificate credentials object so long as
1196
1312
# no X.509 keys are added to it. Therefore, we can use it
1197
1313
# here despite using OpenPGP certificates.
1199
1315
#priority = ':'.join(("NONE", "+VERS-TLS1.1",
1200
1316
# "+AES-256-CBC", "+SHA1",
1201
1317
# "+COMP-NULL", "+CTYPE-OPENPGP",
1413
1534
This function creates a new pipe in self.pipe
1415
1536
parent_pipe, self.child_pipe = multiprocessing.Pipe()
1417
super(MultiprocessingMixInWithPipe,
1418
self).process_request(request, client_address)
1538
proc = MultiprocessingMixIn.process_request(self, request,
1419
1540
self.child_pipe.close()
1420
self.add_pipe(parent_pipe)
1422
def add_pipe(self, parent_pipe):
1541
self.add_pipe(parent_pipe, proc)
1543
def add_pipe(self, parent_pipe, proc):
1423
1544
"""Dummy function; override as necessary"""
1424
1545
raise NotImplementedError
1426
1548
class IPv6_TCPServer(MultiprocessingMixInWithPipe,
1427
1549
socketserver.TCPServer, object):
1428
1550
"""IPv6-capable TCP server. Accepts 'None' as address and/or port
1512
1634
def server_activate(self):
1513
1635
if self.enabled:
1514
1636
return socketserver.TCPServer.server_activate(self)
1515
1638
def enable(self):
1516
1639
self.enabled = True
1517
def add_pipe(self, parent_pipe):
1641
def add_pipe(self, parent_pipe, proc):
1518
1642
# Call "handle_ipc" for both data and EOF events
1519
1643
gobject.io_add_watch(parent_pipe.fileno(),
1520
1644
gobject.IO_IN | gobject.IO_HUP,
1521
1645
functools.partial(self.handle_ipc,
1522
parent_pipe = parent_pipe))
1524
1650
def handle_ipc(self, source, condition, parent_pipe=None,
1525
client_object=None):
1651
proc = None, client_object=None):
1526
1652
condition_names = {
1527
1653
gobject.IO_IN: "IN", # There is data to read.
1528
1654
gobject.IO_OUT: "OUT", # Data can be written (without
1558
1686
"dress: %s", fpr, address)
1559
1687
if self.use_dbus:
1560
1688
# Emit D-Bus signal
1561
mandos_dbus_service.ClientNotFound(fpr, address[0])
1689
mandos_dbus_service.ClientNotFound(fpr,
1562
1691
parent_pipe.send(False)
1565
1694
gobject.io_add_watch(parent_pipe.fileno(),
1566
1695
gobject.IO_IN | gobject.IO_HUP,
1567
1696
functools.partial(self.handle_ipc,
1568
parent_pipe = parent_pipe,
1569
client_object = client))
1570
1702
parent_pipe.send(True)
1571
# remove the old hook in favor of the new above hook on same fileno
1703
# remove the old hook in favor of the new above hook on
1573
1706
if command == 'funcall':
1574
1707
funcname = request[1]
1575
1708
args = request[2]
1576
1709
kwargs = request[3]
1578
parent_pipe.send(('data', getattr(client_object, funcname)(*args, **kwargs)))
1711
parent_pipe.send(('data', getattr(client_object,
1580
1715
if command == 'getattr':
1581
1716
attrname = request[1]
1582
1717
if callable(client_object.__getattribute__(attrname)):
1583
1718
parent_pipe.send(('function',))
1585
parent_pipe.send(('data', client_object.__getattribute__(attrname)))
1720
parent_pipe.send(('data', client_object
1721
.__getattribute__(attrname)))
1587
1723
if command == 'setattr':
1588
1724
attrname = request[1]
1589
1725
value = request[2]
1590
1726
setattr(client_object, attrname, value)