/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:
10
10
import gnutls.connection
11
11
import gnutls.errors
12
12
import ConfigParser
 
13
import sys
13
14
 
14
15
class Client(object):
15
16
    def __init__(self, name=None, dn=None, password=None,
26
27
            # raise RuntimeError XXX
27
28
            self.password = "gazonk"
28
29
        self.fqdn = fqdn
29
 
        # self.created = ...
 
30
        self.created = datetime.datetime.now()
30
31
        self.last_seen = None
31
32
        if timeout is None:
32
33
            timeout = self.server.options.timeout
34
35
        if interval == -1:
35
36
            interval = self.server.options.interval
36
37
        self.interval = interval
 
38
        self.next_check = datetime.datetime.now()
37
39
 
38
40
def server_bind(self):
39
41
    if self.options.interface:
120
122
 
121
123
cred = None
122
124
 
 
125
def string_to_delta(interval):
 
126
    """Parse a string and return a datetime.timedelta
 
127
 
 
128
    >>> string_to_delta('7d')
 
129
    datetime.timedelta(7)
 
130
    >>> string_to_delta('60s')
 
131
    datetime.timedelta(0, 60)
 
132
    >>> string_to_delta('60m')
 
133
    datetime.timedelta(0, 3600)
 
134
    >>> string_to_delta('24h')
 
135
    datetime.timedelta(1)
 
136
    >>> string_to_delta(u'1w')
 
137
    datetime.timedelta(7)
 
138
    """
 
139
    try:
 
140
        suffix=unicode(interval[-1])
 
141
        value=int(interval[:-1])
 
142
        if suffix == u"d":
 
143
            delta = datetime.timedelta(value)
 
144
        elif suffix == u"s":
 
145
            delta = datetime.timedelta(0, value)
 
146
        elif suffix == u"m":
 
147
            delta = datetime.timedelta(0, 0, 0, 0, value)
 
148
        elif suffix == u"h":
 
149
            delta = datetime.timedelta(0, 0, 0, 0, 0, value)
 
150
        elif suffix == u"w":
 
151
            delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
 
152
        else:
 
153
            raise ValueError
 
154
    except (ValueError, IndexError):
 
155
        raise ValueError
 
156
    return delta
 
157
 
123
158
def main():
124
159
    parser = OptionParser()
125
160
    parser.add_option("-i", "--interface", type="string",
144
179
    parser.add_option("-t", "--timeout", type="string", # Parsed later
145
180
                      default="15m",
146
181
                      help="Amount of downtime allowed for clients")
 
182
    parser.add_option("--interval", type="string", # Parsed later
 
183
                      default="5m",
 
184
                      help="How often to check that a client is up")
 
185
    parser.add_option("--check", action="store_true", default=False,
 
186
                      help="Run self-test")
147
187
    (options, args) = parser.parse_args()
 
188
 
 
189
    if options.check:
 
190
        import doctest
 
191
        doctest.testmod()
 
192
        sys.exit()
148
193
    
149
 
    # Parse the time argument
 
194
    # Parse the time arguments
150
195
    try:
151
 
        suffix=options.timeout[-1]
152
 
        value=int(options.timeout[:-1])
153
 
        if suffix == "d":
154
 
            options.timeout = datetime.timedelta(value)
155
 
        elif suffix == "s":
156
 
            options.timeout = datetime.timedelta(0, value)
157
 
        elif suffix == "m":
158
 
            options.timeout = datetime.timedelta(0, 0, 0, 0, value)
159
 
        elif suffix == "h":
160
 
            options.timeout = datetime.timedelta(0, 0, 0, 0, 0, value)
161
 
        elif suffix == "w":
162
 
            options.timeout = datetime.timedelta(0, 0, 0, 0, 0, 0,
163
 
                                                 value)
164
 
        else:
165
 
            raise ValueError
166
 
    except (ValueError, IndexError):
 
196
        options.timeout = string_to_delta(options.timeout)
 
197
    except ValueError:
167
198
        parser.error("option --timeout: Unparseable time")
168
199
    
 
200
    try:
 
201
        options.interval = string_to_delta(options.interval)
 
202
    except ValueError:
 
203
        parser.error("option --interval: Unparseable time")
 
204
    
169
205
    cert = gnutls.crypto.X509Certificate(open(options.cert).read())
170
206
    key = gnutls.crypto.X509PrivateKey(open(options.key).read())
171
207
    ca = gnutls.crypto.X509Certificate(open(options.ca).read())