2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1997,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6 * Copyright (C) Paul Ashton 1997.
7 * Copyright (C) Jeremy Allison 1998-2001.
8 * Copyright (C) Andrew Bartlett 2001.
9 * Copyright (C) Guenther Deschner 2008-2009.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 /* This is the implementation of the netlogon pipe. */
28 #include "system/passwd.h" /* uid_wrapper */
30 #include "../libcli/auth/schannel.h"
31 #include "../librpc/gen_ndr/srv_netlogon.h"
32 #include "../librpc/gen_ndr/ndr_samr_c.h"
33 #include "../librpc/gen_ndr/ndr_lsa_c.h"
34 #include "rpc_client/cli_lsarpc.h"
35 #include "rpc_client/init_lsa.h"
36 #include "rpc_server/rpc_ncacn_np.h"
37 #include "../libcli/security/security.h"
38 #include "../libcli/security/dom_sid.h"
39 #include "librpc/gen_ndr/ndr_drsblobs.h"
40 #include "lib/crypto/arcfour.h"
41 #include "lib/crypto/md4.h"
42 #include "nsswitch/libwbclient/wbclient.h"
43 #include "../libcli/registry/util_reg.h"
47 #include "../lib/tsocket/tsocket.h"
48 #include "lib/param/param.h"
50 extern userdom_struct current_user_info;
53 #define DBGC_CLASS DBGC_RPC_SRV
55 struct netlogon_server_pipe_state {
56 struct netr_Credential client_challenge;
57 struct netr_Credential server_challenge;
60 /*************************************************************************
62 *************************************************************************/
64 WERROR _netr_LogonControl(struct pipes_struct *p,
65 struct netr_LogonControl *r)
67 struct netr_LogonControl2Ex l;
69 switch (r->in.level) {
73 return WERR_NOT_SUPPORTED;
75 return WERR_UNKNOWN_LEVEL;
78 l.in.logon_server = r->in.logon_server;
79 l.in.function_code = r->in.function_code;
80 l.in.level = r->in.level;
82 l.out.query = r->out.query;
84 return _netr_LogonControl2Ex(p, &l);
87 /*************************************************************************
89 *************************************************************************/
91 WERROR _netr_LogonControl2(struct pipes_struct *p,
92 struct netr_LogonControl2 *r)
94 struct netr_LogonControl2Ex l;
96 l.in.logon_server = r->in.logon_server;
97 l.in.function_code = r->in.function_code;
98 l.in.level = r->in.level;
99 l.in.data = r->in.data;
100 l.out.query = r->out.query;
102 return _netr_LogonControl2Ex(p, &l);
105 /*************************************************************************
106 *************************************************************************/
108 static bool wb_change_trust_creds(const char *domain, WERROR *tc_status)
111 struct wbcAuthErrorInfo *error = NULL;
113 result = wbcChangeTrustCredentials(domain, &error);
115 case WBC_ERR_WINBIND_NOT_AVAILABLE:
117 case WBC_ERR_DOMAIN_NOT_FOUND:
118 *tc_status = WERR_NO_SUCH_DOMAIN;
120 case WBC_ERR_SUCCESS:
121 *tc_status = WERR_OK;
127 if (error && error->nt_status != 0) {
128 *tc_status = ntstatus_to_werror(NT_STATUS(error->nt_status));
130 *tc_status = WERR_TRUST_FAILURE;
132 wbcFreeMemory(error);
136 /*************************************************************************
137 *************************************************************************/
139 static bool wb_check_trust_creds(const char *domain, WERROR *tc_status)
142 struct wbcAuthErrorInfo *error = NULL;
144 result = wbcCheckTrustCredentials(domain, &error);
146 case WBC_ERR_WINBIND_NOT_AVAILABLE:
148 case WBC_ERR_DOMAIN_NOT_FOUND:
149 *tc_status = WERR_NO_SUCH_DOMAIN;
151 case WBC_ERR_SUCCESS:
152 *tc_status = WERR_OK;
158 if (error && error->nt_status != 0) {
159 *tc_status = ntstatus_to_werror(NT_STATUS(error->nt_status));
161 *tc_status = WERR_TRUST_FAILURE;
163 wbcFreeMemory(error);
167 /****************************************************************
168 _netr_LogonControl2Ex
169 ****************************************************************/
171 WERROR _netr_LogonControl2Ex(struct pipes_struct *p,
172 struct netr_LogonControl2Ex *r)
174 uint32_t flags = 0x0;
175 WERROR pdc_connection_status = WERR_OK;
176 uint32_t logon_attempts = 0x0;
179 const char *dc_name = NULL;
180 struct sockaddr_storage dc_ss;
181 const char *domain = NULL;
182 struct netr_NETLOGON_INFO_1 *info1;
183 struct netr_NETLOGON_INFO_2 *info2;
184 struct netr_NETLOGON_INFO_3 *info3;
185 struct netr_NETLOGON_INFO_4 *info4;
190 case NDR_NETR_LOGONCONTROL:
191 fn = "_netr_LogonControl";
193 case NDR_NETR_LOGONCONTROL2:
194 fn = "_netr_LogonControl2";
196 case NDR_NETR_LOGONCONTROL2EX:
197 fn = "_netr_LogonControl2Ex";
200 return WERR_INVALID_PARAM;
203 acct_ctrl = p->session_info->info->acct_flags;
205 switch (r->in.function_code) {
206 case NETLOGON_CONTROL_TC_VERIFY:
207 case NETLOGON_CONTROL_CHANGE_PASSWORD:
208 case NETLOGON_CONTROL_REDISCOVER:
209 if ((geteuid() != sec_initial_uid()) &&
210 !nt_token_check_domain_rid(p->session_info->security_token, DOMAIN_RID_ADMINS) &&
211 !nt_token_check_sid(&global_sid_Builtin_Administrators, p->session_info->security_token) &&
212 !(acct_ctrl & (ACB_WSTRUST | ACB_SVRTRUST))) {
213 return WERR_ACCESS_DENIED;
220 tc_status = WERR_NO_SUCH_DOMAIN;
222 switch (r->in.function_code) {
223 case NETLOGON_CONTROL_QUERY:
226 case NETLOGON_CONTROL_REPLICATE:
227 case NETLOGON_CONTROL_SYNCHRONIZE:
228 case NETLOGON_CONTROL_PDC_REPLICATE:
229 case NETLOGON_CONTROL_BACKUP_CHANGE_LOG:
230 case NETLOGON_CONTROL_BREAKPOINT:
231 if (acct_ctrl & ACB_NORMAL) {
232 return WERR_NOT_SUPPORTED;
233 } else if (acct_ctrl & (ACB_WSTRUST | ACB_SVRTRUST)) {
234 return WERR_ACCESS_DENIED;
236 return WERR_ACCESS_DENIED;
238 case NETLOGON_CONTROL_TRUNCATE_LOG:
239 if (acct_ctrl & ACB_NORMAL) {
241 } else if (acct_ctrl & (ACB_WSTRUST | ACB_SVRTRUST)) {
242 return WERR_ACCESS_DENIED;
244 return WERR_ACCESS_DENIED;
247 case NETLOGON_CONTROL_TRANSPORT_NOTIFY:
248 case NETLOGON_CONTROL_FORCE_DNS_REG:
249 case NETLOGON_CONTROL_QUERY_DNS_REG:
250 return WERR_NOT_SUPPORTED;
251 case NETLOGON_CONTROL_FIND_USER:
252 if (!r->in.data || !r->in.data->user) {
253 return WERR_NOT_SUPPORTED;
256 case NETLOGON_CONTROL_SET_DBFLAG:
258 return WERR_NOT_SUPPORTED;
261 case NETLOGON_CONTROL_TC_VERIFY:
262 if (!r->in.data || !r->in.data->domain) {
263 return WERR_NOT_SUPPORTED;
266 if (!wb_check_trust_creds(r->in.data->domain, &tc_status)) {
267 return WERR_NOT_SUPPORTED;
270 case NETLOGON_CONTROL_TC_QUERY:
271 if (!r->in.data || !r->in.data->domain) {
272 return WERR_NOT_SUPPORTED;
275 domain = r->in.data->domain;
277 if (!is_trusted_domain(domain)) {
281 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
282 tc_status = WERR_NO_LOGON_SERVERS;
286 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
295 case NETLOGON_CONTROL_REDISCOVER:
296 if (!r->in.data || !r->in.data->domain) {
297 return WERR_NOT_SUPPORTED;
300 domain = r->in.data->domain;
302 if (!is_trusted_domain(domain)) {
306 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
307 tc_status = WERR_NO_LOGON_SERVERS;
311 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
320 case NETLOGON_CONTROL_CHANGE_PASSWORD:
321 if (!r->in.data || !r->in.data->domain) {
322 return WERR_NOT_SUPPORTED;
325 if (!wb_change_trust_creds(r->in.data->domain, &tc_status)) {
326 return WERR_NOT_SUPPORTED;
331 /* no idea what this should be */
332 DEBUG(0,("%s: unimplemented function level [%d]\n",
333 fn, r->in.function_code));
334 return WERR_UNKNOWN_LEVEL;
337 /* prepare the response */
339 switch (r->in.level) {
341 info1 = talloc_zero(p->mem_ctx, struct netr_NETLOGON_INFO_1);
342 W_ERROR_HAVE_NO_MEMORY(info1);
344 info1->flags = flags;
345 info1->pdc_connection_status = pdc_connection_status;
347 r->out.query->info1 = info1;
350 info2 = talloc_zero(p->mem_ctx, struct netr_NETLOGON_INFO_2);
351 W_ERROR_HAVE_NO_MEMORY(info2);
353 info2->flags = flags;
354 info2->pdc_connection_status = pdc_connection_status;
355 info2->trusted_dc_name = dc_name;
356 info2->tc_connection_status = tc_status;
358 r->out.query->info2 = info2;
361 info3 = talloc_zero(p->mem_ctx, struct netr_NETLOGON_INFO_3);
362 W_ERROR_HAVE_NO_MEMORY(info3);
364 info3->flags = flags;
365 info3->logon_attempts = logon_attempts;
367 r->out.query->info3 = info3;
370 info4 = talloc_zero(p->mem_ctx, struct netr_NETLOGON_INFO_4);
371 W_ERROR_HAVE_NO_MEMORY(info4);
373 info4->trusted_dc_name = dc_name;
374 info4->trusted_domain_name = r->in.data->domain;
376 r->out.query->info4 = info4;
379 return WERR_UNKNOWN_LEVEL;
385 /*************************************************************************
386 _netr_NetrEnumerateTrustedDomains
387 *************************************************************************/
389 NTSTATUS _netr_NetrEnumerateTrustedDomains(struct pipes_struct *p,
390 struct netr_NetrEnumerateTrustedDomains *r)
393 NTSTATUS result = NT_STATUS_OK;
396 const char **trusted_domains = NULL;
397 struct lsa_DomainList domain_list;
398 struct dcerpc_binding_handle *h = NULL;
399 struct policy_handle pol;
400 uint32_t enum_ctx = 0;
402 uint32_t max_size = (uint32_t)-1;
404 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
406 status = rpcint_binding_handle(p->mem_ctx,
412 if (!NT_STATUS_IS_OK(status)) {
416 status = dcerpc_lsa_open_policy2(h,
420 LSA_POLICY_VIEW_LOCAL_INFORMATION,
423 if (!NT_STATUS_IS_OK(status)) {
426 if (!NT_STATUS_IS_OK(result)) {
432 /* Lookup list of trusted domains */
433 status = dcerpc_lsa_EnumTrustDom(h,
440 if (!NT_STATUS_IS_OK(status)) {
443 if (!NT_STATUS_IS_OK(result) &&
444 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
445 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
450 for (i = 0; i < domain_list.count; i++) {
451 if (!add_string_to_array(p->mem_ctx, domain_list.domains[i].name.string,
452 &trusted_domains, &num_domains)) {
453 status = NT_STATUS_NO_MEMORY;
457 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
459 if (num_domains > 0) {
460 /* multi sz terminate */
461 trusted_domains = talloc_realloc(p->mem_ctx, trusted_domains, const char *, num_domains + 1);
462 if (trusted_domains == NULL) {
463 status = NT_STATUS_NO_MEMORY;
467 trusted_domains[num_domains] = NULL;
470 if (!push_reg_multi_sz(trusted_domains, &blob, trusted_domains)) {
471 TALLOC_FREE(trusted_domains);
472 status = NT_STATUS_NO_MEMORY;
476 r->out.trusted_domains_blob->data = blob.data;
477 r->out.trusted_domains_blob->length = blob.length;
479 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
481 status = NT_STATUS_OK;
484 if (is_valid_policy_hnd(&pol)) {
485 dcerpc_lsa_Close(h, p->mem_ctx, &pol, &result);
491 /*************************************************************************
492 *************************************************************************/
494 static NTSTATUS samr_find_machine_account(TALLOC_CTX *mem_ctx,
495 struct dcerpc_binding_handle *b,
496 const char *account_name,
497 uint32_t access_mask,
498 struct dom_sid2 **domain_sid_p,
499 uint32_t *user_rid_p,
500 struct policy_handle *user_handle)
503 NTSTATUS result = NT_STATUS_OK;
504 struct policy_handle connect_handle, domain_handle;
505 struct lsa_String domain_name;
506 struct dom_sid2 *domain_sid;
507 struct lsa_String names;
508 struct samr_Ids rids;
509 struct samr_Ids types;
512 status = dcerpc_samr_Connect2(b, mem_ctx,
514 SAMR_ACCESS_CONNECT_TO_SERVER |
515 SAMR_ACCESS_ENUM_DOMAINS |
516 SAMR_ACCESS_LOOKUP_DOMAIN,
519 if (!NT_STATUS_IS_OK(status)) {
522 if (!NT_STATUS_IS_OK(result)) {
527 init_lsa_String(&domain_name, get_global_sam_name());
529 status = dcerpc_samr_LookupDomain(b, mem_ctx,
534 if (!NT_STATUS_IS_OK(status)) {
537 if (!NT_STATUS_IS_OK(result)) {
542 status = dcerpc_samr_OpenDomain(b, mem_ctx,
544 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
548 if (!NT_STATUS_IS_OK(status)) {
551 if (!NT_STATUS_IS_OK(result)) {
556 init_lsa_String(&names, account_name);
558 status = dcerpc_samr_LookupNames(b, mem_ctx,
565 if (!NT_STATUS_IS_OK(status)) {
568 if (!NT_STATUS_IS_OK(result)) {
573 if (rids.count != 1) {
574 status = NT_STATUS_NO_SUCH_USER;
577 if (rids.count != types.count) {
578 status = NT_STATUS_INVALID_PARAMETER;
581 if (types.ids[0] != SID_NAME_USER) {
582 status = NT_STATUS_NO_SUCH_USER;
588 status = dcerpc_samr_OpenUser(b, mem_ctx,
594 if (!NT_STATUS_IS_OK(status)) {
597 if (!NT_STATUS_IS_OK(result)) {
607 *domain_sid_p = domain_sid;
611 if (is_valid_policy_hnd(&domain_handle)) {
612 dcerpc_samr_Close(b, mem_ctx, &domain_handle, &result);
614 if (is_valid_policy_hnd(&connect_handle)) {
615 dcerpc_samr_Close(b, mem_ctx, &connect_handle, &result);
621 /******************************************************************
622 gets a machine password entry. checks access rights of the host.
623 ******************************************************************/
625 static NTSTATUS get_md4pw(struct samr_Password *md4pw, const char *mach_acct,
626 enum netr_SchannelType sec_chan_type,
628 struct messaging_context *msg_ctx)
631 NTSTATUS result = NT_STATUS_OK;
633 struct dcerpc_binding_handle *h = NULL;
634 struct tsocket_address *local;
635 struct policy_handle user_handle;
637 struct dom_sid *domain_sid;
639 union samr_UserInfo *info;
640 struct auth_session_info *session_info;
646 * Currently this code is redundent as we already have a filter
647 * by hostname list. What this code really needs to do is to
648 * get a hosts allowed/hosts denied list from the SAM database
649 * on a per user basis, and make the access decision there.
650 * I will leave this code here for now as a reminder to implement
651 * this at a later date. JRA.
654 if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
656 p->client_id.addr)) {
657 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
662 mem_ctx = talloc_stackframe();
663 if (mem_ctx == NULL) {
664 status = NT_STATUS_NO_MEMORY;
668 status = make_session_info_system(mem_ctx, &session_info);
669 if (!NT_STATUS_IS_OK(status)) {
673 ZERO_STRUCT(user_handle);
675 rc = tsocket_address_inet_from_strings(mem_ctx,
681 status = NT_STATUS_NO_MEMORY;
685 status = rpcint_binding_handle(mem_ctx,
691 if (!NT_STATUS_IS_OK(status)) {
696 status = samr_find_machine_account(mem_ctx, h, mach_acct,
697 SEC_FLAG_MAXIMUM_ALLOWED,
698 &domain_sid, &user_rid,
701 if (!NT_STATUS_IS_OK(status)) {
705 status = dcerpc_samr_QueryUserInfo2(h,
708 UserControlInformation,
711 if (!NT_STATUS_IS_OK(status)) {
714 if (!NT_STATUS_IS_OK(result)) {
719 acct_ctrl = info->info16.acct_flags;
721 if (acct_ctrl & ACB_DISABLED) {
722 DEBUG(0,("get_md4pw: Workstation %s: account is disabled\n", mach_acct));
723 status = NT_STATUS_ACCOUNT_DISABLED;
727 if (!(acct_ctrl & ACB_SVRTRUST) &&
728 !(acct_ctrl & ACB_WSTRUST) &&
729 !(acct_ctrl & ACB_DOMTRUST))
731 DEBUG(0,("get_md4pw: Workstation %s: account is not a trust account\n", mach_acct));
732 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
736 switch (sec_chan_type) {
738 if (!(acct_ctrl & ACB_SVRTRUST)) {
739 DEBUG(0,("get_md4pw: Workstation %s: BDC secure channel requested "
740 "but not a server trust account\n", mach_acct));
741 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
746 if (!(acct_ctrl & ACB_WSTRUST)) {
747 DEBUG(0,("get_md4pw: Workstation %s: WORKSTATION secure channel requested "
748 "but not a workstation trust account\n", mach_acct));
749 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
753 case SEC_CHAN_DOMAIN:
754 if (!(acct_ctrl & ACB_DOMTRUST)) {
755 DEBUG(0,("get_md4pw: Workstation %s: DOMAIN secure channel requested "
756 "but not a interdomain trust account\n", mach_acct));
757 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
766 status = dcerpc_samr_QueryUserInfo2(h,
769 UserInternal1Information,
773 if (!NT_STATUS_IS_OK(status)) {
776 if (!NT_STATUS_IS_OK(result)) {
781 if (info->info18.nt_pwd_active == 0) {
782 DEBUG(0,("get_md4pw: Workstation %s: account does not have a password\n", mach_acct));
783 status = NT_STATUS_LOGON_FAILURE;
787 /* samr gives out nthash unencrypted (!) */
788 memcpy(md4pw->hash, info->info18.nt_pwd.hash, 16);
790 sid_compose(sid, domain_sid, user_rid);
793 if (h && is_valid_policy_hnd(&user_handle)) {
794 dcerpc_samr_Close(h, mem_ctx, &user_handle, &result);
797 talloc_free(mem_ctx);
802 /*************************************************************************
803 _netr_ServerReqChallenge
804 *************************************************************************/
806 NTSTATUS _netr_ServerReqChallenge(struct pipes_struct *p,
807 struct netr_ServerReqChallenge *r)
809 struct netlogon_server_pipe_state *pipe_state =
810 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
813 DEBUG(10,("_netr_ServerReqChallenge: new challenge requested. Clearing old state.\n"));
814 talloc_free(pipe_state);
815 p->private_data = NULL;
818 pipe_state = talloc(p, struct netlogon_server_pipe_state);
819 NT_STATUS_HAVE_NO_MEMORY(pipe_state);
821 pipe_state->client_challenge = *r->in.credentials;
823 generate_random_buffer(pipe_state->server_challenge.data,
824 sizeof(pipe_state->server_challenge.data));
826 *r->out.return_credentials = pipe_state->server_challenge;
828 p->private_data = pipe_state;
833 /*************************************************************************
834 _netr_ServerAuthenticate
835 Create the initial credentials.
836 *************************************************************************/
838 NTSTATUS _netr_ServerAuthenticate(struct pipes_struct *p,
839 struct netr_ServerAuthenticate *r)
841 struct netr_ServerAuthenticate3 a;
842 uint32_t negotiate_flags = 0;
845 a.in.server_name = r->in.server_name;
846 a.in.account_name = r->in.account_name;
847 a.in.secure_channel_type = r->in.secure_channel_type;
848 a.in.computer_name = r->in.computer_name;
849 a.in.credentials = r->in.credentials;
850 a.in.negotiate_flags = &negotiate_flags;
852 a.out.return_credentials = r->out.return_credentials;
854 a.out.negotiate_flags = &negotiate_flags;
856 return _netr_ServerAuthenticate3(p, &a);
860 /*************************************************************************
861 _netr_ServerAuthenticate3
862 *************************************************************************/
864 NTSTATUS _netr_ServerAuthenticate3(struct pipes_struct *p,
865 struct netr_ServerAuthenticate3 *r)
869 /* r->in.negotiate_flags is an aliased pointer to r->out.negotiate_flags,
870 * so use a copy to avoid destroying the client values. */
871 uint32_t in_neg_flags = *r->in.negotiate_flags;
873 struct loadparm_context *lp_ctx;
875 struct samr_Password mach_pwd;
876 struct netlogon_creds_CredentialState *creds;
877 struct netlogon_server_pipe_state *pipe_state =
878 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
880 /* According to Microsoft (see bugid #6099)
881 * Windows 7 looks at the negotiate_flags
882 * returned in this structure *even if the
883 * call fails with access denied* ! So in order
884 * to allow Win7 to connect to a Samba NT style
885 * PDC we set the flags before we know if it's
890 srv_flgs = NETLOGON_NEG_ACCOUNT_LOCKOUT |
891 NETLOGON_NEG_PERSISTENT_SAMREPL |
892 NETLOGON_NEG_ARCFOUR |
893 NETLOGON_NEG_PROMOTION_COUNT |
894 NETLOGON_NEG_CHANGELOG_BDC |
895 NETLOGON_NEG_FULL_SYNC_REPL |
896 NETLOGON_NEG_MULTIPLE_SIDS |
898 NETLOGON_NEG_PASSWORD_CHANGE_REFUSAL |
899 NETLOGON_NEG_PASSWORD_SET2;
901 /* Ensure we support strong (128-bit) keys. */
902 if (in_neg_flags & NETLOGON_NEG_STRONG_KEYS) {
903 srv_flgs |= NETLOGON_NEG_STRONG_KEYS;
906 if (lp_server_schannel() != false) {
907 srv_flgs |= NETLOGON_NEG_SCHANNEL;
911 case NDR_NETR_SERVERAUTHENTICATE:
912 fn = "_netr_ServerAuthenticate";
914 case NDR_NETR_SERVERAUTHENTICATE2:
915 fn = "_netr_ServerAuthenticate2";
917 case NDR_NETR_SERVERAUTHENTICATE3:
918 fn = "_netr_ServerAuthenticate3";
921 return NT_STATUS_INTERNAL_ERROR;
924 /* We use this as the key to store the creds: */
925 /* r->in.computer_name */
928 DEBUG(0,("%s: no challenge sent to client %s\n", fn,
929 r->in.computer_name));
930 status = NT_STATUS_ACCESS_DENIED;
934 if ( (lp_server_schannel() == true) &&
935 ((in_neg_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
937 /* schannel must be used, but client did not offer it. */
938 DEBUG(0,("%s: schannel required but client failed "
939 "to offer it. Client was %s\n",
940 fn, r->in.account_name));
941 status = NT_STATUS_ACCESS_DENIED;
945 status = get_md4pw(&mach_pwd,
947 r->in.secure_channel_type,
949 if (!NT_STATUS_IS_OK(status)) {
950 DEBUG(0,("%s: failed to get machine password for "
952 fn, r->in.account_name, nt_errstr(status) ));
953 /* always return NT_STATUS_ACCESS_DENIED */
954 status = NT_STATUS_ACCESS_DENIED;
958 /* From the client / server challenges and md4 password, generate sess key */
959 /* Check client credentials are valid. */
960 creds = netlogon_creds_server_init(p->mem_ctx,
963 r->in.secure_channel_type,
964 &pipe_state->client_challenge,
965 &pipe_state->server_challenge,
968 r->out.return_credentials,
969 *r->in.negotiate_flags);
971 DEBUG(0,("%s: netlogon_creds_server_check failed. Rejecting auth "
972 "request from client %s machine account %s\n",
973 fn, r->in.computer_name,
974 r->in.account_name));
975 status = NT_STATUS_ACCESS_DENIED;
979 creds->sid = dom_sid_dup(creds, &sid);
981 status = NT_STATUS_NO_MEMORY;
985 lp_ctx = loadparm_init_s3(p->mem_ctx, loadparm_s3_context());
986 if (lp_ctx == NULL) {
987 DEBUG(10, ("loadparm_init_s3 failed\n"));
988 status = NT_STATUS_INTERNAL_ERROR;
992 /* Store off the state so we can continue after client disconnect. */
994 status = schannel_save_creds_state(p->mem_ctx, lp_ctx, creds);
997 talloc_unlink(p->mem_ctx, lp_ctx);
999 if (!NT_STATUS_IS_OK(status)) {
1003 sid_peek_rid(&sid, r->out.rid);
1005 status = NT_STATUS_OK;
1009 *r->out.negotiate_flags = srv_flgs;
1013 /*************************************************************************
1014 _netr_ServerAuthenticate2
1015 *************************************************************************/
1017 NTSTATUS _netr_ServerAuthenticate2(struct pipes_struct *p,
1018 struct netr_ServerAuthenticate2 *r)
1020 struct netr_ServerAuthenticate3 a;
1023 a.in.server_name = r->in.server_name;
1024 a.in.account_name = r->in.account_name;
1025 a.in.secure_channel_type = r->in.secure_channel_type;
1026 a.in.computer_name = r->in.computer_name;
1027 a.in.credentials = r->in.credentials;
1028 a.in.negotiate_flags = r->in.negotiate_flags;
1030 a.out.return_credentials = r->out.return_credentials;
1032 a.out.negotiate_flags = r->out.negotiate_flags;
1034 return _netr_ServerAuthenticate3(p, &a);
1037 /*************************************************************************
1038 * If schannel is required for this call test that it actually is available.
1039 *************************************************************************/
1040 static NTSTATUS schannel_check_required(struct pipe_auth_data *auth_info,
1041 const char *computer_name,
1042 bool integrity, bool privacy)
1044 if (auth_info && auth_info->auth_type == DCERPC_AUTH_TYPE_SCHANNEL) {
1045 if (!privacy && !integrity) {
1046 return NT_STATUS_OK;
1049 if ((!privacy && integrity) &&
1050 auth_info->auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
1051 return NT_STATUS_OK;
1054 if ((privacy || integrity) &&
1055 auth_info->auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
1056 return NT_STATUS_OK;
1060 /* test didn't pass */
1061 DEBUG(0, ("schannel_check_required: [%s] is not using schannel\n",
1064 return NT_STATUS_ACCESS_DENIED;
1067 /*************************************************************************
1068 *************************************************************************/
1070 static NTSTATUS netr_creds_server_step_check(struct pipes_struct *p,
1071 TALLOC_CTX *mem_ctx,
1072 const char *computer_name,
1073 struct netr_Authenticator *received_authenticator,
1074 struct netr_Authenticator *return_authenticator,
1075 struct netlogon_creds_CredentialState **creds_out)
1078 bool schannel_global_required = (lp_server_schannel() == true) ? true:false;
1079 struct loadparm_context *lp_ctx;
1081 if (schannel_global_required) {
1082 status = schannel_check_required(&p->auth,
1085 if (!NT_STATUS_IS_OK(status)) {
1090 lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_context());
1091 if (lp_ctx == NULL) {
1092 DEBUG(0, ("loadparm_init_s3 failed\n"));
1093 return NT_STATUS_INTERNAL_ERROR;
1096 status = schannel_check_creds_state(mem_ctx, lp_ctx,
1097 computer_name, received_authenticator,
1098 return_authenticator, creds_out);
1099 talloc_unlink(mem_ctx, lp_ctx);
1103 /*************************************************************************
1104 *************************************************************************/
1106 static NTSTATUS netr_set_machine_account_password(TALLOC_CTX *mem_ctx,
1107 struct auth_session_info *session_info,
1108 struct messaging_context *msg_ctx,
1109 const char *account_name,
1110 struct samr_Password *nt_hash)
1113 NTSTATUS result = NT_STATUS_OK;
1114 struct dcerpc_binding_handle *h = NULL;
1115 struct tsocket_address *local;
1116 struct policy_handle user_handle;
1118 union samr_UserInfo *info;
1119 struct samr_UserInfo18 info18;
1123 ZERO_STRUCT(user_handle);
1125 rc = tsocket_address_inet_from_strings(mem_ctx,
1131 status = NT_STATUS_NO_MEMORY;
1135 status = rpcint_binding_handle(mem_ctx,
1141 if (!NT_STATUS_IS_OK(status)) {
1146 status = samr_find_machine_account(mem_ctx,
1149 SEC_FLAG_MAXIMUM_ALLOWED,
1154 if (!NT_STATUS_IS_OK(status)) {
1158 status = dcerpc_samr_QueryUserInfo2(h,
1161 UserControlInformation,
1164 if (!NT_STATUS_IS_OK(status)) {
1167 if (!NT_STATUS_IS_OK(result)) {
1172 acct_ctrl = info->info16.acct_flags;
1174 if (!(acct_ctrl & ACB_WSTRUST ||
1175 acct_ctrl & ACB_SVRTRUST ||
1176 acct_ctrl & ACB_DOMTRUST)) {
1177 status = NT_STATUS_NO_SUCH_USER;
1181 if (acct_ctrl & ACB_DISABLED) {
1182 status = NT_STATUS_ACCOUNT_DISABLED;
1186 ZERO_STRUCT(info18);
1188 in = data_blob_const(nt_hash->hash, 16);
1189 out = data_blob_talloc_zero(mem_ctx, 16);
1190 sess_crypt_blob(&out, &in, &session_info->session_key, true);
1191 memcpy(info18.nt_pwd.hash, out.data, out.length);
1193 info18.nt_pwd_active = true;
1195 info->info18 = info18;
1198 status = dcerpc_samr_SetUserInfo2(h,
1201 UserInternal1Information,
1205 if (!NT_STATUS_IS_OK(status)) {
1208 if (!NT_STATUS_IS_OK(result)) {
1214 if (h && is_valid_policy_hnd(&user_handle)) {
1215 dcerpc_samr_Close(h, mem_ctx, &user_handle, &result);
1221 /*************************************************************************
1222 _netr_ServerPasswordSet
1223 *************************************************************************/
1225 NTSTATUS _netr_ServerPasswordSet(struct pipes_struct *p,
1226 struct netr_ServerPasswordSet *r)
1228 NTSTATUS status = NT_STATUS_OK;
1230 struct netlogon_creds_CredentialState *creds;
1232 DEBUG(5,("_netr_ServerPasswordSet: %d\n", __LINE__));
1235 status = netr_creds_server_step_check(p, p->mem_ctx,
1236 r->in.computer_name,
1238 r->out.return_authenticator,
1242 if (!NT_STATUS_IS_OK(status)) {
1243 DEBUG(2,("_netr_ServerPasswordSet: netlogon_creds_server_step failed. Rejecting auth "
1244 "request from client %s machine account %s\n",
1245 r->in.computer_name, creds->computer_name));
1250 DEBUG(3,("_netr_ServerPasswordSet: Server Password Set by remote machine:[%s] on account [%s]\n",
1251 r->in.computer_name, creds->computer_name));
1253 netlogon_creds_des_decrypt(creds, r->in.new_password);
1255 DEBUG(100,("_netr_ServerPasswordSet: new given value was :\n"));
1256 for(i = 0; i < sizeof(r->in.new_password->hash); i++)
1257 DEBUG(100,("%02X ", r->in.new_password->hash[i]));
1260 status = netr_set_machine_account_password(p->mem_ctx,
1263 creds->account_name,
1264 r->in.new_password);
1268 /****************************************************************
1269 _netr_ServerPasswordSet2
1270 ****************************************************************/
1272 NTSTATUS _netr_ServerPasswordSet2(struct pipes_struct *p,
1273 struct netr_ServerPasswordSet2 *r)
1276 struct netlogon_creds_CredentialState *creds = NULL;
1277 DATA_BLOB plaintext;
1278 struct samr_CryptPassword password_buf;
1279 struct samr_Password nt_hash;
1282 status = netr_creds_server_step_check(p, p->mem_ctx,
1283 r->in.computer_name,
1285 r->out.return_authenticator,
1289 if (!NT_STATUS_IS_OK(status)) {
1290 const char *computer_name = "<unknown>";
1292 if (creds && creds->computer_name) {
1293 computer_name = creds->computer_name;
1295 DEBUG(2,("_netr_ServerPasswordSet2: netlogon_creds_server_step "
1296 "failed. Rejecting auth request from client %s machine account %s\n",
1297 r->in.computer_name, computer_name));
1302 memcpy(password_buf.data, r->in.new_password->data, 512);
1303 SIVAL(password_buf.data, 512, r->in.new_password->length);
1304 netlogon_creds_arcfour_crypt(creds, password_buf.data, 516);
1306 if (!extract_pw_from_buffer(p->mem_ctx, password_buf.data, &plaintext)) {
1308 return NT_STATUS_WRONG_PASSWORD;
1311 mdfour(nt_hash.hash, plaintext.data, plaintext.length);
1313 status = netr_set_machine_account_password(p->mem_ctx,
1316 creds->account_name,
1322 /*************************************************************************
1323 _netr_LogonSamLogoff
1324 *************************************************************************/
1326 NTSTATUS _netr_LogonSamLogoff(struct pipes_struct *p,
1327 struct netr_LogonSamLogoff *r)
1330 struct netlogon_creds_CredentialState *creds;
1333 status = netr_creds_server_step_check(p, p->mem_ctx,
1334 r->in.computer_name,
1336 r->out.return_authenticator,
1343 static NTSTATUS _netr_LogonSamLogon_check(const struct netr_LogonSamLogonEx *r)
1345 switch (r->in.logon_level) {
1346 case NetlogonInteractiveInformation:
1347 case NetlogonServiceInformation:
1348 case NetlogonInteractiveTransitiveInformation:
1349 case NetlogonServiceTransitiveInformation:
1350 if (r->in.logon->password == NULL) {
1351 return NT_STATUS_INVALID_PARAMETER;
1354 switch (r->in.validation_level) {
1355 case NetlogonValidationSamInfo: /* 2 */
1356 case NetlogonValidationSamInfo2: /* 3 */
1358 case NetlogonValidationSamInfo4: /* 6 */
1359 if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
1360 DEBUG(10,("Not adding validation info level 6 "
1361 "without ADS passdb backend\n"));
1362 return NT_STATUS_INVALID_INFO_CLASS;
1366 return NT_STATUS_INVALID_INFO_CLASS;
1370 case NetlogonNetworkInformation:
1371 case NetlogonNetworkTransitiveInformation:
1372 if (r->in.logon->network == NULL) {
1373 return NT_STATUS_INVALID_PARAMETER;
1376 switch (r->in.validation_level) {
1377 case NetlogonValidationSamInfo: /* 2 */
1378 case NetlogonValidationSamInfo2: /* 3 */
1380 case NetlogonValidationSamInfo4: /* 6 */
1381 if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
1382 DEBUG(10,("Not adding validation info level 6 "
1383 "without ADS passdb backend\n"));
1384 return NT_STATUS_INVALID_INFO_CLASS;
1388 return NT_STATUS_INVALID_INFO_CLASS;
1393 case NetlogonGenericInformation:
1394 if (r->in.logon->generic == NULL) {
1395 return NT_STATUS_INVALID_PARAMETER;
1398 /* we don't support this here */
1399 return NT_STATUS_INVALID_PARAMETER;
1401 switch (r->in.validation_level) {
1402 /* TODO: case NetlogonValidationGenericInfo: 4 */
1403 case NetlogonValidationGenericInfo2: /* 5 */
1406 return NT_STATUS_INVALID_INFO_CLASS;
1412 return NT_STATUS_INVALID_PARAMETER;
1415 return NT_STATUS_OK;
1418 /*************************************************************************
1419 _netr_LogonSamLogon_base
1420 *************************************************************************/
1422 static NTSTATUS _netr_LogonSamLogon_base(struct pipes_struct *p,
1423 struct netr_LogonSamLogonEx *r,
1424 struct netlogon_creds_CredentialState *creds)
1426 NTSTATUS status = NT_STATUS_OK;
1427 union netr_LogonLevel *logon = r->in.logon;
1428 const char *nt_username, *nt_domain, *nt_workstation;
1429 struct auth_usersupplied_info *user_info = NULL;
1430 struct auth_serversupplied_info *server_info = NULL;
1431 struct auth_context *auth_context = NULL;
1432 uint8_t pipe_session_key[16];
1433 bool process_creds = true;
1437 case NDR_NETR_LOGONSAMLOGON:
1438 process_creds = true;
1439 fn = "_netr_LogonSamLogon";
1441 case NDR_NETR_LOGONSAMLOGONWITHFLAGS:
1442 process_creds = true;
1443 fn = "_netr_LogonSamLogonWithFlags";
1445 case NDR_NETR_LOGONSAMLOGONEX:
1446 process_creds = false;
1447 fn = "_netr_LogonSamLogonEx";
1450 return NT_STATUS_INTERNAL_ERROR;
1453 *r->out.authoritative = true; /* authoritative response */
1455 switch (r->in.validation_level) {
1457 r->out.validation->sam2 = talloc_zero(p->mem_ctx, struct netr_SamInfo2);
1458 if (!r->out.validation->sam2) {
1459 return NT_STATUS_NO_MEMORY;
1463 r->out.validation->sam3 = talloc_zero(p->mem_ctx, struct netr_SamInfo3);
1464 if (!r->out.validation->sam3) {
1465 return NT_STATUS_NO_MEMORY;
1469 r->out.validation->sam6 = talloc_zero(p->mem_ctx, struct netr_SamInfo6);
1470 if (!r->out.validation->sam6) {
1471 return NT_STATUS_NO_MEMORY;
1475 DEBUG(0,("%s: bad validation_level value %d.\n",
1476 fn, (int)r->in.validation_level));
1477 return NT_STATUS_INVALID_INFO_CLASS;
1480 switch (r->in.logon_level) {
1481 case NetlogonInteractiveInformation:
1482 case NetlogonServiceInformation:
1483 case NetlogonInteractiveTransitiveInformation:
1484 case NetlogonServiceTransitiveInformation:
1485 nt_username = logon->password->identity_info.account_name.string ?
1486 logon->password->identity_info.account_name.string : "";
1487 nt_domain = logon->password->identity_info.domain_name.string ?
1488 logon->password->identity_info.domain_name.string : "";
1489 nt_workstation = logon->password->identity_info.workstation.string ?
1490 logon->password->identity_info.workstation.string : "";
1492 DEBUG(3,("SAM Logon (Interactive). Domain:[%s]. ", lp_workgroup()));
1494 case NetlogonNetworkInformation:
1495 case NetlogonNetworkTransitiveInformation:
1496 nt_username = logon->network->identity_info.account_name.string ?
1497 logon->network->identity_info.account_name.string : "";
1498 nt_domain = logon->network->identity_info.domain_name.string ?
1499 logon->network->identity_info.domain_name.string : "";
1500 nt_workstation = logon->network->identity_info.workstation.string ?
1501 logon->network->identity_info.workstation.string : "";
1503 DEBUG(3,("SAM Logon (Network). Domain:[%s]. ", lp_workgroup()));
1506 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1507 return NT_STATUS_INVALID_INFO_CLASS;
1510 DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, nt_workstation, nt_domain));
1511 fstrcpy(current_user_info.smb_name, nt_username);
1512 sub_set_smb_name(nt_username);
1514 DEBUG(5,("Attempting validation level %d for unmapped username %s.\n",
1515 r->in.validation_level, nt_username));
1517 status = NT_STATUS_OK;
1519 switch (r->in.logon_level) {
1520 case NetlogonNetworkInformation:
1521 case NetlogonNetworkTransitiveInformation:
1523 const char *wksname = nt_workstation;
1525 status = make_auth_context_fixed(talloc_tos(), &auth_context,
1526 logon->network->challenge);
1527 if (!NT_STATUS_IS_OK(status)) {
1531 /* For a network logon, the workstation name comes in with two
1532 * backslashes in the front. Strip them if they are there. */
1534 if (*wksname == '\\') wksname++;
1535 if (*wksname == '\\') wksname++;
1537 /* Standard challenge/response authentication */
1538 if (!make_user_info_netlogon_network(&user_info,
1539 nt_username, nt_domain,
1542 logon->network->identity_info.parameter_control,
1543 logon->network->lm.data,
1544 logon->network->lm.length,
1545 logon->network->nt.data,
1546 logon->network->nt.length)) {
1547 status = NT_STATUS_NO_MEMORY;
1551 case NetlogonInteractiveInformation:
1552 case NetlogonServiceInformation:
1553 case NetlogonInteractiveTransitiveInformation:
1554 case NetlogonServiceTransitiveInformation:
1556 /* 'Interactive' authentication, supplies the password in its
1557 MD4 form, encrypted with the session key. We will convert
1558 this to challenge/response for the auth subsystem to chew
1563 status = make_auth_context_subsystem(talloc_tos(),
1565 if (!NT_STATUS_IS_OK(status)) {
1569 auth_context->get_ntlm_challenge(auth_context, chal);
1571 if (!make_user_info_netlogon_interactive(&user_info,
1572 nt_username, nt_domain,
1575 logon->password->identity_info.parameter_control,
1577 logon->password->lmpassword.hash,
1578 logon->password->ntpassword.hash,
1579 creds->session_key)) {
1580 status = NT_STATUS_NO_MEMORY;
1585 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1586 return NT_STATUS_INVALID_INFO_CLASS;
1589 if ( NT_STATUS_IS_OK(status) ) {
1590 status = auth_context->check_ntlm_password(auth_context,
1591 user_info, &server_info);
1594 TALLOC_FREE(auth_context);
1595 free_user_info(&user_info);
1597 DEBUG(5,("%s: check_password returned status %s\n",
1598 fn, nt_errstr(status)));
1600 /* Check account and password */
1602 if (!NT_STATUS_IS_OK(status)) {
1603 /* If we don't know what this domain is, we need to
1604 indicate that we are not authoritative. This
1605 allows the client to decide if it needs to try
1606 a local user. Fix by jpjanosi@us.ibm.com, #2976 */
1607 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)
1608 && !strequal(nt_domain, get_global_sam_name())
1609 && !is_trusted_domain(nt_domain) )
1610 *r->out.authoritative = false; /* We are not authoritative */
1612 TALLOC_FREE(server_info);
1616 if (server_info->guest) {
1617 /* We don't like guest domain logons... */
1618 DEBUG(5,("%s: Attempted domain logon as GUEST "
1620 TALLOC_FREE(server_info);
1621 return NT_STATUS_LOGON_FAILURE;
1624 /* This is the point at which, if the login was successful, that
1625 the SAM Local Security Authority should record that the user is
1626 logged in to the domain. */
1628 if (process_creds) {
1629 /* Get the pipe session key from the creds. */
1630 memcpy(pipe_session_key, creds->session_key, 16);
1632 struct schannel_state *schannel_auth;
1633 /* Get the pipe session key from the schannel. */
1634 if ((p->auth.auth_type != DCERPC_AUTH_TYPE_SCHANNEL)
1635 || (p->auth.auth_ctx == NULL)) {
1636 return NT_STATUS_INVALID_HANDLE;
1639 schannel_auth = talloc_get_type_abort(p->auth.auth_ctx,
1640 struct schannel_state);
1641 memcpy(pipe_session_key, schannel_auth->creds->session_key, 16);
1644 switch (r->in.validation_level) {
1646 status = serverinfo_to_SamInfo2(server_info, pipe_session_key, 16,
1647 r->out.validation->sam2);
1650 status = serverinfo_to_SamInfo3(server_info, pipe_session_key, 16,
1651 r->out.validation->sam3);
1654 status = serverinfo_to_SamInfo6(server_info, pipe_session_key, 16,
1655 r->out.validation->sam6);
1659 TALLOC_FREE(server_info);
1664 /****************************************************************
1665 _netr_LogonSamLogonWithFlags
1666 ****************************************************************/
1668 NTSTATUS _netr_LogonSamLogonWithFlags(struct pipes_struct *p,
1669 struct netr_LogonSamLogonWithFlags *r)
1672 struct netlogon_creds_CredentialState *creds;
1673 struct netr_LogonSamLogonEx r2;
1674 struct netr_Authenticator return_authenticator;
1676 *r->out.authoritative = true;
1678 r2.in.server_name = r->in.server_name;
1679 r2.in.computer_name = r->in.computer_name;
1680 r2.in.logon_level = r->in.logon_level;
1681 r2.in.logon = r->in.logon;
1682 r2.in.validation_level = r->in.validation_level;
1683 r2.in.flags = r->in.flags;
1684 r2.out.validation = r->out.validation;
1685 r2.out.authoritative = r->out.authoritative;
1686 r2.out.flags = r->out.flags;
1688 status = _netr_LogonSamLogon_check(&r2);
1689 if (!NT_STATUS_IS_OK(status)) {
1694 status = netr_creds_server_step_check(p, p->mem_ctx,
1695 r->in.computer_name,
1697 &return_authenticator,
1700 if (!NT_STATUS_IS_OK(status)) {
1704 status = _netr_LogonSamLogon_base(p, &r2, creds);
1706 *r->out.return_authenticator = return_authenticator;
1711 /*************************************************************************
1713 *************************************************************************/
1715 NTSTATUS _netr_LogonSamLogon(struct pipes_struct *p,
1716 struct netr_LogonSamLogon *r)
1719 struct netr_LogonSamLogonWithFlags r2;
1722 r2.in.server_name = r->in.server_name;
1723 r2.in.computer_name = r->in.computer_name;
1724 r2.in.credential = r->in.credential;
1725 r2.in.logon_level = r->in.logon_level;
1726 r2.in.logon = r->in.logon;
1727 r2.in.validation_level = r->in.validation_level;
1728 r2.in.return_authenticator = r->in.return_authenticator;
1729 r2.in.flags = &flags;
1730 r2.out.validation = r->out.validation;
1731 r2.out.authoritative = r->out.authoritative;
1732 r2.out.flags = &flags;
1733 r2.out.return_authenticator = r->out.return_authenticator;
1735 status = _netr_LogonSamLogonWithFlags(p, &r2);
1740 /*************************************************************************
1741 _netr_LogonSamLogonEx
1742 - no credential chaining. Map into net sam logon.
1743 *************************************************************************/
1745 NTSTATUS _netr_LogonSamLogonEx(struct pipes_struct *p,
1746 struct netr_LogonSamLogonEx *r)
1749 struct netlogon_creds_CredentialState *creds = NULL;
1750 struct loadparm_context *lp_ctx;
1752 *r->out.authoritative = true;
1754 status = _netr_LogonSamLogon_check(r);
1755 if (!NT_STATUS_IS_OK(status)) {
1759 /* Only allow this if the pipe is protected. */
1760 if (p->auth.auth_type != DCERPC_AUTH_TYPE_SCHANNEL) {
1761 DEBUG(0,("_netr_LogonSamLogonEx: client %s not using schannel for netlogon\n",
1762 get_remote_machine_name() ));
1763 return NT_STATUS_INVALID_PARAMETER;
1766 lp_ctx = loadparm_init_s3(p->mem_ctx, loadparm_s3_context());
1767 if (lp_ctx == NULL) {
1768 DEBUG(0, ("loadparm_init_s3 failed\n"));
1769 return NT_STATUS_INTERNAL_ERROR;
1773 status = schannel_get_creds_state(p->mem_ctx, lp_ctx,
1774 r->in.computer_name, &creds);
1776 talloc_unlink(p->mem_ctx, lp_ctx);
1778 if (!NT_STATUS_IS_OK(status)) {
1782 status = _netr_LogonSamLogon_base(p, r, creds);
1788 /*************************************************************************
1790 *************************************************************************/
1791 #if 0 /* JERRY -- not correct */
1792 NTSTATUS _ds_enum_dom_trusts(struct pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1793 DS_R_ENUM_DOM_TRUSTS *r_u)
1795 NTSTATUS status = NT_STATUS_OK;
1797 /* TODO: According to MSDN, the can only be executed against a
1798 DC or domain member running Windows 2000 or later. Need
1799 to test against a standalone 2k server and see what it
1800 does. A windows 2000 DC includes its own domain in the
1808 /****************************************************************
1809 ****************************************************************/
1811 WERROR _netr_LogonUasLogon(struct pipes_struct *p,
1812 struct netr_LogonUasLogon *r)
1814 p->rng_fault_state = true;
1815 return WERR_NOT_SUPPORTED;
1818 /****************************************************************
1819 ****************************************************************/
1821 WERROR _netr_LogonUasLogoff(struct pipes_struct *p,
1822 struct netr_LogonUasLogoff *r)
1824 p->rng_fault_state = true;
1825 return WERR_NOT_SUPPORTED;
1828 /****************************************************************
1829 ****************************************************************/
1831 NTSTATUS _netr_DatabaseDeltas(struct pipes_struct *p,
1832 struct netr_DatabaseDeltas *r)
1834 p->rng_fault_state = true;
1835 return NT_STATUS_NOT_IMPLEMENTED;
1838 /****************************************************************
1839 ****************************************************************/
1841 NTSTATUS _netr_DatabaseSync(struct pipes_struct *p,
1842 struct netr_DatabaseSync *r)
1844 p->rng_fault_state = true;
1845 return NT_STATUS_NOT_IMPLEMENTED;
1848 /****************************************************************
1849 ****************************************************************/
1851 NTSTATUS _netr_AccountDeltas(struct pipes_struct *p,
1852 struct netr_AccountDeltas *r)
1854 p->rng_fault_state = true;
1855 return NT_STATUS_NOT_IMPLEMENTED;
1858 /****************************************************************
1859 ****************************************************************/
1861 NTSTATUS _netr_AccountSync(struct pipes_struct *p,
1862 struct netr_AccountSync *r)
1864 p->rng_fault_state = true;
1865 return NT_STATUS_NOT_IMPLEMENTED;
1868 /****************************************************************
1869 ****************************************************************/
1871 static bool wb_getdcname(TALLOC_CTX *mem_ctx,
1873 const char **dcname,
1878 struct wbcDomainControllerInfo *dc_info = NULL;
1880 result = wbcLookupDomainController(domain,
1884 case WBC_ERR_SUCCESS:
1886 case WBC_ERR_WINBIND_NOT_AVAILABLE:
1888 case WBC_ERR_DOMAIN_NOT_FOUND:
1889 *werr = WERR_NO_SUCH_DOMAIN;
1892 *werr = WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1896 *dcname = talloc_strdup(mem_ctx, dc_info->dc_name);
1897 wbcFreeMemory(dc_info);
1908 /****************************************************************
1910 ****************************************************************/
1912 WERROR _netr_GetDcName(struct pipes_struct *p,
1913 struct netr_GetDcName *r)
1918 struct netr_DsRGetDCNameInfo *info;
1921 ret = wb_getdcname(p->mem_ctx,
1924 WBC_LOOKUP_DC_IS_FLAT_NAME |
1925 WBC_LOOKUP_DC_RETURN_FLAT_NAME |
1926 WBC_LOOKUP_DC_PDC_REQUIRED,
1932 flags = DS_PDC_REQUIRED | DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
1934 status = dsgetdcname(p->mem_ctx,
1941 if (!NT_STATUS_IS_OK(status)) {
1942 return ntstatus_to_werror(status);
1945 *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
1947 if (!*r->out.dcname) {
1954 /****************************************************************
1956 ****************************************************************/
1958 WERROR _netr_GetAnyDCName(struct pipes_struct *p,
1959 struct netr_GetAnyDCName *r)
1964 struct netr_DsRGetDCNameInfo *info;
1967 ret = wb_getdcname(p->mem_ctx,
1970 WBC_LOOKUP_DC_IS_FLAT_NAME |
1971 WBC_LOOKUP_DC_RETURN_FLAT_NAME,
1977 flags = DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
1979 status = dsgetdcname(p->mem_ctx,
1986 if (!NT_STATUS_IS_OK(status)) {
1987 return ntstatus_to_werror(status);
1990 *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
1992 if (!*r->out.dcname) {
1999 /****************************************************************
2000 ****************************************************************/
2002 NTSTATUS _netr_DatabaseSync2(struct pipes_struct *p,
2003 struct netr_DatabaseSync2 *r)
2005 p->rng_fault_state = true;
2006 return NT_STATUS_NOT_IMPLEMENTED;
2009 /****************************************************************
2010 ****************************************************************/
2012 NTSTATUS _netr_DatabaseRedo(struct pipes_struct *p,
2013 struct netr_DatabaseRedo *r)
2015 p->rng_fault_state = true;
2016 return NT_STATUS_NOT_IMPLEMENTED;
2019 /****************************************************************
2020 ****************************************************************/
2022 WERROR _netr_DsRGetDCName(struct pipes_struct *p,
2023 struct netr_DsRGetDCName *r)
2025 p->rng_fault_state = true;
2026 return WERR_NOT_SUPPORTED;
2029 /****************************************************************
2030 ****************************************************************/
2032 NTSTATUS _netr_LogonGetCapabilities(struct pipes_struct *p,
2033 struct netr_LogonGetCapabilities *r)
2035 struct netlogon_creds_CredentialState *creds;
2039 status = netr_creds_server_step_check(p, p->mem_ctx,
2040 r->in.computer_name,
2042 r->out.return_authenticator,
2045 if (!NT_STATUS_IS_OK(status)) {
2049 if (r->in.query_level != 1) {
2050 return NT_STATUS_NOT_SUPPORTED;
2053 r->out.capabilities->server_capabilities = creds->negotiate_flags;
2055 return NT_STATUS_OK;
2058 /****************************************************************
2059 ****************************************************************/
2061 WERROR _netr_NETRLOGONSETSERVICEBITS(struct pipes_struct *p,
2062 struct netr_NETRLOGONSETSERVICEBITS *r)
2064 p->rng_fault_state = true;
2065 return WERR_NOT_SUPPORTED;
2068 /****************************************************************
2069 ****************************************************************/
2071 WERROR _netr_LogonGetTrustRid(struct pipes_struct *p,
2072 struct netr_LogonGetTrustRid *r)
2074 p->rng_fault_state = true;
2075 return WERR_NOT_SUPPORTED;
2078 /****************************************************************
2079 ****************************************************************/
2081 WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(struct pipes_struct *p,
2082 struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
2084 p->rng_fault_state = true;
2085 return WERR_NOT_SUPPORTED;
2088 /****************************************************************
2089 ****************************************************************/
2091 WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(struct pipes_struct *p,
2092 struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
2094 p->rng_fault_state = true;
2095 return WERR_NOT_SUPPORTED;
2098 /****************************************************************
2099 ****************************************************************/
2101 WERROR _netr_DsRGetDCNameEx(struct pipes_struct *p,
2102 struct netr_DsRGetDCNameEx *r)
2104 p->rng_fault_state = true;
2105 return WERR_NOT_SUPPORTED;
2108 /****************************************************************
2109 ****************************************************************/
2111 WERROR _netr_DsRGetSiteName(struct pipes_struct *p,
2112 struct netr_DsRGetSiteName *r)
2114 p->rng_fault_state = true;
2115 return WERR_NOT_SUPPORTED;
2118 /****************************************************************
2119 ****************************************************************/
2121 NTSTATUS _netr_LogonGetDomainInfo(struct pipes_struct *p,
2122 struct netr_LogonGetDomainInfo *r)
2124 p->rng_fault_state = true;
2125 return NT_STATUS_NOT_IMPLEMENTED;
2128 /****************************************************************
2129 ****************************************************************/
2131 WERROR _netr_ServerPasswordGet(struct pipes_struct *p,
2132 struct netr_ServerPasswordGet *r)
2134 p->rng_fault_state = true;
2135 return WERR_NOT_SUPPORTED;
2138 /****************************************************************
2139 ****************************************************************/
2141 WERROR _netr_NETRLOGONSENDTOSAM(struct pipes_struct *p,
2142 struct netr_NETRLOGONSENDTOSAM *r)
2144 p->rng_fault_state = true;
2145 return WERR_NOT_SUPPORTED;
2148 /****************************************************************
2149 ****************************************************************/
2151 WERROR _netr_DsRAddressToSitenamesW(struct pipes_struct *p,
2152 struct netr_DsRAddressToSitenamesW *r)
2154 p->rng_fault_state = true;
2155 return WERR_NOT_SUPPORTED;
2158 /****************************************************************
2159 ****************************************************************/
2161 WERROR _netr_DsRGetDCNameEx2(struct pipes_struct *p,
2162 struct netr_DsRGetDCNameEx2 *r)
2164 p->rng_fault_state = true;
2165 return WERR_NOT_SUPPORTED;
2168 /****************************************************************
2169 ****************************************************************/
2171 WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(struct pipes_struct *p,
2172 struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
2174 p->rng_fault_state = true;
2175 return WERR_NOT_SUPPORTED;
2178 /****************************************************************
2179 ****************************************************************/
2181 WERROR _netr_NetrEnumerateTrustedDomainsEx(struct pipes_struct *p,
2182 struct netr_NetrEnumerateTrustedDomainsEx *r)
2184 p->rng_fault_state = true;
2185 return WERR_NOT_SUPPORTED;
2188 /****************************************************************
2189 ****************************************************************/
2191 WERROR _netr_DsRAddressToSitenamesExW(struct pipes_struct *p,
2192 struct netr_DsRAddressToSitenamesExW *r)
2194 p->rng_fault_state = true;
2195 return WERR_NOT_SUPPORTED;
2198 /****************************************************************
2199 ****************************************************************/
2201 WERROR _netr_DsrGetDcSiteCoverageW(struct pipes_struct *p,
2202 struct netr_DsrGetDcSiteCoverageW *r)
2204 p->rng_fault_state = true;
2205 return WERR_NOT_SUPPORTED;
2208 /****************************************************************
2209 ****************************************************************/
2211 WERROR _netr_DsrEnumerateDomainTrusts(struct pipes_struct *p,
2212 struct netr_DsrEnumerateDomainTrusts *r)
2214 p->rng_fault_state = true;
2215 return WERR_NOT_SUPPORTED;
2218 /****************************************************************
2219 ****************************************************************/
2221 WERROR _netr_DsrDeregisterDNSHostRecords(struct pipes_struct *p,
2222 struct netr_DsrDeregisterDNSHostRecords *r)
2224 p->rng_fault_state = true;
2225 return WERR_NOT_SUPPORTED;
2228 /****************************************************************
2229 ****************************************************************/
2231 NTSTATUS _netr_ServerTrustPasswordsGet(struct pipes_struct *p,
2232 struct netr_ServerTrustPasswordsGet *r)
2234 p->rng_fault_state = true;
2235 return NT_STATUS_NOT_IMPLEMENTED;
2238 /****************************************************************
2239 ****************************************************************/
2241 WERROR _netr_DsRGetForestTrustInformation(struct pipes_struct *p,
2242 struct netr_DsRGetForestTrustInformation *r)
2244 p->rng_fault_state = true;
2245 return WERR_NOT_SUPPORTED;
2248 /****************************************************************
2249 ****************************************************************/
2251 static NTSTATUS fill_forest_trust_array(TALLOC_CTX *mem_ctx,
2252 struct lsa_ForestTrustInformation *info)
2254 struct lsa_ForestTrustRecord *e;
2255 struct pdb_domain_info *dom_info;
2256 struct lsa_ForestTrustDomainInfo *domain_info;
2258 dom_info = pdb_get_domain_info(mem_ctx);
2259 if (dom_info == NULL) {
2260 return NT_STATUS_NO_MEMORY;
2264 info->entries = talloc_array(info, struct lsa_ForestTrustRecord *, 2);
2265 if (info->entries == NULL) {
2266 return NT_STATUS_NO_MEMORY;
2269 e = talloc(info, struct lsa_ForestTrustRecord);
2271 return NT_STATUS_NO_MEMORY;
2275 e->type = LSA_FOREST_TRUST_TOP_LEVEL_NAME;
2276 e->time = 0; /* so far always 0 in trces. */
2277 e->forest_trust_data.top_level_name.string = talloc_steal(info,
2278 dom_info->dns_forest);
2280 info->entries[0] = e;
2282 e = talloc(info, struct lsa_ForestTrustRecord);
2284 return NT_STATUS_NO_MEMORY;
2287 /* TODO: check if disabled and set flags accordingly */
2289 e->type = LSA_FOREST_TRUST_DOMAIN_INFO;
2290 e->time = 0; /* so far always 0 in traces. */
2292 domain_info = &e->forest_trust_data.domain_info;
2293 domain_info->domain_sid = dom_sid_dup(info, &dom_info->sid);
2295 domain_info->dns_domain_name.string = talloc_steal(info,
2296 dom_info->dns_domain);
2297 domain_info->netbios_domain_name.string = talloc_steal(info,
2300 info->entries[1] = e;
2302 return NT_STATUS_OK;
2305 /****************************************************************
2306 _netr_GetForestTrustInformation
2307 ****************************************************************/
2309 NTSTATUS _netr_GetForestTrustInformation(struct pipes_struct *p,
2310 struct netr_GetForestTrustInformation *r)
2313 struct netlogon_creds_CredentialState *creds;
2314 struct lsa_ForestTrustInformation *info, **info_ptr;
2315 struct loadparm_context *lp_ctx;
2317 /* TODO: check server name */
2319 lp_ctx = loadparm_init_s3(p->mem_ctx, loadparm_s3_context());
2320 if (lp_ctx == NULL) {
2321 DEBUG(0, ("loadparm_init_s3 failed\n"));
2322 return NT_STATUS_INTERNAL_ERROR;
2325 status = schannel_check_creds_state(p->mem_ctx, lp_ctx,
2326 r->in.computer_name,
2328 r->out.return_authenticator,
2330 talloc_unlink(p->mem_ctx, lp_ctx);
2331 if (!NT_STATUS_IS_OK(status)) {
2335 if ((creds->secure_channel_type != SEC_CHAN_DNS_DOMAIN) &&
2336 (creds->secure_channel_type != SEC_CHAN_DOMAIN)) {
2337 return NT_STATUS_NOT_IMPLEMENTED;
2340 info_ptr = talloc(p->mem_ctx, struct lsa_ForestTrustInformation *);
2342 return NT_STATUS_NO_MEMORY;
2344 info = talloc_zero(info_ptr, struct lsa_ForestTrustInformation);
2346 return NT_STATUS_NO_MEMORY;
2349 status = fill_forest_trust_array(p->mem_ctx, info);
2350 if (!NT_STATUS_IS_OK(status)) {
2355 r->out.forest_trust_info = info_ptr;
2357 return NT_STATUS_OK;
2360 /****************************************************************
2361 ****************************************************************/
2363 static NTSTATUS get_password_from_trustAuth(TALLOC_CTX *mem_ctx,
2364 const DATA_BLOB *trustAuth_blob,
2365 const DATA_BLOB *session_key,
2366 struct samr_Password *current_pw_enc,
2367 struct samr_Password *previous_pw_enc)
2369 enum ndr_err_code ndr_err;
2370 struct trustAuthInOutBlob trustAuth;
2372 ndr_err = ndr_pull_struct_blob_all(trustAuth_blob, mem_ctx, &trustAuth,
2373 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
2374 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2375 return NT_STATUS_UNSUCCESSFUL;
2379 if (trustAuth.count != 0 && trustAuth.current.count != 0 &&
2380 trustAuth.current.array[0].AuthType == TRUST_AUTH_TYPE_CLEAR) {
2381 mdfour(previous_pw_enc->hash,
2382 trustAuth.current.array[0].AuthInfo.clear.password,
2383 trustAuth.current.array[0].AuthInfo.clear.size);
2385 return NT_STATUS_UNSUCCESSFUL;
2388 arcfour_crypt_blob(current_pw_enc->hash, sizeof(current_pw_enc->hash),
2391 if (trustAuth.previous.count != 0 &&
2392 trustAuth.previous.array[0].AuthType == TRUST_AUTH_TYPE_CLEAR) {
2393 mdfour(previous_pw_enc->hash,
2394 trustAuth.previous.array[0].AuthInfo.clear.password,
2395 trustAuth.previous.array[0].AuthInfo.clear.size);
2397 mdfour(previous_pw_enc->hash, NULL, 0);
2399 arcfour_crypt_blob(previous_pw_enc->hash, sizeof(previous_pw_enc->hash),
2402 return NT_STATUS_OK;
2405 /****************************************************************
2406 _netr_ServerGetTrustInfo
2407 ****************************************************************/
2409 NTSTATUS _netr_ServerGetTrustInfo(struct pipes_struct *p,
2410 struct netr_ServerGetTrustInfo *r)
2413 struct netlogon_creds_CredentialState *creds;
2415 size_t account_name_last;
2417 struct netr_TrustInfo *trust_info;
2418 struct pdb_trusted_domain *td;
2419 DATA_BLOB trustAuth_blob;
2420 struct samr_Password *new_owf_enc;
2421 struct samr_Password *old_owf_enc;
2422 DATA_BLOB session_key;
2423 struct loadparm_context *lp_ctx;
2425 lp_ctx = loadparm_init_s3(p->mem_ctx, loadparm_s3_context());
2426 if (lp_ctx == NULL) {
2427 DEBUG(0, ("loadparm_init_s3 failed\n"));
2428 return NT_STATUS_INTERNAL_ERROR;
2431 /* TODO: check server name */
2433 status = schannel_check_creds_state(p->mem_ctx, lp_ctx,
2434 r->in.computer_name,
2436 r->out.return_authenticator,
2438 talloc_unlink(p->mem_ctx, lp_ctx);
2439 if (!NT_STATUS_IS_OK(status)) {
2443 account_name = talloc_strdup(p->mem_ctx, r->in.account_name);
2444 if (account_name == NULL) {
2445 return NT_STATUS_NO_MEMORY;
2448 account_name_last = strlen(account_name);
2449 if (account_name_last == 0) {
2450 return NT_STATUS_INVALID_PARAMETER;
2452 account_name_last--;
2453 if (account_name[account_name_last] == '.') {
2454 account_name[account_name_last] = '\0';
2457 if ((creds->secure_channel_type != SEC_CHAN_DNS_DOMAIN) &&
2458 (creds->secure_channel_type != SEC_CHAN_DOMAIN)) {
2466 account_name_last = strlen(account_name);
2467 if (account_name_last == 0) {
2468 return NT_STATUS_INVALID_PARAMETER;
2470 account_name_last--;
2471 if (account_name[account_name_last] == '$') {
2472 account_name[account_name_last] = '\0';
2475 status = pdb_get_trusted_domain(p->mem_ctx, account_name, &td);
2476 if (!NT_STATUS_IS_OK(status)) {
2480 if (r->out.trust_info != NULL) {
2481 trust_info = talloc_zero(p->mem_ctx, struct netr_TrustInfo);
2482 if (trust_info == NULL) {
2483 return NT_STATUS_NO_MEMORY;
2485 trust_info->count = 1;
2487 trust_info->data = talloc_array(trust_info, uint32_t, 1);
2488 if (trust_info->data == NULL) {
2489 return NT_STATUS_NO_MEMORY;
2491 trust_info->data[0] = td->trust_attributes;
2493 *r->out.trust_info = trust_info;
2496 new_owf_enc = talloc_zero(p->mem_ctx, struct samr_Password);
2497 old_owf_enc = talloc_zero(p->mem_ctx, struct samr_Password);
2498 if (new_owf_enc == NULL || old_owf_enc == NULL) {
2499 return NT_STATUS_NO_MEMORY;
2502 /* TODO: which trustAuth shall we use if we have in/out trust or do they have to
2504 if (td->trust_direction & NETR_TRUST_FLAG_INBOUND) {
2505 trustAuth_blob = td->trust_auth_incoming;
2506 } else if (td->trust_direction & NETR_TRUST_FLAG_OUTBOUND) {
2507 trustAuth_blob = td->trust_auth_outgoing;
2510 session_key.data = creds->session_key;
2511 session_key.length = sizeof(creds->session_key);
2512 status = get_password_from_trustAuth(p->mem_ctx, &trustAuth_blob,
2514 new_owf_enc, old_owf_enc);
2516 if (!NT_STATUS_IS_OK(status)) {
2520 r->out.new_owf_password = new_owf_enc;
2521 r->out.old_owf_password = old_owf_enc;
2523 /* TODO: look for machine password */
2524 r->out.new_owf_password = NULL;
2525 r->out.old_owf_password = NULL;
2527 return NT_STATUS_NOT_IMPLEMENTED;
2530 return NT_STATUS_OK;
2533 /****************************************************************
2534 ****************************************************************/
2536 NTSTATUS _netr_Unused47(struct pipes_struct *p,
2537 struct netr_Unused47 *r)
2539 p->rng_fault_state = true;
2540 return NT_STATUS_NOT_IMPLEMENTED;
2543 /****************************************************************
2544 ****************************************************************/
2546 NTSTATUS _netr_DsrUpdateReadOnlyServerDnsRecords(struct pipes_struct *p,
2547 struct netr_DsrUpdateReadOnlyServerDnsRecords *r)
2549 p->rng_fault_state = true;
2550 return NT_STATUS_NOT_IMPLEMENTED;