/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-01-18 20:11:37 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080118201137-oxhupsfl5ut9cjz5
* server.py (server_metaclass): New.
  (IPv6_UDPServer, IPv6_TCPServer): Use "server_metaclass" instead of
  "server_bind" and "init_with_options".  Removed "address_family" and
  "allow_reuse_address", since they are now added in "server_metaclass".
  (server_bind, init_with_options): Removed.
  
  (main): Pass "options" to Client constructor.
  (Client.__init__): Take an "options" parameter and use it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
import ConfigParser
13
13
import sys
14
14
 
 
15
 
15
16
class Client(object):
16
 
    def __init__(self, name=None, dn=None, password=None,
17
 
                 passfile=None, fqdn=None, timeout=None,
18
 
                 interval=-1):
 
17
    def __init__(self, name=None, options=None, dn=None,
 
18
                 password=None, passfile=None, fqdn=None,
 
19
                 timeout=None, interval=-1):
19
20
        self.name = name
20
21
        self.dn = dn
21
22
        if password:
30
31
        self.created = datetime.datetime.now()
31
32
        self.last_seen = None
32
33
        if timeout is None:
33
 
            timeout = self.server.options.timeout
 
34
            timeout = options.timeout
34
35
        self.timeout = timeout
35
36
        if interval == -1:
36
 
            interval = self.server.options.interval
 
37
            interval = options.interval
37
38
        self.interval = interval
38
39
        self.next_check = datetime.datetime.now()
39
40
 
40
 
def server_bind(self):
41
 
    if self.options.interface:
42
 
        if not hasattr(socket, "SO_BINDTODEVICE"):
43
 
            # From /usr/include/asm-i486/socket.h
44
 
            socket.SO_BINDTODEVICE = 25
45
 
        try:
46
 
            self.socket.setsockopt(socket.SOL_SOCKET,
47
 
                                   socket.SO_BINDTODEVICE,
48
 
                                   self.options.interface)
49
 
        except socket.error, error:
50
 
            if error[0] == errno.EPERM:
51
 
                print "Warning: Denied permission to bind to interface", \
52
 
                      self.options.interface
53
 
            else:
54
 
                raise error
55
 
    return super(type(self), self).server_bind()
56
 
 
57
 
 
58
 
def init_with_options(self, *args, **kwargs):
59
 
    if "options" in kwargs:
60
 
        self.options = kwargs["options"]
61
 
        del kwargs["options"]
62
 
    if "clients" in kwargs:
63
 
        self.clients = kwargs["clients"]
64
 
        del kwargs["clients"]
65
 
    if "credentials" in kwargs:
66
 
        self.credentials = kwargs["credentials"]
67
 
        del kwargs["credentials"]
68
 
    return super(type(self), self).__init__(*args, **kwargs)
 
41
 
 
42
class server_metaclass(type):
 
43
    "Common behavior for the UDP and TCP server classes"
 
44
    def __new__(cls, name, bases, attrs):
 
45
        attrs["address_family"] = socket.AF_INET6
 
46
        attrs["allow_reuse_address"] = True
 
47
        def server_bind(self):
 
48
            if self.options.interface:
 
49
                if not hasattr(socket, "SO_BINDTODEVICE"):
 
50
                    # From /usr/include/asm-i486/socket.h
 
51
                    socket.SO_BINDTODEVICE = 25
 
52
                try:
 
53
                    self.socket.setsockopt(socket.SOL_SOCKET,
 
54
                                           socket.SO_BINDTODEVICE,
 
55
                                           self.options.interface)
 
56
                except socket.error, error:
 
57
                    if error[0] == errno.EPERM:
 
58
                        print "Warning: No permission to bind to interface", \
 
59
                              self.options.interface
 
60
                    else:
 
61
                        raise error
 
62
            return super(type(self), self).server_bind()
 
63
        attrs["server_bind"] = server_bind
 
64
        def init(self, *args, **kwargs):
 
65
            if "options" in kwargs:
 
66
                self.options = kwargs["options"]
 
67
                del kwargs["options"]
 
68
            if "clients" in kwargs:
 
69
                self.clients = kwargs["clients"]
 
70
                del kwargs["clients"]
 
71
            if "credentials" in kwargs:
 
72
                self.credentials = kwargs["credentials"]
 
73
                del kwargs["credentials"]
 
74
            return super(type(self), self).__init__(*args, **kwargs)
 
75
        attrs["__init__"] = init
 
76
        return type.__new__(cls, name, bases, attrs)
69
77
 
70
78
 
71
79
class udp_handler(SocketServer.DatagramRequestHandler, object):
75
83
 
76
84
 
77
85
class IPv6_UDPServer(SocketServer.UDPServer, object):
78
 
    __init__ = init_with_options
79
 
    address_family = socket.AF_INET6
80
 
    allow_reuse_address = True
81
 
    server_bind = server_bind
 
86
    __metaclass__ = server_metaclass
82
87
    def verify_request(self, request, client_address):
83
88
        print "UDP request came"
84
89
        return request[0] == "Marco"
110
115
            # Log maybe? XXX
111
116
        session.bye()
112
117
 
 
118
 
113
119
class IPv6_TCPServer(SocketServer.ForkingTCPServer, object):
114
 
    __init__ = init_with_options
115
 
    address_family = socket.AF_INET6
116
 
    allow_reuse_address = True
 
120
    __metaclass__ = server_metaclass
117
121
    request_queue_size = 1024
118
 
    server_bind = server_bind
119
122
 
120
123
 
121
124
in6addr_any = "::"
212
215
    defaults = {}
213
216
    client_config_object = ConfigParser.SafeConfigParser(defaults)
214
217
    client_config_object.read("mandos-clients.conf")
215
 
    clients = [Client(name=section,
 
218
    clients = [Client(name=section, options=options,
216
219
                      **(dict(client_config_object.items(section))))
217
220
               for section in client_config_object.sections()]
218
221