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 ...",
166
167
syslogger.setFormatter(logging.Formatter
322
323
def extended_timeout_milliseconds(self):
323
324
"Return the 'extended_timeout' attribute in milliseconds"
324
return _timedelta_to_milliseconds(self.extended_timeout)
325
return _timedelta_to_milliseconds(self.extended_timeout)
326
327
def interval_milliseconds(self):
327
328
"Return the 'interval' attribute in milliseconds"
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
385
389
def send_changedstate(self):
386
390
self.changedstate.acquire()
387
391
self.changedstate.notify_all()
388
392
self.changedstate.release()
390
394
def enable(self):
391
395
"""Start this client's checker and timeout hooks"""
392
396
if getattr(self, "enabled", False):
461
465
timeout = self.timeout
462
466
self.last_checked_ok = datetime.datetime.utcnow()
463
467
gobject.source_remove(self.disable_initiator_tag)
468
self.disable_initiator_tag = (gobject.timeout_add
469
(_timedelta_to_milliseconds
470
(timeout), self.disable))
464
471
self.expires = datetime.datetime.utcnow() + timeout
465
self.disable_initiator_tag = (gobject.timeout_add
466
(_timedelta_to_milliseconds(timeout),
469
473
def need_approval(self):
470
474
self.last_approval_request = datetime.datetime.utcnow()
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))
637
inspect.getmembers(cls, self._is_dbus_property))
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.
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)
643
650
# No such property
754
761
return dbus.String(dt.isoformat(),
755
762
variant_level=variant_level)
757
class AlternateDBusNamesMetaclass(DBusObjectWithProperties.__metaclass__):
764
class AlternateDBusNamesMetaclass(DBusObjectWithProperties
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.
883
891
""" Modify a variable so that it's a property which announces
884
892
its changes to DBus.
886
transform_fun: Function that takes a value and transforms it
894
transform_fun: Function that takes a value and a variant_level
895
and transforms it to a D-Bus type.
888
896
dbus_name: D-Bus name of the variable
889
897
type_func: Function that transform the value before sending it
890
898
to the D-Bus. Default: no transform
891
899
variant_level: D-Bus variant level. Default: 1
901
attrname = "_{0}".format(dbus_name)
894
902
def setter(self, value):
895
old_value = real_value[0]
896
real_value[0] = value
897
903
if hasattr(self, "dbus_object_path"):
898
if type_func(old_value) != type_func(real_value[0]):
899
dbus_value = transform_func(type_func(real_value[0]),
904
if (not hasattr(self, attrname) or
905
type_func(getattr(self, attrname, None))
906
!= type_func(value)):
907
dbus_value = transform_func(type_func(value),
901
910
self.PropertyChanged(dbus.String(dbus_name),
912
setattr(self, attrname, value)
904
return property(lambda self: real_value[0], setter)
914
return property(lambda self: getattr(self, attrname), setter)
907
917
expires = notifychangeproperty(datetime_to_dbus, "Expires")
912
922
last_enabled = notifychangeproperty(datetime_to_dbus,
914
924
checker = notifychangeproperty(dbus.Boolean, "CheckerRunning",
915
type_func = lambda checker: checker is not None)
925
type_func = lambda checker:
916
927
last_checked_ok = notifychangeproperty(datetime_to_dbus,
918
last_approval_request = notifychangeproperty(datetime_to_dbus,
919
"LastApprovalRequest")
929
last_approval_request = notifychangeproperty(
930
datetime_to_dbus, "LastApprovalRequest")
920
931
approved_by_default = notifychangeproperty(dbus.Boolean,
921
932
"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)
933
approval_delay = notifychangeproperty(dbus.UInt16,
936
_timedelta_to_milliseconds)
937
approval_duration = notifychangeproperty(
938
dbus.UInt16, "ApprovalDuration",
939
type_func = _timedelta_to_milliseconds)
926
940
host = notifychangeproperty(dbus.String, "Host")
927
941
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)
943
_timedelta_to_milliseconds)
944
extended_timeout = notifychangeproperty(
945
dbus.UInt16, "ExtendedTimeout",
946
type_func = _timedelta_to_milliseconds)
947
interval = notifychangeproperty(dbus.UInt16,
950
_timedelta_to_milliseconds)
933
951
checker_command = notifychangeproperty(dbus.String, "Checker")
935
953
del notifychangeproperty
1170
1188
gobject.source_remove(self.disable_initiator_tag)
1171
1189
self.disable_initiator_tag = None
1172
1190
self.expires = None
1173
time_to_die = (self.
1174
_timedelta_to_milliseconds((self
1191
time_to_die = _timedelta_to_milliseconds((self
1179
1196
if time_to_die <= 0:
1180
1197
# The timeout has passed
1183
1200
self.expires = (datetime.datetime.utcnow()
1184
+ datetime.timedelta(milliseconds = time_to_die))
1201
+ datetime.timedelta(milliseconds =
1185
1203
self.disable_initiator_tag = (gobject.timeout_add
1186
1204
(time_to_die, self.disable))
1378
1396
#wait until timeout or approved
1379
#x = float(client._timedelta_to_milliseconds(delay))
1380
1397
time = datetime.datetime.now()
1381
1398
client.changedstate.acquire()
1382
client.changedstate.wait(float(client._timedelta_to_milliseconds(delay) / 1000))
1399
(client.changedstate.wait
1400
(float(client._timedelta_to_milliseconds(delay)
1383
1402
client.changedstate.release()
1384
1403
time2 = datetime.datetime.now()
1385
1404
if (time2 - time) >= delay:
1496
1515
self.handle_error(request, address)
1497
1516
self.close_request(request)
1499
1518
def process_request(self, request, address):
1500
1519
"""Start a new process to process the request."""
1501
multiprocessing.Process(target = self.sub_process_main,
1502
args = (request, address)).start()
1520
proc = multiprocessing.Process(target = self.sub_process_main,
1505
1527
class MultiprocessingMixInWithPipe(MultiprocessingMixIn, object):
1512
1534
parent_pipe, self.child_pipe = multiprocessing.Pipe()
1514
super(MultiprocessingMixInWithPipe,
1515
self).process_request(request, client_address)
1536
proc = MultiprocessingMixIn.process_request(self, request,
1516
1538
self.child_pipe.close()
1517
self.add_pipe(parent_pipe)
1539
self.add_pipe(parent_pipe, proc)
1519
def add_pipe(self, parent_pipe):
1541
def add_pipe(self, parent_pipe, proc):
1520
1542
"""Dummy function; override as necessary"""
1521
1543
raise NotImplementedError
1610
1632
def server_activate(self):
1611
1633
if self.enabled:
1612
1634
return socketserver.TCPServer.server_activate(self)
1613
1636
def enable(self):
1614
1637
self.enabled = True
1615
def add_pipe(self, parent_pipe):
1639
def add_pipe(self, parent_pipe, proc):
1616
1640
# Call "handle_ipc" for both data and EOF events
1617
1641
gobject.io_add_watch(parent_pipe.fileno(),
1618
1642
gobject.IO_IN | gobject.IO_HUP,
1619
1643
functools.partial(self.handle_ipc,
1620
parent_pipe = parent_pipe))
1622
1648
def handle_ipc(self, source, condition, parent_pipe=None,
1623
client_object=None):
1649
proc = None, client_object=None):
1624
1650
condition_names = {
1625
1651
gobject.IO_IN: "IN", # There is data to read.
1626
1652
gobject.IO_OUT: "OUT", # Data can be written (without
1635
1661
for cond, name in
1636
1662
condition_names.iteritems()
1637
1663
if cond & condition)
1638
# error or the other end of multiprocessing.Pipe has closed
1664
# error, or the other end of multiprocessing.Pipe has closed
1639
1665
if condition & (gobject.IO_ERR | condition & gobject.IO_HUP):
1666
# Wait for other process to exit
1642
1670
# Read a request from the child
1656
1684
"dress: %s", fpr, address)
1657
1685
if self.use_dbus:
1658
1686
# Emit D-Bus signal
1659
mandos_dbus_service.ClientNotFound(fpr, address[0])
1687
mandos_dbus_service.ClientNotFound(fpr,
1660
1689
parent_pipe.send(False)
1663
1692
gobject.io_add_watch(parent_pipe.fileno(),
1664
1693
gobject.IO_IN | gobject.IO_HUP,
1665
1694
functools.partial(self.handle_ipc,
1666
parent_pipe = parent_pipe,
1667
client_object = client))
1668
1700
parent_pipe.send(True)
1669
# remove the old hook in favor of the new above hook on same fileno
1701
# remove the old hook in favor of the new above hook on
1671
1704
if command == 'funcall':
1672
1705
funcname = request[1]
1673
1706
args = request[2]
1674
1707
kwargs = request[3]
1676
parent_pipe.send(('data', getattr(client_object, funcname)(*args, **kwargs)))
1709
parent_pipe.send(('data', getattr(client_object,
1678
1713
if command == 'getattr':
1679
1714
attrname = request[1]
1680
1715
if callable(client_object.__getattribute__(attrname)):
1681
1716
parent_pipe.send(('function',))
1683
parent_pipe.send(('data', client_object.__getattribute__(attrname)))
1718
parent_pipe.send(('data', client_object
1719
.__getattribute__(attrname)))
1685
1721
if command == 'setattr':
1686
1722
attrname = request[1]
1978
2014
bus_name = dbus.service.BusName("se.recompile.Mandos",
1979
2015
bus, do_not_queue=True)
1980
old_bus_name = dbus.service.BusName("se.bsnet.fukt.Mandos",
1981
bus, do_not_queue=True)
2016
old_bus_name = (dbus.service.BusName
2017
("se.bsnet.fukt.Mandos", bus,
1982
2019
except dbus.exceptions.NameExistsException as e:
1983
2020
logger.error(unicode(e) + ", disabling D-Bus")
1984
2021
use_dbus = False