11
11
# "AvahiService" class, and some lines in "main".
13
13
# Everything else is
14
# Copyright © 2008-2014 Teddy Hogeborn
15
# Copyright © 2008-2014 Björn Påhlsson
14
# Copyright © 2008-2013 Teddy Hogeborn
15
# Copyright © 2008-2013 Björn Påhlsson
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
88
88
except ImportError:
89
89
SO_BINDTODEVICE = None
91
if sys.version_info.major == 2:
95
92
stored_state_file = "clients.pickle"
97
94
logger = logging.getLogger()
95
syslogger = (logging.handlers.SysLogHandler
96
(facility = logging.handlers.SysLogHandler.LOG_DAEMON,
97
address = str("/dev/log")))
101
100
if_nametoindex = (ctypes.cdll.LoadLibrary
107
106
SIOCGIFINDEX = 0x8933 # From /usr/include/linux/sockios.h
108
107
with contextlib.closing(socket.socket()) as s:
109
108
ifreq = fcntl.ioctl(s, SIOCGIFINDEX,
110
struct.pack(b"16s16x", interface))
111
interface_index = struct.unpack("I", ifreq[16:20])[0]
109
struct.pack(str("16s16x"),
111
interface_index = struct.unpack(str("I"),
112
113
return interface_index
115
116
def initlogger(debug, level=logging.WARNING):
116
117
"""init logger and add loglevel"""
119
syslogger = (logging.handlers.SysLogHandler
121
logging.handlers.SysLogHandler.LOG_DAEMON,
122
address = "/dev/log"))
123
119
syslogger.setFormatter(logging.Formatter
124
120
('Mandos [%(process)d]: %(levelname)s:'
176
172
def password_encode(self, password):
177
173
# Passphrase can not be empty and can not contain newlines or
178
174
# NUL bytes. So we prefix it and hex encode it.
179
encoded = b"mandos" + binascii.hexlify(password)
180
if len(encoded) > 2048:
181
# GnuPG can't handle long passwords, so encode differently
182
encoded = (b"mandos" + password.replace(b"\\", b"\\\\")
183
.replace(b"\n", b"\\n")
184
.replace(b"\0", b"\\x00"))
175
return b"mandos" + binascii.hexlify(password)
187
177
def encrypt(self, data, password):
188
178
passphrase = self.password_encode(password)
225
215
class AvahiError(Exception):
226
216
def __init__(self, value, *args, **kwargs):
227
217
self.value = value
228
return super(AvahiError, self).__init__(value, *args,
218
super(AvahiError, self).__init__(value, *args, **kwargs)
219
def __unicode__(self):
220
return unicode(repr(self.value))
231
222
class AvahiServiceError(AvahiError):
282
273
" after %i retries, exiting.",
283
274
self.rename_count)
284
275
raise AvahiServiceError("Too many renames")
285
self.name = str(self.server
286
.GetAlternativeServiceName(self.name))
276
self.name = unicode(self.server
277
.GetAlternativeServiceName(self.name))
287
278
logger.info("Changing Zeroconf service name to %r ...",
338
329
elif state == avahi.ENTRY_GROUP_FAILURE:
339
330
logger.critical("Avahi: Error in group state changed %s",
341
raise AvahiGroupError("State changed: {!s}"
332
raise AvahiGroupError("State changed: {0!s}"
344
335
def cleanup(self):
395
386
"""Add the new name to the syslog messages"""
396
387
ret = AvahiService.rename(self)
397
388
syslogger.setFormatter(logging.Formatter
398
('Mandos ({}) [%(process)d]:'
389
('Mandos ({0}) [%(process)d]:'
399
390
' %(levelname)s: %(message)s'
400
391
.format(self.name)))
395
def timedelta_to_milliseconds(td):
396
"Convert a datetime.timedelta() to milliseconds"
397
return ((td.days * 24 * 60 * 60 * 1000)
398
+ (td.seconds * 1000)
399
+ (td.microseconds // 1000))
404
402
class Client(object):
405
403
"""A representation of a client host served by this server.
461
459
"enabled": "True",
462
def timeout_milliseconds(self):
463
"Return the 'timeout' attribute in milliseconds"
464
return timedelta_to_milliseconds(self.timeout)
466
def extended_timeout_milliseconds(self):
467
"Return the 'extended_timeout' attribute in milliseconds"
468
return timedelta_to_milliseconds(self.extended_timeout)
470
def interval_milliseconds(self):
471
"Return the 'interval' attribute in milliseconds"
472
return timedelta_to_milliseconds(self.interval)
474
def approval_delay_milliseconds(self):
475
return timedelta_to_milliseconds(self.approval_delay)
465
478
def config_parser(config):
466
479
"""Construct a new dict of client settings of this form:
491
504
"rb") as secfile:
492
505
client["secret"] = secfile.read()
494
raise TypeError("No secret or secfile for section {}"
507
raise TypeError("No secret or secfile for section {0}"
495
508
.format(section))
496
509
client["timeout"] = string_to_delta(section["timeout"])
497
510
client["extended_timeout"] = string_to_delta(
514
527
server_settings = {}
515
528
self.server_settings = server_settings
516
529
# adding all client settings
517
for setting, value in settings.items():
530
for setting, value in settings.iteritems():
518
531
setattr(self, setting, value)
603
616
if self.checker_initiator_tag is not None:
604
617
gobject.source_remove(self.checker_initiator_tag)
605
618
self.checker_initiator_tag = (gobject.timeout_add
607
.total_seconds() * 1000),
619
(self.interval_milliseconds(),
608
620
self.start_checker))
609
621
# Schedule a disable() when 'timeout' has passed
610
622
if self.disable_initiator_tag is not None:
611
623
gobject.source_remove(self.disable_initiator_tag)
612
624
self.disable_initiator_tag = (gobject.timeout_add
614
.total_seconds() * 1000),
625
(self.timeout_milliseconds(),
616
627
# Also start a new checker *right now*.
617
628
self.start_checker()
649
660
self.disable_initiator_tag = None
650
661
if getattr(self, "enabled", False):
651
662
self.disable_initiator_tag = (gobject.timeout_add
652
(int(timeout.total_seconds()
653
* 1000), self.disable))
663
(timedelta_to_milliseconds
664
(timeout), self.disable))
654
665
self.expires = datetime.datetime.utcnow() + timeout
656
667
def need_approval(self):
673
684
# If a checker exists, make sure it is not a zombie
675
686
pid, status = os.waitpid(self.checker.pid, os.WNOHANG)
676
except AttributeError:
678
except OSError as error:
679
if error.errno != errno.ECHILD:
687
except (AttributeError, OSError) as error:
688
if (isinstance(error, OSError)
689
and error.errno != errno.ECHILD):
683
693
logger.warning("Checker was a zombie")
687
697
# Start a new checker if needed
688
698
if self.checker is None:
689
699
# Escape attributes for the shell
690
escaped_attrs = { attr:
691
re.escape(str(getattr(self, attr)))
692
for attr in self.runtime_expansions }
700
escaped_attrs = dict(
701
(attr, re.escape(unicode(getattr(self, attr))))
703
self.runtime_expansions)
694
705
command = self.checker_command % escaped_attrs
695
706
except TypeError as error:
776
787
# "Set" method, so we fail early here:
777
788
if byte_arrays and signature != "ay":
778
789
raise ValueError("Byte arrays not supported for non-'ay'"
779
" signature {!r}".format(signature))
790
" signature {0!r}".format(signature))
780
791
def decorator(func):
781
792
func._dbus_is_property = True
782
793
func._dbus_interface = dbus_interface
813
824
"""Decorator to annotate D-Bus methods, signals or properties
816
@dbus_annotations({"org.freedesktop.DBus.Deprecated": "true",
817
"org.freedesktop.DBus.Property."
818
"EmitsChangedSignal": "false"})
819
827
@dbus_service_property("org.example.Interface", signature="b",
829
@dbus_annotations({{"org.freedesktop.DBus.Deprecated": "true",
830
"org.freedesktop.DBus.Property."
831
"EmitsChangedSignal": "false"})
821
832
def Property_dbus_property(self):
822
833
return dbus.Boolean(False)
830
841
class DBusPropertyException(dbus.exceptions.DBusException):
831
842
"""A base class for D-Bus property-related exceptions
844
def __unicode__(self):
845
return unicode(str(self))
835
848
class DBusPropertyAccessException(DBusPropertyException):
836
849
"""A property's access permissions disallows an operation.
859
872
If called like _is_dbus_thing("method") it returns a function
860
873
suitable for use as predicate to inspect.getmembers().
862
return lambda obj: getattr(obj, "_dbus_is_{}".format(thing),
875
return lambda obj: getattr(obj, "_dbus_is_{0}".format(thing),
865
878
def _get_all_dbus_things(self, thing):
914
927
# The byte_arrays option is not supported yet on
915
928
# signatures other than "ay".
916
929
if prop._dbus_signature != "ay":
917
raise ValueError("Byte arrays not supported for non-"
918
"'ay' signature {!r}"
919
.format(prop._dbus_signature))
920
931
value = dbus.ByteArray(b''.join(chr(byte)
921
932
for byte in value))
946
957
value.variant_level+1)
947
958
return dbus.Dictionary(properties, signature="sv")
949
@dbus.service.signal(dbus.PROPERTIES_IFACE, signature="sa{sv}as")
950
def PropertiesChanged(self, interface_name, changed_properties,
951
invalidated_properties):
952
"""Standard D-Bus PropertiesChanged() signal, see D-Bus
957
960
@dbus.service.method(dbus.INTROSPECTABLE_IFACE,
958
961
out_signature="s",
959
962
path_keyword='object_path',
995
998
"_dbus_annotations",
997
for name, value in annots.items():
1000
for name, value in annots.iteritems():
998
1001
ann_tag = document.createElement(
1000
1003
ann_tag.setAttribute("name", name)
1003
1006
# Add interface annotation tags
1004
1007
for annotation, value in dict(
1005
1008
itertools.chain.from_iterable(
1006
annotations().items()
1009
annotations().iteritems()
1007
1010
for name, annotations in
1008
1011
self._get_all_dbus_things("interface")
1009
1012
if name == if_tag.getAttribute("name")
1011
1014
ann_tag = document.createElement("annotation")
1012
1015
ann_tag.setAttribute("name", annotation)
1013
1016
ann_tag.setAttribute("value", value)
1070
1073
def wrapper(cls):
1071
1074
for orig_interface_name, alt_interface_name in (
1072
alt_interface_names.items()):
1075
alt_interface_names.iteritems()):
1074
1077
interface_names = set()
1075
1078
# Go though all attributes of the class
1192
1195
attribute.func_closure)))
1194
1197
# Deprecate all alternate interfaces
1195
iname="_AlternateDBusNames_interface_annotation{}"
1198
iname="_AlternateDBusNames_interface_annotation{0}"
1196
1199
for interface_name in interface_names:
1197
1200
@dbus_interface_annotations(interface_name)
1198
1201
def func(self):
1207
1210
if interface_names:
1208
1211
# Replace the class with a new subclass of it with
1209
1212
# methods, signals, etc. as created above.
1210
cls = type(b"{}Alternate".format(cls.__name__),
1213
cls = type(b"{0}Alternate".format(cls.__name__),
1226
1229
runtime_expansions = (Client.runtime_expansions
1227
1230
+ ("dbus_object_path",))
1229
_interface = "se.recompile.Mandos.Client"
1231
1232
# dbus.service.Object doesn't use super(), so we can't either.
1233
1234
def __init__(self, bus = None, *args, **kwargs):
1235
1236
Client.__init__(self, *args, **kwargs)
1236
1237
# Only now, when this client is initialized, can it show up on
1238
client_object_name = str(self.name).translate(
1239
client_object_name = unicode(self.name).translate(
1239
1240
{ord("."): ord("_"),
1240
1241
ord("-"): ord("_")})
1241
1242
self.dbus_object_path = (dbus.ObjectPath
1246
1247
def notifychangeproperty(transform_func,
1247
1248
dbus_name, type_func=lambda x: x,
1248
variant_level=1, invalidate_only=False,
1249
_interface=_interface):
1250
1250
""" Modify a variable so that it's a property which announces
1251
1251
its changes to DBus.
1257
1257
to the D-Bus. Default: no transform
1258
1258
variant_level: D-Bus variant level. Default: 1
1260
attrname = "_{}".format(dbus_name)
1260
attrname = "_{0}".format(dbus_name)
1261
1261
def setter(self, value):
1262
1262
if hasattr(self, "dbus_object_path"):
1263
1263
if (not hasattr(self, attrname) or
1264
1264
type_func(getattr(self, attrname, None))
1265
1265
!= type_func(value)):
1267
self.PropertiesChanged(_interface,
1272
dbus_value = transform_func(type_func(value),
1275
self.PropertyChanged(dbus.String(dbus_name),
1277
self.PropertiesChanged(_interface,
1279
dbus.String(dbus_name):
1280
dbus_value }), dbus.Array())
1266
dbus_value = transform_func(type_func(value),
1269
self.PropertyChanged(dbus.String(dbus_name),
1281
1271
setattr(self, attrname, value)
1283
1273
return property(lambda self: getattr(self, attrname), setter)
1303
1293
approval_delay = notifychangeproperty(dbus.UInt64,
1304
1294
"ApprovalDelay",
1306
lambda td: td.total_seconds()
1296
timedelta_to_milliseconds)
1308
1297
approval_duration = notifychangeproperty(
1309
1298
dbus.UInt64, "ApprovalDuration",
1310
type_func = lambda td: td.total_seconds() * 1000)
1299
type_func = timedelta_to_milliseconds)
1311
1300
host = notifychangeproperty(dbus.String, "Host")
1312
1301
timeout = notifychangeproperty(dbus.UInt64, "Timeout",
1313
type_func = lambda td:
1314
td.total_seconds() * 1000)
1303
timedelta_to_milliseconds)
1315
1304
extended_timeout = notifychangeproperty(
1316
1305
dbus.UInt64, "ExtendedTimeout",
1317
type_func = lambda td: td.total_seconds() * 1000)
1306
type_func = timedelta_to_milliseconds)
1318
1307
interval = notifychangeproperty(dbus.UInt64,
1321
lambda td: td.total_seconds()
1310
timedelta_to_milliseconds)
1323
1311
checker_command = notifychangeproperty(dbus.String, "Checker")
1324
secret = notifychangeproperty(dbus.ByteArray, "Secret",
1325
invalidate_only=True)
1327
1313
del notifychangeproperty
1355
1341
*args, **kwargs)
1357
1343
def start_checker(self, *args, **kwargs):
1358
old_checker_pid = getattr(self.checker, "pid", None)
1344
old_checker = self.checker
1345
if self.checker is not None:
1346
old_checker_pid = self.checker.pid
1348
old_checker_pid = None
1359
1349
r = Client.start_checker(self, *args, **kwargs)
1360
1350
# Only if new checker process was started
1361
1351
if (self.checker is not None
1371
1361
def approve(self, value=True):
1372
1362
self.approved = value
1373
gobject.timeout_add(int(self.approval_duration.total_seconds()
1374
* 1000), self._reset_approved)
1363
gobject.timeout_add(timedelta_to_milliseconds
1364
(self.approval_duration),
1365
self._reset_approved)
1375
1366
self.send_changedstate()
1377
1368
## D-Bus methods, signals & properties
1369
_interface = "se.recompile.Mandos.Client"
1373
@dbus_interface_annotations(_interface)
1375
return { "org.freedesktop.DBus.Property.EmitsChangedSignal":
1383
1380
# CheckerCompleted - signal
1395
1392
# PropertyChanged - signal
1396
@dbus_annotations({"org.freedesktop.DBus.Deprecated": "true"})
1397
1393
@dbus.service.signal(_interface, signature="sv")
1398
1394
def PropertyChanged(self, property, value):
1475
1471
access="readwrite")
1476
1472
def ApprovalDelay_dbus_property(self, value=None):
1477
1473
if value is None: # get
1478
return dbus.UInt64(self.approval_delay.total_seconds()
1474
return dbus.UInt64(self.approval_delay_milliseconds())
1480
1475
self.approval_delay = datetime.timedelta(0, 0, 0, value)
1482
1477
# ApprovalDuration - property
1484
1479
access="readwrite")
1485
1480
def ApprovalDuration_dbus_property(self, value=None):
1486
1481
if value is None: # get
1487
return dbus.UInt64(self.approval_duration.total_seconds()
1482
return dbus.UInt64(timedelta_to_milliseconds(
1483
self.approval_duration))
1489
1484
self.approval_duration = datetime.timedelta(0, 0, 0, value)
1491
1486
# Name - property
1504
1499
def Host_dbus_property(self, value=None):
1505
1500
if value is None: # get
1506
1501
return dbus.String(self.host)
1507
self.host = str(value)
1502
self.host = unicode(value)
1509
1504
# Created - property
1510
1505
@dbus_service_property(_interface, signature="s", access="read")
1557
1552
access="readwrite")
1558
1553
def Timeout_dbus_property(self, value=None):
1559
1554
if value is None: # get
1560
return dbus.UInt64(self.timeout.total_seconds() * 1000)
1555
return dbus.UInt64(self.timeout_milliseconds())
1561
1556
old_timeout = self.timeout
1562
1557
self.timeout = datetime.timedelta(0, 0, 0, value)
1563
1558
# Reschedule disabling
1574
1569
gobject.source_remove(self.disable_initiator_tag)
1575
1570
self.disable_initiator_tag = (
1576
1571
gobject.timeout_add(
1577
int((self.expires - now).total_seconds()
1578
* 1000), self.disable))
1572
timedelta_to_milliseconds(self.expires - now),
1580
1575
# ExtendedTimeout - property
1581
1576
@dbus_service_property(_interface, signature="t",
1582
1577
access="readwrite")
1583
1578
def ExtendedTimeout_dbus_property(self, value=None):
1584
1579
if value is None: # get
1585
return dbus.UInt64(self.extended_timeout.total_seconds()
1580
return dbus.UInt64(self.extended_timeout_milliseconds())
1587
1581
self.extended_timeout = datetime.timedelta(0, 0, 0, value)
1589
1583
# Interval - property
1591
1585
access="readwrite")
1592
1586
def Interval_dbus_property(self, value=None):
1593
1587
if value is None: # get
1594
return dbus.UInt64(self.interval.total_seconds() * 1000)
1588
return dbus.UInt64(self.interval_milliseconds())
1595
1589
self.interval = datetime.timedelta(0, 0, 0, value)
1596
1590
if getattr(self, "checker_initiator_tag", None) is None:
1608
1602
def Checker_dbus_property(self, value=None):
1609
1603
if value is None: # get
1610
1604
return dbus.String(self.checker_command)
1611
self.checker_command = str(value)
1605
self.checker_command = unicode(value)
1613
1607
# CheckerRunning - property
1614
1608
@dbus_service_property(_interface, signature="b",
1630
1624
@dbus_service_property(_interface, signature="ay",
1631
1625
access="write", byte_arrays=True)
1632
1626
def Secret_dbus_property(self, value):
1633
self.secret = bytes(value)
1627
self.secret = str(value)
1670
1664
def handle(self):
1671
1665
with contextlib.closing(self.server.child_pipe) as child_pipe:
1672
1666
logger.info("TCP connection from: %s",
1673
str(self.client_address))
1667
unicode(self.client_address))
1674
1668
logger.debug("Pipe FD: %d",
1675
1669
self.server.child_pipe.fileno())
1702
1696
logger.debug("Protocol version: %r", line)
1704
1698
if int(line.strip().split()[0]) > 1:
1705
raise RuntimeError(line)
1706
1700
except (ValueError, IndexError, RuntimeError) as error:
1707
1701
logger.error("Unknown protocol version: %s", error)
1757
1751
if self.server.use_dbus:
1758
1752
# Emit D-Bus signal
1759
1753
client.NeedApproval(
1760
client.approval_delay.total_seconds()
1761
* 1000, client.approved_by_default)
1754
client.approval_delay_milliseconds(),
1755
client.approved_by_default)
1763
1757
logger.warning("Client %s was not approved",
1770
1764
#wait until timeout or approved
1771
1765
time = datetime.datetime.now()
1772
1766
client.changedstate.acquire()
1773
client.changedstate.wait(delay.total_seconds())
1767
client.changedstate.wait(
1768
float(timedelta_to_milliseconds(delay)
1774
1770
client.changedstate.release()
1775
1771
time2 = datetime.datetime.now()
1776
1772
if (time2 - time) >= delay:
1914
1910
def add_pipe(self, parent_pipe, proc):
1915
1911
"""Dummy function; override as necessary"""
1916
raise NotImplementedError()
1912
raise NotImplementedError
1919
1915
class IPv6_TCPServer(MultiprocessingMixInWithPipe,
1976
1972
self.socket.setsockopt(socket.SOL_SOCKET,
1977
1973
SO_BINDTODEVICE,
1978
(self.interface + "\0")
1974
str(self.interface + '\0'))
1980
1975
except socket.error as error:
1981
1976
if error.errno == errno.EPERM:
1982
1977
logger.error("No permission to bind to"
2185
2180
token_duration = Token(re.compile(r"P"), None,
2186
2181
frozenset((token_year, token_month,
2187
2182
token_day, token_time,
2189
2184
# Define starting values
2190
2185
value = datetime.timedelta() # Value so far
2191
2186
found_token = None
2192
followers = frozenset((token_duration,)) # Following valid tokens
2187
followers = frozenset(token_duration,) # Following valid tokens
2193
2188
s = duration # String left to parse
2194
2189
# Loop until end token is found
2195
2190
while found_token is not token_end:
2255
2250
elif suffix == "w":
2256
2251
delta = datetime.timedelta(0, 0, 0, 0, 0, 0, value)
2258
raise ValueError("Unknown suffix {!r}"
2253
raise ValueError("Unknown suffix {0!r}"
2259
2254
.format(suffix))
2260
except IndexError as e:
2255
except (ValueError, IndexError) as e:
2261
2256
raise ValueError(*(e.args))
2262
2257
timevalue += delta
2263
2258
return timevalue
2278
2273
# Close all standard open file descriptors
2279
2274
null = os.open(os.devnull, os.O_NOCTTY | os.O_RDWR)
2280
2275
if not stat.S_ISCHR(os.fstat(null).st_mode):
2281
raise OSError(errno.ENODEV, "{} not a character device"
2276
raise OSError(errno.ENODEV,
2277
"{0} not a character device"
2282
2278
.format(os.devnull))
2283
2279
os.dup2(null, sys.stdin.fileno())
2284
2280
os.dup2(null, sys.stdout.fileno())
2295
2291
parser = argparse.ArgumentParser()
2296
2292
parser.add_argument("-v", "--version", action="version",
2297
version = "%(prog)s {}".format(version),
2293
version = "%(prog)s {0}".format(version),
2298
2294
help="show version number and exit")
2299
2295
parser.add_argument("-i", "--interface", metavar="IF",
2300
2296
help="Bind to interface IF")
2333
2329
help="Directory to save/restore state in")
2334
2330
parser.add_argument("--foreground", action="store_true",
2335
2331
help="Run in foreground", default=None)
2336
parser.add_argument("--no-zeroconf", action="store_false",
2337
dest="zeroconf", help="Do not use Zeroconf",
2340
2333
options = parser.parse_args()
2342
2335
if options.check:
2344
fail_count, test_count = doctest.testmod()
2345
sys.exit(os.EX_OK if fail_count == 0 else 1)
2347
2340
# Default values for config file for server-global settings
2348
2341
server_defaults = { "interface": "",
2392
2384
for option in ("interface", "address", "port", "debug",
2393
2385
"priority", "servicename", "configdir",
2394
2386
"use_dbus", "use_ipv6", "debuglevel", "restore",
2395
"statedir", "socket", "foreground", "zeroconf"):
2387
"statedir", "socket", "foreground"):
2396
2388
value = getattr(options, option)
2397
2389
if value is not None:
2398
2390
server_settings[option] = value
2400
2392
# Force all strings to be unicode
2401
2393
for option in server_settings.keys():
2402
if isinstance(server_settings[option], bytes):
2403
server_settings[option] = (server_settings[option]
2394
if type(server_settings[option]) is str:
2395
server_settings[option] = unicode(server_settings[option])
2405
2396
# Force all boolean options to be boolean
2406
2397
for option in ("debug", "use_dbus", "use_ipv6", "restore",
2407
"foreground", "zeroconf"):
2408
2399
server_settings[option] = bool(server_settings[option])
2409
2400
# Debug implies foreground
2410
2401
if server_settings["debug"]:
2414
2405
##################################################################
2416
if (not server_settings["zeroconf"] and
2417
not (server_settings["port"]
2418
or server_settings["socket"] != "")):
2419
parser.error("Needs port or socket to work without"
2422
2407
# For convenience
2423
2408
debug = server_settings["debug"]
2424
2409
debuglevel = server_settings["debuglevel"]
2427
2412
stored_state_path = os.path.join(server_settings["statedir"],
2428
2413
stored_state_file)
2429
2414
foreground = server_settings["foreground"]
2430
zeroconf = server_settings["zeroconf"]
2433
2417
initlogger(debug, logging.DEBUG)
2441
2425
if server_settings["servicename"] != "Mandos":
2442
2426
syslogger.setFormatter(logging.Formatter
2443
('Mandos ({}) [%(process)d]:'
2427
('Mandos ({0}) [%(process)d]:'
2444
2428
' %(levelname)s: %(message)s'
2445
2429
.format(server_settings
2446
2430
["servicename"])))
2454
2438
global mandos_dbus_service
2455
2439
mandos_dbus_service = None
2458
if server_settings["socket"] != "":
2459
socketfd = server_settings["socket"]
2460
2441
tcp_server = MandosServer((server_settings["address"],
2461
2442
server_settings["port"]),
2466
2447
gnutls_priority=
2467
2448
server_settings["priority"],
2468
2449
use_dbus=use_dbus,
2450
socketfd=(server_settings["socket"]
2470
2452
if not foreground:
2471
2453
pidfilename = "/run/mandos.pid"
2472
if not os.path.isdir("/run/."):
2473
pidfilename = "/var/run/mandos.pid"
2476
2456
pidfile = open(pidfilename, "w")
2542
2522
use_dbus = False
2543
2523
server_settings["use_dbus"] = False
2544
2524
tcp_server.use_dbus = False
2546
protocol = avahi.PROTO_INET6 if use_ipv6 else avahi.PROTO_INET
2547
service = AvahiServiceToSyslog(name =
2548
server_settings["servicename"],
2549
servicetype = "_mandos._tcp",
2550
protocol = protocol, bus = bus)
2551
if server_settings["interface"]:
2552
service.interface = (if_nametoindex
2553
(server_settings["interface"]
2525
protocol = avahi.PROTO_INET6 if use_ipv6 else avahi.PROTO_INET
2526
service = AvahiServiceToSyslog(name =
2527
server_settings["servicename"],
2528
servicetype = "_mandos._tcp",
2529
protocol = protocol, bus = bus)
2530
if server_settings["interface"]:
2531
service.interface = (if_nametoindex
2532
(str(server_settings["interface"])))
2556
2534
global multiprocessing_manager
2557
2535
multiprocessing_manager = multiprocessing.Manager()
2581
2559
os.remove(stored_state_path)
2582
2560
except IOError as e:
2583
2561
if e.errno == errno.ENOENT:
2584
logger.warning("Could not load persistent state: {}"
2562
logger.warning("Could not load persistent state: {0}"
2585
2563
.format(os.strerror(e.errno)))
2587
2565
logger.critical("Could not load persistent state:",
2623
2601
if datetime.datetime.utcnow() >= client["expires"]:
2624
2602
if not client["last_checked_ok"]:
2625
2603
logger.warning(
2626
"disabling client {} - Client never "
2604
"disabling client {0} - Client never "
2627
2605
"performed a successful checker"
2628
2606
.format(client_name))
2629
2607
client["enabled"] = False
2630
2608
elif client["last_checker_status"] != 0:
2631
2609
logger.warning(
2632
"disabling client {} - Client last"
2633
" checker failed with error code {}"
2610
"disabling client {0} - Client "
2611
"last checker failed with error code {1}"
2634
2612
.format(client_name,
2635
2613
client["last_checker_status"]))
2636
2614
client["enabled"] = False
2649
2627
except PGPError:
2650
2628
# If decryption fails, we use secret from new settings
2651
logger.debug("Failed to decrypt {} old secret"
2629
logger.debug("Failed to decrypt {0} old secret"
2652
2630
.format(client_name))
2653
2631
client["secret"] = (
2654
2632
client_settings[client_name]["secret"])
2662
2640
clients_data[client_name] = client_settings[client_name]
2664
2642
# Create all client objects
2665
for client_name, client in clients_data.items():
2643
for client_name, client in clients_data.iteritems():
2666
2644
tcp_server.clients[client_name] = client_class(
2667
2645
name = client_name, settings = client,
2668
2646
server_settings = server_settings)
2727
2705
def GetAllClientsWithProperties(self):
2729
2707
return dbus.Dictionary(
2730
{ c.dbus_object_path: c.GetAll("")
2731
for c in tcp_server.clients.itervalues() },
2708
((c.dbus_object_path, c.GetAll(""))
2709
for c in tcp_server.clients.itervalues()),
2732
2710
signature="oa{sv}")
2734
2712
@dbus.service.method(_interface, in_signature="o")
2773
2750
# A list of attributes that can not be pickled
2775
exclude = { "bus", "changedstate", "secret",
2776
"checker", "server_settings" }
2752
exclude = set(("bus", "changedstate", "secret",
2753
"checker", "server_settings"))
2777
2754
for name, typ in (inspect.getmembers
2778
2755
(dbus.service.Object)):
2779
2756
exclude.add(name)
2802
2779
except NameError:
2804
2781
if e.errno in (errno.ENOENT, errno.EACCES, errno.EEXIST):
2805
logger.warning("Could not save persistent state: {}"
2782
logger.warning("Could not save persistent state: {0}"
2806
2783
.format(os.strerror(e.errno)))
2808
2785
logger.warning("Could not save persistent state:",
2812
2789
# Delete all clients, and settings from config
2813
2790
while tcp_server.clients:
2837
2814
tcp_server.server_activate()
2839
2816
# Find out what port we got
2841
service.port = tcp_server.socket.getsockname()[1]
2817
service.port = tcp_server.socket.getsockname()[1]
2843
2819
logger.info("Now listening on address %r, port %d,"
2844
2820
" flowinfo %d, scope_id %d",
2850
2826
#service.interface = tcp_server.socket.getsockname()[3]
2854
# From the Avahi example code
2857
except dbus.exceptions.DBusException as error:
2858
logger.critical("D-Bus Exception", exc_info=error)
2861
# End of Avahi example code
2829
# From the Avahi example code
2832
except dbus.exceptions.DBusException as error:
2833
logger.critical("D-Bus Exception", exc_info=error)
2836
# End of Avahi example code
2863
2838
gobject.io_add_watch(tcp_server.fileno(), gobject.IO_IN,
2864
2839
lambda *args, **kwargs: