/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/python

from __future__ import division

import SocketServer
import socket
import select
from optparse import OptionParser
import datetime
import errno
import gnutls.crypto
import gnutls.connection
import gnutls.errors
import ConfigParser
import sys
import re
import os
import signal
from sets import Set
import subprocess

class Client(object):
    def __init__(self, name=None, options=None, dn=None,
                 password=None, passfile=None, fqdn=None,
                 timeout=None, interval=-1):
        self.name = name
        self.dn = dn
        if password:
            self.password = password
        elif passfile:
            self.password = open(passfile).readall()
        else:
            print "No Password or Passfile in client config file"
            # raise RuntimeError XXX
            self.password = "gazonk"
        self.fqdn = fqdn                # string
        self.created = datetime.datetime.now()
        self.last_seen = None           # datetime.datetime()
        if timeout is None:
            timeout = options.timeout
        self.timeout = timeout          # datetime.timedelta()
        if interval == -1:
            interval = options.interval
        else:
            interval = string_to_delta(interval)
        self.interval = interval        # datetime.timedelta()
        self.next_check = datetime.datetime.now() # datetime.datetime()
        # Note: next_check may be in the past if checker is not None
        self.checker = None             # or a subprocess.Popen()
    def check_action(self):
        """The checker said something and might have completed.
        Check if is has, and take appropriate actions."""
        if self.checker.poll() is None:
            # False alarm, no result yet
            #self.checker.read()
            #print "Checker for %(name)s said nothing?" % vars(self)
            return
        now = datetime.datetime.now()
        if self.checker.returncode == 0:
            print "Checker for %(name)s succeeded" % vars(self)
            self.last_seen = now
        else:
            print "Checker for %(name)s failed" % vars(self)
        while self.next_check <= now:
            self.next_check += self.interval
        self.checker = None
    handle_request = check_action
    def start_checker(self):
        self.stop_checker()
        try:
            self.checker = subprocess.Popen("sleep 10; fping -q -- %s"
                                            % re.escape(self.fqdn),
                                            stdout=subprocess.PIPE,
                                            close_fds=True,
                                            shell=True, cwd="/")
        except subprocess.OSError, e:
            print "Failed to start subprocess:", e
    def stop_checker(self):
        if self.checker is None:
            return
        os.kill(self.checker.pid, signal.SIGTERM)
        if self.checker.poll() is None:
            os.kill(self.checker.pid, signal.SIGKILL)
        self.checker = None
    __del__ = stop_checker
    def fileno(self):
        if self.checker is None:
            return None
        return self.checker.stdout.fileno()
    def next_stop(self):
        """The time when something must be done about this client
        May be in the past."""
        if self.last_seen is None:
            # This client has never been seen
            next_timeout = self.created + self.timeout
        else:
            next_timeout = self.last_seen + self.timeout
        if self.checker is None:
            return min(next_timeout, self.next_check)
        else:
            return next_timeout
    def still_valid(self, now=None):
        """Has this client's timeout not passed?"""
        if now is None:
            now = datetime.datetime.now()
        if self.last_seen is None:
            return now < (self.created + self.timeout)
        else:
            return now < (self.last_seen + self.timeout)
    def it_is_time_to_check(self, now=None):
        if now is None:
            now = datetime.datetime.now()
        return self.next_check <= now


class server_metaclass(type):
    "Common behavior for the UDP and TCP server classes"
    def __new__(cls, name, bases, attrs):
        attrs["address_family"] = socket.AF_INET6
        attrs["allow_reuse_address"] = True
        def server_bind(self):
            if self.options.interface:
                if not hasattr(socket, "SO_BINDTODEVICE"):
                    # From /usr/include/asm-i486/socket.h
                    socket.SO_BINDTODEVICE = 25
                try:
                    self.socket.setsockopt(socket.SOL_SOCKET,
                                           socket.SO_BINDTODEVICE,
                                           self.options.interface)
                except socket.error, error:
                    if error[0] == errno.EPERM:
                        print "Warning: No permission to bind to interface", \
                              self.options.interface
                    else:
                        raise error
            return super(type(self), self).server_bind()
        attrs["server_bind"] = server_bind
        def init(self, *args, **kwargs):
            if "options" in kwargs:
                self.options = kwargs["options"]
                del kwargs["options"]
            if "clients" in kwargs:
                self.clients = kwargs["clients"]
                del kwargs["clients"]
            if "credentials" in kwargs:
                self.credentials = kwargs["credentials"]
                del kwargs["credentials"]
            return super(type(self), self).__init__(*args, **kwargs)
        attrs["__init__"] = init
        return type.__new__(cls, name, bases, attrs)


class udp_handler(SocketServer.DatagramRequestHandler, object):
    def handle(self):
        self.wfile.write("Polo")
        print "UDP request answered"


class IPv6_UDPServer(SocketServer.UDPServer, object):
    __metaclass__ = server_metaclass
    def verify_request(self, request, client_address):
        print "UDP request came"
        return request[0] == "Marco"


class tcp_handler(SocketServer.BaseRequestHandler, object):
    def handle(self):
        print "TCP request came"
        print "Request:", self.request
        print "Client Address:", self.client_address
        print "Server:", self.server
        session = gnutls.connection.ServerSession(self.request,
                                                  self.server.credentials)
        session.handshake()
        if session.peer_certificate:
            print "DN:", session.peer_certificate.subject
        try:
            session.verify_peer()
        except gnutls.errors.CertificateError, error:
            print "Verify failed", error
            session.bye()
            return
        try:
            session.send([client.password
                          for client in self.server.clients
                          if (client.dn ==
                              session.peer_certificate.subject)][0])
        except IndexError:
            session.send("gazonk")
            # Log maybe? XXX
        session.bye()


