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
110
108
a sensible number of times
112
110
def __init__(self, interface = avahi.IF_UNSPEC, name = None,
113
servicetype = None, port = None, TXT = None, domain = "",
111
type = None, port = None, TXT = None, domain = "",
114
112
host = "", max_renames = 32768):
115
113
self.interface = interface
117
self.type = servicetype
129
127
if self.rename_count >= self.max_renames:
130
128
logger.critical(u"No suitable Zeroconf service name found"
131
129
u" after %i retries, exiting.",
133
131
raise AvahiServiceError("Too many renames")
134
132
self.name = server.GetAlternativeServiceName(self.name)
135
133
logger.info(u"Changing Zeroconf service name to %r ...",
224
222
interval = property(lambda self: self._interval,
226
224
del _set_interval
227
def __init__(self, name = None, stop_hook=None, config=None):
225
def __init__(self, name = None, stop_hook=None, config={}):
228
226
"""Note: the 'checker' key in 'config' sets the
229
227
'checker_command' attribute and *not* the 'checker'
234
230
logger.debug(u"Creating client %r", self.name)
235
231
# Uppercase and remove spaces from fingerprint for later
241
237
if "secret" in config:
242
238
self.secret = config["secret"].decode(u"base64")
243
239
elif "secfile" in config:
244
with closing(open(os.path.expanduser
246
(config["secfile"])))) \
248
self.secret = secfile.read()
240
sf = open(config["secfile"])
241
self.secret = sf.read()
250
244
raise TypeError(u"No secret or secfile for client %s"
300
294
def checker_callback(self, pid, condition):
301
295
"""The checker has completed, so take appropriate actions."""
296
now = datetime.datetime.now()
302
297
self.checker_callback_tag = None
303
298
self.checker = None
304
299
if os.WIFEXITED(condition) \
305
300
and (os.WEXITSTATUS(condition) == 0):
306
301
logger.info(u"Checker for %(name)s succeeded",
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,
309
308
elif not os.WIFEXITED(condition):
310
309
logger.warning(u"Checker for %(name)s crashed?",
313
312
logger.info(u"Checker for %(name)s failed",
315
def bump_timeout(self):
316
"""Bump up the timeout for this client.
317
This should only be called when the client has been seen,
320
self.last_checked_ok = datetime.datetime.now()
321
gobject.source_remove(self.stop_initiator_tag)
322
self.stop_initiator_tag = gobject.timeout_add\
323
(self._timeout_milliseconds, self.stop)
324
314
def start_checker(self):
325
315
"""Start a new checker subprocess if one is not running.
326
316
If a checker already exists, leave it running and do
425
415
(crt, ctypes.byref(datum),
426
416
gnutls.library.constants.GNUTLS_OPENPGP_FMT_RAW)
427
417
# Verify the self signature in the key
428
crtverify = ctypes.c_uint()
418
crtverify = ctypes.c_uint();
429
419
gnutls.library.functions.gnutls_openpgp_crt_verify_self\
430
420
(crt, 0, ctypes.byref(crtverify))
431
421
if crtverify.value != 0:
432
422
gnutls.library.functions.gnutls_openpgp_crt_deinit(crt)
433
423
raise gnutls.errors.CertificateSecurityError("Verify failed")
434
424
# New buffer for the fingerprint
435
buf = ctypes.create_string_buffer(20)
436
buf_len = ctypes.c_size_t()
425
buffer = ctypes.create_string_buffer(20)
426
buffer_length = ctypes.c_size_t()
437
427
# Get the fingerprint from the certificate into the buffer
438
428
gnutls.library.functions.gnutls_openpgp_crt_get_fingerprint\
439
(crt, ctypes.byref(buf), ctypes.byref(buf_len))
429
(crt, ctypes.byref(buffer), ctypes.byref(buffer_length))
440
430
# Deinit the certificate
441
431
gnutls.library.functions.gnutls_openpgp_crt_deinit(crt)
442
432
# Convert the buffer to a Python bytestring
443
fpr = ctypes.string_at(buf, buf_len.value)
433
fpr = ctypes.string_at(buffer, buffer_length.value)
444
434
# Convert the bytestring to hexadecimal notation
445
435
hex_fpr = u''.join(u"%02X" % ord(char) for char in fpr)
449
class TCP_handler(SocketServer.BaseRequestHandler, object):
439
class tcp_handler(SocketServer.BaseRequestHandler, object):
450
440
"""A TCP request handler class.
451
441
Instantiated by IPv6_TCPServer for each request to handle it.
452
442
Note: This will run in its own forked process."""
454
444
def handle(self):
455
445
logger.info(u"TCP connection from: %s",
456
unicode(self.client_address))
446
unicode(self.client_address))
457
447
session = gnutls.connection.ClientSession\
458
448
(self.request, gnutls.connection.X509Credentials())
474
464
#priority = ':'.join(("NONE", "+VERS-TLS1.1", "+AES-256-CBC",
475
465
# "+SHA1", "+COMP-NULL", "+CTYPE-OPENPGP",
477
# Use a fallback default, since this MUST be set.
478
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"]
479
471
gnutls.library.functions.gnutls_priority_set_direct\
480
(session._c_object, priority, None)
472
(session._c_object, priority, None);
483
475
session.handshake()
526
class IPv6_TCPServer(SocketServer.ForkingMixIn,
527
SocketServer.TCPServer, object):
516
class IPv6_TCPServer(SocketServer.ForkingTCPServer, object):
528
517
"""IPv6 TCP server. Accepts 'None' as address and/or port.
530
519
settings: Server settings
540
529
self.clients = kwargs["clients"]
541
530
del kwargs["clients"]
542
531
self.enabled = False
543
super(IPv6_TCPServer, self).__init__(*args, **kwargs)
532
return super(type(self), self).__init__(*args, **kwargs)
544
533
def server_bind(self):
545
534
"""This overrides the normal server_bind() function
546
535
to bind to an interface if one was specified, and also NOT to
577
566
# ["interface"]))
578
return super(IPv6_TCPServer, self).server_bind()
567
return super(type(self), self).server_bind()
579
568
def server_activate(self):
581
return super(IPv6_TCPServer, self).server_activate()
570
return super(type(self), self).server_activate()
582
571
def enable(self):
583
572
self.enabled = True
649
638
"""Call the C function if_nametoindex(), or equivalent"""
650
639
global if_nametoindex
641
if "ctypes.util" not in sys.modules:
652
643
if_nametoindex = ctypes.cdll.LoadLibrary\
653
644
(ctypes.util.find_library("c")).if_nametoindex
654
645
except (OSError, AttributeError):
659
650
def if_nametoindex(interface):
660
651
"Get an interface index the hard way, i.e. using fcntl()"
661
652
SIOCGIFINDEX = 0x8933 # From /usr/include/linux/sockios.h
662
with closing(socket.socket()) as s:
663
ifreq = fcntl.ioctl(s, SIOCGIFINDEX,
664
struct.pack("16s16x", interface))
654
ifreq = fcntl.ioctl(s, SIOCGIFINDEX,
655
struct.pack("16s16x", interface))
665
657
interface_index = struct.unpack("I", ifreq[16:20])[0]
666
658
return interface_index
667
659
return if_nametoindex(interface)
686
global main_loop_started
687
main_loop_started = False
694
689
parser = OptionParser(version = "%%prog %s" % version)
695
690
parser.add_option("-i", "--interface", type="string",
696
691
metavar="IF", help="Bind to interface IF")
711
706
default="/etc/mandos", metavar="DIR",
712
707
help="Directory to search for configuration"
714
options = parser.parse_args()[0]
709
(options, args) = parser.parse_args()
716
711
if options.check:
775
770
tcp_server = IPv6_TCPServer((server_settings["address"],
776
771
server_settings["port"]),
778
773
settings=server_settings,
780
775
pidfilename = "/var/run/mandos.pid"
810
805
service = AvahiService(name = server_settings["servicename"],
811
servicetype = "_mandos._tcp", )
806
type = "_mandos._tcp", );
812
807
if server_settings["interface"]:
813
808
service.interface = if_nametoindex\
814
809
(server_settings["interface"])