/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: Teddy Hogeborn
  • Date: 2012-01-01 05:42:51 UTC
  • mfrom: (237.4.22 release)
  • Revision ID: teddy@recompile.se-20120101054251-26jflhej4znq2mzq
Merge from release branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
# "AvahiService" class, and some lines in "main".
12
12
13
13
# Everything else is
14
 
# Copyright © 2008-2011 Teddy Hogeborn
15
 
# Copyright © 2008-2011 Björn Påhlsson
 
14
# Copyright © 2008-2012 Teddy Hogeborn
 
15
# Copyright © 2008-2012 Björn Påhlsson
16
16
17
17
# This program is free software: you can redistribute it and/or modify
18
18
# it under the terms of the GNU General Public License as published by
85
85
    except ImportError:
86
86
        SO_BINDTODEVICE = None
87
87
 
88
 
version = "1.4.1"
 
88
version = "1.5.0"
89
89
stored_state_file = "clients.pickle"
90
90
 
91
91
logger = logging.getLogger()
460
460
 
461
461
    @staticmethod
462
462
    def config_parser(config):
463
 
        """ Construct a new dict of client settings of this form:
 
463
        """Construct a new dict of client settings of this form:
464
464
        { client_name: {setting_name: value, ...}, ...}
465
 
        with exceptions for any special settings as defined above"""
 
465
        with exceptions for any special settings as defined above.
 
466
        NOTE: Must be a pure function. Must return the same result
 
467
        value given the same arguments.
 
468
        """
466
469
        settings = {}
467
470
        for client_name in config.sections():
468
471
            section = dict(config.items(client_name))
472
475
            # Reformat values from string types to Python types
473
476
            client["approved_by_default"] = config.getboolean(
474
477
                client_name, "approved_by_default")
475
 
            client["enabled"] = config.getboolean(client_name, "enabled")
 
478
            client["enabled"] = config.getboolean(client_name,
 
479
                                                  "enabled")
476
480
            
477
481
            client["fingerprint"] = (section["fingerprint"].upper()
478
482
                                     .replace(" ", ""))
498
502
            client["last_approval_request"] = None
499
503
            client["last_checked_ok"] = None
500
504
            client["last_checker_status"] = None
501
 
            if client["enabled"]:
502
 
                client["last_enabled"] = datetime.datetime.utcnow()
503
 
                client["expires"] = (datetime.datetime.utcnow()
504
 
                                     + client["timeout"])
505
 
            else:
506
 
                client["last_enabled"] = None
507
 
                client["expires"] = None
508
 
 
 
505
        
509
506
        return settings
510
507
        
511
508
        
518
515
        for setting, value in settings.iteritems():
519
516
            setattr(self, setting, value)
520
517
        
 
518
        if self.enabled:
 
519
            if not hasattr(self, "last_enabled"):
 
520
                self.last_enabled = datetime.datetime.utcnow()
 
521
            if not hasattr(self, "expires"):
 
522
                self.expires = (datetime.datetime.utcnow()
 
523
                                + self.timeout)
 
524
        else:
 
525
            self.last_enabled = None
 
526
            self.expires = None
 
527
       
521
528
        logger.debug("Creating client %r", self.name)
522
529
        # Uppercase and remove spaces from fingerprint for later
523
530
        # comparison purposes with return value from the fingerprint()
524
531
        # function
525
532
        logger.debug("  Fingerprint: %s", self.fingerprint)
526
 
        self.created = settings.get("created", datetime.datetime.utcnow())
 
533
        self.created = settings.get("created",
 
534
                                    datetime.datetime.utcnow())
527
535
 
528
536
        # attributes specific for this server instance
529
537
        self.checker = None
842
850
            # signatures other than "ay".
843
851
            if prop._dbus_signature != "ay":
844
852
                raise ValueError
845
 
            value = dbus.ByteArray(''.join(unichr(byte)
846
 
                                           for byte in value))
 
