395
395
logger.error(bad_states[state] + ": %r", error)
397
397
elif state == avahi.SERVER_RUNNING:
400
except dbus.exceptions.DBusException as error:
401
if (error.get_dbus_name()
402
== "org.freedesktop.Avahi.CollisionError"):
403
logger.info("Local Zeroconf service name"
405
return self.rename(remove=False)
407
logger.critical("D-Bus Exception", exc_info=error)
400
411
if error is None:
401
412
logger.debug("Unknown state: %r", state)
861
class DBusObjectWithProperties(dbus.service.Object):
862
"""A D-Bus object with properties.
874
class DBusObjectWithAnnotations(dbus.service.Object):
875
"""A D-Bus object with annotations.
864
Classes inheriting from this can use the dbus_service_property
865
decorator to expose methods as D-Bus properties. It exposes the
866
standard Get(), Set(), and GetAll() methods on the D-Bus.
877
Classes inheriting from this can use the dbus_annotations
878
decorator to add annotations to methods or signals.
885
897
for name, athing in
886
898
inspect.getmembers(cls, self._is_dbus_thing(thing)))
900
@dbus.service.method(dbus.INTROSPECTABLE_IFACE,
902
path_keyword = 'object_path',
903
connection_keyword = 'connection')
904
def Introspect(self, object_path, connection):
905
"""Overloading of standard D-Bus method.
907
Inserts annotation tags on methods and signals.
909
xmlstring = dbus.service.Object.Introspect(self, object_path,
912
document = xml.dom.minidom.parseString(xmlstring)
914
for if_tag in document.getElementsByTagName("interface"):
915
# Add annotation tags
916
for typ in ("method", "signal"):
917
for tag in if_tag.getElementsByTagName(typ):
919
for name, prop in (self.
920
_get_all_dbus_things(typ)):
921
if (name == tag.getAttribute("name")
922
and prop._dbus_interface
923
== if_tag.getAttribute("name")):
924
annots.update(getattr(
925
prop, "_dbus_annotations", {}))
926
for name, value in annots.items():
927
ann_tag = document.createElement(
929
ann_tag.setAttribute("name", name)
930
ann_tag.setAttribute("value", value)
931
tag.appendChild(ann_tag)
932
# Add interface annotation tags
933
for annotation, value in dict(
934
itertools.chain.from_iterable(
935
annotations().items()
936
for name, annotations
937
in self._get_all_dbus_things("interface")
938
if name == if_tag.getAttribute("name")
940
ann_tag = document.createElement("annotation")
941
ann_tag.setAttribute("name", annotation)
942
ann_tag.setAttribute("value", value)
943
if_tag.appendChild(ann_tag)
944
# Fix argument name for the Introspect method itself
945
if (if_tag.getAttribute("name")
946
== dbus.INTROSPECTABLE_IFACE):
947
for cn in if_tag.getElementsByTagName("method"):
948
if cn.getAttribute("name") == "Introspect":
949
for arg in cn.getElementsByTagName("arg"):
950
if (arg.getAttribute("direction")
952
arg.setAttribute("name",
954
xmlstring = document.toxml("utf-8")
956
except (AttributeError, xml.dom.DOMException,
957
xml.parsers.expat.ExpatError) as error:
958
logger.error("Failed to override Introspection method",
963
class DBusObjectWithProperties(DBusObjectWithAnnotations):
964
"""A D-Bus object with properties.
966
Classes inheriting from this can use the dbus_service_property
967
decorator to expose methods as D-Bus properties. It exposes the
968
standard Get(), Set(), and GetAll() methods on the D-Bus.
888
971
def _get_dbus_property(self, interface_name, property_name):
889
972
"""Returns a bound method if one exists which is a D-Bus
890
973
property with the specified name and interface.
900
983
raise DBusPropertyNotFound("{}:{}.{}".format(
901
984
self.dbus_object_path, interface_name, property_name))
987
def _get_all_interface_names(cls):
988
"""Get a sequence of all interfaces supported by an object"""
989
return (name for name in set(getattr(getattr(x, attr),
990
"_dbus_interface", None)
991
for x in (inspect.getmro(cls))
903
995
@dbus.service.method(dbus.PROPERTIES_IFACE,
904
996
in_signature="ss",
905
997
out_signature="v")
995
1088
if prop._dbus_interface
996
1089
== if_tag.getAttribute("name")):
997
1090
if_tag.appendChild(tag)
998
# Add annotation tags
999
for typ in ("method", "signal", "property"):
1000
for tag in if_tag.getElementsByTagName(typ):
1002
for name, prop in (self.
1003
_get_all_dbus_things(typ)):
1004
if (name == tag.getAttribute("name")
1005
and prop._dbus_interface
1006
== if_tag.getAttribute("name")):
1007
annots.update(getattr(
1008
prop, "_dbus_annotations", {}))
1009
for name, value in annots.items():
1010
ann_tag = document.createElement(
1012
ann_tag.setAttribute("name", name)
1013
ann_tag.setAttribute("value", value)
1014
tag.appendChild(ann_tag)
1015
# Add interface annotation tags
1016
for annotation, value in dict(
1017
itertools.chain.from_iterable(
1018
annotations().items()
1019
for name, annotations
1020
in self._get_all_dbus_things("interface")
1021
if name == if_tag.getAttribute("name")
1023
ann_tag = document.createElement("annotation")
1024
ann_tag.setAttribute("name", annotation)
1025
ann_tag.setAttribute("value", value)
1026
if_tag.appendChild(ann_tag)
1091
# Add annotation tags for properties
1092
for tag in if_tag.getElementsByTagName("property"):
1094
for name, prop in self._get_all_dbus_things(
1096
if (name == tag.getAttribute("name")
1097
and prop._dbus_interface
1098
== if_tag.getAttribute("name")):
1099
annots.update(getattr(
1100
prop, "_dbus_annotations", {}))
1101
for name, value in annots.items():
1102
ann_tag = document.createElement(
1104
ann_tag.setAttribute("name", name)
1105
ann_tag.setAttribute("value", value)
1106
tag.appendChild(ann_tag)
1027
1107
# Add the names to the return values for the
1028
1108
# "org.freedesktop.DBus.Properties" methods
1029
1109
if (if_tag.getAttribute("name")
1047
1127
exc_info=error)
1048
1128
return xmlstring
1131
dbus.OBJECT_MANAGER_IFACE
1132
except AttributeError:
1133
dbus.OBJECT_MANAGER_IFACE = "org.freedesktop.DBus.ObjectManager"
1135
class DBusObjectWithObjectManager(DBusObjectWithAnnotations):
1136
"""A D-Bus object with an ObjectManager.
1138
Classes inheriting from this exposes the standard
1139
GetManagedObjects call and the InterfacesAdded and
1140
InterfacesRemoved signals on the standard
1141
"org.freedesktop.DBus.ObjectManager" interface.
1143
Note: No signals are sent automatically; they must be sent
1146
@dbus.service.method(dbus.OBJECT_MANAGER_IFACE,
1147
out_signature = "a{oa{sa{sv}}}")
1148
def GetManagedObjects(self):
1149
"""This function must be overridden"""
1150
raise NotImplementedError()
1152
@dbus.service.signal(dbus.OBJECT_MANAGER_IFACE,
1153
signature = "oa{sa{sv}}")
1154
def InterfacesAdded(self, object_path, interfaces_and_properties):
1157
@dbus.service.signal(dbus.OBJECT_MANAGER_IFACE,
1159
def InterfacesRemoved(self, object_path, interfaces):
1162
@dbus.service.method(dbus.INTROSPECTABLE_IFACE,
1163
out_signature = "s",
1164
path_keyword = 'object_path',
1165
connection_keyword = 'connection')
1166
def Introspect(self, object_path, connection):
1167
"""Overloading of standard D-Bus method.
1169
Override return argument name of GetManagedObjects to be
1170
"objpath_interfaces_and_properties"
1172
xmlstring = DBusObjectWithAnnotations(self, object_path,
1175
document = xml.dom.minidom.parseString(xmlstring)
1177
for if_tag in document.getElementsByTagName("interface"):
1178
# Fix argument name for the GetManagedObjects method
1179
if (if_tag.getAttribute("name")
1180
== dbus.OBJECT_MANAGER_IFACE):
1181
for cn in if_tag.getElementsByTagName("method"):
1182
if (cn.getAttribute("name")
1183
== "GetManagedObjects"):
1184
for arg in cn.getElementsByTagName("arg"):
1185
if (arg.getAttribute("direction")
1189
"objpath_interfaces"
1191
xmlstring = document.toxml("utf-8")
1193
except (AttributeError, xml.dom.DOMException,
1194
xml.parsers.expat.ExpatError) as error:
1195
logger.error("Failed to override Introspection method",
1051
1199
def datetime_to_dbus(dt, variant_level=0):
1052
1200
"""Convert a UTC datetime.datetime() to a D-Bus type."""
1365
1513
if exitstatus >= 0:
1366
1514
# Emit D-Bus signal
1367
1515
self.CheckerCompleted(dbus.Int16(exitstatus),
1516
# This is specific to GNU libC
1517
dbus.Int64(exitstatus << 8),
1369
1518
dbus.String(command))
1371
1520
# Emit D-Bus signal
1372
1521
self.CheckerCompleted(dbus.Int16(-1),
1374
self.last_checker_signal),
1523
# This is specific to GNU libC
1525
| self.last_checker_signal),
1375
1526
dbus.String(command))
1513
1668
self.approval_duration = datetime.timedelta(0, 0, 0, value)
1515
1670
# Name - property
1672
{"org.freedesktop.DBus.Property.EmitsChangedSignal": "const"})
1516
1673
@dbus_service_property(_interface, signature="s", access="read")
1517
1674
def Name_dbus_property(self):
1518
1675
return dbus.String(self.name)
1520
1677
# Fingerprint - property
1679
{"org.freedesktop.DBus.Property.EmitsChangedSignal": "const"})
1521
1680
@dbus_service_property(_interface, signature="s", access="read")
1522
1681
def Fingerprint_dbus_property(self):
1523
1682
return dbus.String(self.fingerprint)
1652
1813
self.stop_checker()
1654
1815
# ObjectPath - property
1817
{"org.freedesktop.DBus.Property.EmitsChangedSignal": "const",
1818
"org.freedesktop.DBus.Deprecated": "true"})
1655
1819
@dbus_service_property(_interface, signature="o", access="read")
1656
1820
def ObjectPath_dbus_property(self):
1657
1821
return self.dbus_object_path # is already a dbus.ObjectPath
1659
1823
# Secret = property
1825
{"org.freedesktop.DBus.Property.EmitsChangedSignal":
1660
1827
@dbus_service_property(_interface,
1661
1828
signature="ay",
1662
1829
access="write",
2911
@dbus_annotations({"org.freedesktop.DBus.Deprecated":
2750
2913
@dbus.service.signal(_interface, signature="os")
2751
2914
def ClientRemoved(self, objpath, name):
2918
@dbus_annotations({"org.freedesktop.DBus.Deprecated":
2755
2920
@dbus.service.method(_interface, out_signature="ao")
2756
2921
def GetAllClients(self):
2758
2923
return dbus.Array(c.dbus_object_path for c in
2759
2924
tcp_server.clients.itervalues())
2926
@dbus_annotations({"org.freedesktop.DBus.Deprecated":
2761
2928
@dbus.service.method(_interface,
2762
2929
out_signature="a{oa{sv}}")
2763
2930
def GetAllClientsWithProperties(self):
2765
2932
return dbus.Dictionary(
2766
{ c.dbus_object_path: c.GetAll("")
2933
{ c.dbus_object_path: c.GetAll(
2934
"se.recompile.Mandos.Client")
2767
2935
for c in tcp_server.clients.itervalues() },
2768
2936
signature="oa{sv}")
2774
2942
if c.dbus_object_path == object_path:
2775
2943
del tcp_server.clients[c.name]
2776
2944
c.remove_from_connection()
2777
# Don't signal anything except ClientRemoved
2945
# Don't signal the disabling
2778
2946
c.disable(quiet=True)
2780
self.ClientRemoved(object_path, c.name)
2947
# Emit D-Bus signal for removal
2948
self.client_removed_signal(c)
2782
2950
raise KeyError(object_path)
2954
@dbus.service.method(dbus.OBJECT_MANAGER_IFACE,
2955
out_signature = "a{oa{sa{sv}}}")
2956
def GetManagedObjects(self):
2958
return dbus.Dictionary(
2959
{ client.dbus_object_path:
2961
{ interface: client.GetAll(interface)
2963
client._get_all_interface_names()})
2964
for client in tcp_server.clients.values()})
2966
def client_added_signal(self, client):
2967
"""Send the new standard signal and the old signal"""
2969
# New standard signal
2970
self.InterfacesAdded(
2971
client.dbus_object_path,
2973
{ interface: client.GetAll(interface)
2975
client._get_all_interface_names()}))
2977
self.ClientAdded(client.dbus_object_path)
2979
def client_removed_signal(self, client):
2980
"""Send the new standard signal and the old signal"""
2982
# New standard signal
2983
self.InterfacesRemoved(
2984
client.dbus_object_path,
2985
client._get_all_interface_names())
2987
self.ClientRemoved(client.dbus_object_path,
2786
2990
mandos_dbus_service = MandosDBusService()
2852
3056
name, client = tcp_server.clients.popitem()
2854
3058
client.remove_from_connection()
2855
# Don't signal anything except ClientRemoved
3059
# Don't signal the disabling
2856
3060
client.disable(quiet=True)
2859
mandos_dbus_service.ClientRemoved(
2860
client.dbus_object_path, client.name)
3061
# Emit D-Bus signal for removal
3062
mandos_dbus_service.client_removed_signal(client)
2861
3063
client_settings.clear()
2863
3065
atexit.register(cleanup)
2865
3067
for client in tcp_server.clients.itervalues():
2868
mandos_dbus_service.ClientAdded(client.dbus_object_path)
3069
# Emit D-Bus signal for adding
3070
mandos_dbus_service.client_added_signal(client)
2869
3071
# Need to initiate checking of clients
2870
3072
if client.enabled:
2871
3073
client.init_checker()