/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: 2022-04-23 23:58:39 UTC
  • Revision ID: teddy@recompile.se-20220423235839-cnt9aq1kjveqaydc
Bug fix in mandos-ctl: handle backslashes in password

* mandos-ctl (mode=password): When sending the password to gpg, use
  "printf" instead of "echo -n".  This avoids the behavior of the
  "echo" builtin in "dash", which always interprets backslash escape
  codes.

Reported-By: Jesse Norell <jesse@kci.net>

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
# -*- mode: python; coding: utf-8 -*-
 
1
#!/usr/bin/python3 -bI
 
2
# -*- mode: python; after-save-hook: (lambda () (let ((command (if (fboundp 'file-local-name) (file-local-name (buffer-file-name)) (or (file-remote-p (buffer-file-name) 'localname) (buffer-file-name))))) (if (= (progn (if (get-buffer "*Test*") (kill-buffer "*Test*")) (process-file-shell-command (format "%s --check" (shell-quote-argument command)) nil "*Test*")) 0) (let ((w (get-buffer-window "*Test*"))) (if w (delete-window w))) (progn (with-current-buffer "*Test*" (compilation-mode)) (display-buffer "*Test*" '(display-buffer-in-side-window)))))); coding: utf-8 -*-
3
3
#
4
4
# Mandos server - give out binary blobs to connecting clients.
5
5
#
11
11
# "AvahiService" class, and some lines in "main".
12
12
#
13
13
# Everything else is
14
 
# Copyright © 2008-2019 Teddy Hogeborn
15
 
# Copyright © 2008-2019 Björn Påhlsson
 
14
# Copyright © 2008-2020 Teddy Hogeborn
 
15
# Copyright © 2008-2020 Björn Påhlsson
16
16
#
17
17
# This file is part of Mandos.
18
18
#
77
77
import itertools
78
78
import collections
79
79
import codecs
 
80
import unittest
 
81
import random
 
82
import shlex
80
83
 
81
84
import dbus
82
85
import dbus.service
90
93
 
91
94
if sys.version_info.major == 2:
92
95
    __metaclass__ = type
 
96
    str = unicode
 
97
 
 
98
# Add collections.abc.Callable if it does not exist
 
99
try:
 
100
    collections.abc.Callable
 
101
except AttributeError:
 
102
    class abc:
 
103
        Callable = collections.Callable
 
104
    collections.abc = abc
 
105
    del abc
 
106
 
 
107
# Add shlex.quote if it does not exist
 
108
try:
 
109
    shlex.quote
 
110
except AttributeError:
 
111
    shlex.quote = re.escape
 
112
 
 
113
# Show warnings by default
 
114
if not sys.warnoptions:
 
115
    import warnings
 
116
    warnings.simplefilter("default")
93
117
 
94
118
# Try to find the value of SO_BINDTODEVICE:
95
119
try:
116
140
            # No value found
117
141
            SO_BINDTODEVICE = None
118
142
 
119
 
if sys.version_info.major == 2:
120
 
    str = unicode
121
 
 
122
143
if sys.version_info < (3, 2):
123
144
    configparser.Configparser = configparser.SafeConfigParser
124
145
 
125
 
version = "1.8.7"
 
146
version = "1.8.14"
126
147
stored_state_file = "clients.pickle"
127
148
 
128
149
logger = logging.getLogger()
 
150
logging.captureWarnings(True)   # Show warnings via the logging system
129
151
syslogger = None
130
152
 
131
153
try:
167
189
        facility=logging.handlers.SysLogHandler.LOG_DAEMON,
168
190
        address="/dev/log"))
169
191
    syslogger.setFormatter(logging.Formatter
170
 
                           ('Mandos [%(process)d]: %(levelname)s:'
171
 
                            ' %(message)s'))
 
192
                           ("Mandos [%(process)d]: %(levelname)s:"
 
193
                            " %(message)s"))
172
194
    logger.addHandler(syslogger)
173
195
 
174
196
    if debug:
175
197
        console = logging.StreamHandler()
176
 
        console.setFormatter(logging.Formatter('%(asctime)s %(name)s'
177
 
                                               ' [%(process)d]:'
178
 
                                               ' %(levelname)s:'
179
 
                                               ' %(message)s'))
 
198
        console.setFormatter(logging.Formatter("%(asctime)s %(name)s"
 
199
                                               " [%(process)d]:"
 
200
                                               " %(levelname)s:"
 
201
                                               " %(message)s"))
180
202
        logger.addHandler(console)
181
203
    logger.setLevel(level)
182
204
 
196
218
            output = subprocess.check_output(["gpgconf"])
197
219
            for line in output.splitlines():
198
220
                name, text, path = line.split(b":")
199
 
                if name == "gpg":
 
221
                if name == b"gpg":
200
222
                    self.gpg = path
201
223
                    break
202
224
        except OSError as e:
203
225
            if e.errno != errno.ENOENT:
204
226
                raise
205
 
        self.gnupgargs = ['--batch',
206
 
                          '--homedir', self.tempdir,
207
 
                          '--force-mdc',
208
 
                          '--quiet']
 
227
        self.gnupgargs = ["--batch",
 
228
                          "--homedir", self.tempdir,
 
229
                          "--force-mdc",
 
230
                          "--quiet"]
209
231
        # Only GPG version 1 has the --no-use-agent option.
210
 
        if self.gpg == "gpg" or self.gpg.endswith("/gpg"):
 
232
        if self.gpg == b"gpg" or self.gpg.endswith(b"/gpg"):
211
233
            self.gnupgargs.append("--no-use-agent")
212
234
 
213
235
    def __enter__(self):
250
272
                dir=self.tempdir) as passfile:
251
273
            passfile.write(passphrase)
