153
124
self.rename_count = 0
154
125
self.max_renames = max_renames
155
self.protocol = protocol
156
self.group = None # our entry group
159
self.entry_group_state_changed_match = None
160
126
def rename(self):
161
127
"""Derived from the Avahi example code"""
162
128
if self.rename_count >= self.max_renames:
163
logger.critical("No suitable Zeroconf service name found"
164
" after %i retries, exiting.",
129
logger.critical(u"No suitable Zeroconf service name found"
130
u" after %i retries, exiting.",
165
131
self.rename_count)
166
raise AvahiServiceError("Too many renames")
167
self.name = unicode(self.server
168
.GetAlternativeServiceName(self.name))
169
logger.info("Changing Zeroconf service name to %r ...",
132
raise AvahiServiceError(u"Too many renames")
133
self.name = server.GetAlternativeServiceName(self.name)
134
logger.info(u"Changing Zeroconf service name to %r ...",
171
136
syslogger.setFormatter(logging.Formatter
172
('Mandos (%s) [%%(process)d]:'
173
' %%(levelname)s: %%(message)s'
137
('Mandos (%s): %%(levelname)s:'
138
' %%(message)s' % self.name))
178
except dbus.exceptions.DBusException as error:
179
logger.critical("DBusException: %s", error)
182
141
self.rename_count += 1
183
142
def remove(self):
184
143
"""Derived from the Avahi example code"""
185
if self.entry_group_state_changed_match is not None:
186
self.entry_group_state_changed_match.remove()
187
self.entry_group_state_changed_match = None
188
if self.group is not None:
144
if group is not None:
191
147
"""Derived from the Avahi example code"""
193
if self.group is None:
194
self.group = dbus.Interface(
195
self.bus.get_object(avahi.DBUS_NAME,
196
self.server.EntryGroupNew()),
197
avahi.DBUS_INTERFACE_ENTRY_GROUP)
198
self.entry_group_state_changed_match = (
199
self.group.connect_to_signal(
200
'StateChanged', self .entry_group_state_changed))
201
logger.debug("Adding Zeroconf service '%s' of type '%s' ...",
202
self.name, self.type)
203
self.group.AddService(
206
dbus.UInt32(0), # flags
207
self.name, self.type,
208
self.domain, self.host,
209
dbus.UInt16(self.port),
210
avahi.string_array_to_txt_array(self.TXT))
212
def entry_group_state_changed(self, state, error):
213
"""Derived from the Avahi example code"""
214
logger.debug("Avahi entry group state change: %i", state)
216
if state == avahi.ENTRY_GROUP_ESTABLISHED:
217
logger.debug("Zeroconf service established.")
218
elif state == avahi.ENTRY_GROUP_COLLISION:
219
logger.info("Zeroconf service name collision.")
221
elif state == avahi.ENTRY_GROUP_FAILURE:
222
logger.critical("Avahi: Error in group state changed %s",
224
raise AvahiGroupError("State changed: %s"
227
"""Derived from the Avahi example code"""
228
if self.group is not None:
231
except (dbus.exceptions.UnknownMethodException,
232
dbus.exceptions.DBusException) as e:
236
def server_state_changed(self, state, error=None):
237
"""Derived from the Avahi example code"""
238
logger.debug("Avahi server state change: %i", state)
239
bad_states = { avahi.SERVER_INVALID:
240
"Zeroconf server invalid",
241
avahi.SERVER_REGISTERING: None,
242
avahi.SERVER_COLLISION:
243
"Zeroconf server name collision",
244
avahi.SERVER_FAILURE:
245
"Zeroconf server failure" }
246
if state in bad_states:
247
if bad_states[state] is not None:
249
logger.error(bad_states[state])
251
logger.error(bad_states[state] + ": %r", error)
253
elif state == avahi.SERVER_RUNNING:
257
logger.debug("Unknown state: %r", state)
259
logger.debug("Unknown state: %r: %r", state, error)
261
"""Derived from the Avahi example code"""
262
if self.server is None:
263
self.server = dbus.Interface(
264
self.bus.get_object(avahi.DBUS_NAME,
265
avahi.DBUS_PATH_SERVER,
266
follow_name_owner_changes=True),
267
avahi.DBUS_INTERFACE_SERVER)
268
self.server.connect_to_signal("StateChanged",
269
self.server_state_changed)
270
self.server_state_changed(self.server.GetState())
273
def _timedelta_to_milliseconds(td):
274
"Convert a datetime.timedelta() to milliseconds"
275
return ((td.days * 24 * 60 * 60 * 1000)
276
+ (td.seconds * 1000)
277
+ (td.microseconds // 1000))
279
class Client(object):
150
group = dbus.Interface(bus.get_object
152
server.EntryGroupNew()),
153
avahi.DBUS_INTERFACE_ENTRY_GROUP)
154
group.connect_to_signal('StateChanged',
155
entry_group_state_changed)
156
logger.debug(u"Adding Zeroconf service '%s' of type '%s' ...",
157
service.name, service.type)
159
self.interface, # interface
160
avahi.PROTO_INET6, # protocol
161
dbus.UInt32(0), # flags
162
self.name, self.type,
163
self.domain, self.host,
164
dbus.UInt16(self.port),
165
avahi.string_array_to_txt_array(self.TXT))
168
# From the Avahi example code:
169
group = None # our entry group
170
# End of Avahi example code
173
def _datetime_to_dbus(dt, variant_level=0):
174
"""Convert a UTC datetime.datetime() to a D-Bus type."""
175
return dbus.String(dt.isoformat(), variant_level=variant_level)
178
class Client(dbus.service.Object):
280
179
"""A representation of a client host served by this server.
283
_approved: bool(); 'None' if not yet approved/disapproved
284
approval_delay: datetime.timedelta(); Time to wait for approval
285
approval_duration: datetime.timedelta(); Duration of one approval
181
name: string; from the config file, used in log messages
182
fingerprint: string (40 or 32 hexadecimal digits); used to
183
uniquely identify the client
184
secret: bytestring; sent verbatim (over TLS) to client
185
host: string; available for use by the checker command
186
created: datetime.datetime(); (UTC) object creation
187
last_enabled: datetime.datetime(); (UTC)
189
last_checked_ok: datetime.datetime(); (UTC) or None
190
timeout: datetime.timedelta(); How long from last_checked_ok
191
until this client is invalid
192
interval: datetime.timedelta(); How often to start a new checker
193
disable_hook: If set, called by disable() as disable_hook(self)
286
194
checker: subprocess.Popen(); a running checker process used
287
195
to see if the client lives.
288
196
'None' if no process is running.
289
checker_callback_tag: a gobject event source tag, or None
290
checker_command: string; External command which is run to check
291
if client lives. %() expansions are done at
197
checker_initiator_tag: a gobject event source tag, or None
198
disable_initiator_tag: - '' -
199
checker_callback_tag: - '' -
200
checker_command: string; External command which is run to check if
201
client lives. %() expansions are done at
292
202
runtime with vars(self) as dict, so that for
293
203
instance %(name)s can be used in the command.
294
checker_initiator_tag: a gobject event source tag, or None
295
created: datetime.datetime(); (UTC) object creation
296
client_structure: Object describing what attributes a client has
297
and is used for storing the client at exit
298
current_checker_command: string; current running checker_command
299
disable_initiator_tag: a gobject event source tag, or None
301
fingerprint: string (40 or 32 hexadecimal digits); used to
302
uniquely identify the client
303
host: string; available for use by the checker command
304
interval: datetime.timedelta(); How often to start a new checker
305
last_approval_request: datetime.datetime(); (UTC) or None
306
last_checked_ok: datetime.datetime(); (UTC) or None
307
Last_checker_status: integer between 0 and 255 reflecting exit status
308
of last checker. -1 reflect crashed checker.
309
last_enabled: datetime.datetime(); (UTC)
310
name: string; from the config file, used in log messages and
312
secret: bytestring; sent verbatim (over TLS) to client
313
timeout: datetime.timedelta(); How long from last_checked_ok
314
until this client is disabled
315
extended_timeout: extra long timeout when password has been sent
316
runtime_expansions: Allowed attributes for runtime expansion.
317
expires: datetime.datetime(); time (UTC) when a client will be
204
use_dbus: bool(); Whether to provide D-Bus interface and signals
205
dbus_object_path: dbus.ObjectPath ; only set if self.use_dbus
321
runtime_expansions = ("approval_delay", "approval_duration",
322
"created", "enabled", "fingerprint",
323
"host", "interval", "last_checked_ok",
324
"last_enabled", "name", "timeout")
326
207
def timeout_milliseconds(self):
327
208
"Return the 'timeout' attribute in milliseconds"
328
return _timedelta_to_milliseconds(self.timeout)
330
def extended_timeout_milliseconds(self):
331
"Return the 'extended_timeout' attribute in milliseconds"
332
return _timedelta_to_milliseconds(self.extended_timeout)
209
return ((self.timeout.days * 24 * 60 * 60 * 1000)
210
+ (self.timeout.seconds * 1000)
211
+ (self.timeout.microseconds // 1000))
334
213
def interval_milliseconds(self):
335
214
"Return the 'interval' attribute in milliseconds"
336
return _timedelta_to_milliseconds(self.interval)
338
def approval_delay_milliseconds(self):
339
return _timedelta_to_milliseconds(self.approval_delay)
341
def __init__(self, name = None, config=None):
215
return ((self.interval.days * 24 * 60 * 60 * 1000)
216
+ (self.interval.seconds * 1000)
217
+ (self.interval.microseconds // 1000))
219
def __init__(self, name = None, disable_hook=None, config=None,
342
221
"""Note: the 'checker' key in 'config' sets the
343
222
'checker_command' attribute and *not* the 'checker'
346
225
if config is None:
348
logger.debug("Creating client %r", self.name)
227
logger.debug(u"Creating client %r", self.name)
228
self.use_dbus = use_dbus
230
self.dbus_object_path = (dbus.ObjectPath
232
+ self.name.replace(".", "_")))
233
dbus.service.Object.__init__(self, bus,
234
self.dbus_object_path)
349
235
# Uppercase and remove spaces from fingerprint for later
350
236
# comparison purposes with return value from the fingerprint()
352
238
self.fingerprint = (config["fingerprint"].upper()
354
logger.debug(" Fingerprint: %s", self.fingerprint)
240
logger.debug(u" Fingerprint: %s", self.fingerprint)
355
241
if "secret" in config:
356
self.secret = config["secret"].decode("base64")
242
self.secret = config["secret"].decode(u"base64")
357
243
elif "secfile" in config:
358
with open(os.path.expanduser(os.path.expandvars
359
(config["secfile"])),
244
with closing(open(os.path.expanduser
246
(config["secfile"])))) as secfile:
361
247
self.secret = secfile.read()
363
raise TypeError("No secret or secfile for client %s"
249
raise TypeError(u"No secret or secfile for client %s"
365
251
self.host = config.get("host", "")
366
252
self.created = datetime.datetime.utcnow()
368
self.last_approval_request = None
369
self.last_enabled = datetime.datetime.utcnow()
254
self.last_enabled = None
370
255
self.last_checked_ok = None
371
self.last_checker_status = 0
372
256
self.timeout = string_to_delta(config["timeout"])
373
self.extended_timeout = string_to_delta(config
374
["extended_timeout"])
375
257
self.interval = string_to_delta(config["interval"])
258
self.disable_hook = disable_hook
376
259
self.checker = None
377
260
self.checker_initiator_tag = None
378
261
self.disable_initiator_tag = None
379
self.expires = datetime.datetime.utcnow() + self.timeout
380
262
self.checker_callback_tag = None
381
263
self.checker_command = config["checker"]
382
self.current_checker_command = None
383
self._approved = None
384
self.approved_by_default = config.get("approved_by_default",
386
self.approvals_pending = 0
387
self.approval_delay = string_to_delta(
388
config["approval_delay"])
389
self.approval_duration = string_to_delta(
390
config["approval_duration"])
391
self.changedstate = (multiprocessing_manager
392
.Condition(multiprocessing_manager
394
self.client_structure = [attr for attr in self.__dict__.iterkeys() if not attr.startswith("_")]
395
self.client_structure.append("client_structure")
398
for name, t in inspect.getmembers(type(self),
399
lambda obj: isinstance(obj, property)):
400
if not name.startswith("_"):
401
self.client_structure.append(name)
404
def send_changedstate(self):
405
self.changedstate.acquire()
406
self.changedstate.notify_all()
407
self.changedstate.release()
409
265
def enable(self):
410
266
"""Start this client's checker and timeout hooks"""
411
if getattr(self, "enabled", False):
414
self.send_changedstate()
415
self.expires = datetime.datetime.utcnow() + self.timeout
267
self.last_enabled = datetime.datetime.utcnow()
268
# Schedule a new checker to be started an 'interval' from now,
269
# and every interval from then on.
270
self.checker_initiator_tag = (gobject.timeout_add
271
(self.interval_milliseconds(),
273
# Also start a new checker *right now*.
275
# Schedule a disable() when 'timeout' has passed
276
self.disable_initiator_tag = (gobject.timeout_add
277
(self.timeout_milliseconds(),
416
279
self.enabled = True
417
self.last_enabled = datetime.datetime.utcnow()
282
self.PropertyChanged(dbus.String(u"enabled"),
283
dbus.Boolean(True, variant_level=1))
284
self.PropertyChanged(dbus.String(u"last_enabled"),
285
(_datetime_to_dbus(self.last_enabled,
420
def disable(self, quiet=True):
421
289
"""Disable this client."""
422
290
if not getattr(self, "enabled", False):
425
self.send_changedstate()
427
logger.info("Disabling client %s", self.name)
292
logger.info(u"Disabling client %s", self.name)
428
293
if getattr(self, "disable_initiator_tag", False):
429
294
gobject.source_remove(self.disable_initiator_tag)
430
295
self.disable_initiator_tag = None
432
296
if getattr(self, "checker_initiator_tag", False):
433
297
gobject.source_remove(self.checker_initiator_tag)
434
298
self.checker_initiator_tag = None
435
299
self.stop_checker()
300
if self.disable_hook:
301
self.disable_hook(self)
436
302
self.enabled = False
305
self.PropertyChanged(dbus.String(u"enabled"),
306
dbus.Boolean(False, variant_level=1))
437
307
# Do not run this again if called by a gobject.timeout_add
440
310
def __del__(self):
311
self.disable_hook = None
443
def init_checker(self):
444
# Schedule a new checker to be started an 'interval' from now,
445
# and every interval from then on.
446
self.checker_initiator_tag = (gobject.timeout_add
447
(self.interval_milliseconds(),
449
# Schedule a disable() when 'timeout' has passed
450
self.disable_initiator_tag = (gobject.timeout_add
451
(self.timeout_milliseconds(),
453
# Also start a new checker *right now*.
457
314
def checker_callback(self, pid, condition, command):
458
315
"""The checker has completed, so take appropriate actions."""
459
316
self.checker_callback_tag = None
460
317
self.checker = None
461
if os.WIFEXITED(condition):
462
self.last_checker_status = os.WEXITSTATUS(condition)
463
if self.last_checker_status == 0:
464
logger.info("Checker for %(name)s succeeded",
468
logger.info("Checker for %(name)s failed",
471
self.last_checker_status = -1
472
logger.warning("Checker for %(name)s crashed?",
320
self.PropertyChanged(dbus.String(u"checker_running"),
321
dbus.Boolean(False, variant_level=1))
322
if (os.WIFEXITED(condition)
323
and (os.WEXITSTATUS(condition) == 0)):
324
logger.info(u"Checker for %(name)s succeeded",
328
self.CheckerCompleted(dbus.Boolean(True),
329
dbus.UInt16(condition),
330
dbus.String(command))
332
elif not os.WIFEXITED(condition):
333
logger.warning(u"Checker for %(name)s crashed?",
337
self.CheckerCompleted(dbus.Boolean(False),
338
dbus.UInt16(condition),
339
dbus.String(command))
341
logger.info(u"Checker for %(name)s failed",
345
self.CheckerCompleted(dbus.Boolean(False),
346
dbus.UInt16(condition),
347
dbus.String(command))
475
def checked_ok(self, timeout=None):
349
def bump_timeout(self):
476
350
"""Bump up the timeout for this client.
478
351
This should only be called when the client has been seen,
482
timeout = self.timeout
483
354
self.last_checked_ok = datetime.datetime.utcnow()
484
if self.disable_initiator_tag is not None:
485
gobject.source_remove(self.disable_initiator_tag)
486
if getattr(self, "enabled", False):
487
self.disable_initiator_tag = (gobject.timeout_add
488
(_timedelta_to_milliseconds
489
(timeout), self.disable))
490
self.expires = datetime.datetime.utcnow() + timeout
492
def need_approval(self):
493
self.last_approval_request = datetime.datetime.utcnow()
355
gobject.source_remove(self.disable_initiator_tag)
356
self.disable_initiator_tag = (gobject.timeout_add
357
(self.timeout_milliseconds(),
361
self.PropertyChanged(
362
dbus.String(u"last_checked_ok"),
363
(_datetime_to_dbus(self.last_checked_ok,
495
366
def start_checker(self):
496
367
"""Start a new checker subprocess if one is not running.
498
368
If a checker already exists, leave it running and do
500
370
# The reason for not killing a running checker is that if we
574
423
self.checker_callback_tag = None
575
424
if getattr(self, "checker", None) is None:
577
logger.debug("Stopping checker for %(name)s", vars(self))
426
logger.debug(u"Stopping checker for %(name)s", vars(self))
579
428
os.kill(self.checker.pid, signal.SIGTERM)
581
430
#if self.checker.poll() is None:
582
431
# os.kill(self.checker.pid, signal.SIGKILL)
583
except OSError as error:
432
except OSError, error:
584
433
if error.errno != errno.ESRCH: # No such process
586
435
self.checker = None
588
# Encrypts a client secret and stores it in a varible encrypted_secret
589
def encrypt_secret(self, key):
590
# Encryption-key need to be specific size, so we hash inputed key
591
hasheng = hashlib.sha256()
593
encryptionkey = hasheng.digest()
595
# Create validation hash so we know at decryption if it was sucessful
596
hasheng = hashlib.sha256()
597
hasheng.update(self.secret)
598
validationhash = hasheng.digest()
601
iv = os.urandom(Crypto.Cipher.AES.block_size)
602
ciphereng = Crypto.Cipher.AES.new(encryptionkey,
603
Crypto.Cipher.AES.MODE_CFB, iv)
604
ciphertext = ciphereng.encrypt(validationhash+self.secret)
605
self.encrypted_secret = (ciphertext, iv)
607
# Decrypt a encrypted client secret
608
def decrypt_secret(self, key):
609
# Decryption-key need to be specific size, so we hash inputed key
610
hasheng = hashlib.sha256()
612
encryptionkey = hasheng.digest()
614
# Decrypt encrypted secret
615
ciphertext, iv = self.encrypted_secret
616
ciphereng = Crypto.Cipher.AES.new(encryptionkey,
617
Crypto.Cipher.AES.MODE_CFB, iv)
618
plain = ciphereng.decrypt(ciphertext)
620
# Validate decrypted secret to know if it was succesful
621
hasheng = hashlib.sha256()
622
validationhash = plain[:hasheng.digest_size]
623
secret = plain[hasheng.digest_size:]
624
hasheng.update(secret)
626
# if validation fails, we use key as new secret. Otherwhise, we use
627
# the decrypted secret
628
if hasheng.digest() == validationhash:
632
del self.encrypted_secret
635
def dbus_service_property(dbus_interface, signature="v",
636
access="readwrite", byte_arrays=False):
637
"""Decorators for marking methods of a DBusObjectWithProperties to
638
become properties on the D-Bus.
640
The decorated method will be called with no arguments by "Get"
641
and with one argument by "Set".
643
The parameters, where they are supported, are the same as
644
dbus.service.method, except there is only "signature", since the
645
type from Get() and the type sent to Set() is the same.
647
# Encoding deeply encoded byte arrays is not supported yet by the
648
# "Set" method, so we fail early here:
649
if byte_arrays and signature != "ay":
650
raise ValueError("Byte arrays not supported for non-'ay'"
651
" signature %r" % signature)
653
func._dbus_is_property = True
654
func._dbus_interface = dbus_interface
655
func._dbus_signature = signature
656
func._dbus_access = access
657
func._dbus_name = func.__name__
658
if func._dbus_name.endswith("_dbus_property"):
659
func._dbus_name = func._dbus_name[:-14]
660
func._dbus_get_args_options = {'byte_arrays': byte_arrays }
665
class DBusPropertyException(dbus.exceptions.DBusException):
666
"""A base class for D-Bus property-related exceptions
668
def __unicode__(self):
669
return unicode(str(self))
672
class DBusPropertyAccessException(DBusPropertyException):
673
"""A property's access permissions disallows an operation.
678
class DBusPropertyNotFound(DBusPropertyException):
679
"""An attempt was made to access a non-existing property.
684
class DBusObjectWithProperties(dbus.service.Object):
685
"""A D-Bus object with properties.
687
Classes inheriting from this can use the dbus_service_property
688
decorator to expose methods as D-Bus properties. It exposes the
689
standard Get(), Set(), and GetAll() methods on the D-Bus.
693
def _is_dbus_property(obj):
694
return getattr(obj, "_dbus_is_property", False)
696
def _get_all_dbus_properties(self):
697
"""Returns a generator of (name, attribute) pairs
699
return ((prop.__get__(self)._dbus_name, prop.__get__(self))
700
for cls in self.__class__.__mro__
702
inspect.getmembers(cls, self._is_dbus_property))
704
def _get_dbus_property(self, interface_name, property_name):
705
"""Returns a bound method if one exists which is a D-Bus
706
property with the specified name and interface.
708
for cls in self.__class__.__mro__:
709
for name, value in (inspect.getmembers
710
(cls, self._is_dbus_property)):
711
if (value._dbus_name == property_name
712
and value._dbus_interface == interface_name):
713
return value.__get__(self)
716
raise DBusPropertyNotFound(self.dbus_object_path + ":"
717
+ interface_name + "."
720
@dbus.service.method(dbus.PROPERTIES_IFACE, in_signature="ss",
722
def Get(self, interface_name, property_name):
723
"""Standard D-Bus property Get() method, see D-Bus standard.
725
prop = self._get_dbus_property(interface_name, property_name)
726
if prop._dbus_access == "write":
727
raise DBusPropertyAccessException(property_name)
729
if not hasattr(value, "variant_level"):
731
return type(value)(value, variant_level=value.variant_level+1)
733
@dbus.service.method(dbus.PROPERTIES_IFACE, in_signature="ssv")
734
def Set(self, interface_name, property_name, value):
735
"""Standard D-Bus property Set() method, see D-Bus standard.
737
prop = self._get_dbus_property(interface_name, property_name)
738
if prop._dbus_access == "read":
739
raise DBusPropertyAccessException(property_name)
740
if prop._dbus_get_args_options["byte_arrays"]:
741
# The byte_arrays option is not supported yet on
742
# signatures other than "ay".
743
if prop._dbus_signature != "ay":
745
value = dbus.ByteArray(''.join(unichr(byte)
749
@dbus.service.method(dbus.PROPERTIES_IFACE, in_signature="s",
750
out_signature="a{sv}")
751
def GetAll(self, interface_name):
752
"""Standard D-Bus property GetAll() method, see D-Bus
755
Note: Will not include properties with access="write".
758
for name, prop in self._get_all_dbus_properties():
760
and interface_name != prop._dbus_interface):
761
# Interface non-empty but did not match
763
# Ignore write-only properties
764
if prop._dbus_access == "write":
767
if not hasattr(value, "variant_level"):
770
all[name] = type(value)(value, variant_level=
771
value.variant_level+1)
772
return dbus.Dictionary(all, signature="sv")
774
@dbus.service.method(dbus.INTROSPECTABLE_IFACE,
776
path_keyword='object_path',
777
connection_keyword='connection')
778
def Introspect(self, object_path, connection):
779
"""Standard D-Bus method, overloaded to insert property tags.
781
xmlstring = dbus.service.Object.Introspect(self, object_path,
784
document = xml.dom.minidom.parseString(xmlstring)
785
def make_tag(document, name, prop):
786
e = document.createElement("property")
787
e.setAttribute("name", name)
788
e.setAttribute("type", prop._dbus_signature)
789
e.setAttribute("access", prop._dbus_access)
791
for if_tag in document.getElementsByTagName("interface"):
792
for tag in (make_tag(document, name, prop)
794
in self._get_all_dbus_properties()
795
if prop._dbus_interface
796
== if_tag.getAttribute("name")):
797
if_tag.appendChild(tag)
798
# Add the names to the return values for the
799
# "org.freedesktop.DBus.Properties" methods
800
if (if_tag.getAttribute("name")
801
== "org.freedesktop.DBus.Properties"):
802
for cn in if_tag.getElementsByTagName("method"):
803
if cn.getAttribute("name") == "Get":
804
for arg in cn.getElementsByTagName("arg"):
805
if (arg.getAttribute("direction")
807
arg.setAttribute("name", "value")
808
elif cn.getAttribute("name") == "GetAll":
809
for arg in cn.getElementsByTagName("arg"):
810
if (arg.getAttribute("direction")
812
arg.setAttribute("name", "props")
813
xmlstring = document.toxml("utf-8")
815
except (AttributeError, xml.dom.DOMException,
816
xml.parsers.expat.ExpatError) as error:
817
logger.error("Failed to override Introspection method",
822
def datetime_to_dbus (dt, variant_level=0):
823
"""Convert a UTC datetime.datetime() to a D-Bus type."""
825
return dbus.String("", variant_level = variant_level)
826
return dbus.String(dt.isoformat(),
827
variant_level=variant_level)
829
class AlternateDBusNamesMetaclass(DBusObjectWithProperties
831
"""Applied to an empty subclass of a D-Bus object, this metaclass
832
will add additional D-Bus attributes matching a certain pattern.
834
def __new__(mcs, name, bases, attr):
835
# Go through all the base classes which could have D-Bus
836
# methods, signals, or properties in them
837
for base in (b for b in bases
838
if issubclass(b, dbus.service.Object)):
839
# Go though all attributes of the base class
840
for attrname, attribute in inspect.getmembers(base):
841
# Ignore non-D-Bus attributes, and D-Bus attributes
842
# with the wrong interface name
843
if (not hasattr(attribute, "_dbus_interface")
844
or not attribute._dbus_interface
845
.startswith("se.recompile.Mandos")):
847
# Create an alternate D-Bus interface name based on
849
alt_interface = (attribute._dbus_interface
850
.replace("se.recompile.Mandos",
851
"se.bsnet.fukt.Mandos"))
852
# Is this a D-Bus signal?
853
if getattr(attribute, "_dbus_is_signal", False):
854
# Extract the original non-method function by
856
nonmethod_func = (dict(
857
zip(attribute.func_code.co_freevars,
858
attribute.__closure__))["func"]
860
# Create a new, but exactly alike, function
861
# object, and decorate it to be a new D-Bus signal
862
# with the alternate D-Bus interface name
863
new_function = (dbus.service.signal
865
attribute._dbus_signature)
867
nonmethod_func.func_code,
868
nonmethod_func.func_globals,
869
nonmethod_func.func_name,
870
nonmethod_func.func_defaults,
871
nonmethod_func.func_closure)))
872
# Define a creator of a function to call both the
873
# old and new functions, so both the old and new
874
# signals gets sent when the function is called
875
def fixscope(func1, func2):
876
"""This function is a scope container to pass
877
func1 and func2 to the "call_both" function
878
outside of its arguments"""
879
def call_both(*args, **kwargs):
880
"""This function will emit two D-Bus
881
signals by calling func1 and func2"""
882
func1(*args, **kwargs)
883
func2(*args, **kwargs)
885
# Create the "call_both" function and add it to
887
attr[attrname] = fixscope(attribute,
889
# Is this a D-Bus method?
890
elif getattr(attribute, "_dbus_is_method", False):
891
# Create a new, but exactly alike, function
892
# object. Decorate it to be a new D-Bus method
893
# with the alternate D-Bus interface name. Add it
895
attr[attrname] = (dbus.service.method
897
attribute._dbus_in_signature,
898
attribute._dbus_out_signature)
900
(attribute.func_code,
901
attribute.func_globals,
903
attribute.func_defaults,
904
attribute.func_closure)))
905
# Is this a D-Bus property?
906
elif getattr(attribute, "_dbus_is_property", False):
907
# Create a new, but exactly alike, function
908
# object, and decorate it to be a new D-Bus
909
# property with the alternate D-Bus interface
910
# name. Add it to the class.
911
attr[attrname] = (dbus_service_property
913
attribute._dbus_signature,
914
attribute._dbus_access,
916
._dbus_get_args_options
919
(attribute.func_code,
920
attribute.func_globals,
922
attribute.func_defaults,
923
attribute.func_closure)))
924
return type.__new__(mcs, name, bases, attr)
926
class ClientDBus(Client, DBusObjectWithProperties):
927
"""A Client class using D-Bus
930
dbus_object_path: dbus.ObjectPath
931
bus: dbus.SystemBus()
934
runtime_expansions = (Client.runtime_expansions
935
+ ("dbus_object_path",))
937
# dbus.service.Object doesn't use super(), so we can't either.
939
def __init__(self, bus = None, *args, **kwargs):
941
Client.__init__(self, *args, **kwargs)
943
self._approvals_pending = 0
944
# Only now, when this client is initialized, can it show up on
946
client_object_name = unicode(self.name).translate(
949
self.dbus_object_path = (dbus.ObjectPath
950
("/clients/" + client_object_name))
951
DBusObjectWithProperties.__init__(self, self.bus,
952
self.dbus_object_path)
954
def notifychangeproperty(transform_func,
955
dbus_name, type_func=lambda x: x,
957
""" Modify a variable so that it's a property which announces
960
transform_fun: Function that takes a value and a variant_level
961
and transforms it to a D-Bus type.
962
dbus_name: D-Bus name of the variable
963
type_func: Function that transform the value before sending it
964
to the D-Bus. Default: no transform
965
variant_level: D-Bus variant level. Default: 1
967
attrname = "_{0}".format(dbus_name)
968
def setter(self, value):
969
if hasattr(self, "dbus_object_path"):
970
if (not hasattr(self, attrname) or
971
type_func(getattr(self, attrname, None))
972
!= type_func(value)):
973
dbus_value = transform_func(type_func(value),
976
self.PropertyChanged(dbus.String(dbus_name),
978
setattr(self, attrname, value)
980
return property(lambda self: getattr(self, attrname), setter)
983
expires = notifychangeproperty(datetime_to_dbus, "Expires")
984
approvals_pending = notifychangeproperty(dbus.Boolean,
987
enabled = notifychangeproperty(dbus.Boolean, "Enabled")
988
last_enabled = notifychangeproperty(datetime_to_dbus,
990
checker = notifychangeproperty(dbus.Boolean, "CheckerRunning",
991
type_func = lambda checker:
993
last_checked_ok = notifychangeproperty(datetime_to_dbus,
995
last_approval_request = notifychangeproperty(
996
datetime_to_dbus, "LastApprovalRequest")
997
approved_by_default = notifychangeproperty(dbus.Boolean,
999
approval_delay = notifychangeproperty(dbus.UInt16,
1002
_timedelta_to_milliseconds)
1003
approval_duration = notifychangeproperty(
1004
dbus.UInt16, "ApprovalDuration",
1005
type_func = _timedelta_to_milliseconds)
1006
host = notifychangeproperty(dbus.String, "Host")
1007
timeout = notifychangeproperty(dbus.UInt16, "Timeout",
1009
_timedelta_to_milliseconds)
1010
extended_timeout = notifychangeproperty(
1011
dbus.UInt16, "ExtendedTimeout",
1012
type_func = _timedelta_to_milliseconds)
1013
interval = notifychangeproperty(dbus.UInt16,
1016
_timedelta_to_milliseconds)
1017
checker_command = notifychangeproperty(dbus.String, "Checker")
1019
del notifychangeproperty
1021
def __del__(self, *args, **kwargs):
1023
self.remove_from_connection()
1026
if hasattr(DBusObjectWithProperties, "__del__"):
1027
DBusObjectWithProperties.__del__(self, *args, **kwargs)
1028
Client.__del__(self, *args, **kwargs)
1030
def checker_callback(self, pid, condition, command,
1032
self.checker_callback_tag = None
1034
if os.WIFEXITED(condition):
1035
exitstatus = os.WEXITSTATUS(condition)
1037
self.CheckerCompleted(dbus.Int16(exitstatus),
1038
dbus.Int64(condition),
1039
dbus.String(command))
1042
self.CheckerCompleted(dbus.Int16(-1),
1043
dbus.Int64(condition),
1044
dbus.String(command))
1046
return Client.checker_callback(self, pid, condition, command,
1049
def start_checker(self, *args, **kwargs):
1050
old_checker = self.checker
1051
if self.checker is not None:
1052
old_checker_pid = self.checker.pid
1054
old_checker_pid = None
1055
r = Client.start_checker(self, *args, **kwargs)
1056
# Only if new checker process was started
1057
if (self.checker is not None
1058
and old_checker_pid != self.checker.pid):
1060
self.CheckerStarted(self.current_checker_command)
1063
def _reset_approved(self):
1064
self._approved = None
1067
def approve(self, value=True):
1068
self.send_changedstate()
1069
self._approved = value
1070
gobject.timeout_add(_timedelta_to_milliseconds
1071
(self.approval_duration),
1072
self._reset_approved)
1075
## D-Bus methods, signals & properties
1076
_interface = "se.recompile.Mandos.Client"
437
self.PropertyChanged(dbus.String(u"checker_running"),
438
dbus.Boolean(False, variant_level=1))
440
def still_valid(self):
441
"""Has the timeout not yet passed for this client?"""
442
if not getattr(self, "enabled", False):
444
now = datetime.datetime.utcnow()
445
if self.last_checked_ok is None:
446
return now < (self.created + self.timeout)
448
return now < (self.last_checked_ok + self.timeout)
450
## D-Bus methods & signals
451
_interface = u"org.mandos_system.Mandos.Client"
453
# BumpTimeout - method
454
BumpTimeout = dbus.service.method(_interface)(bump_timeout)
455
BumpTimeout.__name__ = "BumpTimeout"
1080
457
# CheckerCompleted - signal
1081
@dbus.service.signal(_interface, signature="nxs")
1082
def CheckerCompleted(self, exitcode, waitstatus, command):
458
@dbus.service.signal(_interface, signature="bqs")
459
def CheckerCompleted(self, success, condition, command):
1149
579
# StopChecker - method
1150
@dbus.service.method(_interface)
1151
def StopChecker(self):
1156
# ApprovalPending - property
1157
@dbus_service_property(_interface, signature="b", access="read")
1158
def ApprovalPending_dbus_property(self):
1159
return dbus.Boolean(bool(self.approvals_pending))
1161
# ApprovedByDefault - property
1162
@dbus_service_property(_interface, signature="b",
1164
def ApprovedByDefault_dbus_property(self, value=None):
1165
if value is None: # get
1166
return dbus.Boolean(self.approved_by_default)
1167
self.approved_by_default = bool(value)
1169
# ApprovalDelay - property
1170
@dbus_service_property(_interface, signature="t",
1172
def ApprovalDelay_dbus_property(self, value=None):
1173
if value is None: # get
1174
return dbus.UInt64(self.approval_delay_milliseconds())
1175
self.approval_delay = datetime.timedelta(0, 0, 0, value)
1177
# ApprovalDuration - property
1178
@dbus_service_property(_interface, signature="t",
1180
def ApprovalDuration_dbus_property(self, value=None):
1181
if value is None: # get
1182
return dbus.UInt64(_timedelta_to_milliseconds(
1183
self.approval_duration))
1184
self.approval_duration = datetime.timedelta(0, 0, 0, value)
1187
@dbus_service_property(_interface, signature="s", access="read")
1188
def Name_dbus_property(self):
1189
return dbus.String(self.name)
1191
# Fingerprint - property
1192
@dbus_service_property(_interface, signature="s", access="read")
1193
def Fingerprint_dbus_property(self):
1194
return dbus.String(self.fingerprint)
1197
@dbus_service_property(_interface, signature="s",
1199
def Host_dbus_property(self, value=None):
1200
if value is None: # get
1201
return dbus.String(self.host)
1204
# Created - property
1205
@dbus_service_property(_interface, signature="s", access="read")
1206
def Created_dbus_property(self):
1207
return dbus.String(datetime_to_dbus(self.created))
1209
# LastEnabled - property
1210
@dbus_service_property(_interface, signature="s", access="read")
1211
def LastEnabled_dbus_property(self):
1212
return datetime_to_dbus(self.last_enabled)
1214
# Enabled - property
1215
@dbus_service_property(_interface, signature="b",
1217
def Enabled_dbus_property(self, value=None):
1218
if value is None: # get
1219
return dbus.Boolean(self.enabled)
1225
# LastCheckedOK - property
1226
@dbus_service_property(_interface, signature="s",
1228
def LastCheckedOK_dbus_property(self, value=None):
1229
if value is not None:
1232
return datetime_to_dbus(self.last_checked_ok)
1234
# Expires - property
1235
@dbus_service_property(_interface, signature="s", access="read")
1236
def Expires_dbus_property(self):
1237
return datetime_to_dbus(self.expires)
1239
# LastApprovalRequest - property
1240
@dbus_service_property(_interface, signature="s", access="read")
1241
def LastApprovalRequest_dbus_property(self):
1242
return datetime_to_dbus(self.last_approval_request)
1244
# Timeout - property
1245
@dbus_service_property(_interface, signature="t",
1247
def Timeout_dbus_property(self, value=None):
1248
if value is None: # get
1249
return dbus.UInt64(self.timeout_milliseconds())
1250
self.timeout = datetime.timedelta(0, 0, 0, value)
1251
if getattr(self, "disable_initiator_tag", None) is None:
1253
# Reschedule timeout
1254
gobject.source_remove(self.disable_initiator_tag)
1255
self.disable_initiator_tag = None
1257
time_to_die = _timedelta_to_milliseconds((self
1262
if time_to_die <= 0:
1263
# The timeout has passed
1266
self.expires = (datetime.datetime.utcnow()
1267
+ datetime.timedelta(milliseconds =
1269
self.disable_initiator_tag = (gobject.timeout_add
1270
(time_to_die, self.disable))
1272
# ExtendedTimeout - property
1273
@dbus_service_property(_interface, signature="t",
1275
def ExtendedTimeout_dbus_property(self, value=None):
1276
if value is None: # get
1277
return dbus.UInt64(self.extended_timeout_milliseconds())
1278
self.extended_timeout = datetime.timedelta(0, 0, 0, value)
1280
# Interval - property
1281
@dbus_service_property(_interface, signature="t",
1283
def Interval_dbus_property(self, value=None):
1284
if value is None: # get
1285
return dbus.UInt64(self.interval_milliseconds())
1286
self.interval = datetime.timedelta(0, 0, 0, value)
1287
if getattr(self, "checker_initiator_tag", None) is None:
1289
# Reschedule checker run
1290
gobject.source_remove(self.checker_initiator_tag)
1291
self.checker_initiator_tag = (gobject.timeout_add
1292
(value, self.start_checker))
1293
self.start_checker() # Start one now, too
1295
# Checker - property
1296
@dbus_service_property(_interface, signature="s",
1298
def Checker_dbus_property(self, value=None):
1299
if value is None: # get
1300
return dbus.String(self.checker_command)
1301
self.checker_command = value
1303
# CheckerRunning - property
1304
@dbus_service_property(_interface, signature="b",
1306
def CheckerRunning_dbus_property(self, value=None):
1307
if value is None: # get
1308
return dbus.Boolean(self.checker is not None)
1310
self.start_checker()
1314
# ObjectPath - property
1315
@dbus_service_property(_interface, signature="o", access="read")
1316
def ObjectPath_dbus_property(self):
1317
return self.dbus_object_path # is already a dbus.ObjectPath
1320
@dbus_service_property(_interface, signature="ay",
1321
access="write", byte_arrays=True)
1322
def Secret_dbus_property(self, value):
1323
self.secret = str(value)
580
StopChecker = dbus.service.method(_interface)(stop_checker)
581
StopChecker.__name__ = "StopChecker"
1328
class ProxyClient(object):
1329
def __init__(self, child_pipe, fpr, address):
1330
self._pipe = child_pipe
1331
self._pipe.send(('init', fpr, address))
1332
if not self._pipe.recv():
1335
def __getattribute__(self, name):
1336
if(name == '_pipe'):
1337
return super(ProxyClient, self).__getattribute__(name)
1338
self._pipe.send(('getattr', name))
1339
data = self._pipe.recv()
1340
if data[0] == 'data':
1342
if data[0] == 'function':
1343
def func(*args, **kwargs):
1344
self._pipe.send(('funcall', name, args, kwargs))
1345
return self._pipe.recv()[1]
1348
def __setattr__(self, name, value):
1349
if(name == '_pipe'):
1350
return super(ProxyClient, self).__setattr__(name, value)
1351
self._pipe.send(('setattr', name, value))
1353
class ClientDBusTransitional(ClientDBus):
1354
__metaclass__ = AlternateDBusNamesMetaclass
1356
class ClientHandler(socketserver.BaseRequestHandler, object):
1357
"""A class to handle client connections.
1359
Instantiated once for each connection to handle it.
586
def peer_certificate(session):
587
"Return the peer's OpenPGP certificate as a bytestring"
588
# If not an OpenPGP certificate...
589
if (gnutls.library.functions
590
.gnutls_certificate_type_get(session._c_object)
591
!= gnutls.library.constants.GNUTLS_CRT_OPENPGP):
592
# ...do the normal thing
593
return session.peer_certificate
594
list_size = ctypes.c_uint()
595
cert_list = (gnutls.library.functions
596
.gnutls_certificate_get_peers
597
(session._c_object, ctypes.byref(list_size)))
598
if list_size.value == 0:
601
return ctypes.string_at(cert.data, cert.size)
604
def fingerprint(openpgp):
605
"Convert an OpenPGP bytestring to a hexdigit fingerprint string"
606
# New GnuTLS "datum" with the OpenPGP public key
607
datum = (gnutls.library.types
608
.gnutls_datum_t(ctypes.cast(ctypes.c_char_p(openpgp),
611
ctypes.c_uint(len(openpgp))))
612
# New empty GnuTLS certificate
613
crt = gnutls.library.types.gnutls_openpgp_crt_t()
614
(gnutls.library.functions
615
.gnutls_openpgp_crt_init(ctypes.byref(crt)))
616
# Import the OpenPGP public key into the certificate
617
(gnutls.library.functions
618
.gnutls_openpgp_crt_import(crt, ctypes.byref(datum),
619
gnutls.library.constants
620
.GNUTLS_OPENPGP_FMT_RAW))
621
# Verify the self signature in the key
622
crtverify = ctypes.c_uint()
623
(gnutls.library.functions
624
.gnutls_openpgp_crt_verify_self(crt, 0, ctypes.byref(crtverify)))
625
if crtverify.value != 0:
626
gnutls.library.functions.gnutls_openpgp_crt_deinit(crt)
627
raise gnutls.errors.CertificateSecurityError("Verify failed")
628
# New buffer for the fingerprint
629
buf = ctypes.create_string_buffer(20)
630
buf_len = ctypes.c_size_t()
631
# Get the fingerprint from the certificate into the buffer
632
(gnutls.library.functions
633
.gnutls_openpgp_crt_get_fingerprint(crt, ctypes.byref(buf),
634
ctypes.byref(buf_len)))
635
# Deinit the certificate
636
gnutls.library.functions.gnutls_openpgp_crt_deinit(crt)
637
# Convert the buffer to a Python bytestring
638
fpr = ctypes.string_at(buf, buf_len.value)
639
# Convert the bytestring to hexadecimal notation
640
hex_fpr = u''.join(u"%02X" % ord(char) for char in fpr)
644
class TCP_handler(SocketServer.BaseRequestHandler, object):
645
"""A TCP request handler class.
646
Instantiated by IPv6_TCPServer for each request to handle it.
1360
647
Note: This will run in its own forked process."""
1362
649
def handle(self):
1363
with contextlib.closing(self.server.child_pipe) as child_pipe:
1364
logger.info("TCP connection from: %s",
1365
unicode(self.client_address))
1366
logger.debug("Pipe FD: %d",
1367
self.server.child_pipe.fileno())
1369
session = (gnutls.connection
1370
.ClientSession(self.request,
1372
.X509Credentials()))
1374
# Note: gnutls.connection.X509Credentials is really a
1375
# generic GnuTLS certificate credentials object so long as
1376
# no X.509 keys are added to it. Therefore, we can use it
1377
# here despite using OpenPGP certificates.
1379
#priority = ':'.join(("NONE", "+VERS-TLS1.1",
1380
# "+AES-256-CBC", "+SHA1",
1381
# "+COMP-NULL", "+CTYPE-OPENPGP",
1383
# Use a fallback default, since this MUST be set.
1384
priority = self.server.gnutls_priority
1385
if priority is None:
1387
(gnutls.library.functions
1388
.gnutls_priority_set_direct(session._c_object,
1391
# Start communication using the Mandos protocol
1392
# Get protocol number
1393
line = self.request.makefile().readline()
1394
logger.debug("Protocol version: %r", line)
1396
if int(line.strip().split()[0]) > 1:
1398
except (ValueError, IndexError, RuntimeError) as error:
1399
logger.error("Unknown protocol version: %s", error)
1402
# Start GnuTLS connection
1405
except gnutls.errors.GNUTLSError as error:
1406
logger.warning("Handshake failed: %s", error)
1407
# Do not run session.bye() here: the session is not
1408
# established. Just abandon the request.
1410
logger.debug("Handshake succeeded")
1412
approval_required = False
1415
fpr = self.fingerprint(self.peer_certificate
1418
gnutls.errors.GNUTLSError) as error:
1419
logger.warning("Bad certificate: %s", error)
1421
logger.debug("Fingerprint: %s", fpr)
1424
client = ProxyClient(child_pipe, fpr,
1425
self.client_address)
1429
if client.approval_delay:
1430
delay = client.approval_delay
1431
client.approvals_pending += 1
1432
approval_required = True
1435
if not client.enabled:
1436
logger.info("Client %s is disabled",
1438
if self.server.use_dbus:
1440
client.Rejected("Disabled")
1443
if client._approved or not client.approval_delay:
1444
#We are approved or approval is disabled
1446
elif client._approved is None:
1447
logger.info("Client %s needs approval",
1449
if self.server.use_dbus:
1451
client.NeedApproval(
1452
client.approval_delay_milliseconds(),
1453
client.approved_by_default)
1455
logger.warning("Client %s was not approved",
1457
if self.server.use_dbus:
1459
client.Rejected("Denied")
1462
#wait until timeout or approved
1463
time = datetime.datetime.now()
1464
client.changedstate.acquire()
1465
(client.changedstate.wait
1466
(float(client._timedelta_to_milliseconds(delay)
1468
client.changedstate.release()
1469
time2 = datetime.datetime.now()
1470
if (time2 - time) >= delay:
1471
if not client.approved_by_default:
1472
logger.warning("Client %s timed out while"
1473
" waiting for approval",
1475
if self.server.use_dbus:
1477
client.Rejected("Approval timed out")
1482
delay -= time2 - time
1485
while sent_size < len(client.secret):
1487
sent = session.send(client.secret[sent_size:])
1488
except gnutls.errors.GNUTLSError as error:
1489
logger.warning("gnutls send failed")
1491
logger.debug("Sent: %d, remaining: %d",
1492
sent, len(client.secret)
1493
- (sent_size + sent))
1496
logger.info("Sending secret to %s", client.name)
1497
# bump the timeout using extended_timeout
1498
client.checked_ok(client.extended_timeout)
1499
if self.server.use_dbus:
1504
if approval_required:
1505
client.approvals_pending -= 1
1508
except gnutls.errors.GNUTLSError as error:
1509
logger.warning("GnuTLS bye failed")
1512
def peer_certificate(session):
1513
"Return the peer's OpenPGP certificate as a bytestring"
1514
# If not an OpenPGP certificate...
1515
if (gnutls.library.functions
1516
.gnutls_certificate_type_get(session._c_object)
1517
!= gnutls.library.constants.GNUTLS_CRT_OPENPGP):
1518
# ...do the normal thing
1519
return session.peer_certificate
1520
list_size = ctypes.c_uint(1)
1521
cert_list = (gnutls.library.functions
1522
.gnutls_certificate_get_peers
1523
(session._c_object, ctypes.byref(list_size)))
1524
if not bool(cert_list) and list_size.value != 0:
1525
raise gnutls.errors.GNUTLSError("error getting peer"
1527
if list_size.value == 0:
1530
return ctypes.string_at(cert.data, cert.size)
1533
def fingerprint(openpgp):
1534
"Convert an OpenPGP bytestring to a hexdigit fingerprint"
1535
# New GnuTLS "datum" with the OpenPGP public key
1536
datum = (gnutls.library.types
1537
.gnutls_datum_t(ctypes.cast(ctypes.c_char_p(openpgp),
1540
ctypes.c_uint(len(openpgp))))
1541
# New empty GnuTLS certificate
1542
crt = gnutls.library.types.gnutls_openpgp_crt_t()
1543
(gnutls.library.functions
1544
.gnutls_openpgp_crt_init(ctypes.byref(crt)))
1545
# Import the OpenPGP public key into the certificate
1546
(gnutls.library.functions
1547
.gnutls_openpgp_crt_import(crt, ctypes.byref(datum),
1548
gnutls.library.constants
1549
.GNUTLS_OPENPGP_FMT_RAW))
1550
# Verify the self signature in the key
1551
crtverify = ctypes.c_uint()
1552
(gnutls.library.functions
1553
.gnutls_openpgp_crt_verify_self(crt, 0,
1554
ctypes.byref(crtverify)))
1555
if crtverify.value != 0:
1556
gnutls.library.functions.gnutls_openpgp_crt_deinit(crt)
1557
raise (gnutls.errors.CertificateSecurityError
1559
# New buffer for the fingerprint
1560
buf = ctypes.create_string_buffer(20)
1561
buf_len = ctypes.c_size_t()
1562
# Get the fingerprint from the certificate into the buffer
1563
(gnutls.library.functions
1564
.gnutls_openpgp_crt_get_fingerprint(crt, ctypes.byref(buf),
1565
ctypes.byref(buf_len)))
1566
# Deinit the certificate
1567
gnutls.library.functions.gnutls_openpgp_crt_deinit(crt)
1568
# Convert the buffer to a Python bytestring
1569
fpr = ctypes.string_at(buf, buf_len.value)
1570
# Convert the bytestring to hexadecimal notation
1571
hex_fpr = ''.join("%02X" % ord(char) for char in fpr)
1575
class MultiprocessingMixIn(object):
1576
"""Like socketserver.ThreadingMixIn, but with multiprocessing"""
1577
def sub_process_main(self, request, address):
1579
self.finish_request(request, address)
1581
self.handle_error(request, address)
1582
self.close_request(request)
1584
def process_request(self, request, address):
1585
"""Start a new process to process the request."""
1586
proc = multiprocessing.Process(target = self.sub_process_main,
1593
class MultiprocessingMixInWithPipe(MultiprocessingMixIn, object):
1594
""" adds a pipe to the MixIn """
1595
def process_request(self, request, client_address):
1596
"""Overrides and wraps the original process_request().
1598
This function creates a new pipe in self.pipe
1600
parent_pipe, self.child_pipe = multiprocessing.Pipe()
1602
proc = MultiprocessingMixIn.process_request(self, request,
1604
self.child_pipe.close()
1605
self.add_pipe(parent_pipe, proc)
1607
def add_pipe(self, parent_pipe, proc):
1608
"""Dummy function; override as necessary"""
1609
raise NotImplementedError
1612
class IPv6_TCPServer(MultiprocessingMixInWithPipe,
1613
socketserver.TCPServer, object):
1614
"""IPv6-capable TCP server. Accepts 'None' as address and/or port
650
logger.info(u"TCP connection from: %s",
651
unicode(self.client_address))
652
session = (gnutls.connection
653
.ClientSession(self.request,
657
line = self.request.makefile().readline()
658
logger.debug(u"Protocol version: %r", line)
660
if int(line.strip().split()[0]) > 1:
662
except (ValueError, IndexError, RuntimeError), error:
663
logger.error(u"Unknown protocol version: %s", error)
666
# Note: gnutls.connection.X509Credentials is really a generic
667
# GnuTLS certificate credentials object so long as no X.509
668
# keys are added to it. Therefore, we can use it here despite
669
# using OpenPGP certificates.
671
#priority = ':'.join(("NONE", "+VERS-TLS1.1", "+AES-256-CBC",
672
# "+SHA1", "+COMP-NULL", "+CTYPE-OPENPGP",
674
# Use a fallback default, since this MUST be set.
675
priority = self.server.settings.get("priority", "NORMAL")
676
(gnutls.library.functions
677
.gnutls_priority_set_direct(session._c_object,
682
except gnutls.errors.GNUTLSError, error:
683
logger.warning(u"Handshake failed: %s", error)
684
# Do not run session.bye() here: the session is not
685
# established. Just abandon the request.
688
fpr = fingerprint(peer_certificate(session))
689
except (TypeError, gnutls.errors.GNUTLSError), error:
690
logger.warning(u"Bad certificate: %s", error)
693
logger.debug(u"Fingerprint: %s", fpr)
694
for c in self.server.clients:
695
if c.fingerprint == fpr:
699
logger.warning(u"Client not found for fingerprint: %s",
703
# Have to check if client.still_valid(), since it is possible
704
# that the client timed out while establishing the GnuTLS
706
if not client.still_valid():
707
logger.warning(u"Client %(name)s is invalid",
711
## This won't work here, since we're in a fork.
712
# client.bump_timeout()
714
while sent_size < len(client.secret):
715
sent = session.send(client.secret[sent_size:])
716
logger.debug(u"Sent: %d, remaining: %d",
717
sent, len(client.secret)
718
- (sent_size + sent))
723
class IPv6_TCPServer(SocketServer.ForkingMixIn,
724
SocketServer.TCPServer, object):
725
"""IPv6 TCP server. Accepts 'None' as address and/or port.
727
settings: Server settings
728
clients: Set() of Client objects
1617
729
enabled: Boolean; whether this server is activated yet
1618
interface: None or a network interface name (string)
1619
use_ipv6: Boolean; to use IPv6 or not
1621
def __init__(self, server_address, RequestHandlerClass,
1622
interface=None, use_ipv6=True):
1623
self.interface = interface
1625
self.address_family = socket.AF_INET6
1626
socketserver.TCPServer.__init__(self, server_address,
1627
RequestHandlerClass)
731
address_family = socket.AF_INET6
732
def __init__(self, *args, **kwargs):
733
if "settings" in kwargs:
734
self.settings = kwargs["settings"]
735
del kwargs["settings"]
736
if "clients" in kwargs:
737
self.clients = kwargs["clients"]
738
del kwargs["clients"]
740
super(IPv6_TCPServer, self).__init__(*args, **kwargs)
1628
741
def server_bind(self):
1629
742
"""This overrides the normal server_bind() function
1630
743
to bind to an interface if one was specified, and also NOT to
1631
744
bind to an address or port if they were not specified."""
1632
if self.interface is not None:
1633
if SO_BINDTODEVICE is None:
1634
logger.error("SO_BINDTODEVICE does not exist;"
1635
" cannot bind to interface %s",
1639
self.socket.setsockopt(socket.SOL_SOCKET,
1643
except socket.error as error:
1644
if error[0] == errno.EPERM:
1645
logger.error("No permission to"
1646
" bind to interface %s",
1648
elif error[0] == errno.ENOPROTOOPT:
1649
logger.error("SO_BINDTODEVICE not available;"
1650
" cannot bind to interface %s",
745
if self.settings["interface"]:
746
# 25 is from /usr/include/asm-i486/socket.h
747
SO_BINDTODEVICE = getattr(socket, "SO_BINDTODEVICE", 25)
749
self.socket.setsockopt(socket.SOL_SOCKET,
751
self.settings["interface"])
752
except socket.error, error:
753
if error[0] == errno.EPERM:
754
logger.error(u"No permission to"
755
u" bind to interface %s",
756
self.settings["interface"])
1654
759
# Only bind(2) the socket if we really need to.
1655
760
if self.server_address[0] or self.server_address[1]:
1656
761
if not self.server_address[0]:
1657
if self.address_family == socket.AF_INET6:
1658
any_address = "::" # in6addr_any
1660
any_address = socket.INADDR_ANY
1661
self.server_address = (any_address,
763
self.server_address = (in6addr_any,
1662
764
self.server_address[1])
1663
765
elif not self.server_address[1]:
1664
766
self.server_address = (self.server_address[0],
1666
# if self.interface:
768
# if self.settings["interface"]:
1667
769
# self.server_address = (self.server_address[0],
1670
772
# if_nametoindex
1672
return socketserver.TCPServer.server_bind(self)
1675
class MandosServer(IPv6_TCPServer):
1679
clients: set of Client objects
1680
gnutls_priority GnuTLS priority string
1681
use_dbus: Boolean; to emit D-Bus signals or not
1683
Assumes a gobject.MainLoop event loop.
1685
def __init__(self, server_address, RequestHandlerClass,
1686
interface=None, use_ipv6=True, clients=None,
1687
gnutls_priority=None, use_dbus=True):
1688
self.enabled = False
1689
self.clients = clients
1690
if self.clients is None:
1692
self.use_dbus = use_dbus
1693
self.gnutls_priority = gnutls_priority
1694
IPv6_TCPServer.__init__(self, server_address,
1695
RequestHandlerClass,
1696
interface = interface,
1697
use_ipv6 = use_ipv6)
775
return super(IPv6_TCPServer, self).server_bind()
1698
776
def server_activate(self):
1699
777
if self.enabled:
1700
return socketserver.TCPServer.server_activate(self)
778
return super(IPv6_TCPServer, self).server_activate()
1702
779
def enable(self):
1703
780
self.enabled = True
1705
def add_pipe(self, parent_pipe, proc):
1706
# Call "handle_ipc" for both data and EOF events
1707
gobject.io_add_watch(parent_pipe.fileno(),
1708
gobject.IO_IN | gobject.IO_HUP,
1709
functools.partial(self.handle_ipc,
1714
def handle_ipc(self, source, condition, parent_pipe=None,
1715
proc = None, client_object=None):
1717
gobject.IO_IN: "IN", # There is data to read.
1718
gobject.IO_OUT: "OUT", # Data can be written (without
1720
gobject.IO_PRI: "PRI", # There is urgent data to read.
1721
gobject.IO_ERR: "ERR", # Error condition.
1722
gobject.IO_HUP: "HUP" # Hung up (the connection has been
1723
# broken, usually for pipes and
1726
conditions_string = ' | '.join(name
1728
condition_names.iteritems()
1729
if cond & condition)
1730
# error, or the other end of multiprocessing.Pipe has closed
1731
if condition & (gobject.IO_ERR | condition & gobject.IO_HUP):
1732
# Wait for other process to exit
1736
# Read a request from the child
1737
request = parent_pipe.recv()
1738
command = request[0]
1740
if command == 'init':
1742
address = request[2]
1744
for c in self.clients.itervalues():
1745
if c.fingerprint == fpr:
1749
logger.info("Client not found for fingerprint: %s, ad"
1750
"dress: %s", fpr, address)
1753
mandos_dbus_service.ClientNotFound(fpr,
1755
parent_pipe.send(False)
1758
gobject.io_add_watch(parent_pipe.fileno(),
1759
gobject.IO_IN | gobject.IO_HUP,
1760
functools.partial(self.handle_ipc,
1766
parent_pipe.send(True)
1767
# remove the old hook in favor of the new above hook on
1770
if command == 'funcall':
1771
funcname = request[1]
1775
parent_pipe.send(('data', getattr(client_object,
1779
if command == 'getattr':
1780
attrname = request[1]
1781
if callable(client_object.__getattribute__(attrname)):
1782
parent_pipe.send(('function',))
1784
parent_pipe.send(('data', client_object
1785
.__getattribute__(attrname)))
1787
if command == 'setattr':
1788
attrname = request[1]
1790
setattr(client_object, attrname, value)
1795
783
def string_to_delta(interval):
1796
784
"""Parse a string and return a datetime.timedelta
1798
786
>>> string_to_delta('7d')
1799
787
datetime.timedelta(7)
1800
788
>>> string_to_delta('60s')
2068
1052
# No console logging
2069
1053
logger.removeHandler(console)
2071
# Need to fork before connecting to D-Bus
2073
1054
# Close all input and output, do double fork, etc.
2077
# From the Avahi example code
2078
DBusGMainLoop(set_as_default=True )
2079
main_loop = gobject.MainLoop()
2080
bus = dbus.SystemBus()
2081
# End of Avahi example code
2084
bus_name = dbus.service.BusName("se.recompile.Mandos",
2085
bus, do_not_queue=True)
2086
old_bus_name = (dbus.service.BusName
2087
("se.bsnet.fukt.Mandos", bus,
2089
except dbus.exceptions.NameExistsException as e:
2090
logger.error(unicode(e) + ", disabling D-Bus")
2092
server_settings["use_dbus"] = False
2093
tcp_server.use_dbus = False
2094
protocol = avahi.PROTO_INET6 if use_ipv6 else avahi.PROTO_INET
2095
service = AvahiService(name = server_settings["servicename"],
2096
servicetype = "_mandos._tcp",
2097
protocol = protocol, bus = bus)
2098
if server_settings["interface"]:
2099
service.interface = (if_nametoindex
2100
(str(server_settings["interface"])))
2102
global multiprocessing_manager
2103
multiprocessing_manager = multiprocessing.Manager()
2105
client_class = Client
2107
client_class = functools.partial(ClientDBusTransitional,
2110
special_settings = {
2111
# Some settings need to be accessd by special methods;
2112
# booleans need .getboolean(), etc. Here is a list of them:
2113
"approved_by_default":
2115
client_config.getboolean(section, "approved_by_default"),
2117
# Construct a new dict of client settings of this form:
2118
# { client_name: {setting_name: value, ...}, ...}
2119
# with exceptions for any special settings as defined above
2120
client_settings = dict((clientname,
2122
(value if setting not in special_settings
2123
else special_settings[setting](clientname)))
2124
for setting, value in client_config.items(clientname)))
2125
for clientname in client_config.sections())
2127
old_client_settings = {}
2130
if server_settings["restore"]:
2132
with open(stored_state_path, "rb") as stored_state:
2133
clients_data, old_client_settings = pickle.load(stored_state)
2134
os.remove(stored_state_path)
2135
except IOError as e:
2136
logger.warning("Could not load persistant state: {0}".format(e))
2137
if e.errno != errno.ENOENT:
2140
for client in clients_data:
2141
client_name = client["name"]
2143
# Decide which value to use after restoring saved state.
2144
# We have three different values: Old config file,
2145
# new config file, and saved state.
2146
# New config value takes precedence if it differs from old
2147
# config value, otherwise use saved state.
2148
for name, value in client_settings[client_name].items():
2150
# For each value in new config, check if it differs
2151
# from the old config value (Except for the "secret"
2153
if name != "secret" and value != old_client_settings[client_name][name]:
2154
setattr(client, name, value)
2158
# Clients who has passed its expire date, can still be enabled if its
2159
# last checker was sucessful. Clients who checkers failed before we
2160
# stored it state is asumed to had failed checker during downtime.
2161
if client["enabled"] and client["last_checked_ok"]:
2162
if ((datetime.datetime.utcnow() - client["last_checked_ok"])
2163
> client["interval"]):
2164
if client["last_checker_status"] != 0:
2165
client["enabled"] = False
2167
client["expires"] = datetime.datetime.utcnow() + client["timeout"]
2169
client["changedstate"] = (multiprocessing_manager
2170
.Condition(multiprocessing_manager
2173
new_client = ClientDBusTransitional.__new__(ClientDBusTransitional)
2174
tcp_server.clients[client_name] = new_client
2175
new_client.bus = bus
2176
for name, value in client.iteritems():
2177
setattr(new_client, name, value)
2178
client_object_name = unicode(client_name).translate(
2179
{ord("."): ord("_"),
2180
ord("-"): ord("_")})
2181
new_client.dbus_object_path = (dbus.ObjectPath
2182
("/clients/" + client_object_name))
2183
DBusObjectWithProperties.__init__(new_client,
2185
new_client.dbus_object_path)
2187
tcp_server.clients[client_name] = Client.__new__(Client)
2188
for name, value in client.iteritems():
2189
setattr(tcp_server.clients[client_name], name, value)
2191
tcp_server.clients[client_name].decrypt_secret(
2192
client_settings[client_name]["secret"])
2194
# Create/remove clients based on new changes made to config
2195
for clientname in set(old_client_settings) - set(client_settings):
2196
del tcp_server.clients[clientname]
2197
for clientname in set(client_settings) - set(old_client_settings):
2198
tcp_server.clients[clientname] = (client_class(name = clientname,
2204
if not tcp_server.clients:
2205
logger.warning("No clients defined")
1059
pidfile.write(str(pid) + "\n")
1063
logger.error(u"Could not write to file %r with PID %d",
1066
# "pidfile" was never created
1071
"Cleanup function; run on exit"
1073
# From the Avahi example code
1074
if not group is None:
1077
# End of Avahi example code
1080
client = clients.pop()
1081
client.disable_hook = None
1084
atexit.register(cleanup)
2211
pidfile.write(str(pid) + "\n".encode("utf-8"))
2214
logger.error("Could not write to file %r with PID %d",
2217
# "pidfile" was never created
2221
1087
signal.signal(signal.SIGINT, signal.SIG_IGN)
2223
1088
signal.signal(signal.SIGHUP, lambda signum, frame: sys.exit())
2224
1089
signal.signal(signal.SIGTERM, lambda signum, frame: sys.exit())
2227
class MandosDBusService(dbus.service.Object):
1092
class MandosServer(dbus.service.Object):
2228
1093
"""A D-Bus proxy object"""
2229
1094
def __init__(self):
2230
dbus.service.Object.__init__(self, bus, "/")
2231
_interface = "se.recompile.Mandos"
1095
dbus.service.Object.__init__(self, bus,
1097
_interface = u"org.mandos_system.Mandos"
1099
@dbus.service.signal(_interface, signature="oa{sv}")
1100
def ClientAdded(self, objpath, properties):
2233
1104
@dbus.service.signal(_interface, signature="o")
2234
def ClientAdded(self, objpath):
2238
@dbus.service.signal(_interface, signature="ss")
2239
def ClientNotFound(self, fingerprint, address):
2243
@dbus.service.signal(_interface, signature="os")
2244
def ClientRemoved(self, objpath, name):
1105
def ClientRemoved(self, objpath):
2248
1109
@dbus.service.method(_interface, out_signature="ao")
2249
1110
def GetAllClients(self):
2251
return dbus.Array(c.dbus_object_path
2253
tcp_server.clients.itervalues())
2255
@dbus.service.method(_interface,
2256
out_signature="a{oa{sv}}")
1111
return dbus.Array(c.dbus_object_path for c in clients)
1113
@dbus.service.method(_interface, out_signature="a{oa{sv}}")
2257
1114
def GetAllClientsWithProperties(self):
2259
1115
return dbus.Dictionary(
2260
((c.dbus_object_path, c.GetAll(""))
2261
for c in tcp_server.clients.itervalues()),
1116
((c.dbus_object_path, c.GetAllProperties())
2262
1118
signature="oa{sv}")
2264
1120
@dbus.service.method(_interface, in_signature="o")
2265
1121
def RemoveClient(self, object_path):
2267
for c in tcp_server.clients.itervalues():
2268
1123
if c.dbus_object_path == object_path:
2269
del tcp_server.clients[c.name]
2270
c.remove_from_connection()
2271
1125
# Don't signal anything except ClientRemoved
2272
c.disable(quiet=True)
2273
1128
# Emit D-Bus signal
2274
self.ClientRemoved(object_path, c.name)
1129
self.ClientRemoved(object_path)
2276
raise KeyError(object_path)
1132
@dbus.service.method(_interface)
2280
class MandosDBusServiceTransitional(MandosDBusService):
2281
__metaclass__ = AlternateDBusNamesMetaclass
2282
mandos_dbus_service = MandosDBusServiceTransitional()
2285
"Cleanup function; run on exit"
2288
multiprocessing.active_children()
2289
if not (tcp_server.clients or client_settings):
2292
# Store client before exiting. Secrets are encrypted with key based
2293
# on what config file has. If config file is removed/edited, old
2294
# secret will thus be unrecovable.
2296
for client in tcp_server.clients.itervalues():
2297
client.encrypt_secret(client_settings[client.name]["secret"])
2301
# A list of attributes that will not be stored when shuting down.
2302
exclude = set(("bus", "changedstate", "secret"))
2303
for name, typ in inspect.getmembers(dbus.service.Object):
2306
client_dict["encrypted_secret"] = client.encrypted_secret
2307
for attr in client.client_structure:
2308
if attr not in exclude:
2309
client_dict[attr] = getattr(client, attr)
2311
clients.append(client_dict)
2312
del client_settings[client.name]["secret"]
2315
with os.fdopen(os.open(stored_state_path, os.O_CREAT|os.O_WRONLY|os.O_TRUNC, 0600), "wb") as stored_state:
2316
pickle.dump((clients, client_settings), stored_state)
2317
except IOError as e:
2318
logger.warning("Could not save persistant state: {0}".format(e))
2319
if e.errno != errno.ENOENT:
2322
# Delete all clients, and settings from config
2323
while tcp_server.clients:
2324
name, client = tcp_server.clients.popitem()
2326
client.remove_from_connection()
2327
# Don't signal anything except ClientRemoved
2328
client.disable(quiet=True)
2331
mandos_dbus_service.ClientRemoved(client
2334
client_settings.clear()
2336
atexit.register(cleanup)
2338
for client in tcp_server.clients.itervalues():
1138
mandos_server = MandosServer()
1140
for client in clients:
2340
1142
# Emit D-Bus signal
2341
mandos_dbus_service.ClientAdded(client.dbus_object_path)
2342
# Need to initiate checking of clients
2344
client.init_checker()
1143
mandos_server.ClientAdded(client.dbus_object_path,
1144
client.GetAllProperties())
2347
1147
tcp_server.enable()
2348
1148
tcp_server.server_activate()
2350
1150
# Find out what port we got
2351
1151
service.port = tcp_server.socket.getsockname()[1]
2353
logger.info("Now listening on address %r, port %d,"
2354
" flowinfo %d, scope_id %d"
2355
% tcp_server.socket.getsockname())
2357
logger.info("Now listening on address %r, port %d"
2358
% tcp_server.socket.getsockname())
1152
logger.info(u"Now listening on address %r, port %d, flowinfo %d,"
1153
u" scope_id %d" % tcp_server.socket.getsockname())
2360
1155
#service.interface = tcp_server.socket.getsockname()[3]
2363
1158
# From the Avahi example code
1159
server.connect_to_signal("StateChanged", server_state_changed)
2366
except dbus.exceptions.DBusException as error:
2367
logger.critical("DBusException: %s", error)
1161
server_state_changed(server.GetState())
1162
except dbus.exceptions.DBusException, error:
1163
logger.critical(u"DBusException: %s", error)
2370
1165
# End of Avahi example code