traffic: new version of model with packet_rate, version number
[samba.git] / python / samba / tests / password_hash_ldap.py
1 # Tests for Tests for source4/dsdb/samdb/ldb_modules/password_hash.c
2 #
3 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 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
22 These tests are designed to also run against Windows to confirm the values
23 returned from Windows.
24
25 To run against Windows:
26 Set the following environment variables:
27    PASSWORD=Administrator password
28    USERNAME=Administrator
29    SMB_CONF_PATH=/dev/null
30    PYTHONPATH=bin/python
31    SERVER=Windows server IP
32
33    /usr/bin/python source4/scripting/bin/subunitrun
34        samba.tests.password_hash_ldap.PassWordHashLDAPTests
35        -U"Administrator%adminpassword"
36 """
37
38 from samba.tests.password_hash import (
39     PassWordHashTests,
40     get_package,
41     USER_NAME,
42     USER_PASS
43 )
44 from samba.samdb import SamDB
45 from samba.ndr import ndr_unpack
46 from samba.dcerpc import drsblobs, drsuapi, misc
47 from samba import drs_utils, net
48 from samba.credentials import Credentials
49 from samba.compat import text_type
50 import binascii
51 import os
52
53
54 def attid_equal(a1, a2):
55     return (a1 & 0xffffffff) == (a2 & 0xffffffff)
56
57
58 class PassWordHashLDAPTests(PassWordHashTests):
59
60     def setUp(self):
61         super(PassWordHashLDAPTests, self).setUp()
62
63     # Get the supplemental credentials for the user under test
64     def get_supplemental_creds_drs(self):
65         binding_str = "ncacn_ip_tcp:%s[seal]" % os.environ["SERVER"]
66         dn = "cn=" + USER_NAME + ",cn=users," + self.base_dn
67         drs = drsuapi.drsuapi(binding_str, self.get_loadparm(), self.creds)
68         (drs_handle, supported_extensions) = drs_utils.drs_DsBind(drs)
69
70         req8 = drsuapi.DsGetNCChangesRequest8()
71
72         null_guid = misc.GUID()
73         req8.destination_dsa_guid          = null_guid
74         req8.source_dsa_invocation_id      = null_guid
75         req8.naming_context                = drsuapi.DsReplicaObjectIdentifier()
76         req8.naming_context.dn             = text_type(dn)
77
78         req8.highwatermark = drsuapi.DsReplicaHighWaterMark()
79         req8.highwatermark.tmp_highest_usn = 0
80         req8.highwatermark.reserved_usn    = 0
81         req8.highwatermark.highest_usn     = 0
82         req8.uptodateness_vector           = None
83         req8.replica_flags                 = (drsuapi.DRSUAPI_DRS_INIT_SYNC |
84                                               drsuapi.DRSUAPI_DRS_PER_SYNC |
85                                               drsuapi.DRSUAPI_DRS_GET_ANC |
86                                               drsuapi.DRSUAPI_DRS_NEVER_SYNCED |
87                                               drsuapi.DRSUAPI_DRS_WRIT_REP)
88         req8.max_object_count         = 402
89         req8.max_ndr_size             = 402116
90         req8.extended_op              = drsuapi.DRSUAPI_EXOP_REPL_OBJ
91         req8.fsmo_info                = 0
92         req8.partial_attribute_set    = None
93         req8.partial_attribute_set_ex = None
94         req8.mapping_ctr.num_mappings = 0
95         req8.mapping_ctr.mappings     = None
96         (level, ctr) = drs.DsGetNCChanges(drs_handle, 8, req8)
97
98         obj_item = ctr.first_object
99         obj = obj_item.object
100
101         sc_blob = None
102
103         for i in range(0, obj.attribute_ctr.num_attributes):
104             attr = obj.attribute_ctr.attributes[i]
105             if attid_equal(attr.attid,
106                            drsuapi.DRSUAPI_ATTID_supplementalCredentials):
107                 net_ctx = net.Net(self.creds)
108                 net_ctx.replicate_decrypt(drs, attr, 0)
109                 sc_blob = attr.value_ctr.values[0].blob
110
111         sc = ndr_unpack(drsblobs.supplementalCredentialsBlob, sc_blob)
112         return sc
113
114     def test_wDigest_supplementalCredentials(self):
115         self.creds = Credentials()
116         self.creds.set_username(os.environ["USERNAME"])
117         self.creds.set_password(os.environ["PASSWORD"])
118         self.creds.guess(self.lp)
119         ldb = SamDB("ldap://" + os.environ["SERVER"],
120                     credentials=self.creds,
121                     lp=self.lp)
122
123         self.add_user(ldb=ldb)
124
125         sc = self.get_supplemental_creds_drs()
126
127         (pos, package) = get_package(sc, "Primary:WDigest")
128         self.assertEquals("Primary:WDigest", package.name)
129
130         # Check that the WDigest values are correct.
131         #
132         digests = ndr_unpack(drsblobs.package_PrimaryWDigestBlob,
133                              binascii.a2b_hex(package.data))
134         self.check_wdigests(digests)