/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 server.py

  • Committer: Teddy Hogeborn
  • Date: 2008-07-29 03:35:39 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080729033539-08zecoj3jwlkpjhw
* server.conf: New file.

* mandos-clients.conf: Renamed to clients.conf.

* Makefile (FORTIFY): New.
  (CFLAGS): Include $(FORTIFY).

* plugins.d/mandosclient.c (main): New "if_index" variable.  Bug fix:
                                   check if interface exists.  New
                                   "--connect" option.

* server.py (serviceInterface): Removed; replaced by
                                "AvahiService.interface".  All users
                                changed.
  (AvahiError, AvahiServiceError, AvahiGroupError): New exception
                                                    classes.
  (AvahiService): New class.
  (serviceName): Removed; replaced by "AvahiService.name".  All users
                 changed.
  (serviceType): Removed; replaced by "AvahiService.type".  All users
                 changed.
  (servicePort): Removed; replaced by "AvahiService.port".  All users
                 changed.
  (serviceTXT): Removed; replaced by "AvahiService.TXT".  All users
                changed.
  (domain): Removed; replaced by "AvahiService.domain".  All users
            changed.
  (host): Removed; replaced by "AvahiService.host".  All users
          changed.
  (rename_count): Removed; replaced by "AvahiService.rename_count" and
                 "AvahiService.max_renames".  All users changed.
  (Client.__init__): If no secret or secfile, raise TypeError instead
                     of RuntimeError.
  (Client.last_seen): Renamed to "Client.last_checked_ok".  All users
                      changed.
  (Client.stop, Client.stop_checker): Use "getattr" with default value
                                      instead of "hasattr".
  (Client.still_valid): Removed "now" argument.
  (Client.handle): Separate the "no client found" and "client invalid"
                   cases for clearer code.
  (IPv6_TCPServer.__init__): "options" argument replaced by
                             "settings".  All callers changed.
  (IPv6_TCPServer.options): Replaced by "IPv6_TCPServer.settings".
                            All users changed.
  (IPv6_TCPServer.server_bind): Use getattr instead of hasattr.
  (add_service): Removed; replaced by "AvahiService.add".  All callers
                 changed.
  (remove_service): Removed; replaced by "AvahiService.remove".  All
                    callers changed.
  (entry_group_state_changed): On entry group collision, call the new
                               AvahiService.rename method.  Raise
                               AvahiGroupError on group error.
  (if_nametoindex): Use ctypes.utils.find_library to locate the C
                    library.  Cache the result.  Loop on EINTR.
  (daemon): Use os.path.devnull to locate "/dev/null".
  (killme): Removed.  All callers changed to do "sys.exit()" instead,
            except where stated otherwise.
  (main): Removed "exitstatus".  Removed all default values from all
          non-bool options.  New option "--configdir".  New variables
          "server_defaults" and "server_settings", read from
          "%(configdir)s/server.conf".  Let any supplied command line
          options override server settings.   Variable "defaults"
          renamed to "client_defaults", which is read from
          "clients.conf" instead of "mandos-clients.conf".  New global
          AvahiService object "service" replaces old global variables.
          Catch AvahiError and exit with error if caught.

Show diffs side-by-side

added added

removed removed

Lines of Context:
433
433
    def handle(self):
434
434
        logger.debug(u"TCP connection from: %s",
435
435
                     unicode(self.client_address))
436
 
 
437
 
        line = self.request.makefile().readline()
438
 
        logger.debug(u"Protocol version: %r", line)
439
 
        try:
440
 
            if int(line.strip().split()[0]) > 1:
441
 
                raise RuntimeError
442
 
        except (ValueError, IndexError, RuntimeError), error:
443
 
            logger.error(u"Unknown protocol version: %s", error)
444
 
            return
445
 
        
446
436
        session = gnutls.connection.ClientSession\
447
437
                  (self.request, gnutls.connection.X509Credentials())
448
438
        # Note: gnutls.connection.X509Credentials is really a generic
519
509
        """This overrides the normal server_bind() function
520
510
        to bind to an interface if one was specified, and also NOT to
521
511
        bind to an address or port if they were not specified."""
522
 
        if self.settings["interface"]:
 
512
        if self.settings["interface"] != avahi.IF_UNSPEC:
523
513
            # 25 is from /usr/include/asm-i486/socket.h
524
514
            SO_BINDTODEVICE = getattr(socket, "SO_BINDTODEVICE", 25)
525
515
            try:
602
592
                        unicode(error))
603
593
        raise AvahiGroupError("State changed: %s", str(error))
604
594
 
605
 
def if_nametoindex(interface):
 
595
def if_nametoindex(interface, _func=[None]):
606
596
    """Call the C function if_nametoindex(), or equivalent"""
607
 
    global if_nametoindex
 
597
    if _func[0] is not None:
 
598
        return _func[0](interface)
608
599
    try:
609
600
        if "ctypes.util" not in sys.modules:
610
601
            import ctypes.util
611
 
        if_nametoindex = ctypes.cdll.LoadLibrary\
612
 
            (ctypes.util.find_library("c")).if_nametoindex
 
602
        while True:
 
603
            try:
 
604
                libc = ctypes.cdll.LoadLibrary\
 
605
                       (ctypes.util.find_library("c"))
 
606
                func[0] = libc.if_nametoindex
 
607
                return _func[0](interface)
 
608
            except IOError, e:
 
609
                if e != errno.EINTR:
 
610
                    raise
613
611
    except (OSError, AttributeError):
614
612
        if "struct" not in sys.modules:
615
613
            import struct
616
614
        if "fcntl" not in sys.modules:
617
615
            import fcntl
618
 
        def if_nametoindex(interface):
 
616
        def the_hard_way(interface):
619
617
            "Get an interface index the hard way, i.e. using fcntl()"
620
618
            SIOCGIFINDEX = 0x8933  # From /usr/include/linux/sockios.h
621
619
            s = socket.socket()
624
622
            s.close()
625
623
            interface_index = struct.unpack("I", ifreq[16:20])[0]
626
624
            return interface_index
627
 
    return if_nametoindex(interface)
 
625
        _func[0] = the_hard_way
 
626
        return _func[0](interface)
628
627
 
629
628
 
630
629
def daemon(nochdir, noclose):
700
699
    server_settings["debug"] = server_config.getboolean\
701
700
                               (server_section, "debug")
702
701
    del server_config
 
702
    if not server_settings["interface"]:
 
703
        server_settings["interface"] = avahi.IF_UNSPEC
703
704
    
704
705
    # Override the settings from the config file with command line
705
706
    # options, if set.
723
724
    global service
724
725
    service = AvahiService(name = server_settings["servicename"],
725
726
                           type = "_mandos._tcp", );
726
 
    if server_settings["interface"]:
727
 
        service.interface = if_nametoindex(server_settings["interface"])
728
727
    
729
728
    global main_loop
730
729
    global bus
795
794
                                clients=clients)
796
795
    # Find out what port we got
797
796
    service.port = tcp_server.socket.getsockname()[1]
798
 
    logger.debug(u"Now listening on address %r, port %d, flowinfo %d,"
799
 
                 u" scope_id %d" % tcp_server.socket.getsockname())
 
797
    logger.debug(u"Now listening on port %d", service.port)
800
798
    
801
 
    #service.interface = tcp_server.socket.getsockname()[3]
 
799
    if not server_settings["interface"]:
 
800
        service.interface = if_nametoindex\
 
801
                            (server_settings["interface"])
802
802
    
803
803
    try:
804
804
        # From the Avahi example code