/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to server.py

  • Committer: Teddy Hogeborn
  • Date: 2008-01-30 17:28:18 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080130172818-fnrq2xmtg1wa8wo2
* client.cpp (main): Get t_old early since it is used on error exits.

* server.py (Client.checker): New attribute.
  (Client.check_action, Client.start_checker, Client.stop_checker,
  Client.fileno, Client.next_stop, Client.still_valid,
  Client.it_is_time_to_check): New methods.
  (Client.handle_request): Alias to "check_action".
  (main): Stop all started checkers on exit.  Exit cleanly on keyboard
  interrupt.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
import gnutls.errors
12
12
import ConfigParser
13
13
import sys
14
 
 
 
14
import re
 
15
import os
 
16
import signal
15
17
 
16
18
class Client(object):
17
19
    def __init__(self, name=None, options=None, dn=None,
27
29
            print "No Password or Passfile in client config file"
28
30
            # raise RuntimeError XXX
29
31
            self.password = "gazonk"
30
 
        self.fqdn = fqdn
 
32
        self.fqdn = fqdn                # string
31
33
        self.created = datetime.datetime.now()
32
 
        self.last_seen = None
 
34
        self.last_seen = None           # datetime.datetime()
33
35
        if timeout is None:
34
36
            timeout = options.timeout
35
 
        self.timeout = timeout
 
37
        self.timeout = timeout          # datetime.timedelta()
36
38
        if interval == -1:
37
39
            interval = options.interval
38
 
        self.interval = interval
39
 
        self.next_check = datetime.datetime.now()
 
40
        self.interval = interval        # datetime.timedelta()
 
41
        self.next_check = datetime.datetime.now() # datetime.datetime()
 
42
        self.checker = None             # or a subprocess.Popen()
 
43
    def check_action(self, now=None):
 
44
        """The checker said something and might have completed.
 
45
        Check if is has, and take appropriate actions."""
 
46
        if self.checker.poll() is None:
 
47
            # False alarm, no result yet
 
48
            #self.checker.read()
 
49
            return
 
50
        if now is None:
 
51
            now = datetime.datetime.now()
 
52
        if self.checker.returncode == 0:
 
53
            self.last_seen = now
 
54
        while self.next_check <= now:
 
55
            self.next_check += self.interval
 
56
    handle_request = check_action
 
57
    def start_checker(self):
 
58
        self.stop_checker()
 
59
        try:
 
60
            self.checker = subprocess.Popen("sleep 1; fping -q -- %s"
 
61
                                            % re.escape(self.fqdn),
 
62
                                            stdout=subprocess.PIPE,
 
63
                                            close_fds=True,
 
64
                                            shell=True, cwd="/")
 
65
        except subprocess.OSError, e:
 
66
            print "Failed to start subprocess:", e
 
67
    def stop_checker(self):
 
68
        if self.checker is None:
 
69
            return
 
70
        os.kill(self.checker.pid, signal.SIGTERM)
 
71
        if self.checker.poll() is None:
 
72
            os.kill(self.checker.pid, signal.SIGKILL)
 
73
        self.checker = None
 
74
    __del__ = stop_checker
 
75
    def fileno(self):
 
76
        if self.checker is None:
 
77
            return None
 
78
        return self.checker.stdout.fileno()
 
79
    def next_stop(self):
 
80
        """The time when something must be done about this client"""
 
81
        return min(self.last_seen + self.timeout, self.next_check)
 
82
    def still_valid(self, now=None):
 
83
        """Has this client's timeout not passed?"""
 
84
        if now is None:
 
85
            now = datetime.datetime.now()
 
86
        return now < (self.last_seen + timeout)
 
87
    def it_is_time_to_check(self, now=None):
 
88
        if now is None:
 
89
            now = datetime.datetime.now()
 
90
        return self.next_check <= now
40
91
 
41
92
 
42
93
class server_metaclass(type):
229
280
                                credentials=cred)
230
281
    
231
282
    while True:
232
 
        in_, out, err = select.select((udp_server,
233
 
                                       tcp_server), (), ())
234
 
        for server in in_:
235
 
            server.handle_request()
 
283
        try:
 
284
            input, out, err = select.select((udp_server,
 
285
                                             tcp_server), (), ())
 
286
            if not input:
 
287
                pass
 
288
            else:
 
289
                for obj in input:
 
290
                    obj.handle_request()
 
291
        except KeyboardInterrupt:
 
292
            break
 
293
    
 
294
    # Cleanup here
 
295
    for client in clients:
 
296
        client.stop_checker()
236
297
 
237
298
 
238
299
if __name__ == "__main__":