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

  • Committer: Teddy Hogeborn
  • Date: 2011-08-17 22:46:35 UTC
  • Revision ID: teddy@fukt.bsnet.se-20110817224635-l83agxniozmovv6m
* Makefile (LDFLAGS): Added "-Xlinker --as-needed".

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-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
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@recompile.se>.
 
22
# Contact the authors at <mandos@fukt.bsnet.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"
56
55
    }
57
56
defaultkeywords = ("Name", "Enabled", "Timeout", "LastCheckedOK")
58
 
domain = "se.recompile"
 
57
domain = "se.bsnet.fukt"
59
58
busname = domain + ".Mandos"
60
59
server_path = "/"
61
60
server_interface = domain + ".Mandos"
62
61
client_interface = domain + ".Mandos.Client"
63
 
version = "1.4.1"
 
62
version = "1.3.1"
64
63
 
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)
95
94
    """
96
 
    value = datetime.timedelta(0)
97
 
    regexp = re.compile("(\d+)([dsmhw]?)")
 
95
    timevalue = datetime.timedelta(0)
 
96
    regexp = re.compile("\d+[dsmhw]")
98
97
    
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
 
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
113
118
 
114
119
def print_clients(clients, keywords):
115
120
    def valuetostring(value, keyword):
144
149
                options.remove,
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",
236
238
            #restore stderr
237
239
            os.dup2(stderrcopy, sys.stderr.fileno())
238
240
            os.close(stderrcopy)
239
 
    except dbus.exceptions.DBusException:
 
241
    except dbus.exceptions.DBusException, e:
240
242
        print("Access denied: Accessing mandos server through dbus.",
241
243
              file=sys.stderr)
242
244
        sys.exit(1)
268
270
                        "LastEnabled", "ApprovalPending",
269
271
                        "ApprovedByDefault",
270
272
                        "LastApprovalRequest", "ApprovalDelay",
271
 
                        "ApprovalDuration", "Checker",
272
 
                        "ExtendedTimeout")
 
273
                        "ApprovalDuration", "Checker")
273
274
        else:
274
275
            keywords = defaultkeywords
275
276
        
295
296
                                         dbus_interface=
296
297
                                         dbus.PROPERTIES_IFACE)
297
298
                         else 1)
298
 
            if options.checker is not None:
 
299
            if options.checker:
299
300
                client.Set(client_interface, "Checker",
300
301
                           options.checker,
301
302
                           dbus_interface=dbus.PROPERTIES_IFACE)
302
 
            if options.host is not None:
 
303
            if options.host:
303
304
                client.Set(client_interface, "Host", options.host,
304
305
                           dbus_interface=dbus.PROPERTIES_IFACE)
305
 
            if options.interval is not None:
 
306
            if options.interval:
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
            if options.timeout:
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:
 
328
            if options.secret:
333
329
                client.Set(client_interface, "Secret",
334
330
                           dbus.ByteArray(open(options.secret,
335
331
                                               "rb").read()),