/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: 2015-08-10 09:00:23 UTC
  • mto: (237.7.594 trunk)
  • mto: This revision was merged to the branch mainline in revision 325.
  • Revision ID: teddy@recompile.se-20150810090023-fz6vjqr7zf33e2tf
Support the standard org.freedesktop.DBus.ObjectManager interface.

Now that the D-Bus standard has an interface to keep track of new and
removed objects, use that instead of our own methods.  This deprecates
our D-Bus methods "GetAllClients" and "GetAllClientsWithProperties"
and the signals "ClientAdded" and "ClientRemoved", all on the server
interface "se.recompile.Mandos".

* DBUS-API: Removed references to deprecated methods and signals;
  insert reference to the org.freedesktop.DBus.ObjectManager
  interface.
* mandos (DBusObjectWithProperties._get_all_interface_names): New.
  (dbus.OBJECT_MANAGER_IFACE): If not present, monkey patch.
  (DBusObjectWithObjectManager): New.
  (main/MandosDBusService): Inherit from DBusObjectWithObjectManager.
  (main/MandosDBusService.ClientRemoved): Annotate as deprecated.
  (main/MandosDBusService.GetAllClients): - '' -
  (main/MandosDBusService.GetAllClientsWithProperties): Annotate as
                                                        deprecated.
                                                        Also only
                                                        return
                                                        properties on
                                                        client
                                                        interface.
  (main/MandosDBusService.RemoveClient): Call client_removed_signal
                                         instead of ClientRemoved.
  (main/MandosDBusService.GetManagedObjects): New.
  (main/MandosDBusService.client_added_signal): New.
  (main/MandosDBusService.client_removed_signal): - '' -
  (main/cleanup): Call "client_removed_signal" instead of sending
                  "ClientRemoved" signal directly.
  (main): Call "client_added_signal" instead of sending "ClientAdded"
          signal directly.
* mandos-ctl: Use GetManagedObjects instead of
              GetAllClientsWithProperties.  Also, show better error
              message in case of failure to connect to the D-Bus

* mandos-monitor (MandosClientPropertyCache.properties_changed):
  Bug fix; only update properties on client interface.
  (UserInterface.find_and_remove_client): Change to accept arguments
                                          from InterfacesRemoved
                                          signal.  Also, bug fix:
                                          working error message when
                                          removing unknown client.
  (UserInterface.add_new_client): Change to accept arguments from
                                  InterfacesRemoved signal.  Pass
                                  properties to MandosClientWidget
                                  constructor.
  (UserInterface.run): Connect find_and_remove_client method to
                       InterfacesRemoved signal and the add_new_client
                       method to the InterfacesAdded signal.

Show diffs side-by-side

added added

removed removed

Lines of Context:
75
75
version = "1.6.9"
76
76
 
77
77
 
 
78
try:
 
79
    dbus.OBJECT_MANAGER_IFACE
 
80
except AttributeError:
 
81
    dbus.OBJECT_MANAGER_IFACE = "org.freedesktop.DBus.ObjectManager"
 
82
 
78
83
def milliseconds_to_string(ms):
79
84
    td = datetime.timedelta(0, 0, 0, ms)
80
85
    return ("{days}{hours:02}:{minutes:02}:{seconds:02}".format(
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