PEP8: fix E703: statement ends with a semicolon
[nivanova/samba-autobuild/.git] / python / samba / tests / password_hash_gpgme.py
1 # Tests for Tests for source4/dsdb/samdb/ldb_modules/password_hash.c
2 #
3 # Copyright (C) Catalyst IT Ltd. 2017
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 """
20 Tests for source4/dsdb/samdb/ldb_modules/password_hash.c
21 These tests need to be run in an environment in which
22 io->ac->gpg_key_ids != NULL, so that the gpg supplemental credentials
23 are generated. The functional level needs to be >= 2008 so that the
24 kerberos newer keys are generated.
25 """
26
27
28 from samba.tests.password_hash import (
29     PassWordHashTests,
30     get_package,
31     USER_PASS,
32     USER_NAME
33 )
34 from samba.ndr import ndr_unpack
35 from samba.dcerpc import drsblobs
36 import binascii
37 from samba.tests.pso import PasswordSettings
38 import samba
39
40
41 class PassWordHashGpgmeTests(PassWordHashTests):
42
43     def setUp(self):
44         super(PassWordHashGpgmeTests, self).setUp()
45
46     def test_default_supplementalCredentials(self):
47         self.add_user()
48         if not self.lp.get("password hash gpg key ids"):
49             self.skipTest("No password hash gpg key ids, " +
50                           "Primary:SambaGPG will not be generated")
51
52         sc = self.get_supplemental_creds()
53
54         # Check that we got all the expected supplemental credentials
55         # And they are in the expected order.
56         size = len(sc.sub.packages)
57         self.assertEquals(5, size)
58         (pos, package) = get_package(sc, "Primary:Kerberos-Newer-Keys")
59         self.assertEquals(1, pos)
60         self.assertEquals("Primary:Kerberos-Newer-Keys", package.name)
61
62         (pos, package) = get_package(sc, "Primary:Kerberos")
63         self.assertEquals(2, pos)
64         self.assertEquals("Primary:Kerberos", package.name)
65
66         (pos, wd_package) = get_package(sc, "Primary:WDigest")
67         self.assertEquals(3, pos)
68         self.assertEquals("Primary:WDigest", wd_package.name)
69
70         (pos, package) = get_package(sc, "Packages")
71         self.assertEquals(4, pos)
72         self.assertEquals("Packages", package.name)
73
74         (pos, package) = get_package(sc, "Primary:SambaGPG")
75         self.assertEquals(5, pos)
76         self.assertEquals("Primary:SambaGPG", package.name)
77
78         # Check that the WDigest values are correct.
79         #
80         digests = ndr_unpack(drsblobs.package_PrimaryWDigestBlob,
81                              binascii.a2b_hex(wd_package.data))
82         self.check_wdigests(digests)
83
84     def test_supplementalCredentials_cleartext(self):
85         self.add_user(clear_text=True)
86         if not self.lp.get("password hash gpg key ids"):
87             self.skipTest("No password hash gpg key ids, " +
88                           "Primary:SambaGPG will not be generated")
89
90         sc = self.get_supplemental_creds()
91
92         # Check that we got all the expected supplemental credentials
93         # And they are in the expected order.
94         size = len(sc.sub.packages)
95         self.assertEquals(6, size)
96         (pos, package) = get_package(sc, "Primary:Kerberos-Newer-Keys")
97         self.assertEquals(1, pos)
98         self.assertEquals("Primary:Kerberos-Newer-Keys", package.name)
99
100         (pos, package) = get_package(sc, "Primary:Kerberos")
101         self.assertEquals(2, pos)
102         self.assertEquals("Primary:Kerberos", package.name)
103
104         (pos, wd_package) = get_package(sc, "Primary:WDigest")
105         self.assertEquals(3, pos)
106         self.assertEquals("Primary:WDigest", wd_package.name)
107
108         (pos, ct_package) = get_package(sc, "Primary:CLEARTEXT")
109         self.assertEquals(4, pos)
110         self.assertEquals("Primary:CLEARTEXT", ct_package.name)
111
112         (pos, package) = get_package(sc, "Packages")
113         self.assertEquals(5, pos)
114         self.assertEquals("Packages", package.name)
115
116         (pos, package) = get_package(sc, "Primary:SambaGPG")
117         self.assertEquals(6, pos)
118         self.assertEquals("Primary:SambaGPG", package.name)
119
120         # Check that the WDigest values are correct.
121         #
122         digests = ndr_unpack(drsblobs.package_PrimaryWDigestBlob,
123                              binascii.a2b_hex(wd_package.data))
124         self.check_wdigests(digests)
125
126         # Check the clear text  value is correct.
127         ct = ndr_unpack(drsblobs.package_PrimaryCLEARTEXTBlob,
128                         binascii.a2b_hex(ct_package.data))
129         self.assertEquals(USER_PASS.encode('utf-16-le'), ct.cleartext)
130
131     def assert_cleartext(self, expect_cleartext, password=None):
132         """Checks cleartext is (or isn't) returned as expected"""
133         sc = self.get_supplemental_creds()
134         if expect_cleartext:
135             (pos, ct_package) = get_package(sc, "Primary:CLEARTEXT")
136             self.assertTrue(ct_package != None, "Failed to retrieve cleartext")
137
138             # Check the clear-text value is correct.
139             ct = ndr_unpack(drsblobs.package_PrimaryCLEARTEXTBlob,
140                             binascii.a2b_hex(ct_package.data))
141             self.assertEquals(password.encode('utf-16-le'), ct.cleartext)
142         else:
143             ct_package = get_package(sc, "Primary:CLEARTEXT")
144             self.assertTrue(ct_package == None,
145                             "Got cleartext when we shouldn't have")
146
147     def test_supplementalCredentials_cleartext_pso(self):
148         """Checks that a PSO's cleartext setting can override the domain's"""
149
150         # create a user that stores plain-text passwords
151         self.add_user(clear_text=True)
152
153         # check that clear-text is present in the supplementary-credentials
154         self.assert_cleartext(expect_cleartext=True, password=USER_PASS)
155
156         # create a PSO overriding the plain-text setting & apply it to the user
157         no_plaintext_pso = PasswordSettings("no-plaintext-PSO", self.ldb,
158                                             precedence=200,
159                                             store_plaintext=False)
160         self.addCleanup(self.ldb.delete, no_plaintext_pso.dn)
161         userdn = "cn=" + USER_NAME + ",cn=users," + self.base_dn
162         no_plaintext_pso.apply_to(userdn)
163
164         # set the password to update the cleartext password stored
165         new_password = samba.generate_random_password(32, 32)
166         self.ldb.setpassword("(sAMAccountName=%s)" % USER_NAME, new_password)
167
168         # this time cleartext shouldn't be in the supplementary creds
169         self.assert_cleartext(expect_cleartext=False)
170
171         # unapply PSO, update password, and check we get the cleartext again
172         no_plaintext_pso.unapply(userdn)
173         new_password = samba.generate_random_password(32, 32)
174         self.ldb.setpassword("(sAMAccountName=%s)" % USER_NAME, new_password)
175         self.assert_cleartext(expect_cleartext=True, password=new_password)
176
177         # Now update the domain setting and check we no longer get cleartext
178         self.set_store_cleartext(False)
179         new_password = samba.generate_random_password(32, 32)
180         self.ldb.setpassword("(sAMAccountName=%s)" % USER_NAME, new_password)
181         self.assert_cleartext(expect_cleartext=False)
182
183         # create a PSO overriding the domain setting & apply it to the user
184         plaintext_pso = PasswordSettings("plaintext-PSO", self.ldb,
185                                          precedence=100, store_plaintext=True)
186         self.addCleanup(self.ldb.delete, plaintext_pso.dn)
187         plaintext_pso.apply_to(userdn)
188         new_password = samba.generate_random_password(32, 32)
189         self.ldb.setpassword("(sAMAccountName=%s)" % USER_NAME, new_password)
190         self.assert_cleartext(expect_cleartext=True, password=new_password)
191
192     def test_userPassword_multiple_hashes(self):
193         self.add_user(options=[(
194             "password hash userPassword schemes",
195             "CryptSHA512 CryptSHA256 CryptSHA512")])
196
197         sc = self.get_supplemental_creds()
198
199         # Check that we got all the expected supplemental credentials
200         # And they are in the expected order.
201         size = len(sc.sub.packages)
202         self.assertEquals(6, size)
203
204         (pos, package) = get_package(sc, "Primary:Kerberos-Newer-Keys")
205         self.assertEquals(1, pos)
206         self.assertEquals("Primary:Kerberos-Newer-Keys", package.name)
207
208         (pos, package) = get_package(sc, "Primary:Kerberos")
209         self.assertEquals(2, pos)
210         self.assertEquals("Primary:Kerberos", package.name)
211
212         (pos, wp_package) = get_package(sc, "Primary:WDigest")
213         self.assertEquals(3, pos)
214         self.assertEquals("Primary:WDigest", wp_package.name)
215
216         (pos, up_package) = get_package(sc, "Primary:userPassword")
217         self.assertEquals(4, pos)
218         self.assertEquals("Primary:userPassword", up_package.name)
219
220         (pos, package) = get_package(sc, "Packages")
221         self.assertEquals(5, pos)
222         self.assertEquals("Packages", package.name)
223
224         (pos, package) = get_package(sc, "Primary:SambaGPG")
225         self.assertEquals(6, pos)
226         self.assertEquals("Primary:SambaGPG", package.name)
227
228         # Check that the WDigest values are correct.
229         #
230         digests = ndr_unpack(drsblobs.package_PrimaryWDigestBlob,
231                              binascii.a2b_hex(wp_package.data))
232         self.check_wdigests(digests)
233
234         # Check that the userPassword hashes are computed correctly
235         # Expect three hashes to be calculated
236         up = ndr_unpack(drsblobs.package_PrimaryUserPasswordBlob,
237                         binascii.a2b_hex(up_package.data))
238         self.checkUserPassword(up, [
239             ("{CRYPT}", "6", None),
240             ("{CRYPT}", "5", None),
241             ("{CRYPT}", "6", None)
242         ])
243         self.checkNtHash(USER_PASS, up.current_nt_hash.hash)
244
245     def test_userPassword_multiple_hashes_rounds_specified(self):
246         self.add_user(options=[(
247             "password hash userPassword schemes",
248             "CryptSHA512:rounds=5120 CryptSHA256:rounds=2560 CryptSHA512:rounds=5122")])
249
250         sc = self.get_supplemental_creds()
251
252         # Check that we got all the expected supplemental credentials
253         # And they are in the expected order.
254         size = len(sc.sub.packages)
255         self.assertEquals(6, size)
256
257         (pos, package) = get_package(sc, "Primary:Kerberos-Newer-Keys")
258         self.assertEquals(1, pos)
259         self.assertEquals("Primary:Kerberos-Newer-Keys", package.name)
260
261         (pos, package) = get_package(sc, "Primary:Kerberos")
262         self.assertEquals(2, pos)
263         self.assertEquals("Primary:Kerberos", package.name)
264
265         (pos, wp_package) = get_package(sc, "Primary:WDigest")
266         self.assertEquals(3, pos)
267         self.assertEquals("Primary:WDigest", wp_package.name)
268
269         (pos, up_package) = get_package(sc, "Primary:userPassword")
270         self.assertEquals(4, pos)
271         self.assertEquals("Primary:userPassword", up_package.name)
272
273         (pos, package) = get_package(sc, "Packages")
274         self.assertEquals(5, pos)
275         self.assertEquals("Packages", package.name)
276
277         (pos, package) = get_package(sc, "Primary:SambaGPG")
278         self.assertEquals(6, pos)
279         self.assertEquals("Primary:SambaGPG", package.name)
280
281         # Check that the WDigest values are correct.
282         #
283         digests = ndr_unpack(drsblobs.package_PrimaryWDigestBlob,
284                              binascii.a2b_hex(wp_package.data))
285         self.check_wdigests(digests)
286
287         # Check that the userPassword hashes are computed correctly
288         # Expect three hashes to be calculated
289         up = ndr_unpack(drsblobs.package_PrimaryUserPasswordBlob,
290                         binascii.a2b_hex(up_package.data))
291         self.checkUserPassword(up, [
292             ("{CRYPT}", "6", 5120),
293             ("{CRYPT}", "5", 2560),
294             ("{CRYPT}", "6", 5122)
295         ])
296         self.checkNtHash(USER_PASS, up.current_nt_hash.hash)