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. */
29 #include "../libcli/auth/schannel.h"
30 #include "../librpc/gen_ndr/srv_netlogon.h"
31 #include "../librpc/gen_ndr/ndr_samr_c.h"
32 #include "../librpc/gen_ndr/ndr_lsa_c.h"
33 #include "rpc_client/cli_lsarpc.h"
34 #include "rpc_client/init_lsa.h"
35 #include "rpc_server/rpc_ncacn_np.h"
36 #include "../libcli/security/security.h"
37 #include "../libcli/security/dom_sid.h"
38 #include "librpc/gen_ndr/ndr_drsblobs.h"
39 #include "lib/crypto/arcfour.h"
40 #include "lib/crypto/md4.h"
41 #include "nsswitch/libwbclient/wbclient.h"
42 #include "../libcli/registry/util_reg.h"
46 #include "../lib/tsocket/tsocket.h"
47 #include "lib/param/param.h"
49 extern userdom_struct current_user_info;
52 #define DBGC_CLASS DBGC_RPC_SRV
54 struct netlogon_server_pipe_state {
55 struct netr_Credential client_challenge;
56 struct netr_Credential server_challenge;
59 /*************************************************************************
61 *************************************************************************/
63 WERROR _netr_LogonControl(struct pipes_struct *p,
64 struct netr_LogonControl *r)
66 struct netr_LogonControl2Ex l;
68 switch (r->in.level) {
72 return WERR_NOT_SUPPORTED;
74 return WERR_UNKNOWN_LEVEL;
77 l.in.logon_server = r->in.logon_server;
78 l.in.function_code = r->in.function_code;
79 l.in.level = r->in.level;
81 l.out.query = r->out.query;
83 return _netr_LogonControl2Ex(p, &l);
86 /****************************************************************************
87 Send a message to smbd to do a sam synchronisation
88 **************************************************************************/
90 static void send_sync_message(struct messaging_context *msg_ctx)
92 DEBUG(3, ("sending sam synchronisation message\n"));
93 message_send_all(msg_ctx, MSG_SMB_SAM_SYNC, NULL, 0, NULL);
96 /*************************************************************************
98 *************************************************************************/
100 WERROR _netr_LogonControl2(struct pipes_struct *p,
101 struct netr_LogonControl2 *r)
103 struct netr_LogonControl2Ex l;
105 l.in.logon_server = r->in.logon_server;
106 l.in.function_code = r->in.function_code;
107 l.in.level = r->in.level;
108 l.in.data = r->in.data;
109 l.out.query = r->out.query;
111 return _netr_LogonControl2Ex(p, &l);
114 /*************************************************************************
115 *************************************************************************/
117 static bool wb_change_trust_creds(const char *domain, WERROR *tc_status)
120 struct wbcAuthErrorInfo *error = NULL;
122 result = wbcChangeTrustCredentials(domain, &error);
124 case WBC_ERR_WINBIND_NOT_AVAILABLE:
126 case WBC_ERR_DOMAIN_NOT_FOUND:
127 *tc_status = WERR_NO_SUCH_DOMAIN;
129 case WBC_ERR_SUCCESS:
130 *tc_status = WERR_OK;
136 if (error && error->nt_status != 0) {
137 *tc_status = ntstatus_to_werror(NT_STATUS(error->nt_status));
139 *tc_status = WERR_TRUST_FAILURE;
141 wbcFreeMemory(error);
145 /*************************************************************************
146 *************************************************************************/
148 static bool wb_check_trust_creds(const char *domain, WERROR *tc_status)
151 struct wbcAuthErrorInfo *error = NULL;
153 result = wbcCheckTrustCredentials(domain, &error);
155 case WBC_ERR_WINBIND_NOT_AVAILABLE:
157 case WBC_ERR_DOMAIN_NOT_FOUND:
158 *tc_status = WERR_NO_SUCH_DOMAIN;
160 case WBC_ERR_SUCCESS:
161 *tc_status = WERR_OK;
167 if (error && error->nt_status != 0) {
168 *tc_status = ntstatus_to_werror(NT_STATUS(error->nt_status));
170 *tc_status = WERR_TRUST_FAILURE;
172 wbcFreeMemory(error);
176 /****************************************************************
177 _netr_LogonControl2Ex
178 ****************************************************************/
180 WERROR _netr_LogonControl2Ex(struct pipes_struct *p,
181 struct netr_LogonControl2Ex *r)
183 uint32_t flags = 0x0;
184 WERROR pdc_connection_status = WERR_OK;
185 uint32_t logon_attempts = 0x0;
188 const char *dc_name = NULL;
189 struct sockaddr_storage dc_ss;
190 const char *domain = NULL;
191 struct netr_NETLOGON_INFO_1 *info1;
192 struct netr_NETLOGON_INFO_2 *info2;
193 struct netr_NETLOGON_INFO_3 *info3;
194 struct netr_NETLOGON_INFO_4 *info4;
199 case NDR_NETR_LOGONCONTROL:
200 fn = "_netr_LogonControl";
202 case NDR_NETR_LOGONCONTROL2:
203 fn = "_netr_LogonControl2";
205 case NDR_NETR_LOGONCONTROL2EX:
206 fn = "_netr_LogonControl2Ex";
209 return WERR_INVALID_PARAM;
212 acct_ctrl = p->session_info->info->acct_flags;
214 switch (r->in.function_code) {
215 case NETLOGON_CONTROL_TC_VERIFY:
216 case NETLOGON_CONTROL_CHANGE_PASSWORD:
217 case NETLOGON_CONTROL_REDISCOVER:
218 if ((geteuid() != sec_initial_uid()) &&
219 !nt_token_check_domain_rid(p->session_info->security_token, DOMAIN_RID_ADMINS) &&
220 !nt_token_check_sid(&global_sid_Builtin_Administrators, p->session_info->security_token) &&
221 !(acct_ctrl & (ACB_WSTRUST | ACB_SVRTRUST))) {
222 return WERR_ACCESS_DENIED;
229 tc_status = WERR_NO_SUCH_DOMAIN;
231 switch (r->in.function_code) {
232 case NETLOGON_CONTROL_QUERY:
235 case NETLOGON_CONTROL_REPLICATE:
236 case NETLOGON_CONTROL_SYNCHRONIZE:
237 case NETLOGON_CONTROL_PDC_REPLICATE:
238 case NETLOGON_CONTROL_BACKUP_CHANGE_LOG:
239 case NETLOGON_CONTROL_BREAKPOINT:
240 if (acct_ctrl & ACB_NORMAL) {
241 return WERR_NOT_SUPPORTED;
242 } else if (acct_ctrl & (ACB_WSTRUST | ACB_SVRTRUST)) {
243 return WERR_ACCESS_DENIED;
245 return WERR_ACCESS_DENIED;
247 case NETLOGON_CONTROL_TRUNCATE_LOG:
248 if (acct_ctrl & ACB_NORMAL) {
250 } else if (acct_ctrl & (ACB_WSTRUST | ACB_SVRTRUST)) {
251 return WERR_ACCESS_DENIED;
253 return WERR_ACCESS_DENIED;
256 case NETLOGON_CONTROL_TRANSPORT_NOTIFY:
257 case NETLOGON_CONTROL_FORCE_DNS_REG:
258 case NETLOGON_CONTROL_QUERY_DNS_REG:
259 return WERR_NOT_SUPPORTED;
260 case NETLOGON_CONTROL_FIND_USER:
261 if (!r->in.data || !r->in.data->user) {
262 return WERR_NOT_SUPPORTED;
265 case NETLOGON_CONTROL_SET_DBFLAG:
267 return WERR_NOT_SUPPORTED;
270 case NETLOGON_CONTROL_TC_VERIFY:
271 if (!r->in.data || !r->in.data->domain) {
272 return WERR_NOT_SUPPORTED;
275 if (!wb_check_trust_creds(r->in.data->domain, &tc_status)) {
276 return WERR_NOT_SUPPORTED;
279 case NETLOGON_CONTROL_TC_QUERY:
280 if (!r->in.data || !r->in.data->domain) {
281 return WERR_NOT_SUPPORTED;
284 domain = r->in.data->domain;
286 if (!is_trusted_domain(domain)) {
290 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
291 tc_status = WERR_NO_LOGON_SERVERS;
295 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
304 case NETLOGON_CONTROL_REDISCOVER:
305 if (!r->in.data || !r->in.data->domain) {
306 return WERR_NOT_SUPPORTED;
309 domain = r->in.data->domain;
311 if (!is_trusted_domain(domain)) {
315 if (!get_dc_name(domain, NULL, dc_name2, &dc_ss)) {
316 tc_status = WERR_NO_LOGON_SERVERS;
320 dc_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dc_name2);
329 case NETLOGON_CONTROL_CHANGE_PASSWORD:
330 if (!r->in.data || !r->in.data->domain) {
331 return WERR_NOT_SUPPORTED;
334 if (!wb_change_trust_creds(r->in.data->domain, &tc_status)) {
335 return WERR_NOT_SUPPORTED;
340 /* no idea what this should be */
341 DEBUG(0,("%s: unimplemented function level [%d]\n",
342 fn, r->in.function_code));
343 return WERR_UNKNOWN_LEVEL;
346 /* prepare the response */
348 switch (r->in.level) {
350 info1 = talloc_zero(p->mem_ctx, struct netr_NETLOGON_INFO_1);
351 W_ERROR_HAVE_NO_MEMORY(info1);
353 info1->flags = flags;
354 info1->pdc_connection_status = pdc_connection_status;
356 r->out.query->info1 = info1;
359 info2 = talloc_zero(p->mem_ctx, struct netr_NETLOGON_INFO_2);
360 W_ERROR_HAVE_NO_MEMORY(info2);
362 info2->flags = flags;
363 info2->pdc_connection_status = pdc_connection_status;
364 info2->trusted_dc_name = dc_name;
365 info2->tc_connection_status = tc_status;
367 r->out.query->info2 = info2;
370 info3 = talloc_zero(p->mem_ctx, struct netr_NETLOGON_INFO_3);
371 W_ERROR_HAVE_NO_MEMORY(info3);
373 info3->flags = flags;
374 info3->logon_attempts = logon_attempts;
376 r->out.query->info3 = info3;
379 info4 = talloc_zero(p->mem_ctx, struct netr_NETLOGON_INFO_4);
380 W_ERROR_HAVE_NO_MEMORY(info4);
382 info4->trusted_dc_name = dc_name;
383 info4->trusted_domain_name = r->in.data->domain;
385 r->out.query->info4 = info4;
388 return WERR_UNKNOWN_LEVEL;
391 if (lp_server_role() == ROLE_DOMAIN_BDC) {
392 send_sync_message(p->msg_ctx);
398 /*************************************************************************
399 _netr_NetrEnumerateTrustedDomains
400 *************************************************************************/
402 NTSTATUS _netr_NetrEnumerateTrustedDomains(struct pipes_struct *p,
403 struct netr_NetrEnumerateTrustedDomains *r)
406 NTSTATUS result = NT_STATUS_OK;
409 const char **trusted_domains = NULL;
410 struct lsa_DomainList domain_list;
411 struct dcerpc_binding_handle *h = NULL;
412 struct policy_handle pol;
413 uint32_t enum_ctx = 0;
415 uint32_t max_size = (uint32_t)-1;
417 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
419 status = rpcint_binding_handle(p->mem_ctx,
425 if (!NT_STATUS_IS_OK(status)) {
429 status = dcerpc_lsa_open_policy2(h,
433 LSA_POLICY_VIEW_LOCAL_INFORMATION,
436 if (!NT_STATUS_IS_OK(status)) {
439 if (!NT_STATUS_IS_OK(result)) {
445 /* Lookup list of trusted domains */
446 status = dcerpc_lsa_EnumTrustDom(h,
453 if (!NT_STATUS_IS_OK(status)) {
456 if (!NT_STATUS_IS_OK(result) &&
457 !NT_STATUS_EQUAL(result, NT_STATUS_NO_MORE_ENTRIES) &&
458 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
463 for (i = 0; i < domain_list.count; i++) {
464 if (!add_string_to_array(p->mem_ctx, domain_list.domains[i].name.string,
465 &trusted_domains, &num_domains)) {
466 status = NT_STATUS_NO_MEMORY;
470 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
472 if (num_domains > 0) {
473 /* multi sz terminate */
474 trusted_domains = talloc_realloc(p->mem_ctx, trusted_domains, const char *, num_domains + 1);
475 if (trusted_domains == NULL) {
476 status = NT_STATUS_NO_MEMORY;
480 trusted_domains[num_domains] = NULL;
483 if (!push_reg_multi_sz(trusted_domains, &blob, trusted_domains)) {
484 TALLOC_FREE(trusted_domains);
485 status = NT_STATUS_NO_MEMORY;
489 r->out.trusted_domains_blob->data = blob.data;
490 r->out.trusted_domains_blob->length = blob.length;
492 DEBUG(6,("_netr_NetrEnumerateTrustedDomains: %d\n", __LINE__));
494 status = NT_STATUS_OK;
497 if (is_valid_policy_hnd(&pol)) {
498 dcerpc_lsa_Close(h, p->mem_ctx, &pol, &result);
504 /*************************************************************************
505 *************************************************************************/
507 static NTSTATUS samr_find_machine_account(TALLOC_CTX *mem_ctx,
508 struct dcerpc_binding_handle *b,
509 const char *account_name,
510 uint32_t access_mask,
511 struct dom_sid2 **domain_sid_p,
512 uint32_t *user_rid_p,
513 struct policy_handle *user_handle)
516 NTSTATUS result = NT_STATUS_OK;
517 struct policy_handle connect_handle, domain_handle;
518 struct lsa_String domain_name;
519 struct dom_sid2 *domain_sid;
520 struct lsa_String names;
521 struct samr_Ids rids;
522 struct samr_Ids types;
525 status = dcerpc_samr_Connect2(b, mem_ctx,
527 SAMR_ACCESS_CONNECT_TO_SERVER |
528 SAMR_ACCESS_ENUM_DOMAINS |
529 SAMR_ACCESS_LOOKUP_DOMAIN,
532 if (!NT_STATUS_IS_OK(status)) {
535 if (!NT_STATUS_IS_OK(result)) {
540 init_lsa_String(&domain_name, get_global_sam_name());
542 status = dcerpc_samr_LookupDomain(b, mem_ctx,
547 if (!NT_STATUS_IS_OK(status)) {
550 if (!NT_STATUS_IS_OK(result)) {
555 status = dcerpc_samr_OpenDomain(b, mem_ctx,
557 SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
561 if (!NT_STATUS_IS_OK(status)) {
564 if (!NT_STATUS_IS_OK(result)) {
569 init_lsa_String(&names, account_name);
571 status = dcerpc_samr_LookupNames(b, mem_ctx,
578 if (!NT_STATUS_IS_OK(status)) {
581 if (!NT_STATUS_IS_OK(result)) {
586 if (rids.count != 1) {
587 status = NT_STATUS_NO_SUCH_USER;
590 if (rids.count != types.count) {
591 status = NT_STATUS_INVALID_PARAMETER;
594 if (types.ids[0] != SID_NAME_USER) {
595 status = NT_STATUS_NO_SUCH_USER;
601 status = dcerpc_samr_OpenUser(b, mem_ctx,
607 if (!NT_STATUS_IS_OK(status)) {
610 if (!NT_STATUS_IS_OK(result)) {
620 *domain_sid_p = domain_sid;
624 if (is_valid_policy_hnd(&domain_handle)) {
625 dcerpc_samr_Close(b, mem_ctx, &domain_handle, &result);
627 if (is_valid_policy_hnd(&connect_handle)) {
628 dcerpc_samr_Close(b, mem_ctx, &connect_handle, &result);
634 /******************************************************************
635 gets a machine password entry. checks access rights of the host.
636 ******************************************************************/
638 static NTSTATUS get_md4pw(struct samr_Password *md4pw, const char *mach_acct,
639 enum netr_SchannelType sec_chan_type,
641 struct messaging_context *msg_ctx)
644 NTSTATUS result = NT_STATUS_OK;
646 struct dcerpc_binding_handle *h = NULL;
647 struct tsocket_address *local;
648 struct policy_handle user_handle;
650 struct dom_sid *domain_sid;
652 union samr_UserInfo *info;
653 struct auth_session_info *session_info;
659 * Currently this code is redundent as we already have a filter
660 * by hostname list. What this code really needs to do is to
661 * get a hosts allowed/hosts denied list from the SAM database
662 * on a per user basis, and make the access decision there.
663 * I will leave this code here for now as a reminder to implement
664 * this at a later date. JRA.
667 if (!allow_access(lp_domain_hostsdeny(), lp_domain_hostsallow(),
669 p->client_id.addr)) {
670 DEBUG(0,("get_md4pw: Workstation %s denied access to domain\n", mach_acct));
675 mem_ctx = talloc_stackframe();
676 if (mem_ctx == NULL) {
677 status = NT_STATUS_NO_MEMORY;
681 status = make_session_info_system(mem_ctx, &session_info);
682 if (!NT_STATUS_IS_OK(status)) {
686 ZERO_STRUCT(user_handle);
688 rc = tsocket_address_inet_from_strings(mem_ctx,
694 status = NT_STATUS_NO_MEMORY;
698 status = rpcint_binding_handle(mem_ctx,
704 if (!NT_STATUS_IS_OK(status)) {
709 status = samr_find_machine_account(mem_ctx, h, mach_acct,
710 SEC_FLAG_MAXIMUM_ALLOWED,
711 &domain_sid, &user_rid,
714 if (!NT_STATUS_IS_OK(status)) {
718 status = dcerpc_samr_QueryUserInfo2(h,
721 UserControlInformation,
724 if (!NT_STATUS_IS_OK(status)) {
727 if (!NT_STATUS_IS_OK(result)) {
732 acct_ctrl = info->info16.acct_flags;
734 if (acct_ctrl & ACB_DISABLED) {
735 DEBUG(0,("get_md4pw: Workstation %s: account is disabled\n", mach_acct));
736 status = NT_STATUS_ACCOUNT_DISABLED;
740 if (!(acct_ctrl & ACB_SVRTRUST) &&
741 !(acct_ctrl & ACB_WSTRUST) &&
742 !(acct_ctrl & ACB_DOMTRUST))
744 DEBUG(0,("get_md4pw: Workstation %s: account is not a trust account\n", mach_acct));
745 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
749 switch (sec_chan_type) {
751 if (!(acct_ctrl & ACB_SVRTRUST)) {
752 DEBUG(0,("get_md4pw: Workstation %s: BDC secure channel requested "
753 "but not a server trust account\n", mach_acct));
754 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
759 if (!(acct_ctrl & ACB_WSTRUST)) {
760 DEBUG(0,("get_md4pw: Workstation %s: WORKSTATION secure channel requested "
761 "but not a workstation trust account\n", mach_acct));
762 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
766 case SEC_CHAN_DOMAIN:
767 if (!(acct_ctrl & ACB_DOMTRUST)) {
768 DEBUG(0,("get_md4pw: Workstation %s: DOMAIN secure channel requested "
769 "but not a interdomain trust account\n", mach_acct));
770 status = NT_STATUS_NO_TRUST_SAM_ACCOUNT;
779 status = dcerpc_samr_QueryUserInfo2(h,
782 UserInternal1Information,
786 if (!NT_STATUS_IS_OK(status)) {
789 if (!NT_STATUS_IS_OK(result)) {
794 if (info->info18.nt_pwd_active == 0) {
795 DEBUG(0,("get_md4pw: Workstation %s: account does not have a password\n", mach_acct));
796 status = NT_STATUS_LOGON_FAILURE;
800 /* samr gives out nthash unencrypted (!) */
801 memcpy(md4pw->hash, info->info18.nt_pwd.hash, 16);
803 sid_compose(sid, domain_sid, user_rid);
806 if (h && is_valid_policy_hnd(&user_handle)) {
807 dcerpc_samr_Close(h, mem_ctx, &user_handle, &result);
810 talloc_free(mem_ctx);
815 /*************************************************************************
816 _netr_ServerReqChallenge
817 *************************************************************************/
819 NTSTATUS _netr_ServerReqChallenge(struct pipes_struct *p,
820 struct netr_ServerReqChallenge *r)
822 struct netlogon_server_pipe_state *pipe_state =
823 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
826 DEBUG(10,("_netr_ServerReqChallenge: new challenge requested. Clearing old state.\n"));
827 talloc_free(pipe_state);
828 p->private_data = NULL;
831 pipe_state = talloc(p, struct netlogon_server_pipe_state);
832 NT_STATUS_HAVE_NO_MEMORY(pipe_state);
834 pipe_state->client_challenge = *r->in.credentials;
836 generate_random_buffer(pipe_state->server_challenge.data,
837 sizeof(pipe_state->server_challenge.data));
839 *r->out.return_credentials = pipe_state->server_challenge;
841 p->private_data = pipe_state;
846 /*************************************************************************
847 _netr_ServerAuthenticate
848 Create the initial credentials.
849 *************************************************************************/
851 NTSTATUS _netr_ServerAuthenticate(struct pipes_struct *p,
852 struct netr_ServerAuthenticate *r)
854 struct netr_ServerAuthenticate3 a;
855 uint32_t negotiate_flags = 0;
858 a.in.server_name = r->in.server_name;
859 a.in.account_name = r->in.account_name;
860 a.in.secure_channel_type = r->in.secure_channel_type;
861 a.in.computer_name = r->in.computer_name;
862 a.in.credentials = r->in.credentials;
863 a.in.negotiate_flags = &negotiate_flags;
865 a.out.return_credentials = r->out.return_credentials;
867 a.out.negotiate_flags = &negotiate_flags;
869 return _netr_ServerAuthenticate3(p, &a);
873 /*************************************************************************
874 _netr_ServerAuthenticate3
875 *************************************************************************/
877 NTSTATUS _netr_ServerAuthenticate3(struct pipes_struct *p,
878 struct netr_ServerAuthenticate3 *r)
882 /* r->in.negotiate_flags is an aliased pointer to r->out.negotiate_flags,
883 * so use a copy to avoid destroying the client values. */
884 uint32_t in_neg_flags = *r->in.negotiate_flags;
886 struct loadparm_context *lp_ctx;
888 struct samr_Password mach_pwd;
889 struct netlogon_creds_CredentialState *creds;
890 struct netlogon_server_pipe_state *pipe_state =
891 talloc_get_type(p->private_data, struct netlogon_server_pipe_state);
893 /* According to Microsoft (see bugid #6099)
894 * Windows 7 looks at the negotiate_flags
895 * returned in this structure *even if the
896 * call fails with access denied* ! So in order
897 * to allow Win7 to connect to a Samba NT style
898 * PDC we set the flags before we know if it's
903 srv_flgs = NETLOGON_NEG_ACCOUNT_LOCKOUT |
904 NETLOGON_NEG_PERSISTENT_SAMREPL |
905 NETLOGON_NEG_ARCFOUR |
906 NETLOGON_NEG_PROMOTION_COUNT |
907 NETLOGON_NEG_CHANGELOG_BDC |
908 NETLOGON_NEG_FULL_SYNC_REPL |
909 NETLOGON_NEG_MULTIPLE_SIDS |
911 NETLOGON_NEG_PASSWORD_CHANGE_REFUSAL |
912 NETLOGON_NEG_PASSWORD_SET2;
914 /* Ensure we support strong (128-bit) keys. */
915 if (in_neg_flags & NETLOGON_NEG_STRONG_KEYS) {
916 srv_flgs |= NETLOGON_NEG_STRONG_KEYS;
919 if (lp_server_schannel() != false) {
920 srv_flgs |= NETLOGON_NEG_SCHANNEL;
924 case NDR_NETR_SERVERAUTHENTICATE:
925 fn = "_netr_ServerAuthenticate";
927 case NDR_NETR_SERVERAUTHENTICATE2:
928 fn = "_netr_ServerAuthenticate2";
930 case NDR_NETR_SERVERAUTHENTICATE3:
931 fn = "_netr_ServerAuthenticate3";
934 return NT_STATUS_INTERNAL_ERROR;
937 /* We use this as the key to store the creds: */
938 /* r->in.computer_name */
941 DEBUG(0,("%s: no challenge sent to client %s\n", fn,
942 r->in.computer_name));
943 status = NT_STATUS_ACCESS_DENIED;
947 if ( (lp_server_schannel() == true) &&
948 ((in_neg_flags & NETLOGON_NEG_SCHANNEL) == 0) ) {
950 /* schannel must be used, but client did not offer it. */
951 DEBUG(0,("%s: schannel required but client failed "
952 "to offer it. Client was %s\n",
953 fn, r->in.account_name));
954 status = NT_STATUS_ACCESS_DENIED;
958 status = get_md4pw(&mach_pwd,
960 r->in.secure_channel_type,
962 if (!NT_STATUS_IS_OK(status)) {
963 DEBUG(0,("%s: failed to get machine password for "
965 fn, r->in.account_name, nt_errstr(status) ));
966 /* always return NT_STATUS_ACCESS_DENIED */
967 status = NT_STATUS_ACCESS_DENIED;
971 /* From the client / server challenges and md4 password, generate sess key */
972 /* Check client credentials are valid. */
973 creds = netlogon_creds_server_init(p->mem_ctx,
976 r->in.secure_channel_type,
977 &pipe_state->client_challenge,
978 &pipe_state->server_challenge,
981 r->out.return_credentials,
982 *r->in.negotiate_flags);
984 DEBUG(0,("%s: netlogon_creds_server_check failed. Rejecting auth "
985 "request from client %s machine account %s\n",
986 fn, r->in.computer_name,
987 r->in.account_name));
988 status = NT_STATUS_ACCESS_DENIED;
992 creds->sid = dom_sid_dup(creds, &sid);
994 status = NT_STATUS_NO_MEMORY;
998 lp_ctx = loadparm_init_s3(p->mem_ctx, loadparm_s3_context());
999 if (lp_ctx == NULL) {
1000 DEBUG(10, ("loadparm_init_s3 failed\n"));
1001 status = NT_STATUS_INTERNAL_ERROR;
1005 /* Store off the state so we can continue after client disconnect. */
1007 status = schannel_save_creds_state(p->mem_ctx, lp_ctx, creds);
1010 talloc_unlink(p->mem_ctx, lp_ctx);
1012 if (!NT_STATUS_IS_OK(status)) {
1016 sid_peek_rid(&sid, r->out.rid);
1018 status = NT_STATUS_OK;
1022 *r->out.negotiate_flags = srv_flgs;
1026 /*************************************************************************
1027 _netr_ServerAuthenticate2
1028 *************************************************************************/
1030 NTSTATUS _netr_ServerAuthenticate2(struct pipes_struct *p,
1031 struct netr_ServerAuthenticate2 *r)
1033 struct netr_ServerAuthenticate3 a;
1036 a.in.server_name = r->in.server_name;
1037 a.in.account_name = r->in.account_name;
1038 a.in.secure_channel_type = r->in.secure_channel_type;
1039 a.in.computer_name = r->in.computer_name;
1040 a.in.credentials = r->in.credentials;
1041 a.in.negotiate_flags = r->in.negotiate_flags;
1043 a.out.return_credentials = r->out.return_credentials;
1045 a.out.negotiate_flags = r->out.negotiate_flags;
1047 return _netr_ServerAuthenticate3(p, &a);
1050 /*************************************************************************
1051 * If schannel is required for this call test that it actually is available.
1052 *************************************************************************/
1053 static NTSTATUS schannel_check_required(struct pipe_auth_data *auth_info,
1054 const char *computer_name,
1055 bool integrity, bool privacy)
1057 if (auth_info && auth_info->auth_type == DCERPC_AUTH_TYPE_SCHANNEL) {
1058 if (!privacy && !integrity) {
1059 return NT_STATUS_OK;
1062 if ((!privacy && integrity) &&
1063 auth_info->auth_level == DCERPC_AUTH_LEVEL_INTEGRITY) {
1064 return NT_STATUS_OK;
1067 if ((privacy || integrity) &&
1068 auth_info->auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
1069 return NT_STATUS_OK;
1073 /* test didn't pass */
1074 DEBUG(0, ("schannel_check_required: [%s] is not using schannel\n",
1077 return NT_STATUS_ACCESS_DENIED;
1080 /*************************************************************************
1081 *************************************************************************/
1083 static NTSTATUS netr_creds_server_step_check(struct pipes_struct *p,
1084 TALLOC_CTX *mem_ctx,
1085 const char *computer_name,
1086 struct netr_Authenticator *received_authenticator,
1087 struct netr_Authenticator *return_authenticator,
1088 struct netlogon_creds_CredentialState **creds_out)
1091 bool schannel_global_required = (lp_server_schannel() == true) ? true:false;
1092 struct loadparm_context *lp_ctx;
1094 if (schannel_global_required) {
1095 status = schannel_check_required(&p->auth,
1098 if (!NT_STATUS_IS_OK(status)) {
1103 lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_context());
1104 if (lp_ctx == NULL) {
1105 DEBUG(0, ("loadparm_init_s3 failed\n"));
1106 return NT_STATUS_INTERNAL_ERROR;
1109 status = schannel_check_creds_state(mem_ctx, lp_ctx,
1110 computer_name, received_authenticator,
1111 return_authenticator, creds_out);
1112 talloc_unlink(mem_ctx, lp_ctx);
1116 /*************************************************************************
1117 *************************************************************************/
1119 static NTSTATUS netr_set_machine_account_password(TALLOC_CTX *mem_ctx,
1120 struct auth_session_info *session_info,
1121 struct messaging_context *msg_ctx,
1122 const char *account_name,
1123 struct samr_Password *nt_hash)
1126 NTSTATUS result = NT_STATUS_OK;
1127 struct dcerpc_binding_handle *h = NULL;
1128 struct tsocket_address *local;
1129 struct policy_handle user_handle;
1131 union samr_UserInfo *info;
1132 struct samr_UserInfo18 info18;
1136 ZERO_STRUCT(user_handle);
1138 rc = tsocket_address_inet_from_strings(mem_ctx,
1144 status = NT_STATUS_NO_MEMORY;
1148 status = rpcint_binding_handle(mem_ctx,
1154 if (!NT_STATUS_IS_OK(status)) {
1159 status = samr_find_machine_account(mem_ctx,
1162 SEC_FLAG_MAXIMUM_ALLOWED,
1167 if (!NT_STATUS_IS_OK(status)) {
1171 status = dcerpc_samr_QueryUserInfo2(h,
1174 UserControlInformation,
1177 if (!NT_STATUS_IS_OK(status)) {
1180 if (!NT_STATUS_IS_OK(result)) {
1185 acct_ctrl = info->info16.acct_flags;
1187 if (!(acct_ctrl & ACB_WSTRUST ||
1188 acct_ctrl & ACB_SVRTRUST ||
1189 acct_ctrl & ACB_DOMTRUST)) {
1190 status = NT_STATUS_NO_SUCH_USER;
1194 if (acct_ctrl & ACB_DISABLED) {
1195 status = NT_STATUS_ACCOUNT_DISABLED;
1199 ZERO_STRUCT(info18);
1201 in = data_blob_const(nt_hash->hash, 16);
1202 out = data_blob_talloc_zero(mem_ctx, 16);
1203 sess_crypt_blob(&out, &in, &session_info->session_key, true);
1204 memcpy(info18.nt_pwd.hash, out.data, out.length);
1206 info18.nt_pwd_active = true;
1208 info->info18 = info18;
1211 status = dcerpc_samr_SetUserInfo2(h,
1214 UserInternal1Information,
1218 if (!NT_STATUS_IS_OK(status)) {
1221 if (!NT_STATUS_IS_OK(result)) {
1227 if (h && is_valid_policy_hnd(&user_handle)) {
1228 dcerpc_samr_Close(h, mem_ctx, &user_handle, &result);
1234 /*************************************************************************
1235 _netr_ServerPasswordSet
1236 *************************************************************************/
1238 NTSTATUS _netr_ServerPasswordSet(struct pipes_struct *p,
1239 struct netr_ServerPasswordSet *r)
1241 NTSTATUS status = NT_STATUS_OK;
1243 struct netlogon_creds_CredentialState *creds;
1245 DEBUG(5,("_netr_ServerPasswordSet: %d\n", __LINE__));
1248 status = netr_creds_server_step_check(p, p->mem_ctx,
1249 r->in.computer_name,
1251 r->out.return_authenticator,
1255 if (!NT_STATUS_IS_OK(status)) {
1256 DEBUG(2,("_netr_ServerPasswordSet: netlogon_creds_server_step failed. Rejecting auth "
1257 "request from client %s machine account %s\n",
1258 r->in.computer_name, creds->computer_name));
1263 DEBUG(3,("_netr_ServerPasswordSet: Server Password Set by remote machine:[%s] on account [%s]\n",
1264 r->in.computer_name, creds->computer_name));
1266 netlogon_creds_des_decrypt(creds, r->in.new_password);
1268 DEBUG(100,("_netr_ServerPasswordSet: new given value was :\n"));
1269 for(i = 0; i < sizeof(r->in.new_password->hash); i++)
1270 DEBUG(100,("%02X ", r->in.new_password->hash[i]));
1273 status = netr_set_machine_account_password(p->mem_ctx,
1276 creds->account_name,
1277 r->in.new_password);
1281 /****************************************************************
1282 _netr_ServerPasswordSet2
1283 ****************************************************************/
1285 NTSTATUS _netr_ServerPasswordSet2(struct pipes_struct *p,
1286 struct netr_ServerPasswordSet2 *r)
1289 struct netlogon_creds_CredentialState *creds;
1290 DATA_BLOB plaintext;
1291 struct samr_CryptPassword password_buf;
1292 struct samr_Password nt_hash;
1295 status = netr_creds_server_step_check(p, p->mem_ctx,
1296 r->in.computer_name,
1298 r->out.return_authenticator,
1302 if (!NT_STATUS_IS_OK(status)) {
1303 DEBUG(2,("_netr_ServerPasswordSet2: netlogon_creds_server_step "
1304 "failed. Rejecting auth request from client %s machine account %s\n",
1305 r->in.computer_name, creds->computer_name));
1310 memcpy(password_buf.data, r->in.new_password->data, 512);
1311 SIVAL(password_buf.data, 512, r->in.new_password->length);
1312 netlogon_creds_arcfour_crypt(creds, password_buf.data, 516);
1314 if (!extract_pw_from_buffer(p->mem_ctx, password_buf.data, &plaintext)) {
1315 return NT_STATUS_WRONG_PASSWORD;
1318 mdfour(nt_hash.hash, plaintext.data, plaintext.length);
1320 status = netr_set_machine_account_password(p->mem_ctx,
1323 creds->account_name,
1328 /*************************************************************************
1329 _netr_LogonSamLogoff
1330 *************************************************************************/
1332 NTSTATUS _netr_LogonSamLogoff(struct pipes_struct *p,
1333 struct netr_LogonSamLogoff *r)
1336 struct netlogon_creds_CredentialState *creds;
1339 status = netr_creds_server_step_check(p, p->mem_ctx,
1340 r->in.computer_name,
1342 r->out.return_authenticator,
1349 static NTSTATUS _netr_LogonSamLogon_check(const struct netr_LogonSamLogonEx *r)
1351 switch (r->in.logon_level) {
1352 case NetlogonInteractiveInformation:
1353 case NetlogonServiceInformation:
1354 case NetlogonInteractiveTransitiveInformation:
1355 case NetlogonServiceTransitiveInformation:
1356 if (r->in.logon->password == NULL) {
1357 return NT_STATUS_INVALID_PARAMETER;
1360 switch (r->in.validation_level) {
1361 case NetlogonValidationSamInfo: /* 2 */
1362 case NetlogonValidationSamInfo2: /* 3 */
1364 case NetlogonValidationSamInfo4: /* 6 */
1365 if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
1366 DEBUG(10,("Not adding validation info level 6 "
1367 "without ADS passdb backend\n"));
1368 return NT_STATUS_INVALID_INFO_CLASS;
1372 return NT_STATUS_INVALID_INFO_CLASS;
1376 case NetlogonNetworkInformation:
1377 case NetlogonNetworkTransitiveInformation:
1378 if (r->in.logon->network == NULL) {
1379 return NT_STATUS_INVALID_PARAMETER;
1382 switch (r->in.validation_level) {
1383 case NetlogonValidationSamInfo: /* 2 */
1384 case NetlogonValidationSamInfo2: /* 3 */
1386 case NetlogonValidationSamInfo4: /* 6 */
1387 if ((pdb_capabilities() & PDB_CAP_ADS) == 0) {
1388 DEBUG(10,("Not adding validation info level 6 "
1389 "without ADS passdb backend\n"));
1390 return NT_STATUS_INVALID_INFO_CLASS;
1394 return NT_STATUS_INVALID_INFO_CLASS;
1399 case NetlogonGenericInformation:
1400 if (r->in.logon->generic == NULL) {
1401 return NT_STATUS_INVALID_PARAMETER;
1404 /* we don't support this here */
1405 return NT_STATUS_INVALID_PARAMETER;
1407 switch (r->in.validation_level) {
1408 /* TODO: case NetlogonValidationGenericInfo: 4 */
1409 case NetlogonValidationGenericInfo2: /* 5 */
1412 return NT_STATUS_INVALID_INFO_CLASS;
1418 return NT_STATUS_INVALID_PARAMETER;
1421 return NT_STATUS_OK;
1424 /*************************************************************************
1425 _netr_LogonSamLogon_base
1426 *************************************************************************/
1428 static NTSTATUS _netr_LogonSamLogon_base(struct pipes_struct *p,
1429 struct netr_LogonSamLogonEx *r,
1430 struct netlogon_creds_CredentialState *creds)
1432 NTSTATUS status = NT_STATUS_OK;
1433 union netr_LogonLevel *logon = r->in.logon;
1434 const char *nt_username, *nt_domain, *nt_workstation;
1435 struct auth_usersupplied_info *user_info = NULL;
1436 struct auth_serversupplied_info *server_info = NULL;
1437 struct auth_context *auth_context = NULL;
1438 uint8_t pipe_session_key[16];
1439 bool process_creds = true;
1443 case NDR_NETR_LOGONSAMLOGON:
1444 process_creds = true;
1445 fn = "_netr_LogonSamLogon";
1447 case NDR_NETR_LOGONSAMLOGONWITHFLAGS:
1448 process_creds = true;
1449 fn = "_netr_LogonSamLogonWithFlags";
1451 case NDR_NETR_LOGONSAMLOGONEX:
1452 process_creds = false;
1453 fn = "_netr_LogonSamLogonEx";
1456 return NT_STATUS_INTERNAL_ERROR;
1459 *r->out.authoritative = true; /* authoritative response */
1461 switch (r->in.validation_level) {
1463 r->out.validation->sam2 = talloc_zero(p->mem_ctx, struct netr_SamInfo2);
1464 if (!r->out.validation->sam2) {
1465 return NT_STATUS_NO_MEMORY;
1469 r->out.validation->sam3 = talloc_zero(p->mem_ctx, struct netr_SamInfo3);
1470 if (!r->out.validation->sam3) {
1471 return NT_STATUS_NO_MEMORY;
1475 r->out.validation->sam6 = talloc_zero(p->mem_ctx, struct netr_SamInfo6);
1476 if (!r->out.validation->sam6) {
1477 return NT_STATUS_NO_MEMORY;
1481 DEBUG(0,("%s: bad validation_level value %d.\n",
1482 fn, (int)r->in.validation_level));
1483 return NT_STATUS_INVALID_INFO_CLASS;
1486 switch (r->in.logon_level) {
1487 case NetlogonInteractiveInformation:
1488 case NetlogonServiceInformation:
1489 case NetlogonInteractiveTransitiveInformation:
1490 case NetlogonServiceTransitiveInformation:
1491 nt_username = logon->password->identity_info.account_name.string ?
1492 logon->password->identity_info.account_name.string : "";
1493 nt_domain = logon->password->identity_info.domain_name.string ?
1494 logon->password->identity_info.domain_name.string : "";
1495 nt_workstation = logon->password->identity_info.workstation.string ?
1496 logon->password->identity_info.workstation.string : "";
1498 DEBUG(3,("SAM Logon (Interactive). Domain:[%s]. ", lp_workgroup()));
1500 case NetlogonNetworkInformation:
1501 case NetlogonNetworkTransitiveInformation:
1502 nt_username = logon->network->identity_info.account_name.string ?
1503 logon->network->identity_info.account_name.string : "";
1504 nt_domain = logon->network->identity_info.domain_name.string ?
1505 logon->network->identity_info.domain_name.string : "";
1506 nt_workstation = logon->network->identity_info.workstation.string ?
1507 logon->network->identity_info.workstation.string : "";
1509 DEBUG(3,("SAM Logon (Network). Domain:[%s]. ", lp_workgroup()));
1512 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1513 return NT_STATUS_INVALID_INFO_CLASS;
1516 DEBUG(3,("User:[%s@%s] Requested Domain:[%s]\n", nt_username, nt_workstation, nt_domain));
1517 fstrcpy(current_user_info.smb_name, nt_username);
1518 sub_set_smb_name(nt_username);
1520 DEBUG(5,("Attempting validation level %d for unmapped username %s.\n",
1521 r->in.validation_level, nt_username));
1523 status = NT_STATUS_OK;
1525 switch (r->in.logon_level) {
1526 case NetlogonNetworkInformation:
1527 case NetlogonNetworkTransitiveInformation:
1529 const char *wksname = nt_workstation;
1531 status = make_auth_context_fixed(talloc_tos(), &auth_context,
1532 logon->network->challenge);
1533 if (!NT_STATUS_IS_OK(status)) {
1537 /* For a network logon, the workstation name comes in with two
1538 * backslashes in the front. Strip them if they are there. */
1540 if (*wksname == '\\') wksname++;
1541 if (*wksname == '\\') wksname++;
1543 /* Standard challenge/response authentication */
1544 if (!make_user_info_netlogon_network(&user_info,
1545 nt_username, nt_domain,
1548 logon->network->identity_info.parameter_control,
1549 logon->network->lm.data,
1550 logon->network->lm.length,
1551 logon->network->nt.data,
1552 logon->network->nt.length)) {
1553 status = NT_STATUS_NO_MEMORY;
1557 case NetlogonInteractiveInformation:
1558 case NetlogonServiceInformation:
1559 case NetlogonInteractiveTransitiveInformation:
1560 case NetlogonServiceTransitiveInformation:
1562 /* 'Interactive' authentication, supplies the password in its
1563 MD4 form, encrypted with the session key. We will convert
1564 this to challenge/response for the auth subsystem to chew
1569 status = make_auth_context_subsystem(talloc_tos(),
1571 if (!NT_STATUS_IS_OK(status)) {
1575 auth_context->get_ntlm_challenge(auth_context, chal);
1577 if (!make_user_info_netlogon_interactive(&user_info,
1578 nt_username, nt_domain,
1581 logon->password->identity_info.parameter_control,
1583 logon->password->lmpassword.hash,
1584 logon->password->ntpassword.hash,
1585 creds->session_key)) {
1586 status = NT_STATUS_NO_MEMORY;
1591 DEBUG(2,("SAM Logon: unsupported switch value\n"));
1592 return NT_STATUS_INVALID_INFO_CLASS;
1595 if ( NT_STATUS_IS_OK(status) ) {
1596 status = auth_context->check_ntlm_password(auth_context,
1597 user_info, &server_info);
1600 TALLOC_FREE(auth_context);
1601 free_user_info(&user_info);
1603 DEBUG(5,("%s: check_password returned status %s\n",
1604 fn, nt_errstr(status)));
1606 /* Check account and password */
1608 if (!NT_STATUS_IS_OK(status)) {
1609 /* If we don't know what this domain is, we need to
1610 indicate that we are not authoritative. This
1611 allows the client to decide if it needs to try
1612 a local user. Fix by jpjanosi@us.ibm.com, #2976 */
1613 if ( NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)
1614 && !strequal(nt_domain, get_global_sam_name())
1615 && !is_trusted_domain(nt_domain) )
1616 *r->out.authoritative = false; /* We are not authoritative */
1618 TALLOC_FREE(server_info);
1622 if (server_info->guest) {
1623 /* We don't like guest domain logons... */
1624 DEBUG(5,("%s: Attempted domain logon as GUEST "
1626 TALLOC_FREE(server_info);
1627 return NT_STATUS_LOGON_FAILURE;
1630 /* This is the point at which, if the login was successful, that
1631 the SAM Local Security Authority should record that the user is
1632 logged in to the domain. */
1634 if (process_creds) {
1635 /* Get the pipe session key from the creds. */
1636 memcpy(pipe_session_key, creds->session_key, 16);
1638 struct schannel_state *schannel_auth;
1639 /* Get the pipe session key from the schannel. */
1640 if ((p->auth.auth_type != DCERPC_AUTH_TYPE_SCHANNEL)
1641 || (p->auth.auth_ctx == NULL)) {
1642 return NT_STATUS_INVALID_HANDLE;
1645 schannel_auth = talloc_get_type_abort(p->auth.auth_ctx,
1646 struct schannel_state);
1647 memcpy(pipe_session_key, schannel_auth->creds->session_key, 16);
1650 switch (r->in.validation_level) {
1652 status = serverinfo_to_SamInfo2(server_info, pipe_session_key, 16,
1653 r->out.validation->sam2);
1656 status = serverinfo_to_SamInfo3(server_info, pipe_session_key, 16,
1657 r->out.validation->sam3);
1660 status = serverinfo_to_SamInfo6(server_info, pipe_session_key, 16,
1661 r->out.validation->sam6);
1665 TALLOC_FREE(server_info);
1670 /****************************************************************
1671 _netr_LogonSamLogonWithFlags
1672 ****************************************************************/
1674 NTSTATUS _netr_LogonSamLogonWithFlags(struct pipes_struct *p,
1675 struct netr_LogonSamLogonWithFlags *r)
1678 struct netlogon_creds_CredentialState *creds;
1679 struct netr_LogonSamLogonEx r2;
1680 struct netr_Authenticator return_authenticator;
1682 *r->out.authoritative = true;
1684 r2.in.server_name = r->in.server_name;
1685 r2.in.computer_name = r->in.computer_name;
1686 r2.in.logon_level = r->in.logon_level;
1687 r2.in.logon = r->in.logon;
1688 r2.in.validation_level = r->in.validation_level;
1689 r2.in.flags = r->in.flags;
1690 r2.out.validation = r->out.validation;
1691 r2.out.authoritative = r->out.authoritative;
1692 r2.out.flags = r->out.flags;
1694 status = _netr_LogonSamLogon_check(&r2);
1695 if (!NT_STATUS_IS_OK(status)) {
1700 status = netr_creds_server_step_check(p, p->mem_ctx,
1701 r->in.computer_name,
1703 &return_authenticator,
1706 if (!NT_STATUS_IS_OK(status)) {
1710 status = _netr_LogonSamLogon_base(p, &r2, creds);
1712 *r->out.return_authenticator = return_authenticator;
1717 /*************************************************************************
1719 *************************************************************************/
1721 NTSTATUS _netr_LogonSamLogon(struct pipes_struct *p,
1722 struct netr_LogonSamLogon *r)
1725 struct netr_LogonSamLogonWithFlags r2;
1728 r2.in.server_name = r->in.server_name;
1729 r2.in.computer_name = r->in.computer_name;
1730 r2.in.credential = r->in.credential;
1731 r2.in.logon_level = r->in.logon_level;
1732 r2.in.logon = r->in.logon;
1733 r2.in.validation_level = r->in.validation_level;
1734 r2.in.return_authenticator = r->in.return_authenticator;
1735 r2.in.flags = &flags;
1736 r2.out.validation = r->out.validation;
1737 r2.out.authoritative = r->out.authoritative;
1738 r2.out.flags = &flags;
1739 r2.out.return_authenticator = r->out.return_authenticator;
1741 status = _netr_LogonSamLogonWithFlags(p, &r2);
1746 /*************************************************************************
1747 _netr_LogonSamLogonEx
1748 - no credential chaining. Map into net sam logon.
1749 *************************************************************************/
1751 NTSTATUS _netr_LogonSamLogonEx(struct pipes_struct *p,
1752 struct netr_LogonSamLogonEx *r)
1755 struct netlogon_creds_CredentialState *creds = NULL;
1756 struct loadparm_context *lp_ctx;
1758 *r->out.authoritative = true;
1760 status = _netr_LogonSamLogon_check(r);
1761 if (!NT_STATUS_IS_OK(status)) {
1765 /* Only allow this if the pipe is protected. */
1766 if (p->auth.auth_type != DCERPC_AUTH_TYPE_SCHANNEL) {
1767 DEBUG(0,("_netr_LogonSamLogonEx: client %s not using schannel for netlogon\n",
1768 get_remote_machine_name() ));
1769 return NT_STATUS_INVALID_PARAMETER;
1772 lp_ctx = loadparm_init_s3(p->mem_ctx, loadparm_s3_context());
1773 if (lp_ctx == NULL) {
1774 DEBUG(0, ("loadparm_init_s3 failed\n"));
1775 return NT_STATUS_INTERNAL_ERROR;
1779 status = schannel_get_creds_state(p->mem_ctx, lp_ctx,
1780 r->in.computer_name, &creds);
1782 talloc_unlink(p->mem_ctx, lp_ctx);
1784 if (!NT_STATUS_IS_OK(status)) {
1788 status = _netr_LogonSamLogon_base(p, r, creds);
1794 /*************************************************************************
1796 *************************************************************************/
1797 #if 0 /* JERRY -- not correct */
1798 NTSTATUS _ds_enum_dom_trusts(struct pipes_struct *p, DS_Q_ENUM_DOM_TRUSTS *q_u,
1799 DS_R_ENUM_DOM_TRUSTS *r_u)
1801 NTSTATUS status = NT_STATUS_OK;
1803 /* TODO: According to MSDN, the can only be executed against a
1804 DC or domain member running Windows 2000 or later. Need
1805 to test against a standalone 2k server and see what it
1806 does. A windows 2000 DC includes its own domain in the
1814 /****************************************************************
1815 ****************************************************************/
1817 WERROR _netr_LogonUasLogon(struct pipes_struct *p,
1818 struct netr_LogonUasLogon *r)
1820 p->rng_fault_state = true;
1821 return WERR_NOT_SUPPORTED;
1824 /****************************************************************
1825 ****************************************************************/
1827 WERROR _netr_LogonUasLogoff(struct pipes_struct *p,
1828 struct netr_LogonUasLogoff *r)
1830 p->rng_fault_state = true;
1831 return WERR_NOT_SUPPORTED;
1834 /****************************************************************
1835 ****************************************************************/
1837 NTSTATUS _netr_DatabaseDeltas(struct pipes_struct *p,
1838 struct netr_DatabaseDeltas *r)
1840 p->rng_fault_state = true;
1841 return NT_STATUS_NOT_IMPLEMENTED;
1844 /****************************************************************
1845 ****************************************************************/
1847 NTSTATUS _netr_DatabaseSync(struct pipes_struct *p,
1848 struct netr_DatabaseSync *r)
1850 p->rng_fault_state = true;
1851 return NT_STATUS_NOT_IMPLEMENTED;
1854 /****************************************************************
1855 ****************************************************************/
1857 NTSTATUS _netr_AccountDeltas(struct pipes_struct *p,
1858 struct netr_AccountDeltas *r)
1860 p->rng_fault_state = true;
1861 return NT_STATUS_NOT_IMPLEMENTED;
1864 /****************************************************************
1865 ****************************************************************/
1867 NTSTATUS _netr_AccountSync(struct pipes_struct *p,
1868 struct netr_AccountSync *r)
1870 p->rng_fault_state = true;
1871 return NT_STATUS_NOT_IMPLEMENTED;
1874 /****************************************************************
1875 ****************************************************************/
1877 static bool wb_getdcname(TALLOC_CTX *mem_ctx,
1879 const char **dcname,
1884 struct wbcDomainControllerInfo *dc_info = NULL;
1886 result = wbcLookupDomainController(domain,
1890 case WBC_ERR_SUCCESS:
1892 case WBC_ERR_WINBIND_NOT_AVAILABLE:
1894 case WBC_ERR_DOMAIN_NOT_FOUND:
1895 *werr = WERR_NO_SUCH_DOMAIN;
1898 *werr = WERR_DOMAIN_CONTROLLER_NOT_FOUND;
1902 *dcname = talloc_strdup(mem_ctx, dc_info->dc_name);
1903 wbcFreeMemory(dc_info);
1914 /****************************************************************
1916 ****************************************************************/
1918 WERROR _netr_GetDcName(struct pipes_struct *p,
1919 struct netr_GetDcName *r)
1924 struct netr_DsRGetDCNameInfo *info;
1927 ret = wb_getdcname(p->mem_ctx,
1930 WBC_LOOKUP_DC_IS_FLAT_NAME |
1931 WBC_LOOKUP_DC_RETURN_FLAT_NAME |
1932 WBC_LOOKUP_DC_PDC_REQUIRED,
1938 flags = DS_PDC_REQUIRED | DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
1940 status = dsgetdcname(p->mem_ctx,
1947 if (!NT_STATUS_IS_OK(status)) {
1948 return ntstatus_to_werror(status);
1951 *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
1953 if (!*r->out.dcname) {
1960 /****************************************************************
1962 ****************************************************************/
1964 WERROR _netr_GetAnyDCName(struct pipes_struct *p,
1965 struct netr_GetAnyDCName *r)
1970 struct netr_DsRGetDCNameInfo *info;
1973 ret = wb_getdcname(p->mem_ctx,
1976 WBC_LOOKUP_DC_IS_FLAT_NAME |
1977 WBC_LOOKUP_DC_RETURN_FLAT_NAME,
1983 flags = DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME;
1985 status = dsgetdcname(p->mem_ctx,
1992 if (!NT_STATUS_IS_OK(status)) {
1993 return ntstatus_to_werror(status);
1996 *r->out.dcname = talloc_strdup(p->mem_ctx, info->dc_unc);
1998 if (!*r->out.dcname) {
2005 /****************************************************************
2006 ****************************************************************/
2008 NTSTATUS _netr_DatabaseSync2(struct pipes_struct *p,
2009 struct netr_DatabaseSync2 *r)
2011 p->rng_fault_state = true;
2012 return NT_STATUS_NOT_IMPLEMENTED;
2015 /****************************************************************
2016 ****************************************************************/
2018 NTSTATUS _netr_DatabaseRedo(struct pipes_struct *p,
2019 struct netr_DatabaseRedo *r)
2021 p->rng_fault_state = true;
2022 return NT_STATUS_NOT_IMPLEMENTED;
2025 /****************************************************************
2026 ****************************************************************/
2028 WERROR _netr_DsRGetDCName(struct pipes_struct *p,
2029 struct netr_DsRGetDCName *r)
2031 p->rng_fault_state = true;
2032 return WERR_NOT_SUPPORTED;
2035 /****************************************************************
2036 ****************************************************************/
2038 NTSTATUS _netr_LogonGetCapabilities(struct pipes_struct *p,
2039 struct netr_LogonGetCapabilities *r)
2041 return NT_STATUS_NOT_IMPLEMENTED;
2044 /****************************************************************
2045 ****************************************************************/
2047 WERROR _netr_NETRLOGONSETSERVICEBITS(struct pipes_struct *p,
2048 struct netr_NETRLOGONSETSERVICEBITS *r)
2050 p->rng_fault_state = true;
2051 return WERR_NOT_SUPPORTED;
2054 /****************************************************************
2055 ****************************************************************/
2057 WERROR _netr_LogonGetTrustRid(struct pipes_struct *p,
2058 struct netr_LogonGetTrustRid *r)
2060 p->rng_fault_state = true;
2061 return WERR_NOT_SUPPORTED;
2064 /****************************************************************
2065 ****************************************************************/
2067 WERROR _netr_NETRLOGONCOMPUTESERVERDIGEST(struct pipes_struct *p,
2068 struct netr_NETRLOGONCOMPUTESERVERDIGEST *r)
2070 p->rng_fault_state = true;
2071 return WERR_NOT_SUPPORTED;
2074 /****************************************************************
2075 ****************************************************************/
2077 WERROR _netr_NETRLOGONCOMPUTECLIENTDIGEST(struct pipes_struct *p,
2078 struct netr_NETRLOGONCOMPUTECLIENTDIGEST *r)
2080 p->rng_fault_state = true;
2081 return WERR_NOT_SUPPORTED;
2084 /****************************************************************
2085 ****************************************************************/
2087 WERROR _netr_DsRGetDCNameEx(struct pipes_struct *p,
2088 struct netr_DsRGetDCNameEx *r)
2090 p->rng_fault_state = true;
2091 return WERR_NOT_SUPPORTED;
2094 /****************************************************************
2095 ****************************************************************/
2097 WERROR _netr_DsRGetSiteName(struct pipes_struct *p,
2098 struct netr_DsRGetSiteName *r)
2100 p->rng_fault_state = true;
2101 return WERR_NOT_SUPPORTED;
2104 /****************************************************************
2105 ****************************************************************/
2107 NTSTATUS _netr_LogonGetDomainInfo(struct pipes_struct *p,
2108 struct netr_LogonGetDomainInfo *r)
2110 p->rng_fault_state = true;
2111 return NT_STATUS_NOT_IMPLEMENTED;
2114 /****************************************************************
2115 ****************************************************************/
2117 WERROR _netr_ServerPasswordGet(struct pipes_struct *p,
2118 struct netr_ServerPasswordGet *r)
2120 p->rng_fault_state = true;
2121 return WERR_NOT_SUPPORTED;
2124 /****************************************************************
2125 ****************************************************************/
2127 WERROR _netr_NETRLOGONSENDTOSAM(struct pipes_struct *p,
2128 struct netr_NETRLOGONSENDTOSAM *r)
2130 p->rng_fault_state = true;
2131 return WERR_NOT_SUPPORTED;
2134 /****************************************************************
2135 ****************************************************************/
2137 WERROR _netr_DsRAddressToSitenamesW(struct pipes_struct *p,
2138 struct netr_DsRAddressToSitenamesW *r)
2140 p->rng_fault_state = true;
2141 return WERR_NOT_SUPPORTED;
2144 /****************************************************************
2145 ****************************************************************/
2147 WERROR _netr_DsRGetDCNameEx2(struct pipes_struct *p,
2148 struct netr_DsRGetDCNameEx2 *r)
2150 p->rng_fault_state = true;
2151 return WERR_NOT_SUPPORTED;
2154 /****************************************************************
2155 ****************************************************************/
2157 WERROR _netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN(struct pipes_struct *p,
2158 struct netr_NETRLOGONGETTIMESERVICEPARENTDOMAIN *r)
2160 p->rng_fault_state = true;
2161 return WERR_NOT_SUPPORTED;
2164 /****************************************************************
2165 ****************************************************************/
2167 WERROR _netr_NetrEnumerateTrustedDomainsEx(struct pipes_struct *p,
2168 struct netr_NetrEnumerateTrustedDomainsEx *r)
2170 p->rng_fault_state = true;
2171 return WERR_NOT_SUPPORTED;
2174 /****************************************************************
2175 ****************************************************************/
2177 WERROR _netr_DsRAddressToSitenamesExW(struct pipes_struct *p,
2178 struct netr_DsRAddressToSitenamesExW *r)
2180 p->rng_fault_state = true;
2181 return WERR_NOT_SUPPORTED;
2184 /****************************************************************
2185 ****************************************************************/
2187 WERROR _netr_DsrGetDcSiteCoverageW(struct pipes_struct *p,
2188 struct netr_DsrGetDcSiteCoverageW *r)
2190 p->rng_fault_state = true;
2191 return WERR_NOT_SUPPORTED;
2194 /****************************************************************
2195 ****************************************************************/
2197 WERROR _netr_DsrEnumerateDomainTrusts(struct pipes_struct *p,
2198 struct netr_DsrEnumerateDomainTrusts *r)
2200 p->rng_fault_state = true;
2201 return WERR_NOT_SUPPORTED;
2204 /****************************************************************
2205 ****************************************************************/
2207 WERROR _netr_DsrDeregisterDNSHostRecords(struct pipes_struct *p,
2208 struct netr_DsrDeregisterDNSHostRecords *r)
2210 p->rng_fault_state = true;
2211 return WERR_NOT_SUPPORTED;
2214 /****************************************************************
2215 ****************************************************************/
2217 NTSTATUS _netr_ServerTrustPasswordsGet(struct pipes_struct *p,
2218 struct netr_ServerTrustPasswordsGet *r)
2220 p->rng_fault_state = true;
2221 return NT_STATUS_NOT_IMPLEMENTED;
2224 /****************************************************************
2225 ****************************************************************/
2227 WERROR _netr_DsRGetForestTrustInformation(struct pipes_struct *p,
2228 struct netr_DsRGetForestTrustInformation *r)
2230 p->rng_fault_state = true;
2231 return WERR_NOT_SUPPORTED;
2234 /****************************************************************
2235 ****************************************************************/
2237 static NTSTATUS fill_forest_trust_array(TALLOC_CTX *mem_ctx,
2238 struct lsa_ForestTrustInformation *info)
2240 struct lsa_ForestTrustRecord *e;
2241 struct pdb_domain_info *dom_info;
2242 struct lsa_ForestTrustDomainInfo *domain_info;
2244 dom_info = pdb_get_domain_info(mem_ctx);
2245 if (dom_info == NULL) {
2246 return NT_STATUS_NO_MEMORY;
2250 info->entries = talloc_array(info, struct lsa_ForestTrustRecord *, 2);
2251 if (info->entries == NULL) {
2252 return NT_STATUS_NO_MEMORY;
2255 e = talloc(info, struct lsa_ForestTrustRecord);
2257 return NT_STATUS_NO_MEMORY;
2261 e->type = LSA_FOREST_TRUST_TOP_LEVEL_NAME;
2262 e->time = 0; /* so far always 0 in trces. */
2263 e->forest_trust_data.top_level_name.string = talloc_steal(info,
2264 dom_info->dns_forest);
2266 info->entries[0] = e;
2268 e = talloc(info, struct lsa_ForestTrustRecord);
2270 return NT_STATUS_NO_MEMORY;
2273 /* TODO: check if disabled and set flags accordingly */
2275 e->type = LSA_FOREST_TRUST_DOMAIN_INFO;
2276 e->time = 0; /* so far always 0 in traces. */
2278 domain_info = &e->forest_trust_data.domain_info;
2279 domain_info->domain_sid = dom_sid_dup(info, &dom_info->sid);
2281 domain_info->dns_domain_name.string = talloc_steal(info,
2282 dom_info->dns_domain);
2283 domain_info->netbios_domain_name.string = talloc_steal(info,
2286 info->entries[1] = e;
2288 return NT_STATUS_OK;
2291 /****************************************************************
2292 _netr_GetForestTrustInformation
2293 ****************************************************************/
2295 NTSTATUS _netr_GetForestTrustInformation(struct pipes_struct *p,
2296 struct netr_GetForestTrustInformation *r)
2299 struct netlogon_creds_CredentialState *creds;
2300 struct lsa_ForestTrustInformation *info, **info_ptr;
2301 struct loadparm_context *lp_ctx;
2303 /* TODO: check server name */
2305 lp_ctx = loadparm_init_s3(p->mem_ctx, loadparm_s3_context());
2306 if (lp_ctx == NULL) {
2307 DEBUG(0, ("loadparm_init_s3 failed\n"));
2308 return NT_STATUS_INTERNAL_ERROR;
2311 status = schannel_check_creds_state(p->mem_ctx, lp_ctx,
2312 r->in.computer_name,
2314 r->out.return_authenticator,
2316 talloc_unlink(p->mem_ctx, lp_ctx);
2317 if (!NT_STATUS_IS_OK(status)) {
2321 if ((creds->secure_channel_type != SEC_CHAN_DNS_DOMAIN) &&
2322 (creds->secure_channel_type != SEC_CHAN_DOMAIN)) {
2323 return NT_STATUS_NOT_IMPLEMENTED;
2326 info_ptr = talloc(p->mem_ctx, struct lsa_ForestTrustInformation *);
2328 return NT_STATUS_NO_MEMORY;
2330 info = talloc_zero(info_ptr, struct lsa_ForestTrustInformation);
2332 return NT_STATUS_NO_MEMORY;
2335 status = fill_forest_trust_array(p->mem_ctx, info);
2336 if (!NT_STATUS_IS_OK(status)) {
2341 r->out.forest_trust_info = info_ptr;
2343 return NT_STATUS_OK;
2346 /****************************************************************
2347 ****************************************************************/
2349 static NTSTATUS get_password_from_trustAuth(TALLOC_CTX *mem_ctx,
2350 const DATA_BLOB *trustAuth_blob,
2351 const DATA_BLOB *session_key,
2352 struct samr_Password *current_pw_enc,
2353 struct samr_Password *previous_pw_enc)
2355 enum ndr_err_code ndr_err;
2356 struct trustAuthInOutBlob trustAuth;
2358 ndr_err = ndr_pull_struct_blob_all(trustAuth_blob, mem_ctx, &trustAuth,
2359 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
2360 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
2361 return NT_STATUS_UNSUCCESSFUL;
2365 if (trustAuth.count != 0 && trustAuth.current.count != 0 &&
2366 trustAuth.current.array[0].AuthType == TRUST_AUTH_TYPE_CLEAR) {
2367 mdfour(previous_pw_enc->hash,
2368 trustAuth.current.array[0].AuthInfo.clear.password,
2369 trustAuth.current.array[0].AuthInfo.clear.size);
2371 return NT_STATUS_UNSUCCESSFUL;
2374 arcfour_crypt_blob(current_pw_enc->hash, sizeof(current_pw_enc->hash),
2377 if (trustAuth.previous.count != 0 &&
2378 trustAuth.previous.array[0].AuthType == TRUST_AUTH_TYPE_CLEAR) {
2379 mdfour(previous_pw_enc->hash,
2380 trustAuth.previous.array[0].AuthInfo.clear.password,
2381 trustAuth.previous.array[0].AuthInfo.clear.size);
2383 mdfour(previous_pw_enc->hash, NULL, 0);
2385 arcfour_crypt_blob(previous_pw_enc->hash, sizeof(previous_pw_enc->hash),
2388 return NT_STATUS_OK;
2391 /****************************************************************
2392 _netr_ServerGetTrustInfo
2393 ****************************************************************/
2395 NTSTATUS _netr_ServerGetTrustInfo(struct pipes_struct *p,
2396 struct netr_ServerGetTrustInfo *r)
2399 struct netlogon_creds_CredentialState *creds;
2401 size_t account_name_last;
2403 struct netr_TrustInfo *trust_info;
2404 struct pdb_trusted_domain *td;
2405 DATA_BLOB trustAuth_blob;
2406 struct samr_Password *new_owf_enc;
2407 struct samr_Password *old_owf_enc;
2408 DATA_BLOB session_key;
2409 struct loadparm_context *lp_ctx;
2411 lp_ctx = loadparm_init_s3(p->mem_ctx, loadparm_s3_context());
2412 if (lp_ctx == NULL) {
2413 DEBUG(0, ("loadparm_init_s3 failed\n"));
2414 return NT_STATUS_INTERNAL_ERROR;
2417 /* TODO: check server name */
2419 status = schannel_check_creds_state(p->mem_ctx, lp_ctx,
2420 r->in.computer_name,
2422 r->out.return_authenticator,
2424 talloc_unlink(p->mem_ctx, lp_ctx);
2425 if (!NT_STATUS_IS_OK(status)) {
2429 account_name = talloc_strdup(p->mem_ctx, r->in.account_name);
2430 if (account_name == NULL) {
2431 return NT_STATUS_NO_MEMORY;
2434 account_name_last = strlen(account_name);
2435 if (account_name_last == 0) {
2436 return NT_STATUS_INVALID_PARAMETER;
2438 account_name_last--;
2439 if (account_name[account_name_last] == '.') {
2440 account_name[account_name_last] = '\0';
2443 if ((creds->secure_channel_type != SEC_CHAN_DNS_DOMAIN) &&
2444 (creds->secure_channel_type != SEC_CHAN_DOMAIN)) {
2452 account_name_last = strlen(account_name);
2453 if (account_name_last == 0) {
2454 return NT_STATUS_INVALID_PARAMETER;
2456 account_name_last--;
2457 if (account_name[account_name_last] == '$') {
2458 account_name[account_name_last] = '\0';
2461 status = pdb_get_trusted_domain(p->mem_ctx, account_name, &td);
2462 if (!NT_STATUS_IS_OK(status)) {
2466 if (r->out.trust_info != NULL) {
2467 trust_info = talloc_zero(p->mem_ctx, struct netr_TrustInfo);
2468 if (trust_info == NULL) {
2469 return NT_STATUS_NO_MEMORY;
2471 trust_info->count = 1;
2473 trust_info->data = talloc_array(trust_info, uint32_t, 1);
2474 if (trust_info->data == NULL) {
2475 return NT_STATUS_NO_MEMORY;
2477 trust_info->data[0] = td->trust_attributes;
2479 *r->out.trust_info = trust_info;
2482 new_owf_enc = talloc_zero(p->mem_ctx, struct samr_Password);
2483 old_owf_enc = talloc_zero(p->mem_ctx, struct samr_Password);
2484 if (new_owf_enc == NULL || old_owf_enc == NULL) {
2485 return NT_STATUS_NO_MEMORY;
2488 /* TODO: which trustAuth shall we use if we have in/out trust or do they have to
2490 if (td->trust_direction & NETR_TRUST_FLAG_INBOUND) {
2491 trustAuth_blob = td->trust_auth_incoming;
2492 } else if (td->trust_direction & NETR_TRUST_FLAG_OUTBOUND) {
2493 trustAuth_blob = td->trust_auth_outgoing;
2496 session_key.data = creds->session_key;
2497 session_key.length = sizeof(creds->session_key);
2498 status = get_password_from_trustAuth(p->mem_ctx, &trustAuth_blob,
2500 new_owf_enc, old_owf_enc);
2502 if (!NT_STATUS_IS_OK(status)) {
2506 r->out.new_owf_password = new_owf_enc;
2507 r->out.old_owf_password = old_owf_enc;
2509 /* TODO: look for machine password */
2510 r->out.new_owf_password = NULL;
2511 r->out.old_owf_password = NULL;
2513 return NT_STATUS_NOT_IMPLEMENTED;
2516 return NT_STATUS_OK;
2519 /****************************************************************
2520 ****************************************************************/
2522 NTSTATUS _netr_Unused47(struct pipes_struct *p,
2523 struct netr_Unused47 *r)
2525 p->rng_fault_state = true;
2526 return NT_STATUS_NOT_IMPLEMENTED;
2529 /****************************************************************
2530 ****************************************************************/
2532 NTSTATUS _netr_DsrUpdateReadOnlyServerDnsRecords(struct pipes_struct *p,
2533 struct netr_DsrUpdateReadOnlyServerDnsRecords *r)
2535 p->rng_fault_state = true;
2536 return NT_STATUS_NOT_IMPLEMENTED;