4
4
# Mandos Monitor - Control and monitor the Mandos server
 
6
 
# Copyright © 2008-2012 Teddy Hogeborn
 
7
 
# Copyright © 2008-2012 Björn Påhlsson
 
 
6
# Copyright © 2008-2010 Teddy Hogeborn
 
 
7
# Copyright © 2008-2010 Björn Påhlsson
 
9
9
# This program is free software: you can redistribute it and/or modify
 
10
10
# it under the terms of the GNU General Public License as published by
 
 
19
19
# You should have received a copy of the GNU General Public License
 
20
20
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
22
 
# Contact the authors at <mandos@recompile.se>.
 
 
22
# Contact the authors at <mandos@fukt.bsnet.se>.
 
25
25
from __future__ import (division, absolute_import, print_function,
 
 
52
52
    "ApprovalDelay": "Approval Delay",
 
53
53
    "ApprovalDuration": "Approval Duration",
 
54
54
    "Checker": "Checker",
 
55
 
    "ExtendedTimeout" : "Extended Timeout"
 
57
56
defaultkeywords = ("Name", "Enabled", "Timeout", "LastCheckedOK")
 
58
 
domain = "se.recompile"
 
 
57
domain = "se.bsnet.fukt"
 
59
58
busname = domain + ".Mandos"
 
61
60
server_interface = domain + ".Mandos"
 
62
61
client_interface = domain + ".Mandos.Client"
 
65
64
def timedelta_to_milliseconds(td):
 
66
65
    """Convert a datetime.timedelta object to milliseconds"""
 
 
93
92
    >>> string_to_delta("5m 30s")
 
94
93
    datetime.timedelta(0, 330)
 
96
 
    value = datetime.timedelta(0)
 
97
 
    regexp = re.compile("(\d+)([dsmhw]?)")
 
 
95
    timevalue = datetime.timedelta(0)
 
 
96
    regexp = re.compile("\d+[dsmhw]")
 
99
 
    for num, suffix in regexp.findall(interval):
 
101
 
            value += datetime.timedelta(int(num))
 
103
 
            value += datetime.timedelta(0, int(num))
 
105
 
            value += datetime.timedelta(0, 0, 0, 0, int(num))
 
107
 
            value += datetime.timedelta(0, 0, 0, 0, 0, int(num))
 
109
 
            value += datetime.timedelta(0, 0, 0, 0, 0, 0, int(num))
 
111
 
            value += datetime.timedelta(0, 0, 0, int(num))
 
 
98
    for s in regexp.findall(interval):
 
 
100
            suffix = unicode(s[-1])
 
 
103
                delta = datetime.timedelta(value)
 
 
105
                delta = datetime.timedelta(0, value)
 
 
107
                delta = datetime.timedelta(0, 0, 0, 0, value)
 
 
109
                delta = datetime.timedelta(0, 0, 0, 0, 0, value)
 
 
111
                delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
 
 
114
        except (ValueError, IndexError):
 
114
119
def print_clients(clients, keywords):
 
115
120
    def valuetostring(value, keyword):
 
 
145
150
                options.checker is not None,
 
146
151
                options.timeout is not None,
 
147
 
                options.extended_timeout is not None,
 
148
152
                options.interval is not None,
 
149
153
                options.approved_by_default is not None,
 
150
154
                options.approval_delay is not None,
 
 
181
185
                        help="Set checker command for client")
 
182
186
    parser.add_argument("-t", "--timeout",
 
183
187
                        help="Set timeout for client")
 
184
 
    parser.add_argument("--extended-timeout",
 
185
 
                        help="Set extended timeout for client")
 
186
188
    parser.add_argument("-i", "--interval",
 
187
189
                        help="Set checker interval for client")
 
188
190
    parser.add_argument("--approve-by-default", action="store_true",
 
 
268
270
                        "LastEnabled", "ApprovalPending",
 
269
271
                        "ApprovedByDefault",
 
270
272
                        "LastApprovalRequest", "ApprovalDelay",
 
271
 
                        "ApprovalDuration", "Checker",
 
 
273
                        "ApprovalDuration", "Checker")
 
274
275
            keywords = defaultkeywords
 
 
296
297
                                         dbus.PROPERTIES_IFACE)
 
298
 
            if options.checker is not None:
 
299
300
                client.Set(client_interface, "Checker",
 
301
302
                           dbus_interface=dbus.PROPERTIES_IFACE)
 
302
 
            if options.host is not None:
 
303
304
                client.Set(client_interface, "Host", options.host,
 
304
305
                           dbus_interface=dbus.PROPERTIES_IFACE)
 
305
 
            if options.interval is not None:
 
306
307
                client.Set(client_interface, "Interval",
 
307
308
                           timedelta_to_milliseconds
 
308
309
                           (string_to_delta(options.interval)),
 
309
310
                           dbus_interface=dbus.PROPERTIES_IFACE)
 
310
 
            if options.approval_delay is not None:
 
 
311
            if options.approval_delay:
 
311
312
                client.Set(client_interface, "ApprovalDelay",
 
312
313
                           timedelta_to_milliseconds
 
313
314
                           (string_to_delta(options.
 
314
315
                                            approval_delay)),
 
315
316
                           dbus_interface=dbus.PROPERTIES_IFACE)
 
316
 
            if options.approval_duration is not None:
 
 
317
            if options.approval_duration:
 
317
318
                client.Set(client_interface, "ApprovalDuration",
 
318
319
                           timedelta_to_milliseconds
 
319
320
                           (string_to_delta(options.
 
320
321
                                            approval_duration)),
 
321
322
                           dbus_interface=dbus.PROPERTIES_IFACE)
 
322
 
            if options.timeout is not None:
 
323
324
                client.Set(client_interface, "Timeout",
 
324
325
                           timedelta_to_milliseconds
 
325
326
                           (string_to_delta(options.timeout)),
 
326
327
                           dbus_interface=dbus.PROPERTIES_IFACE)
 
327
 
            if options.extended_timeout is not None:
 
328
 
                client.Set(client_interface, "ExtendedTimeout",
 
329
 
                           timedelta_to_milliseconds
 
330
 
                           (string_to_delta(options.extended_timeout)),
 
331
 
                           dbus_interface=dbus.PROPERTIES_IFACE)
 
332
 
            if options.secret is not None:
 
333
329
                client.Set(client_interface, "Secret",
 
334
 
                           dbus.ByteArray(options.secret.read()),
 
 
330
                           dbus.ByteArray(open(options.secret,
 
335
332
                           dbus_interface=dbus.PROPERTIES_IFACE)
 
336
333
            if options.approved_by_default is not None:
 
337
334
                client.Set(client_interface, "ApprovedByDefault",