/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: 2016-06-22 05:27:52 UTC
  • mfrom: (856 trunk)
  • mto: This revision was merged to the branch mainline in revision 857.
  • Revision ID: teddy@recompile.se-20160622052752-3p5440jhpxyqft1m
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-2015 Teddy Hogeborn
7
 
# Copyright © 2008-2015 Björn Påhlsson
 
6
# Copyright © 2008-2016 Teddy Hogeborn
 
7
# Copyright © 2008-2016 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
72
72
server_path = "/"
73
73
server_interface = domain + ".Mandos"
74
74
client_interface = domain + ".Mandos.Client"
75
 
version = "1.6.9"
76
 
 
 
75
version = "1.7.8"
 
76
 
 
77
 
 
78
try:
 
79
    dbus.OBJECT_MANAGER_IFACE
 
80
except AttributeError:
 
81
    dbus.OBJECT_MANAGER_IFACE = "org.freedesktop.DBus.ObjectManager"
77
82
 
78
83
def milliseconds_to_string(ms):
79
84
    td = datetime.timedelta(0, 0, 0, ms)
111
116
    # avoid excessive use of external libraries.
112
117
    
113
118
    # New type for defining tokens, syntax, and semantics all-in-one
114
 
    Token = collections.namedtuple("Token",
115
 
                                   ("regexp", # To match token; if
116
 
                                              # "value" is not None,
117
 
                                              # must have a "group"
118
 
                                              # containing digits
119
 
                                    "value",  # datetime.timedelta or
120
 
                                              # None
121
 
                                    "followers")) # Tokens valid after
122
 
                                                  # this token
 
119
    Token = collections.namedtuple("Token", (
 
120
        "regexp",  # To match token; if "value" is not None, must have
 
121
                   # a "group" containing digits
 
122
        "value",   # datetime.timedelta or None
 
123
        "followers"))           # Tokens valid after this token
123
124
    # RFC 3339 "duration" tokens, syntax, and semantics; taken from
124
125
    # the "duration" ABNF definition in RFC 3339, Appendix A.
125
126
    token_end = Token(re.compile(r"$"), None, frozenset())
178
179
                break
179
180
        else:
180
181
            # No currently valid tokens were found
181
 
            raise ValueError("Invalid RFC 3339 duration")
 
182
            raise ValueError("Invalid RFC 3339 duration: {!r}"
 
183
                             .format(duration))
182
184
    # End token found
183
185
    return value
184
186
 
186
188
def string_to_delta(interval):
187
189
    """Parse a string and return a datetime.timedelta
188
190
    
189
 
    >>> string_to_delta("7d")
 
191
    >>> string_to_delta('7d')
190
192
    datetime.timedelta(7)
191
 
    >>> string_to_delta("60s")
 
193
    >>> string_to_delta('60s')
192
194
    datetime.timedelta(0, 60)
193
 
    >>> string_to_delta("60m")
 
195
    >>> string_to_delta('60m')
194
196
    datetime.timedelta(0, 3600)
195
 
    >>> string_to_delta("24h")
 
197
    >>> string_to_delta('24h')
196
198
    datetime.timedelta(1)
197
 
    >>> string_to_delta("1w")
 
199
    >>> string_to_delta('1w')
198
200
    datetime.timedelta(7)
199
 
    >>> string_to_delta("5m 30s")
 
201
    >>> string_to_delta('5m 30s')
200
202
    datetime.timedelta(0, 330)
201
203
    """
202
204
    
344
346
    
345
347
    mandos_serv = dbus.Interface(mandos_dbus_objc,
346
348
                                 dbus_interface = server_interface)
 
349
    mandos_serv_object_manager = dbus.Interface(
 
350
        mandos_dbus_objc, dbus_interface = dbus.OBJECT_MANAGER_IFACE)
347
351
    
348
352
    #block stderr since dbus library prints to stderr
349
353
    null = os.open(os.path.devnull, os.O_RDWR)
352
356
    os.close(null)
353
357
    try:
354
358
        try:
355
 
            mandos_clients = mandos_serv.GetAllClientsWithProperties()
 
359
            mandos_clients = { path: ifs_and_props[client_interface]
 
360
                               for path, ifs_and_props in
 
361
                               mandos_serv_object_manager
 
362
                               .GetManagedObjects().items()
 
363
                               if client_interface in ifs_and_props }
356
364
        finally:
357
365
            #restore stderr
358
366
            os.dup2(stderrcopy, sys.stderr.fileno())
359
367
            os.close(stderrcopy)
360
 
    except dbus.exceptions.DBusException:
361
 
        print("Access denied: Accessing mandos server through dbus.",
362
 
              file=sys.stderr)
 
368
    except dbus.exceptions.DBusException as e:
 
369
        print("Access denied: Accessing mandos server through D-Bus: {}"
 
370
              .format(e), file=sys.stderr)
363
371
        sys.exit(1)
364
372
    
365
373
    # Compile dict of (clients: properties) to process