summaryrefslogtreecommitdiffstats
path: root/python/samba/tests/dcerpc/binding.py
blob: 1ad1f2964c6b5e186d5914cf284a06d57e103fca (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
#
# Unix SMB/CIFS implementation.
# Copyright (c) 2020      Andreas Schneider <asn@samba.org>
#
# 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 samba.dcerpc., credentials and binding strings"""

import samba.tests
from samba.tests import RpcInterfaceTestCase, TestCase
from samba.dcerpc import lsa
import samba.dcerpc.security as security
from samba.credentials import Credentials, SMB_ENCRYPTION_REQUIRED, SMB_ENCRYPTION_OFF
from samba import NTSTATUSError

class RpcBindingTests(RpcInterfaceTestCase):

    def get_user_creds(self):
        c = Credentials()
        c.guess()
        domain = samba.tests.env_get_var_value('DOMAIN')
        username = samba.tests.env_get_var_value('USERNAME')
        password = samba.tests.env_get_var_value('PASSWORD')
        c.set_domain(domain)
        c.set_username(username)
        c.set_password(password)
        return c

    def test_smb3_dcerpc_no_encryption(self):
        creds = self.get_user_creds()
        creds.set_smb_encryption(SMB_ENCRYPTION_OFF)

        lp = self.get_loadparm()
        lp.set('client ipc max protocol', 'SMB3')
        lp.set('client ipc min protocol', 'SMB3')

        binding_string = ("ncacn_np:%s" % (samba.tests.env_get_var_value('SERVER')))
        lsa_conn = lsa.lsarpc(binding_string, lp, creds)
        self.assertFalse(lsa_conn.transport_encrypted())

        objectAttr = lsa.ObjectAttribute()
        objectAttr.sec_qos = lsa.QosInfo()

        pol_handle = lsa_conn.OpenPolicy2('',
                                          objectAttr,
                                          security.SEC_FLAG_MAXIMUM_ALLOWED)
        self.assertIsNotNone(pol_handle)

    def test_smb3_dcerpc_encryption(self):
        creds = self.get_user_creds()
        creds.set_smb_encryption(SMB_ENCRYPTION_REQUIRED)

        lp = self.get_loadparm()
        lp.set('client ipc max protocol', 'SMB3')
        lp.set('client ipc min protocol', 'SMB3')

        binding_string = ("ncacn_np:%s" % (samba.tests.env_get_var_value('SERVER')))
        lsa_conn = lsa.lsarpc(binding_string, lp, creds)
        self.assertTrue(lsa_conn.transport_encrypted())

        objectAttr = lsa.ObjectAttribute()
        objectAttr.sec_qos = lsa.QosInfo()

        pol_handle = lsa_conn.OpenPolicy2('',
                                          objectAttr,
                                          security.SEC_FLAG_MAXIMUM_ALLOWED)
        self.assertIsNotNone(pol_handle)

    def test_smb2_dcerpc_encryption(self):
        creds = self.get_user_creds()
        creds.set_smb_encryption(SMB_ENCRYPTION_REQUIRED)

        lp = self.get_loadparm()
        lp.set('client ipc max protocol', 'SMB2')
        lp.set('client ipc min protocol', 'SMB2')

        binding_string = ("ncacn_np:%s" % (samba.tests.env_get_var_value('SERVER')))
        self.assertRaises(NTSTATUSError, lsa.lsarpc, binding_string, lp, creds)

    def test_smb1_dcerpc_encryption(self):
        creds = self.get_user_creds()
        creds.set_smb_encryption(SMB_ENCRYPTION_REQUIRED)

        lp = self.get_loadparm()
        lp.set('client ipc max protocol', 'NT1')
        lp.set('client ipc min protocol', 'NT1')

        binding_string = ("ncacn_np:%s" % (samba.tests.env_get_var_value('SERVER')))
        self.assertRaises(NTSTATUSError, lsa.lsarpc, binding_string, lp, creds)