/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: 2014-08-09 13:12:55 UTC
  • Revision ID: teddy@recompile.se-20140809131255-lp31j98u2pl0xpe6
mandos: Stop using str() and remove unnecessary unicode() calls.

* mandos (if_nametoindex): Use "bytes" literal instead of str().
  (initlogger): Use a unicode string for log device.
  (AvahiError.__unicode__): Removed.
  (DBusPropertyException.__unicode__): - '' -
  (ClientDBus.Secret_dbus_property): Use bytes() instead of str().
  (IPv6_TCPServer.server_bind): Use .encode() instead of str().
  (string_to_delta): Removed unnecessary unicode() call.
  (main): Use "isinstance(x, bytes)" instead of "type(x) is str", use
          .decode() instead of unicode(), and use .encode() instead of
          str().

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
 
1
#!/usr/bin/python2.7
2
2
# -*- mode: python; coding: utf-8 -*-
3
3
4
4
# Mandos server - give out binary blobs to connecting clients.
88
88
    except ImportError:
89
89
        SO_BINDTODEVICE = None
90
90
 
91
 
version = "1.6.6"
 
91
version = "1.6.8"
92
92
stored_state_file = "clients.pickle"
93
93
 
94
94
logger = logging.getLogger()
104
104
        SIOCGIFINDEX = 0x8933  # From /usr/include/linux/sockios.h
105
105
        with contextlib.closing(socket.socket()) as s:
106
106
            ifreq = fcntl.ioctl(s, SIOCGIFINDEX,
107
 
                                struct.pack(str("16s16x"),
108
 
                                            interface))
109
 
        interface_index = struct.unpack(str("I"),
110
 
                                        ifreq[16:20])[0]
 
107
                                struct.pack(b"16s16x", interface))
 
108
        interface_index = struct.unpack("I", ifreq[16:20])[0]
111
109
        return interface_index
112
110
 
113
111
 
118
116
    syslogger = (logging.handlers.SysLogHandler
119
117
                 (facility =
120
118
                  logging.handlers.SysLogHandler.LOG_DAEMON,
121
 
                  address = str("/dev/log")))
 
119
                  address = "/dev/log"))
122
120
    syslogger.setFormatter(logging.Formatter
123
121
                           ('Mandos [%(process)d]: %(levelname)s:'
124
122
                            ' %(message)s'))
224
222
class AvahiError(Exception):
225
223
    def __init__(self, value, *args, **kwargs):
226
224
        self.value = value
227
 
        super(AvahiError, self).__init__(value, *args, **kwargs)
228
 
    def __unicode__(self):
229
 
        return unicode(repr(self.value))
 
225
        return super(AvahiError, self).__init__(value, *args,
 
226
                                                **kwargs)
230
227
 
231
228
class AvahiServiceError(AvahiError):
232
229
    pass
338
335
        elif state == avahi.ENTRY_GROUP_FAILURE:
339
336
            logger.critical("Avahi: Error in group state changed %s",
340
337
                            unicode(error))
