2 Unix SMB/CIFS implementation.
3 NT Domain Authentication SMB / MSRPC client
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Jeremy Allison 1998.
6 Largely re-written by Jeremy Allison (C) 2005.
7 Copyright (C) Guenther Deschner 2008.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "../libcli/auth/libcli_auth.h"
25 #include "../librpc/gen_ndr/cli_netlogon.h"
27 /****************************************************************************
28 Wrapper function that uses the auth and auth2 calls to set up a NETLOGON
29 credentials chain. Stores the credentials in the struct dcinfo in the
31 ****************************************************************************/
33 NTSTATUS
rpccli_netlogon_setup_creds(struct rpc_pipe_client
*cli
,
34 const char *server_name
,
36 const char *clnt_name
,
37 const char *machine_account
,
38 const unsigned char machine_pwd
[16],
39 enum netr_SchannelType sec_chan_type
,
40 uint32_t *neg_flags_inout
)
42 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
43 struct netr_Credential clnt_chal_send
;
44 struct netr_Credential srv_chal_recv
;
45 struct samr_Password password
;
48 uint32_t neg_flags
= *neg_flags_inout
;
50 if (!ndr_syntax_id_equal(&cli
->abstract_syntax
,
51 &ndr_table_netlogon
.syntax_id
)) {
52 return NT_STATUS_INVALID_PARAMETER
;
57 /* Store the machine account password we're going to use. */
58 memcpy(password
.hash
, machine_pwd
, 16);
60 fstr_sprintf( mach_acct
, "%s$", machine_account
);
63 /* Create the client challenge. */
64 generate_random_buffer(clnt_chal_send
.data
, 8);
66 /* Get the server challenge. */
67 result
= rpccli_netr_ServerReqChallenge(cli
, talloc_tos(),
72 if (!NT_STATUS_IS_OK(result
)) {
76 /* Calculate the session key and client credentials */
78 cli
->dc
= netlogon_creds_client_init(cli
,
88 return NT_STATUS_NO_MEMORY
;
92 * Send client auth-2 challenge and receive server repy.
95 result
= rpccli_netr_ServerAuthenticate2(cli
, talloc_tos(),
97 cli
->dc
->account_name
,
99 cli
->dc
->computer_name
,
100 &clnt_chal_send
, /* input. */
101 &srv_chal_recv
, /* output. */
104 /* we might be talking to NT4, so let's downgrade in that case and retry
105 * with the returned neg_flags - gd */
107 if (NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
) && !retried
) {
109 TALLOC_FREE(cli
->dc
);
113 if (!NT_STATUS_IS_OK(result
)) {
118 * Check the returned value using the initial
119 * server received challenge.
122 if (!netlogon_creds_client_check(cli
->dc
, &srv_chal_recv
)) {
124 * Server replied with bad credential. Fail.
126 DEBUG(0,("rpccli_netlogon_setup_creds: server %s "
127 "replied with bad credential\n",
129 return NT_STATUS_ACCESS_DENIED
;
132 DEBUG(5,("rpccli_netlogon_setup_creds: server %s credential "
133 "chain established.\n",
136 cli
->dc
->negotiate_flags
= neg_flags
;
137 *neg_flags_inout
= neg_flags
;
142 /* Logon domain user */
144 NTSTATUS
rpccli_netlogon_sam_logon(struct rpc_pipe_client
*cli
,
146 uint32 logon_parameters
,
148 const char *username
,
149 const char *password
,
150 const char *workstation
,
153 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
154 struct netr_Authenticator clnt_creds
;
155 struct netr_Authenticator ret_creds
;
156 union netr_LogonLevel
*logon
;
157 union netr_Validation validation
;
158 uint8_t authoritative
;
159 int validation_level
= 3;
160 fstring clnt_name_slash
;
162 ZERO_STRUCT(ret_creds
);
164 logon
= TALLOC_ZERO_P(mem_ctx
, union netr_LogonLevel
);
166 return NT_STATUS_NO_MEMORY
;
170 fstr_sprintf( clnt_name_slash
, "\\\\%s", workstation
);
172 fstr_sprintf( clnt_name_slash
, "\\\\%s", global_myname() );
175 /* Initialise input parameters */
177 netlogon_creds_client_authenticator(cli
->dc
, &clnt_creds
);
179 switch (logon_type
) {
180 case NetlogonInteractiveInformation
: {
182 struct netr_PasswordInfo
*password_info
;
184 struct samr_Password lmpassword
;
185 struct samr_Password ntpassword
;
187 password_info
= TALLOC_ZERO_P(mem_ctx
, struct netr_PasswordInfo
);
188 if (!password_info
) {
189 return NT_STATUS_NO_MEMORY
;
192 nt_lm_owf_gen(password
, ntpassword
.hash
, lmpassword
.hash
);
194 if (cli
->dc
->negotiate_flags
& NETLOGON_NEG_ARCFOUR
) {
195 netlogon_creds_arcfour_crypt(cli
->dc
, lmpassword
.hash
, 16);
196 netlogon_creds_arcfour_crypt(cli
->dc
, ntpassword
.hash
, 16);
198 netlogon_creds_des_encrypt(cli
->dc
, &lmpassword
);
199 netlogon_creds_des_encrypt(cli
->dc
, &ntpassword
);
202 password_info
->identity_info
.domain_name
.string
= domain
;
203 password_info
->identity_info
.parameter_control
= logon_parameters
;
204 password_info
->identity_info
.logon_id_low
= 0xdead;
205 password_info
->identity_info
.logon_id_high
= 0xbeef;
206 password_info
->identity_info
.account_name
.string
= username
;
207 password_info
->identity_info
.workstation
.string
= clnt_name_slash
;
209 password_info
->lmpassword
= lmpassword
;
210 password_info
->ntpassword
= ntpassword
;
212 logon
->password
= password_info
;
216 case NetlogonNetworkInformation
: {
217 struct netr_NetworkInfo
*network_info
;
219 unsigned char local_lm_response
[24];
220 unsigned char local_nt_response
[24];
221 struct netr_ChallengeResponse lm
;
222 struct netr_ChallengeResponse nt
;
227 network_info
= TALLOC_ZERO_P(mem_ctx
, struct netr_NetworkInfo
);
229 return NT_STATUS_NO_MEMORY
;
232 generate_random_buffer(chal
, 8);
234 SMBencrypt(password
, chal
, local_lm_response
);
235 SMBNTencrypt(password
, chal
, local_nt_response
);
238 lm
.data
= local_lm_response
;
241 nt
.data
= local_nt_response
;
243 network_info
->identity_info
.domain_name
.string
= domain
;
244 network_info
->identity_info
.parameter_control
= logon_parameters
;
245 network_info
->identity_info
.logon_id_low
= 0xdead;
246 network_info
->identity_info
.logon_id_high
= 0xbeef;
247 network_info
->identity_info
.account_name
.string
= username
;
248 network_info
->identity_info
.workstation
.string
= clnt_name_slash
;
250 memcpy(network_info
->challenge
, chal
, 8);
251 network_info
->nt
= nt
;
252 network_info
->lm
= lm
;
254 logon
->network
= network_info
;
259 DEBUG(0, ("switch value %d not supported\n",
261 return NT_STATUS_INVALID_INFO_CLASS
;
264 result
= rpccli_netr_LogonSamLogon(cli
, mem_ctx
,
275 /* Always check returned credentials */
276 if (!netlogon_creds_client_check(cli
->dc
, &ret_creds
.cred
)) {
277 DEBUG(0,("rpccli_netlogon_sam_logon: credentials chain check failed\n"));
278 return NT_STATUS_ACCESS_DENIED
;
286 * Logon domain user with an 'network' SAM logon
288 * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
291 NTSTATUS
rpccli_netlogon_sam_network_logon(struct rpc_pipe_client
*cli
,
293 uint32 logon_parameters
,
295 const char *username
,
297 const char *workstation
,
299 DATA_BLOB lm_response
,
300 DATA_BLOB nt_response
,
301 struct netr_SamInfo3
**info3
)
303 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
304 int validation_level
= 3;
305 const char *workstation_name_slash
;
306 const char *server_name_slash
;
307 struct netr_Authenticator clnt_creds
;
308 struct netr_Authenticator ret_creds
;
309 union netr_LogonLevel
*logon
= NULL
;
310 struct netr_NetworkInfo
*network_info
;
311 uint8_t authoritative
;
312 union netr_Validation validation
;
313 struct netr_ChallengeResponse lm
;
314 struct netr_ChallengeResponse nt
;
318 ZERO_STRUCT(ret_creds
);
323 logon
= TALLOC_ZERO_P(mem_ctx
, union netr_LogonLevel
);
325 return NT_STATUS_NO_MEMORY
;
328 network_info
= TALLOC_ZERO_P(mem_ctx
, struct netr_NetworkInfo
);
330 return NT_STATUS_NO_MEMORY
;
333 netlogon_creds_client_authenticator(cli
->dc
, &clnt_creds
);
335 if (server
[0] != '\\' && server
[1] != '\\') {
336 server_name_slash
= talloc_asprintf(mem_ctx
, "\\\\%s", server
);
338 server_name_slash
= server
;
341 if (workstation
[0] != '\\' && workstation
[1] != '\\') {
342 workstation_name_slash
= talloc_asprintf(mem_ctx
, "\\\\%s", workstation
);
344 workstation_name_slash
= workstation
;
347 if (!workstation_name_slash
|| !server_name_slash
) {
348 DEBUG(0, ("talloc_asprintf failed!\n"));
349 return NT_STATUS_NO_MEMORY
;
352 /* Initialise input parameters */
354 lm
.data
= lm_response
.data
;
355 lm
.length
= lm_response
.length
;
356 nt
.data
= nt_response
.data
;
357 nt
.length
= nt_response
.length
;
359 network_info
->identity_info
.domain_name
.string
= domain
;
360 network_info
->identity_info
.parameter_control
= logon_parameters
;
361 network_info
->identity_info
.logon_id_low
= 0xdead;
362 network_info
->identity_info
.logon_id_high
= 0xbeef;
363 network_info
->identity_info
.account_name
.string
= username
;
364 network_info
->identity_info
.workstation
.string
= workstation_name_slash
;
366 memcpy(network_info
->challenge
, chal
, 8);
367 network_info
->nt
= nt
;
368 network_info
->lm
= lm
;
370 logon
->network
= network_info
;
372 /* Marshall data and send request */
374 result
= rpccli_netr_LogonSamLogon(cli
, mem_ctx
,
379 NetlogonNetworkInformation
,
384 if (!NT_STATUS_IS_OK(result
)) {
388 /* Always check returned credentials. */
389 if (!netlogon_creds_client_check(cli
->dc
, &ret_creds
.cred
)) {
390 DEBUG(0,("rpccli_netlogon_sam_network_logon: credentials chain check failed\n"));
391 return NT_STATUS_ACCESS_DENIED
;
394 netlogon_creds_decrypt_samlogon(cli
->dc
, validation_level
, &validation
);
396 *info3
= validation
.sam3
;
401 NTSTATUS
rpccli_netlogon_sam_network_logon_ex(struct rpc_pipe_client
*cli
,
403 uint32 logon_parameters
,
405 const char *username
,
407 const char *workstation
,
409 DATA_BLOB lm_response
,
410 DATA_BLOB nt_response
,
411 struct netr_SamInfo3
**info3
)
413 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
414 int validation_level
= 3;
415 const char *workstation_name_slash
;
416 const char *server_name_slash
;
417 union netr_LogonLevel
*logon
= NULL
;
418 struct netr_NetworkInfo
*network_info
;
419 uint8_t authoritative
;
420 union netr_Validation validation
;
421 struct netr_ChallengeResponse lm
;
422 struct netr_ChallengeResponse nt
;
430 logon
= TALLOC_ZERO_P(mem_ctx
, union netr_LogonLevel
);
432 return NT_STATUS_NO_MEMORY
;
435 network_info
= TALLOC_ZERO_P(mem_ctx
, struct netr_NetworkInfo
);
437 return NT_STATUS_NO_MEMORY
;
440 if (server
[0] != '\\' && server
[1] != '\\') {
441 server_name_slash
= talloc_asprintf(mem_ctx
, "\\\\%s", server
);
443 server_name_slash
= server
;
446 if (workstation
[0] != '\\' && workstation
[1] != '\\') {
447 workstation_name_slash
= talloc_asprintf(mem_ctx
, "\\\\%s", workstation
);
449 workstation_name_slash
= workstation
;
452 if (!workstation_name_slash
|| !server_name_slash
) {
453 DEBUG(0, ("talloc_asprintf failed!\n"));
454 return NT_STATUS_NO_MEMORY
;
457 /* Initialise input parameters */
459 lm
.data
= lm_response
.data
;
460 lm
.length
= lm_response
.length
;
461 nt
.data
= nt_response
.data
;
462 nt
.length
= nt_response
.length
;
464 network_info
->identity_info
.domain_name
.string
= domain
;
465 network_info
->identity_info
.parameter_control
= logon_parameters
;
466 network_info
->identity_info
.logon_id_low
= 0xdead;
467 network_info
->identity_info
.logon_id_high
= 0xbeef;
468 network_info
->identity_info
.account_name
.string
= username
;
469 network_info
->identity_info
.workstation
.string
= workstation_name_slash
;
471 memcpy(network_info
->challenge
, chal
, 8);
472 network_info
->nt
= nt
;
473 network_info
->lm
= lm
;
475 logon
->network
= network_info
;
477 /* Marshall data and send request */
479 result
= rpccli_netr_LogonSamLogonEx(cli
, mem_ctx
,
482 NetlogonNetworkInformation
,
488 if (!NT_STATUS_IS_OK(result
)) {
492 netlogon_creds_decrypt_samlogon(cli
->dc
, validation_level
, &validation
);
494 *info3
= validation
.sam3
;
499 /*********************************************************
500 Change the domain password on the PDC.
502 Just changes the password betwen the two values specified.
504 Caller must have the cli connected to the netlogon pipe
506 **********************************************************/
508 NTSTATUS
rpccli_netlogon_set_trust_password(struct rpc_pipe_client
*cli
,
510 const char *account_name
,
511 const unsigned char orig_trust_passwd_hash
[16],
512 const char *new_trust_pwd_cleartext
,
513 const unsigned char new_trust_passwd_hash
[16],
514 enum netr_SchannelType sec_channel_type
)
517 struct netr_Authenticator clnt_creds
, srv_cred
;
520 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
521 result
= rpccli_netlogon_setup_creds(cli
,
522 cli
->desthost
, /* server name */
523 lp_workgroup(), /* domain */
524 global_myname(), /* client name */
525 account_name
, /* machine account name */
526 orig_trust_passwd_hash
,
529 if (!NT_STATUS_IS_OK(result
)) {
530 DEBUG(3,("rpccli_netlogon_set_trust_password: unable to setup creds (%s)!\n",
536 netlogon_creds_client_authenticator(cli
->dc
, &clnt_creds
);
538 if (cli
->dc
->negotiate_flags
& NETLOGON_NEG_PASSWORD_SET2
) {
540 struct netr_CryptPassword new_password
;
542 init_netr_CryptPassword(new_trust_pwd_cleartext
,
543 cli
->dc
->session_key
,
546 result
= rpccli_netr_ServerPasswordSet2(cli
, mem_ctx
,
548 cli
->dc
->account_name
,
550 cli
->dc
->computer_name
,
554 if (!NT_STATUS_IS_OK(result
)) {
555 DEBUG(0,("rpccli_netr_ServerPasswordSet2 failed: %s\n",
561 struct samr_Password new_password
;
562 memcpy(new_password
.hash
, new_trust_passwd_hash
, sizeof(new_password
.hash
));
563 netlogon_creds_des_encrypt(cli
->dc
, &new_password
);
565 result
= rpccli_netr_ServerPasswordSet(cli
, mem_ctx
,
567 cli
->dc
->account_name
,
569 cli
->dc
->computer_name
,
573 if (!NT_STATUS_IS_OK(result
)) {
574 DEBUG(0,("rpccli_netr_ServerPasswordSet failed: %s\n",
580 /* Always check returned credentials. */
581 if (!netlogon_creds_client_check(cli
->dc
, &srv_cred
.cred
)) {
582 DEBUG(0,("credentials chain check failed\n"));
583 return NT_STATUS_ACCESS_DENIED
;