/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-03-09 21:23:21 UTC
  • Revision ID: teddy@recompile.se-20160309212321-2qlkzj9tecepc8xq
Server: Add Python 3 compatibility

Add Python 3 compatibility by not using the python-avahi module.  Also
fix miscellaneous things which differs in Python 3.  Especially hard
to fix is loading and saving clients data between Python 3 and 2,
since pickle formats have problems with strings.

* INSTALL: Remove python-avahi (and change python-gobject to
  python-gi, which is preferred now).
* debian/control (Source: mandos/Build-Depends-Indep): Remove
  "python-avahi".
* mandos: Wrap future_builtins import in try-except clause.  Do not
  import avahi module.  Use codecs.decode(..., "base64) instead of
  .decode("base64).  Use .keys(), .values(), and .items() instead of
  .iterkeys(), .itervalues(), and .iteritems().
  (alternate_dbus_interfaces/wrapper): Python 3 still requires the
  "black magic", but luckily it still works.  The Python 3 type()
  constructor requires first argument to be a string, not a byte
  string.
  (copy_function): New.  Use throughout.
  (Avahi, avahi): New class and global variable.
  (GnuTLS._need_version): Changed to be a byte string.
  (main): Decode byte strings loaded from pickle file.
  (main/cleanup): Dump using pickle prototoc 2 which Python 2 can
  read.

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.5"
 
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)
341
346
    
342
347
    mandos_serv = dbus.Interface(mandos_dbus_objc,
343
348
                                 dbus_interface = server_interface)
 
349
    mandos_serv_object_manager = dbus.Interface(
 
350
        mandos_dbus_objc, dbus_interface = dbus.OBJECT_MANAGER_IFACE)
344
351
    
345
352
    #block stderr since dbus library prints to stderr
346
353
    null = os.open(os.path.devnull, os.O_RDWR)
349
356
    os.close(null)
350
357
    try:
351
358
        try:
352
 
            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 }
353
364
        finally:
354
365
            #restore stderr
355
366
            os.dup2(stderrcopy, sys.stderr.fileno())
356
367
            os.close(stderrcopy)
357
 
    except dbus.exceptions.DBusException:
358
 
        print("Access denied: Accessing mandos server through dbus.",
359
 
              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)
360
371
        sys.exit(1)
361
372
    
362
373
    # Compile dict of (clients: properties) to process