341
 
            raise AvahiGroupError("State changed: {0!s}"
 
338
            raise AvahiGroupError("State changed: {!s}"
342
339
                                  .format(error))
343
340
    
344
341
    def cleanup(self):
395
392
        """Add the new name to the syslog messages"""
396
393
        ret = AvahiService.rename(self)
397
394
        syslogger.setFormatter(logging.Formatter
398
 
                               ('Mandos ({0}) [%(process)d]:'
 
395
                               ('Mandos ({}) [%(process)d]:'
399
396
                                ' %(levelname)s: %(message)s'
400
397
                                .format(self.name)))
401
398
        return ret
402
399
 
403
400
 
404
 
def timedelta_to_milliseconds(td):
405
 
    "Convert a datetime.timedelta() to milliseconds"
406
 
    return ((td.days * 24 * 60 * 60 * 1000)
407
 
            + (td.seconds * 1000)
408
 
            + (td.microseconds // 1000))
409
 
 
410
 
 
411
401
class Client(object):
412
402
    """A representation of a client host served by this server.
413
403
    
468
458
                        "enabled": "True",
469
459
                        }
470
460
    
471
 
    def timeout_milliseconds(self):
472
 
        "Return the 'timeout' attribute in milliseconds"
473
 
        return timedelta_to_milliseconds(self.timeout)
474
 
    
475
 
    def extended_timeout_milliseconds(self):
476
 
        "Return the 'extended_timeout' attribute in milliseconds"
477
 
        return timedelta_to_milliseconds(self.extended_timeout)
478
 
    
479
 
    def interval_milliseconds(self):
480
 
        "Return the 'interval' attribute in milliseconds"
481
 
        return timedelta_to_milliseconds(self.interval)
482
 
    
483
 
    def approval_delay_milliseconds(self):
484
 
        return timedelta_to_milliseconds(self.approval_delay)
485
 
    
486
461
    @staticmethod
487
462
    def config_parser(config):
488
463
        """Construct a new dict of client settings of this form:
513
488
                          "rb") as secfile:
514
489
                    client["secret"] = secfile.read()
515
490
            else:
516
 
                raise TypeError("No secret or secfile for section {0}"
 
491
                raise TypeError("No secret or secfile for section {}"
517
492
                                .format(section))
518
493
            client["timeout"] = string_to_delta(section["timeout"])
519
494
            client["extended_timeout"] = string_to_delta(
536
511
            server_settings = {}
537
512
        self.server_settings = server_settings
538
513
        # adding all client settings
539
 
        for setting, value in settings.iteritems():
 
514
        for setting, value in settings.items():
540
515
            setattr(self, setting, value)
541
516
        
542
517
        if self.enabled:
625
600
        if self.checker_initiator_tag is not None:
626
601
            gobject.source_remove(self.checker_initiator_tag)
627
602
        self.checker_initiator_tag = (gobject.timeout_add
628
 
                                      (self.interval_milliseconds(),
 
603
                                      (int(self.interval
 
604
                                           .total_seconds() * 1000),
629
605
                                       self.start_checker))
630
606
        # Schedule a disable() when 'timeout' has passed
631
607
        if self.disable_initiator_tag is not None:
632
608
            gobject.source_remove(self.disable_initiator_tag)
633
609
        self.disable_initiator_tag = (gobject.timeout_add
634
 
                                   (self.timeout_milliseconds(),
635
 
                                    self.disable))
 
610
                                      (int(self.timeout
 
611
                                           .total_seconds() * 1000),
 
612
                                       self.disable))
636
613
        # Also start a new checker *right now*.
637
614
        self.start_checker()
638
615
    
669
646
            self.disable_initiator_tag = None
670
647
        if getattr(self, "enabled", False):
671
648
            self.disable_initiator_tag = (gobject.timeout_add
672
 
                                          (timedelta_to_milliseconds
673
 
                                           (timeout), self.disable))
 
649
                                          (int(timeout.total_seconds()
 
650
                                               * 1000), self.disable))
674
651
            self.expires = datetime.datetime.utcnow() + timeout
675
652
    
676
653
    def need_approval(self):
707
684
        # Start a new checker if needed
708
685
        if self.checker is None:
709
686
            # Escape attributes for the shell
710
 
            escaped_attrs = dict(
711
 
                (attr, re.escape(unicode(getattr(self, attr))))
712
 
                for attr in
713
 
                self.runtime_expansions)
 
687
            escaped_attrs = { attr:
 
688
                                  re.escape(unicode(getattr(self,
 
689
                                                            attr)))
 
690
                              for attr in self.runtime_expansions }
714
691
            try:
715
692
                command = self.checker_command % escaped_attrs
716
693
            except TypeError as error:
797
774
    # "Set" method, so we fail early here:
798
775
    if byte_arrays and signature != "ay":
799
776
        raise ValueError("Byte arrays not supported for non-'ay'"
800
 
                         " signature {0!r}".format(signature))
 
777
                         " signature {!r}".format(signature))
801
778
    def decorator(func):
802
779
        func._dbus_is_property = True
803
780
        func._dbus_interface = dbus_interface
851
828
class DBusPropertyException(dbus.exceptions.DBusException):
852
829
    """A base class for D-Bus property-related exceptions
853
830
    """
854
 
    def __unicode__(self):
855
 
        return unicode(str(self))
856
 
 
 
831
    pass
857
832
 
858
833
class DBusPropertyAccessException(DBusPropertyException):
859
834
    """A property's access permissions disallows an operation.
882
857
        If called like _is_dbus_thing("method") it returns a function
883
858
        suitable for use as predicate to inspect.getmembers().
884
859
        """
885
 
        return lambda obj: getattr(obj, "_dbus_is_{0}".format(thing),
 
860
        return lambda obj: getattr(obj, "_dbus_is_{}".format(thing),
886
861
                                   False)
887
862
    
888
863
    def _get_all_dbus_things(self, thing):
938
913
            # signatures other than "ay".
939
914
            if prop._dbus_signature != "ay":
940
915
                raise ValueError("Byte arrays not supported for non-"
941
 
                                 "'ay' signature {0!r}"
 
916
                                 "'ay' signature {!r}"
942
917
                                 .format(prop._dbus_signature))
943
918
            value = dbus.ByteArray(b''.join(chr(byte)
944
919
                                            for byte in value))
1009
984
                                              (prop,
1010
985
                                               "_dbus_annotations",
1011
986
                                               {}))
1012
 
                        for name, value in annots.iteritems():
 
987
                        for name, value in annots.items():
1013
988
                            ann_tag = document.createElement(
1014
989
                                "annotation")
1015
990
                            ann_tag.setAttribute("name", name)
1018
993
                # Add interface annotation tags
1019
994
                for annotation, value in dict(
1020
995
                    itertools.chain.from_iterable(
1021
 
                        annotations().iteritems()
 
996
                        annotations().items()
1022
997
                        for name, annotations in
1023
998
                        self._get_all_dbus_things("interface")
1024
999
                        if name == if_tag.getAttribute("name")
1025
 
                        )).iteritems():
 
1000
                        )).items():
1026
1001
                    ann_tag = document.createElement("annotation")
1027
1002
                    ann_tag.setAttribute("name", annotation)
1028
1003
                    ann_tag.setAttribute("value", value)
1084
1059
    """
1085
1060
    def wrapper(cls):
1086
1061
        for orig_interface_name, alt_interface_name in (
1087
 
            alt_interface_names.iteritems()):
 
1062
            alt_interface_names.items()):
1088
1063
            attr = {}
1089
1064
            interface_names = set()
1090
1065
            # Go though all attributes of the class
1207
1182
                                        attribute.func_closure)))
1208
1183
            if deprecate:
1209
1184
                # Deprecate all alternate interfaces
1210
 
                iname="_AlternateDBusNames_interface_annotation{0}"
 
1185
                iname="_AlternateDBusNames_interface_annotation{}"
1211
1186
                for interface_name in interface_names:
1212
1187
                    @dbus_interface_annotations(interface_name)
1213
1188
                    def func(self):
1222
1197
            if interface_names:
1223
1198
                # Replace the class with a new subclass of it with
1224
1199
                # methods, signals, etc. as created above.
1225
 
                cls = type(b"{0}Alternate".format(cls.__name__),
 
1200
                cls = type(b"{}Alternate".format(cls.__name__),
1226
1201
                           (cls,), attr)
1227
1202
        return cls
1228
1203
    return wrapper
1269
1244
                   to the D-Bus.  Default: no transform
1270
1245
        variant_level: D-Bus variant level.  Default: 1
1271
1246
        """
1272
 
        attrname = "_{0}".format(dbus_name)
 
1247
        attrname = "_{}".format(dbus_name)
1273
1248
        def setter(self, value):
1274
1249
            if hasattr(self, "dbus_object_path"):
1275
1250
                if (not hasattr(self, attrname) or
1305
1280
    approval_delay = notifychangeproperty(dbus.UInt64,
1306
1281
                                          "ApprovalDelay",
1307
1282
                                          type_func =
1308
 
                                          timedelta_to_milliseconds)
 
1283
                                          lambda td: td.total_seconds()
 
1284
                                          * 1000)
1309
1285
    approval_duration = notifychangeproperty(
1310
1286
        dbus.UInt64, "ApprovalDuration",
1311
 
        type_func = timedelta_to_milliseconds)
 
1287
        type_func = lambda td: td.total_seconds() * 1000)
