11
11
# and some lines in "main".
13
13
# Everything else is
14
# Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
14
# Copyright © 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
33
from __future__ import division, with_statement, absolute_import
35
35
import SocketServer
38
37
from optparse import OptionParser
107
111
a sensible number of times
109
113
def __init__(self, interface = avahi.IF_UNSPEC, name = None,
110
type = None, port = None, TXT = None, domain = "",
114
servicetype = None, port = None, TXT = None, domain = "",
111
115
host = "", max_renames = 32768):
112
116
self.interface = interface
118
self.type = servicetype
126
130
if self.rename_count >= self.max_renames:
127
131
logger.critical(u"No suitable Zeroconf service name found"
128
132
u" after %i retries, exiting.",
130
134
raise AvahiServiceError("Too many renames")
131
135
self.name = server.GetAlternativeServiceName(self.name)
132
136
logger.info(u"Changing Zeroconf service name to %r ...",
177
181
secret: bytestring; sent verbatim (over TLS) to client
178
182
host: string; available for use by the checker command
179
183
created: datetime.datetime(); object creation, not client host
180
185
last_checked_ok: datetime.datetime() or None if not yet checked OK
181
186
timeout: datetime.timedelta(); How long from last_checked_ok
182
187
until this client is invalid
198
203
_timeout_milliseconds: Used when calling gobject.timeout_add()
199
204
_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"
201
224
def _set_timeout(self, timeout):
202
"Setter function for 'timeout' attribute"
225
"Setter function for the 'timeout' attribute"
203
226
self._timeout = timeout
204
227
self._timeout_milliseconds = ((self.timeout.days
205
228
* 24 * 60 * 60 * 1000)
206
229
+ (self.timeout.seconds * 1000)
207
230
+ (self.timeout.microseconds
209
timeout = property(lambda self: self._timeout,
233
self.TimeoutChanged(self._timeout_milliseconds)
234
timeout = property(lambda self: self._timeout, _set_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):
212
247
def _set_interval(self, interval):
213
"Setter function for 'interval' attribute"
248
"Setter function for the 'interval' attribute"
214
249
self._interval = interval
215
250
self._interval_milliseconds = ((self.interval.days
216
251
* 24 * 60 * 60 * 1000)
219
254
+ (self.interval.microseconds
221
interval = property(lambda self: self._interval,
257
self.IntervalChanged(self._interval_milliseconds)
258
interval = property(lambda self: self._interval, _set_interval)
223
259
del _set_interval
224
def __init__(self, name = None, stop_hook=None, config={}):
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
272
"""Note: the 'checker' key in 'config' sets the
226
273
'checker_command' attribute and *not* the 'checker'
275
dbus.service.Object.__init__(self, bus,
277
% name.replace(".", "_"))
229
281
logger.debug(u"Creating client %r", self.name)
230
282
# Uppercase and remove spaces from fingerprint for later
236
288
if "secret" in config:
237
289
self.secret = config["secret"].decode(u"base64")
238
290
elif "secfile" in config:
239
sf = open(config["secfile"])
240
self.secret = sf.read()
291
with closing(open(os.path.expanduser
293
(config["secfile"])))) \
295
self.secret = secfile.read()
243
297
raise TypeError(u"No secret or secfile for client %s"
245
299
self.host = config.get("host", "")
246
300
self.created = datetime.datetime.now()
247
302
self.last_checked_ok = None
248
303
self.timeout = string_to_delta(config["timeout"])
249
304
self.interval = string_to_delta(config["interval"])
253
308
self.stop_initiator_tag = None
254
309
self.checker_callback_tag = None
255
310
self.check_command = config["checker"]
257
313
"""Start this client's checker and timeout hooks"""
258
315
# Schedule a new checker to be started an 'interval' from now,
259
316
# and every interval from then on.
260
317
self.checker_initiator_tag = gobject.timeout_add\
266
323
self.stop_initiator_tag = gobject.timeout_add\
267
324
(self._timeout_milliseconds,
327
self.StateChanged(True)
329
@dbus.service.signal(interface, signature="b")
330
def StateChanged(self, started):
271
The possibility that a client might be restarted is left open,
272
but not currently used."""
273
# If this client doesn't have a secret, it is already stopped.
274
if hasattr(self, "secret") and self.secret:
335
"""Stop this client."""
336
if getattr(self, "started", False):
275
337
logger.info(u"Stopping client %s", self.name)
279
340
if getattr(self, "stop_initiator_tag", False):
285
346
self.stop_checker()
286
347
if self.stop_hook:
287
348
self.stop_hook(self)
350
self.StateChanged(False)
288
351
# Do not run this again if called by a gobject.timeout_add
354
Stop = dbus.service.method(interface)(stop)
290
356
def __del__(self):
291
357
self.stop_hook = None
293
360
def checker_callback(self, pid, condition):
294
361
"""The checker has completed, so take appropriate actions."""
295
now = datetime.datetime.now()
296
362
self.checker_callback_tag = None
297
363
self.checker = None
298
364
if os.WIFEXITED(condition) \
299
365
and (os.WEXITSTATUS(condition) == 0):
300
366
logger.info(u"Checker for %(name)s succeeded",
302
self.last_checked_ok = now
303
gobject.source_remove(self.stop_initiator_tag)
304
self.stop_initiator_tag = gobject.timeout_add\
305
(self._timeout_milliseconds,
369
self.CheckerCompleted(True)
307
371
elif not os.WIFEXITED(condition):
308
372
logger.warning(u"Checker for %(name)s crashed?",
375
self.CheckerCompleted(False)
311
377
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)
313
399
def start_checker(self):
314
400
"""Start a new checker subprocess if one is not running.
315
401
If a checker already exists, leave it running and do
350
436
self.checker_callback_tag = gobject.child_watch_add\
351
437
(self.checker.pid,
352
438
self.checker_callback)
440
self.CheckerStarted(command)
353
441
except OSError, error:
354
442
logger.error(u"Failed to start subprocess: %s",
356
444
# 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
358
456
def stop_checker(self):
359
457
"""Force the checker process, if any, to stop."""
360
458
if self.checker_callback_tag:
372
470
if error.errno != errno.ESRCH: # No such process
374
472
self.checker = None
474
StopChecker = dbus.service.method(interface)(stop_checker)
375
476
def still_valid(self):
376
477
"""Has the timeout not yet passed for this client?"""
377
480
now = datetime.datetime.now()
378
481
if self.last_checked_ok is None:
379
482
return now < (self.created + self.timeout)
381
484
return now < (self.last_checked_ok + self.timeout)
486
stillValid = dbus.service.method(interface, out_signature="b")\
384
492
def peer_certificate(session):
414
522
(crt, ctypes.byref(datum),
415
523
gnutls.library.constants.GNUTLS_OPENPGP_FMT_RAW)
416
524
# Verify the self signature in the key
417
crtverify = ctypes.c_uint();
525
crtverify = ctypes.c_uint()
418
526
gnutls.library.functions.gnutls_openpgp_crt_verify_self\
419
527
(crt, 0, ctypes.byref(crtverify))
420
528
if crtverify.value != 0:
421
529
gnutls.library.functions.gnutls_openpgp_crt_deinit(crt)
422
530
raise gnutls.errors.CertificateSecurityError("Verify failed")
423
531
# New buffer for the fingerprint
424
buffer = ctypes.create_string_buffer(20)
425
buffer_length = ctypes.c_size_t()
532
buf = ctypes.create_string_buffer(20)
533
buf_len = ctypes.c_size_t()
426
534
# Get the fingerprint from the certificate into the buffer
427
535
gnutls.library.functions.gnutls_openpgp_crt_get_fingerprint\
428
(crt, ctypes.byref(buffer), ctypes.byref(buffer_length))
536
(crt, ctypes.byref(buf), ctypes.byref(buf_len))
429
537
# Deinit the certificate
430
538
gnutls.library.functions.gnutls_openpgp_crt_deinit(crt)
431
539
# Convert the buffer to a Python bytestring
432
fpr = ctypes.string_at(buffer, buffer_length.value)
540
fpr = ctypes.string_at(buf, buf_len.value)
433
541
# Convert the bytestring to hexadecimal notation
434
542
hex_fpr = u''.join(u"%02X" % ord(char) for char in fpr)
438
class tcp_handler(SocketServer.BaseRequestHandler, object):
546
class TCP_handler(SocketServer.BaseRequestHandler, object):
439
547
"""A TCP request handler class.
440
548
Instantiated by IPv6_TCPServer for each request to handle it.
441
549
Note: This will run in its own forked process."""
443
551
def handle(self):
444
552
logger.info(u"TCP connection from: %s",
445
unicode(self.client_address))
553
unicode(self.client_address))
446
554
session = gnutls.connection.ClientSession\
447
555
(self.request, gnutls.connection.X509Credentials())
463
571
#priority = ':'.join(("NONE", "+VERS-TLS1.1", "+AES-256-CBC",
464
572
# "+SHA1", "+COMP-NULL", "+CTYPE-OPENPGP",
466
priority = "NORMAL" # Fallback default, since this
468
if self.server.settings["priority"]:
469
priority = self.server.settings["priority"]
574
# Use a fallback default, since this MUST be set.
575
priority = self.server.settings.get("priority", "NORMAL")
470
576
gnutls.library.functions.gnutls_priority_set_direct\
471
(session._c_object, priority, None);
577
(session._c_object, priority, None)
474
580
session.handshake()
515
class IPv6_TCPServer(SocketServer.ForkingTCPServer, object):
623
class IPv6_TCPServer(SocketServer.ForkingMixIn,
624
SocketServer.TCPServer, object):
516
625
"""IPv6 TCP server. Accepts 'None' as address and/or port.
518
627
settings: Server settings
519
628
clients: Set() of Client objects
629
enabled: Boolean; whether this server is activated yet
521
631
address_family = socket.AF_INET6
522
632
def __init__(self, *args, **kwargs):
526
636
if "clients" in kwargs:
527
637
self.clients = kwargs["clients"]
528
638
del kwargs["clients"]
529
return super(type(self), self).__init__(*args, **kwargs)
640
super(IPv6_TCPServer, self).__init__(*args, **kwargs)
530
641
def server_bind(self):
531
642
"""This overrides the normal server_bind() function
532
643
to bind to an interface if one was specified, and also NOT to
563
674
# ["interface"]))
564
return super(type(self), self).server_bind()
675
return super(IPv6_TCPServer, self).server_bind()
676
def server_activate(self):
678
return super(IPv6_TCPServer, self).server_activate()
567
683
def string_to_delta(interval):
630
746
"""Call the C function if_nametoindex(), or equivalent"""
631
747
global if_nametoindex
633
if "ctypes.util" not in sys.modules:
635
749
if_nametoindex = ctypes.cdll.LoadLibrary\
636
750
(ctypes.util.find_library("c")).if_nametoindex
637
751
except (OSError, AttributeError):
642
756
def if_nametoindex(interface):
643
757
"Get an interface index the hard way, i.e. using fcntl()"
644
758
SIOCGIFINDEX = 0x8933 # From /usr/include/linux/sockios.h
646
ifreq = fcntl.ioctl(s, SIOCGIFINDEX,
647
struct.pack("16s16x", interface))
759
with closing(socket.socket()) as s:
760
ifreq = fcntl.ioctl(s, SIOCGIFINDEX,
761
struct.pack("16s16x", interface))
649
762
interface_index = struct.unpack("I", ifreq[16:20])[0]
650
763
return interface_index
651
764
return if_nametoindex(interface)
678
global main_loop_started
679
main_loop_started = False
681
791
parser = OptionParser(version = "%%prog %s" % version)
682
792
parser.add_option("-i", "--interface", type="string",
683
793
metavar="IF", help="Bind to interface IF")
698
808
default="/etc/mandos", metavar="DIR",
699
809
help="Directory to search for configuration"
701
(options, args) = parser.parse_args()
811
options = parser.parse_args()[0]
703
813
if options.check:
758
868
client_config.read(os.path.join(server_settings["configdir"],
872
tcp_server = IPv6_TCPServer((server_settings["address"],
873
server_settings["port"]),
875
settings=server_settings,
877
pidfilename = "/var/run/mandos.pid"
879
pidfile = open(pidfilename, "w")
880
except IOError, error:
881
logger.error("Could not open file %r", pidfilename)
886
uid = pwd.getpwnam("mandos").pw_uid
889
uid = pwd.getpwnam("nobody").pw_uid
893
gid = pwd.getpwnam("mandos").pw_gid
896
gid = pwd.getpwnam("nogroup").pw_gid
902
except OSError, error:
903
if error[0] != errno.EPERM:
762
907
service = AvahiService(name = server_settings["servicename"],
763
type = "_mandos._tcp", );
908
servicetype = "_mandos._tcp", )
764
909
if server_settings["interface"]:
765
910
service.interface = if_nametoindex\
766
911
(server_settings["interface"])
805
949
# Close all input and output, do double fork, etc.
808
pidfilename = "/var/run/mandos/mandos.pid"
811
pidfile = open(pidfilename, "w")
812
954
pidfile.write(str(pid) + "\n")
816
logger.error(u"Could not write %s file with PID %d",
817
pidfilename, os.getpid())
958
logger.error(u"Could not write to file %r with PID %d",
961
# "pidfile" was never created
820
966
"Cleanup function; run on exit"
840
986
for client in clients:
843
tcp_server = IPv6_TCPServer((server_settings["address"],
844
server_settings["port"]),
846
settings=server_settings,
990
tcp_server.server_activate()
848
992
# Find out what port we got
849
993
service.port = tcp_server.socket.getsockname()[1]
850
994
logger.info(u"Now listening on address %r, port %d, flowinfo %d,"