160
160
" after %i retries, exiting.",
161
161
self.rename_count)
162
162
raise AvahiServiceError("Too many renames")
163
self.name = unicode(self.server
164
.GetAlternativeServiceName(self.name))
163
self.name = unicode(self.server.GetAlternativeServiceName(self.name))
165
164
logger.info("Changing Zeroconf service name to %r ...",
167
166
syslogger.setFormatter(logging.Formatter
323
322
def extended_timeout_milliseconds(self):
324
323
"Return the 'extended_timeout' attribute in milliseconds"
325
return _timedelta_to_milliseconds(self.extended_timeout)
324
return _timedelta_to_milliseconds(self.extended_timeout)
327
326
def interval_milliseconds(self):
328
327
"Return the 'interval' attribute in milliseconds"
362
361
self.last_enabled = None
363
362
self.last_checked_ok = None
364
363
self.timeout = string_to_delta(config["timeout"])
365
self.extended_timeout = string_to_delta(config
366
["extended_timeout"])
364
self.extended_timeout = string_to_delta(config["extended_timeout"])
367
365
self.interval = string_to_delta(config["interval"])
368
366
self.disable_hook = disable_hook
369
367
self.checker = None
382
380
config["approval_delay"])
383
381
self.approval_duration = string_to_delta(
384
382
config["approval_duration"])
385
self.changedstate = (multiprocessing_manager
386
.Condition(multiprocessing_manager
383
self.changedstate = multiprocessing_manager.Condition(multiprocessing_manager.Lock())
389
385
def send_changedstate(self):
390
386
self.changedstate.acquire()
391
387
self.changedstate.notify_all()
392
388
self.changedstate.release()
394
390
def enable(self):
395
391
"""Start this client's checker and timeout hooks"""
396
392
if getattr(self, "enabled", False):
464
460
if timeout is None:
465
461
timeout = self.timeout
466
462
self.last_checked_ok = datetime.datetime.utcnow()
467
if self.disable_initiator_tag is not None:
468
gobject.source_remove(self.disable_initiator_tag)
469
if getattr(self, "enabled", False):
470
self.disable_initiator_tag = (gobject.timeout_add
471
(_timedelta_to_milliseconds
472
(timeout), self.disable))
473
self.expires = datetime.datetime.utcnow() + timeout
463
gobject.source_remove(self.disable_initiator_tag)
464
self.expires = datetime.datetime.utcnow() + timeout
465
self.disable_initiator_tag = (gobject.timeout_add
466
(_timedelta_to_milliseconds(timeout),
475
469
def need_approval(self):
476
470
self.last_approval_request = datetime.datetime.utcnow()
636
630
return ((prop.__get__(self)._dbus_name, prop.__get__(self))
637
631
for cls in self.__class__.__mro__
639
inspect.getmembers(cls, self._is_dbus_property))
632
for name, prop in inspect.getmembers(cls, self._is_dbus_property))
641
634
def _get_dbus_property(self, interface_name, property_name):
642
635
"""Returns a bound method if one exists which is a D-Bus
643
636
property with the specified name and interface.
645
638
for cls in self.__class__.__mro__:
646
for name, value in (inspect.getmembers
647
(cls, self._is_dbus_property)):
648
if (value._dbus_name == property_name
649
and value._dbus_interface == interface_name):
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:
650
641
return value.__get__(self)
652
643
# No such property
763
754
return dbus.String(dt.isoformat(),
764
755
variant_level=variant_level)
766
class AlternateDBusNamesMetaclass(DBusObjectWithProperties
757
class AlternateDBusNamesMetaclass(DBusObjectWithProperties.__metaclass__):
768
758
"""Applied to an empty subclass of a D-Bus object, this metaclass
769
759
will add additional D-Bus attributes matching a certain pattern.
893
883
""" Modify a variable so that it's a property which announces
894
884
its changes to DBus.
896
transform_fun: Function that takes a value and a variant_level
897
and transforms it to a D-Bus type.
886
transform_fun: Function that takes a value and transforms it
898
888
dbus_name: D-Bus name of the variable
899
889
type_func: Function that transform the value before sending it
900
890
to the D-Bus. Default: no transform
901
891
variant_level: D-Bus variant level. Default: 1
903
attrname = "_{0}".format(dbus_name)
904
894
def setter(self, value):
895
old_value = real_value[0]
896
real_value[0] = value
905
897
if hasattr(self, "dbus_object_path"):
906
if (not hasattr(self, attrname) or
907
type_func(getattr(self, attrname, None))
908
!= type_func(value)):
909
dbus_value = transform_func(type_func(value),
898
if type_func(old_value) != type_func(real_value[0]):
899
dbus_value = transform_func(type_func(real_value[0]),
912
901
self.PropertyChanged(dbus.String(dbus_name),
914
setattr(self, attrname, value)
916
return property(lambda self: getattr(self, attrname), setter)
904
return property(lambda self: real_value[0], setter)
919
907
expires = notifychangeproperty(datetime_to_dbus, "Expires")
924
912
last_enabled = notifychangeproperty(datetime_to_dbus,
926
914
checker = notifychangeproperty(dbus.Boolean, "CheckerRunning",
927
type_func = lambda checker:
915
type_func = lambda checker: checker is not None)
929
916
last_checked_ok = notifychangeproperty(datetime_to_dbus,
931
last_approval_request = notifychangeproperty(
932
datetime_to_dbus, "LastApprovalRequest")
918
last_approval_request = notifychangeproperty(datetime_to_dbus,
919
"LastApprovalRequest")
933
920
approved_by_default = notifychangeproperty(dbus.Boolean,
934
921
"ApprovedByDefault")
935
approval_delay = notifychangeproperty(dbus.UInt16,
938
_timedelta_to_milliseconds)
939
approval_duration = notifychangeproperty(
940
dbus.UInt16, "ApprovalDuration",
941
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)
942
926
host = notifychangeproperty(dbus.String, "Host")
943
927
timeout = notifychangeproperty(dbus.UInt16, "Timeout",
945
_timedelta_to_milliseconds)
946
extended_timeout = notifychangeproperty(
947
dbus.UInt16, "ExtendedTimeout",
948
type_func = _timedelta_to_milliseconds)
949
interval = notifychangeproperty(dbus.UInt16,
952
_timedelta_to_milliseconds)
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)
953
933
checker_command = notifychangeproperty(dbus.String, "Checker")
955
935
del notifychangeproperty
1190
1170
gobject.source_remove(self.disable_initiator_tag)
1191
1171
self.disable_initiator_tag = None
1192
1172
self.expires = None
1193
time_to_die = _timedelta_to_milliseconds((self
1173
time_to_die = (self.
1174
_timedelta_to_milliseconds((self
1198
1179
if time_to_die <= 0:
1199
1180
# The timeout has passed
1202
1183
self.expires = (datetime.datetime.utcnow()
1203
+ datetime.timedelta(milliseconds =
1184
+ datetime.timedelta(milliseconds = time_to_die))
1205
1185
self.disable_initiator_tag = (gobject.timeout_add
1206
1186
(time_to_die, self.disable))
1398
1378
#wait until timeout or approved
1379
#x = float(client._timedelta_to_milliseconds(delay))
1399
1380
time = datetime.datetime.now()
1400
1381
client.changedstate.acquire()
1401
(client.changedstate.wait
1402
(float(client._timedelta_to_milliseconds(delay)
1382
client.changedstate.wait(float(client._timedelta_to_milliseconds(delay) / 1000))
1404
1383
client.changedstate.release()
1405
1384
time2 = datetime.datetime.now()
1406
1385
if (time2 - time) >= delay:
1517
1496
self.handle_error(request, address)
1518
1497
self.close_request(request)
1520
1499
def process_request(self, request, address):
1521
1500
"""Start a new process to process the request."""
1522
proc = multiprocessing.Process(target = self.sub_process_main,
1501
multiprocessing.Process(target = self.sub_process_main,
1502
args = (request, address)).start()
1529
1505
class MultiprocessingMixInWithPipe(MultiprocessingMixIn, object):
1536
1512
parent_pipe, self.child_pipe = multiprocessing.Pipe()
1538
proc = MultiprocessingMixIn.process_request(self, request,
1514
super(MultiprocessingMixInWithPipe,
1515
self).process_request(request, client_address)
1540
1516
self.child_pipe.close()
1541
self.add_pipe(parent_pipe, proc)
1517
self.add_pipe(parent_pipe)
1543
def add_pipe(self, parent_pipe, proc):
1519
def add_pipe(self, parent_pipe):
1544
1520
"""Dummy function; override as necessary"""
1545
1521
raise NotImplementedError
1634
1610
def server_activate(self):
1635
1611
if self.enabled:
1636
1612
return socketserver.TCPServer.server_activate(self)
1638
1613
def enable(self):
1639
1614
self.enabled = True
1641
def add_pipe(self, parent_pipe, proc):
1615
def add_pipe(self, parent_pipe):
1642
1616
# Call "handle_ipc" for both data and EOF events
1643
1617
gobject.io_add_watch(parent_pipe.fileno(),
1644
1618
gobject.IO_IN | gobject.IO_HUP,
1645
1619
functools.partial(self.handle_ipc,
1620
parent_pipe = parent_pipe))
1650
1622
def handle_ipc(self, source, condition, parent_pipe=None,
1651
proc = None, client_object=None):
1623
client_object=None):
1652
1624
condition_names = {
1653
1625
gobject.IO_IN: "IN", # There is data to read.
1654
1626
gobject.IO_OUT: "OUT", # Data can be written (without
1663
1635
for cond, name in
1664
1636
condition_names.iteritems()
1665
1637
if cond & condition)
1666
# error, or the other end of multiprocessing.Pipe has closed
1638
# error or the other end of multiprocessing.Pipe has closed
1667
1639
if condition & (gobject.IO_ERR | condition & gobject.IO_HUP):
1668
# Wait for other process to exit
1672
1642
# Read a request from the child
1686
1656
"dress: %s", fpr, address)
1687
1657
if self.use_dbus:
1688
1658
# Emit D-Bus signal
1689
mandos_dbus_service.ClientNotFound(fpr,
1659
mandos_dbus_service.ClientNotFound(fpr, address[0])
1691
1660
parent_pipe.send(False)
1694
1663
gobject.io_add_watch(parent_pipe.fileno(),
1695
1664
gobject.IO_IN | gobject.IO_HUP,
1696
1665
functools.partial(self.handle_ipc,
1666
parent_pipe = parent_pipe,
1667
client_object = client))
1702
1668
parent_pipe.send(True)
1703
# remove the old hook in favor of the new above hook on
1669
# remove the old hook in favor of the new above hook on same fileno
1706
1671
if command == 'funcall':
1707
1672
funcname = request[1]
1708
1673
args = request[2]
1709
1674
kwargs = request[3]
1711
parent_pipe.send(('data', getattr(client_object,
1676
parent_pipe.send(('data', getattr(client_object, funcname)(*args, **kwargs)))
1715
1678
if command == 'getattr':
1716
1679
attrname = request[1]
1717
1680
if callable(client_object.__getattribute__(attrname)):
1718
1681
parent_pipe.send(('function',))
1720
parent_pipe.send(('data', client_object
1721
.__getattribute__(attrname)))
1683
parent_pipe.send(('data', client_object.__getattribute__(attrname)))
1723
1685
if command == 'setattr':
1724
1686
attrname = request[1]
2016
1978
bus_name = dbus.service.BusName("se.recompile.Mandos",
2017
1979
bus, do_not_queue=True)
2018
old_bus_name = (dbus.service.BusName
2019
("se.bsnet.fukt.Mandos", bus,
1980
old_bus_name = dbus.service.BusName("se.bsnet.fukt.Mandos",
1981
bus, do_not_queue=True)
2021
1982
except dbus.exceptions.NameExistsException as e:
2022
1983
logger.error(unicode(e) + ", disabling D-Bus")
2023
1984
use_dbus = False