summaryrefslogtreecommitdiffstats
path: root/python/samba/tests/auth_log_base.py
blob: 131f019de43cd5e11d9feafab47b3da04637e597 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Unix SMB/CIFS implementation.
# Copyright (C) Andrew Bartlett <abartlet@samba.org> 2017
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""Tests for the Auth and AuthZ logging.
"""

import samba.tests
from samba.messaging import Messaging
from samba.dcerpc.messaging import MSG_AUTH_LOG, AUTH_EVENT_NAME
from samba.param import LoadParm
import time
import json
import os
import re


def default_msg_filter(msg):
    # When our authentication logging tests were written, these were the only
    # supported message types. The tests were built on the assumption that no
    # new types would be added, and violating this assumption will result in
    # many tests failing as they receive messages that they weren’t
    # expecting. To allow these tests to continue to pass, this default filter
    # makes sure that only messages for which the tests are prepared pass
    # though.
    default_supported_types = {
        "Authentication",
        "Authorization",
    }

    return msg['type'] in default_supported_types


class NoMessageException(Exception):
    pass


class AuthLogTestBase(samba.tests.TestCase):

    @classmethod
    def setUpClass(cls):
        super().setUpClass()

        # connect to the server's messaging bus (we need to explicitly load a
        # different smb.conf here, because in all other respects this test
        # wants to act as a separate remote client)
        server_conf = os.getenv('SERVERCONFFILE')
        if server_conf:
            lp_ctx = LoadParm(filename_for_non_global_lp=server_conf)
        else:
            lp_ctx = samba.tests.env_loadparm()
        cls.msg_ctx = Messaging((1,), lp_ctx=lp_ctx)
        cls.msg_ctx.irpc_add_name(AUTH_EVENT_NAME)

        # Now switch back to using the client-side smb.conf. The tests will
        # use the first interface in the client.conf (we need to strip off
        # the subnet mask portion)
        lp_ctx = samba.tests.env_loadparm()
        client_ip_and_mask = lp_ctx.get('interfaces')[0]
        client_ip = client_ip_and_mask.split('/')[0]

        # the messaging ctx is the server's view of the world, so our own
        # client IP will be the remoteAddress when connections are logged
        cls.remoteAddress = client_ip

        def messageHandler(context, msgType, src, message):
            # This does not look like sub unit output and it
            # makes these tests much easier to debug.
            print(message)
            jsonMsg = json.loads(message)
            context["messages"].append(jsonMsg)

        cls.context = {"messages": []}
        cls.msg_handler_and_context = (messageHandler, cls.context)
        cls.msg_ctx.register(cls.msg_handler_and_context,
                             msg_type=MSG_AUTH_LOG)

        cls.server = os.environ["SERVER"]
        cls.connection = None

    @classmethod
    def tearDownClass(cls):
        cls.msg_ctx.deregister(cls.msg_handler_and_context,
                               msg_type=MSG_AUTH_LOG)
        cls.msg_ctx.irpc_remove_name(AUTH_EVENT_NAME)

        super().tearDownClass()

    def setUp(self):
        super().setUp()
        type(self).discardMessages()

    def isRemote(self, message):
        if self.remoteAddress is None:
            return True

        supported_types = {
            "Authentication",
            "Authorization",
            "KDC Authorization",
        }
        message_type = message["type"]
        if message_type in supported_types:
            remote = message[message_type]["remoteAddress"]
        else:
            return False

        try:
            addr = remote.split(":")
            return addr[1] == self.remoteAddress
        except IndexError:
            return False

    def waitForMessages(self, isLastExpectedMessage, connection=None, *,
                        msgFilter=default_msg_filter):
        """Wait for all the expected messages to arrive
        The connection is passed through to keep the connection alive
        until all the logging messages have been received.

        By default, only Authentication and Authorization messages will be
        returned, so that old tests continue to pass. To receive all messages,
        pass msgFilter=None.

        """

        messages = []
        while True:
            try:
                msg = self.nextMessage(msgFilter=msgFilter)
            except NoMessageException:
                return []

            messages.append(msg)
            if isLastExpectedMessage(msg):
                return messages

    def nextMessage(self, msgFilter=None):
        """Return the next relevant message, or throw a NoMessageException."""
        def is_relevant(msg):
            if not self.isRemote(msg):
                return False

            if msgFilter is None:
                return True

            return msgFilter(msg)

        messages = self.context['messages']

        while True:
            timeout = 2
            until = time.time() + timeout

            while not messages:
                # Fetch a new message from the messaging bus.

                current = time.time()
                if until < current:
                    break

                self.msg_ctx.loop_once(until - current)

            if not messages:
                raise NoMessageException('timed out looking for a message')

            # Grab the next message from the queue.
            msg = messages.pop(0)
            if is_relevant(msg):
                return msg

    # Discard any previously queued messages.
    @classmethod
    def discardMessages(cls):
        messages = cls.context["messages"]

        while True:
            messages.clear()

            # tevent presumably has other tasks to run, so we might need two or
            # three loops before a message comes through.
            for _ in range(5):
                cls.msg_ctx.loop_once(0.001)

            if not messages:
                # No new messages. We’ve probably got them all.
                break

    # Remove any NETLOGON authentication messages
    # NETLOGON is only performed once per session, so to avoid ordering
    # dependencies within the tests it's best to strip out NETLOGON messages.
    #
    def remove_netlogon_messages(self, messages):
        def is_not_netlogon(msg):
            if "Authentication" not in msg:
                return True
            sd = msg["Authentication"]["serviceDescription"]
            return sd != "NETLOGON"

        return list(filter(is_not_netlogon, messages))

    GUID_RE = re.compile(
        "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")

    #
    # Is the supplied GUID string correctly formatted
    #
    def is_guid(self, guid):
        return self.GUID_RE.fullmatch(guid)