class IPv6_TCPServer(SocketServer.ForkingTCPServer, object):
    __metaclass__ = server_metaclass


def string_to_delta(interval):
    """Parse a string and return a datetime.timedelta

    >>> string_to_delta('7d')
    datetime.timedelta(7)
    >>> string_to_delta('60s')
    datetime.timedelta(0, 60)
    >>> string_to_delta('60m')
    datetime.timedelta(0, 3600)
    >>> string_to_delta('24h')
    datetime.timedelta(1)
    >>> string_to_delta(u'1w')
    datetime.timedelta(7)
    """
    try:
        suffix=unicode(interval[-1])
        value=int(interval[:-1])
        if suffix == u"d":
            delta = datetime.timedelta(value)
        elif suffix == u"s":
            delta = datetime.timedelta(0, value)
        elif suffix == u"m":
            delta = datetime.timedelta(0, 0, 0, 0, value)
        elif suffix == u"h":
            delta = datetime.timedelta(0, 0, 0, 0, 0, value)
        elif suffix == u"w":
            delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
        else:
            raise ValueError
    except (ValueError, IndexError):
        raise ValueError
    return delta


def main():
    parser = OptionParser()
    parser.add_option("-i", "--interface", type="string",
                      default="eth0", metavar="IF",
                      help="Interface to bind to")
    parser.add_option("--cert", type="string", default="cert.pem",
                      metavar="FILE",
                      help="Public key certificate to use")
    parser.add_option("--key", type="string", default="key.pem",
                      metavar="FILE",
                      help="Private key to use")
    parser.add_option("--ca", type="string", default="ca.pem",
                      metavar="FILE",
                      help="Certificate Authority certificate to use")
    parser.add_option("--crl", type="string", default="crl.pem",
                      metavar="FILE",
                      help="Certificate Revokation List to use")
    parser.add_option("-p", "--port", type="int", default=49001,
                      help="Port number to receive requests on")
    parser.add_option("--dh", type="int", metavar="BITS",
                      help="DH group to use")
    parser.add_option("-t", "--timeout", type="string", # Parsed later
                      default="15m",
                      help="Amount of downtime allowed for clients")
    parser.add_option("--interval", type="string", # Parsed later
                      default="5m",
                      help="How often to check that a client is up")
    parser.add_option("--check", action="store_true", default=False,
                      help="Run self-test")
    (options, args) = parser.parse_args()

    if options.check:
        import doctest
        doctest.testmod()
        sys.exit()
    
    # Parse the time arguments
    try:
        options.timeout = string_to_delta(options.timeout)
    except ValueError:
        parser.error("option --timeout: Unparseable time")
    
    try:
        options.interval = string_to_delta(options.interval)
    except ValueError:
        parser.error("option --interval: Unparseable time")
    
    cert = gnutls.crypto.X509Certificate(open(options.cert).read())
    key = gnutls.crypto.X509PrivateKey(open(options.key).read())
    ca = gnutls.crypto.X509Certificate(open(options.ca).read())
    crl = gnutls.crypto.X509CRL(open(options.crl).read())
    cred = gnutls.connection.X509Credentials(cert, key, [ca], [crl])
    
    # Parse config file
    defaults = {}
    client_config_object = ConfigParser.SafeConfigParser(defaults)
    client_config_object.read("mandos-clients.conf")
    clients = Set(Client(name=section, options=options,
                         **(dict(client_config_object\
                                 .items(section))))
                  for section in client_config_object.sections())
    
    in6addr_any = "::"
    udp_server = IPv6_UDPServer((in6addr_any, options.port),
                                udp_handler,
                                options=options)
    
    tcp_server = IPv6_TCPServer((in6addr_any, options.port),
                                tcp_handler,
                                options=options,
                                clients=clients,
                                credentials=cred)
    
    while True:
        if not clients:
            break
        try:
            next_stop = min(client.next_stop() for client in clients)
            now = datetime.datetime.now()
            if next_stop > now:
                delay = next_stop - now
                delay_seconds = (delay.days * 24 * 60 * 60
                                 + delay.seconds
                                 + delay.microseconds / 1000000)
                clients_with_checkers = tuple(client for client in
                                              clients
                                              if client.checker
                                              is not None)
                input_checks = (udp_server, tcp_server) \
                               + clients_with_checkers
                print "Waiting for network",
                if clients_with_checkers:
                    print "and checkers for:",
                    for client in clients_with_checkers:
                        print client.name,
                print
                input, out, err = select.select(input_checks, (), (),
                                                delay_seconds)
                for obj in input:
                    obj.handle_request()
            # start new checkers
            for client in clients:
                if client.it_is_time_to_check(now=now) and \
                       client.checker is None:
                    print "Starting checker for client %(name)s" \
                          % vars(client)
                    client.start_checker()
            # delete timed-out clients
            for client in clients.copy():
                if not client.still_valid(now=now):
                    # log xxx
                    print "Removing client %(name)s" % vars(client)
                    clients.remove(client)
        except KeyboardInterrupt:
            break
    
    # Cleanup here
    for client in clients:
        client.stop_checker()


if __name__ == "__main__":
    main()