/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: Björn Påhlsson
  • Date: 2011-12-21 00:29:33 UTC
  • mfrom: (518.1.15 mandos-persistent)
  • Revision ID: belorn@recompile.se-20111221002933-6sc0a64o1ns2s2s8
Working persistant state

Show diffs side-by-side

added added

removed removed

Lines of Context:
85
85
    except ImportError:
86
86
        SO_BINDTODEVICE = None
87
87
 
88
 
 
89
88
version = "1.4.1"
90
89
stored_state_file = "clients.pickle"
91
90
 
128
127
    logger.setLevel(level)
129
128
 
130
129
 
131
 
class CryptoError(Exception):
 
130
class PGPError(Exception):
 
131
    """Exception if encryption/decryption fails"""
132
132
    pass
133
133
 
134
134
 
135
 
class Crypto(object):
 
135
class PGPEngine(object):
136
136
    """A simple class for OpenPGP symmetric encryption & decryption"""
137
137
    def __init__(self):
138
138
        self.gnupg = GnuPGInterface.GnuPG()
184
184
                    ciphertext = f.read()
185
185
                proc.wait()
186
186
            except IOError as e:
187
 
                raise CryptoError(e)
 
187
                raise PGPError(e)
188
188
        self.gnupg.passphrase = None
189
189
        return ciphertext
190
190
    
201
201
                    decrypted_plaintext = f.read()
202
202
                proc.wait()
203
203
            except IOError as e:
204
 
                raise CryptoError(e)
 
204
                raise PGPError(e)
205
205
        self.gnupg.passphrase = None
206
206
        return decrypted_plaintext
207
207
 
411
411
    interval:   datetime.timedelta(); How often to start a new checker
412
412
    last_approval_request: datetime.datetime(); (UTC) or None
413
413
    last_checked_ok: datetime.datetime(); (UTC) or None
414
 
 
415
414
    last_checker_status: integer between 0 and 255 reflecting exit
416
415
                         status of last checker. -1 reflects crashed
417
416
                         checker, or None.
431
430
                          "created", "enabled", "fingerprint",
432
431
                          "host", "interval", "last_checked_ok",
433
432
                          "last_enabled", "name", "timeout")
 
433
    client_defaults = { "timeout": "5m",
 
434
                        "extended_timeout": "15m",
 
435
                        "interval": "2m",
 
436
                        "checker": "fping -q -- %%(host)s",
 
437
                        "host": "",
 
438
                        "approval_delay": "0s",
 
439
                        "approval_duration": "1s",
 
440
                        "approved_by_default": "True",
 
441
                        "enabled": "True",
 
442
                        }
434
443
    
435
444
    def timeout_milliseconds(self):
436
445
        "Return the 'timeout' attribute in milliseconds"
446
455
    
447
456
    def approval_delay_milliseconds(self):
448
457
        return timedelta_to_milliseconds(self.approval_delay)
449
 
    
450
 
    def __init__(self, name = None, config=None):
 
458
 
 
459
    @staticmethod
 
460
    def config_parser(config):
 
461
        """ Construct a new dict of client settings of this form:
 
462
        { client_name: {setting_name: value, ...}, ...}
 
463
        with exceptions for any special settings as defined above"""
 
464
        settings = {}
 
465
        for client_name in config.sections():
 
466
            section = dict(config.items(client_name))
 
467
            client = settings[client_name] = {}
 
468
            
 
469
            client["host"] = section["host"]
 
470
            # Reformat values from string types to Python types
 
471
            client["approved_by_default"] = config.getboolean(
 
472
                client_name, "approved_by_default")
 
473
            client["enabled"] = config.getboolean(client_name, "enabled")
 
474
            
 
475
            client["fingerprint"] = (section["fingerprint"].upper()
 
476
                                     .replace(" ", ""))
 
477
            if "secret" in section:
 
478
                client["secret"] = section["secret"].decode("base64")
 
479
            elif "secfile" in section:
 
480
                with open(os.path.expanduser(os.path.expandvars
 
481
                                             (section["secfile"])),
 
482
                          "rb") as secfile:
 
483
                    client["secret"] = secfile.read()
 
484
            else:
 
485
                raise TypeError("No secret or secfile for section %s"
 
486
                                % section)
 
487
            client["timeout"] = string_to_delta(section["timeout"])
 
