/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 21:23:46 UTC
  • mfrom: (2.1.1 Project)
  • Revision ID: teddy@fukt.bsnet.se-20080118212346-qo103d6o8r7xs4za
Merged from Björn.

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
 
import re
15
 
import os
16
 
import signal
 
14
 
17
15
 
18
16
class Client(object):
19
17
    def __init__(self, name=None, options=None, dn=None,
29
27
            print "No Password or Passfile in client config file"
30
28
            # raise RuntimeError XXX
31
29
            self.password = "gazonk"
32
 
        self.fqdn = fqdn                # string
 
30
        self.fqdn = fqdn
33
31
        self.created = datetime.datetime.now()
34
 
        self.last_seen = None           # datetime.datetime()
 
32
        self.last_seen = None
35
33
        if timeout is None:
36
34
            timeout = options.timeout
37
 
        self.timeout = timeout          # datetime.timedelta()
 
35
        self.timeout = timeout
38
36
        if interval == -1:
39
37
            interval = options.interval
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
 
38
        self.interval = interval
 
39
        self.next_check = datetime.datetime.now()
91
40
 
92
41
 
93
42
class server_metaclass(type):
174
123
 
175
124
in6addr_any = "::"
176
125
 
 
126
cred = None
 
127
 
177
128
def string_to_delta(interval):
178
129
    """Parse a string and return a datetime.timedelta
179
130
 
207
158
        raise ValueError
208
159
    return delta
209
160
 
210
 
 
211
161
def main():
212
162
    parser = OptionParser()
213
163
    parser.add_option("-i", "--interface", type="string",
280
230
                                credentials=cred)
281
231
    
282
232
    while True:
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()
 
233
        in_, out, err = select.select((udp_server,
 
234
                                       tcp_server), (), ())
 
235
        for server in in_:
 
236
            server.handle_request()
297
237
 
298
238
 
299
239
if __name__ == "__main__":