/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: 2007-12-19 07:58:13 UTC
  • Revision ID: teddy@fukt.bsnet.se-20071219075813-5u1w3gujvttj83qs
* server.py (Client.created, Client.next_check): New.
  (string_to_delta): New.
  (main): New options "--check" and "--interval".  Use string_to_delta to
  parse arguments to "--timeout" and "--interval".

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
import ConfigParser
13
13
import sys
14
14
 
15
 
 
16
15
class Client(object):
17
 
    def __init__(self, name=None, options=None, dn=None,
18
 
                 password=None, passfile=None, fqdn=None,
19
 
                 timeout=None, interval=-1):
 
16
    def __init__(self, name=None, dn=None, password=None,
 
17
                 passfile=None, fqdn=None, timeout=None,
 
18
                 interval=-1):
20
19
        self.name = name
21
20
        self.dn = dn
22
21
        if password:
31
30
        self.created = datetime.datetime.now()
32
31
        self.last_seen = None
33
32
        if timeout is None:
34
 
            timeout = options.timeout
 
33
            timeout = self.server.options.timeout
35
34
        self.timeout = timeout
36
35
        if interval == -1:
37
 
            interval = options.interval
 
36
            interval = self.server.options.interval
38
37
        self.interval = interval
39
38
        self.next_check = datetime.datetime.now()
40
39
 
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)
 
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)
77
69
 
78
70
 
79
71
class udp_handler(SocketServer.DatagramRequestHandler, object):
83
75
 
84
76
 
85
77
class IPv6_UDPServer(SocketServer.UDPServer, object):
86
 
    __metaclass__ = server_metaclass
 
78
    __init__ = init_with_options
 
79
    address_family = socket.AF_INET6
 
80
    allow_reuse_address = True
 
81
    server_bind = server_bind
87
82
    def verify_request(self, request, client_address):
88
83
        print "UDP request came"
89
84
        return request[0] == "Marco"
115
110
            # Log maybe? XXX
116
111
        session.bye()
117
112
 
118
 
 
119
113
class IPv6_TCPServer(SocketServer.ForkingTCPServer, object):
120
 
    __metaclass__ = server_metaclass
 
114
    __init__ = init_with_options
 
115
    address_family = socket.AF_INET6
 
116
    allow_reuse_address = True
121
117
    request_queue_size = 1024
 
118
    server_bind = server_bind
122
119
 
123
120
 
124
121
in6addr_any = "::"
215
212
    defaults = {}
216
213
    client_config_object = ConfigParser.SafeConfigParser(defaults)
217
214
    client_config_object.read("mandos-clients.conf")
218
 
    clients = [Client(name=section, options=options,
 
215
    clients = [Client(name=section,
219
216
                      **(dict(client_config_object.items(section))))
220
217
               for section in client_config_object.sections()]
221
218