/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

* mandos: Break long lines.

Show diffs side-by-side

added added

removed removed

Lines of Context:
160
160
                            " after %i retries, exiting.",
161
161
                            self.rename_count)
162
162
            raise AvahiServiceError("Too many renames")
163
 
        self.name = unicode(self.server.GetAlternativeServiceName(self.name))
 
163
        self.name = unicode(self.server
 
164
                            .GetAlternativeServiceName(self.name))
164
165
        logger.info("Changing Zeroconf service name to %r ...",
165
166
                    self.name)
166
167
        syslogger.setFormatter(logging.Formatter
361
362
        self.last_enabled = None
362
363
        self.last_checked_ok = None
363
364
        self.timeout = string_to_delta(config["timeout"])
364
 
        self.extended_timeout = string_to_delta(config["extended_timeout"])
 
365
        self.extended_timeout = string_to_delta(config
 
366
                                                ["extended_timeout"])
365
367
        self.interval = string_to_delta(config["interval"])
366
368
        self.disable_hook = disable_hook
367
369
        self.checker = None
380
382
            config["approval_delay"])
381
383
        self.approval_duration = string_to_delta(
382
384
            config["approval_duration"])
383
 
        self.changedstate = multiprocessing_manager.Condition(multiprocessing_manager.Lock())
 
385
        self.changedstate = (multiprocessing_manager
 
386
                             .Condition(multiprocessing_manager
 
387
                                        .Lock()))
384
388
    
385
389
    def send_changedstate(self):
386
390
        self.changedstate.acquire()
387
391
        self.changedstate.notify_all()
388
392
        self.changedstate.release()
389
 
        
 
393
    
390
394
    def enable(self):
391
395
        """Start this client's checker and timeout hooks"""
392
396
        if getattr(self, "enabled", False):
463
467
        gobject.source_remove(self.disable_initiator_tag)
464
468
        self.expires = datetime.datetime.utcnow() + timeout
465
469
        self.disable_initiator_tag = (gobject.timeout_add
466
 
                                      (_timedelta_to_milliseconds(timeout),
467
 
                                       self.disable))
 
470
                                      (_timedelta_to_milliseconds
 
471
                                       (timeout), self.disable))
468
472
    
469
473
    def need_approval(self):
470
474
        self.last_approval_request = datetime.datetime.utcnow()
629
633
        """
630
634
        return ((prop.__get__(self)._dbus_name, prop.__get__(self))
631
635
                for cls in self.__class__.__mro__
632
 
                for name, prop in inspect.getmembers(cls, self._is_dbus_property))
 
636
                for name, prop in
 
637
                inspect.getmembers(cls, self._is_dbus_property))
633
638
    
634
639
    def _get_dbus_property(self, interface_name, property_name):
635
640
        """Returns a bound method if one exists which is a D-Bus
636
641
        property with the specified name and interface.
637
642
        """
638
643
        for cls in  self.__class__.__mro__:
639
 
            for name, value in inspect.getmembers(cls, self._is_dbus_property):
640
 
                if value._dbus_name == property_name and value._dbus_interface == interface_name:
 
644
            for name, value in (inspect.getmembers
 
645
                                (cls, self._is_dbus_property)):
 
646
                if (value._dbus_name == property_name
 
647
                    and value._dbus_interface == interface_name):
641
648
                    return value.__get__(self)
642
649
        
643
650
        # No such property
754
761
    return dbus.String(dt.isoformat(),
755
762
                       variant_level=variant_level)
756
763
 
757
 
class AlternateDBusNamesMetaclass(DBusObjectWithProperties.__metaclass__):
 
764
class AlternateDBusNamesMetaclass(DBusObjectWithProperties
 
765
                                  .__metaclass__):
758
766
    """Applied to an empty subclass of a D-Bus object, this metaclass
759
767
    will add additional D-Bus attributes matching a certain pattern.
