322
305
"host", "interval", "last_checked_ok",
323
306
"last_enabled", "name", "timeout")
309
def _timedelta_to_milliseconds(td):
310
"Convert a datetime.timedelta() to milliseconds"
311
return ((td.days * 24 * 60 * 60 * 1000)
312
+ (td.seconds * 1000)
313
+ (td.microseconds // 1000))
325
315
def timeout_milliseconds(self):
326
316
"Return the 'timeout' attribute in milliseconds"
327
return _timedelta_to_milliseconds(self.timeout)
329
def extended_timeout_milliseconds(self):
330
"Return the 'extended_timeout' attribute in milliseconds"
331
return _timedelta_to_milliseconds(self.extended_timeout)
317
return self._timedelta_to_milliseconds(self.timeout)
333
319
def interval_milliseconds(self):
334
320
"Return the 'interval' attribute in milliseconds"
335
return _timedelta_to_milliseconds(self.interval)
321
return self._timedelta_to_milliseconds(self.interval)
337
323
def approval_delay_milliseconds(self):
338
return _timedelta_to_milliseconds(self.approval_delay)
324
return self._timedelta_to_milliseconds(self.approval_delay)
340
326
def __init__(self, name = None, disable_hook=None, config=None):
341
327
"""Note: the 'checker' key in 'config' sets the
388
371
config["approval_delay"])
389
372
self.approval_duration = string_to_delta(
390
373
config["approval_duration"])
391
self.changedstate = (multiprocessing_manager
392
.Condition(multiprocessing_manager
374
self.changedstate = multiprocessing_manager.Condition(multiprocessing_manager.Lock())
395
376
def send_changedstate(self):
396
377
self.changedstate.acquire()
397
378
self.changedstate.notify_all()
398
379
self.changedstate.release()
400
381
def enable(self):
401
382
"""Start this client's checker and timeout hooks"""
402
383
if getattr(self, "enabled", False):
403
384
# Already enabled
405
386
self.send_changedstate()
387
self.last_enabled = datetime.datetime.utcnow()
406
388
# Schedule a new checker to be started an 'interval' from now,
407
389
# and every interval from then on.
408
390
self.checker_initiator_tag = (gobject.timeout_add
409
391
(self.interval_milliseconds(),
410
392
self.start_checker))
411
393
# Schedule a disable() when 'timeout' has passed
412
self.expires = datetime.datetime.utcnow() + self.timeout
413
394
self.disable_initiator_tag = (gobject.timeout_add
414
395
(self.timeout_milliseconds(),
416
397
self.enabled = True
417
self.last_enabled = datetime.datetime.utcnow()
418
398
# Also start a new checker *right now*.
419
399
self.start_checker()
461
440
logger.warning("Checker for %(name)s crashed?",
464
def checked_ok(self, timeout=None):
443
def checked_ok(self):
465
444
"""Bump up the timeout for this client.
467
446
This should only be called when the client has been seen,
471
timeout = self.timeout
472
449
self.last_checked_ok = datetime.datetime.utcnow()
473
if self.disable_initiator_tag is not None:
474
gobject.source_remove(self.disable_initiator_tag)
475
if getattr(self, "enabled", False):
476
self.disable_initiator_tag = (gobject.timeout_add
477
(_timedelta_to_milliseconds
478
(timeout), self.disable))
479
self.expires = datetime.datetime.utcnow() + timeout
450
gobject.source_remove(self.disable_initiator_tag)
451
self.disable_initiator_tag = (gobject.timeout_add
452
(self.timeout_milliseconds(),
481
455
def need_approval(self):
482
456
self.last_approval_request = datetime.datetime.utcnow()
639
612
def _get_all_dbus_properties(self):
640
613
"""Returns a generator of (name, attribute) pairs
642
return ((prop.__get__(self)._dbus_name, prop.__get__(self))
643
for cls in self.__class__.__mro__
615
return ((prop._dbus_name, prop)
644
616
for name, prop in
645
inspect.getmembers(cls, self._is_dbus_property))
617
inspect.getmembers(self, self._is_dbus_property))
647
619
def _get_dbus_property(self, interface_name, property_name):
648
620
"""Returns a bound method if one exists which is a D-Bus
649
621
property with the specified name and interface.
651
for cls in self.__class__.__mro__:
652
for name, value in (inspect.getmembers
653
(cls, self._is_dbus_property)):
654
if (value._dbus_name == property_name
655
and value._dbus_interface == interface_name):
656
return value.__get__(self)
623
for name in (property_name,
624
property_name + "_dbus_property"):
625
prop = getattr(self, name, None)
627
or not self._is_dbus_property(prop)
628
or prop._dbus_name != property_name
629
or (interface_name and prop._dbus_interface
630
and interface_name != prop._dbus_interface)):
658
633
# No such property
659
634
raise DBusPropertyNotFound(self.dbus_object_path + ":"
660
635
+ interface_name + "."
765
def datetime_to_dbus (dt, variant_level=0):
766
"""Convert a UTC datetime.datetime() to a D-Bus type."""
768
return dbus.String("", variant_level = variant_level)
769
return dbus.String(dt.isoformat(),
770
variant_level=variant_level)
772
class AlternateDBusNamesMetaclass(DBusObjectWithProperties
774
"""Applied to an empty subclass of a D-Bus object, this metaclass
775
will add additional D-Bus attributes matching a certain pattern.
777
def __new__(mcs, name, bases, attr):
778
# Go through all the base classes which could have D-Bus
779
# methods, signals, or properties in them
780
for base in (b for b in bases
781
if issubclass(b, dbus.service.Object)):
782
# Go though all attributes of the base class
783
for attrname, attribute in inspect.getmembers(base):
784
# Ignore non-D-Bus attributes, and D-Bus attributes
785
# with the wrong interface name
786
if (not hasattr(attribute, "_dbus_interface")
787
or not attribute._dbus_interface
788
.startswith("se.recompile.Mandos")):
790
# Create an alternate D-Bus interface name based on
792
alt_interface = (attribute._dbus_interface
793
.replace("se.recompile.Mandos",
794
"se.bsnet.fukt.Mandos"))
795
# Is this a D-Bus signal?
796
if getattr(attribute, "_dbus_is_signal", False):
797
# Extract the original non-method function by
799
nonmethod_func = (dict(
800
zip(attribute.func_code.co_freevars,
801
attribute.__closure__))["func"]
803
# Create a new, but exactly alike, function
804
# object, and decorate it to be a new D-Bus signal
805
# with the alternate D-Bus interface name
806
new_function = (dbus.service.signal
808
attribute._dbus_signature)
810
nonmethod_func.func_code,
811
nonmethod_func.func_globals,
812
nonmethod_func.func_name,
813
nonmethod_func.func_defaults,
814
nonmethod_func.func_closure)))
815
# Define a creator of a function to call both the
816
# old and new functions, so both the old and new
817
# signals gets sent when the function is called
818
def fixscope(func1, func2):
819
"""This function is a scope container to pass
820
func1 and func2 to the "call_both" function
821
outside of its arguments"""
822
def call_both(*args, **kwargs):
823
"""This function will emit two D-Bus
824
signals by calling func1 and func2"""
825
func1(*args, **kwargs)
826
func2(*args, **kwargs)
828
# Create the "call_both" function and add it to
830
attr[attrname] = fixscope(attribute,
832
# Is this a D-Bus method?
833
elif getattr(attribute, "_dbus_is_method", False):
834
# Create a new, but exactly alike, function
835
# object. Decorate it to be a new D-Bus method
836
# with the alternate D-Bus interface name. Add it
838
attr[attrname] = (dbus.service.method
840
attribute._dbus_in_signature,
841
attribute._dbus_out_signature)
843
(attribute.func_code,
844
attribute.func_globals,
846
attribute.func_defaults,
847
attribute.func_closure)))
848
# Is this a D-Bus property?
849
elif getattr(attribute, "_dbus_is_property", False):
850
# Create a new, but exactly alike, function
851
# object, and decorate it to be a new D-Bus
852
# property with the alternate D-Bus interface
853
# name. Add it to the class.
854
attr[attrname] = (dbus_service_property
856
attribute._dbus_signature,
857
attribute._dbus_access,
859
._dbus_get_args_options
862
(attribute.func_code,
863
attribute.func_globals,
865
attribute.func_defaults,
866
attribute.func_closure)))
867
return type.__new__(mcs, name, bases, attr)
869
740
class ClientDBus(Client, DBusObjectWithProperties):
870
741
"""A Client class using D-Bus
893
764
DBusObjectWithProperties.__init__(self, self.bus,
894
765
self.dbus_object_path)
896
def notifychangeproperty(transform_func,
897
dbus_name, type_func=lambda x: x,
899
""" Modify a variable so that it's a property which announces
767
def _get_approvals_pending(self):
768
return self._approvals_pending
769
def _set_approvals_pending(self, value):
770
old_value = self._approvals_pending
771
self._approvals_pending = value
773
if (hasattr(self, "dbus_object_path")
774
and bval is not bool(old_value)):
775
dbus_bool = dbus.Boolean(bval, variant_level=1)
776
self.PropertyChanged(dbus.String("ApprovalPending"),
902
transform_fun: Function that takes a value and a variant_level
903
and transforms it to a D-Bus type.
904
dbus_name: D-Bus name of the variable
905
type_func: Function that transform the value before sending it
906
to the D-Bus. Default: no transform
907
variant_level: D-Bus variant level. Default: 1
909
attrname = "_{0}".format(dbus_name)
910
def setter(self, value):
911
if hasattr(self, "dbus_object_path"):
912
if (not hasattr(self, attrname) or
913
type_func(getattr(self, attrname, None))
914
!= type_func(value)):
915
dbus_value = transform_func(type_func(value),
918
self.PropertyChanged(dbus.String(dbus_name),
920
setattr(self, attrname, value)
922
return property(lambda self: getattr(self, attrname), setter)
925
expires = notifychangeproperty(datetime_to_dbus, "Expires")
926
approvals_pending = notifychangeproperty(dbus.Boolean,
929
enabled = notifychangeproperty(dbus.Boolean, "Enabled")
930
last_enabled = notifychangeproperty(datetime_to_dbus,
932
checker = notifychangeproperty(dbus.Boolean, "CheckerRunning",
933
type_func = lambda checker:
935
last_checked_ok = notifychangeproperty(datetime_to_dbus,
937
last_approval_request = notifychangeproperty(
938
datetime_to_dbus, "LastApprovalRequest")
939
approved_by_default = notifychangeproperty(dbus.Boolean,
941
approval_delay = notifychangeproperty(dbus.UInt16,
944
_timedelta_to_milliseconds)
945
approval_duration = notifychangeproperty(
946
dbus.UInt16, "ApprovalDuration",
947
type_func = _timedelta_to_milliseconds)
948
host = notifychangeproperty(dbus.String, "Host")
949
timeout = notifychangeproperty(dbus.UInt16, "Timeout",
951
_timedelta_to_milliseconds)
952
extended_timeout = notifychangeproperty(
953
dbus.UInt16, "ExtendedTimeout",
954
type_func = _timedelta_to_milliseconds)
955
interval = notifychangeproperty(dbus.UInt16,
958
_timedelta_to_milliseconds)
959
checker_command = notifychangeproperty(dbus.String, "Checker")
961
del notifychangeproperty
779
approvals_pending = property(_get_approvals_pending,
780
_set_approvals_pending)
781
del _get_approvals_pending, _set_approvals_pending
784
def _datetime_to_dbus(dt, variant_level=0):
785
"""Convert a UTC datetime.datetime() to a D-Bus type."""
786
return dbus.String(dt.isoformat(),
787
variant_level=variant_level)
790
oldstate = getattr(self, "enabled", False)
791
r = Client.enable(self)
792
if oldstate != self.enabled:
794
self.PropertyChanged(dbus.String("Enabled"),
795
dbus.Boolean(True, variant_level=1))
796
self.PropertyChanged(
797
dbus.String("LastEnabled"),
798
self._datetime_to_dbus(self.last_enabled,
802
def disable(self, quiet = False):
803
oldstate = getattr(self, "enabled", False)
804
r = Client.disable(self, quiet=quiet)
805
if not quiet and oldstate != self.enabled:
807
self.PropertyChanged(dbus.String("Enabled"),
808
dbus.Boolean(False, variant_level=1))
963
811
def __del__(self, *args, **kwargs):
988
839
return Client.checker_callback(self, pid, condition, command,
842
def checked_ok(self, *args, **kwargs):
843
Client.checked_ok(self, *args, **kwargs)
845
self.PropertyChanged(
846
dbus.String("LastCheckedOK"),
847
(self._datetime_to_dbus(self.last_checked_ok,
850
def need_approval(self, *args, **kwargs):
851
r = Client.need_approval(self, *args, **kwargs)
853
self.PropertyChanged(
854
dbus.String("LastApprovalRequest"),
855
(self._datetime_to_dbus(self.last_approval_request,
991
859
def start_checker(self, *args, **kwargs):
992
860
old_checker = self.checker
993
861
if self.checker is not None:
1000
868
and old_checker_pid != self.checker.pid):
1001
869
# Emit D-Bus signal
1002
870
self.CheckerStarted(self.current_checker_command)
871
self.PropertyChanged(
872
dbus.String("CheckerRunning"),
873
dbus.Boolean(True, variant_level=1))
876
def stop_checker(self, *args, **kwargs):
877
old_checker = getattr(self, "checker", None)
878
r = Client.stop_checker(self, *args, **kwargs)
879
if (old_checker is not None
880
and getattr(self, "checker", None) is None):
881
self.PropertyChanged(dbus.String("CheckerRunning"),
882
dbus.Boolean(False, variant_level=1))
1005
885
def _reset_approved(self):
1006
886
self._approved = None
1115
998
if value is None: # get
1116
999
return dbus.UInt64(self.approval_delay_milliseconds())
1117
1000
self.approval_delay = datetime.timedelta(0, 0, 0, value)
1002
self.PropertyChanged(dbus.String("ApprovalDelay"),
1003
dbus.UInt64(value, variant_level=1))
1119
1005
# ApprovalDuration - property
1120
1006
@dbus_service_property(_interface, signature="t",
1121
1007
access="readwrite")
1122
1008
def ApprovalDuration_dbus_property(self, value=None):
1123
1009
if value is None: # get
1124
return dbus.UInt64(_timedelta_to_milliseconds(
1010
return dbus.UInt64(self._timedelta_to_milliseconds(
1125
1011
self.approval_duration))
1126
1012
self.approval_duration = datetime.timedelta(0, 0, 0, value)
1014
self.PropertyChanged(dbus.String("ApprovalDuration"),
1015
dbus.UInt64(value, variant_level=1))
1128
1017
# Name - property
1129
1018
@dbus_service_property(_interface, signature="s", access="read")
1142
1031
if value is None: # get
1143
1032
return dbus.String(self.host)
1144
1033
self.host = value
1035
self.PropertyChanged(dbus.String("Host"),
1036
dbus.String(value, variant_level=1))
1146
1038
# Created - property
1147
1039
@dbus_service_property(_interface, signature="s", access="read")
1148
1040
def Created_dbus_property(self):
1149
return dbus.String(datetime_to_dbus(self.created))
1041
return dbus.String(self._datetime_to_dbus(self.created))
1151
1043
# LastEnabled - property
1152
1044
@dbus_service_property(_interface, signature="s", access="read")
1153
1045
def LastEnabled_dbus_property(self):
1154
return datetime_to_dbus(self.last_enabled)
1046
if self.last_enabled is None:
1047
return dbus.String("")
1048
return dbus.String(self._datetime_to_dbus(self.last_enabled))
1156
1050
# Enabled - property
1157
1051
@dbus_service_property(_interface, signature="b",
1190
1086
if value is None: # get
1191
1087
return dbus.UInt64(self.timeout_milliseconds())
1192
1088
self.timeout = datetime.timedelta(0, 0, 0, value)
1090
self.PropertyChanged(dbus.String("Timeout"),
1091
dbus.UInt64(value, variant_level=1))
1193
1092
if getattr(self, "disable_initiator_tag", None) is None:
1195
1094
# Reschedule timeout
1196
1095
gobject.source_remove(self.disable_initiator_tag)
1197
1096
self.disable_initiator_tag = None
1199
time_to_die = _timedelta_to_milliseconds((self
1097
time_to_die = (self.
1098
_timedelta_to_milliseconds((self
1204
1103
if time_to_die <= 0:
1205
1104
# The timeout has passed
1208
self.expires = (datetime.datetime.utcnow()
1209
+ datetime.timedelta(milliseconds =
1211
1107
self.disable_initiator_tag = (gobject.timeout_add
1212
1108
(time_to_die, self.disable))
1214
# ExtendedTimeout - property
1215
@dbus_service_property(_interface, signature="t",
1217
def ExtendedTimeout_dbus_property(self, value=None):
1218
if value is None: # get
1219
return dbus.UInt64(self.extended_timeout_milliseconds())
1220
self.extended_timeout = datetime.timedelta(0, 0, 0, value)
1222
1110
# Interval - property
1223
1111
@dbus_service_property(_interface, signature="t",
1224
1112
access="readwrite")
1307
1200
unicode(self.client_address))
1308
1201
logger.debug("Pipe FD: %d",
1309
1202
self.server.child_pipe.fileno())
1311
1204
session = (gnutls.connection
1312
1205
.ClientSession(self.request,
1313
1206
gnutls.connection
1314
1207
.X509Credentials()))
1316
1209
# Note: gnutls.connection.X509Credentials is really a
1317
1210
# generic GnuTLS certificate credentials object so long as
1318
1211
# no X.509 keys are added to it. Therefore, we can use it
1319
1212
# here despite using OpenPGP certificates.
1321
1214
#priority = ':'.join(("NONE", "+VERS-TLS1.1",
1322
1215
# "+AES-256-CBC", "+SHA1",
1323
1216
# "+COMP-NULL", "+CTYPE-OPENPGP",
1540
1428
This function creates a new pipe in self.pipe
1542
1430
parent_pipe, self.child_pipe = multiprocessing.Pipe()
1544
proc = MultiprocessingMixIn.process_request(self, request,
1432
super(MultiprocessingMixInWithPipe,
1433
self).process_request(request, client_address)
1546
1434
self.child_pipe.close()
1547
self.add_pipe(parent_pipe, proc)
1549
def add_pipe(self, parent_pipe, proc):
1435
self.add_pipe(parent_pipe)
1437
def add_pipe(self, parent_pipe):
1550
1438
"""Dummy function; override as necessary"""
1551
1439
raise NotImplementedError
1554
1441
class IPv6_TCPServer(MultiprocessingMixInWithPipe,
1555
1442
socketserver.TCPServer, object):
1556
1443
"""IPv6-capable TCP server. Accepts 'None' as address and/or port
1640
1527
def server_activate(self):
1641
1528
if self.enabled:
1642
1529
return socketserver.TCPServer.server_activate(self)
1644
1530
def enable(self):
1645
1531
self.enabled = True
1647
def add_pipe(self, parent_pipe, proc):
1532
def add_pipe(self, parent_pipe):
1648
1533
# Call "handle_ipc" for both data and EOF events
1649
1534
gobject.io_add_watch(parent_pipe.fileno(),
1650
1535
gobject.IO_IN | gobject.IO_HUP,
1651
1536
functools.partial(self.handle_ipc,
1537
parent_pipe = parent_pipe))
1656
1539
def handle_ipc(self, source, condition, parent_pipe=None,
1657
proc = None, client_object=None):
1540
client_object=None):
1658
1541
condition_names = {
1659
1542
gobject.IO_IN: "IN", # There is data to read.
1660
1543
gobject.IO_OUT: "OUT", # Data can be written (without
1691
logger.info("Client not found for fingerprint: %s, ad"
1692
"dress: %s", fpr, address)
1572
logger.warning("Client not found for fingerprint: %s, ad"
1573
"dress: %s", fpr, address)
1693
1574
if self.use_dbus:
1694
1575
# Emit D-Bus signal
1695
mandos_dbus_service.ClientNotFound(fpr,
1576
mandos_dbus_service.ClientNotFound(fpr, address[0])
1697
1577
parent_pipe.send(False)
1700
1580
gobject.io_add_watch(parent_pipe.fileno(),
1701
1581
gobject.IO_IN | gobject.IO_HUP,
1702
1582
functools.partial(self.handle_ipc,
1583
parent_pipe = parent_pipe,
1584
client_object = client))
1708
1585
parent_pipe.send(True)
1709
# remove the old hook in favor of the new above hook on
1586
# remove the old hook in favor of the new above hook on same fileno
1712
1588
if command == 'funcall':
1713
1589
funcname = request[1]
1714
1590
args = request[2]
1715
1591
kwargs = request[3]
1717
parent_pipe.send(('data', getattr(client_object,
1593
parent_pipe.send(('data', getattr(client_object, funcname)(*args, **kwargs)))
1721
1595
if command == 'getattr':
1722
1596
attrname = request[1]
1723
1597
if callable(client_object.__getattribute__(attrname)):
1724
1598
parent_pipe.send(('function',))
1726
parent_pipe.send(('data', client_object
1727
.__getattribute__(attrname)))
1600
parent_pipe.send(('data', client_object.__getattribute__(attrname)))
1729
1602
if command == 'setattr':
1730
1603
attrname = request[1]
1731
1604
value = request[2]
1732
1605
setattr(client_object, attrname, value)
2018
1891
# End of Avahi example code
2021
bus_name = dbus.service.BusName("se.recompile.Mandos",
1894
bus_name = dbus.service.BusName("se.bsnet.fukt.Mandos",
2022
1895
bus, do_not_queue=True)
2023
old_bus_name = (dbus.service.BusName
2024
("se.bsnet.fukt.Mandos", bus,
2026
1896
except dbus.exceptions.NameExistsException as e:
2027
1897
logger.error(unicode(e) + ", disabling D-Bus")
2028
1898
use_dbus = False
2029
1899
server_settings["use_dbus"] = False
2030
1900
tcp_server.use_dbus = False
2031
1901
protocol = avahi.PROTO_INET6 if use_ipv6 else avahi.PROTO_INET
2032
service = AvahiServiceToSyslog(name =
2033
server_settings["servicename"],
2034
servicetype = "_mandos._tcp",
2035
protocol = protocol, bus = bus)
1902
service = AvahiService(name = server_settings["servicename"],
1903
servicetype = "_mandos._tcp",
1904
protocol = protocol, bus = bus)
2036
1905
if server_settings["interface"]:
2037
1906
service.interface = (if_nametoindex
2038
1907
(str(server_settings["interface"])))