252
274
            passfile.flush()
253
 
            proc = subprocess.Popen([self.gpg, '--symmetric',
254
 
                                     '--passphrase-file',
 
275
            proc = subprocess.Popen([self.gpg, "--symmetric",
 
276
                                     "--passphrase-file",
255
277
                                     passfile.name]
256
278
                                    + self.gnupgargs,
257
279
                                    stdin=subprocess.PIPE,
268
290
                dir=self.tempdir) as passfile:
269
291
            passfile.write(passphrase)
270
292
            passfile.flush()
271
 
            proc = subprocess.Popen([self.gpg, '--decrypt',
272
 
                                     '--passphrase-file',
 
293
            proc = subprocess.Popen([self.gpg, "--decrypt",
 
294
                                     "--passphrase-file",
273
295
                                     passfile.name]
274
296
                                    + self.gnupgargs,
275
297
                                    stdin=subprocess.PIPE,
328
350
    Attributes:
329
351
    interface: integer; avahi.IF_UNSPEC or an interface index.
330
352
               Used to optionally bind to the specified interface.
331
 
    name: string; Example: 'Mandos'
332
 
    type: string; Example: '_mandos._tcp'.
 
353
    name: string; Example: "Mandos"
 
354
    type: string; Example: "_mandos._tcp".
333
355
     See <https://www.iana.org/assignments/service-names-port-numbers>
334
356
    port: integer; what port to announce
335
357
    TXT: list of strings; TXT record for the service
413
435
                avahi.DBUS_INTERFACE_ENTRY_GROUP)
414
436
        self.entry_group_state_changed_match = (
415
437
            self.group.connect_to_signal(
416
 
                'StateChanged', self.entry_group_state_changed))
 
438
                "StateChanged", self.entry_group_state_changed))
417
439
        logger.debug("Adding Zeroconf service '%s' of type '%s' ...",
418
440
                     self.name, self.type)
419
441
        self.group.AddService(
502
524
class AvahiServiceToSyslog(AvahiService):
503
525
    def rename(self, *args, **kwargs):
504
526
        """Add the new name to the syslog messages"""
505
 
        ret = super(AvahiServiceToSyslog, self).rename(*args, **kwargs)
 
527
        ret = super(AvahiServiceToSyslog, self).rename(*args,
 
528
                                                       **kwargs)
506
529
        syslogger.setFormatter(logging.Formatter(
507
 
            'Mandos ({}) [%(process)d]: %(levelname)s: %(message)s'
 
530
            "Mandos ({}) [%(process)d]: %(levelname)s: %(message)s"
508
531
            .format(self.name)))
509
532
        return ret
510
533
 
540
563
    OPENPGP_FMT_RAW = 0         # gnutls/openpgp.h
541
564
 
542
565
    # Types
543
 
    class session_int(ctypes.Structure):
 
566
    class _session_int(ctypes.Structure):
544
567
        _fields_ = []
545
 
    session_t = ctypes.POINTER(session_int)
 
568
    session_t = ctypes.POINTER(_session_int)
546
569
 
547
570
    class certificate_credentials_st(ctypes.Structure):
548
571
        _fields_ = []
551
574
    certificate_type_t = ctypes.c_int
552
575
 
553
576
    class datum_t(ctypes.Structure):
554
 
        _fields_ = [('data', ctypes.POINTER(ctypes.c_ubyte)),
555
 
                    ('size', ctypes.c_uint)]
 
577
        _fields_ = [("data", ctypes.POINTER(ctypes.c_ubyte)),
 
578
                    ("size", ctypes.c_uint)]
556
579
 
557
 
    class openpgp_crt_int(ctypes.Structure):
 
580
    class _openpgp_crt_int(ctypes.Structure):
558
581
        _fields_ = []
559
 
    openpgp_crt_t = ctypes.POINTER(openpgp_crt_int)
 
582
    openpgp_crt_t = ctypes.POINTER(_openpgp_crt_int)
560
583
    openpgp_crt_fmt_t = ctypes.c_int  # gnutls/openpgp.h
561
584
    log_func = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_char_p)
562
585
    credentials_type_t = ctypes.c_int
571
594
            # gnutls.strerror()
572
595
            self.code = code
573
596
            if message is None and code is not None:
574
 
                message = gnutls.strerror(code)
 
597
                message = gnutls.strerror(code).decode(
 
598
                    "utf-8", errors="replace")
575
599
            return super(gnutls.Error, self).__init__(
576
600
                message, *args)
577
601
 
578
602
    class CertificateSecurityError(Error):
579
603
        pass
580
604
 
 
605
    class PointerTo:
 
606
        def __init__(self, cls):
 
607
            self.cls = cls
 
608
 
 
609
        def from_param(self, obj):
 
610
            if not isinstance(obj, self.cls):
 
611
                raise TypeError("Not of type {}: {!r}"
 
612
                                .format(self.cls.__name__, obj))
 
613
            return ctypes.byref(obj.from_param(obj))
 
614
 
 
615
    class CastToVoidPointer:
 
616
        def __init__(self, cls):
 
617
            self.cls = cls
 
618
 
 
619
        def from_param(self, obj):
 
620
            if not isinstance(obj, self.cls):
 
621
                raise TypeError("Not of type {}: {!r}"
 
622
                                .format(self.cls.__name__, obj))
 
623
            return ctypes.cast(obj.from_param(obj), ctypes.c_void_p)
 
624
 
 
625
    class With_from_param:
 
626
        @classmethod
 
627
        def from_param(cls, obj):
 
628
            return obj._as_parameter_
 
629
 
581
630
    # Classes
582
 
    class Credentials:
 
631
    class Credentials(With_from_param):
583
632
        def __init__(self):
584
 
            self._c_object = gnutls.certificate_credentials_t()
585
 
            gnutls.certificate_allocate_credentials(
586
 
                ctypes.byref(self._c_object))
 
633
            self._as_parameter_ = gnutls.certificate_credentials_t()
 
634
            gnutls.certificate_allocate_credentials(self)
587
635
            self.type = gnutls.CRD_CERTIFICATE
588
636
 
589
637
        def __del__(self):
590
 
            gnutls.certificate_free_credentials(self._c_object)
 
638
            gnutls.certificate_free_credentials(self)
591
639
 
592
 
    class ClientSession:
 
640
    class ClientSession(With_from_param):
593
641
        def __init__(self, socket, credentials=None):
594
 
            self._c_object = gnutls.session_t()
 
642
            self._as_parameter_ = gnutls.session_t()
595
643
            gnutls_flags = gnutls.CLIENT
596
644
            if gnutls.check_version(b"3.5.6"):
597
645
                gnutls_flags |= gnutls.NO_TICKETS
598
646
            if gnutls.has_rawpk:
599
647
                gnutls_flags |= gnutls.ENABLE_RAWPK
600
 
            gnutls.init(ctypes.byref(self._c_object), gnutls_flags)
 
648
            gnutls.init(self, gnutls_flags)
601
649
            del gnutls_flags
602
 
            gnutls.set_default_priority(self._c_object)
603
 
            gnutls.transport_set_ptr(self._c_object, socket.fileno())
604
 
            gnutls.handshake_set_private_extensions(self._c_object,
605
 
                                                    True)
 
650
            gnutls.set_default_priority(self)
 
651
            gnutls.transport_set_ptr(self, socket.fileno())
 
652
            gnutls.handshake_set_private_extensions(self, True)
606
653
            self.socket = socket
607
654
            if credentials is None:
608
655
                credentials = gnutls.Credentials()
609
 
            gnutls.credentials_set(self._c_object, credentials.type,
610
 
                                   ctypes.cast(credentials._c_object,
611
 
                                               ctypes.c_void_p))
 
656
            gnutls.credentials_set(self, credentials.type,
 
657
                                   credentials)
612
658
            self.credentials = credentials
613
659
 
614
660
        def __del__(self):
615
 
            gnutls.deinit(self._c_object)
 
661
            gnutls.deinit(self)
616
662
 
617
663
        def handshake(self):
618
 
            return gnutls.handshake(self._c_object)
 
664
            return gnutls.handshake(self)
619
665
 
620
666
        def send(self, data):
621
667
            data = bytes(data)
622
668
            data_len = len(data)
623
669
            while data_len > 0:
624
 
                data_len -= gnutls.record_send(self._c_object,
625
 
                                               data[-data_len:],
 
670
                data_len -= gnutls.record_send(self, data[-data_len:],
626
671
                                               data_len)
627
672
 
628
673
        def bye(self):
629
 
            return gnutls.bye(self._c_object, gnutls.SHUT_RDWR)
 
674
            return gnutls.bye(self, gnutls.SHUT_RDWR)
630
675
 
631
676
    # Error handling functions
632
677
    def _error_code(result):
633
678
        """A function to raise exceptions on errors, suitable
634
 
        for the 'restype' attribute on ctypes functions"""
635
 
        if result >= 0:
 
679
        for the "restype" attribute on ctypes functions"""
 
680
        if result >= gnutls.E_SUCCESS:
636
681
            return result
637
682
        if result == gnutls.E_NO_CERTIFICATE_FOUND:
638
683
            raise gnutls.CertificateSecurityError(code=result)
639
684
        raise gnutls.Error(code=result)
640
685
 
641
 
    def _retry_on_error(result, func, arguments):
 
686
    def _retry_on_error(result, func, arguments,
 
687
                        _error_code=_error_code):
642
688
        """A function to retry on some errors, suitable
643
 
        for the 'errcheck' attribute on ctypes functions"""
644
 
        while result < 0:
 
689
        for the "errcheck" attribute on ctypes functions"""
 
690
        while result < gnutls.E_SUCCESS:
645
691
            if result not in (gnutls.E_INTERRUPTED, gnutls.E_AGAIN):
646
692
                return _error_code(result)
647
693
            result = func(*arguments)
652
698
 
653
699
    # Functions
654
700
    priority_set_direct = _library.gnutls_priority_set_direct
655
 
    priority_set_direct.argtypes = [session_t, ctypes.c_char_p,
 
701
    priority_set_direct.argtypes = [ClientSession, ctypes.c_char_p,
656
702
                                    ctypes.POINTER(ctypes.c_char_p)]
657
703
    priority_set_direct.restype = _error_code
658
704
 
659
705
    init = _library.gnutls_init
660
 
    init.argtypes = [ctypes.POINTER(session_t), ctypes.c_int]
 
706
    init.argtypes = [PointerTo(ClientSession), ctypes.c_int]
661
707
    init.restype = _error_code
662
708
 
663
709
    set_default_priority = _library.gnutls_set_default_priority
664
 
    set_default_priority.argtypes = [session_t]
 
710
    set_default_priority.argtypes = [ClientSession]
665
711
    set_default_priority.restype = _error_code
666
712
 
667
713
    record_send = _library.gnutls_record_send
668
 
    record_send.argtypes = [session_t, ctypes.c_void_p,
 
714
    record_send.argtypes = [ClientSession, ctypes.c_void_p,
669
715
                            ctypes.c_size_t]
670
716
    record_send.restype = ctypes.c_ssize_t
671
717
    record_send.errcheck = _retry_on_error
673
719
    certificate_allocate_credentials = (
674
720
        _library.gnutls_certificate_allocate_credentials)
675
721
    certificate_allocate_credentials.argtypes = [
676
 
        ctypes.POINTER(certificate_credentials_t)]
 
722
        PointerTo(Credentials)]
677
723
    certificate_allocate_credentials.restype = _error_code
678
724
 
679
725
    certificate_free_credentials = (
680
726
        _library.gnutls_certificate_free_credentials)
681
 
    certificate_free_credentials.argtypes = [
682
 
        certificate_credentials_t]
 
727
    certificate_free_credentials.argtypes = [Credentials]
683
728
    certificate_free_credentials.restype = None
684
729
 
685
730
    handshake_set_private_extensions = (
686
731
        _library.gnutls_handshake_set_private_extensions)
687
 
    handshake_set_private_extensions.argtypes = [session_t,
 
732
    handshake_set_private_extensions.argtypes = [ClientSession,
688
733
                                                 ctypes.c_int]
689
734
    handshake_set_private_extensions.restype = None
690
735
 
691
736
    credentials_set = _library.gnutls_credentials_set
692
 
    credentials_set.argtypes = [session_t, credentials_type_t,
693
 
                                ctypes.c_void_p]
 
737
    credentials_set.argtypes = [ClientSession, credentials_type_t,
 
738
                                CastToVoidPointer(Credentials)]
694
739
    credentials_set.restype = _error_code
695
740
 
696
741
    strerror = _library.gnutls_strerror
698
743
    strerror.restype = ctypes.c_char_p
699
744
 
700
745
    certificate_type_get = _library.gnutls_certificate_type_get
701
 
    certificate_type_get.argtypes = [session_t]
 
746
    certificate_type_get.argtypes = [ClientSession]
702
747
    certificate_type_get.restype = _error_code
703
748
 
704
749
    certificate_get_peers = _library.gnutls_certificate_get_peers
705
 
    certificate_get_peers.argtypes = [session_t,
 
750
    certificate_get_peers.argtypes = [ClientSession,
706
751
                                      ctypes.POINTER(ctypes.c_uint)]
707
752
    certificate_get_peers.restype = ctypes.POINTER(datum_t)
708
753
 
715
760
    global_set_log_function.restype = None
716
761
 
717
762
    deinit = _library.gnutls_deinit
718
 
    deinit.argtypes = [session_t]
 
763
    deinit.argtypes = [ClientSession]
719
764
    deinit.restype = None
720
765
 
721
766
    handshake = _library.gnutls_handshake
722
 
    handshake.argtypes = [session_t]
723
 
    handshake.restype = _error_code
 
767
    handshake.argtypes = [ClientSession]
 
768
    handshake.restype = ctypes.c_int
724
769
    handshake.errcheck = _retry_on_error
725
770
 
726
771
    transport_set_ptr = _library.gnutls_transport_set_ptr
727
 
    transport_set_ptr.argtypes = [session_t, transport_ptr_t]
 
772
    transport_set_ptr.argtypes = [ClientSession, transport_ptr_t]
728
773
    transport_set_ptr.restype = None
729
774
 
730
775
    bye = _library.gnutls_bye
731
 
    bye.argtypes = [session_t, close_request_t]
732
 
    bye.restype = _error_code
 
776
    bye.argtypes = [ClientSession, close_request_t]
 
777
    bye.restype = ctypes.c_int
733
778
    bye.errcheck = _retry_on_error
734
779
 
735
780
    check_version = _library.gnutls_check_version
752
797
 
753
798
        x509_crt_fmt_t = ctypes.c_int
754
799
 
755
 
        # All the function declarations below are from gnutls/abstract.h
 
800
        # All the function declarations below are from
 
801
        # gnutls/abstract.h
756
802
        pubkey_init = _library.gnutls_pubkey_init
757
803
        pubkey_init.argtypes = [ctypes.POINTER(pubkey_t)]
758
804
        pubkey_init.restype = _error_code
772
818
        pubkey_deinit.argtypes = [pubkey_t]
773
819
        pubkey_deinit.restype = None
774
820
    else:
775
 
        # All the function declarations below are from gnutls/openpgp.h
 
821
        # All the function declarations below are from
 
822
        # gnutls/openpgp.h
776
823
 
777
824
        openpgp_crt_init = _library.gnutls_openpgp_crt_init
778
825
        openpgp_crt_init.argtypes = [ctypes.POINTER(openpgp_crt_t)]
784
831
                                       openpgp_crt_fmt_t]
785
832
        openpgp_crt_import.restype = _error_code
786
833
 
787
 
        openpgp_crt_verify_self = _library.gnutls_openpgp_crt_verify_self
788
 
        openpgp_crt_verify_self.argtypes = [openpgp_crt_t, ctypes.c_uint,
789
 
                                            ctypes.POINTER(ctypes.c_uint)]
 
834
        openpgp_crt_verify_self = \
 
835
            _library.gnutls_openpgp_crt_verify_self
 
836
        openpgp_crt_verify_self.argtypes = [
 
837
            openpgp_crt_t,
 
838
            ctypes.c_uint,
 
839
            ctypes.POINTER(ctypes.c_uint),
 
840
        ]
790
841
        openpgp_crt_verify_self.restype = _error_code
791
842
 
792
843
        openpgp_crt_deinit = _library.gnutls_openpgp_crt_deinit
803
854
 
804
855
    if check_version(b"3.6.4"):
805
856
        certificate_type_get2 = _library.gnutls_certificate_type_get2
806
 
        certificate_type_get2.argtypes = [session_t, ctypes.c_int]
 
857
        certificate_type_get2.argtypes = [ClientSession, ctypes.c_int]
807
858
        certificate_type_get2.restype = _error_code
808
859
 
809
860
    # Remove non-public functions
825
876
    """A representation of a client host served by this server.
826
877
 
827
878
    Attributes:
828
 
    approved:   bool(); 'None' if not yet approved/disapproved
 
879
    approved:   bool(); None if not yet approved/disapproved
829
880
    approval_delay: datetime.timedelta(); Time to wait for approval
830
881
    approval_duration: datetime.timedelta(); Duration of one approval
831
882
    checker: multiprocessing.Process(); a running checker process used
832
 
             to see if the client lives. 'None' if no process is
 
883
             to see if the client lives. None if no process is
833
884
             running.
834
885
    checker_callback_tag: a GLib event source tag, or None
835
886
    checker_command: string; External command which is run to check
1030
1081
        if self.checker_initiator_tag is not None:
1031
1082
            GLib.source_remove(self.checker_initiator_tag)
1032
1083
        self.checker_initiator_tag = GLib.timeout_add(
1033
 
            int(self.interval.total_seconds() * 1000),
 
1084
            random.randrange(int(self.interval.total_seconds() * 1000
 
1085
                                 + 1)),
1034
1086
            self.start_checker)
1035
1087
        # Schedule a disable() when 'timeout' has passed
1036
1088
        if self.disable_initiator_tag is not None:
1046
1098
        # Read return code from connection (see call_pipe)
1047
1099
        returncode = connection.recv()
1048
1100
        connection.close()
1049
 
        self.checker.join()
 
1101
        if self.checker is not None:
 
1102
            self.checker.join()
1050
1103
        self.checker_callback_tag = None
1051
1104
        self.checker = None
1052
1105
 
1110
1163
        if self.checker is None:
1111
1164
            # Escape attributes for the shell
1112
1165
            escaped_attrs = {
1113
 
                attr: re.escape(str(getattr(self, attr)))
 
1166
                attr: shlex.quote(str(getattr(self, attr)))
1114
1167
                for attr in self.runtime_expansions}
1115
1168
            try:
1116
1169
                command = self.checker_command % escaped_attrs
1143
1196
                kwargs=popen_args)
1144
1197
            self.checker.start()
1145
1198
            self.checker_callback_tag = GLib.io_add_watch(
1146
 
                pipe[0].fileno(), GLib.IO_IN,
 
1199
                GLib.IOChannel.unix_new(pipe[0].fileno()),
 
1200
                GLib.PRIORITY_DEFAULT, GLib.IO_IN,
1147
1201
                self.checker_callback, pipe[0], command)
1148
1202
        # Re-run this periodically if run by GLib.timeout_add
1149
1203
        return True
1188
1242
        func._dbus_name = func.__name__
1189
1243
        if func._dbus_name.endswith("_dbus_property"):
1190
1244
            func._dbus_name = func._dbus_name[:-14]
1191
 
        func._dbus_get_args_options = {'byte_arrays': byte_arrays}
 
1245
        func._dbus_get_args_options = {"byte_arrays": byte_arrays}
1192
1246
        return func
1193
1247
 
1194
1248
    return decorator
1283
1337
 
1284
1338
    @dbus.service.method(dbus.INTROSPECTABLE_IFACE,
1285
1339
                         out_signature="s",
1286
 
                         path_keyword='object_path',
1287
 
                         connection_keyword='connection')
 
1340
                         path_keyword="object_path",
 
1341
                         connection_keyword="connection")
1288
1342
    def Introspect(self, object_path, connection):
1289
1343
        """Overloading of standard D-Bus method.
1290
1344
 
1404
1458
                raise ValueError("Byte arrays not supported for non-"
1405
1459
                                 "'ay' signature {!r}"
1406
1460
                                 .format(prop._dbus_signature))
1407
 
            value = dbus.ByteArray(b''.join(chr(byte)
1408
 
                                            for byte in value))
 
1461
            value = dbus.ByteArray(bytes(value))
1409
1462
        prop(value)
1410
1463
 
1411
1464
    @dbus.service.method(dbus.PROPERTIES_IFACE,
1444
1497
 
1445
1498
    @dbus.service.method(dbus.INTROSPECTABLE_IFACE,
1446
1499
                         out_signature="s",
1447
 
                         path_keyword='object_path',
1448
 
                         connection_keyword='connection')
 
1500
                         path_keyword="object_path",
 
1501
                         connection_keyword="connection")
1449
1502
    def Introspect(self, object_path, connection):
1450
1503
        """Overloading of standard D-Bus method.
1451
1504
 
1546
1599
 
1547
1600
    @dbus.service.method(dbus.INTROSPECTABLE_IFACE,
1548
1601
                         out_signature="s",
1549
 
                         path_keyword='object_path',
1550
 
                         connection_keyword='connection')
 
1602
                         path_keyword="object_path",
 
1603
                         connection_keyword="connection")
1551
1604
    def Introspect(self, object_path, connection):
1552
1605
        """Overloading of standard D-Bus method.
1553
1606
 
2219
2272
class ProxyClient:
2220
2273
    def __init__(self, child_pipe, key_id, fpr, address):
2221
2274
        self._pipe = child_pipe
2222
 
        self._pipe.send(('init', key_id, fpr, address))
 
2275
        self._pipe.send(("init", key_id, fpr, address))
2223
2276
        if not self._pipe.recv():
2224
2277
            raise KeyError(key_id or fpr)
2225
2278
 
2226
2279
    def __getattribute__(self, name):
2227
 
        if name == '_pipe':
 
2280
        if name == "_pipe":
2228
2281
            return super(ProxyClient, self).__getattribute__(name)
2229
 
        self._pipe.send(('getattr', name))
 
2282
        self._pipe.send(("getattr", name))
2230
2283
        data = self._pipe.recv()
2231
 
        if data[0] == 'data':
 
2284
        if data[0] == "data":
2232
2285
            return data[1]
2233
 
        if data[0] == 'function':
 
2286
        if data[0] == "function":
2234
2287
 
2235
2288
            def func(*args, **kwargs):
2236
 
                self._pipe.send(('funcall', name, args, kwargs))
 
2289
                self._pipe.send(("funcall", name, args, kwargs))
2237
2290
                return self._pipe.recv()[1]
2238
2291
 
2239
2292
            return func
2240
2293
 
2241
2294
    def __setattr__(self, name, value):
2242
 
        if name == '_pipe':
 
2295
        if name == "_pipe":
2243
2296
            return super(ProxyClient, self).__setattr__(name, value)
2244
 
        self._pipe.send(('setattr', name, value))
 
2297
        self._pipe.send(("setattr", name, value))
2245
2298
 
2246
2299
 
2247
2300
class ClientHandler(socketserver.BaseRequestHandler, object):
2259
2312
 
2260
2313
            session = gnutls.ClientSession(self.request)
2261
2314
 
2262
 
            # priority = ':'.join(("NONE", "+VERS-TLS1.1",
 
2315
            # priority = ":".join(("NONE", "+VERS-TLS1.1",
2263
2316
            #                       "+AES-256-CBC", "+SHA1",
2264
2317
            #                       "+COMP-NULL", "+CTYPE-OPENPGP",
2265
2318
            #                       "+DHE-DSS"))
2267
2320
            priority = self.server.gnutls_priority
2268
2321
            if priority is None:
2269
2322
                priority = "NORMAL"
2270
 
            gnutls.priority_set_direct(session._c_object,
2271
 
                                       priority.encode("utf-8"),
2272
 
                                       None)
 
2323
            gnutls.priority_set_direct(session,
 
2324
                                       priority.encode("utf-8"), None)
2273
2325
 
2274
2326
            # Start communication using the Mandos protocol
2275
2327
            # Get protocol number
2302
2354
                    except (TypeError, gnutls.Error) as error:
2303
2355
                        logger.warning("Bad certificate: %s", error)
2304
2356
                        return
2305
 
                    logger.debug("Key ID: %s", key_id)
 
2357
                    logger.debug("Key ID: %s",
 
2358
                                 key_id.decode("utf-8",
 
2359
                                               errors="replace"))
2306
2360
 
2307
2361
                else:
2308
2362
                    key_id = b""
2400
2454
    def peer_certificate(session):
2401
2455
        "Return the peer's certificate as a bytestring"
2402
2456
        try:
2403
 
            cert_type = gnutls.certificate_type_get2(session._c_object,
2404
 
                                                     gnutls.CTYPE_PEERS)
 
2457
            cert_type = gnutls.certificate_type_get2(
 
2458
                session, gnutls.CTYPE_PEERS)
2405
2459
        except AttributeError:
2406
 
            cert_type = gnutls.certificate_type_get(session._c_object)
 
2460
            cert_type = gnutls.certificate_type_get(session)
2407
2461
        if gnutls.has_rawpk:
2408
2462
            valid_cert_types = frozenset((gnutls.CRT_RAWPK,))
2409
2463
        else:
2416
2470
            return b""
2417
2471
        list_size = ctypes.c_uint(1)
2418
2472
        cert_list = (gnutls.certificate_get_peers
2419
 
                     (session._c_object, ctypes.byref(list_size)))
 
2473
                     (session, ctypes.byref(list_size)))
2420
2474
        if not bool(cert_list) and list_size.value != 0:
2421
2475
            raise gnutls.Error("error getting peer certificate")
2422
2476
        if list_size.value == 0:
2444
2498
        buf = ctypes.create_string_buffer(32)
2445
2499
        buf_len = ctypes.c_size_t(len(buf))
2446
2500
        # Get the key ID from the raw public key into the buffer
2447
 
        gnutls.pubkey_get_key_id(pubkey,
2448
 
                                 gnutls.KEYID_USE_SHA256,
2449
 
                                 ctypes.cast(ctypes.byref(buf),
2450
 
                                             ctypes.POINTER(ctypes.c_ubyte)),
2451
 
                                 ctypes.byref(buf_len))
 
2501
        gnutls.pubkey_get_key_id(
 
2502
            pubkey,
 
2503
            gnutls.KEYID_USE_SHA256,
 
2504
            ctypes.cast(ctypes.byref(buf),
 
2505
                        ctypes.POINTER(ctypes.c_ubyte)),
 
2506
            ctypes.byref(buf_len))
2452
2507
        # Deinit the certificate
2453
2508
        gnutls.pubkey_deinit(pubkey)
2454
2509
 
2535
2590
 
2536
2591
class IPv6_TCPServer(MultiprocessingMixInWithPipe,
2537
2592
                     socketserver.TCPServer):
2538
 
    """IPv6-capable TCP server.  Accepts 'None' as address and/or port
 
2593
    """IPv6-capable TCP server.  Accepts None as address and/or port
2539
2594
 
2540
2595
    Attributes:
2541
2596
        enabled:        Boolean; whether this server is activated yet
2673
2728
    def add_pipe(self, parent_pipe, proc):
2674
2729
        # Call "handle_ipc" for both data and EOF events
2675
2730
        GLib.io_add_watch(
2676
 
            parent_pipe.fileno(),
2677
 
            GLib.IO_IN | GLib.IO_HUP,
 
2731
            GLib.IOChannel.unix_new(parent_pipe.fileno()),
 
2732
            GLib.PRIORITY_DEFAULT, GLib.IO_IN | GLib.IO_HUP,
2678
2733
            functools.partial(self.handle_ipc,
2679
2734
                              parent_pipe=parent_pipe,
2680
2735
                              proc=proc))
2693
2748
        request = parent_pipe.recv()
2694
2749
        command = request[0]
2695
2750
 
2696
 
        if command == 'init':
 
2751
        if command == "init":
2697
2752
            key_id = request[1].decode("ascii")
2698
2753
            fpr = request[2].decode("ascii")
2699
2754
            address = request[3]
2700
2755
 
2701
2756
            for c in self.clients.values():
2702
 
                if key_id == "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855":
 
2757
                if key_id == ("E3B0C44298FC1C149AFBF4C8996FB924"
 
2758
                              "27AE41E4649B934CA495991B7852B855"):
2703
2759
                    continue
2704
2760
                if key_id and c.key_id == key_id:
2705
2761
                    client = c
2718
2774
                return False
2719
2775
 
2720
2776
            GLib.io_add_watch(
2721
 
                parent_pipe.fileno(),
2722
 
                GLib.IO_IN | GLib.IO_HUP,
 
2777
                GLib.IOChannel.unix_new(parent_pipe.fileno()),
 
2778
                GLib.PRIORITY_DEFAULT, GLib.IO_IN | GLib.IO_HUP,
2723
2779
                functools.partial(self.handle_ipc,
2724
2780
                                  parent_pipe=parent_pipe,
2725
2781
                                  proc=proc,
2728
2784
            # remove the old hook in favor of the new above hook on
2729
2785
            # same fileno
2730
2786
            return False
2731
 
        if command == 'funcall':
 
2787
        if command == "funcall":
2732
2788
            funcname = request[1]
2733
2789
            args = request[2]
2734
2790
            kwargs = request[3]
2735
2791
 
2736
 
            parent_pipe.send(('data', getattr(client_object,
 
2792
            parent_pipe.send(("data", getattr(client_object,
2737
2793
                                              funcname)(*args,
2738
2794
                                                        **kwargs)))
2739
2795
 
2740
 
        if command == 'getattr':
 
2796
        if command == "getattr":
2741
2797
            attrname = request[1]
2742
2798
            if isinstance(client_object.__getattribute__(attrname),
2743
 
                          collections.Callable):
2744
 
                parent_pipe.send(('function', ))
 
2799
                          collections.abc.Callable):
 
2800
                parent_pipe.send(("function", ))
2745
2801
            else:
2746
2802
                parent_pipe.send((
2747
 
                    'data', client_object.__getattribute__(attrname)))
 
2803
                    "data", client_object.__getattribute__(attrname)))
2748
2804
 
2749
 
        if command == 'setattr':
 
2805
        if command == "setattr":
2750
2806
            attrname = request[1]
2751
2807
            value = request[2]
2752
2808
            setattr(client_object, attrname, value)
2757
2813
def rfc3339_duration_to_delta(duration):
2758
2814
    """Parse an RFC 3339 "duration" and return a datetime.timedelta
2759
2815
 
2760
 
    >>> rfc3339_duration_to_delta("P7D")
2761
 
    datetime.timedelta(7)
2762
 
    >>> rfc3339_duration_to_delta("PT60S")
2763
 
    datetime.timedelta(0, 60)
2764
 
    >>> rfc3339_duration_to_delta("PT60M")
2765
 
    datetime.timedelta(0, 3600)
2766
 
    >>> rfc3339_duration_to_delta("PT24H")
2767
 
    datetime.timedelta(1)
2768
 
    >>> rfc3339_duration_to_delta("P1W")
2769
 
    datetime.timedelta(7)
2770
 
    >>> rfc3339_duration_to_delta("PT5M30S")
2771
 
    datetime.timedelta(0, 330)
2772
 
    >>> rfc3339_duration_to_delta("P1DT3M20S")
2773
 
    datetime.timedelta(1, 200)
 
2816
    >>> timedelta = datetime.timedelta
 
2817
    >>> rfc3339_duration_to_delta("P7D") == timedelta(7)
 
2818
    True
 
2819
    >>> rfc3339_duration_to_delta("PT60S") == timedelta(0, 60)
 
2820
    True
 
2821
    >>> rfc3339_duration_to_delta("PT60M") == timedelta(0, 3600)
 
2822
    True
 
2823
    >>> rfc3339_duration_to_delta("PT24H") == timedelta(1)
 
2824
    True
 
2825
    >>> rfc3339_duration_to_delta("P1W") == timedelta(7)
 
2826
    True
 
2827
    >>> rfc3339_duration_to_delta("PT5M30S") == timedelta(0, 330)
 
2828
    True
 
2829
    >>> rfc3339_duration_to_delta("P1DT3M20S") == timedelta(1, 200)
 
2830
    True
 
2831
    >>> del timedelta
2774
2832
    """
2775
2833
 
2776
2834
    # Parsing an RFC 3339 duration with regular expressions is not
2856
2914
def string_to_delta(interval):
2857
2915
    """Parse a string and return a datetime.timedelta
2858
2916
 
2859
 
    >>> string_to_delta('7d')
2860
 
    datetime.timedelta(7)
2861
 
    >>> string_to_delta('60s')
2862
 
    datetime.timedelta(0, 60)
2863
 
    >>> string_to_delta('60m')
2864
 
    datetime.timedelta(0, 3600)
2865
 
    >>> string_to_delta('24h')
2866
 
    datetime.timedelta(1)
2867
 
    >>> string_to_delta('1w')
2868
 
    datetime.timedelta(7)
2869
 
    >>> string_to_delta('5m 30s')
2870
 
    datetime.timedelta(0, 330)
 
2917
    >>> string_to_delta("7d") == datetime.timedelta(7)
 
2918
    True
 
2919
    >>> string_to_delta("60s") == datetime.timedelta(0, 60)
 
2920
    True
 
2921
    >>> string_to_delta("60m") == datetime.timedelta(0, 3600)
 
2922
    True
 
2923
    >>> string_to_delta("24h") == datetime.timedelta(1)
 
2924
    True
 
2925
    >>> string_to_delta("1w") == datetime.timedelta(7)
 
2926
    True
 
2927
    >>> string_to_delta("5m 30s") == datetime.timedelta(0, 330)
 
2928
    True
2871
2929
    """
2872
2930
 
2873
2931
    try:
2975
3033
 
2976
3034
    options = parser.parse_args()
2977
3035
 
2978
 
    if options.check:
2979
 
        import doctest
2980
 
        fail_count, test_count = doctest.testmod()
2981
 
        sys.exit(os.EX_OK if fail_count == 0 else 1)
2982
 
 
2983
3036
    # Default values for config file for server-global settings
2984
3037
    if gnutls.has_rawpk:
2985
3038
        priority = ("SECURE128:!CTYPE-X.509:+CTYPE-RAWPK:!RSA"
3081
3134
 
3082
3135
    if server_settings["servicename"] != "Mandos":
3083
3136
        syslogger.setFormatter(
3084
 
            logging.Formatter('Mandos ({}) [%(process)d]:'
3085
 
                              ' %(levelname)s: %(message)s'.format(
 
3137
            logging.Formatter("Mandos ({}) [%(process)d]:"
 
3138
                              " %(levelname)s: %(message)s".format(
3086
3139
                                  server_settings["servicename"])))
3087
3140
 
3088
3141
    # Parse config file with clients
3148
3201
 
3149
3202
        @gnutls.log_func
3150
3203
        def debug_gnutls(level, string):
3151
 
            logger.debug("GnuTLS: %s", string[:-1])
 
3204
            logger.debug("GnuTLS: %s",
 
3205
                         string[:-1].decode("utf-8",
 
3206
                                            errors="replace"))
3152
3207
 
3153
3208
        gnutls.global_set_log_function(debug_gnutls)
3154
3209
 
3248
3303
                             if isinstance(s, bytes)
3249
3304
                             else s) for s in
3250
3305
                            value["client_structure"]]
3251
 
                        # .name & .host
3252
 
                        for k in ("name", "host"):
 
3306
                        # .name, .host, and .checker_command
 
3307
                        for k in ("name", "host", "checker_command"):
3253
3308
                            if isinstance(value[k], bytes):
3254
3309
                                value[k] = value[k].decode("utf-8")
3255
3310
                        if "key_id" not in value:
3265
3320
                        for key, value in
3266
3321
                        bytes_old_client_settings.items()}
3267
3322
                    del bytes_old_client_settings
3268
 
                    # .host
 
3323
                    # .host and .checker_command
3269
3324
                    for value in old_client_settings.values():
3270
 
                        if isinstance(value["host"], bytes):
3271
 
                            value["host"] = (value["host"]
3272
 
                                             .decode("utf-8"))
 
3325
                        for attribute in ("host", "checker_command"):
 
3326
                            if isinstance(value[attribute], bytes):
 
3327
                                value[attribute] = (value[attribute]
 
3328
                                                    .decode("utf-8"))
3273
3329
            os.remove(stored_state_path)
3274
3330
        except IOError as e:
3275
3331
            if e.errno == errno.ENOENT:
3528
3584
 
3529
3585
        try:
3530
3586
            with tempfile.NamedTemporaryFile(
3531
 
                    mode='wb',
 
3587
                    mode="wb",
3532
3588
                    suffix=".pickle",
3533
 
                    prefix='clients-',
 
3589
                    prefix="clients-",
3534
3590
                    dir=os.path.dirname(stored_state_path),
3535
3591
                    delete=False) as stored_state:
3536
3592
                pickle.dump((clients, client_settings), stored_state,
3600
3656
                sys.exit(1)
3601
3657
            # End of Avahi example code
3602
3658
 
3603
 
        GLib.io_add_watch(tcp_server.fileno(), GLib.IO_IN,
3604
 
                          lambda *args, **kwargs:
3605
 
                          (tcp_server.handle_request
3606
 
                           (*args[2:], **kwargs) or True))
 
3659
        GLib.io_add_watch(
 
3660
            GLib.IOChannel.unix_new(tcp_server.fileno()),
 
3661
            GLib.PRIORITY_DEFAULT, GLib.IO_IN,
 
3662
            lambda *args, **kwargs: (tcp_server.handle_request
 
3663
                                     (*args[2:], **kwargs) or True))
3607
3664
 
3608
3665
        logger.debug("Starting main loop")
3609
3666
        main_loop.run()
3619
3676
    # Must run before the D-Bus bus name gets deregistered
3620
3677
    cleanup()
3621
3678
 
3622
 
 
3623
 
if __name__ == '__main__':
3624
 
    main()
 
3679
 
 
3680
def should_only_run_tests():
 
3681
    parser = argparse.ArgumentParser(add_help=False)
 
3682
    parser.add_argument("--check", action="store_true")
 
3683
    args, unknown_args = parser.parse_known_args()
 
3684
    run_tests = args.check
 
3685
    if run_tests:
 
3686
        # Remove --check argument from sys.argv
 
3687
        sys.argv[1:] = unknown_args
 
3688
    return run_tests
 
3689
 
 
3690
# Add all tests from doctest strings
 
3691
def load_tests(loader, tests, none):
 
3692
    import doctest
 
3693
    tests.addTests(doctest.DocTestSuite())
 
3694
    return tests
 
3695
 
 
3696
if __name__ == "__main__":
 
3697
    try:
 
3698
        if should_only_run_tests():
 
3699
            # Call using ./mandos --check [--verbose]
 
3700
            unittest.main()
 
3701
        else:
 
3702
            main()
 
3703
    finally:
 
3704
        logging.shutdown()