1312
1288
    host = notifychangeproperty(dbus.String, "Host")
1313
1289
    timeout = notifychangeproperty(dbus.UInt64, "Timeout",
1314
 
                                   type_func =
1315
 
                                   timedelta_to_milliseconds)
 
1290
                                   type_func = lambda td:
 
1291
                                       td.total_seconds() * 1000)
1316
1292
    extended_timeout = notifychangeproperty(
1317
1293
        dbus.UInt64, "ExtendedTimeout",
1318
 
        type_func = timedelta_to_milliseconds)
 
1294
        type_func = lambda td: td.total_seconds() * 1000)
1319
1295
    interval = notifychangeproperty(dbus.UInt64,
1320
1296
                                    "Interval",
1321
1297
                                    type_func =
1322
 
                                    timedelta_to_milliseconds)
 
1298
                                    lambda td: td.total_seconds()
 
1299
                                    * 1000)
1323
1300
    checker_command = notifychangeproperty(dbus.String, "Checker")
1324
1301
    
1325
1302
    del notifychangeproperty
1368
1345
    
1369
1346
    def approve(self, value=True):
1370
1347
        self.approved = value
1371
 
        gobject.timeout_add(timedelta_to_milliseconds
1372
 
                            (self.approval_duration),
1373
 
                            self._reset_approved)
 
