/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos

  • Committer: Teddy Hogeborn
  • Date: 2015-08-10 09:00:23 UTC
  • Revision ID: teddy@recompile.se-20150810090023-fz6vjqr7zf33e2tf
Support the standard org.freedesktop.DBus.ObjectManager interface.

Now that the D-Bus standard has an interface to keep track of new and
removed objects, use that instead of our own methods.  This deprecates
our D-Bus methods "GetAllClients" and "GetAllClientsWithProperties"
and the signals "ClientAdded" and "ClientRemoved", all on the server
interface "se.recompile.Mandos".

* DBUS-API: Removed references to deprecated methods and signals;
  insert reference to the org.freedesktop.DBus.ObjectManager
  interface.
* mandos (DBusObjectWithProperties._get_all_interface_names): New.
  (dbus.OBJECT_MANAGER_IFACE): If not present, monkey patch.
  (DBusObjectWithObjectManager): New.
  (main/MandosDBusService): Inherit from DBusObjectWithObjectManager.
  (main/MandosDBusService.ClientRemoved): Annotate as deprecated.
  (main/MandosDBusService.GetAllClients): - '' -
  (main/MandosDBusService.GetAllClientsWithProperties): Annotate as
                                                        deprecated.
                                                        Also only
                                                        return
                                                        properties on
                                                        client
                                                        interface.
  (main/MandosDBusService.RemoveClient): Call client_removed_signal
                                         instead of ClientRemoved.
  (main/MandosDBusService.GetManagedObjects): New.
  (main/MandosDBusService.client_added_signal): New.
  (main/MandosDBusService.client_removed_signal): - '' -
  (main/cleanup): Call "client_removed_signal" instead of sending
                  "ClientRemoved" signal directly.
  (main): Call "client_added_signal" instead of sending "ClientAdded"
          signal directly.
* mandos-ctl: Use GetManagedObjects instead of
              GetAllClientsWithProperties.  Also, show better error
              message in case of failure to connect to the D-Bus

* mandos-monitor (MandosClientPropertyCache.properties_changed):
  Bug fix; only update properties on client interface.
  (UserInterface.find_and_remove_client): Change to accept arguments
                                          from InterfacesRemoved
                                          signal.  Also, bug fix:
                                          working error message when
                                          removing unknown client.
  (UserInterface.add_new_client): Change to accept arguments from
                                  InterfacesRemoved signal.  Pass
                                  properties to MandosClientWidget
                                  constructor.
  (UserInterface.run): Connect find_and_remove_client method to
                       InterfacesRemoved signal and the add_new_client
                       method to the InterfacesAdded signal.

Show diffs side-by-side

added added

removed removed

Lines of Context:
983
983
        raise DBusPropertyNotFound("{}:{}.{}".format(
984
984
            self.dbus_object_path, interface_name, property_name))
985
985
    
 
986
    @classmethod
 
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))
 
992
                                     for attr in dir(x))
 
993
                if name is not None)
 
994
    
986
995
    @dbus.service.method(dbus.PROPERTIES_IFACE,
987
996
                         in_signature="ss",
988
997
                         out_signature="v")
1118
1127
                         exc_info=error)
1119
1128
        return xmlstring
1120
1129
 
 
1130
try:
 
1131
    dbus.OBJECT_MANAGER_IFACE
 
1132
except AttributeError:
 
1133
    dbus.OBJECT_MANAGER_IFACE = "org.freedesktop.DBus.ObjectManager"
 
1134
 
 
1135
class DBusObjectWithObjectManager(DBusObjectWithAnnotations):
 
1136
    """A D-Bus object with an ObjectManager.
 
1137
    
 
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.
 
1142
    
 
1143
    Note: No signals are sent automatically; they must be sent
 
1144
    manually.
 
1145
    """
 
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()
 
1151
    
 
1152
    @dbus.service.signal(dbus.OBJECT_MANAGER_IFACE,
 
1153
                         signature = "oa{sa{sv}}")
 
1154
    def InterfacesAdded(self, object_path, interfaces_and_properties):
 
1155
        pass
 
1156
    
 
1157
    @dbus.service.signal(dbus.OBJECT_MANAGER_IFACE,
 
1158
                         signature = "oas")
 
1159
    def InterfacesRemoved(self, object_path, interfaces):
 
1160
        pass
 
1161
    
 
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.
 
1168
        
 
1169
        Override return argument name of GetManagedObjects to be
 
1170
        "objpath_interfaces_and_properties"
 