853
            value = dbus.ByteArray(b''.join(chr(byte)
 
854
                                            for byte in value))
847
855
        prop(value)
848
856
    
849
857
    @dbus.service.method(dbus.PROPERTIES_IFACE, in_signature="s",
1359
1367
        if value is None:       # get
1360
1368
            return dbus.UInt64(self.timeout_milliseconds())
1361
1369
        self.timeout = datetime.timedelta(0, 0, 0, value)
1362
 
        if getattr(self, "disable_initiator_tag", None) is None:
1363
 
            return
1364
1370
        # Reschedule timeout
1365
 
        gobject.source_remove(self.disable_initiator_tag)
1366
 
        self.disable_initiator_tag = None
1367
 
        self.expires = None
1368
 
        time_to_die = timedelta_to_milliseconds((self
1369
 
                                                 .last_checked_ok
1370
 
                                                 + self.timeout)
1371
 
                                                - datetime.datetime
1372
 
                                                .utcnow())
1373
 
        if time_to_die <= 0:
1374
 
            # The timeout has passed
1375
 
            self.disable()
1376
 
        else:
1377
 
            self.expires = (datetime.datetime.utcnow()
1378
 
                            + datetime.timedelta(milliseconds =
1379
 
                                                 time_to_die))
1380
 
            self.disable_initiator_tag = (gobject.timeout_add
1381
 
                                          (time_to_die, self.disable))
 
1371
        if self.enabled:
 
1372
            now = datetime.datetime.utcnow()
 
1373
            time_to_die = timedelta_to_milliseconds(
 
1374
                (self.last_checked_ok + self.timeout) - now)
 
1375
            if time_to_die <= 0:
 
1376
                # The timeout has passed
 
1377
                self.disable()
 
1378
            else:
 
1379
                self.expires = (now +
 
1380
                                datetime.timedelta(milliseconds =
 
1381
                                                   time_to_die))
 
1382
                if (getattr(self, "disable_initiator_tag", None)
 
1383
                    is None):
 
1384
                    return
 
1385
                gobject.source_remove(self.disable_initiator_tag)
 
1386
                self.disable_initiator_tag = (gobject.timeout_add
 
1387
                                              (time_to_die,
 
1388
                                               self.disable))
1382
1389
    
1383
1390
    # ExtendedTimeout - property
1384
1391
    @dbus_service_property(_interface, signature="t",
2095
2102
                                % server_settings["servicename"]))
2096
2103
    
2097
2104
    # Parse config file with clients
2098
 
    client_config = configparser.SafeConfigParser(Client.client_defaults)
 
2105
    client_config = configparser.SafeConfigParser(Client
 
2106
                                                  .client_defaults)
2099
2107
    client_config.read(os.path.join(server_settings["configdir"],
2100
2108
                                    "clients.conf"))
2101
2109
    
2263
2271
                        client["expires"] = (datetime.datetime
2264
2272
                                             .utcnow()
2265
2273
                                             + client["timeout"])
2266
 
                    
 
2274
                        logger.debug("Last checker succeeded,"
 
2275
                                     " keeping {0} enabled"
 
2276
                                     .format(client["name"]))
2267
2277
            try:
2268
2278
                client["secret"] = (
2269
2279
                    pgp.decrypt(client["encrypted_secret"],
2278
2288
 
2279
2289
    
2280
2290
    # Add/remove clients based on new changes made to config
2281
 
    for client_name in set(old_client_settings) - set(client_settings):
 
2291
    for client_name in (set(old_client_settings)
 
2292
                        - set(client_settings)):
2282
2293
        del clients_data[client_name]
2283
 
    for client_name in set(client_settings) - set(old_client_settings):
 
2294
    for client_name in (set(client_settings)
 
2295
                        - set(old_client_settings)):
2284
2296
        clients_data[client_name] = client_settings[client_name]
2285
2297
 
2286
2298
    # Create clients all clients