1348
        gobject.timeout_add(int(self.approval_duration.total_seconds()
 
1349
                                * 1000), self._reset_approved)
1374
1350
        self.send_changedstate()
1375
1351
    
1376
1352
    ## D-Bus methods, signals & properties
1479
1455
                           access="readwrite")
1480
1456
    def ApprovalDelay_dbus_property(self, value=None):
1481
1457
        if value is None:       # get
1482
 
            return dbus.UInt64(self.approval_delay_milliseconds())
 
1458
            return dbus.UInt64(self.approval_delay.total_seconds()
 
1459
                               * 1000)
1483
1460
        self.approval_delay = datetime.timedelta(0, 0, 0, value)
1484
1461
    
1485
1462
    # ApprovalDuration - property
1487
1464
                           access="readwrite")
1488
1465
    def ApprovalDuration_dbus_property(self, value=None):
1489
1466
        if value is None:       # get
1490
 
            return dbus.UInt64(timedelta_to_milliseconds(
1491
 
                    self.approval_duration))
 
1467
            return dbus.UInt64(self.approval_duration.total_seconds()
 
1468
                               * 1000)
1492
1469
        self.approval_duration = datetime.timedelta(0, 0, 0, value)
1493
1470
    
1494
1471
    # Name - property
1560
1537
                           access="readwrite")
1561
1538
    def Timeout_dbus_property(self, value=None):
1562
1539
        if value is None:       # get
1563
 
            return dbus.UInt64(self.timeout_milliseconds())
 
1540
            return dbus.UInt64(self.timeout.total_seconds() * 1000)
1564
1541
        old_timeout = self.timeout
1565
1542
        self.timeout = datetime.timedelta(0, 0, 0, value)
1566
1543
        # Reschedule disabling
1577
1554
                gobject.source_remove(self.disable_initiator_tag)
1578
1555
                self.disable_initiator_tag = (
1579
1556
                    gobject.timeout_add(
1580
 
                        timedelta_to_milliseconds(self.expires - now),
1581
 
                        self.disable))
 
1557
                        int((self.expires - now).total_seconds()
 
1558
                            * 1000), self.disable))
1582
1559
    
1583
1560
    # ExtendedTimeout - property
1584
1561
    @dbus_service_property(_interface, signature="t",
1585
1562
                           access="readwrite")
1586
1563
    def ExtendedTimeout_dbus_property(self, value=None):
1587
1564
        if value is None:       # get
1588
 
            return dbus.UInt64(self.extended_timeout_milliseconds())
 
1565
            return dbus.UInt64(self.extended_timeout.total_seconds()
 
1566
                               * 1000)
1589
1567
        self.extended_timeout = datetime.timedelta(0, 0, 0, value)
1590
1568
    
1591
1569
    # Interval - property
1593
1571
                           access="readwrite")
1594
1572
    def Interval_dbus_property(self, value=None):
1595
1573
        if value is None:       # get
1596
 
            return dbus.UInt64(self.interval_milliseconds())
 
1574
            return dbus.UInt64(self.interval.total_seconds() * 1000)
1597
1575
        self.interval = datetime.timedelta(0, 0, 0, value)
1598
1576
        if getattr(self, "checker_initiator_tag", None) is None:
1599
1577
            return
1632
1610
    @dbus_service_property(_interface, signature="ay",
1633
1611
                           access="write", byte_arrays=True)
1634
1612
    def Secret_dbus_property(self, value):
1635
 
        self.secret = str(value)
 
1613
        self.secret = bytes(value)
1636
1614
    
1637
1615
    del _interface
1638
1616
 
1759
1737
                        if self.server.use_dbus:
1760
1738
                            # Emit D-Bus signal
1761
1739
                            client.NeedApproval(
1762
 
                                client.approval_delay_milliseconds(),
1763
 
                                client.approved_by_default)
 
1740
                                client.approval_delay.total_seconds()
 
1741
                                * 1000, client.approved_by_default)
1764
1742
                    else:
1765
1743
                        logger.warning("Client %s was not approved",
1766
1744
                                       client.name)
1772
1750
                    #wait until timeout or approved