1171
        """
 
1172
        xmlstring = DBusObjectWithAnnotations(self, object_path,
 
1173
                                              connection)
 
1174
        try:
 
1175
            document = xml.dom.minidom.parseString(xmlstring)
 
1176
            
 
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")
 
1186
                                    == "out"):
 
1187
                                    arg.setAttribute(
 
1188
                                        "name",
 
1189
                                        "objpath_interfaces"
 
1190
                                        "_and_properties")
 
1191
            xmlstring = document.toxml("utf-8")
 
1192
            document.unlink()
 
1193
        except (AttributeError, xml.dom.DOMException,
 
1194
                xml.parsers.expat.ExpatError) as error:
 
1195
            logger.error("Failed to override Introspection method",
 
1196
                         exc_info = error)
 
1197
        return xmlstring
1121
1198
 
1122
1199
def datetime_to_dbus(dt, variant_level=0):
1123
1200
    """Convert a UTC datetime.datetime() to a D-Bus type."""
2813
2890
        
2814
2891
        @alternate_dbus_interfaces(
2815
2892
            { "se.recompile.Mandos": "se.bsnet.fukt.Mandos" })
2816
 
        class MandosDBusService(DBusObjectWithAnnotations):
 
2893
        class MandosDBusService(DBusObjectWithObjectManager):
2817
2894
            """A D-Bus proxy object"""
2818
2895
            
2819
2896
            def __init__(self):
2831
2908
                "D-Bus signal"
2832
2909
                pass
2833
2910
            
 
2911
            @dbus_annotations({"org.freedesktop.DBus.Deprecated":
 
2912
                               "true"})
2834
2913
            @dbus.service.signal(_interface, signature="os")
2835
2914
            def ClientRemoved(self, objpath, name):
2836
2915
                "D-Bus signal"
2837
2916
                pass
2838
2917
            
 
2918
            @dbus_annotations({"org.freedesktop.DBus.Deprecated":
 
2919
                               "true"})
2839
2920
            @dbus.service.method(_interface, out_signature="ao")
2840
2921
            def GetAllClients(self):
2841
2922
                "D-Bus method"
2842
2923
                return dbus.Array(c.dbus_object_path for c in
2843
2924
                                  tcp_server.clients.itervalues())
2844
2925
            
 
2926
            @dbus_annotations({"org.freedesktop.DBus.Deprecated":
 
2927
                               "true"})
2845
2928
            @dbus.service.method(_interface,
2846
2929
                                 out_signature="a{oa{sv}}")
2847
2930
            def GetAllClientsWithProperties(self):
2848
2931
                "D-Bus method"
2849
2932
                return dbus.Dictionary(
2850
 
                    { c.dbus_object_path: c.GetAll("")
 
2933
                    { c.dbus_object_path: c.GetAll(
 
2934
                        "se.recompile.Mandos.Client")
2851
2935
                      for c in tcp_server.clients.itervalues() },
2852
2936
                    signature="oa{sv}")
2853
2937
            
2858
2942
                    if c.dbus_object_path == object_path:
2859
2943
                        del tcp_server.clients[c.name]
2860
2944
                        c.remove_from_connection()
2861
 
                        # Don't signal anything except ClientRemoved
 
2945
                        # Don't signal the disabling
2862
2946
                        c.disable(quiet=True)
2863
 
                        # Emit D-Bus signal
2864
 
                        self.ClientRemoved(object_path, c.name)
 
2947
                        # Emit D-Bus signal for removal
 
2948
                        self.client_removed_signal(c)
2865
2949
                        return
2866
2950
                raise KeyError(object_path)
2867
2951
            
2868
2952
            del _interface
 
2953
            
 
2954
            @dbus.service.method(dbus.OBJECT_MANAGER_IFACE,
 
2955
                                 out_signature = "a{oa{sa{sv}}}")
 
2956
            def GetManagedObjects(self):
 
2957
                """D-Bus method"""
 
2958
                return dbus.Dictionary(
 
2959
                    { client.dbus_object_path:
 
2960
                      dbus.Dictionary(
 
2961
                          { interface: client.GetAll(interface)
 
2962
                            for interface in
 
2963
                                 client._get_all_interface_names()})
 
2964
                      for client in tcp_server.clients.values()})
 
2965
            
 
2966
            def client_added_signal(self, client):
 
2967
                """Send the new standard signal and the old signal"""
 
2968
                if use_dbus:
 
2969
                    # New standard signal
 
2970
                    self.InterfacesAdded(
 
2971
                        client.dbus_object_path,
 
2972
                        dbus.Dictionary(
 
2973
                            { interface: client.GetAll(interface)
 
2974
                              for interface in
 
2975
                              client._get_all_interface_names()}))
 
2976
                    # Old signal
 
2977
                    self.ClientAdded(client.dbus_object_path)
 
2978
            
 
2979
            def client_removed_signal(self, client):
 
2980
                """Send the new standard signal and the old signal"""
 
2981
                if use_dbus:
 
2982
                    # New standard signal
 
2983
                    self.InterfacesRemoved(
 
2984
                        client.dbus_object_path,
 
2985
                        client._get_all_interface_names())
 
2986
                    # Old signal
 
2987
                    self.ClientRemoved(client.dbus_object_path,
 
2988
                                       client.name)
2869
2989
        
2870
2990
        mandos_dbus_service = MandosDBusService()
2871
2991
    
2936
3056
            name, client = tcp_server.clients.popitem()
2937
3057
            if use_dbus:
2938
3058
                client.remove_from_connection()
2939
 
            # Don't signal anything except ClientRemoved
 
3059
            # Don't signal the disabling
2940
3060
            client.disable(quiet=True)
2941
 
            if use_dbus:
2942
 
                # Emit D-Bus signal
2943
 
                mandos_dbus_service.ClientRemoved(
2944
 
                    client.dbus_object_path, client.name)
 
3061
            # Emit D-Bus signal for removal
 
3062
            mandos_dbus_service.client_removed_signal(client)
2945
3063
        client_settings.clear()
2946
3064
    
2947
3065
    atexit.register(cleanup)
2948
3066
    
2949
3067
    for client in tcp_server.clients.itervalues():
2950
3068
        if use_dbus:
2951
 
            # Emit D-Bus signal
2952
 
            mandos_dbus_service.ClientAdded(client.dbus_object_path)
 
3069
            # Emit D-Bus signal for adding
 
3070
            mandos_dbus_service.client_added_signal(client)
2953
3071
        # Need to initiate checking of clients
2954
3072
        if client.enabled:
2955
3073
            client.init_checker()