/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

  • Committer: Teddy Hogeborn
  • Date: 2019-08-10 15:19:47 UTC
  • Revision ID: teddy@recompile.se-20190810151947-wyw7cetrh1pvtw37
Simplification of Python 3 compatibility code

Normally, "class Foo:" in Python 2 creates a "classic" class, but in
Python 3, all classes are "new-style" classes, which you can get in
Python 2 by doing "class Foo(object):", i.e. inheriting from "object".
But, you can also get a new-style classes from "class Foo:" in Python
2 by setting the global "__metaclass__" variable to "type", which
makes the code less cluttered.  Note: it is still necessary to inherit
from "object" in Python 2 to get a new-style class if the class is
otherwise inheriting only from "classic" classes.

* mandos: Set "__metaclass__ = type" globally for Python 2, and remove
          inheriting from "object" in all places possible.
* mandos-ctl: - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
80
80
 
81
81
import dbus
82
82
import dbus.service
 
83
import gi
83
84
from gi.repository import GLib
84
85
from dbus.mainloop.glib import DBusGMainLoop
85
86
import ctypes
87
88
import xml.dom.minidom
88
89
import inspect
89
90
 
 
91
if sys.version_info.major == 2:
 
92
    __metaclass__ = type
 
93
 
90
94
# Try to find the value of SO_BINDTODEVICE:
91
95
try:
92
96
    # This is where SO_BINDTODEVICE is in Python 3.3 (or 3.4?) and
115
119
if sys.version_info.major == 2:
116
120
    str = unicode
117
121
 
118
 
version = "1.8.5"
 
122
if sys.version_info < (3, 2):
 
123
    configparser.Configparser = configparser.SafeConfigParser
 
124
 
 
125
version = "1.8.7"
119
126
stored_state_file = "clients.pickle"
120
127
 
121
128
logger = logging.getLogger()
179
186
    pass
180
187
 
181
188
 
182
 
class PGPEngine(object):
 
189
class PGPEngine:
183
190
    """A simple class for OpenPGP symmetric encryption & decryption"""
184
191
 
185
192
    def __init__(self):
275
282
 
276
283
 
277
284
# Pretend that we have an Avahi module
278
 
class avahi(object):
 
285
class avahi:
279
286
    """This isn't so much a class as it is a module-like namespace."""
280
287
    IF_UNSPEC = -1               # avahi-common/address.h
281
288
    PROTO_UNSPEC = -1            # avahi-common/address.h
315
322
    pass
316
323
 
317
324
 
318
 
class AvahiService(object):
 
325
class AvahiService:
319
326
    """An Avahi (Zeroconf) service.
320
327
 
321
328
    Attributes:
503
510
 
504
511
 
505
512
# Pretend that we have a GnuTLS module
506
 
class gnutls(object):
 
513
class gnutls:
507
514
    """This isn't so much a class as it is a module-like namespace."""
508
515
 
509
516
    library = ctypes.util.find_library("gnutls")
572
579
        pass
573
580
 
574
581
    # Classes
575
 
    class Credentials(object):
 
582
    class Credentials:
576
583
        def __init__(self):
577
584
            self._c_object = gnutls.certificate_credentials_t()
578
585
            gnutls.certificate_allocate_credentials(
582
589
        def __del__(self):
583
590
            gnutls.certificate_free_credentials(self._c_object)
584
591
 
585
 
    class ClientSession(object):
 
592
    class ClientSession:
586
593
        def __init__(self, socket, credentials=None):
587
594
            self._c_object = gnutls.session_t()
588
595
            gnutls_flags = gnutls.CLIENT
814
821
    connection.close()
815
822
 
816
823
 
817
 
class Client(object):
 
824
class Client:
818
825
    """A representation of a client host served by this server.
819
826
 
820
827
    Attributes:
2209
2216
    del _interface
2210
2217
 
2211
2218
 
2212
 
class ProxyClient(object):
 
2219
class ProxyClient:
2213
2220
    def __init__(self, child_pipe, key_id, fpr, address):
2214
2221
        self._pipe = child_pipe
2215
2222
        self._pipe.send(('init', key_id, fpr, address))
2488
2495
        return hex_fpr
2489
2496
 
2490
2497
 
2491
 
class MultiprocessingMixIn(object):
 
2498
class MultiprocessingMixIn:
2492
2499
    """Like socketserver.ThreadingMixIn, but with multiprocessing"""
2493
2500
 
2494
2501
    def sub_process_main(self, request, address):
2506
2513
        return proc
2507
2514
 
2508
2515
 
2509
 
class MultiprocessingMixInWithPipe(MultiprocessingMixIn, object):
 
2516
class MultiprocessingMixInWithPipe(MultiprocessingMixIn):
2510
2517
    """ adds a pipe to the MixIn """
2511
2518
 
2512
2519
    def process_request(self, request, client_address):
2527
2534
 
2528
2535
 
2529
2536
class IPv6_TCPServer(MultiprocessingMixInWithPipe,
2530
 
                     socketserver.TCPServer, object):
 
2537
                     socketserver.TCPServer):
2531
2538
    """IPv6-capable TCP server.  Accepts 'None' as address and/or port
2532
2539
 
2533
2540
    Attributes:
2998
3005
    del priority
2999
3006
 
3000
3007
    # Parse config file for server-global settings
3001
 
    server_config = configparser.SafeConfigParser(server_defaults)
 
3008
    server_config = configparser.ConfigParser(server_defaults)
3002
3009
    del server_defaults
3003
3010
    server_config.read(os.path.join(options.configdir, "mandos.conf"))
3004
 
    # Convert the SafeConfigParser object to a dict
 
3011
    # Convert the ConfigParser object to a dict
3005
3012
    server_settings = server_config.defaults()
3006
3013
    # Use the appropriate methods on the non-string config options
3007
3014
    for option in ("debug", "use_dbus", "use_ipv6", "restore",
3079
3086
                                  server_settings["servicename"])))
3080
3087
 
3081
3088
    # Parse config file with clients
3082
 
    client_config = configparser.SafeConfigParser(Client
3083
 
                                                  .client_defaults)
 
3089
    client_config = configparser.ConfigParser(Client.client_defaults)
3084
3090
    client_config.read(os.path.join(server_settings["configdir"],
3085
3091
                                    "clients.conf"))
3086
3092
 
3157
3163
        # Close all input and output, do double fork, etc.
3158
3164
        daemon()
3159
3165
 
3160
 
    # multiprocessing will use threads, so before we use GLib we need
3161
 
    # to inform GLib that threads will be used.
3162
 
    GLib.threads_init()
 
3166
    if gi.version_info < (3, 10, 2):
 
3167
        # multiprocessing will use threads, so before we use GLib we
 
3168
        # need to inform GLib that threads will be used.
 
3169
        GLib.threads_init()
3163
3170
 
3164
3171
    global main_loop
3165
3172
    # From the Avahi example code