/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

  • Committer: Teddy Hogeborn
  • Date: 2015-05-31 17:46:06 UTC
  • mto: (237.7.594 trunk)
  • mto: This revision was merged to the branch mainline in revision 325.
  • Revision ID: teddy@recompile.se-20150531174606-a6h51c59ldl76hze
mandos: More Python 3 fixes.

mandos (alternate_dbus_interfaces/wrapper): If Python 3, then do not
                                            use black magic, and use
                                            correct function
                                            attributes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 
37
37
from future_builtins import *
38
38
 
39
 
import SocketServer as socketserver
 
39
try:
 
40
    import SocketServer as socketserver
 
41
except ImportError:
 
42
    import socketserver
40
43
import socket
41
44
import argparse
42
45
import datetime
47
50
import gnutls.library.functions
48
51
import gnutls.library.constants
49
52
import gnutls.library.types
50
 
import ConfigParser as configparser
 
53
try:
 
54
    import ConfigParser as configparser
 
55
except ImportError:
 
56
    import configparser
51
57
import sys
52
58
import re
53
59
import os
62
68
import struct
63
69
import fcntl
64
70
import functools
65
 
import cPickle as pickle
 
71
try:
 
72
    import cPickle as pickle
 
73
except ImportError:
 
74
    import pickle
66
75
import multiprocessing
67
76
import types
68
77
import binascii
69
78
import tempfile
70
79
import itertools
71
80
import collections
 
81
import codecs
72
82
 
73
83
import dbus
74
84
import dbus.service
75
 
import gobject
 
85
try:
 
86
    import gobject
 
87
except ImportError:
 
88
    from gi.repository import GObject as gobject
76
89
import avahi
77
90
from dbus.mainloop.glib import DBusGMainLoop
78
91
import ctypes
1098
1111
                interface_names.add(alt_interface)
1099
1112
                # Is this a D-Bus signal?
1100
1113
                if getattr(attribute, "_dbus_is_signal", False):
1101
 
                    # Extract the original non-method undecorated
1102
 
                    # function by black magic
1103
 
                    nonmethod_func = (dict(
1104
 
                        zip(attribute.func_code.co_freevars,
1105
 
                            attribute.__closure__))
1106
 
                                      ["func"].cell_contents)
 
1114
                    if sys.version == 2:
 
1115
                        # Extract the original non-method undecorated
 
1116
                        # function by black magic
 
1117
                        nonmethod_func = (dict(
 
1118
                            zip(attribute.func_code.co_freevars,
 
1119
                                attribute.__closure__))
 
1120
                                          ["func"].cell_contents)
 
1121
                    else:
 
1122
                        nonmethod_func = attribute
1107
1123
                    # Create a new, but exactly alike, function
1108
1124
                    # object, and decorate it to be a new D-Bus signal
1109
1125
                    # with the alternate D-Bus interface name
 
1126
                    if sys.version == 2:
 
1127
                        new_function = types.FunctionType(
 
1128
                            nonmethod_func.func_code,
 
1129
                            nonmethod_func.func_globals,
 
1130
                            nonmethod_func.func_name,
 
1131
                            nonmethod_func.func_defaults,
 
1132
                            nonmethod_func.func_closure)
 
1133
                    else:
 
1134
                        new_function = types.FunctionType(
 
1135
                            nonmethod_func.__code__,
 
1136
                            nonmethod_func.__globals__,
 
1137
                            nonmethod_func.__name__,
 
1138
                            nonmethod_func.__defaults__,
 
1139
                            nonmethod_func.__closure__)
1110
1140
                    new_function = (dbus.service.signal(
1111
 
                        alt_interface, attribute._dbus_signature)
1112
 
                                    (types.FunctionType(
1113
 
                                        nonmethod_func.func_code,
1114
 
                                        nonmethod_func.func_globals,
1115
 
                                        nonmethod_func.func_name,
1116
 
                                        nonmethod_func.func_defaults,
1117
 
                                        nonmethod_func.func_closure)))
 
1141
                        alt_interface,
 
1142
                        attribute._dbus_signature)(new_function))
1118
1143
                    # Copy annotations, if any
1119
1144
                    try:
1120
1145
                        new_function._dbus_annotations = dict(
1659
1684
        self._pipe = child_pipe
1660
1685
        self._pipe.send(('init', fpr, address))
1661
1686
        if not self._pipe.recv():
1662
 
            raise KeyError()
 
1687
            raise KeyError(fpr)
1663
1688
    
1664
1689
    def __getattribute__(self, name):
1665
1690
        if name == '_pipe':
2169
2194
    # avoid excessive use of external libraries.
2170
2195
    
2171
2196
    # New type for defining tokens, syntax, and semantics all-in-one
2172
 
    Token = collections.namedtuple("Token",
2173
 
                                   ("regexp", # To match token; if
2174
 
                                              # "value" is not None,
2175
 
                                              # must have a "group"
2176
 
                                              # containing digits
2177
 
                                    "value",  # datetime.timedelta or
2178
 
                                              # None
2179
 
                                    "followers")) # Tokens valid after
2180
 
                                                  # this token
2181
2197
    Token = collections.namedtuple("Token", (
2182
2198
        "regexp",  # To match token; if "value" is not None, must have
2183
2199
                   # a "group" containing digits
2218
2234
    # Define starting values
2219
2235
    value = datetime.timedelta() # Value so far
2220
2236
    found_token = None
2221
 
    followers = frozenset((token_duration,)) # Following valid tokens
 
2237
    followers = frozenset((token_duration, )) # Following valid tokens
2222
2238
    s = duration                # String left to parse
2223
2239
    # Loop until end token is found
2224
2240
    while found_token is not token_end:
2241
2257
                break
2242
2258
        else:
2243
2259
            # No currently valid tokens were found
2244
 
            raise ValueError("Invalid RFC 3339 duration")
 
2260
            raise ValueError("Invalid RFC 3339 duration: {!r}"
 
2261
                             .format(duration))
2245
2262
    # End token found
2246
2263
    return value
2247
2264
 
2498
2515
            pidfilename = "/var/run/mandos.pid"
2499
2516
        pidfile = None
2500
2517
        try:
2501
 
            pidfile = open(pidfilename, "w")
 
2518
            pidfile = codecs.open(pidfilename, "w", encoding="utf-8")
2502
2519
        except IOError as e:
2503
2520
            logger.error("Could not open file %r", pidfilename,
2504
2521
                         exc_info=e)
2563
2580
            old_bus_name = dbus.service.BusName(
2564
2581
                "se.bsnet.fukt.Mandos", bus,
2565
2582
                do_not_queue=True)
2566
 
        except dbus.exceptions.NameExistsException as e:
 
2583
        except dbus.exceptions.DBusException as e:
2567
2584
            logger.error("Disabling D-Bus:", exc_info=e)
2568
2585
            use_dbus = False
2569
2586
            server_settings["use_dbus"] = False
2700
2717
    
2701
2718
    if not foreground:
2702
2719
        if pidfile is not None:
 
2720
            pid = os.getpid()
2703
2721
            try:
2704
2722
                with pidfile:
2705
 
                    pid = os.getpid()
2706
 
                    pidfile.write("{}\n".format(pid).encode("utf-8"))
 
2723
                    print(pid, file=pidfile)
2707
2724
            except IOError:
2708
2725
                logger.error("Could not write to file %r with PID %d",
2709
2726
                             pidfilename, pid)