1 # -*- coding: utf-8 -*-
3 # Unix SMB/CIFS implementation.
4 # Copyright © Andrew Bartlett <abartlet@samba.org> 2021
5 # Copyright (C) Catalyst IT Ltd. 2017
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 """Tests for samba.dcerpc.lsa."""
23 from samba
.dcerpc
import lsa
24 from samba
.credentials
import Credentials
25 from samba
.tests
import TestCase
26 from samba
.dcerpc
.security
import dom_sid
27 from samba
import NTSTATUSError
28 from samba
.ntstatus
import NT_STATUS_ACCESS_DENIED
31 class LsaTests(TestCase
):
34 self
.lp
= self
.get_loadparm()
35 self
.server
= samba
.tests
.env_get_var_value('SERVER')
37 def test_lsa_LookupSids3_multiple(self
):
38 machine_creds
= Credentials()
39 machine_creds
.guess(self
.lp
)
40 machine_creds
.set_machine_account()
43 "ncacn_ip_tcp:%s[schannel,seal]" % self
.server
,
50 x
= dom_sid("S-1-5-7")
54 names
= lsa
.TransNameArray2()
55 level
= lsa
.LSA_LOOKUP_NAMES_ALL
57 lookup_options
= lsa
.LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
58 client_revision
= lsa
.LSA_CLIENT_REVISION_2
60 # We want to run LookupSids3 multiple times on the same
61 # connection as we have code to re-use the sam.ldb and we need
62 # to check things work for the second request.
63 (domains
, names
, count
) = c
.LookupSids3(sids
, names
, level
, count
, lookup_options
, client_revision
)
64 self
.assertEqual(count
, 1)
65 self
.assertEqual(names
.count
, 1)
66 self
.assertEqual(names
.names
[0].name
.string
,
68 (domains2
, names2
, count2
) = c
.LookupSids3(sids
, names
, level
, count
, lookup_options
, client_revision
)
69 self
.assertEqual(count2
, 1)
70 self
.assertEqual(names2
.count
, 1)
71 self
.assertEqual(names2
.names
[0].name
.string
,
74 # Just looking for any exceptions in the last couple of loops
75 c
.LookupSids3(sids
, names
, level
, count
, lookup_options
, client_revision
)
76 c
.LookupSids3(sids
, names
, level
, count
, lookup_options
, client_revision
)
78 def test_lsa_LookupSids3_multiple_conns(self
):
79 machine_creds
= Credentials()
80 machine_creds
.guess(self
.lp
)
81 machine_creds
.set_machine_account()
84 "ncacn_ip_tcp:%s[schannel,seal]" % self
.server
,
91 x
= dom_sid("S-1-5-7")
95 names
= lsa
.TransNameArray2()
96 level
= lsa
.LSA_LOOKUP_NAMES_ALL
98 lookup_options
= lsa
.LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
99 client_revision
= lsa
.LSA_CLIENT_REVISION_2
101 # We want to run LookupSids3, and then again on a new
102 # connection to show that we don't have an issue with the DB
103 # being tied to the wrong connection.
104 (domains
, names
, count
) = c
.LookupSids3(sids
,
110 self
.assertEqual(count
, 1)
111 self
.assertEqual(names
.count
, 1)
112 self
.assertEqual(names
.names
[0].name
.string
,
116 "ncacn_ip_tcp:%s[schannel,seal]" % self
.server
,
120 (domains
, names
, count
) = c
.LookupSids3(sids
,
126 self
.assertEqual(count
, 1)
127 self
.assertEqual(names
.count
, 1)
128 self
.assertEqual(names
.names
[0].name
.string
,
132 def test_lsa_LookupNames4_LookupSids3_multiple(self
):
134 Test by going back and forward between real DB lookups
135 name->sid->name to ensure the sam.ldb handle is fine once
139 machine_creds
= Credentials()
140 machine_creds
.guess(self
.lp
)
141 machine_creds
.set_machine_account()
143 c_normal
= lsa
.lsarpc(
144 "ncacn_np:%s[seal]" % self
.server
,
148 username
, domain
= c_normal
.GetUserName(None, None, None)
151 "ncacn_ip_tcp:%s[schannel,seal]" % self
.server
,
155 sids
= lsa
.TransSidArray3()
157 level
= lsa
.LSA_LOOKUP_NAMES_ALL
159 lookup_options
= lsa
.LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
160 client_revision
= lsa
.LSA_CLIENT_REVISION_2
161 (domains
, sids
, count
) = c
.LookupNames4(names
,
168 # Another lookup on the same connection, will re-used the
169 # server-side implicit state handle on the connection
170 (domains
, sids
, count
) = c
.LookupNames4(names
,
177 self
.assertEqual(count
, 1)
178 self
.assertEqual(sids
.count
, 1)
180 # Now look the SIDs back up
181 names
= lsa
.TransNameArray2()
183 sid
.sid
= sids
.sids
[0].sid
184 lookup_sids
= lsa
.SidArray()
185 lookup_sids
.sids
= [sid
]
186 lookup_sids
.num_sids
= 1
187 level
= lsa
.LSA_LOOKUP_NAMES_ALL
190 client_revision
= lsa
.LSA_CLIENT_REVISION_2
192 (domains
, names
, count
) = c
.LookupSids3(lookup_sids
,
198 self
.assertEqual(count
, 1)
199 self
.assertEqual(names
.count
, 1)
200 self
.assertEqual(names
.names
[0].name
.string
,
203 # And once more just to be sure, just checking for a fault
204 sids
= lsa
.TransSidArray3()
206 level
= lsa
.LSA_LOOKUP_NAMES_ALL
208 lookup_options
= lsa
.LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
209 client_revision
= lsa
.LSA_CLIENT_REVISION_2
210 (domains
, sids
, count
) = c
.LookupNames4(names
,
218 def test_lsa_LookupNames4_multiple_conns(self
):
220 Test by going back and forward between real DB lookups
221 name->sid->name to ensure the sam.ldb handle is fine once
225 machine_creds
= Credentials()
226 machine_creds
.guess(self
.lp
)
227 machine_creds
.set_machine_account()
229 c_normal
= lsa
.lsarpc(
230 "ncacn_np:%s[seal]" % self
.server
,
234 username
, domain
= c_normal
.GetUserName(None, None, None)
237 "ncacn_ip_tcp:%s[schannel,seal]" % self
.server
,
241 sids
= lsa
.TransSidArray3()
243 level
= lsa
.LSA_LOOKUP_NAMES_ALL
245 lookup_options
= lsa
.LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
246 client_revision
= lsa
.LSA_CLIENT_REVISION_2
247 (domains
, sids
, count
) = c
.LookupNames4(names
,
255 "ncacn_ip_tcp:%s[schannel,seal]" % self
.server
,
259 sids
= lsa
.TransSidArray3()
261 level
= lsa
.LSA_LOOKUP_NAMES_ALL
263 lookup_options
= lsa
.LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
264 client_revision
= lsa
.LSA_CLIENT_REVISION_2
265 (domains
, sids
, count
) = c
.LookupNames4(names
,
272 def test_lsa_LookupNames4_without_schannel(self
):
274 machine_creds
= Credentials()
275 machine_creds
.guess(self
.lp
)
276 machine_creds
.set_machine_account()
278 c_normal
= lsa
.lsarpc(
279 "ncacn_np:%s[seal]" % self
.server
,
283 username
, domain
= c_normal
.GetUserName(None, None, None)
285 sids
= lsa
.TransSidArray3()
287 level
= lsa
.LSA_LOOKUP_NAMES_ALL
289 lookup_options
= lsa
.LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
290 client_revision
= lsa
.LSA_CLIENT_REVISION_2
292 with self
.assertRaises(NTSTATUSError
) as e
:
293 c_normal
.LookupNames4(names
,
299 if (e
.exception
.args
[0] != NT_STATUS_ACCESS_DENIED
):
300 raise AssertionError("LookupNames4 without schannel must fail with ACCESS_DENIED")
302 def test_lsa_LookupSids3_without_schannel(self
):
303 machine_creds
= Credentials()
304 machine_creds
.guess(self
.lp
)
305 machine_creds
.set_machine_account()
308 "ncacn_ip_tcp:%s[seal]" % self
.server
,
312 sids
= lsa
.SidArray()
315 x
= dom_sid("S-1-5-7")
319 names
= lsa
.TransNameArray2()
320 level
= lsa
.LSA_LOOKUP_NAMES_ALL
322 lookup_options
= lsa
.LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
323 client_revision
= lsa
.LSA_CLIENT_REVISION_2
325 with self
.assertRaises(NTSTATUSError
) as e
:
332 if (e
.exception
.args
[0] != NT_STATUS_ACCESS_DENIED
):
333 raise AssertionError("LookupSids3 without schannel must fail with ACCESS_DENIED")