88
82
except ImportError:
89
83
SO_BINDTODEVICE = None
92
stored_state_file = "clients.pickle"
94
logger = logging.getLogger()
88
#logger = logging.getLogger('mandos')
89
logger = logging.Logger('mandos')
95
90
syslogger = (logging.handlers.SysLogHandler
96
91
(facility = logging.handlers.SysLogHandler.LOG_DAEMON,
97
92
address = str("/dev/log")))
100
if_nametoindex = (ctypes.cdll.LoadLibrary
101
(ctypes.util.find_library("c"))
103
except (OSError, AttributeError):
104
def if_nametoindex(interface):
105
"Get an interface index the hard way, i.e. using fcntl()"
106
SIOCGIFINDEX = 0x8933 # From /usr/include/linux/sockios.h
107
with contextlib.closing(socket.socket()) as s:
108
ifreq = fcntl.ioctl(s, SIOCGIFINDEX,
109
struct.pack(str("16s16x"),
111
interface_index = struct.unpack(str("I"),
113
return interface_index
116
def initlogger(debug, level=logging.WARNING):
117
"""init logger and add loglevel"""
119
syslogger.setFormatter(logging.Formatter
120
('Mandos [%(process)d]: %(levelname)s:'
122
logger.addHandler(syslogger)
125
console = logging.StreamHandler()
126
console.setFormatter(logging.Formatter('%(asctime)s %(name)s'
130
logger.addHandler(console)
131
logger.setLevel(level)
134
class PGPError(Exception):
135
"""Exception if encryption/decryption fails"""
139
class PGPEngine(object):
140
"""A simple class for OpenPGP symmetric encryption & decryption"""
142
self.gnupg = GnuPGInterface.GnuPG()
143
self.tempdir = tempfile.mkdtemp(prefix="mandos-")
144
self.gnupg = GnuPGInterface.GnuPG()
145
self.gnupg.options.meta_interactive = False
146
self.gnupg.options.homedir = self.tempdir
147
self.gnupg.options.extra_args.extend(['--force-mdc',
154
def __exit__(self, exc_type, exc_value, traceback):
162
if self.tempdir is not None:
163
# Delete contents of tempdir
164
for root, dirs, files in os.walk(self.tempdir,
166
for filename in files:
167
os.remove(os.path.join(root, filename))
169
os.rmdir(os.path.join(root, dirname))
171
os.rmdir(self.tempdir)
174
def password_encode(self, password):
175
# Passphrase can not be empty and can not contain newlines or
176
# NUL bytes. So we prefix it and hex encode it.
177
return b"mandos" + binascii.hexlify(password)
179
def encrypt(self, data, password):
180
self.gnupg.passphrase = self.password_encode(password)
181
with open(os.devnull, "w") as devnull:
183
proc = self.gnupg.run(['--symmetric'],
184
create_fhs=['stdin', 'stdout'],
185
attach_fhs={'stderr': devnull})
186
with contextlib.closing(proc.handles['stdin']) as f:
188
with contextlib.closing(proc.handles['stdout']) as f:
189
ciphertext = f.read()
193
self.gnupg.passphrase = None
196
def decrypt(self, data, password):
197
self.gnupg.passphrase = self.password_encode(password)
198
with open(os.devnull, "w") as devnull:
200
proc = self.gnupg.run(['--decrypt'],
201
create_fhs=['stdin', 'stdout'],
202
attach_fhs={'stderr': devnull})
203
with contextlib.closing(proc.handles['stdin']) as f:
205
with contextlib.closing(proc.handles['stdout']) as f:
206
decrypted_plaintext = f.read()
210
self.gnupg.passphrase = None
211
return decrypted_plaintext
93
syslogger.setFormatter(logging.Formatter
94
('Mandos [%(process)d]: %(levelname)s:'
96
logger.addHandler(syslogger)
98
console = logging.StreamHandler()
99
console.setFormatter(logging.Formatter('%(name)s [%(process)d]:'
102
logger.addHandler(console)
214
104
class AvahiError(Exception):
215
105
def __init__(self, value, *args, **kwargs):
426
298
interval: datetime.timedelta(); How often to start a new checker
427
299
last_approval_request: datetime.datetime(); (UTC) or None
428
300
last_checked_ok: datetime.datetime(); (UTC) or None
429
last_checker_status: integer between 0 and 255 reflecting exit
430
status of last checker. -1 reflects crashed
431
checker, -2 means no checker completed yet.
432
last_enabled: datetime.datetime(); (UTC) or None
301
last_enabled: datetime.datetime(); (UTC)
433
302
name: string; from the config file, used in log messages and
434
303
D-Bus identifiers
435
304
secret: bytestring; sent verbatim (over TLS) to client
436
305
timeout: datetime.timedelta(); How long from last_checked_ok
437
306
until this client is disabled
438
extended_timeout: extra long timeout when secret has been sent
307
extended_timeout: extra long timeout when password has been sent
439
308
runtime_expansions: Allowed attributes for runtime expansion.
440
309
expires: datetime.datetime(); time (UTC) when a client will be
441
310
disabled, or None
444
313
runtime_expansions = ("approval_delay", "approval_duration",
445
"created", "enabled", "expires",
446
"fingerprint", "host", "interval",
447
"last_approval_request", "last_checked_ok",
314
"created", "enabled", "fingerprint",
315
"host", "interval", "last_checked_ok",
448
316
"last_enabled", "name", "timeout")
449
client_defaults = { "timeout": "5m",
450
"extended_timeout": "15m",
452
"checker": "fping -q -- %%(host)s",
454
"approval_delay": "0s",
455
"approval_duration": "1s",
456
"approved_by_default": "True",
460
318
def timeout_milliseconds(self):
461
319
"Return the 'timeout' attribute in milliseconds"
462
return timedelta_to_milliseconds(self.timeout)
320
return _timedelta_to_milliseconds(self.timeout)
464
322
def extended_timeout_milliseconds(self):
465
323
"Return the 'extended_timeout' attribute in milliseconds"
466
return timedelta_to_milliseconds(self.extended_timeout)
324
return _timedelta_to_milliseconds(self.extended_timeout)
468
326
def interval_milliseconds(self):
469
327
"Return the 'interval' attribute in milliseconds"
470
return timedelta_to_milliseconds(self.interval)
328
return _timedelta_to_milliseconds(self.interval)
472
330
def approval_delay_milliseconds(self):
473
return timedelta_to_milliseconds(self.approval_delay)
476
def config_parser(config):
477
"""Construct a new dict of client settings of this form:
478
{ client_name: {setting_name: value, ...}, ...}
479
with exceptions for any special settings as defined above.
480
NOTE: Must be a pure function. Must return the same result
481
value given the same arguments.
484
for client_name in config.sections():
485
section = dict(config.items(client_name))
486
client = settings[client_name] = {}
488
client["host"] = section["host"]
489
# Reformat values from string types to Python types
490
client["approved_by_default"] = config.getboolean(
491
client_name, "approved_by_default")
492
client["enabled"] = config.getboolean(client_name,
495
client["fingerprint"] = (section["fingerprint"].upper()
497
if "secret" in section:
498
client["secret"] = section["secret"].decode("base64")
499
elif "secfile" in section:
500
with open(os.path.expanduser(os.path.expandvars
501
(section["secfile"])),
503
client["secret"] = secfile.read()
505
raise TypeError("No secret or secfile for section {0}"
507
client["timeout"] = string_to_delta(section["timeout"])
508
client["extended_timeout"] = string_to_delta(
509
section["extended_timeout"])
510
client["interval"] = string_to_delta(section["interval"])
511
client["approval_delay"] = string_to_delta(
512
section["approval_delay"])
513
client["approval_duration"] = string_to_delta(
514
section["approval_duration"])
515
client["checker_command"] = section["checker"]
516
client["last_approval_request"] = None
517
client["last_checked_ok"] = None
518
client["last_checker_status"] = -2
522
def __init__(self, settings, name = None):
331
return _timedelta_to_milliseconds(self.approval_delay)
333
def __init__(self, name = None, disable_hook=None, config=None):
334
"""Note: the 'checker' key in 'config' sets the
335
'checker_command' attribute and *not* the 'checker'
524
# adding all client settings
525
for setting, value in settings.iteritems():
526
setattr(self, setting, value)
529
if not hasattr(self, "last_enabled"):
530
self.last_enabled = datetime.datetime.utcnow()
531
if not hasattr(self, "expires"):
532
self.expires = (datetime.datetime.utcnow()
535
self.last_enabled = None
538
340
logger.debug("Creating client %r", self.name)
539
341
# Uppercase and remove spaces from fingerprint for later
540
342
# comparison purposes with return value from the fingerprint()
344
self.fingerprint = (config["fingerprint"].upper()
542
346
logger.debug(" Fingerprint: %s", self.fingerprint)
543
self.created = settings.get("created",
544
datetime.datetime.utcnow())
546
# attributes specific for this server instance
347
if "secret" in config:
348
self.secret = config["secret"].decode("base64")
349
elif "secfile" in config:
350
with open(os.path.expanduser(os.path.expandvars
351
(config["secfile"])),
353
self.secret = secfile.read()
355
raise TypeError("No secret or secfile for client %s"
357
self.host = config.get("host", "")
358
self.created = datetime.datetime.utcnow()
360
self.last_approval_request = None
361
self.last_enabled = None
362
self.last_checked_ok = None
363
self.timeout = string_to_delta(config["timeout"])
364
self.extended_timeout = string_to_delta(config["extended_timeout"])
365
self.interval = string_to_delta(config["interval"])
366
self.disable_hook = disable_hook
547
367
self.checker = None
548
368
self.checker_initiator_tag = None
549
369
self.disable_initiator_tag = None
550
371
self.checker_callback_tag = None
372
self.checker_command = config["checker"]
551
373
self.current_checker_command = None
374
self.last_connect = None
375
self._approved = None
376
self.approved_by_default = config.get("approved_by_default",
553
378
self.approvals_pending = 0
554
self.changedstate = (multiprocessing_manager
555
.Condition(multiprocessing_manager
557
self.client_structure = [attr for attr in
558
self.__dict__.iterkeys()
559
if not attr.startswith("_")]
560
self.client_structure.append("client_structure")
562
for name, t in inspect.getmembers(type(self),
566
if not name.startswith("_"):
567
self.client_structure.append(name)
379
self.approval_delay = string_to_delta(
380
config["approval_delay"])
381
self.approval_duration = string_to_delta(
382
config["approval_duration"])
383
self.changedstate = multiprocessing_manager.Condition(multiprocessing_manager.Lock())
569
# Send notice to process children that client state has changed
570
385
def send_changedstate(self):
571
with self.changedstate:
572
self.changedstate.notify_all()
386
self.changedstate.acquire()
387
self.changedstate.notify_all()
388
self.changedstate.release()
574
390
def enable(self):
575
391
"""Start this client's checker and timeout hooks"""
576
392
if getattr(self, "enabled", False):
577
393
# Already enabled
395
self.send_changedstate()
396
# Schedule a new checker to be started an 'interval' from now,
397
# and every interval from then on.
398
self.checker_initiator_tag = (gobject.timeout_add
399
(self.interval_milliseconds(),
401
# Schedule a disable() when 'timeout' has passed
579
402
self.expires = datetime.datetime.utcnow() + self.timeout
403
self.disable_initiator_tag = (gobject.timeout_add
404
(self.timeout_milliseconds(),
580
406
self.enabled = True
581
407
self.last_enabled = datetime.datetime.utcnow()
583
self.send_changedstate()
408
# Also start a new checker *right now*.
585
411
def disable(self, quiet=True):
586
412
"""Disable this client."""
587
413
if not getattr(self, "enabled", False):
416
self.send_changedstate()
590
418
logger.info("Disabling client %s", self.name)
591
if getattr(self, "disable_initiator_tag", None) is not None:
419
if getattr(self, "disable_initiator_tag", False):
592
420
gobject.source_remove(self.disable_initiator_tag)
593
421
self.disable_initiator_tag = None
594
422
self.expires = None
595
if getattr(self, "checker_initiator_tag", None) is not None:
423
if getattr(self, "checker_initiator_tag", False):
596
424
gobject.source_remove(self.checker_initiator_tag)
597
425
self.checker_initiator_tag = None
598
426
self.stop_checker()
427
if self.disable_hook:
428
self.disable_hook(self)
599
429
self.enabled = False
601
self.send_changedstate()
602
430
# Do not run this again if called by a gobject.timeout_add
605
433
def __del__(self):
434
self.disable_hook = None
608
def init_checker(self):
609
# Schedule a new checker to be started an 'interval' from now,
610
# and every interval from then on.
611
if self.checker_initiator_tag is not None:
612
gobject.source_remove(self.checker_initiator_tag)
613
self.checker_initiator_tag = (gobject.timeout_add
614
(self.interval_milliseconds(),
616
# Schedule a disable() when 'timeout' has passed
617
if self.disable_initiator_tag is not None:
618
gobject.source_remove(self.disable_initiator_tag)
619
self.disable_initiator_tag = (gobject.timeout_add
620
(self.timeout_milliseconds(),
622
# Also start a new checker *right now*.
625
437
def checker_callback(self, pid, condition, command):
626
438
"""The checker has completed, so take appropriate actions."""
627
439
self.checker_callback_tag = None
628
440
self.checker = None
629
441
if os.WIFEXITED(condition):
630
self.last_checker_status = os.WEXITSTATUS(condition)
631
if self.last_checker_status == 0:
442
exitstatus = os.WEXITSTATUS(condition)
632
444
logger.info("Checker for %(name)s succeeded",
634
446
self.checked_ok()
1012
743
except (AttributeError, xml.dom.DOMException,
1013
744
xml.parsers.expat.ExpatError) as error:
1014
745
logger.error("Failed to override Introspection method",
1016
747
return xmlstring
1019
def datetime_to_dbus(dt, variant_level=0):
750
def datetime_to_dbus (dt, variant_level=0):
1020
751
"""Convert a UTC datetime.datetime() to a D-Bus type."""
1022
753
return dbus.String("", variant_level = variant_level)
1023
754
return dbus.String(dt.isoformat(),
1024
755
variant_level=variant_level)
1027
def alternate_dbus_interfaces(alt_interface_names, deprecate=True):
1028
"""A class decorator; applied to a subclass of
1029
dbus.service.Object, it will add alternate D-Bus attributes with
1030
interface names according to the "alt_interface_names" mapping.
1033
@alternate_dbus_interfaces({"org.example.Interface":
1034
"net.example.AlternateInterface"})
1035
class SampleDBusObject(dbus.service.Object):
1036
@dbus.service.method("org.example.Interface")
1037
def SampleDBusMethod():
1040
The above "SampleDBusMethod" on "SampleDBusObject" will be
1041
reachable via two interfaces: "org.example.Interface" and
1042
"net.example.AlternateInterface", the latter of which will have
1043
its D-Bus annotation "org.freedesktop.DBus.Deprecated" set to
1044
"true", unless "deprecate" is passed with a False value.
1046
This works for methods and signals, and also for D-Bus properties
1047
(from DBusObjectWithProperties) and interfaces (from the
1048
dbus_interface_annotations decorator).
757
class AlternateDBusNamesMetaclass(DBusObjectWithProperties.__metaclass__):
758
"""Applied to an empty subclass of a D-Bus object, this metaclass
759
will add additional D-Bus attributes matching a certain pattern.
1051
for orig_interface_name, alt_interface_name in (
1052
alt_interface_names.iteritems()):
1054
interface_names = set()
1055
# Go though all attributes of the class
1056
for attrname, attribute in inspect.getmembers(cls):
761
def __new__(mcs, name, bases, attr):
762
# Go through all the base classes which could have D-Bus
763
# methods, signals, or properties in them
764
for base in (b for b in bases
765
if issubclass(b, dbus.service.Object)):
766
# Go though all attributes of the base class
767
for attrname, attribute in inspect.getmembers(base):
1057
768
# Ignore non-D-Bus attributes, and D-Bus attributes
1058
769
# with the wrong interface name
1059
770
if (not hasattr(attribute, "_dbus_interface")
1060
771
or not attribute._dbus_interface
1061
.startswith(orig_interface_name)):
772
.startswith("se.recompile.Mandos")):
1063
774
# Create an alternate D-Bus interface name based on
1064
775
# the current name
1065
776
alt_interface = (attribute._dbus_interface
1066
.replace(orig_interface_name,
1067
alt_interface_name))
1068
interface_names.add(alt_interface)
777
.replace("se.recompile.Mandos",
778
"se.bsnet.fukt.Mandos"))
1069
779
# Is this a D-Bus signal?
1070
780
if getattr(attribute, "_dbus_is_signal", False):
1071
781
# Extract the original non-method function by
1257
912
last_enabled = notifychangeproperty(datetime_to_dbus,
1259
914
checker = notifychangeproperty(dbus.Boolean, "CheckerRunning",
1260
type_func = lambda checker:
1261
checker is not None)
915
type_func = lambda checker: checker is not None)
1262
916
last_checked_ok = notifychangeproperty(datetime_to_dbus,
1263
917
"LastCheckedOK")
1264
last_checker_status = notifychangeproperty(dbus.Int16,
1265
"LastCheckerStatus")
1266
last_approval_request = notifychangeproperty(
1267
datetime_to_dbus, "LastApprovalRequest")
918
last_approval_request = notifychangeproperty(datetime_to_dbus,
919
"LastApprovalRequest")
1268
920
approved_by_default = notifychangeproperty(dbus.Boolean,
1269
921
"ApprovedByDefault")
1270
approval_delay = notifychangeproperty(dbus.UInt64,
1273
timedelta_to_milliseconds)
1274
approval_duration = notifychangeproperty(
1275
dbus.UInt64, "ApprovalDuration",
1276
type_func = timedelta_to_milliseconds)
922
approval_delay = notifychangeproperty(dbus.UInt16, "ApprovalDelay",
923
type_func = _timedelta_to_milliseconds)
924
approval_duration = notifychangeproperty(dbus.UInt16, "ApprovalDuration",
925
type_func = _timedelta_to_milliseconds)
1277
926
host = notifychangeproperty(dbus.String, "Host")
1278
timeout = notifychangeproperty(dbus.UInt64, "Timeout",
1280
timedelta_to_milliseconds)
1281
extended_timeout = notifychangeproperty(
1282
dbus.UInt64, "ExtendedTimeout",
1283
type_func = timedelta_to_milliseconds)
1284
interval = notifychangeproperty(dbus.UInt64,
1287
timedelta_to_milliseconds)
927
timeout = notifychangeproperty(dbus.UInt16, "Timeout",
928
type_func = _timedelta_to_milliseconds)
929
extended_timeout = notifychangeproperty(dbus.UInt16, "ExtendedTimeout",
930
type_func = _timedelta_to_milliseconds)
931
interval = notifychangeproperty(dbus.UInt16, "Interval",
932
type_func = _timedelta_to_milliseconds)
1288
933
checker_command = notifychangeproperty(dbus.String, "Checker")
1290
935
del notifychangeproperty
1999
1597
def __init__(self, server_address, RequestHandlerClass,
2000
1598
interface=None, use_ipv6=True, clients=None,
2001
gnutls_priority=None, use_dbus=True, socketfd=None):
1599
gnutls_priority=None, use_dbus=True):
2002
1600
self.enabled = False
2003
1601
self.clients = clients
2004
1602
if self.clients is None:
1603
self.clients = set()
2006
1604
self.use_dbus = use_dbus
2007
1605
self.gnutls_priority = gnutls_priority
2008
1606
IPv6_TCPServer.__init__(self, server_address,
2009
1607
RequestHandlerClass,
2010
1608
interface = interface,
2011
use_ipv6 = use_ipv6,
2012
socketfd = socketfd)
1609
use_ipv6 = use_ipv6)
2013
1610
def server_activate(self):
2014
1611
if self.enabled:
2015
1612
return socketserver.TCPServer.server_activate(self)
2017
1613
def enable(self):
2018
1614
self.enabled = True
2020
def add_pipe(self, parent_pipe, proc):
1615
def add_pipe(self, parent_pipe):
2021
1616
# Call "handle_ipc" for both data and EOF events
2022
1617
gobject.io_add_watch(parent_pipe.fileno(),
2023
1618
gobject.IO_IN | gobject.IO_HUP,
2024
1619
functools.partial(self.handle_ipc,
1620
parent_pipe = parent_pipe))
2029
1622
def handle_ipc(self, source, condition, parent_pipe=None,
2030
proc = None, client_object=None):
2031
# error, or the other end of multiprocessing.Pipe has closed
2032
if condition & (gobject.IO_ERR | gobject.IO_HUP):
2033
# Wait for other process to exit
1623
client_object=None):
1625
gobject.IO_IN: "IN", # There is data to read.
1626
gobject.IO_OUT: "OUT", # Data can be written (without
1628
gobject.IO_PRI: "PRI", # There is urgent data to read.
1629
gobject.IO_ERR: "ERR", # Error condition.
1630
gobject.IO_HUP: "HUP" # Hung up (the connection has been
1631
# broken, usually for pipes and
1634
conditions_string = ' | '.join(name
1636
condition_names.iteritems()
1637
if cond & condition)
1638
# error or the other end of multiprocessing.Pipe has closed
1639
if condition & (gobject.IO_ERR | condition & gobject.IO_HUP):
2037
1642
# Read a request from the child
2399
1998
client_class = Client
2401
client_class = functools.partial(ClientDBus, bus = bus)
2403
client_settings = Client.config_parser(client_config)
2404
old_client_settings = {}
2407
# Get client data and settings from last running state.
2408
if server_settings["restore"]:
2410
with open(stored_state_path, "rb") as stored_state:
2411
clients_data, old_client_settings = (pickle.load
2413
os.remove(stored_state_path)
2414
except IOError as e:
2415
if e.errno == errno.ENOENT:
2416
logger.warning("Could not load persistent state: {0}"
2417
.format(os.strerror(e.errno)))
2419
logger.critical("Could not load persistent state:",
2422
except EOFError as e:
2423
logger.warning("Could not load persistent state: "
2424
"EOFError:", exc_info=e)
2426
with PGPEngine() as pgp:
2427
for client_name, client in clients_data.iteritems():
2428
# Decide which value to use after restoring saved state.
2429
# We have three different values: Old config file,
2430
# new config file, and saved state.
2431
# New config value takes precedence if it differs from old
2432
# config value, otherwise use saved state.
2433
for name, value in client_settings[client_name].items():
2435
# For each value in new config, check if it
2436
# differs from the old config value (Except for
2437
# the "secret" attribute)
2438
if (name != "secret" and
2439
value != old_client_settings[client_name]
2441
client[name] = value
2445
# Clients who has passed its expire date can still be
2446
# enabled if its last checker was successful. Clients
2447
# whose checker succeeded before we stored its state is
2448
# assumed to have successfully run all checkers during
2450
if client["enabled"]:
2451
if datetime.datetime.utcnow() >= client["expires"]:
2452
if not client["last_checked_ok"]:
2454
"disabling client {0} - Client never "
2455
"performed a successful checker"
2456
.format(client_name))
2457
client["enabled"] = False
2458
elif client["last_checker_status"] != 0:
2460
"disabling client {0} - Client "
2461
"last checker failed with error code {1}"
2462
.format(client_name,
2463
client["last_checker_status"]))
2464
client["enabled"] = False
2466
client["expires"] = (datetime.datetime
2468
+ client["timeout"])
2469
logger.debug("Last checker succeeded,"
2470
" keeping {0} enabled"
2471
.format(client_name))
2000
client_class = functools.partial(ClientDBusTransitional, bus = bus)
2001
def client_config_items(config, section):
2002
special_settings = {
2003
"approved_by_default":
2004
lambda: config.getboolean(section,
2005
"approved_by_default"),
2007
for name, value in config.items(section):
2473
client["secret"] = (
2474
pgp.decrypt(client["encrypted_secret"],
2475
client_settings[client_name]
2478
# If decryption fails, we use secret from new settings
2479
logger.debug("Failed to decrypt {0} old secret"
2480
.format(client_name))
2481
client["secret"] = (
2482
client_settings[client_name]["secret"])
2484
# Add/remove clients based on new changes made to config
2485
for client_name in (set(old_client_settings)
2486
- set(client_settings)):
2487
del clients_data[client_name]
2488
for client_name in (set(client_settings)
2489
- set(old_client_settings)):
2490
clients_data[client_name] = client_settings[client_name]
2492
# Create all client objects
2493
for client_name, client in clients_data.iteritems():
2494
tcp_server.clients[client_name] = client_class(
2495
name = client_name, settings = client)
2009
yield (name, special_settings[name]())
2013
tcp_server.clients.update(set(
2014
client_class(name = section,
2015
config= dict(client_config_items(
2016
client_config, section)))
2017
for section in client_config.sections()))
2497
2018
if not tcp_server.clients:
2498
2019
logger.warning("No clients defined")
2579
mandos_dbus_service = MandosDBusService()
2093
class MandosDBusServiceTransitional(MandosDBusService):
2094
__metaclass__ = AlternateDBusNamesMetaclass
2095
mandos_dbus_service = MandosDBusServiceTransitional()
2582
2098
"Cleanup function; run on exit"
2583
2099
service.cleanup()
2585
multiprocessing.active_children()
2586
if not (tcp_server.clients or client_settings):
2589
# Store client before exiting. Secrets are encrypted with key
2590
# based on what config file has. If config file is
2591
# removed/edited, old secret will thus be unrecovable.
2593
with PGPEngine() as pgp:
2594
for client in tcp_server.clients.itervalues():
2595
key = client_settings[client.name]["secret"]
2596
client.encrypted_secret = pgp.encrypt(client.secret,
2600
# A list of attributes that can not be pickled
2602
exclude = set(("bus", "changedstate", "secret",
2604
for name, typ in (inspect.getmembers
2605
(dbus.service.Object)):
2608
client_dict["encrypted_secret"] = (client
2610
for attr in client.client_structure:
2611
if attr not in exclude:
2612
client_dict[attr] = getattr(client, attr)
2614
clients[client.name] = client_dict
2615
del client_settings[client.name]["secret"]
2618
with (tempfile.NamedTemporaryFile
2619
(mode='wb', suffix=".pickle", prefix='clients-',
2620
dir=os.path.dirname(stored_state_path),
2621
delete=False)) as stored_state:
2622
pickle.dump((clients, client_settings), stored_state)
2623
tempname=stored_state.name
2624
os.rename(tempname, stored_state_path)
2625
except (IOError, OSError) as e:
2631
if e.errno in (errno.ENOENT, errno.EACCES, errno.EEXIST):
2632
logger.warning("Could not save persistent state: {0}"
2633
.format(os.strerror(e.errno)))
2635
logger.warning("Could not save persistent state:",
2639
# Delete all clients, and settings from config
2640
2101
while tcp_server.clients:
2641
name, client = tcp_server.clients.popitem()
2102
client = tcp_server.clients.pop()
2643
2104
client.remove_from_connection()
2105
client.disable_hook = None
2644
2106
# Don't signal anything except ClientRemoved
2645
2107
client.disable(quiet=True)
2647
2109
# Emit D-Bus signal
2648
mandos_dbus_service.ClientRemoved(client
2110
mandos_dbus_service.ClientRemoved(client.dbus_object_path,
2651
client_settings.clear()
2653
2113
atexit.register(cleanup)
2655
for client in tcp_server.clients.itervalues():
2115
for client in tcp_server.clients:
2657
2117
# Emit D-Bus signal
2658
2118
mandos_dbus_service.ClientAdded(client.dbus_object_path)
2659
# Need to initiate checking of clients
2661
client.init_checker()
2663
2121
tcp_server.enable()
2664
2122
tcp_server.server_activate()