694
697
command = self.checker_command % escaped_attrs
695
698
except TypeError as error:
696
logger.error('Could not format string "%s":'
697
' %s', self.checker_command, error)
699
logger.error('Could not format string "%s"',
700
self.checker_command, exc_info=error)
698
701
return True # Try again later
699
702
self.current_checker_command = command
797
def dbus_annotations(annotations):
798
"""Decorator to annotate D-Bus methods, signals or properties
801
@dbus_service_property("org.example.Interface", signature="b",
803
@dbus_annotations({{"org.freedesktop.DBus.Deprecated": "true",
804
"org.freedesktop.DBus.Property."
805
"EmitsChangedSignal": "false"})
806
def Property_dbus_property(self):
807
return dbus.Boolean(False)
810
func._dbus_annotations = annotations
794
815
class DBusPropertyException(dbus.exceptions.DBusException):
795
816
"""A base class for D-Bus property-related exceptions
936
958
if prop._dbus_interface
937
959
== if_tag.getAttribute("name")):
938
960
if_tag.appendChild(tag)
961
# Add annotation tags
962
for typ in ("method", "signal", "property"):
963
for tag in if_tag.getElementsByTagName(typ):
965
for name, prop in (self.
966
_get_all_dbus_things(typ)):
967
if (name == tag.getAttribute("name")
968
and prop._dbus_interface
969
== if_tag.getAttribute("name")):
970
annots.update(getattr
974
for name, value in annots.iteritems():
975
ann_tag = document.createElement(
977
ann_tag.setAttribute("name", name)
978
ann_tag.setAttribute("value", value)
979
tag.appendChild(ann_tag)
939
980
# Add interface annotation tags
940
981
for annotation, value in dict(
944
985
self._get_all_dbus_things("interface")
945
986
if name == if_tag.getAttribute("name")
947
attr_tag = document.createElement("annotation")
948
attr_tag.setAttribute("name", annotation)
949
attr_tag.setAttribute("value", value)
950
if_tag.appendChild(attr_tag)
988
ann_tag = document.createElement("annotation")
989
ann_tag.setAttribute("name", annotation)
990
ann_tag.setAttribute("value", value)
991
if_tag.appendChild(ann_tag)
951
992
# Add the names to the return values for the
952
993
# "org.freedesktop.DBus.Properties" methods
953
994
if (if_tag.getAttribute("name")
980
1021
variant_level=variant_level)
983
class AlternateDBusNamesMetaclass(DBusObjectWithProperties
985
"""Applied to an empty subclass of a D-Bus object, this metaclass
986
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).
988
def __new__(mcs, name, bases, attr):
989
# Go through all the base classes which could have D-Bus
990
# methods, signals, or properties in them
991
old_interface_names = []
992
for base in (b for b in bases
993
if issubclass(b, dbus.service.Object)):
994
# Go though all attributes of the base class
995
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):
996
1054
# Ignore non-D-Bus attributes, and D-Bus attributes
997
1055
# with the wrong interface name
998
1056
if (not hasattr(attribute, "_dbus_interface")
999
1057
or not attribute._dbus_interface
1000
.startswith("se.recompile.Mandos")):
1058
.startswith(orig_interface_name)):
1002
1060
# Create an alternate D-Bus interface name based on
1003
1061
# the current name
1004
1062
alt_interface = (attribute._dbus_interface
1005
.replace("se.recompile.Mandos",
1006
"se.bsnet.fukt.Mandos"))
1007
if alt_interface != attribute._dbus_interface:
1008
old_interface_names.append(alt_interface)
1063
.replace(orig_interface_name,
1064
alt_interface_name))
1065
interface_names.add(alt_interface)
1009
1066
# Is this a D-Bus signal?
1010
1067
if getattr(attribute, "_dbus_is_signal", False):
1011
1068
# Extract the original non-method function by
1026
1083
nonmethod_func.func_name,
1027
1084
nonmethod_func.func_defaults,
1028
1085
nonmethod_func.func_closure)))
1086
# Copy annotations, if any
1088
new_function._dbus_annotations = (
1089
dict(attribute._dbus_annotations))
1090
except AttributeError:
1029
1092
# Define a creator of a function to call both the
1030
# old and new functions, so both the old and new
1031
# 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
1032
1096
def fixscope(func1, func2):
1033
1097
"""This function is a scope container to pass
1034
1098
func1 and func2 to the "call_both" function
1041
1105
return call_both
1042
1106
# Create the "call_both" function and add it to
1044
attr[attrname] = fixscope(attribute,
1108
attr[attrname] = fixscope(attribute, new_function)
1046
1109
# Is this a D-Bus method?
1047
1110
elif getattr(attribute, "_dbus_is_method", False):
1048
1111
# Create a new, but exactly alike, function
1059
1122
attribute.func_name,
1060
1123
attribute.func_defaults,
1061
1124
attribute.func_closure)))
1125
# Copy annotations, if any
1127
attr[attrname]._dbus_annotations = (
1128
dict(attribute._dbus_annotations))
1129
except AttributeError:
1062
1131
# Is this a D-Bus property?
1063
1132
elif getattr(attribute, "_dbus_is_property", False):
1064
1133
# Create a new, but exactly alike, function
1078
1147
attribute.func_name,
1079
1148
attribute.func_defaults,
1080
1149
attribute.func_closure)))
1150
# Copy annotations, if any
1152
attr[attrname]._dbus_annotations = (
1153
dict(attribute._dbus_annotations))
1154
except AttributeError:
1081
1156
# Is this a D-Bus interface?
1082
1157
elif getattr(attribute, "_dbus_is_interface", False):
1083
1158
# Create a new, but exactly alike, function
1092
1167
attribute.func_name,
1093
1168
attribute.func_defaults,
1094
1169
attribute.func_closure)))
1095
# Deprecate all old interfaces
1096
basename="_AlternateDBusNamesMetaclass_interface_annotation{0}"
1097
for old_interface_name in old_interface_names:
1098
@dbus_interface_annotations(old_interface_name)
1100
return { "org.freedesktop.DBus.Deprecated": "true" }
1101
# Find an unused name
1102
for aname in (basename.format(i) for i in
1104
if aname not in attr:
1107
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"})
1110
1195
class ClientDBus(Client, DBusObjectWithProperties):
1111
1196
"""A Client class using D-Bus
2046
2128
null = os.open(os.devnull, os.O_NOCTTY | os.O_RDWR)
2047
2129
if not stat.S_ISCHR(os.fstat(null).st_mode):
2048
2130
raise OSError(errno.ENODEV,
2049
"%s not a character device"
2131
"{0} not a character device"
2132
.format(os.devnull))
2051
2133
os.dup2(null, sys.stdin.fileno())
2052
2134
os.dup2(null, sys.stdout.fileno())
2053
2135
os.dup2(null, sys.stderr.fileno())
2063
2145
parser = argparse.ArgumentParser()
2064
2146
parser.add_argument("-v", "--version", action="version",
2065
version = "%%(prog)s %s" % version,
2147
version = "%(prog)s {0}".format(version),
2066
2148
help="show version number and exit")
2067
2149
parser.add_argument("-i", "--interface", metavar="IF",
2068
2150
help="Bind to interface IF")
2172
2254
if server_settings["servicename"] != "Mandos":
2173
2255
syslogger.setFormatter(logging.Formatter
2174
('Mandos (%s) [%%(process)d]:'
2175
' %%(levelname)s: %%(message)s'
2176
% server_settings["servicename"]))
2256
('Mandos ({0}) [%(process)d]:'
2257
' %(levelname)s: %(message)s'
2258
.format(server_settings
2178
2261
# Parse config file with clients
2179
2262
client_config = configparser.SafeConfigParser(Client
2197
2280
pidfilename = "/var/run/mandos.pid"
2199
2282
pidfile = open(pidfilename, "w")
2201
logger.error("Could not open file %r", pidfilename)
2283
except IOError as e:
2284
logger.error("Could not open file %r", pidfilename,
2204
uid = pwd.getpwnam("_mandos").pw_uid
2205
gid = pwd.getpwnam("_mandos").pw_gid
2287
for name in ("_mandos", "mandos", "nobody"):
2208
uid = pwd.getpwnam("mandos").pw_uid
2209
gid = pwd.getpwnam("mandos").pw_gid
2289
uid = pwd.getpwnam(name).pw_uid
2290
gid = pwd.getpwnam(name).pw_gid
2210
2292
except KeyError:
2212
uid = pwd.getpwnam("nobody").pw_uid
2213
gid = pwd.getpwnam("nobody").pw_gid
2295
2374
(stored_state))
2296
2375
os.remove(stored_state_path)
2297
2376
except IOError as e:
2298
logger.warning("Could not load persistent state: {0}"
2300
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:",
2302
2384
except EOFError as e:
2303
2385
logger.warning("Could not load persistent state: "
2304
"EOFError: {0}".format(e))
2386
"EOFError:", exc_info=e)
2306
2388
with PGPEngine() as pgp:
2307
2389
for client_name, client in clients_data.iteritems():
2505
2586
pickle.dump((clients, client_settings), stored_state)
2506
2587
os.rename(tempname, stored_state_path)
2507
2588
except (IOError, OSError) as e:
2508
logger.warning("Could not save persistent state: {0}"
2512
2591
os.remove(tempname)
2513
2592
except NameError:
2515
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:",
2519
2602
# Delete all clients, and settings from config
2547
2630
service.port = tcp_server.socket.getsockname()[1]
2549
2632
logger.info("Now listening on address %r, port %d,"
2550
" flowinfo %d, scope_id %d"
2551
% tcp_server.socket.getsockname())
2633
" flowinfo %d, scope_id %d",
2634
*tcp_server.socket.getsockname())
2553
logger.info("Now listening on address %r, port %d"
2554
% tcp_server.socket.getsockname())
2636
logger.info("Now listening on address %r, port %d",
2637
*tcp_server.socket.getsockname())
2556
2639
#service.interface = tcp_server.socket.getsockname()[3]