/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos

  • Committer: Teddy Hogeborn
  • Date: 2009-09-23 20:31:47 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090923203147-mbs7klc1tugrrdwf
* mandos (Client.__init__): Open "secfile" file in binary mode.

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
from dbus.mainloop.glib import DBusGMainLoop
68
68
import ctypes
69
69
import ctypes.util
 
70
import xml.dom.minidom
 
71
import inspect
70
72
 
71
73
try:
72
74
    SO_BINDTODEVICE = socket.SO_BINDTODEVICE
77
79
        SO_BINDTODEVICE = None
78
80
 
79
81
 
80
 
version = "1.0.11"
 
82
version = "1.0.12"
81
83
 
82
84
logger = logging.Logger(u'mandos')
83
85
syslogger = (logging.handlers.SysLogHandler
174
176
                                    self.server.EntryGroupNew()),
175
177
                avahi.DBUS_INTERFACE_ENTRY_GROUP)
176
178
            self.group.connect_to_signal('StateChanged',
177
 
                                         self.entry_group_state_changed)
 
179
                                         self
 
180
                                         .entry_group_state_changed)
178
181
        logger.debug(u"Adding Zeroconf service '%s' of type '%s' ...",
179
182
                     self.name, self.type)
180
183
        self.group.AddService(
246
249
                                    to see if the client lives.
247
250
                                    'None' if no process is running.
248
251
    checker_initiator_tag: a gobject event source tag, or None
249
 
    disable_initiator_tag:    - '' -
 
252
    disable_initiator_tag: - '' -
250
253
    checker_callback_tag:  - '' -
251
254
    checker_command: string; External command which is run to check if
252
255
                     client lives.  %() expansions are done at
256
259
    """
257
260
    
258
261
    @staticmethod
259
 
    def _datetime_to_milliseconds(dt):
260
 
        "Convert a datetime.datetime() to milliseconds"
261
 
        return ((dt.days * 24 * 60 * 60 * 1000)
262
 
                + (dt.seconds * 1000)
263
 
                + (dt.microseconds // 1000))
 
262
    def _timedelta_to_milliseconds(td):
 
263
        "Convert a datetime.timedelta() to milliseconds"
 
264
        return ((td.days * 24 * 60 * 60 * 1000)
 
265
                + (td.seconds * 1000)
 
266
                + (td.microseconds // 1000))
264
267
    
265
268
    def timeout_milliseconds(self):
266
269
        "Return the 'timeout' attribute in milliseconds"
267
 
        return self._datetime_to_milliseconds(self.timeout)
 
270
        return self._timedelta_to_milliseconds(self.timeout)
268
271
    
269
272
    def interval_milliseconds(self):
270
273
        "Return the 'interval' attribute in milliseconds"
271
 
        return self._datetime_to_milliseconds(self.interval)
 
274
        return self._timedelta_to_milliseconds(self.interval)
272
275
    
273
276
    def __init__(self, name = None, disable_hook=None, config=None):
274
277
        """Note: the 'checker' key in 'config' sets the
289
292
        elif u"secfile" in config:
290
293
            with closing(open(os.path.expanduser
291
294
                              (os.path.expandvars
292
 
                               (config[u"secfile"])))) as secfile:
 
295
                               (config[u"secfile"])),
 
296
                              "rb")) as secfile:
293
297
                self.secret = secfile.read()
294
298
        else:
295
299
            raise TypeError(u"No secret or secfile for client %s"
395
399
        # is as it should be.
396
400
        
397
401
        # If a checker exists, make sure it is not a zombie
398
 
        if self.checker is not None:
 
402
        try:
399
403
            pid, status = os.waitpid(self.checker.pid, os.WNOHANG)
 
404
        except (AttributeError, OSError), error:
 
405
            if (isinstance(error, OSError)
 
406
                and error.errno != errno.ECHILD):
 
407
                raise error
 
408
        else:
400
409
            if pid:
401
410
                logger.warning(u"Checker was a zombie")
402
411
                gobject.source_remove(self.checker_callback_tag)
477
486
            return now < (self.last_checked_ok + self.timeout)
478
487
 
479
488
 
480
 
class ClientDBus(Client, dbus.service.Object):
 
489
def dbus_service_property(dbus_interface, signature=u"v",
 
490
                          access=u"readwrite", byte_arrays=False):
 
491
    """Decorators for marking methods of a DBusObjectWithProperties to
 
492
    become properties on the D-Bus.
 
493
    
 
494
    The decorated method will be called with no arguments by "Get"
 
495
    and with one argument by "Set".
 
496
    
 
497
    The parameters, where they are supported, are the same as
 
498
    dbus.service.method, except there is only "signature", since the
 
499
    type from Get() and the type sent to Set() is the same.
 
500
    """
 
501
    def decorator(func):
 
502
        func._dbus_is_property = True
 
503
        func._dbus_interface = dbus_interface
 
504
        func._dbus_signature = signature
 
505
        func._dbus_access = access
 
506
        func._dbus_name = func.__name__
 
507
        if func._dbus_name.endswith(u"_dbus_property"):
 
508
            func._dbus_name = func._dbus_name[:-14]
 
509
        func._dbus_get_args_options = {u'byte_arrays': byte_arrays }
 
510
        return func
 
511
    return decorator
 
512
 
 
513
 
 
514
class DBusPropertyException(dbus.exceptions.DBusException):
 
515
    """A base class for D-Bus property-related exceptions
 
516
    """
 
517
    def __unicode__(self):
 
518
        return unicode(str(self))
 
519
 
 
520
 
 
521
class DBusPropertyAccessException(DBusPropertyException):
 
522
    """A property's access permissions disallows an operation.
 
523
    """
 
524
    pass
 
525
 
 
526
 
 
527
class DBusPropertyNotFound(DBusPropertyException):
 
528
    """An attempt was made to access a non-existing property.
 
529
    """
 
530
    pass
 
531
 
 
532
 
 
533
class DBusObjectWithProperties(dbus.service.Object):
 
534
    """A D-Bus object with properties.
 
535
 
 
536
    Classes inheriting from this can use the dbus_service_property
 
537
    decorator to expose methods as D-Bus properties.  It exposes the
 
538
    standard Get(), Set(), and GetAll() methods on the D-Bus.
 
539
    """
 
540
    
 
541
    @staticmethod
 
542
    def _is_dbus_property(obj):
 
543
        return getattr(obj, u"_dbus_is_property", False)
 
544
    
 
545
    def _get_all_dbus_properties(self):
 
546
        """Returns a generator of (name, attribute) pairs
 
547
        """
 
548
        return ((prop._dbus_name, prop)
 
549
                for name, prop in
 
550
                inspect.getmembers(self, self._is_dbus_property))
 
551
    
 
552
    def _get_dbus_property(self, interface_name, property_name):
 
553
        """Returns a bound method if one exists which is a D-Bus
 
554
        property with the specified name and interface.
 
555
        """
 
556
        for name in (property_name,
 
557
                     property_name + u"_dbus_property"):
 
558
            prop = getattr(self, name, None)
 
559
            if (prop is None
 
560
                or not self._is_dbus_property(prop)
 
561
                or prop._dbus_name != property_name
 
562
                or (interface_name and prop._dbus_interface
 
563
                    and interface_name != prop._dbus_interface)):
 
564
                continue
 
565
            return prop
 
566
        # No such property
 
567
        raise DBusPropertyNotFound(self.dbus_object_path + u":"
 
568
                                   + interface_name + u"."
 
569
                                   + property_name)
 
570
    
 
571
    @dbus.service.method(dbus.PROPERTIES_IFACE, in_signature=u"ss",
 
572
                         out_signature=u"v")
 
573
    def Get(self, interface_name, property_name):
 
574
        """Standard D-Bus property Get() method, see D-Bus standard.
 
575
        """
 
576
        prop = self._get_dbus_property(interface_name, property_name)
 
577
        if prop._dbus_access == u"write":
 
578
            raise DBusPropertyAccessException(property_name)
 
579
        value = prop()
 
580
        if not hasattr(value, u"variant_level"):
 
581
            return value
 
582
        return type(value)(value, variant_level=value.variant_level+1)
 
583
    
 
584
    @dbus.service.method(dbus.PROPERTIES_IFACE, in_signature=u"ssv")
 
585
    def Set(self, interface_name, property_name, value):
 
586
        """Standard D-Bus property Set() method, see D-Bus standard.
 
587
        """
 
588
        prop = self._get_dbus_property(interface_name, property_name)
 
589
        if prop._dbus_access == u"read":
 
590
            raise DBusPropertyAccessException(property_name)
 
591
        if prop._dbus_get_args_options[u"byte_arrays"]:
 
592
            value = dbus.ByteArray(''.join(unichr(byte)
 
593
                                           for byte in value))
 
594
        prop(value)
 
595
    
 
596
    @dbus.service.method(dbus.PROPERTIES_IFACE, in_signature=u"s",
 
597
                         out_signature=u"a{sv}")
 
598
    def GetAll(self, interface_name):
 
599
        """Standard D-Bus property GetAll() method, see D-Bus
 
600
        standard.
 
601
 
 
602
        Note: Will not include properties with access="write".
 
603
        """
 
604
        all = {}
 
605
        for name, prop in self._get_all_dbus_properties():
 
606
            if (interface_name
 
607
                and interface_name != prop._dbus_interface):
 
608
                # Interface non-empty but did not match
 
609
                continue
 
610
            # Ignore write-only properties
 
611
            if prop._dbus_access == u"write":
 
612
                continue
 
613
            value = prop()
 
614
            if not hasattr(value, u"variant_level"):
 
615
                all[name] = value
 
616
                continue
 
617
            all[name] = type(value)(value, variant_level=
 
618
                                    value.variant_level+1)
 
619
        return dbus.Dictionary(all, signature=u"sv")
 
620
    
 
621
    @dbus.service.method(dbus.INTROSPECTABLE_IFACE,
 
622
                         out_signature=u"s",
 
623
                         path_keyword='object_path',
 
624
                         connection_keyword='connection')
 
625
    def Introspect(self, object_path, connection):
 
626
        """Standard D-Bus method, overloaded to insert property tags.
 
627
        """
 
628
        xmlstring = dbus.service.Object.Introspect(self, object_path,
 
629
                                           connection)
 
630
        document = xml.dom.minidom.parseString(xmlstring)
 
631
        del xmlstring
 
632
        def make_tag(document, name, prop):
 
633
            e = document.createElement(u"property")
 
634
            e.setAttribute(u"name", name)
 
635
            e.setAttribute(u"type", prop._dbus_signature)
 
636
            e.setAttribute(u"access", prop._dbus_access)
 
637
            return e
 
638
        for if_tag in document.getElementsByTagName(u"interface"):
 
639
            for tag in (make_tag(document, name, prop)
 
640
                        for name, prop
 
641
                        in self._get_all_dbus_properties()
 
642
                        if prop._dbus_interface
 
643
                        == if_tag.getAttribute(u"name")):
 
644
                if_tag.appendChild(tag)
 
645
        xmlstring = document.toxml(u"utf-8")
 
646
        document.unlink()
 
647
        return xmlstring
 
648
 
 
649
 
 
650
class ClientDBus(Client, DBusObjectWithProperties):
481
651
    """A Client class using D-Bus
482
652
    
483
653
    Attributes:
494
664
        self.dbus_object_path = (dbus.ObjectPath
495
665
                                 (u"/clients/"
496
666
                                  + self.name.replace(u".", u"_")))
497
 
        dbus.service.Object.__init__(self, self.bus,
498
 
                                     self.dbus_object_path)
 
667
        DBusObjectWithProperties.__init__(self, self.bus,
 
668
                                          self.dbus_object_path)
499
669
    
500
670
    @staticmethod
501
671
    def _datetime_to_dbus(dt, variant_level=0):
530
700
            self.remove_from_connection()
531
701
        except LookupError:
532
702
            pass
533
 
        if hasattr(dbus.service.Object, u"__del__"):
534
 
            dbus.service.Object.__del__(self, *args, **kwargs)
 
703
        if hasattr(DBusObjectWithProperties, u"__del__"):
 
704
            DBusObjectWithProperties.__del__(self, *args, **kwargs)
535
705
        Client.__del__(self, *args, **kwargs)
536
706
    
537
707
    def checker_callback(self, pid, condition, command,
611
781
        "D-Bus signal"
612
782
        pass
613
783
    
614
 
    # GetAllProperties - method
615
 
    @dbus.service.method(_interface, out_signature=u"a{sv}")
616
 
    def GetAllProperties(self):
617
 
        "D-Bus method"
618
 
        return dbus.Dictionary({
619
 
                dbus.String(u"name"):
620
 
                    dbus.String(self.name, variant_level=1),
621
 
                dbus.String(u"fingerprint"):
622
 
                    dbus.String(self.fingerprint, variant_level=1),
623
 
                dbus.String(u"host"):
624
 
                    dbus.String(self.host, variant_level=1),
625
 
                dbus.String(u"created"):
626
 
                    self._datetime_to_dbus(self.created,
627
 
                                           variant_level=1),
628
 
                dbus.String(u"last_enabled"):
629
 
                    (self._datetime_to_dbus(self.last_enabled,
630
 
                                            variant_level=1)
631
 
                     if self.last_enabled is not None
632
 
                     else dbus.Boolean(False, variant_level=1)),
633
 
                dbus.String(u"enabled"):
634
 
                    dbus.Boolean(self.enabled, variant_level=1),
635
 
                dbus.String(u"last_checked_ok"):
636
 
                    (self._datetime_to_dbus(self.last_checked_ok,
637
 
                                            variant_level=1)
638
 
                     if self.last_checked_ok is not None
639
 
                     else dbus.Boolean (False, variant_level=1)),
640
 
                dbus.String(u"timeout"):
641
 
                    dbus.UInt64(self.timeout_milliseconds(),
642
 
                                variant_level=1),
643
 
                dbus.String(u"interval"):
644
 
                    dbus.UInt64(self.interval_milliseconds(),
645
 
                                variant_level=1),
646
 
                dbus.String(u"checker"):
647
 
                    dbus.String(self.checker_command,
648
 
                                variant_level=1),
649
 
                dbus.String(u"checker_running"):
650
 
                    dbus.Boolean(self.checker is not None,
651
 
                                 variant_level=1),
652
 
                dbus.String(u"object_path"):
653
 
                    dbus.ObjectPath(self.dbus_object_path,
654
 
                                    variant_level=1)
655
 
                }, signature=u"sv")
656
 
    
657
 
    # IsStillValid - method
658
 
    @dbus.service.method(_interface, out_signature=u"b")
659
 
    def IsStillValid(self):
660
 
        return self.still_valid()
661
 
    
662
784
    # PropertyChanged - signal
663
785
    @dbus.service.signal(_interface, signature=u"sv")
664
786
    def PropertyChanged(self, property, value):
677
799
        "D-Bus signal"
678
800
        pass
679
801
    
680
 
    # SetChecker - method
681
 
    @dbus.service.method(_interface, in_signature=u"s")
682
 
    def SetChecker(self, checker):
683
 
        "D-Bus setter method"
684
 
        self.checker_command = checker
685
 
        # Emit D-Bus signal
686
 
        self.PropertyChanged(dbus.String(u"checker"),
687
 
                             dbus.String(self.checker_command,
688
 
                                         variant_level=1))
689
 
    
690
 
    # SetHost - method
691
 
    @dbus.service.method(_interface, in_signature=u"s")
692
 
    def SetHost(self, host):
693
 
        "D-Bus setter method"
694
 
        self.host = host
695
 
        # Emit D-Bus signal
696
 
        self.PropertyChanged(dbus.String(u"host"),
697
 
                             dbus.String(self.host, variant_level=1))
698
 
    
699
 
    # SetInterval - method
700
 
    @dbus.service.method(_interface, in_signature=u"t")
701
 
    def SetInterval(self, milliseconds):
702
 
        self.interval = datetime.timedelta(0, 0, 0, milliseconds)
703
 
        # Emit D-Bus signal
704
 
        self.PropertyChanged(dbus.String(u"interval"),
705
 
                             (dbus.UInt64(self.interval_milliseconds(),
706
 
                                          variant_level=1)))
707
 
    
708
 
    # SetSecret - method
709
 
    @dbus.service.method(_interface, in_signature=u"ay",
710
 
                         byte_arrays=True)
711
 
    def SetSecret(self, secret):
712
 
        "D-Bus setter method"
713
 
        self.secret = str(secret)
714
 
    
715
 
    # SetTimeout - method
716
 
    @dbus.service.method(_interface, in_signature=u"t")
717
 
    def SetTimeout(self, milliseconds):
718
 
        self.timeout = datetime.timedelta(0, 0, 0, milliseconds)
719
 
        # Emit D-Bus signal
720
 
        self.PropertyChanged(dbus.String(u"timeout"),
721
 
                             (dbus.UInt64(self.timeout_milliseconds(),
722
 
                                          variant_level=1)))
723
 
    
724
802
    # Enable - method
725
803
    @dbus.service.method(_interface)
726
804
    def Enable(self):
744
822
    def StopChecker(self):
745
823
        self.stop_checker()
746
824
    
 
825
    # name - property
 
826
    @dbus_service_property(_interface, signature=u"s", access=u"read")
 
827
    def name_dbus_property(self):
 
828
        return dbus.String(self.name)
 
829
    
 
830
    # fingerprint - property
 
831
    @dbus_service_property(_interface, signature=u"s", access=u"read")
 
832
    def fingerprint_dbus_property(self):
 
833
        return dbus.String(self.fingerprint)
 
834
    
 
835
    # host - property
 
836
    @dbus_service_property(_interface, signature=u"s",
 
837
                           access=u"readwrite")
 
838
    def host_dbus_property(self, value=None):
 
839
        if value is None:       # get
 
840
            return dbus.String(self.host)
 
841
        self.host = value
 
842
        # Emit D-Bus signal
 
843
        self.PropertyChanged(dbus.String(u"host"),
 
844
                             dbus.String(value, variant_level=1))
 
845
    
 
846
    # created - property
 
847
    @dbus_service_property(_interface, signature=u"s", access=u"read")
 
848
    def created_dbus_property(self):
 
849
        return dbus.String(self._datetime_to_dbus(self.created))
 
850
    
 
851
    # last_enabled - property
 
852
    @dbus_service_property(_interface, signature=u"s", access=u"read")
 
853
    def last_enabled_dbus_property(self):
 
854
        if self.last_enabled is None:
 
855
            return dbus.String(u"")
 
856
        return dbus.String(self._datetime_to_dbus(self.last_enabled))
 
857
    
 
858
    # enabled - property
 
859
    @dbus_service_property(_interface, signature=u"b",
 
860
                           access=u"readwrite")
 
861
    def enabled_dbus_property(self, value=None):
 
862
        if value is None:       # get
 
863
            return dbus.Boolean(self.enabled)
 
864
        if value:
 
865
            self.enable()
 
866
        else:
 
867
            self.disable()
 
868
    
 
869
    # last_checked_ok - property
 
870
    @dbus_service_property(_interface, signature=u"s",
 
871
                           access=u"readwrite")
 
872
    def last_checked_ok_dbus_property(self, value=None):
 
873
        if value is not None:
 
874
            self.checked_ok()
 
875
            return
 
876
        if self.last_checked_ok is None:
 
877
            return dbus.String(u"")
 
878
        return dbus.String(self._datetime_to_dbus(self
 
879
                                                  .last_checked_ok))
 
880
    
 
881
    # timeout - property
 
882
    @dbus_service_property(_interface, signature=u"t",
 
883
                           access=u"readwrite")
 
884
    def timeout_dbus_property(self, value=None):
 
885
        if value is None:       # get
 
886
            return dbus.UInt64(self.timeout_milliseconds())
 
887
        self.timeout = datetime.timedelta(0, 0, 0, value)
 
888
        # Emit D-Bus signal
 
889
        self.PropertyChanged(dbus.String(u"timeout"),
 
890
                             dbus.UInt64(value, variant_level=1))
 
891
        if getattr(self, u"disable_initiator_tag", None) is None:
 
892
            return
 
893
        # Reschedule timeout
 
894
        gobject.source_remove(self.disable_initiator_tag)
 
895
        self.disable_initiator_tag = None
 
896
        time_to_die = (self.
 
897
                       _timedelta_to_milliseconds((self
 
898
                                                   .last_checked_ok
 
899
                                                   + self.timeout)
 
900
                                                  - datetime.datetime
 
901
                                                  .utcnow()))
 
902
        if time_to_die <= 0:
 
903
            # The timeout has passed
 
904
            self.disable()
 
905
        else:
 
906
            self.disable_initiator_tag = (gobject.timeout_add
 
907
                                          (time_to_die, self.disable))
 
908
    
 
909
    # interval - property
 
910
    @dbus_service_property(_interface, signature=u"t",
 
911
                           access=u"readwrite")
 
912
    def interval_dbus_property(self, value=None):
 
913
        if value is None:       # get
 
914
            return dbus.UInt64(self.interval_milliseconds())
 
915
        self.interval = datetime.timedelta(0, 0, 0, value)
 
916
        # Emit D-Bus signal
 
917
        self.PropertyChanged(dbus.String(u"interval"),
 
918
                             dbus.UInt64(value, variant_level=1))
 
919
        if getattr(self, u"checker_initiator_tag", None) is None:
 
920
            return
 
921
        # Reschedule checker run
 
922
        gobject.source_remove(self.checker_initiator_tag)
 
923
        self.checker_initiator_tag = (gobject.timeout_add
 
924
                                      (value, self.start_checker))
 
925
        self.start_checker()    # Start one now, too
 
926
 
 
927
    # checker - property
 
928
    @dbus_service_property(_interface, signature=u"s",
 
929
                           access=u"readwrite")
 
930
    def checker_dbus_property(self, value=None):
 
931
        if value is None:       # get
 
932
            return dbus.String(self.checker_command)
 
933
        self.checker_command = value
 
934
        # Emit D-Bus signal
 
935
        self.PropertyChanged(dbus.String(u"checker"),
 
936
                             dbus.String(self.checker_command,
 
937
                                         variant_level=1))
 
938
    
 
939
    # checker_running - property
 
940
    @dbus_service_property(_interface, signature=u"b",
 
941
                           access=u"readwrite")
 
942
    def checker_running_dbus_property(self, value=None):
 
943
        if value is None:       # get
 
944
            return dbus.Boolean(self.checker is not None)
 
945
        if value:
 
946
            self.start_checker()
 
947
        else:
 
948
            self.stop_checker()
 
949
    
 
950
    # object_path - property
 
951
    @dbus_service_property(_interface, signature=u"o", access=u"read")
 
952
    def object_path_dbus_property(self):
 
953
        return self.dbus_object_path # is already a dbus.ObjectPath
 
954
    
 
955
    # secret = property
 
956
    @dbus_service_property(_interface, signature=u"ay",
 
957
                           access=u"write", byte_arrays=True)
 
958
    def secret_dbus_property(self, value):
 
959
        self.secret = str(value)
 
960
    
747
961
    del _interface
748
962
 
749
963
 
983
1197
        clients:        set of Client objects
984
1198
        gnutls_priority GnuTLS priority string
985
1199
        use_dbus:       Boolean; to emit D-Bus signals or not
986
 
        clients:        set of Client objects
987
 
        gnutls_priority GnuTLS priority string
988
 
        use_dbus:       Boolean; to emit D-Bus signals or not
989
1200
    
990
1201
    Assumes a gobject.MainLoop event loop.
991
1202
    """
1172
1383
 
1173
1384
def main():
1174
1385
    
1175
 
    ######################################################################
 
1386
    ##################################################################
1176
1387
    # Parsing of options, both command line and config file
1177
1388
    
1178
1389
    parser = optparse.OptionParser(version = "%%prog %s" % version)
1428
1639
            def GetAllClientsWithProperties(self):
1429
1640
                "D-Bus method"
1430
1641
                return dbus.Dictionary(
1431
 
                    ((c.dbus_object_path, c.GetAllProperties())
 
1642
                    ((c.dbus_object_path, c.GetAll(u""))
1432
1643
                     for c in tcp_server.clients),
1433
1644
                    signature=u"oa{sv}")
1434
1645
            
1454
1665
        if use_dbus:
1455
1666
            # Emit D-Bus signal
1456
1667
            mandos_dbus_service.ClientAdded(client.dbus_object_path,
1457
 
                                            client.GetAllProperties())
 
1668
                                            client.GetAll(u""))
1458
1669
        client.enable()
1459
1670
    
1460
1671
    tcp_server.enable()