1773
1751
                    time = datetime.datetime.now()
1774
1752
                    client.changedstate.acquire()
1775
 
                    client.changedstate.wait(
1776
 
                        float(timedelta_to_milliseconds(delay)
1777
 
                              / 1000))
 
1753
                    client.changedstate.wait(delay.total_seconds())
1778
1754
                    client.changedstate.release()
1779
1755
                    time2 = datetime.datetime.now()
1780
1756
                    if (time2 - time) >= delay:
1979
1955
                try:
1980
1956
                    self.socket.setsockopt(socket.SOL_SOCKET,
1981
1957
                                           SO_BINDTODEVICE,
1982
 
                                           str(self.interface + '\0'))
 
1958
                                           (self.interface + "\0")
 
1959
                                           .encode("utf-8"))
1983
1960
                except socket.error as error:
1984
1961
                    if error.errno == errno.EPERM:
1985
1962
                        logger.error("No permission to bind to"
2188
2165
    token_duration = Token(re.compile(r"P"), None,
2189
2166
                           frozenset((token_year, token_month,
2190
2167
                                      token_day, token_time,
2191
 
                                      token_week))),
 
2168
                                      token_week)))
2192
2169
    # Define starting values
2193
2170
    value = datetime.timedelta() # Value so far
2194
2171
    found_token = None
2195
 
    followers = frozenset(token_duration,) # Following valid tokens
 
2172
    followers = frozenset((token_duration,)) # Following valid tokens
2196
2173
    s = duration                # String left to parse
2197
2174
    # Loop until end token is found
2198
2175
    while found_token is not token_end:
2245
2222
    timevalue = datetime.timedelta(0)
2246
2223
    for s in interval.split():
2247
2224
        try:
2248
 
            suffix = unicode(s[-1])
 
2225
            suffix = s[-1]
2249
2226
            value = int(s[:-1])
2250
2227
            if suffix == "d":
2251
2228
                delta = datetime.timedelta(value)
2258
2235
            elif suffix == "w":
2259
2236
                delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
2260
2237
            else:
2261
 
                raise ValueError("Unknown suffix {0!r}"
 
2238
                raise ValueError("Unknown suffix {!r}"
2262
2239
                                 .format(suffix))
2263
2240
        except IndexError as e:
2264
2241
            raise ValueError(*(e.args))
2281
2258
        # Close all standard open file descriptors
2282
2259
        null = os.open(os.devnull, os.O_NOCTTY | os.O_RDWR)
2283
2260
        if not stat.S_ISCHR(os.fstat(null).st_mode):
2284
 
            raise OSError(errno.ENODEV,
2285
 
                          "{0} not a character device"
 
2261
            raise OSError(errno.ENODEV, "{} not a character device"
2286
2262
                          .format(os.devnull))
2287
2263
        os.dup2(null, sys.stdin.fileno())
2288
2264
        os.dup2(null, sys.stdout.fileno())
2298
2274
    
2299
2275
    parser = argparse.ArgumentParser()
2300
2276
    parser.add_argument("-v", "--version", action="version",
2301
 
                        version = "%(prog)s {0}".format(version),
 
2277
                        version = "%(prog)s {}".format(version),
2302
2278
                        help="show version number and exit")
2303
2279
    parser.add_argument("-i", "--interface", metavar="IF",
2304
2280
                        help="Bind to interface IF")
2403
2379
    del options
2404
2380
    # Force all strings to be unicode
2405
2381
    for option in server_settings.keys():
2406
 
        if type(server_settings[option]) is str:
2407
 
            server_settings[option] = unicode(server_settings[option])
 
2382
        if isinstance(server_settings[option], bytes):
 
2383
            server_settings[option] = (server_settings[option]
 
2384
                                       .decode("utf-8"))
2408
2385
    # Force all boolean options to be boolean
2409
2386
    for option in ("debug", "use_dbus", "use_ipv6", "restore",
2410
2387
                   "foreground", "zeroconf"):
2443
2420
    
2444
2421
    if server_settings["servicename"] != "Mandos":
2445
2422
        syslogger.setFormatter(logging.Formatter
2446
 
                               ('Mandos ({0}) [%(process)d]:'
 
2423
                               ('Mandos ({}) [%(process)d]:'
2447
2424
                                ' %(levelname)s: %(message)s'
2448
2425
                                .format(server_settings
2449
2426
                                        ["servicename"])))
2553
2530
                                       protocol = protocol, bus = bus)
2554
2531
        if server_settings["interface"]:
2555
2532
            service.interface = (if_nametoindex
2556
 
                                 (str(server_settings["interface"])))
 
2533
                                 (server_settings["interface"]
 
2534
                                  .encode("utf-8")))
2557
2535
    
2558
2536
    global multiprocessing_manager
2559
2537
    multiprocessing_manager = multiprocessing.Manager()
2583
2561
            os.remove(stored_state_path)
2584
2562
        except IOError as e:
2585
2563
            if e.errno == errno.ENOENT:
2586
 
                logger.warning("Could not load persistent state: {0}"
 
2564
                logger.warning("Could not load persistent state: {}"
2587
2565
                                .format(os.strerror(e.errno)))
2588
2566
            else:
2589
2567
                logger.critical("Could not load persistent state:",
2594
2572
                           "EOFError:", exc_info=e)
2595
2573
    
2596
2574
    with PGPEngine() as pgp:
2597
 
        for client_name, client in clients_data.iteritems():
 
2575
        for client_name, client in clients_data.items():
2598
2576
            # Skip removed clients
2599
2577
            if client_name not in client_settings:
2600
2578
                continue
2625
2603
                if datetime.datetime.utcnow() >= client["expires"]:
2626
2604
                    if not client["last_checked_ok"]:
2627
2605
                        logger.warning(
2628
 
                            "disabling client {0} - Client never "
 
2606
                            "disabling client {} - Client never "
2629
2607
                            "performed a successful checker"
2630
2608
                            .format(client_name))
2631
2609
                        client["enabled"] = False
2632
2610
                    elif client["last_checker_status"] != 0:
2633
2611
                        logger.warning(
2634
 
                            "disabling client {0} - Client "
2635
 
                            "last checker failed with error code {1}"
 
2612
                            "disabling client {} - Client last"
 
2613
                            " checker failed with error code {}"
2636
2614
                            .format(client_name,
2637
2615
                                    client["last_checker_status"]))
2638
2616
                        client["enabled"] = False
2641
2619
                                             .utcnow()
2642
2620
                                             + client["timeout"])
2643
2621
                        logger.debug("Last checker succeeded,"
2644
 
                                     " keeping {0} enabled"
 
2622
                                     " keeping {} enabled"
2645
2623
                                     .format(client_name))
2646
2624
            try:
2647
2625
                client["secret"] = (
2650
2628
                                ["secret"]))