760
768
    """
896
904
            real_value[0] = value
897
905
            if hasattr(self, "dbus_object_path"):
898
906
                if type_func(old_value) != type_func(real_value[0]):
899
 
                    dbus_value = transform_func(type_func(real_value[0]),
 
907
                    dbus_value = transform_func(type_func
 
908
                                                (real_value[0]),
900
909
                                                variant_level)
901
910
                    self.PropertyChanged(dbus.String(dbus_name),
902
911
                                         dbus_value)
912
921
    last_enabled = notifychangeproperty(datetime_to_dbus,
913
922
                                        "LastEnabled")
914
923
    checker = notifychangeproperty(dbus.Boolean, "CheckerRunning",
915
 
                                   type_func = lambda checker: checker is not None)
 
924
                                   type_func = lambda checker:
 
925
                                       checker is not None)
916
926
    last_checked_ok = notifychangeproperty(datetime_to_dbus,
917
927
                                           "LastCheckedOK")
918
 
    last_approval_request = notifychangeproperty(datetime_to_dbus,
919
 
                                                 "LastApprovalRequest")
 
928
    last_approval_request = notifychangeproperty(
 
929
        datetime_to_dbus, "LastApprovalRequest")
920
930
    approved_by_default = notifychangeproperty(dbus.Boolean,
921
931
                                               "ApprovedByDefault")
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)
 
932
    approval_delay = notifychangeproperty(dbus.UInt16,
 
933
                                          "ApprovalDelay",
 
934
                                          type_func =
 
935
                                          _timedelta_to_milliseconds)
 
936
    approval_duration = notifychangeproperty(
 
937
        dbus.UInt16, "ApprovalDuration",
 
938
        type_func = _timedelta_to_milliseconds)
926
939
    host = notifychangeproperty(dbus.String, "Host")
927
940
    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)
 
941
                                   type_func =
 
942
                                   _timedelta_to_milliseconds)
 
943
    extended_timeout = notifychangeproperty(
 
944
        dbus.UInt16, "ExtendedTimeout",
 
945
        type_func = _timedelta_to_milliseconds)
 
946
    interval = notifychangeproperty(dbus.UInt16,
 
947
                                    "Interval",
 
948
                                    type_func =
 
949
                                    _timedelta_to_milliseconds)
933
950
    checker_command = notifychangeproperty(dbus.String, "Checker")
934
951
    
935
952
    del notifychangeproperty
1181
1198
            self.disable()
1182
1199
        else:
1183
1200
            self.expires = (datetime.datetime.utcnow()
1184
 
                            + datetime.timedelta(milliseconds = time_to_die))
 
1201
                            + datetime.timedelta(milliseconds =
 
1202
                                                 time_to_die))
1185
1203
            self.disable_initiator_tag = (gobject.timeout_add
1186
1204
                                          (time_to_die, self.disable))
1187
1205
    
1353
1371
                                       client.name)
1354
1372
                        if self.server.use_dbus:
1355
1373
                            # Emit D-Bus signal
1356
 
                            client.Rejected("Disabled")                    
 
1374
                            client.Rejected("Disabled")
1357
1375
                        return
1358
1376
                    
1359
1377
                    if client._approved or not client.approval_delay:
1376
1394
                        return
1377
1395
                    
1378
1396
                    #wait until timeout or approved
1379
 
                    #x = float(client._timedelta_to_milliseconds(delay))
 
1397
                    #x = float(client
 
1398
                    #          ._timedelta_to_milliseconds(delay))
1380
1399
                    time = datetime.datetime.now()
1381
1400
                    client.changedstate.acquire()
1382
 
                    client.changedstate.wait(float(client._timedelta_to_milliseconds(delay) / 1000))
 
1401
                    (client.changedstate.wait
 
1402
                     (float(client._timedelta_to_milliseconds(delay)
 
1403
                            / 1000)))
1383
1404
                    client.changedstate.release()
1384
1405
                    time2 = datetime.datetime.now()
1385
1406
                    if (time2 - time) >= delay:
1617
1638
        gobject.io_add_watch(parent_pipe.fileno(),
1618
1639
                             gobject.IO_IN | gobject.IO_HUP,
1619
1640
                             functools.partial(self.handle_ipc,
1620
 
                                               parent_pipe = parent_pipe))
 
1641
                                               parent_pipe =
 
1642
                                               parent_pipe))
1621
1643
        
1622
1644
    def handle_ipc(self, source, condition, parent_pipe=None,
1623
1645
                   client_object=None):
1656
1678
                            "dress: %s", fpr, address)
1657
1679
                if self.use_dbus:
1658
1680
                    # Emit D-Bus signal
1659
 
                    mandos_dbus_service.ClientNotFound(fpr, address[0])
 
1681
                    mandos_dbus_service.ClientNotFound(fpr,
 
1682
                                                       address[0])
1660
1683
                parent_pipe.send(False)
1661
1684
                return False
1662
1685
            
1663
1686
            gobject.io_add_watch(parent_pipe.fileno(),
1664
1687
                                 gobject.IO_IN | gobject.IO_HUP,
1665
1688
                                 functools.partial(self.handle_ipc,
1666
 
                                                   parent_pipe = parent_pipe,
1667
 
                                                   client_object = client))
 
1689
                                                   parent_pipe =
 
1690
                                                   parent_pipe,
 
1691
                                                   client_object =
 
1692
                                                   client))
1668
1693
            parent_pipe.send(True)
1669
 
            # remove the old hook in favor of the new above hook on same fileno
 
1694
            # remove the old hook in favor of the new above hook on
 
1695
            # same fileno
1670
1696
            return False
1671
1697
        if command == 'funcall':
1672
1698
            funcname = request[1]
1673
1699
            args = request[2]
1674
1700
            kwargs = request[3]
1675
1701
            
1676
 
            parent_pipe.send(('data', getattr(client_object, funcname)(*args, **kwargs)))
 
1702
            parent_pipe.send(('data', getattr(client_object,
 
1703
                                              funcname)(*args,
 
1704
                                                         **kwargs)))
1677
1705
        
1678
1706
        if command == 'getattr':
1679
1707
            attrname = request[1]
1680
1708
            if callable(client_object.__getattribute__(attrname)):
1681
1709
                parent_pipe.send(('function',))
1682
1710
            else:
1683
 
                parent_pipe.send(('data', client_object.__getattribute__(attrname)))
 
1711
                parent_pipe.send(('data', client_object
 
1712
                                  .__getattribute__(attrname)))
1684
1713
        
1685
1714
        if command == 'setattr':
1686
1715
            attrname = request[1]
1977
2006
        try:
1978
2007
            bus_name = dbus.service.BusName("se.recompile.Mandos",
1979
2008
                                            bus, do_not_queue=True)
1980
 
            old_bus_name = dbus.service.BusName("se.bsnet.fukt.Mandos",
1981
 
                                                bus, do_not_queue=True)
 
2009
            old_bus_name = (dbus.service.BusName
 
2010
                            ("se.bsnet.fukt.Mandos", bus,
 
2011
                             do_not_queue=True))
1982
2012
        except dbus.exceptions.NameExistsException as e:
1983
2013
            logger.error(unicode(e) + ", disabling D-Bus")
1984
2014
            use_dbus = False
1997
2027
    
1998
2028
    client_class = Client
1999
2029
    if use_dbus:
2000
 
        client_class = functools.partial(ClientDBusTransitional, bus = bus)        
 
2030
        client_class = functools.partial(ClientDBusTransitional,
 
2031
                                         bus = bus)        
2001
2032
    def client_config_items(config, section):
2002
2033
        special_settings = {
2003
2034
            "approved_by_default":
2107
2138
            client.disable(quiet=True)
2108
2139
            if use_dbus:
2109
2140
                # Emit D-Bus signal
2110
 
                mandos_dbus_service.ClientRemoved(client.dbus_object_path,
 
2141
                mandos_dbus_service.ClientRemoved(client
 
2142
                                                  .dbus_object_path,
2111
2143
                                                  client.name)
2112
2144
    
2113
2145
    atexit.register(cleanup)