/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 mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2012-01-08 10:12:18 UTC
  • mfrom: (237.7.101 trunk)
  • Revision ID: teddy@recompile.se-20120108101218-h6yp0akairb23436
merge from trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
4
4
# Mandos Monitor - Control and monitor the Mandos server
5
5
6
 
# Copyright © 2008-2010 Teddy Hogeborn
7
 
# Copyright © 2008-2010 Björn Påhlsson
 
6
# Copyright © 2008-2012 Teddy Hogeborn
 
7
# Copyright © 2008-2012 Björn Påhlsson
8
8
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/>.
21
21
22
 
# Contact the authors at <mandos@fukt.bsnet.se>.
 
22
# Contact the authors at <mandos@recompile.se>.
23
23
24
24
 
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"
55
56
    }
56
57
defaultkeywords = ("Name", "Enabled", "Timeout", "LastCheckedOK")
57
 
domain = "se.bsnet.fukt"
 
58
domain = "se.recompile"
58
59
busname = domain + ".Mandos"
59
60
server_path = "/"
60
61
server_interface = domain + ".Mandos"
61
62
client_interface = domain + ".Mandos.Client"
62
 
version = "1.3.0"
 
63
version = "1.5.1"
63
64
 
64
65
def timedelta_to_milliseconds(td):
65
66
    """Convert a datetime.timedelta object to milliseconds"""
92
93
    >>> string_to_delta("5m 30s")
93
94
    datetime.timedelta(0, 330)
94
95
    """
95
 
    timevalue = datetime.timedelta(0)
96
 
    regexp = re.compile("\d+[dsmhw]")
 
96
    value = datetime.timedelta(0)
 
97
    regexp = re.compile("(\d+)([dsmhw]?)")
97
98
    
98
 
    for s in regexp.findall(interval):
99
 
        try:
100
 
            suffix = unicode(s[-1])
101
 
            value = int(s[:-1])
102
 
            if suffix == "d":
103
 
                delta = datetime.timedelta(value)
104
 
            elif suffix == "s":
105
 
                delta = datetime.timedelta(0, value)
106
 
            elif suffix == "m":
107
 
                delta = datetime.timedelta(0, 0, 0, 0, value)
108
 
            elif suffix == "h":
109
 
                delta = datetime.timedelta(0, 0, 0, 0, 0, value)
110
 
            elif suffix == "w":
111
 
                delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
112
 
            else:
113
 
                raise ValueError
114
 
        except (ValueError, IndexError):
115
 
            raise ValueError
116
 
        timevalue += delta
117
 
    return timevalue
 
99
    for num, suffix in regexp.findall(interval):
 
100
        if suffix == "d":
 
101
            value += datetime.timedelta(int(num))
 
102
        elif suffix == "s":
 
103
            value += datetime.timedelta(0, int(num))
 
104
        elif suffix == "m":
 
105
            value += datetime.timedelta(0, 0, 0, 0, int(num))
 
106
        elif suffix == "h":
 
107
            value += datetime.timedelta(0, 0, 0, 0, 0, int(num))
 
108
        elif suffix == "w":
 
109
            value += datetime.timedelta(0, 0, 0, 0, 0, 0, int(num))
 
110
        elif suffix == "":
 
111
            value += datetime.timedelta(0, 0, 0, int(num))
 
112
    return value
118
113
 
119
114
def print_clients(clients, keywords):
120
115
    def valuetostring(value, keyword):
149
144
                options.remove,
150
145
                options.checker is not None,
151
146
                options.timeout is not None,
 
147
                options.extended_timeout is not None,
152
148
                options.interval is not None,
153
149
                options.approved_by_default is not None,
154
150
                options.approval_delay is not None,
185
181
                        help="Set checker command for client")
186
182
    parser.add_argument("-t", "--timeout",
187
183
                        help="Set timeout for client")
 
184
    parser.add_argument("--extended-timeout",
 
185
                        help="Set extended timeout for client")
188
186
    parser.add_argument("-i", "--interval",
189
187
                        help="Set checker interval for client")
190
188
    parser.add_argument("--approve-by-default", action="store_true",
238
236
            #restore stderr
239
237
            os.dup2(stderrcopy, sys.stderr.fileno())
240
238
            os.close(stderrcopy)
241
 
    except dbus.exceptions.DBusException, e:
 
239
    except dbus.exceptions.DBusException:
242
240
        print("Access denied: Accessing mandos server through dbus.",
243
241
              file=sys.stderr)
244
242
        sys.exit(1)
270
268
                        "LastEnabled", "ApprovalPending",
271
269
                        "ApprovedByDefault",
272
270
                        "LastApprovalRequest", "ApprovalDelay",
273
 
                        "ApprovalDuration", "Checker")
 
271
                        "ApprovalDuration", "Checker",
 
272
                        "ExtendedTimeout")
274
273
        else:
275
274
            keywords = defaultkeywords
276
275
        
296
295
                                         dbus_interface=
297
296
                                         dbus.PROPERTIES_IFACE)
298
297
                         else 1)
299
 
            if options.checker:
 
298
            if options.checker is not None:
300
299
                client.Set(client_interface, "Checker",
301
300
                           options.checker,
302
301
                           dbus_interface=dbus.PROPERTIES_IFACE)
303
 
            if options.host:
 
302
            if options.host is not None:
304
303
                client.Set(client_interface, "Host", options.host,
305
304
                           dbus_interface=dbus.PROPERTIES_IFACE)
306
 
            if options.interval:
 
305
            if options.interval is not None:
307
306
                client.Set(client_interface, "Interval",
308
307
                           timedelta_to_milliseconds
309
308
                           (string_to_delta(options.interval)),
310
309
                           dbus_interface=dbus.PROPERTIES_IFACE)
311
 
            if options.approval_delay:
 
310
            if options.approval_delay is not None:
312
311
                client.Set(client_interface, "ApprovalDelay",
313
312
                           timedelta_to_milliseconds
314
313
                           (string_to_delta(options.
315
314
                                            approval_delay)),
316
315
                           dbus_interface=dbus.PROPERTIES_IFACE)
317
 
            if options.approval_duration:
 
316
            if options.approval_duration is not None:
318
317
                client.Set(client_interface, "ApprovalDuration",
319
318
                           timedelta_to_milliseconds
320
319
                           (string_to_delta(options.
321
320
                                            approval_duration)),
322
321
                           dbus_interface=dbus.PROPERTIES_IFACE)
323
 
            if options.timeout:
 
322
            if options.timeout is not None:
324
323
                client.Set(client_interface, "Timeout",
325
324
                           timedelta_to_milliseconds
326
325
                           (string_to_delta(options.timeout)),
327
326
                           dbus_interface=dbus.PROPERTIES_IFACE)
328
 
            if options.secret:
 
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:
329
333
                client.Set(client_interface, "Secret",
330
 
                           dbus.ByteArray(open(options.secret,
331
 
                                               "rb").read()),
 
334
                           dbus.ByteArray(options.secret.read()),
332
335
                           dbus_interface=dbus.PROPERTIES_IFACE)
333
336
            if options.approved_by_default is not None:
334
337
                client.Set(client_interface, "ApprovedByDefault",