1 # Integration tests for pycredentials
3 # Copyright (C) Catalyst IT Ltd. 2017
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.
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.
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/>.
18 from samba.tests import TestCase, delete_force
22 from samba.auth import system_session
23 from samba.credentials import Credentials, CLI_CRED_NTLMv2_AUTH
24 from samba.dcerpc import netlogon, ntlmssp
25 from samba.dcerpc.netlogon import netr_Authenticator, netr_WorkstationInformation
26 from samba.dcerpc.misc import SEC_CHAN_WKSTA
27 from samba.dsdb import (
28 UF_WORKSTATION_TRUST_ACCOUNT,
31 from samba.ndr import ndr_pack
32 from samba.samdb import SamDB
34 Integration tests for pycredentials
40 class PyCredentialsTests(TestCase):
43 super(PyCredentialsTests, self).setUp()
45 self.server = os.environ["SERVER"]
46 self.domain = os.environ["DOMAIN"]
47 self.host = os.environ["SERVER_IP"]
48 self.lp = self.get_loadparm()
50 self.credentials = self.get_credentials()
52 self.session = system_session()
53 self.ldb = SamDB(url="ldap://%s" % self.host,
54 session_info=self.session,
55 credentials=self.credentials,
58 self.create_machine_account()
59 self.create_user_account()
63 super(PyCredentialsTests, self).tearDown()
64 delete_force(self.ldb, self.machine_dn)
65 delete_force(self.ldb, self.user_dn)
67 # Until a successful netlogon connection has been established there will
68 # not be a valid authenticator associated with the credentials
69 # and new_client_authenticator should throw a ValueError
70 def test_no_netlogon_connection(self):
71 self.assertRaises(ValueError,
72 self.machine_creds.new_client_authenticator)
74 # Once a netlogon connection has been established,
75 # new_client_authenticator should return a value
77 def test_have_netlogon_connection(self):
78 c = self.get_netlogon_connection()
79 a = self.machine_creds.new_client_authenticator()
80 self.assertIsNotNone(a)
82 # Get an authenticator and use it on a sequence of operations requiring
84 def test_client_authenticator(self):
85 c = self.get_netlogon_connection()
86 (authenticator, subsequent) = self.get_authenticator(c)
87 self.do_NetrLogonSamLogonWithFlags(c, authenticator, subsequent)
88 (authenticator, subsequent) = self.get_authenticator(c)
89 self.do_NetrLogonGetDomainInfo(c, authenticator, subsequent)
90 (authenticator, subsequent) = self.get_authenticator(c)
91 self.do_NetrLogonGetDomainInfo(c, authenticator, subsequent)
92 (authenticator, subsequent) = self.get_authenticator(c)
93 self.do_NetrLogonGetDomainInfo(c, authenticator, subsequent)
98 # Establish aealed schannel netlogon connection over TCP/IP
99 def get_netlogon_connection(self):
100 return netlogon.netlogon("ncacn_ip_tcp:%s[schannel,seal]" % self.server,
105 # Create the machine account
106 def create_machine_account(self):
107 self.machine_pass = samba.generate_random_password(32, 32)
108 self.machine_name = MACHINE_NAME
109 self.machine_dn = "cn=%s,%s" % (self.machine_name, self.ldb.domain_dn())
111 # remove the account if it exists, this will happen if a previous test
113 delete_force(self.ldb, self.machine_dn)
116 '"' + self.machine_pass.encode('utf-8') + '"', 'utf-8'
117 ).encode('utf-16-le')
119 "dn": self.machine_dn,
120 "objectclass": "computer",
121 "sAMAccountName": "%s$" % self.machine_name,
122 "userAccountControl":
123 str(UF_WORKSTATION_TRUST_ACCOUNT | UF_PASSWD_NOTREQD),
124 "unicodePwd": utf16pw})
126 self.machine_creds = Credentials()
127 self.machine_creds.guess(self.get_loadparm())
128 self.machine_creds.set_secure_channel_type(SEC_CHAN_WKSTA)
129 self.machine_creds.set_password(self.machine_pass)
130 self.machine_creds.set_username(self.machine_name + "$")
133 # Create a test user account
134 def create_user_account(self):
135 self.user_pass = samba.generate_random_password(32, 32)
136 self.user_name = USER_NAME
137 self.user_dn = "cn=%s,%s" % (self.user_name, self.ldb.domain_dn())
139 # remove the account if it exists, this will happen if a previous test
141 delete_force(self.ldb, self.user_dn)
144 '"' + self.user_pass.encode('utf-8') + '"', 'utf-8'
145 ).encode('utf-16-le')
148 "objectclass": "user",
149 "sAMAccountName": "%s" % self.user_name,
150 "userAccountControl": str(UF_NORMAL_ACCOUNT),
151 "unicodePwd": utf16pw})
153 self.user_creds = Credentials()
154 self.user_creds.guess(self.get_loadparm())
155 self.user_creds.set_password(self.user_pass)
156 self.user_creds.set_username(self.user_name)
160 # Get the authenticator from the machine creds.
161 def get_authenticator(self, c):
162 auth = self.machine_creds.new_client_authenticator();
163 current = netr_Authenticator()
164 current.cred.data = [ord(x) for x in auth["credential"]]
165 current.timestamp = auth["timestamp"]
167 subsequent = netr_Authenticator()
168 return (current, subsequent)
170 def do_NetrLogonSamLogonWithFlags(self, c, current, subsequent):
171 logon = samlogon_logon_info(self.domain,
175 logon_level = netlogon.NetlogonNetworkTransitiveInformation
176 validation_level = netlogon.NetlogonValidationSamInfo4
178 c.netr_LogonSamLogonWithFlags(self.server,
179 self.user_creds.get_workstation(),
187 def do_NetrLogonGetDomainInfo(self, c, current, subsequent):
188 query = netr_WorkstationInformation()
190 c.netr_LogonGetDomainInfo(self.server,
191 self.user_creds.get_workstation(),
198 # Build the logon data required by NetrLogonSamLogonWithFlags
199 def samlogon_logon_info(domain_name, computer_name, creds):
201 target_info_blob = samlogon_target(domain_name, computer_name)
203 challenge = b"abcdefgh"
204 # User account under test
205 response = creds.get_ntlm_response(flags=CLI_CRED_NTLMv2_AUTH,
207 target_info=target_info_blob)
209 logon = netlogon.netr_NetworkInfo()
211 logon.challenge = [ord(x) for x in challenge]
212 logon.nt = netlogon.netr_ChallengeResponse()
213 logon.nt.length = len(response["nt_response"])
214 logon.nt.data = [ord(x) for x in response["nt_response"]]
215 logon.identity_info = netlogon.netr_IdentityInfo()
217 (username, domain) = creds.get_ntlm_username_domain()
218 logon.identity_info.domain_name.string = domain
219 logon.identity_info.account_name.string = username
220 logon.identity_info.workstation.string = creds.get_workstation()
225 # Build the samlogon target info.
226 def samlogon_target(domain_name, computer_name):
227 target_info = ntlmssp.AV_PAIR_LIST()
228 target_info.count = 3
229 computername = ntlmssp.AV_PAIR()
230 computername.AvId = ntlmssp.MsvAvNbComputerName
231 computername.Value = computer_name
233 domainname = ntlmssp.AV_PAIR()
234 domainname.AvId = ntlmssp.MsvAvNbDomainName
235 domainname.Value = domain_name
237 eol = ntlmssp.AV_PAIR()
238 eol.AvId = ntlmssp.MsvAvEOL
239 target_info.pair = [domainname, computername, eol]
241 return ndr_pack(target_info)