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"
26 #include "rpc_client/cli_netlogon.h"
27 #include "rpc_client/init_netlogon.h"
29 /****************************************************************************
30 Wrapper function that uses the auth and auth2 calls to set up a NETLOGON
31 credentials chain. Stores the credentials in the struct dcinfo in the
33 ****************************************************************************/
35 NTSTATUS
rpccli_netlogon_setup_creds(struct rpc_pipe_client
*cli
,
36 const char *server_name
,
38 const char *clnt_name
,
39 const char *machine_account
,
40 const unsigned char machine_pwd
[16],
41 enum netr_SchannelType sec_chan_type
,
42 uint32_t *neg_flags_inout
)
44 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
45 struct netr_Credential clnt_chal_send
;
46 struct netr_Credential srv_chal_recv
;
47 struct samr_Password password
;
50 uint32_t neg_flags
= *neg_flags_inout
;
52 if (!ndr_syntax_id_equal(&cli
->abstract_syntax
,
53 &ndr_table_netlogon
.syntax_id
)) {
54 return NT_STATUS_INVALID_PARAMETER
;
59 /* Store the machine account password we're going to use. */
60 memcpy(password
.hash
, machine_pwd
, 16);
62 fstr_sprintf( mach_acct
, "%s$", machine_account
);
65 /* Create the client challenge. */
66 generate_random_buffer(clnt_chal_send
.data
, 8);
68 /* Get the server challenge. */
69 result
= rpccli_netr_ServerReqChallenge(cli
, talloc_tos(),
74 if (!NT_STATUS_IS_OK(result
)) {
78 /* Calculate the session key and client credentials */
80 cli
->dc
= netlogon_creds_client_init(cli
,
90 return NT_STATUS_NO_MEMORY
;
94 * Send client auth-2 challenge and receive server repy.
97 result
= rpccli_netr_ServerAuthenticate2(cli
, talloc_tos(),
99 cli
->dc
->account_name
,
101 cli
->dc
->computer_name
,
102 &clnt_chal_send
, /* input. */
103 &srv_chal_recv
, /* output. */
106 /* we might be talking to NT4, so let's downgrade in that case and retry
107 * with the returned neg_flags - gd */
109 if (NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
) && !retried
) {
111 TALLOC_FREE(cli
->dc
);
115 if (!NT_STATUS_IS_OK(result
)) {
120 * Check the returned value using the initial
121 * server received challenge.
124 if (!netlogon_creds_client_check(cli
->dc
, &srv_chal_recv
)) {
126 * Server replied with bad credential. Fail.
128 DEBUG(0,("rpccli_netlogon_setup_creds: server %s "
129 "replied with bad credential\n",
131 return NT_STATUS_ACCESS_DENIED
;
134 DEBUG(5,("rpccli_netlogon_setup_creds: server %s credential "
135 "chain established.\n",
138 cli
->dc
->negotiate_flags
= neg_flags
;
139 *neg_flags_inout
= neg_flags
;
144 /* Logon domain user */
146 NTSTATUS
rpccli_netlogon_sam_logon(struct rpc_pipe_client
*cli
,
148 uint32 logon_parameters
,
150 const char *username
,
151 const char *password
,
152 const char *workstation
,
155 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
156 struct netr_Authenticator clnt_creds
;
157 struct netr_Authenticator ret_creds
;
158 union netr_LogonLevel
*logon
;
159 union netr_Validation validation
;
160 uint8_t authoritative
;
161 int validation_level
= 3;
162 fstring clnt_name_slash
;
164 ZERO_STRUCT(ret_creds
);
166 logon
= TALLOC_ZERO_P(mem_ctx
, union netr_LogonLevel
);
168 return NT_STATUS_NO_MEMORY
;
172 fstr_sprintf( clnt_name_slash
, "\\\\%s", workstation
);
174 fstr_sprintf( clnt_name_slash
, "\\\\%s", global_myname() );
177 /* Initialise input parameters */
179 netlogon_creds_client_authenticator(cli
->dc
, &clnt_creds
);
181 switch (logon_type
) {
182 case NetlogonInteractiveInformation
: {
184 struct netr_PasswordInfo
*password_info
;
186 struct samr_Password lmpassword
;
187 struct samr_Password ntpassword
;
189 password_info
= TALLOC_ZERO_P(mem_ctx
, struct netr_PasswordInfo
);
190 if (!password_info
) {
191 return NT_STATUS_NO_MEMORY
;
194 nt_lm_owf_gen(password
, ntpassword
.hash
, lmpassword
.hash
);
196 if (cli
->dc
->negotiate_flags
& NETLOGON_NEG_ARCFOUR
) {
197 netlogon_creds_arcfour_crypt(cli
->dc
, lmpassword
.hash
, 16);
198 netlogon_creds_arcfour_crypt(cli
->dc
, ntpassword
.hash
, 16);
200 netlogon_creds_des_encrypt(cli
->dc
, &lmpassword
);
201 netlogon_creds_des_encrypt(cli
->dc
, &ntpassword
);
204 password_info
->identity_info
.domain_name
.string
= domain
;
205 password_info
->identity_info
.parameter_control
= logon_parameters
;
206 password_info
->identity_info
.logon_id_low
= 0xdead;
207 password_info
->identity_info
.logon_id_high
= 0xbeef;
208 password_info
->identity_info
.account_name
.string
= username
;
209 password_info
->identity_info
.workstation
.string
= clnt_name_slash
;
211 password_info
->lmpassword
= lmpassword
;
212 password_info
->ntpassword
= ntpassword
;
214 logon
->password
= password_info
;
218 case NetlogonNetworkInformation
: {
219 struct netr_NetworkInfo
*network_info
;
221 unsigned char local_lm_response
[24];
222 unsigned char local_nt_response
[24];
223 struct netr_ChallengeResponse lm
;
224 struct netr_ChallengeResponse nt
;
229 network_info
= TALLOC_ZERO_P(mem_ctx
, struct netr_NetworkInfo
);
231 return NT_STATUS_NO_MEMORY
;
234 generate_random_buffer(chal
, 8);
236 SMBencrypt(password
, chal
, local_lm_response
);
237 SMBNTencrypt(password
, chal
, local_nt_response
);
240 lm
.data
= local_lm_response
;
243 nt
.data
= local_nt_response
;
245 network_info
->identity_info
.domain_name
.string
= domain
;
246 network_info
->identity_info
.parameter_control
= logon_parameters
;
247 network_info
->identity_info
.logon_id_low
= 0xdead;
248 network_info
->identity_info
.logon_id_high
= 0xbeef;
249 network_info
->identity_info
.account_name
.string
= username
;
250 network_info
->identity_info
.workstation
.string
= clnt_name_slash
;
252 memcpy(network_info
->challenge
, chal
, 8);
253 network_info
->nt
= nt
;
254 network_info
->lm
= lm
;
256 logon
->network
= network_info
;
261 DEBUG(0, ("switch value %d not supported\n",
263 return NT_STATUS_INVALID_INFO_CLASS
;
266 result
= rpccli_netr_LogonSamLogon(cli
, mem_ctx
,
277 /* Always check returned credentials */
278 if (!netlogon_creds_client_check(cli
->dc
, &ret_creds
.cred
)) {
279 DEBUG(0,("rpccli_netlogon_sam_logon: credentials chain check failed\n"));
280 return NT_STATUS_ACCESS_DENIED
;
288 * Logon domain user with an 'network' SAM logon
290 * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
293 NTSTATUS
rpccli_netlogon_sam_network_logon(struct rpc_pipe_client
*cli
,
295 uint32 logon_parameters
,
297 const char *username
,
299 const char *workstation
,
301 DATA_BLOB lm_response
,
302 DATA_BLOB nt_response
,
303 struct netr_SamInfo3
**info3
)
305 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
306 int validation_level
= 3;
307 const char *workstation_name_slash
;
308 const char *server_name_slash
;
309 struct netr_Authenticator clnt_creds
;
310 struct netr_Authenticator ret_creds
;
311 union netr_LogonLevel
*logon
= NULL
;
312 struct netr_NetworkInfo
*network_info
;
313 uint8_t authoritative
;
314 union netr_Validation validation
;
315 struct netr_ChallengeResponse lm
;
316 struct netr_ChallengeResponse nt
;
320 ZERO_STRUCT(ret_creds
);
325 logon
= TALLOC_ZERO_P(mem_ctx
, union netr_LogonLevel
);
327 return NT_STATUS_NO_MEMORY
;
330 network_info
= TALLOC_ZERO_P(mem_ctx
, struct netr_NetworkInfo
);
332 return NT_STATUS_NO_MEMORY
;
335 netlogon_creds_client_authenticator(cli
->dc
, &clnt_creds
);
337 if (server
[0] != '\\' && server
[1] != '\\') {
338 server_name_slash
= talloc_asprintf(mem_ctx
, "\\\\%s", server
);
340 server_name_slash
= server
;
343 if (workstation
[0] != '\\' && workstation
[1] != '\\') {
344 workstation_name_slash
= talloc_asprintf(mem_ctx
, "\\\\%s", workstation
);
346 workstation_name_slash
= workstation
;
349 if (!workstation_name_slash
|| !server_name_slash
) {
350 DEBUG(0, ("talloc_asprintf failed!\n"));
351 return NT_STATUS_NO_MEMORY
;
354 /* Initialise input parameters */
356 lm
.data
= lm_response
.data
;
357 lm
.length
= lm_response
.length
;
358 nt
.data
= nt_response
.data
;
359 nt
.length
= nt_response
.length
;
361 network_info
->identity_info
.domain_name
.string
= domain
;
362 network_info
->identity_info
.parameter_control
= logon_parameters
;
363 network_info
->identity_info
.logon_id_low
= 0xdead;
364 network_info
->identity_info
.logon_id_high
= 0xbeef;
365 network_info
->identity_info
.account_name
.string
= username
;
366 network_info
->identity_info
.workstation
.string
= workstation_name_slash
;
368 memcpy(network_info
->challenge
, chal
, 8);
369 network_info
->nt
= nt
;
370 network_info
->lm
= lm
;
372 logon
->network
= network_info
;
374 /* Marshall data and send request */
376 result
= rpccli_netr_LogonSamLogon(cli
, mem_ctx
,
381 NetlogonNetworkInformation
,
386 if (!NT_STATUS_IS_OK(result
)) {
390 /* Always check returned credentials. */
391 if (!netlogon_creds_client_check(cli
->dc
, &ret_creds
.cred
)) {
392 DEBUG(0,("rpccli_netlogon_sam_network_logon: credentials chain check failed\n"));
393 return NT_STATUS_ACCESS_DENIED
;
396 netlogon_creds_decrypt_samlogon(cli
->dc
, validation_level
, &validation
);
398 *info3
= validation
.sam3
;
403 NTSTATUS
rpccli_netlogon_sam_network_logon_ex(struct rpc_pipe_client
*cli
,
405 uint32 logon_parameters
,
407 const char *username
,
409 const char *workstation
,
411 DATA_BLOB lm_response
,
412 DATA_BLOB nt_response
,
413 struct netr_SamInfo3
**info3
)
415 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
416 int validation_level
= 3;
417 const char *workstation_name_slash
;
418 const char *server_name_slash
;
419 union netr_LogonLevel
*logon
= NULL
;
420 struct netr_NetworkInfo
*network_info
;
421 uint8_t authoritative
;
422 union netr_Validation validation
;
423 struct netr_ChallengeResponse lm
;
424 struct netr_ChallengeResponse nt
;
432 logon
= TALLOC_ZERO_P(mem_ctx
, union netr_LogonLevel
);
434 return NT_STATUS_NO_MEMORY
;
437 network_info
= TALLOC_ZERO_P(mem_ctx
, struct netr_NetworkInfo
);
439 return NT_STATUS_NO_MEMORY
;
442 if (server
[0] != '\\' && server
[1] != '\\') {
443 server_name_slash
= talloc_asprintf(mem_ctx
, "\\\\%s", server
);
445 server_name_slash
= server
;
448 if (workstation
[0] != '\\' && workstation
[1] != '\\') {
449 workstation_name_slash
= talloc_asprintf(mem_ctx
, "\\\\%s", workstation
);
451 workstation_name_slash
= workstation
;
454 if (!workstation_name_slash
|| !server_name_slash
) {
455 DEBUG(0, ("talloc_asprintf failed!\n"));
456 return NT_STATUS_NO_MEMORY
;
459 /* Initialise input parameters */
461 lm
.data
= lm_response
.data
;
462 lm
.length
= lm_response
.length
;
463 nt
.data
= nt_response
.data
;
464 nt
.length
= nt_response
.length
;
466 network_info
->identity_info
.domain_name
.string
= domain
;
467 network_info
->identity_info
.parameter_control
= logon_parameters
;
468 network_info
->identity_info
.logon_id_low
= 0xdead;
469 network_info
->identity_info
.logon_id_high
= 0xbeef;
470 network_info
->identity_info
.account_name
.string
= username
;
471 network_info
->identity_info
.workstation
.string
= workstation_name_slash
;
473 memcpy(network_info
->challenge
, chal
, 8);
474 network_info
->nt
= nt
;
475 network_info
->lm
= lm
;
477 logon
->network
= network_info
;
479 /* Marshall data and send request */
481 result
= rpccli_netr_LogonSamLogonEx(cli
, mem_ctx
,
484 NetlogonNetworkInformation
,
490 if (!NT_STATUS_IS_OK(result
)) {
494 netlogon_creds_decrypt_samlogon(cli
->dc
, validation_level
, &validation
);
496 *info3
= validation
.sam3
;
501 /*********************************************************
502 Change the domain password on the PDC.
504 Just changes the password betwen the two values specified.
506 Caller must have the cli connected to the netlogon pipe
508 **********************************************************/
510 NTSTATUS
rpccli_netlogon_set_trust_password(struct rpc_pipe_client
*cli
,
512 const char *account_name
,
513 const unsigned char orig_trust_passwd_hash
[16],
514 const char *new_trust_pwd_cleartext
,
515 const unsigned char new_trust_passwd_hash
[16],
516 enum netr_SchannelType sec_channel_type
)
519 struct netr_Authenticator clnt_creds
, srv_cred
;
522 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
523 result
= rpccli_netlogon_setup_creds(cli
,
524 cli
->desthost
, /* server name */
525 lp_workgroup(), /* domain */
526 global_myname(), /* client name */
527 account_name
, /* machine account name */
528 orig_trust_passwd_hash
,
531 if (!NT_STATUS_IS_OK(result
)) {
532 DEBUG(3,("rpccli_netlogon_set_trust_password: unable to setup creds (%s)!\n",
538 netlogon_creds_client_authenticator(cli
->dc
, &clnt_creds
);
540 if (cli
->dc
->negotiate_flags
& NETLOGON_NEG_PASSWORD_SET2
) {
542 struct netr_CryptPassword new_password
;
544 init_netr_CryptPassword(new_trust_pwd_cleartext
,
545 cli
->dc
->session_key
,
548 result
= rpccli_netr_ServerPasswordSet2(cli
, mem_ctx
,
550 cli
->dc
->account_name
,
552 cli
->dc
->computer_name
,
556 if (!NT_STATUS_IS_OK(result
)) {
557 DEBUG(0,("rpccli_netr_ServerPasswordSet2 failed: %s\n",
563 struct samr_Password new_password
;
564 memcpy(new_password
.hash
, new_trust_passwd_hash
, sizeof(new_password
.hash
));
565 netlogon_creds_des_encrypt(cli
->dc
, &new_password
);
567 result
= rpccli_netr_ServerPasswordSet(cli
, mem_ctx
,
569 cli
->dc
->account_name
,
571 cli
->dc
->computer_name
,
575 if (!NT_STATUS_IS_OK(result
)) {
576 DEBUG(0,("rpccli_netr_ServerPasswordSet failed: %s\n",
582 /* Always check returned credentials. */
583 if (!netlogon_creds_client_check(cli
->dc
, &srv_cred
.cred
)) {
584 DEBUG(0,("credentials chain check failed\n"));
585 return NT_STATUS_ACCESS_DENIED
;