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 "system/filesys.h"
25 #include "libsmb/libsmb.h"
26 #include "rpc_client/rpc_client.h"
27 #include "rpc_client/cli_pipe.h"
28 #include "../libcli/auth/libcli_auth.h"
29 #include "../libcli/auth/netlogon_creds_cli.h"
30 #include "../librpc/gen_ndr/ndr_netlogon_c.h"
31 #include "../librpc/gen_ndr/schannel.h"
32 #include "rpc_client/cli_netlogon.h"
33 #include "rpc_client/util_netlogon.h"
34 #include "../libcli/security/security.h"
35 #include "lib/param/param.h"
36 #include "libcli/smb/smbXcli_base.h"
37 #include "dbwrap/dbwrap.h"
38 #include "dbwrap/dbwrap_open.h"
40 #include "lib/crypto/gnutls_helpers.h"
43 NTSTATUS
rpccli_pre_open_netlogon_creds(void)
45 static bool already_open
= false;
47 struct loadparm_context
*lp_ctx
;
49 struct db_context
*global_db
;
56 frame
= talloc_stackframe();
58 lp_ctx
= loadparm_init_s3(frame
, loadparm_s3_helpers());
61 return NT_STATUS_NO_MEMORY
;
64 fname
= lpcfg_private_db_path(frame
, lp_ctx
, "netlogon_creds_cli");
67 return NT_STATUS_NO_MEMORY
;
70 global_db
= db_open(frame
, fname
,
71 0, TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
,
72 O_RDWR
|O_CREAT
, 0600, DBWRAP_LOCK_ORDER_2
,
73 DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS
);
74 if (global_db
== NULL
) {
76 return NT_STATUS_NO_MEMORY
;
79 status
= netlogon_creds_cli_set_global_db(lp_ctx
, &global_db
);
81 if (!NT_STATUS_IS_OK(status
)) {
89 static NTSTATUS
rpccli_create_netlogon_creds(
90 const char *server_computer
,
91 const char *server_netbios_domain
,
92 const char *server_dns_domain
,
93 const char *client_account
,
94 enum netr_SchannelType sec_chan_type
,
95 struct messaging_context
*msg_ctx
,
97 struct netlogon_creds_cli_context
**netlogon_creds
)
99 TALLOC_CTX
*frame
= talloc_stackframe();
100 struct loadparm_context
*lp_ctx
;
103 status
= rpccli_pre_open_netlogon_creds();
104 if (!NT_STATUS_IS_OK(status
)) {
109 lp_ctx
= loadparm_init_s3(frame
, loadparm_s3_helpers());
110 if (lp_ctx
== NULL
) {
112 return NT_STATUS_NO_MEMORY
;
114 status
= netlogon_creds_cli_context_global(lp_ctx
,
119 server_netbios_domain
,
121 mem_ctx
, netlogon_creds
);
123 if (!NT_STATUS_IS_OK(status
)) {
130 NTSTATUS
rpccli_create_netlogon_creds_ctx(
131 struct cli_credentials
*creds
,
132 const char *server_computer
,
133 struct messaging_context
*msg_ctx
,
135 struct netlogon_creds_cli_context
**creds_ctx
)
137 enum netr_SchannelType sec_chan_type
;
138 const char *server_netbios_domain
;
139 const char *server_dns_domain
;
140 const char *client_account
;
142 sec_chan_type
= cli_credentials_get_secure_channel_type(creds
);
143 client_account
= cli_credentials_get_username(creds
);
144 server_netbios_domain
= cli_credentials_get_domain(creds
);
145 server_dns_domain
= cli_credentials_get_realm(creds
);
147 return rpccli_create_netlogon_creds(server_computer
,
148 server_netbios_domain
,
156 static NTSTATUS
rpccli_setup_netlogon_creds_locked(
157 struct cli_state
*cli
,
158 enum dcerpc_transport_t transport
,
159 const char *remote_name
,
160 const struct sockaddr_storage
*remote_sockaddr
,
161 struct netlogon_creds_cli_context
*creds_ctx
,
163 struct cli_credentials
*cli_creds
,
165 struct rpc_pipe_client
**_netlogon_pipe
,
166 uint32_t *negotiate_flags
)
168 TALLOC_CTX
*frame
= talloc_stackframe();
169 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
170 struct netlogon_creds_CredentialState
*creds
= NULL
;
171 uint8_t num_nt_hashes
= 0;
172 const struct samr_Password
*nt_hashes
[2] = { NULL
, NULL
};
173 uint8_t idx_nt_hashes
= 0;
176 status
= netlogon_creds_cli_get(creds_ctx
, frame
, &creds
);
177 if (NT_STATUS_IS_OK(status
)) {
178 const char *action
= "using";
181 action
= "overwrite";
185 remote_name
= smbXcli_conn_remote_name(cli
->conn
);
187 remote_name
= "<UNKNOWN>";
190 DEBUG(5,("%s: %s cached netlogon_creds cli[%s/%s] to %s\n",
191 __FUNCTION__
, action
,
192 creds
->account_name
, creds
->computer_name
,
200 nt_hashes
[0] = cli_credentials_get_nt_hash(cli_creds
, talloc_tos());
201 if (nt_hashes
[0] == NULL
) {
203 return NT_STATUS_NO_MEMORY
;
207 nt_hashes
[1] = cli_credentials_get_old_nt_hash(cli_creds
,
209 if (nt_hashes
[1] != NULL
) {
213 status
= cli_rpc_pipe_open_noauth_transport(cli
,
219 if (!NT_STATUS_IS_OK(status
)) {
220 DEBUG(5,("%s: failed to open noauth netlogon connection to %s - %s\n",
227 talloc_steal(frame
, netlogon_pipe
);
229 status
= netlogon_creds_cli_auth(creds_ctx
,
230 netlogon_pipe
->binding_handle
,
234 if (!NT_STATUS_IS_OK(status
)) {
239 status
= netlogon_creds_cli_get(creds_ctx
, frame
, &creds
);
240 if (!NT_STATUS_IS_OK(status
)) {
242 return NT_STATUS_INTERNAL_ERROR
;
245 DEBUG(5,("%s: using new netlogon_creds cli[%s/%s] to %s\n",
247 creds
->account_name
, creds
->computer_name
,
251 if (_netlogon_pipe
!= NULL
) {
252 *_netlogon_pipe
= talloc_move(mem_ctx
, &netlogon_pipe
);
255 if (negotiate_flags
!= NULL
) {
256 *negotiate_flags
= creds
->negotiate_flags
;
263 NTSTATUS
rpccli_setup_netlogon_creds(
264 struct cli_state
*cli
,
265 enum dcerpc_transport_t transport
,
266 const char *remote_name
,
267 const struct sockaddr_storage
*remote_sockaddr
,
268 struct netlogon_creds_cli_context
*creds_ctx
,
270 struct cli_credentials
*cli_creds
)
272 TALLOC_CTX
*frame
= talloc_stackframe();
273 struct netlogon_creds_cli_lck
*lck
;
276 status
= netlogon_creds_cli_lck(
277 creds_ctx
, NETLOGON_CREDS_CLI_LCK_EXCLUSIVE
,
279 if (!NT_STATUS_IS_OK(status
)) {
280 DBG_WARNING("netlogon_creds_cli_lck failed: %s\n",
286 status
= rpccli_setup_netlogon_creds_locked(cli
,
302 NTSTATUS
rpccli_connect_netlogon(
303 struct cli_state
*cli
,
304 enum dcerpc_transport_t transport
,
305 const char *remote_name
,
306 const struct sockaddr_storage
*remote_sockaddr
,
307 struct netlogon_creds_cli_context
*creds_ctx
,
309 struct cli_credentials
*trust_creds
,
310 struct rpc_pipe_client
**_rpccli
)
312 TALLOC_CTX
*frame
= talloc_stackframe();
313 struct netlogon_creds_CredentialState
*creds
= NULL
;
314 enum netlogon_creds_cli_lck_type lck_type
;
315 enum netr_SchannelType sec_chan_type
;
316 struct netlogon_creds_cli_lck
*lck
= NULL
;
317 uint32_t negotiate_flags
;
318 uint8_t found_session_key
[16] = {0};
319 bool found_existing_creds
= false;
321 struct rpc_pipe_client
*rpccli
;
325 sec_chan_type
= cli_credentials_get_secure_channel_type(trust_creds
);
326 if (sec_chan_type
== SEC_CHAN_NULL
) {
327 DBG_ERR("secure_channel_type gave SEC_CHAN_NULL\n");
328 status
= NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
335 * See whether we can use existing netlogon_creds or
336 * whether we have to serverauthenticate.
338 status
= netlogon_creds_cli_get(creds_ctx
, frame
, &creds
);
340 if (NT_STATUS_IS_OK(status
)) {
341 bool cmp
= mem_equal_const_time(found_session_key
,
343 sizeof(found_session_key
));
344 found_existing_creds
= !cmp
;
346 memcpy(found_session_key
,
348 sizeof(found_session_key
));
353 lck_type
= (force_reauth
|| !found_existing_creds
) ?
354 NETLOGON_CREDS_CLI_LCK_EXCLUSIVE
:
355 NETLOGON_CREDS_CLI_LCK_SHARED
;
357 status
= netlogon_creds_cli_lck(creds_ctx
, lck_type
, frame
, &lck
);
358 if (!NT_STATUS_IS_OK(status
)) {
359 DBG_DEBUG("netlogon_creds_cli_lck failed: %s\n",
364 if (!found_existing_creds
) {
366 * Try to find creds under the lock again. Someone
367 * else might have done it for us.
369 status
= netlogon_creds_cli_get(creds_ctx
, frame
, &creds
);
371 if (NT_STATUS_IS_OK(status
)) {
372 bool cmp
= mem_equal_const_time(found_session_key
,
374 sizeof(found_session_key
));
375 found_existing_creds
= !cmp
;
377 memcpy(found_session_key
, creds
->session_key
,
378 sizeof(found_session_key
));
384 do_serverauth
= force_reauth
|| !found_existing_creds
;
386 if (!do_serverauth
) {
388 * Do the quick schannel bind without a reauth
390 status
= cli_rpc_pipe_open_bind_schannel(cli
,
397 if (!retry
&& NT_STATUS_EQUAL(status
, NT_STATUS_NETWORK_ACCESS_DENIED
)) {
398 DBG_DEBUG("Retrying with serverauthenticate\n");
403 if (!NT_STATUS_IS_OK(status
)) {
404 DBG_DEBUG("cli_rpc_pipe_open_bind_schannel "
405 "failed: %s\n", nt_errstr(status
));
411 if (cli_credentials_is_anonymous(trust_creds
)) {
412 DBG_WARNING("get_trust_credential for %s only gave anonymous,"
413 "unable to negotiate NETLOGON credentials\n",
414 netlogon_creds_cli_debug_string(
416 status
= NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
420 status
= rpccli_setup_netlogon_creds_locked(cli
,
430 if (!NT_STATUS_IS_OK(status
)) {
431 DBG_DEBUG("rpccli_setup_netlogon_creds failed for %s, "
432 "unable to setup NETLOGON credentials: %s\n",
433 netlogon_creds_cli_debug_string(
438 talloc_steal(frame
, rpccli
);
440 if (!(negotiate_flags
& NETLOGON_NEG_AUTHENTICATED_RPC
)) {
441 if (lp_winbind_sealed_pipes() || lp_require_strong_key()) {
442 status
= NT_STATUS_DOWNGRADE_DETECTED
;
443 DBG_WARNING("Unwilling to make connection to %s"
444 "without connection level security, "
445 "must set 'winbind sealed pipes = false'"
446 " and 'require strong key = false' "
448 netlogon_creds_cli_debug_string(
455 * we keep the AUTH_TYPE_NONE rpccli for the
461 status
= cli_rpc_pipe_client_prepare_alter(rpccli
,
462 true, /* new_auth_context */
464 false); /* new_pres_context */
465 if (!NT_STATUS_IS_OK(status
)) {
466 DBG_WARNING("cli_rpc_pipe_client_prepare_alter "
467 "failed: %s\n", nt_errstr(status
));
471 status
= cli_rpc_pipe_client_auth_schannel(rpccli
,
474 if (!NT_STATUS_IS_OK(status
)) {
475 DBG_DEBUG("cli_rpc_pipe_client_auth_schannel "
476 "failed: %s\n", nt_errstr(status
));
480 status
= netlogon_creds_cli_check(creds_ctx
, rpccli
->binding_handle
,
482 if (!NT_STATUS_IS_OK(status
)) {
483 DBG_WARNING("netlogon_creds_cli_check failed: %s\n",
489 *_rpccli
= talloc_move(NULL
, &rpccli
);
490 status
= NT_STATUS_OK
;
492 ZERO_STRUCT(found_session_key
);
498 /* Logon domain user */
500 NTSTATUS
rpccli_netlogon_password_logon(
501 struct netlogon_creds_cli_context
*creds_ctx
,
502 struct dcerpc_binding_handle
*binding_handle
,
504 uint32_t logon_parameters
,
506 const char *username
,
507 const char *password
,
508 const char *workstation
,
509 const uint64_t logon_id
,
510 enum netr_LogonInfoClass logon_type
,
511 uint8_t *authoritative
,
513 uint16_t *_validation_level
,
514 union netr_Validation
**_validation
)
516 TALLOC_CTX
*frame
= talloc_stackframe();
518 union netr_LogonLevel
*logon
;
519 uint16_t validation_level
= 0;
520 union netr_Validation
*validation
= NULL
;
521 char *workstation_slash
= NULL
;
523 unsigned char local_nt_response
[24];
524 unsigned char local_lm_response
[24];
525 struct samr_Password lmpassword
= {.hash
= {0}};
526 struct samr_Password ntpassword
= {.hash
= {0}};
527 struct netr_ChallengeResponse lm
= {0};
528 struct netr_ChallengeResponse nt
= {0};
530 logon
= talloc_zero(frame
, union netr_LogonLevel
);
533 return NT_STATUS_NO_MEMORY
;
536 if (workstation
== NULL
) {
537 workstation
= lp_netbios_name();
540 workstation_slash
= talloc_asprintf(frame
, "\\\\%s", workstation
);
541 if (workstation_slash
== NULL
) {
543 return NT_STATUS_NO_MEMORY
;
546 /* Initialise input parameters */
548 switch (logon_type
) {
549 case NetlogonInteractiveInformation
:
550 case NetlogonInteractiveTransitiveInformation
: {
552 struct netr_PasswordInfo
*password_info
;
555 password_info
= talloc_zero(frame
, struct netr_PasswordInfo
);
556 if (password_info
== NULL
) {
558 return NT_STATUS_NO_MEMORY
;
561 nt_lm_owf_gen(password
, ntpassword
.hash
, lmpassword
.hash
);
563 password_info
->identity_info
.domain_name
.string
= domain
;
564 password_info
->identity_info
.parameter_control
= logon_parameters
;
565 password_info
->identity_info
.logon_id
= logon_id
;
566 password_info
->identity_info
.account_name
.string
= username
;
567 password_info
->identity_info
.workstation
.string
= workstation_slash
;
569 password_info
->lmpassword
= lmpassword
;
570 password_info
->ntpassword
= ntpassword
;
572 logon
->password
= password_info
;
576 case NetlogonNetworkInformation
:
577 case NetlogonNetworkTransitiveInformation
: {
578 struct netr_NetworkInfo
*network_info
;
585 network_info
= talloc_zero(frame
, struct netr_NetworkInfo
);
586 if (network_info
== NULL
) {
588 return NT_STATUS_NO_MEMORY
;
591 generate_random_buffer(chal
, 8);
593 SMBencrypt(password
, chal
, local_lm_response
);
594 rc
= SMBNTencrypt(password
, chal
, local_nt_response
);
597 return gnutls_error_to_ntstatus(rc
, NT_STATUS_ACCESS_DISABLED_BY_POLICY_OTHER
);
601 lm
.data
= local_lm_response
;
604 nt
.data
= local_nt_response
;
606 network_info
->identity_info
.domain_name
.string
= domain
;
607 network_info
->identity_info
.parameter_control
= logon_parameters
;
608 network_info
->identity_info
.logon_id
= logon_id
;
609 network_info
->identity_info
.account_name
.string
= username
;
610 network_info
->identity_info
.workstation
.string
= workstation_slash
;
612 memcpy(network_info
->challenge
, chal
, 8);
613 network_info
->nt
= nt
;
614 network_info
->lm
= lm
;
616 logon
->network
= network_info
;
621 DEBUG(0, ("switch value %d not supported\n",
624 return NT_STATUS_INVALID_INFO_CLASS
;
627 status
= netlogon_creds_cli_LogonSamLogon(creds_ctx
,
636 if (!NT_STATUS_IS_OK(status
)) {
642 *_validation_level
= validation_level
;
643 *_validation
= validation
;
649 * Logon domain user with an 'network' SAM logon
651 * @param info3 Pointer to a NET_USER_INFO_3 already allocated by the caller.
655 NTSTATUS
rpccli_netlogon_network_logon(
656 struct netlogon_creds_cli_context
*creds_ctx
,
657 struct dcerpc_binding_handle
*binding_handle
,
659 uint32_t logon_parameters
,
660 const char *username
,
662 const char *workstation
,
663 const uint64_t logon_id
,
665 DATA_BLOB lm_response
,
666 DATA_BLOB nt_response
,
667 enum netr_LogonInfoClass logon_type
,
668 uint8_t *authoritative
,
670 uint16_t *_validation_level
,
671 union netr_Validation
**_validation
)
674 const char *workstation_name_slash
;
675 union netr_LogonLevel
*logon
= NULL
;
676 struct netr_NetworkInfo
*network_info
;
677 uint16_t validation_level
= 0;
678 union netr_Validation
*validation
= NULL
;
679 struct netr_ChallengeResponse lm
;
680 struct netr_ChallengeResponse nt
;
687 switch (logon_type
) {
688 case NetlogonNetworkInformation
:
689 case NetlogonNetworkTransitiveInformation
:
692 DEBUG(0, ("switch value %d not supported\n",
694 return NT_STATUS_INVALID_INFO_CLASS
;
697 logon
= talloc_zero(mem_ctx
, union netr_LogonLevel
);
699 return NT_STATUS_NO_MEMORY
;
702 network_info
= talloc_zero(mem_ctx
, struct netr_NetworkInfo
);
704 return NT_STATUS_NO_MEMORY
;
707 if (workstation
== NULL
) {
708 workstation
= lp_netbios_name();
711 if (workstation
[0] != '\\' && workstation
[1] != '\\') {
712 workstation_name_slash
= talloc_asprintf(mem_ctx
, "\\\\%s", workstation
);
714 workstation_name_slash
= workstation
;
717 if (!workstation_name_slash
) {
718 DEBUG(0, ("talloc_asprintf failed!\n"));
719 return NT_STATUS_NO_MEMORY
;
722 /* Initialise input parameters */
724 lm
.data
= lm_response
.data
;
725 lm
.length
= lm_response
.length
;
726 nt
.data
= nt_response
.data
;
727 nt
.length
= nt_response
.length
;
729 network_info
->identity_info
.domain_name
.string
= domain
;
730 network_info
->identity_info
.parameter_control
= logon_parameters
;
731 network_info
->identity_info
.logon_id
= logon_id
;
732 network_info
->identity_info
.account_name
.string
= username
;
733 network_info
->identity_info
.workstation
.string
= workstation_name_slash
;
735 if (chal
.length
!= 8) {
736 DBG_WARNING("Invalid challenge length %zd\n", chal
.length
);
737 return NT_STATUS_INVALID_PARAMETER
;
740 memcpy(network_info
->challenge
, chal
.data
, chal
.length
);
741 network_info
->nt
= nt
;
742 network_info
->lm
= lm
;
744 logon
->network
= network_info
;
746 /* Marshall data and send request */
748 status
= netlogon_creds_cli_LogonSamLogon(creds_ctx
,
757 if (!NT_STATUS_IS_OK(status
)) {
761 *_validation_level
= validation_level
;
762 *_validation
= validation
;
767 NTSTATUS
rpccli_netlogon_interactive_logon(
768 struct netlogon_creds_cli_context
*creds_ctx
,
769 struct dcerpc_binding_handle
*binding_handle
,
771 uint32_t logon_parameters
,
772 const char *username
,
774 const char *workstation
,
775 const uint64_t logon_id
,
778 enum netr_LogonInfoClass logon_type
,
779 uint8_t *authoritative
,
781 uint16_t *_validation_level
,
782 union netr_Validation
**_validation
)
784 TALLOC_CTX
*frame
= talloc_stackframe();
786 const char *workstation_name_slash
;
787 union netr_LogonLevel
*logon
= NULL
;
788 struct netr_PasswordInfo
*password_info
= NULL
;
789 uint16_t validation_level
= 0;
790 union netr_Validation
*validation
= NULL
;
791 struct netr_ChallengeResponse lm
;
792 struct netr_ChallengeResponse nt
;
799 switch (logon_type
) {
800 case NetlogonInteractiveInformation
:
801 case NetlogonInteractiveTransitiveInformation
:
804 DEBUG(0, ("switch value %d not supported\n",
807 return NT_STATUS_INVALID_INFO_CLASS
;
810 logon
= talloc_zero(mem_ctx
, union netr_LogonLevel
);
813 return NT_STATUS_NO_MEMORY
;
816 password_info
= talloc_zero(logon
, struct netr_PasswordInfo
);
817 if (password_info
== NULL
) {
819 return NT_STATUS_NO_MEMORY
;
822 if (workstation
[0] != '\\' && workstation
[1] != '\\') {
823 workstation_name_slash
= talloc_asprintf(frame
, "\\\\%s", workstation
);
825 workstation_name_slash
= workstation
;
828 if (workstation_name_slash
== NULL
) {
830 return NT_STATUS_NO_MEMORY
;
833 /* Initialise input parameters */
835 password_info
->identity_info
.domain_name
.string
= domain
;
836 password_info
->identity_info
.parameter_control
= logon_parameters
;
837 password_info
->identity_info
.logon_id
= logon_id
;
838 password_info
->identity_info
.account_name
.string
= username
;
839 password_info
->identity_info
.workstation
.string
= workstation_name_slash
;
841 if (nt_hash
.length
!= sizeof(password_info
->ntpassword
.hash
)) {
843 return NT_STATUS_INVALID_PARAMETER
;
845 memcpy(password_info
->ntpassword
.hash
, nt_hash
.data
, nt_hash
.length
);
846 if (lm_hash
.length
!= 0) {
847 if (lm_hash
.length
!= sizeof(password_info
->lmpassword
.hash
)) {
849 return NT_STATUS_INVALID_PARAMETER
;
851 memcpy(password_info
->lmpassword
.hash
, lm_hash
.data
, lm_hash
.length
);
854 logon
->password
= password_info
;
856 /* Marshall data and send request */
858 status
= netlogon_creds_cli_LogonSamLogon(creds_ctx
,
867 if (!NT_STATUS_IS_OK(status
)) {
872 *_validation_level
= validation_level
;
873 *_validation
= validation
;