488
            client["extended_timeout"] = string_to_delta(
 
489
                section["extended_timeout"])
 
490
            client["interval"] = string_to_delta(section["interval"])
 
491
            client["approval_delay"] = string_to_delta(
 
492
                section["approval_delay"])
 
493
            client["approval_duration"] = string_to_delta(
 
494
                section["approval_duration"])
 
495
            client["checker_command"] = section["checker"]
 
496
            client["last_approval_request"] = None
 
497
            client["last_checked_ok"] = None
 
498
            client["last_checker_status"] = None
 
499
            if client["enabled"]:
 
500
                client["last_enabled"] = datetime.datetime.utcnow()
 
501
                client["expires"] = (datetime.datetime.utcnow()
 
502
                                     + client["timeout"])
 
503
            else:
 
504
                client["last_enabled"] = None
 
505
                client["expires"] = None
 
506
 
 
507
        return settings
 
508
        
 
509
        
 
510
    def __init__(self, settings, name = None):
451
511
        """Note: the 'checker' key in 'config' sets the
452
512
        'checker_command' attribute and *not* the 'checker'
453
513
        attribute."""
454
514
        self.name = name
455
 
        if config is None:
456
 
            config = {}
 
515
        # adding all client settings
 
516
        for setting, value in settings.iteritems():
 
517
            setattr(self, setting, value)
 
518
        
457
519
        logger.debug("Creating client %r", self.name)
458
520
        # Uppercase and remove spaces from fingerprint for later
459
521
        # comparison purposes with return value from the fingerprint()
460
522
        # function
461
 
        self.fingerprint = (config["fingerprint"].upper()
462
 
                            .replace(" ", ""))
463
523
        logger.debug("  Fingerprint: %s", self.fingerprint)
464
 
        if "secret" in config:
465
 
            self.secret = config["secret"].decode("base64")
466
 
        elif "secfile" in config:
467
 
            with open(os.path.expanduser(os.path.expandvars
468
 
                                         (config["secfile"])),
469
 
                      "rb") as secfile:
470
 
                self.secret = secfile.read()
471
 
        else:
472
 
            raise TypeError("No secret or secfile for client %s"
473
 
                            % self.name)
474
 
        self.host = config.get("host", "")
475
 
        self.created = datetime.datetime.utcnow()
476
 
        self.enabled = config.get("enabled", True)
477
 
        self.last_approval_request = None
478
 
        if self.enabled:
479
 
            self.last_enabled = datetime.datetime.utcnow()
480
 
        else:
481
 
            self.last_enabled = None
482
 
        self.last_checked_ok = None
483
 
        self.last_checker_status = None
484
 
        self.timeout = string_to_delta(config["timeout"])
485
 
        self.extended_timeout = string_to_delta(config
486
 
                                                ["extended_timeout"])
487
 
        self.interval = string_to_delta(config["interval"])
 
524
        self.created = settings.get("created", datetime.datetime.utcnow())
 
525
 
 
526
        # attributes specific for this server instance
488
527
        self.checker = None
489
528
        self.checker_initiator_tag = None
490
529
        self.disable_initiator_tag = None
491
 
        if self.enabled:
492
 
            self.expires = datetime.datetime.utcnow() + self.timeout
493
 
        else:
494
 
            self.expires = None
495
530
        self.checker_callback_tag = None
496
 
        self.checker_command = config["checker"]
497
531
        self.current_checker_command = None
498
532
        self.approved = None
499
 
        self.approved_by_default = config.get("approved_by_default",
500
 
                                              True)
501
533
        self.approvals_pending = 0
502
 
        self.approval_delay = string_to_delta(
503
 
            config["approval_delay"])
504
 
        self.approval_duration = string_to_delta(
505
 
            config["approval_duration"])
506
534
        self.changedstate = (multiprocessing_manager
507
535
                             .Condition(multiprocessing_manager
508
536
                                        .Lock()))
575
603
        self.checker_callback_tag = None
576
604
        self.checker = None
577
605
        if os.WIFEXITED(condition):
578
 
            self.last_checker_status =  os.WEXITSTATUS(condition)
 
606
            self.last_checker_status = os.WEXITSTATUS(condition)
579
607
            if self.last_checker_status == 0:
580
608
                logger.info("Checker for %(name)s succeeded",
581
609
                            vars(self))
