11
11
# and some lines in "main".
13
13
# Everything else is
14
# Copyright © 2008 Teddy Hogeborn & Björn Påhlsson
14
# Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
16
16
# This program is free software: you can redistribute it and/or modify
17
17
# it under the terms of the GNU General Public License as published by
30
30
# Contact the authors at <mandos@fukt.bsnet.se>.
33
from __future__ import division, with_statement, absolute_import
33
from __future__ import division
35
35
import SocketServer
37
38
from optparse import OptionParser
111
108
a sensible number of times
113
110
def __init__(self, interface = avahi.IF_UNSPEC, name = None,
114
servicetype = None, port = None, TXT = None, domain = "",
111
type = None, port = None, TXT = None, domain = "",
115
112
host = "", max_renames = 32768):
116
113
self.interface = interface
118
self.type = servicetype
130
127
if self.rename_count >= self.max_renames:
131
128
logger.critical(u"No suitable Zeroconf service name found"
132
129
u" after %i retries, exiting.",
134
131
raise AvahiServiceError("Too many renames")
135
132
self.name = server.GetAlternativeServiceName(self.name)
136
133
logger.info(u"Changing Zeroconf service name to %r ...",
181
178
secret: bytestring; sent verbatim (over TLS) to client
182
179
host: string; available for use by the checker command
183
180
created: datetime.datetime(); object creation, not client host
185
181
last_checked_ok: datetime.datetime() or None if not yet checked OK
186
182
timeout: datetime.timedelta(); How long from last_checked_ok
187
183
until this client is invalid
203
199
_timeout_milliseconds: Used when calling gobject.timeout_add()
204
200
_interval_milliseconds: - '' -
206
interface = u"org.mandos_system.Mandos.Clients"
208
@dbus.service.method(interface, out_signature="s")
210
"D-Bus getter method"
213
@dbus.service.method(interface, out_signature="s")
214
def getFingerprint(self):
215
"D-Bus getter method"
216
return self.fingerprint
218
@dbus.service.method(interface, in_signature="ay",
220
def setSecret(self, secret):
221
"D-Bus setter method"
224
202
def _set_timeout(self, timeout):
225
"Setter function for the 'timeout' attribute"
203
"Setter function for 'timeout' attribute"
226
204
self._timeout = timeout
227
205
self._timeout_milliseconds = ((self.timeout.days
228
206
* 24 * 60 * 60 * 1000)
229
207
+ (self.timeout.seconds * 1000)
230
208
+ (self.timeout.microseconds
233
self.TimeoutChanged(self._timeout_milliseconds)
234
timeout = property(lambda self: self._timeout, _set_timeout)
210
timeout = property(lambda self: self._timeout,
237
@dbus.service.method(interface, out_signature="t")
238
def getTimeout(self):
239
"D-Bus getter method"
240
return self._timeout_milliseconds
242
@dbus.service.signal(interface, signature="t")
243
def TimeoutChanged(self, t):
247
213
def _set_interval(self, interval):
248
"Setter function for the 'interval' attribute"
214
"Setter function for 'interval' attribute"
249
215
self._interval = interval
250
216
self._interval_milliseconds = ((self.interval.days
251
217
* 24 * 60 * 60 * 1000)
254
220
+ (self.interval.microseconds
257
self.IntervalChanged(self._interval_milliseconds)
258
interval = property(lambda self: self._interval, _set_interval)
222
interval = property(lambda self: self._interval,
259
224
del _set_interval
261
@dbus.service.method(interface, out_signature="t")
262
def getInterval(self):
263
"D-Bus getter method"
264
return self._interval_milliseconds
266
@dbus.service.signal(interface, signature="t")
267
def IntervalChanged(self, t):
271
def __init__(self, name = None, stop_hook=None, config=None):
225
def __init__(self, name = None, stop_hook=None, config={}):
272
226
"""Note: the 'checker' key in 'config' sets the
273
227
'checker_command' attribute and *not* the 'checker'
275
dbus.service.Object.__init__(self, bus,
277
% name.replace(".", "_"))
281
230
logger.debug(u"Creating client %r", self.name)
282
231
# Uppercase and remove spaces from fingerprint for later
288
237
if "secret" in config:
289
238
self.secret = config["secret"].decode(u"base64")
290
239
elif "secfile" in config:
291
with closing(open(os.path.expanduser
293
(config["secfile"])))) \
295
self.secret = secfile.read()
240
sf = open(config["secfile"])
241
self.secret = sf.read()
297
244
raise TypeError(u"No secret or secfile for client %s"
299
246
self.host = config.get("host", "")
300
247
self.created = datetime.datetime.now()
302
248
self.last_checked_ok = None
303
249
self.timeout = string_to_delta(config["timeout"])
304
250
self.interval = string_to_delta(config["interval"])
308
254
self.stop_initiator_tag = None
309
255
self.checker_callback_tag = None
310
256
self.check_command = config["checker"]
313
258
"""Start this client's checker and timeout hooks"""
315
259
# Schedule a new checker to be started an 'interval' from now,
316
260
# and every interval from then on.
317
261
self.checker_initiator_tag = gobject.timeout_add\
323
267
self.stop_initiator_tag = gobject.timeout_add\
324
268
(self._timeout_milliseconds,
327
self.StateChanged(True)
329
@dbus.service.signal(interface, signature="b")
330
def StateChanged(self, started):
335
"""Stop this client."""
336
if getattr(self, "started", False):
272
The possibility that a client might be restarted is left open,
273
but not currently used."""
274
# If this client doesn't have a secret, it is already stopped.
275
if hasattr(self, "secret") and self.secret:
337
276
logger.info(u"Stopping client %s", self.name)
340
280
if getattr(self, "stop_initiator_tag", False):
346
286
self.stop_checker()
347
287
if self.stop_hook:
348
288
self.stop_hook(self)
350
self.StateChanged(False)
351
289
# Do not run this again if called by a gobject.timeout_add
354
Stop = dbus.service.method(interface)(stop)
356
291
def __del__(self):
357
292
self.stop_hook = None
360
294
def checker_callback(self, pid, condition):
361
295
"""The checker has completed, so take appropriate actions."""
296
now = datetime.datetime.now()
362
297
self.checker_callback_tag = None
363
298
self.checker = None
364
299
if os.WIFEXITED(condition) \
365
300
and (os.WEXITSTATUS(condition) == 0):
366
301
logger.info(u"Checker for %(name)s succeeded",
369
self.CheckerCompleted(True)
303
self.last_checked_ok = now
304
gobject.source_remove(self.stop_initiator_tag)
305
self.stop_initiator_tag = gobject.timeout_add\
306
(self._timeout_milliseconds,
371
308
elif not os.WIFEXITED(condition):
372
309
logger.warning(u"Checker for %(name)s crashed?",
375
self.CheckerCompleted(False)
377
312
logger.info(u"Checker for %(name)s failed",
380
self.CheckerCompleted(False)
382
@dbus.service.signal(interface, signature="b")
383
def CheckerCompleted(self, success):
387
def bump_timeout(self):
388
"""Bump up the timeout for this client.
389
This should only be called when the client has been seen,
392
self.last_checked_ok = datetime.datetime.now()
393
gobject.source_remove(self.stop_initiator_tag)
394
self.stop_initiator_tag = gobject.timeout_add\
395
(self._timeout_milliseconds, self.stop)
397
bumpTimeout = dbus.service.method(interface)(bump_timeout)
399
314
def start_checker(self):
400
315
"""Start a new checker subprocess if one is not running.
401
316
If a checker already exists, leave it running and do
436
351
self.checker_callback_tag = gobject.child_watch_add\
437
352
(self.checker.pid,
438
353
self.checker_callback)
440
self.CheckerStarted(command)
441
354
except OSError, error:
442
355
logger.error(u"Failed to start subprocess: %s",
444
357
# Re-run this periodically if run by gobject.timeout_add
447
@dbus.service.signal(interface, signature="s")
448
def CheckerStarted(self, command):
451
@dbus.service.method(interface, out_signature="b")
452
def checkerIsRunning(self):
453
"D-Bus getter method"
454
return self.checker is not None
456
359
def stop_checker(self):
457
360
"""Force the checker process, if any, to stop."""
458
361
if self.checker_callback_tag:
470
373
if error.errno != errno.ESRCH: # No such process
472
375
self.checker = None
474
StopChecker = dbus.service.method(interface)(stop_checker)
476
376
def still_valid(self):
477
377
"""Has the timeout not yet passed for this client?"""
480
378
now = datetime.datetime.now()
481
379
if self.last_checked_ok is None:
482
380
return now < (self.created + self.timeout)
484
382
return now < (self.last_checked_ok + self.timeout)
486
stillValid = dbus.service.method(interface, out_signature="b")\
492
385
def peer_certificate(session):
522
415
(crt, ctypes.byref(datum),
523
416
gnutls.library.constants.GNUTLS_OPENPGP_FMT_RAW)
524
417
# Verify the self signature in the key
525
crtverify = ctypes.c_uint()
418
crtverify = ctypes.c_uint();
526
419
gnutls.library.functions.gnutls_openpgp_crt_verify_self\
527
420
(crt, 0, ctypes.byref(crtverify))
528
421
if crtverify.value != 0:
529
422
gnutls.library.functions.gnutls_openpgp_crt_deinit(crt)
530
423
raise gnutls.errors.CertificateSecurityError("Verify failed")
531
424
# New buffer for the fingerprint
532
buf = ctypes.create_string_buffer(20)
533
buf_len = ctypes.c_size_t()
425
buffer = ctypes.create_string_buffer(20)
426
buffer_length = ctypes.c_size_t()
534
427
# Get the fingerprint from the certificate into the buffer
535
428
gnutls.library.functions.gnutls_openpgp_crt_get_fingerprint\
536
(crt, ctypes.byref(buf), ctypes.byref(buf_len))
429
(crt, ctypes.byref(buffer), ctypes.byref(buffer_length))
537
430
# Deinit the certificate
538
431
gnutls.library.functions.gnutls_openpgp_crt_deinit(crt)
539
432
# Convert the buffer to a Python bytestring
540
fpr = ctypes.string_at(buf, buf_len.value)
433
fpr = ctypes.string_at(buffer, buffer_length.value)
541
434
# Convert the bytestring to hexadecimal notation
542
435
hex_fpr = u''.join(u"%02X" % ord(char) for char in fpr)
546
class TCP_handler(SocketServer.BaseRequestHandler, object):
439
class tcp_handler(SocketServer.BaseRequestHandler, object):
547
440
"""A TCP request handler class.
548
441
Instantiated by IPv6_TCPServer for each request to handle it.
549
442
Note: This will run in its own forked process."""
551
444
def handle(self):
552
445
logger.info(u"TCP connection from: %s",
553
unicode(self.client_address))
446
unicode(self.client_address))
554
447
session = gnutls.connection.ClientSession\
555
448
(self.request, gnutls.connection.X509Credentials())
571
464
#priority = ':'.join(("NONE", "+VERS-TLS1.1", "+AES-256-CBC",
572
465
# "+SHA1", "+COMP-NULL", "+CTYPE-OPENPGP",
574
# Use a fallback default, since this MUST be set.
575
priority = self.server.settings.get("priority", "NORMAL")
467
priority = "NORMAL" # Fallback default, since this
469
if self.server.settings["priority"]:
470
priority = self.server.settings["priority"]
576
471
gnutls.library.functions.gnutls_priority_set_direct\
577
(session._c_object, priority, None)
472
(session._c_object, priority, None);
580
475
session.handshake()
623
class IPv6_TCPServer(SocketServer.ForkingMixIn,
624
SocketServer.TCPServer, object):
516
class IPv6_TCPServer(SocketServer.ForkingTCPServer, object):
625
517
"""IPv6 TCP server. Accepts 'None' as address and/or port.
627
519
settings: Server settings
637
529
self.clients = kwargs["clients"]
638
530
del kwargs["clients"]
639
531
self.enabled = False
640
super(IPv6_TCPServer, self).__init__(*args, **kwargs)
532
return super(type(self), self).__init__(*args, **kwargs)
641
533
def server_bind(self):
642
534
"""This overrides the normal server_bind() function
643
535
to bind to an interface if one was specified, and also NOT to
674
566
# ["interface"]))
675
return super(IPv6_TCPServer, self).server_bind()
567
return super(type(self), self).server_bind()
676
568
def server_activate(self):
678
return super(IPv6_TCPServer, self).server_activate()
570
return super(type(self), self).server_activate()
679
571
def enable(self):
680
572
self.enabled = True
746
638
"""Call the C function if_nametoindex(), or equivalent"""
747
639
global if_nametoindex
641
if "ctypes.util" not in sys.modules:
749
643
if_nametoindex = ctypes.cdll.LoadLibrary\
750
644
(ctypes.util.find_library("c")).if_nametoindex
751
645
except (OSError, AttributeError):
756
650
def if_nametoindex(interface):
757
651
"Get an interface index the hard way, i.e. using fcntl()"
758
652
SIOCGIFINDEX = 0x8933 # From /usr/include/linux/sockios.h
759
with closing(socket.socket()) as s:
760
ifreq = fcntl.ioctl(s, SIOCGIFINDEX,
761
struct.pack("16s16x", interface))
654
ifreq = fcntl.ioctl(s, SIOCGIFINDEX,
655
struct.pack("16s16x", interface))
762
657
interface_index = struct.unpack("I", ifreq[16:20])[0]
763
658
return interface_index
764
659
return if_nametoindex(interface)
686
global main_loop_started
687
main_loop_started = False
791
689
parser = OptionParser(version = "%%prog %s" % version)
792
690
parser.add_option("-i", "--interface", type="string",
793
691
metavar="IF", help="Bind to interface IF")
808
706
default="/etc/mandos", metavar="DIR",
809
707
help="Directory to search for configuration"
811
options = parser.parse_args()[0]
709
(options, args) = parser.parse_args()
813
711
if options.check:
872
770
tcp_server = IPv6_TCPServer((server_settings["address"],
873
771
server_settings["port"]),
875
773
settings=server_settings,
877
775
pidfilename = "/var/run/mandos.pid"
907
805
service = AvahiService(name = server_settings["servicename"],
908
servicetype = "_mandos._tcp", )
806
type = "_mandos._tcp", );
909
807
if server_settings["interface"]:
910
808
service.interface = if_nametoindex\
911
809
(server_settings["interface"])