2651
2629
            except PGPError:
2652
2630
                # If decryption fails, we use secret from new settings
2653
 
                logger.debug("Failed to decrypt {0} old secret"
 
2631
                logger.debug("Failed to decrypt {} old secret"
2654
2632
                             .format(client_name))
2655
2633
                client["secret"] = (
2656
2634
                    client_settings[client_name]["secret"])
2664
2642
        clients_data[client_name] = client_settings[client_name]
2665
2643
    
2666
2644
    # Create all client objects
2667
 
    for client_name, client in clients_data.iteritems():
 
2645
    for client_name, client in clients_data.items():
2668
2646
        tcp_server.clients[client_name] = client_class(
2669
2647
            name = client_name, settings = client,
2670
2648
            server_settings = server_settings)
2677
2655
            try:
2678
2656
                with pidfile:
2679
2657
                    pid = os.getpid()
2680
 
                    pidfile.write(str(pid) + "\n".encode("utf-8"))
 
2658
                    pidfile.write("{}\n".format(pid).encode("utf-8"))
2681
2659
            except IOError:
2682
2660
                logger.error("Could not write to file %r with PID %d",
2683
2661
                             pidfilename, pid)
2774
2752
                
2775
2753
                # A list of attributes that can not be pickled
2776
2754
                # + secret.
2777
 
                exclude = set(("bus", "changedstate", "secret",
2778
 
                               "checker", "server_settings"))
 
2755
                exclude = { "bus", "changedstate", "secret",
 
2756
                            "checker", "server_settings" }
2779
2757
                for name, typ in (inspect.getmembers
2780
2758
                                  (dbus.service.Object)):
2781
2759
                    exclude.add(name)
2804
2782
                except NameError:
2805
2783
                    pass
2806
2784
            if e.errno in (errno.ENOENT, errno.EACCES, errno.EEXIST):
2807
 
                logger.warning("Could not save persistent state: {0}"
 
2785
                logger.warning("Could not save persistent state: {}"
2808
2786
                               .format(os.strerror(e.errno)))
2809
2787
            else:
2810
2788
                logger.warning("Could not save persistent state:",