/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: 2009-04-16 22:26:57 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090416222657-dqa98ddwlq4pcjbj
Code cleanup.

* mandos (ForkingMixInWithPipe.handle_ipc): Take "self" argument.
  (IPv6_TCPServer): Split off attributes "enabled", "clients",
                   "use_dbus", "gnutls_priority", and methods
                   "server_activate", "enable", and "handle" into
                   separate class "MandosServer".
  (MandosServer): New.
  (main): Use MandosServer instead of IPv6_TCPServer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
312
312
    
313
313
    def enable(self):
314
314
        """Start this client's checker and timeout hooks"""
315
 
        if getattr(self, u"enabled", False):
316
 
            # Already enabled
317
 
            return
318
315
        self.last_enabled = datetime.datetime.utcnow()
319
316
        # Schedule a new checker to be started an 'interval' from now,
320
317
        # and every interval from then on.
896
893
 
897
894
 
898
895
class ForkingMixInWithPipe(socketserver.ForkingMixIn, object):
899
 
    """Like socketserver.ForkingMixIn, but also pass a pipe."""
 
896
    """Like socketserver.ForkingMixIn, but also pass a pipe.
 
897
    
 
898
    Assumes a gobject.MainLoop event loop.
 
899
    """
900
900
    def process_request(self, request, client_address):
901
901
        """Overrides and wraps the original process_request().
902
902
        
906
906
        super(ForkingMixInWithPipe,
907
907
              self).process_request(request, client_address)
908
908
        os.close(self.pipe[1])  # close write end
909
 
        self.add_pipe(self.pipe[0])
910
 
    def add_pipe(self, pipe):
 
909
        # Call "handle_ipc" for both data and EOF events
 
910
        gobject.io_add_watch(self.pipe[0],
 
911
                             gobject.IO_IN | gobject.IO_HUP,
 
912
                             self.handle_ipc)
 
913
    def handle_ipc(self, source, condition):
911
914
        """Dummy function; override as necessary"""
912
 
        os.close(pipe)
 
915
        os.close(source)
 
916
        return False
913
917
 
914
918
 
915
919
class IPv6_TCPServer(ForkingMixInWithPipe,
920
924
        enabled:        Boolean; whether this server is activated yet
921
925
        interface:      None or a network interface name (string)
922
926
        use_ipv6:       Boolean; to use IPv6 or not
 
927
        ----
 
928
        clients:        set of Client objects
 
929
        gnutls_priority GnuTLS priority string
 
930
        use_dbus:       Boolean; to emit D-Bus signals or not
923
931
    """
924
932
    def __init__(self, server_address, RequestHandlerClass,
925
933
                 interface=None, use_ipv6=True):
982
990
        clients:        set of Client objects
983
991
        gnutls_priority GnuTLS priority string
984
992
        use_dbus:       Boolean; to emit D-Bus signals or not
985
 
        clients:        set of Client objects
986
 
        gnutls_priority GnuTLS priority string
987
 
        use_dbus:       Boolean; to emit D-Bus signals or not
988
 
    
989
 
    Assumes a gobject.MainLoop event loop.
990
993
    """
991
994
    def __init__(self, server_address, RequestHandlerClass,
992
995
                 interface=None, use_ipv6=True, clients=None,
993
996
                 gnutls_priority=None, use_dbus=True):
994
997
        self.enabled = False
995
998
        self.clients = clients
996
 
        if self.clients is None:
997
 
            self.clients = set()
998
999
        self.use_dbus = use_dbus
999
1000
        self.gnutls_priority = gnutls_priority
1000
1001
        IPv6_TCPServer.__init__(self, server_address,
1006
1007
            return socketserver.TCPServer.server_activate(self)
1007
1008
    def enable(self):
1008
1009
        self.enabled = True
1009
 
    def add_pipe(self, pipe):
1010
 
        # Call "handle_ipc" for both data and EOF events
1011
 
        gobject.io_add_watch(pipe, gobject.IO_IN | gobject.IO_HUP,
1012
 
                             self.handle_ipc)
1013
1010
    def handle_ipc(self, source, condition, file_objects={}):
1014
1011
        condition_names = {
1015
1012
            gobject.IO_IN: u"IN",   # There is data to read.
1279
1276
    global mandos_dbus_service
1280
1277
    mandos_dbus_service = None
1281
1278
    
 
1279
    clients = set()
1282
1280
    tcp_server = MandosServer((server_settings[u"address"],
1283
1281
                               server_settings[u"port"]),
1284
1282
                              ClientHandler,
1285
1283
                              interface=server_settings[u"interface"],
1286
1284
                              use_ipv6=use_ipv6,
 
1285
                              clients=clients,
1287
1286
                              gnutls_priority=
1288
1287
                              server_settings[u"priority"],
1289
1288
                              use_dbus=use_dbus)
1346
1345
    client_class = Client
1347
1346
    if use_dbus:
1348
1347
        client_class = functools.partial(ClientDBus, bus = bus)
1349
 
    tcp_server.clients.update(set(
 
1348
    clients.update(set(
1350
1349
            client_class(name = section,
1351
1350
                         config= dict(client_config.items(section)))
1352
1351
            for section in client_config.sections()))
1353
 
    if not tcp_server.clients:
 
1352
    if not clients:
1354
1353
        logger.warning(u"No clients defined")
1355
1354
    
1356
1355
    if debug:
1382
1381
        "Cleanup function; run on exit"
1383
1382
        service.cleanup()
1384
1383
        
1385
 
        while tcp_server.clients:
1386
 
            client = tcp_server.clients.pop()
 
1384
        while clients:
 
1385
            client = clients.pop()
1387
1386
            client.disable_hook = None
1388
1387
            client.disable()
1389
1388
    
1419
1418
            @dbus.service.method(_interface, out_signature=u"ao")
1420
1419
            def GetAllClients(self):
1421
1420
                "D-Bus method"
1422
 
                return dbus.Array(c.dbus_object_path
1423
 
                                  for c in tcp_server.clients)
 
1421
                return dbus.Array(c.dbus_object_path for c in clients)
1424
1422
            
1425
1423
            @dbus.service.method(_interface,
1426
1424
                                 out_signature=u"a{oa{sv}}")
1428
1426
                "D-Bus method"
1429
1427
                return dbus.Dictionary(
1430
1428
                    ((c.dbus_object_path, c.GetAllProperties())
1431
 
                     for c in tcp_server.clients),
 
1429
                     for c in clients),
1432
1430
                    signature=u"oa{sv}")
1433
1431
            
1434
1432
            @dbus.service.method(_interface, in_signature=u"o")
1435
1433
            def RemoveClient(self, object_path):
1436
1434
                "D-Bus method"
1437
 
                for c in tcp_server.clients:
 
1435
                for c in clients:
1438
1436
                    if c.dbus_object_path == object_path:
1439
 
                        tcp_server.clients.remove(c)
 
1437
                        clients.remove(c)
1440
1438
                        c.remove_from_connection()
1441
1439
                        # Don't signal anything except ClientRemoved
1442
1440
                        c.disable(signal=False)
1449
1447
        
1450
1448
        mandos_dbus_service = MandosDBusService()
1451
1449
    
1452
 
    for client in tcp_server.clients:
 
1450
    for client in clients:
1453
1451
        if use_dbus:
1454
1452
            # Emit D-Bus signal
1455
1453
            mandos_dbus_service.ClientAdded(client.dbus_object_path,