1018
1021
variant_level=variant_level)
1021
class AlternateDBusNamesMetaclass(DBusObjectWithProperties
1023
"""Applied to an empty subclass of a D-Bus object, this metaclass
1024
will add additional D-Bus attributes matching a certain pattern.
1024
def alternate_dbus_interfaces(alt_interface_names, deprecate=True):
1025
"""A class decorator; applied to a subclass of
1026
dbus.service.Object, it will add alternate D-Bus attributes with
1027
interface names according to the "alt_interface_names" mapping.
1030
@alternate_dbus_names({"org.example.Interface":
1031
"net.example.AlternateInterface"})
1032
class SampleDBusObject(dbus.service.Object):
1033
@dbus.service.method("org.example.Interface")
1034
def SampleDBusMethod():
1037
The above "SampleDBusMethod" on "SampleDBusObject" will be
1038
reachable via two interfaces: "org.example.Interface" and
1039
"net.example.AlternateInterface", the latter of which will have
1040
its D-Bus annotation "org.freedesktop.DBus.Deprecated" set to
1041
"true", unless "deprecate" is passed with a False value.
1043
This works for methods and signals, and also for D-Bus properties
1044
(from DBusObjectWithProperties) and interfaces (from the
1045
dbus_interface_annotations decorator).
1026
def __new__(mcs, name, bases, attr):
1027
# Go through all the base classes which could have D-Bus
1028
# methods, signals, or properties in them
1029
old_interface_names = []
1030
for base in (b for b in bases
1031
if issubclass(b, dbus.service.Object)):
1032
# Go though all attributes of the base class
1033
for attrname, attribute in inspect.getmembers(base):
1048
for orig_interface_name, alt_interface_name in (
1049
alt_interface_names.iteritems()):
1051
interface_names = set()
1052
# Go though all attributes of the class
1053
for attrname, attribute in inspect.getmembers(cls):
1034
1054
# Ignore non-D-Bus attributes, and D-Bus attributes
1035
1055
# with the wrong interface name
1036
1056
if (not hasattr(attribute, "_dbus_interface")
1037
1057
or not attribute._dbus_interface
1038
.startswith("se.recompile.Mandos")):
1058
.startswith(orig_interface_name)):
1040
1060
# Create an alternate D-Bus interface name based on
1041
1061
# the current name
1042
1062
alt_interface = (attribute._dbus_interface
1043
.replace("se.recompile.Mandos",
1044
"se.bsnet.fukt.Mandos"))
1045
if alt_interface != attribute._dbus_interface:
1046
old_interface_names.append(alt_interface)
1063
.replace(orig_interface_name,
1064
alt_interface_name))
1065
interface_names.add(alt_interface)
1047
1066
# Is this a D-Bus signal?
1048
1067
if getattr(attribute, "_dbus_is_signal", False):
1049
1068
# Extract the original non-method function by
1071
1090
except AttributeError:
1073
1092
# Define a creator of a function to call both the
1074
# old and new functions, so both the old and new
1075
# signals gets sent when the function is called
1093
# original and alternate functions, so both the
1094
# original and alternate signals gets sent when
1095
# the function is called
1076
1096
def fixscope(func1, func2):
1077
1097
"""This function is a scope container to pass
1078
1098
func1 and func2 to the "call_both" function
1085
1105
return call_both
1086
1106
# Create the "call_both" function and add it to
1088
attr[attrname] = fixscope(attribute,
1108
attr[attrname] = fixscope(attribute, new_function)
1090
1109
# Is this a D-Bus method?
1091
1110
elif getattr(attribute, "_dbus_is_method", False):
1092
1111
# Create a new, but exactly alike, function
1148
1167
attribute.func_name,
1149
1168
attribute.func_defaults,
1150
1169
attribute.func_closure)))
1151
# Deprecate all old interfaces
1152
basename="_AlternateDBusNamesMetaclass_interface_annotation{0}"
1153
for old_interface_name in old_interface_names:
1154
@dbus_interface_annotations(old_interface_name)
1156
return { "org.freedesktop.DBus.Deprecated": "true" }
1157
# Find an unused name
1158
for aname in (basename.format(i) for i in
1160
if aname not in attr:
1163
return type.__new__(mcs, name, bases, attr)
1171
# Deprecate all alternate interfaces
1172
iname="_AlternateDBusNames_interface_annotation{0}"
1173
for interface_name in interface_names:
1174
@dbus_interface_annotations(interface_name)
1176
return { "org.freedesktop.DBus.Deprecated":
1178
# Find an unused name
1179
for aname in (iname.format(i)
1180
for i in itertools.count()):
1181
if aname not in attr:
1185
# Replace the class with a new subclass of it with
1186
# methods, signals, etc. as created above.
1187
cls = type(b"{0}Alternate".format(cls.__name__),
1193
@alternate_dbus_interfaces({"se.recompile.Mandos":
1194
"se.bsnet.fukt.Mandos"})
1166
1195
class ClientDBus(Client, DBusObjectWithProperties):
1167
1196
"""A Client class using D-Bus
2102
2128
null = os.open(os.devnull, os.O_NOCTTY | os.O_RDWR)
2103
2129
if not stat.S_ISCHR(os.fstat(null).st_mode):
2104
2130
raise OSError(errno.ENODEV,
2105
"%s not a character device"
2131
"{0} not a character device"
2132
.format(os.devnull))
2107
2133
os.dup2(null, sys.stdin.fileno())
2108
2134
os.dup2(null, sys.stdout.fileno())
2109
2135
os.dup2(null, sys.stderr.fileno())
2119
2145
parser = argparse.ArgumentParser()
2120
2146
parser.add_argument("-v", "--version", action="version",
2121
version = "%%(prog)s %s" % version,
2147
version = "%(prog)s {0}".format(version),
2122
2148
help="show version number and exit")
2123
2149
parser.add_argument("-i", "--interface", metavar="IF",
2124
2150
help="Bind to interface IF")
2228
2254
if server_settings["servicename"] != "Mandos":
2229
2255
syslogger.setFormatter(logging.Formatter
2230
('Mandos (%s) [%%(process)d]:'
2231
' %%(levelname)s: %%(message)s'
2232
% server_settings["servicename"]))
2256
('Mandos ({0}) [%(process)d]:'
2257
' %(levelname)s: %(message)s'
2258
.format(server_settings
2234
2261
# Parse config file with clients
2235
2262
client_config = configparser.SafeConfigParser(Client
2347
2374
(stored_state))
2348
2375
os.remove(stored_state_path)
2349
2376
except IOError as e:
2350
logger.warning("Could not load persistent state: {0}"
2352
if e.errno != errno.ENOENT:
2377
if e.errno == errno.ENOENT:
2378
logger.warning("Could not load persistent state: {0}"
2379
.format(os.strerror(e.errno)))
2381
logger.critical("Could not load persistent state:",
2354
2384
except EOFError as e:
2355
2385
logger.warning("Could not load persistent state: "
2356
"EOFError: {0}".format(e))
2386
"EOFError:", exc_info=e)
2358
2388
with PGPEngine() as pgp:
2359
2389
for client_name, client in clients_data.iteritems():
2557
2586
pickle.dump((clients, client_settings), stored_state)
2558
2587
os.rename(tempname, stored_state_path)
2559
2588
except (IOError, OSError) as e:
2560
logger.warning("Could not save persistent state: {0}"
2564
2591
os.remove(tempname)
2565
2592
except NameError:
2567
if e.errno not in set((errno.ENOENT, errno.EACCES,
2594
if e.errno in (errno.ENOENT, errno.EACCES, errno.EEXIST):
2595
logger.warning("Could not save persistent state: {0}"
2596
.format(os.strerror(e.errno)))
2598
logger.warning("Could not save persistent state:",
2571
2602
# Delete all clients, and settings from config
2599
2630
service.port = tcp_server.socket.getsockname()[1]
2601
2632
logger.info("Now listening on address %r, port %d,"
2602
" flowinfo %d, scope_id %d"
2603
% tcp_server.socket.getsockname())
2633
" flowinfo %d, scope_id %d",
2634
*tcp_server.socket.getsockname())
2605
logger.info("Now listening on address %r, port %d"
2606
% tcp_server.socket.getsockname())
2636
logger.info("Now listening on address %r, port %d",
2637
*tcp_server.socket.getsockname())
2608
2639
#service.interface = tcp_server.socket.getsockname()[3]