1011
1039
    def __init__(self, bus = None, *args, **kwargs):
1012
1040
        self.bus = bus
1013
1041
        Client.__init__(self, *args, **kwargs)
 
1042
        self._approvals_pending = 0
1014
1043
        
1015
1044
        self._approvals_pending = 0
1016
1045
        # Only now, when this client is initialized, can it show up on
1068
1097
        datetime_to_dbus, "LastApprovalRequest")
1069
1098
    approved_by_default = notifychangeproperty(dbus.Boolean,
1070
1099
                                               "ApprovedByDefault")
1071
 
    approval_delay = notifychangeproperty(dbus.UInt16,
 
1100
    approval_delay = notifychangeproperty(dbus.UInt64,
1072
1101
                                          "ApprovalDelay",
1073
1102
                                          type_func =
1074
1103
                                          timedelta_to_milliseconds)
1075
1104
    approval_duration = notifychangeproperty(
1076
 
        dbus.UInt16, "ApprovalDuration",
 
1105
        dbus.UInt64, "ApprovalDuration",
1077
1106
        type_func = timedelta_to_milliseconds)
1078
1107
    host = notifychangeproperty(dbus.String, "Host")
1079
 
    timeout = notifychangeproperty(dbus.UInt16, "Timeout",
 
1108
    timeout = notifychangeproperty(dbus.UInt64, "Timeout",
1080
1109
                                   type_func =
1081
1110
                                   timedelta_to_milliseconds)
1082
1111
    extended_timeout = notifychangeproperty(
1083
 
        dbus.UInt16, "ExtendedTimeout",
 
1112
        dbus.UInt64, "ExtendedTimeout",
1084
1113
        type_func = timedelta_to_milliseconds)
1085
 
    interval = notifychangeproperty(dbus.UInt16,
 
1114
    interval = notifychangeproperty(dbus.UInt64,
1086
1115
                                    "Interval",
1087
1116
                                    type_func =
1088
1117
                                    timedelta_to_milliseconds)
1279
1308
    def Host_dbus_property(self, value=None):
1280
1309
        if value is None:       # get
1281
1310
            return dbus.String(self.host)
1282
 
        self.host = value
 
1311
        self.host = unicode(value)
1283
1312
    
1284
1313
    # Created - property
1285
1314
    @dbus_service_property(_interface, signature="s", access="read")
1379
1408
    def Checker_dbus_property(self, value=None):
1380
1409
        if value is None:       # get
1381
1410
            return dbus.String(self.checker_command)
1382
 
        self.checker_command = value
 
1411
        self.checker_command = unicode(value)
1383
1412
    
1384
1413
    # CheckerRunning - property
1385
1414
    @dbus_service_property(_interface, signature="b",
1664
1693
    def sub_process_main(self, request, address):
1665
1694
        try:
1666
1695
            self.finish_request(request, address)
1667
 
        except:
 
1696
        except Exception:
1668
1697
            self.handle_error(request, address)
1669
1698
        self.close_request(request)
1670
1699
    
2064
2093
                                % server_settings["servicename"]))
2065
2094
    
2066
2095
    # Parse config file with clients
2067
 
    client_defaults = { "timeout": "5m",
2068
 
                        "extended_timeout": "15m",
2069
 
                        "interval": "2m",
2070
 
                        "checker": "fping -q -- %%(host)s",
2071
 
                        "host": "",
2072
 
                        "approval_delay": "0s",
2073
 
                        "approval_duration": "1s",
2074
 
                        }
2075
 
    client_config = configparser.SafeConfigParser(client_defaults)
 
2096
    client_config = configparser.SafeConfigParser(Client.client_defaults)
2076
2097
    client_config.read(os.path.join(server_settings["configdir"],
2077
2098
                                    "clients.conf"))
2078
2099
    
2179
2200
        client_class = functools.partial(ClientDBusTransitional,
2180
2201
                                         bus = bus)
2181
2202
    
2182
 
    special_settings = {
2183
 
        # Some settings need to be accessd by special methods;
2184
 
        # booleans need .getboolean(), etc.  Here is a list of them:
2185
 
        "approved_by_default":
2186
 
            lambda section:
2187
 
            client_config.getboolean(section, "approved_by_default"),
2188
 
        "enabled":
2189
 
            lambda section:
2190
 
            client_config.getboolean(section, "enabled"),
2191
 
        }
2192
 
    # Construct a new dict of client settings of this form:
2193
 
    # { client_name: {setting_name: value, ...}, ...}
2194
 
    # with exceptions for any special settings as defined above
2195
 
    client_settings = dict((clientname,
2196
 
                           dict((setting,
2197
 
                                 (value
2198
 
                                  if setting not in special_settings
2199
 
                                  else special_settings[setting]
2200
 
                                  (clientname)))
2201
 
                                for setting, value in
2202
 
                                client_config.items(clientname)))
2203
 
                          for clientname in client_config.sections())
2204
 
    
 
2203
    client_settings = Client.config_parser(client_config)
2205
2204
    old_client_settings = {}
2206
 
    clients_data = []
 
2205
    clients_data = {}
2207
2206
    
2208
2207
    # Get client data and settings from last running state.
2209
2208
    if server_settings["restore"]:
2218
2217
            if e.errno != errno.ENOENT:
2219
2218
                raise
2220
2219
    
2221
 
    with Crypto() as crypt:
2222
 
        for client in clients_data:
2223
 
            client_name = client["name"]
2224
 
            
 
2220
    with PGPEngine() as pgp:
 
2221
        for client_name, client in clients_data.iteritems():
2225
2222
            # Decide which value to use after restoring saved state.
2226
2223
            # We have three different values: Old config file,
2227
2224
            # new config file, and saved state.
2235
2232
                    if (name != "secret" and
2236
2233
                        value != old_client_settings[client_name]
2237
2234
                        [name]):
2238
 
                        setattr(client, name, value)
 
2235
                        client[name] = value
2239
2236
                except KeyError:
2240
2237
                    pass
2241
2238
            
2242
2239
            # Clients who has passed its expire date can still be
2243
 
            # enabled if its last checker was sucessful.  Clients
 
2240
            # enabled if its last checker was successful.  Clients
2244
2241
            # whose checker failed before we stored its state is
2245
2242
            # assumed to have failed all checkers during downtime.
2246
 
            if client["enabled"] and client["last_checked_ok"]:
2247
 
                if ((datetime.datetime.utcnow()
2248
 
                     - client["last_checked_ok"])
2249
 
                    > client["interval"]):
2250
 
                    if client["last_checker_status"] != 0:
 
2243
            if client["enabled"]:
 
2244
                if datetime.datetime.utcnow() >= client["expires"]:
 
2245
                    if not client["last_checked_ok"]:
 
2246
                        logger.warning(
 
2247
                            "disabling client {0} - Client never "
 
2248
                            "performed a successfull checker"
 
2249
                            .format(client["name"]))
 
2250
                        client["enabled"] = False
 
2251
                    elif client["last_checker_status"] != 0:
 
2252
                        logger.warning(
 
2253
                            "disabling client {0} - Client "
 
2254
                            "last checker failed with error code {1}"
 
2255
                            .format(client["name"],
 
2256
                                    client["last_checker_status"]))
2251
2257
                        client["enabled"] = False
2252
2258
                    else:
2253
2259
                        client["expires"] = (datetime.datetime
2254
2260
                                             .utcnow()
2255
2261
                                             + client["timeout"])
2256
 
            
2257
 
            client["changedstate"] = (multiprocessing_manager
2258
 
                                      .Condition
2259
 
                                      (multiprocessing_manager
2260
 
                                       .Lock()))
2261
 
            if use_dbus:
2262
 
                new_client = (ClientDBusTransitional.__new__
2263
 
                              (ClientDBusTransitional))
2264
 
                tcp_server.clients[client_name] = new_client
2265
 
                new_client.bus = bus
2266
 
                for name, value in client.iteritems():
2267
 
                    setattr(new_client, name, value)
2268
 
                client_object_name = unicode(client_name).translate(
2269
 
                    {ord("."): ord("_"),
2270
 
                     ord("-"): ord("_")})
2271
 
                new_client.dbus_object_path = (dbus.ObjectPath
2272
 
                                               ("/clients/"
2273
 
                                                + client_object_name))
2274
 
                DBusObjectWithProperties.__init__(new_client,
2275
 
                                                  new_client.bus,
2276
 
                                                  new_client
2277
 
                                                  .dbus_object_path)
2278
 
            else:
2279
 
                tcp_server.clients[client_name] = (Client.__new__
2280
 
                                                   (Client))
2281
 
                for name, value in client.iteritems():
2282
 
                    setattr(tcp_server.clients[client_name],
2283
 
                            name, value)
2284
 
            
 
2262
                    
2285
2263
            try:
2286
 
                tcp_server.clients[client_name].secret = (
2287
 
                    crypt.decrypt(tcp_server.clients[client_name]
2288
 
                                  .encrypted_secret,
2289
 
                                  client_settings[client_name]
2290
 
                                  ["secret"]))
2291
 
            except CryptoError:
 
2264
                client["secret"] = (
 
2265
                    pgp.decrypt(client["encrypted_secret"],
 
2266
                                client_settings[client_name]
 
2267
                                ["secret"]))
 
2268
            except PGPError:
2292
2269
                # If decryption fails, we use secret from new settings
2293
 
                tcp_server.clients[client_name].secret = (
 
2270
                logger.debug("Failed to decrypt {0} old secret"
 
2271
                             .format(client_name))
 
2272
                client["secret"] = (
2294
2273
                    client_settings[client_name]["secret"])
 
2274
 
2295
2275
    
2296
 
    # Create/remove clients based on new changes made to config
2297
 
    for clientname in set(old_client_settings) - set(client_settings):
2298
 
        del tcp_server.clients[clientname]
2299
 
    for clientname in set(client_settings) - set(old_client_settings):
2300
 
        tcp_server.clients[clientname] = (client_class(name
2301
 
                                                       = clientname,
2302
 
                                                       config =
2303
 
                                                       client_settings
2304
 
                                                       [clientname]))
 
2276
    # Add/remove clients based on new changes made to config
 
2277
    for client_name in set(old_client_settings) - set(client_settings):
 
2278
        del clients_data[client_name]
 
2279
    for client_name in set(client_settings) - set(old_client_settings):
 
2280
        clients_data[client_name] = client_settings[client_name]
 
2281
 
 
2282
    # Create clients all clients
 
2283
    for client_name, client in clients_data.iteritems():
 
2284
        tcp_server.clients[client_name] = client_class(
 
2285
            name = client_name, settings = client)
2305
2286
    
2306
2287
    if not tcp_server.clients:
2307
2288
        logger.warning("No clients defined")
2319
2300
            # "pidfile" was never created
2320
2301
            pass
2321
2302
        del pidfilename
2322
 
        
2323
2303
        signal.signal(signal.SIGINT, signal.SIG_IGN)
2324
2304
    
2325
2305
    signal.signal(signal.SIGHUP, lambda signum, frame: sys.exit())
2394
2374
        # Store client before exiting. Secrets are encrypted with key
2395
2375
        # based on what config file has. If config file is
2396
2376
        # removed/edited, old secret will thus be unrecovable.
2397
 
        clients = []
2398
 
        with Crypto() as crypt:
 
2377
        clients = {}
 
2378
        with PGPEngine() as pgp:
2399
2379
            for client in tcp_server.clients.itervalues():
2400
2380
                key = client_settings[client.name]["secret"]
2401
 
                client.encrypted_secret = crypt.encrypt(client.secret,
2402
 
                                                        key)
 
2381
                client.encrypted_secret = pgp.encrypt(client.secret,
 
2382
                                                      key)
2403
2383
                client_dict = {}
2404
2384
                
2405
 
                # A list of attributes that will not be stored when
2406
 
                # shutting down.
2407
 
                exclude = set(("bus", "changedstate", "secret"))
 
2385
                # A list of attributes that can not be pickled
 
2386
                # + secret.
 
2387
                exclude = set(("bus", "changedstate", "secret",
 
2388
                               "checker"))
2408
2389
                for name, typ in (inspect.getmembers
2409
2390
                                  (dbus.service.Object)):
2410
2391
                    exclude.add(name)
2415
2396
                    if attr not in exclude:
2416
2397
                        client_dict[attr] = getattr(client, attr)
2417
2398
                
2418
 
                clients.append(client_dict)
 
2399
                clients[client.name] = client_dict
2419
2400
                del client_settings[client.name]["secret"]
2420
2401
        
2421
2402
        try:
2497
2478
    # Must run before the D-Bus bus name gets deregistered
2498
2479
    cleanup()
2499
2480
 
2500
 
 
2501
2481
if __name__ == '__main__':
2502
2482
    main()